Hello everyone.
I need to programmatically place a Popup-Menu (Pulldown) on an arbitrary place
in my custom view, and collect user selection.
I have done this by creating a category for the NSMenu object - like this
// This category adds a generic popup-menu capability to NSMenu.
@interface NSMenu (PopUpRegularMenuAdditions)
- (NSMenuItem *)popUpMenuForView:(NSView *)view at:(NSPoint)location
pullsDown:(BOOL)pullsDown;
@end
@implementation NSMenu (PopUpRegularMenuAdditions)
- (NSMenuItem *)popUpMenuForView:(NSView *)view at:(NSPoint)location
pullsDown:(BOOL)pullsDown
{
NSRect frame = NSMakeRect(location.x, location.y, 32.0f, 32.0f);
if (pullsDown) // add a dummy-first-menu-item on pull-down menus, that will be
hidden.
[self insertItemWithTitle:@"Dummy" action:NULL keyEquivalent:@"" atIndex:0];
NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc]
initTextCell:@"Dummy" pullsDown:pullsDown];
[popUpButtonCell setMenu:self]; // Attach menu to the popupbutton cell
[popUpButtonCell setArrowPosition: NSPopUpArrowAtBottom];
[popUpButtonCell setPreferredEdge:NSMinYEdge];
[popUpButtonCell setFont:[NSFont messageFontOfSize:10.0f]];
[popUpButtonCell setUsesItemFromMenu: NO];
// here is another weirdness -- immediately after I popUpButtonCell
setMenu:self], the selected item index changes to some arbitrary value.
// I therefore reset it here
[popUpButtonCell selectItemAtIndex:-1];
if (!pullsDown)
[popUpButtonCell selectItem:nil];
[popUpButtonCell performClickWithFrame:frame inView:view];
NSInteger resultIndex = [popUpButtonCell indexOfSelectedItem];
NSLog (@"Selected menu item index %d", [popUpButtonCell indexOfSelectedItem]);
NSMenuItem *result = [popUpButtonCell selectedItem];
[popUpButtonCell release];
if (pullsDown)
[self removeItemAtIndex:0]; // remove the dummy-first-menu-item on pull-down
menus.
return result;
}
@end
This works nicely ---- until the menu has submenus. If a user selects an item
that belongs to a submenu --- then
resultIndex = [popUpButtonCell indexOfSelectedItem]; returns -1 (unknown)
result = [popUpButtonCell selectedItem]; returns nil!
How can I resolve this thing? I MUST have popup menus with sub-menus! Is there
a hidden member or method that will tell me what was the last selected menu
item?
Any hint will be appreciated.
Motti Shneor
------------------------------------------
Senior Software Engineer
Waves Audio ltd.
[mailto: [email protected]]
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]