Hold the context menu (NSMenu). Do not allow closing the context menu when clicked anywhere in the window.

I am showing the right-click context menu in my NSTextField. I use the following code to display the context menu in the rightMouseDown event of my NSTextField:

- (void) rightMouseDown:(NSEvent*)theEvent
{
  NSMenu* theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];

  [theMenu insertItemWithTitle:@"Suggest Link/Movie" action:@selector(openSuggestionMovieLink) keyEquivalent:@"" atIndex:0];
  [theMenu setDelegate:self];

  [NSMenu popUpContextMenu:theMenu withEvent:theEvent forView:self];
}

I need to stop closing this context menu when the user clicks anywhere in the window.

I tried to do the same, overriding the mouse event in the window and not sending a call [super mousedown: event] inside it when the context menu is open. This did not work.

I also tried using the delegate method of NSMenu menuDidClose: and again opening the menu in it.

- (void)menuDidClose:(NSMenu *)menu
{
   NSLog(@"close");
   [NSMenu popUpContextMenu:menu withEvent:nil forView:self];
}

But nothing works for me.

Is there any way to do the same. Please point me in the right direction. I would appreciate any help. Thanks.

+4
1

NSMenu

- (void)cancelTracking;
- (void)cancelTrackingWithoutAnimation;

? ?

+1

All Articles