I have a menubar (via setting a GMenu to g_application_set_menubar()).
I use a main menu and create the actual sub-menus via g_menu_append_submenu.
I create the menuitem manually and add the submenu to it, then append it. There seems to be simply no way to disable this menu (make it appear disabled, un-clickable). There is zero documentation about this on the internet or official docs.
I find this is a very basic UI operation and don’t quite understand why it is not possible. Any help very welcome.
@ebassi tried it and it works with regular items, but associating an action with items that contain a submenu does not appear to work (or the way to do it is not obvious).
Yes, it just doesn’t work. As much as I try, it remains enabled. Here is code I used:
//create the action and disable it
Glib::RefPtr<Gio::SimpleAction> automation_action = window->add_action_bool("AutomationMenu", false);
automation_action->set_enabled(false);
//create the menu
Glib::RefPtr<Gio::Menu> automation_menu = Gio::Menu::create();
//create the submenu item
Glib::RefPtr<Gio::MenuItem> automation_menu_item = Gio::MenuItem::create("Automation", automation_menu);
//set the "submenu-action" on this submenu item
automation_menu_item->set_attribute_value("submenu-action", Glib::Variant<Glib::ustring>::create("gui.AutomationMenu"));
//tried using gtk directly, but does not work either
//g_menu_item_set_attribute(automation_menu_item->gobj(), "submenu-action", "s", "win.AutomationMenu");
//append the menu to the main one
menu->append_item(automation_menu_item)
To summarise: yes, this seems to be a limitation of the current GMenu → GtkMenuShell tracking code.
When inserting a new menu item from the GMenu into a GtkMenuShell we split into the following 3 cases:
a separator
a plain menu item
a menu item with a sub-menu attached
In case (2) we bind the following properties between the GtkMenuTrackerItem (which tracks the action bound to a GMenuItem) and the GTK menu item widget:
label
icon
sensitive
visible
action-role
toggled
accelerator
Whenever an action is enabled or disabled, the sensitivity of the widget is toggled, so we can disable menu items by disabling the action they map to.
For the case of (3), though, we only track the label property, and we decide whether or not to show the menu by using the hidden-when attribute.
Ideally, it should be possible to also bind the sensitive property to the menu tracker, and controlling the submenu-action of a menu item we could make the menu item as insensitive.