Modifying a menu on the fly?


I would like to modify a bevel button’s menu when a key is up/down and the menu is being shown. For instance, when the mouse is over a given menu item and the user presses the option key, I’d like to strike off the menu item (and show the menu back to normal when the user releases the key).

I’ve searched in various MBS classes, like CarbonApplicationEventsMBS, which only give notifications before the menu appears (e.g. MenuPopulated event). I’m lost in this jungle of Cocoa classes.

Any idea for receiving events when a menu is being tracked?
Or, else, how can I find globally if a menu is being shown on screen?

I don’t think you can do anything that sophisticated. You can use the Alternate property on a NSMenu to alternate between two menu items but you cannot limit it to the menu item under the mouse.

I think you will have to come up with an alternative UI.

I recognise I was asking something big. But with MacOS, there are often events/methods forming mechanics that are rarely used and not largely known; I was hoping one might exist.

My menu is going to hold around 20 items, and it’s shown in a bevel button; these are two constraints against defining keyboard shortcuts, sadly (and I assume, albeit I’ve not tried, that I can’t just add extra menu items with just “option” as the shortcut, to have alternate items).

Statically, I’m sure. Dynamically, you convinced me :wink:

Won’t be easy though. The expected way a bevel button should react when clicked on is still to show a menu, so that’s the starting point, even for the alternatives.
I’ll try others ideas anyway.

Thank you.

Everything I’ve read seems to indicate that menus are implemented low level and don’t allow the kind of event handling that other controls have.

If you want to put the effort in you could build your own pop up menu style control which could then do anything you want. We needed a custom WYSIWYG font menu that was invoked from a combo box control so we built one using OverlayMBS + other bits and bobs.

I have created dynamic menus that will show different things based on current settings or happenings in the app. It’s not all that difficult. You can create a new menu item and make it dynamic, add checkboxes, etc.

Oh wait - I read the original post again. You want to change it while it is being shown. Ah. That I have not tried. From what I have seen you have to create the menu item and then show it. I suppose you could create a second menu item and then show it on top of the original one. I don’t know if that would work or not.

Obviously what you want to do can be done on a Mac - just look at the GO menu in the Finder when pressing the command key to show your library as an option. Just not sure if Xojo supports that…

Thanks for the idea, but I tend to avoid non-native looking UI (well, we could argue whether my idea is “native” as well).

Not sure for a combobox, but you can already achieve that with a popup menu and the MBS plugin (you can receive an event right before any menu pops up, including the one from popup menu controls (using CarbonApplicationEventMBS class IIRC), grab the underlying NSMenu and change the font of each NSMenuItem in that event). But combobox don’t use real menus.

Yes, that’s the solution I’ve finally opted for. Not quite elegant, as every menu item gets an indicator for the action, while only one item may be chosen, but I guess better isn’t possible.

I very doubt it. I’ve seen times where bugs were attempting to show an additional menu (in various applications), but the former would always dismiss.
Even if that’s possible, that wouldn’t suite my needs. I need to show some kind of indicator for removing, modifying or adding an item in the menu.
I think my solution is intuitive enough so I can move on other parts of the app.

Thanks for your advices!

Altering menus, while displayed based upon keyboard activity is most certainly a standard thing to do:

  • Open Safari
  • Click the file menu and leave it open
  • Toggle the option key up and down.

Several menu items switch their text and shortcuts when down. I’ve seen other apps do the same sort of thing. So you are not asking for something unusual.

If you put the effort in you can make it near enough native that no-one would care.

Changing the font of a menu item is easy but we wanted a much more sophisticated menu that could be filtered via the keyboard while open and allowed the items to be expanded to show the font family styles.

Yes, I’m aware of this (those are alternate menu items). The key here is, they have keyboard shortcuts (and alternate items must have the same key to work (ignoring modifiers): cmd-O and cmd-shift-O will work for grouped items, while cmd-O and cmd-shift-R or cmd-O and cmd-R won’t; in addition, they must follow each other).

Edit: I’ve just discovered, while writing my answer (which I’ll leave as is, as it contains information that may be worth for others), that contextual menus also support alternate items, albeit they haven’t keyboard shortcuts (tried in the Finder). I’m surprised and this proves my request is doable; I just don’t know how. In my knowledge, what allows grouping menu items together is the modifier keys being different (else, the OS has no chance in knowing how to alternate between a group).

I thought so when I started this thread; went to not believing it anymore when following the thread and your reply (the one I’m quoting) made me reconsidering my reconsideration :wink:
I thank you for that.

I’ve yet to find an app where this behaviour occurs in a popup menu or bevel button, but it sounds like it should be doable, and expectable.

I’d say it depends on your target and your own knowledge. Take an open dialog, for example. There are a ton of hidden features (command-D to move to the desktop, command-up to move one folder up, command-. to show/hide invisible files, dragging a folder into the dialog to move to it). The command-. trick, I know it since around one year ago, while I’m sure other users knew about it. On the other hand, I’m sure other users don’t know them (or partially).
I’ve never seen them documented in one place, but I know I hate customised dialogs which don’t support these shortcuts I use everyday. And that’s the same for menus: command-down to move to the last item, typing chars to select, etc.). You probably have thought of them (or you had not), but that’s a lot to add just to work around the initial issue.

Agreed; you made the right choice.


The SelectedRow property is updated while the menu is being operated. You can use a timer to check its value and maybe perform your desired actions.