简体   繁体   中英

Is there a way to control the NSMenuItem item that is initially highlighted when opening a menu?

I'm working on the details of a symbols pop up button, similar to what Xcode 3 at the top of its editor window.

My controller object is the delegate of the NSMenu that is shown when the NSPopUpButton is shown. I implement the two methods numberOfItemsInMenu: and menu:updateItem:atIndex:shouldCancel: to populate the menu right before it's about to be displayed.

However, the title and image of the selected NSMenuItem have to change each time the user changes the selection in the editor NSTextView , or makes changes to the text; just as is the case with Xcode.

The problem I'm having is when the user goes to click on the NSPopUpButton to display the NSMenu , the selected NSMenuItem and the item that should be selected do not match up, since the menu doesn't have the proper number of items yet.

I'm wondering if there is a way to control which NSMenuItem is initially highlighted and tracked when the user clicks to open the menu. As it stands, the first item it always highlighted and tracked or, if the user had previously selected a item, that item is highlighted and tracked.

I tried explaining as best I could, but here is a image illustrating my problem:

http://imgur.com/izGvh

I want the highlighted item to be the same as the selected item when the user opens the menu. Any ideas?

I would use a NSPopupbutton - it can do what you want. maybe you even hide it?

I am not very sure that I understood your problem but If we can add tags to these menu Items. eg

[mMenuItemOne setTag : 1];
[mMenuItemTwo setTag : 2];
[mMenuItemThree setTag : 3];
[mMenuItemFour setTag : 4];}

we can select any Menu item using [_popUp selectItemWithTag: _selectedItem];

Have you tried this NSMenuDelegate method: Handling Highlighting

– menu:willHighlightItem:

NSMenuDelegate Protocol Reference

Also you can store the NSMenuItem index in some var to keep track of the selected item for later use.

Just a hint not a full solution.

Try to post an NSMouseMoved event to your app right after the menu shown. Main problem is here to detect the position of the item you want to be highlighted. Just a starting point.

I'm not sure, do you want to move the mouse selection highlight (the blue stuff) or the checkmark?

In general, the checkmark is the thing you want to change. You can do so from your validateMenuItem: method (or at any other time if your item is set not to take part in validation) using the -setState: method.

The blue highlight is an indicator of the user's keyboard input or mouse location, and you should not mess with it. Changing it does not make any sense, as it would be changed back the moment the user moves the mouse even a single pixel.

That said, you can set the selectedItem of the NSPopUpButton , which will cause the entire menu to be moved so the selectedItem is under the mouse (assuming the mouse just clicked the popup button).

If your menu delegate method is called after the pop up button has decided what item to select, you can't use it. Instead, you could probably set the selectedItem and menu of the popup button from a NSPopUpButtonWillPopUpNotification handler.

If that is also called too late, you'll probably have to subclass NSPopupButtonCell and do it in an override of -attachPopUpWithFrame:inView: (I think that's the spot that should also work when you don't click and just hit the space key while the popup button is selected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM