Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It appears that popUpContextMenu is already synchronous. Since I didn't see a way to use NSMenu without having it send a notification to an NSView I came up with a scheme that instantiates a temporary NSView. The goal is to display a popup menu and return the selected item in the context of a single function call. Following is code snippets of my proposed solution:</p> <pre><code>// Dummy View class used to receive Menu Events @interface DVFBaseView : NSView { NSMenuItem* nsMenuItem; } - (void) OnMenuSelection:(id)sender; - (NSMenuItem*)MenuItem; @end @implementation DVFBaseView - (NSMenuItem*)MenuItem { return nsMenuItem; } - (void)OnMenuSelection:(id)sender { nsMenuItem = sender; } @end // Calling Code (in response to rightMouseDown event in my main NSView void HandleRButtonDown (NSPoint pt) { NSRect graphicsRect; // contains an origin, width, height graphicsRect = NSMakeRect(200, 200, 50, 100); //----------------------------- // Create Menu and Dummy View //----------------------------- nsMenu = [[[NSMenu alloc] initWithTitle:@"Contextual Menu"] autorelease]; nsView = [[[DVFBaseView alloc] initWithFrame:graphicsRect] autorelease]; NSMenuItem* item = [nsMenu addItemWithTitle:@"Menu Item# 1" action:@selector(OnMenuSelection:) keyEquivalent:@""]; [item setTag:ID_FIRST]; item = [nsMenu addItemWithTitle:@"Menu Item #2" action:@selector(OnMenuSelection:) keyEquivalent:@""]; [item setTag:ID_SECOND]; //--------------------------------------------------------------------------------------------- // Providing a valid windowNumber is key in getting the Menu to display in the proper location //--------------------------------------------------------------------------------------------- int windowNumber = [(NSWindow*)myWindow windowNumber]; NSRect frame = [(NSWindow*)myWindow frame]; NSPoint wp = {pt.x, frame.size.height - pt.y}; // Origin in lower left NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:wp modifierFlags:NSApplicationDefined timestamp: (NSTimeInterval) 0 windowNumber: windowNumber context: [NSGraphicsContext currentContext] subtype:0 data1: 0 data2: 0]; [NSMenu popUpContextMenu:nsMenu withEvent:event forView:nsView]; NSMenuItem* MenuItem = [nsView MenuItem]; switch ([MenuItem tag]) { case ID_FIRST: HandleFirstCommand(); break; case ID_SECOND: HandleSecondCommand(); break; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload