The MenuItem widget and the derived widgets are the only valid children for menus.
Their function is to correctly handle highlighting, alignment, events and submenus.
As a GtkMenuItem derives from Bin it can hold any valid child widget, although only a
few are really useful.
By default, a GtkMenuItem sets a AccelLabel as its child. GtkMenuItem has direct
functions to set the label and its mnemonic. For more advanced label settings, you can fetch the child widget from the GtkBin.
An example for setting markup and accelerator on a MenuItem:
GtkWidget *menu_item = gtk_menu_item_new_with_label ("Example Menu Item");
GtkWidget *child = gtk_bin_get_child (GTK_BIN (menu_item));
gtk_label_set_markup (GTK_LABEL (child), "<i>new label</i> with <b>markup</b>");
gtk_accel_label_set_accel (GTK_ACCEL_LABEL (child), GDK_KEY_1, 0);
GtkMenuItem as GtkBuildable
The GtkMenuItem implementation of the Buildable interface supports adding a submenu by
specifying “submenu” as the “type” attribute of a `<child>` element.
An example of UI definition fragment with submenus:
<object class="GtkMenuItem">
<child type="submenu">
<object class="GtkMenu"/>
</child>
</object>
CSS nodes
menuitem
├── <child>
╰── [arrow.right]
m has a single CSS node with
name menuitem. If the menuitem has a submenu, it gets another CSS node with name arrow, which has the .left or .right style class.
- public unowned string? get_accel_path ()
Retrieve the accelerator path that was previously set on
this.
- public virtual unowned string get_label ()
Sets text
on the this
label
- public bool get_reserve_indicator ()
Returns whether the this reserves
space for the submenu indicator, regardless if it has a submenu or not.
- public bool get_right_justified ()
Gets whether the menu item appears justified at the right side of the
menu bar.
- public unowned Widget? get_submenu ()
Gets the submenu underneath this menu item, if any.
- public bool get_use_underline ()
Checks if an underline in the text indicates the next character should
be used for the mnemonic accelerator key.
- public void set_accel_path (string? accel_path)
Set the accelerator path on this,
through which runtime changes of the menu item’s accelerator caused by the user can be identified and saved to persistent storage (
see save on this).
- public virtual void set_label (string label)
Sets text
on the this
label
- public void set_reserve_indicator (bool reserve)
Sets whether the this should reserve
space for the submenu indicator, regardless if it actually has a submenu or not.
- public void set_right_justified (bool right_justified)
Sets whether the menu item appears justified at the right side of a
menu bar.
- public void set_submenu (Menu? submenu)
Sets or replaces the menu item’s submenu, or removes it when a
null submenu is passed.
- public void set_use_underline (bool setting)
If true, an underline in the text indicates the next character should
be used for the mnemonic accelerator key.