开发者

Looking for popUpMenuPositioningItem:atLocation:inView: equivalent for 10.5

开发者 https://www.devze.com 2022-12-18 23:45 出处:网络
I\'m working on an application that needs to display a context menu on screen in various scenarios. In the function I\'m writing, I don\'t have access to any NSWindows or NSViews. I\'d like to use pop

I'm working on an application that needs to display a context menu on screen in various scenarios. In the function I'm writing, I don't have access to any NSWindows or NSViews. I'd like to use popUpMenuPositioningItem:atLocation:inView as this function works perfectly for me in 10.6. However, we have a requirement to support 10.5, so this function isn't available to me.

The feature I'm most interested in, as stated in the documentation is:

If view is nil, the location is interpreted in the screen coordinate system. This allows you to pop up a me开发者_如何学Gonu disconnected from any window.

Basically, I need to display the context menu given a location on screen, but without any associated view.

Is there any way to achieve this on 10.5?


// Set up the button cell, converting to NSView coordinates. The menu is
// positioned such that the currently selected menu item appears over the
// popup button, which is the expected Mac popup menu behavior.
NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@""
                                                          pullsDown:NO];
[button autorelease];
[button setMenu:menu_];
// We use selectItemWithTag below so if the index is out-of-bounds nothing
// bad happens.
[button selectItemWithTag:index];
[button setFont:[NSFont menuFontOfSize:fontSize_]];

// Create a dummy view to associate the popup with, since the OS will use
// that view for positioning the menu.
NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease];
[view addSubview:dummyView];
NSRect dummyBounds = [dummyView convertRect:bounds fromView:view];

// Display the menu, and set a flag if a menu item was chosen.
[button performClickWithFrame:dummyBounds inView:dummyView];

if ([self menuItemWasChosen])
  index_ = [button indexOfSelectedItem];

[dummyView removeFromSuperview];


I don't know how to do it in Cocoa, but you could maybe use the Carbon function PopUpMenuSelect.

0

精彩评论

暂无评论...
验证码 换一张
取 消