Next: <GMountOperation>, Previous: <GMenuLinkIter>, Up: Classes   [Index]


1.56 <GMenuModel> ¶

Class: <GMenuModel> ¶

It derives from <GObject>.

<GMenuModel> represents the contents of a menu — an ordered list of menu items. The items are associated with actions, which can be activated through them. Items can be grouped in sections, and may have submenus associated with them. Both items and sections usually have some representation data, such as labels or icons. The type of the associated action (ie whether it is stateful, and what kind of state it has) can influence the representation of the item.

The conceptual model of menus in <GMenuModel> is hierarchical: sections and submenus are again represented by <GMenuModel>s. Menus themselves do not define their own roles. Rather, the role of a particular <GMenuModel> is defined by the item that references it (or, in the case of the ‘root’ menu, is defined by the context in which it is used).

As an example, consider the visible portions of this menu:

An example menu ¶

![](menu-example.png)

While this kind of deeply nested menu is no longer considered good UI practice, it serves as a good example of the concepts in <GMenuModel>. There are 8 ‘menus’ visible in the screenshot: one menubar, two submenus and 5 sections:

  • the toplevel menubar (containing 4 items)
  • the View submenu (containing 3 sections)
  • the first section of the View submenu (containing 2 items)
  • the second section of the View submenu (containing 1 item)
  • the final section of the View submenu (containing 1 item)
  • the Highlight Mode submenu (containing 2 sections)
  • the Sources section (containing 2 items)
  • the Markup section (containing 2 items)

The example illustrates the conceptual connection between these 8 menus. Each large block in the figure represents a menu and the smaller blocks within the large block represent items in that menu. Some items contain references to other menus.

A menu example ¶

Notice that the separators visible in the example appear nowhere in the menu model. This is because separators are not explicitly represented in the menu model. Instead, a separator is inserted between any two non-empty sections of a menu. Section items can have labels just like any other item. In that case, a display system may show a section header instead of a separator.

The motivation for this abstract model of application controls is that modern user interfaces tend to make these controls available outside the application. Examples include global menus, jumplists, dash boards, etc. To support such uses, it is necessary to ‘export’ information about actions and their representation in menus, which is exactly what the action group exporter and the menu model exporter do for <GActionGroup> and <GMenuModel>. The client-side counterparts to make use of the exported information are <GDBusActionGroup> and <GDBusMenuModel>.

The API of <GMenuModel> is very generic, with iterators for the attributes and links of an item, see menu-model:iterate-item-attributes and menu-model:iterate-item-links. The ‘standard’ attributes and link types have predefined names: G_MENU_ATTRIBUTE_LABEL, G_MENU_ATTRIBUTE_ACTION, G_MENU_ATTRIBUTE_TARGET, G_MENU_LINK_SECTION and G_MENU_LINK_SUBMENU.

Items in a <GMenuModel> represent active controls if they refer to an action that can get activated when the user interacts with the menu item. The reference to the action is encoded by the string ID in the G_MENU_ATTRIBUTE_ACTION attribute. An action ID uniquely identifies an action in an action group. Which action group(s) provide actions depends on the context in which the menu model is used. E.g. when the model is exported as the application menu of a ‘GtkApplication‘, actions can be application-wide or window-specific (and thus come from two different action groups). By convention, the application-wide actions have names that start with app., while the names of window-specific actions start with win..

While a wide variety of stateful actions is possible, the following is the minimum that is expected to be supported by all users of exported menu information:

  • an action with no parameter type and no state
  • an action with no parameter type and boolean state
  • an action with string parameter type and string state

Stateless ¶

A stateless action typically corresponds to an ordinary menu item.

Selecting such a menu item will activate the action (with no parameter).

Boolean State ¶

An action with a boolean state will most typically be used with a ‘toggle’ or ‘switch’ menu item. The state can be set directly, but activating the action (with no parameter) results in the state being toggled.

Selecting a toggle menu item will activate the action. The menu item should be rendered as ‘checked’ when the state is true.

String Parameter and State ¶

Actions with string parameters and state will most typically be used to represent an enumerated choice over the items available for a group of radio menu items. Activating the action with a string parameter is equivalent to setting that parameter as the state.

Radio menu items, in addition to being associated with the action, will have a target value. Selecting that menu item will result in activation of the action with the target value as the parameter. The menu item should be rendered as ‘selected’ when the state of the action is equal to the target value of the menu item.

Fields ¶

parent-instance
priv

Procedures ¶

Method: menu-model:get-item-attribute-value self item-index attribute [expected-type] ⇒ %return ¶
Method: get-item-attribute-value self item-index attribute [expected-type] ⇒ %return ¶

Queries the item at position item-index in self for the attribute specified by attribute.

If expected-type is non-#f then it specifies the expected type of the attribute. If it is #f then any type will be accepted.

If the attribute exists and matches expected-type (or if the expected type is unspecified) then the value is returned.

If the attribute does not exist, or does not match the expected type then #f is returned.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

attribute

Passed: a string.

the attribute to query

expected-type

Passed, or left out: a <GVariantType>, or #f.

the expected type of the attribute, or #f

%return

Returned: a <GVariant>, or #f.

the value of the attribute

Method: menu-model:get-item-link self item-index link ⇒ %return ¶
Method: get-item-link self item-index link ⇒ %return ¶

Queries the item at position item-index in self for the link specified by link.

If the link exists, the linked <GMenuModel> is returned. If the link does not exist, #f is returned.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

link

Passed: a string.

the link to query

%return

Returned: a <GMenuModel>, or #f.

the linked <GMenuModel>, or #f

Method: menu-model:get-n-items self ⇒ %return ¶
Method: get-n-items self ⇒ %return ¶

Query the number of items in self.

self

Passed: a <GMenuModel>.

a <GMenuModel>

%return

Returned: a 32-bit integer.

the number of items

Method: menu-model:is-mutable? self ⇒ %return ¶
Method: is-mutable? self ⇒ %return ¶

Queries if self is mutable.

An immutable <GMenuModel> will never emit the menu-model:items-changed signal. Consumers of the model may make optimisations accordingly.

self

Passed: a <GMenuModel>.

a <GMenuModel>

%return

Returned: a boolean.

#t if the model is mutable (ie: "items-changed" may be emitted).

Method: menu-model:items-changed self position removed added ¶
Method: items-changed self position removed added ¶

Requests emission of the menu-model:items-changed signal on self.

This function should never be called except by <GMenuModel> subclasses. Any other calls to this function will very likely lead to a violation of the interface of the model.

The implementation should update its internal representation of the menu before emitting the signal. The implementation should further expect to receive queries about the new state of the menu (and particularly added menu items) while signal handlers are running.

The implementation must dispatch this call directly from a mainloop entry and not in response to calls – particularly those from the <GMenuModel> API. Said another way: the menu must not change while user code is running without returning to the mainloop.

self

Passed: a <GMenuModel>.

a <GMenuModel>

position

Passed: a 32-bit integer.

the position of the change

removed

Passed: a 32-bit integer.

the number of items removed

added

Passed: a 32-bit integer.

the number of items added

Method: menu-model:iterate-item-attributes self item-index ⇒ %return ¶
Method: iterate-item-attributes self item-index ⇒ %return ¶

Creates a <GMenuAttributeIter> to iterate over the attributes of the item at position item-index in self.

You must free the iterator with object:unref when you are done.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

%return

Returned: a <GMenuAttributeIter>.

a new <GMenuAttributeIter>

Method: menu-model:iterate-item-links self item-index ⇒ %return ¶
Method: iterate-item-links self item-index ⇒ %return ¶

Creates a <GMenuLinkIter> to iterate over the links of the item at position item-index in self.

You must free the iterator with object:unref when you are done.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

%return

Returned: a <GMenuLinkIter>.

a new <GMenuLinkIter>

Signals ¶

Signal: menu-model:items-changed ¶

Connect a handler with (connect object menu-model:items-changed handler). It is called as (handler self position removed added).

Emitted when a change has occurred to the menu.

The only changes that can occur to a menu is that items are removed or added. Items may not change (except by being removed and added back in the same location). This signal is capable of describing both of those changes (at the same time).

The signal means that starting at the index position, removed items were removed and added items were added in their place. If removed is zero then only items were added. If added is zero then only items were removed.

As an example, if the menu contains items a, b, c, d (in that order) and the signal (2, 1, 3) occurs then the new composition of the menu will be a, b, \_, \_, \_, d (with each _ representing some new item).

Signal handlers may query the model (particularly the added items) and expect to see the results of the modification that is being reported. The signal is emitted after the modification.

self

Passed: a <GMenuModel>.

position

Passed: a 32-bit integer.

the position of the change

removed

Passed: a 32-bit integer.

the number of items removed

added

Passed: a 32-bit integer.

the number of items added

Virtual methods ¶

Virtual method: menu-model:do-get-item-attribute-value implementor self item-index attribute [expected-type] ⇒ %return ¶

Queries the item at position item-index in self for the attribute specified by attribute.

If expected-type is non-#f then it specifies the expected type of the attribute. If it is #f then any type will be accepted.

If the attribute exists and matches expected-type (or if the expected type is unspecified) then the value is returned.

If the attribute does not exist, or does not match the expected type then #f is returned.

To override it in a new class, give register-type #:vfuncs `((get-item-attribute-value . ,procedure)). The procedure is called as (procedure self item-index attribute expected-type) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

attribute

Passed: a string.

the attribute to query

expected-type

Passed, or left out: a <GVariantType>, or #f.

the expected type of the attribute, or #f

%return

Returned: a <GVariant>, or #f.

the value of the attribute

Virtual method: menu-model:do-get-item-attributes implementor self item-index ⇒ attributes ¶

Gets all the attributes associated with the item in the menu model.

To override it in a new class, give register-type #:vfuncs `((get-item-attributes . ,procedure)). The procedure is called as (procedure self item-index) ⇒ attributes.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

the <GMenuModel> to query

item-index

Passed: a 32-bit integer.

The <GMenuItem> to query

attributes

Returned: a hash table, each key a string and each value a <GVariant>.

Attributes on the item

Virtual method: menu-model:do-get-item-link implementor self item-index link ⇒ %return ¶

Queries the item at position item-index in self for the link specified by link.

If the link exists, the linked <GMenuModel> is returned. If the link does not exist, #f is returned.

To override it in a new class, give register-type #:vfuncs `((get-item-link . ,procedure)). The procedure is called as (procedure self item-index link) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

link

Passed: a string.

the link to query

%return

Returned: a <GMenuModel>, or #f.

the linked <GMenuModel>, or #f

Virtual method: menu-model:do-get-item-links implementor self item-index ⇒ links ¶

Gets all the links associated with the item in the menu model.

To override it in a new class, give register-type #:vfuncs `((get-item-links . ,procedure)). The procedure is called as (procedure self item-index) ⇒ links.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

the <GMenuModel> to query

item-index

Passed: a 32-bit integer.

The <GMenuItem> to query

links

Returned: a hash table, each key a string and each value a <GMenuModel>.

Links from the item

Virtual method: menu-model:do-get-n-items implementor self ⇒ %return ¶

Query the number of items in self.

To override it in a new class, give register-type #:vfuncs `((get-n-items . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

%return

Returned: a 32-bit integer.

the number of items

Virtual method: menu-model:do-is-mutable implementor self ⇒ %return ¶

Queries if self is mutable.

An immutable <GMenuModel> will never emit the menu-model:items-changed signal. Consumers of the model may make optimisations accordingly.

To override it in a new class, give register-type #:vfuncs `((is-mutable . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

%return

Returned: a boolean.

#t if the model is mutable (ie: "items-changed" may be emitted).

Virtual method: menu-model:do-iterate-item-attributes implementor self item-index ⇒ %return ¶

Creates a <GMenuAttributeIter> to iterate over the attributes of the item at position item-index in self.

You must free the iterator with object:unref when you are done.

To override it in a new class, give register-type #:vfuncs `((iterate-item-attributes . ,procedure)). The procedure is called as (procedure self item-index) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

%return

Returned: a <GMenuAttributeIter>.

a new <GMenuAttributeIter>

Virtual method: menu-model:do-iterate-item-links implementor self item-index ⇒ %return ¶

Creates a <GMenuLinkIter> to iterate over the links of the item at position item-index in self.

You must free the iterator with object:unref when you are done.

To override it in a new class, give register-type #:vfuncs `((iterate-item-links . ,procedure)). The procedure is called as (procedure self item-index) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GMenuModel>, or a class that derives from it, or its GType number.

self

Passed: a <GMenuModel>.

a <GMenuModel>

item-index

Passed: a 32-bit integer.

the index of the item

%return

Returned: a <GMenuLinkIter>.

a new <GMenuLinkIter>


Next: <GMountOperation>, Previous: <GMenuLinkIter>, Up: Classes   [Index]