Question: How do I disable a menubar menu in GTK? Is it even possible?

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.

What about Gtk3 API Reference Manual with devhelp?

Might gtk_widget_set_sensitive() help.


Joël

Do you know about Object Orientated Heritage?

Oh, even more API Reference Manual:

A widget like GtkMenu does in general inherit from GtkWidget and other types.

Hi, I am not using widgets, this is a GLib application menu (as mentioned in the OP).

You don’t change the GMenu or GMenuItem: those only describe the menu structure, and the actions associated to each menu.

If you want to enable or disable a menu item, you have to use the GAction:enabled property on the action tied to the menu item.

More information about GAction is available on the wiki.

@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).

@reduz Have you tried setting the submenu-action attribute to the menu item, and toggle the enabled state on it?

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:

  1. a separator
  2. a plain menu item
  3. 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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.