A problem with PopupButtonCell --- submenu selection not available
A problem with PopupButtonCell --- submenu selection not available
- Subject: A problem with PopupButtonCell --- submenu selection not available
- From: Motti Shneor <email@hidden>
- Date: Mon, 5 Jul 2010 07:32:30 -0400
- Acceptlanguage: en-US
- Thread-topic: A problem with PopupButtonCell --- submenu selection not available
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@hidden]
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden