views:

68

answers:

1

Hey,

I have a little cocoa app which usually operates in the background (as agent). Sometimes I'd like to be able to popup a contextmenu (no window or s.th. visible at this time).

As I'm only targetting 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 popsup perfectly but if I hover the items with the mouse cursor they don't highlight and I can't click them.

The menuItemSelected: method looks just like this:

-(IBAction)menuItemSelected:(id)sender {

}

Any idea what I'm doing wrong?

A: 

I suspect that the windowing system doesn't consider your application to be active, and thus doesn't send mouse events to the menu you've created.

As an experiment, try creating a dummy window before popping up the menu. I'd create an NSPanel, possibly with style NSNonActivatingPanelMask. makeKeyAndOrderFront: your window/panel, then pop up the menu and see what happens.

If this works, I'd stick with the approach and hide the window.

Adam Preble
Thanks! I'll try that. For now I created a window that looks and behaves similar to a nsmenu...I already tried creating a nonactivating NSPanel but I think I may have missed makeKeyAndOrderFront.. If I remember correctly I used only orderFront.I'll report if your solution works soon.
Measunny
unfortunately that doesn't seem to work either :-(It works if I add a NSButton to the window, set my menu as the menu of the button and then rightclick the button (menuForEvent is called then) But I think I can't do that programatically either (without having the user rightclick the button... ok sending a mousevent on the button, but thats really ugly)
Measunny