I’m using NSMenuItemMBS objects for adding a status menu.
In the main menu, some items have a submenu, containing items that should be checked at certain times.
So I made a subclass in Xojo and set the menu up. So far, so good.
In order to add/remove the checkmark, I added the validateMenuItem event to the subclass. Surprisingly, this event is correctly called only for the base menu; all contained items in the submenu, though also being of the same subclass, won’t call their validateMenuItem event.
So I’m wondering what is required to make this event being called.
I tried all three (not changing, setting to true, and setting to false), to the NSMenuMBS parent.
Here’s the function where I create the NSMenuMBS (the menu containing the submenu):
Public Sub CreateLocalListSubMenu()
if MyStatusItem<>nil then 'NSStatusItemMBS
Var m As NSMenuMBS=MyStatusItem.menu
if m<>nil then
Var mi As new CConnectionMenuItem //NSMenuItemMBS subclass
mi.Enabled=True
mi.Title="Connexions locales"
mi.submenu=new NSMenuMBS
//mi.submenu.autoenablesItems=True
mi.submenu.autoenablesItems=False
mi.tag=-5
m.addItem mi
LocalListSubMenu=mi //Property of the app class
end if
end if
End Sub
And to add a new item, this is simply so:
Var LocalMenu As NSMenuMBS
if LocalListSubMenu<>nil then LocalMenu=LocalListSubMenu.submenu
if LocalMenu<>nil then
Var mi As new CConnectionMenuItem(Data.DisplayName)
mi.Share=Data
LocalMenu.addItem mi
end if
LocalMenu.addItem mi
The line above adds a CConnectionMenuItem object (subclass of NSMenuItemMBS) to the menu, but then it looses its subclass (the object becomes a plain NSMenuItemMBS), so the subclass events’ won’t work.
I’m not sure it’s the right workaround, but I’ve solved the issue by adding a property to the app class:
SharesMenuItems() as CConnectionMenuItem
and whenever I add a CConnectionMenuItem to a NSSubMenuMBS, I also add it to the property. This has the effect of letting the framework remember that this is a subclass rather than a plain NSMenuItemMBS.