Search code examples
cocoawebkitnsmenu

How Can I Show NSMenu at Mouse Cursor?


I am working on an app where I want to show an NSMenu "context menu" next to the current mouse location. At this point, I know how to trigger the command from the WebView to show the context menu, I just can't seem to get the menu to show up at the correct placement on the screen. It seems like my coordinates are inverted vertically from what I need. This seems like such a simple problem, but I have spent a good bit of time trying to best a "best practices" solution. Do I need to figure out which screen the mouse is in and calculate the y coordinate manually?

Here is my really simple code that gets close:

- (IBAction)showMenu:(id)sender
{
    CGEventRef ourEvent = CGEventCreate(NULL);
    CGPoint point = CGEventGetLocation(ourEvent);
    NSPoint wp = {0,0};
    wp = [self.window convertScreenToBase:point];
    NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
    NSLog(@"Location? x= %f, y = %f", (float)wp.x, (float)wp.y);

    NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:CGPointMake(wp.x, wp.y) modifierFlags:0 timestamp:NSTimeIntervalSince1970 windowNumber:[_window windowNumber]  context:nil eventNumber:0 clickCount:0 pressure:0.1];

    //NSEvent *event = [NSEvent eventWithCGEvent:ourEvent];


    NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];

    [theMenu insertItemWithTitle:@"Item 1" action:@selector(beep:) keyEquivalent:@"" atIndex:0];
    [theMenu insertItemWithTitle:@"Item 2" action:@selector(beep:) keyEquivalent:@"" atIndex:1];

    [NSMenu popUpContextMenu:theMenu withEvent:event forView:nil];
}

Can someone please provide me sample code for the most natural way to get this done.


Solution

  • You are mixing Quartz with Cocoa. There is a lot of overlap between the APIs, and if you can stick with just one, do so. Do this instead:

    NSPoint point = [NSEvent mouseLocation];
    

    Quartz and Cocoa use different coordinate systems: Quartz uses "hardware address" style coordinates with (0, 0) at the top left, Cocoa uses "mathematics" style coordinates with (0, 0) at the bottom left.