NSMenu does not start tracking
I have a small cocoa application that usually runs in the background (as an agent). Sometimes I would like to open the context menu (neither window nor s.th. is visible at this time).
Since I'm only targeting Snow Leopard, I tried this:
if (windows) {
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"test"] autorelease];
[theMenu setShowsStateColumn:NO];
[theMenu setAutoenablesItems:NO];
for (id item in windows) {
NSString *labelText = @"some text";
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:labelText
action:@selector(menuItemSelected:)
keyEquivalent:@""] autorelease];
[theMenuItem setTarget:self];
[theMenuItem setRepresentedObject:item];
[theMenuItem setEnabled:YES];
[theMenuItem setImage:icon];
[theMenu addItem:theMenuItem];
}
[theMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
}
The menu pops up fine, but if I find items with the mouse cursor, they are not highlighted and I cannot click them.
The menuItemSelected: method looks like this:
-(IBAction)menuItemSelected:(id)sender {
}
Any idea what I am doing wrong?
a source to share
I suspect that the windowing system does not consider your application active and thus does not dispatch mouse events to the menu you created.
As an experiment, try creating a dummy window before the menu appears. I would create NSPanel
perhaps with style NSNonActivatingPanelMask
. makeKeyAndOrderFront:
your window / pane, then popup menu and see what happens.
If that works, I'll stick with the approach and hide the window.
a source to share