glibmm 2.80.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Symbols | List of all members
Gio::MenuModel Class Reference

MenuModel - An abstract class representing the contents of a menu. More...

#include <giomm/menumodel.h>

Inheritance diagram for Gio::MenuModel:
Inheritance graph
[legend]

Public Types

enum class  Attribute {
  Attribute::ACTION ,
  Attribute::LABEL ,
  Attribute::TARGET ,
  Attribute::ACTION_NAMESPACE ,
  Attribute::ICON
}
 Attributes in Gio::MenuModel::get_item_attribute(). More...
 
enum class  Link {
  Link::SECTION ,
  Link::SUBMENU
}
 Link types in Gio::MenuModel::get_item_link(). More...
 
- Public Types inherited from Glib::Object
using DestroyNotify = void(*)(gpointer data)
 

Public Member Functions

 MenuModel (MenuModel &&src) noexcept
 
MenuModeloperator= (MenuModel &&src) noexcept
 
 ~MenuModel () noexcept override
 
GMenuModelgobj ()
 Provides access to the underlying C GObject.
 
const GMenuModelgobj () const
 Provides access to the underlying C GObject.
 
GMenuModelgobj_copy ()
 Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
 
Glib::VariantBase get_item_attribute (int item_index, Attribute attribute, const Glib::VariantType &expected_type) const
 Queries the item at position item_index in model for the attribute specified by attribute.
 
Glib::RefPtr< MenuModelget_item_link (int item_index, Link link)
 Queries the item at position item_index in model for the link specified by link.
 
Glib::RefPtr< const MenuModelget_item_link (int item_index, Link link) const
 Queries the item at position item_index in model for the link specified by link.
 
bool is_mutable () const
 Queries if model is mutable.
 
int get_n_items () const
 Query the number of items in model.
 
Glib::RefPtr< MenuAttributeIteriterate_item_attributes (int item_index)
 Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.
 
Glib::RefPtr< const MenuAttributeIteriterate_item_attributes (int item_index) const
 Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.
 
Glib::RefPtr< MenuLinkIteriterate_item_links (int item_index)
 Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.
 
Glib::RefPtr< const MenuLinkIteriterate_item_links (int item_index) const
 Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.
 
void items_changed (int position, int removed, int added)
 Requests emission of the MenuModel::signal_items_changed() signal on model.
 
Glib::SignalProxy< void(int, int, int)> signal_items_changed ()
 
- Public Member Functions inherited from Glib::Object
 Object (const Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&src) noexcept
 
Objectoperator= (Object &&src) noexcept
 
voidget_data (const QueryQuark & key)
 
void set_data (const Quark & key, void *data)
 
void set_data_with_c_callback (const Quark & key, void *data, GDestroyNotify notify)
 
void set_data (const Quark & key, void *data, DestroyNotify notify)
 Prefer set_data_with_c_callback() with a callback with C linkage.
 
void remove_data (const QueryQuark &quark)
 
voidsteal_data (const QueryQuark &quark)
 
- Public Member Functions inherited from Glib::ObjectBase
 ObjectBase (const ObjectBase &)=delete
 
ObjectBaseoperator= (const ObjectBase &)=delete
 
void set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value)
 You probably want to use a specific property_*() accessor method instead.
 
void get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
void set_property (const Glib::ustring & property_name, const PropertyType & value)
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
void get_property (const Glib::ustring & property_name, PropertyType & value) const
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
PropertyType get_property (const Glib::ustring & property_name) const
 You probably want to use a specific property_*() accessor method instead.
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void()> &slot)
 You can use the signal_changed() signal of the property proxy instead.
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, sigc::slot< void()> &&slot)
 You can use the signal_changed() signal of the property proxy instead.
 
void freeze_notify ()
 Increases the freeze count on object.
 
void thaw_notify ()
 Reverts the effect of a previous call to freeze_notify().
 
virtual void reference () const
 Increment the reference count for this object.
 
virtual void unreference () const
 Decrement the reference count for this object.
 
GObject * gobj ()
 Provides access to the underlying C GObject.
 
const GObject * gobj () const
 Provides access to the underlying C GObject.
 
GObject * gobj_copy () const
 Give a ref-ed copy to someone. Use for direct struct access.
 

Static Public Member Functions

static GType get_type ()
 Get the GType for this class, for use with the underlying GObject type system.
 

Protected Member Functions

 MenuModel ()
 
- Protected Member Functions inherited from Glib::Object
 Object ()
 
 Object (const Glib::ConstructParams &construct_params)
 
 Object (GObject *castitem)
 
 ~Object () noexcept override
 
- Protected Member Functions inherited from Glib::ObjectBase
 ObjectBase ()
 This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor.
 
 ObjectBase (const char *custom_type_name)
 A derived constructor always overrides this choice.
 
 ObjectBase (const std::type_info &custom_type_info)
 This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually.
 
 ObjectBase (ObjectBase &&src) noexcept
 
ObjectBaseoperator= (ObjectBase &&src) noexcept
 
virtual ~ObjectBase () noexcept=0
 
void initialize (GObject *castitem)
 
void initialize_move (GObject *castitem, Glib::ObjectBase *previous_wrapper)
 

Related Symbols

(Note that these are not member symbols.)

Glib::RefPtr< Gio::MenuModelwrap (GMenuModel *object, bool take_copy=false)
 A Glib::wrap() method for this object.
 

Detailed Description

MenuModel - An abstract class representing the contents of a menu.

MenuModel 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 MenuModel is hierarchical: sections and submenus are again represented by MenuModels. Menus themselves do not define their own roles. Rather, the role of a particular MenuModel 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).

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 Gio::DBus::Connection::export_action_group() and the Gio::DBus::Connection::export_menu_model() do for ActionGroup and MenuModel. The client-side counterparts to make use of the exported information are Gio::DBus::ActionGroup and Gio::DBus::MenuModel.

The API of MenuModel is very generic, with iterators for the attributes and links of an item, see iterate_item_attributes() and iterate_item_links(). The 'standard' attributes and link types have predefined names, listed in the enums Gio::MenuModel::Attribute and Gio::MenuModel::Link.

Items in a MenuModel 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 Gio::MenuModel::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 Gtk::Application, 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:

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.

See the C API docs for a graphical example.

Since glibmm 2.32:

Constructor & Destructor Documentation

◆ MenuModel() [1/2]

Gio::MenuModel::MenuModel ( MenuModel &&  src)
noexcept

◆ ~MenuModel()

Gio::MenuModel::~MenuModel ( )
overridenoexcept

◆ MenuModel() [2/2]

Gio::MenuModel::MenuModel ( )
protected

Member Function Documentation

◆ get_item_attribute()

Glib::VariantBase Gio::MenuModel::get_item_attribute ( int  item_index,
Attribute  attribute,
const Glib::VariantType expected_type 
) const

Queries the item at position item_index in model for the attribute specified by attribute.

If expected_type is non-nullptr then it specifies the expected type of the attribute. If it is nullptr 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 nullptr is returned.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
attributeThe attribute to query.
expected_typeThe expected type of the attribute, or nullptr.
Returns
The value of the attribute.

◆ get_item_link() [1/2]

Glib::RefPtr< MenuModel > Gio::MenuModel::get_item_link ( int  item_index,
Link  link 
)

Queries the item at position item_index in model for the link specified by link.

If the link exists, the linked MenuModel is returned. If the link does not exist, nullptr is returned.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
linkThe link to query.
Returns
The linked MenuModel, or nullptr.

◆ get_item_link() [2/2]

Glib::RefPtr< const MenuModel > Gio::MenuModel::get_item_link ( int  item_index,
Link  link 
) const

Queries the item at position item_index in model for the link specified by link.

If the link exists, the linked MenuModel is returned. If the link does not exist, nullptr is returned.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
linkThe link to query.
Returns
The linked MenuModel, or nullptr.

◆ get_n_items()

int Gio::MenuModel::get_n_items ( ) const

Query the number of items in model.

Since glibmm 2.32:
Returns
The number of items.

◆ get_type()

static GType Gio::MenuModel::get_type ( )
static

Get the GType for this class, for use with the underlying GObject type system.

◆ gobj() [1/2]

GMenuModel * Gio::MenuModel::gobj ( )
inline

Provides access to the underlying C GObject.

◆ gobj() [2/2]

const GMenuModel * Gio::MenuModel::gobj ( ) const
inline

Provides access to the underlying C GObject.

◆ gobj_copy()

GMenuModel * Gio::MenuModel::gobj_copy ( )

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

◆ is_mutable()

bool Gio::MenuModel::is_mutable ( ) const

Queries if model is mutable.

An immutable MenuModel will never emit the MenuModel::signal_items_changed() signal. Consumers of the model may make optimisations accordingly.

Since glibmm 2.32:
Returns
true if the model is mutable (ie: "items-changed" may be emitted).

◆ items_changed()

void Gio::MenuModel::items_changed ( int  position,
int  removed,
int  added 
)

Requests emission of the MenuModel::signal_items_changed() signal on model.

This function should never be called except by MenuModel 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 MenuModel API. Said another way: the menu must not change while user code is running without returning to the mainloop.

Since glibmm 2.32:
Parameters
positionThe position of the change.
removedThe number of items removed.
addedThe number of items added.

◆ iterate_item_attributes() [1/2]

Glib::RefPtr< MenuAttributeIter > Gio::MenuModel::iterate_item_attributes ( int  item_index)

Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.

You must free the iterator with Glib::object_unref() when you are done.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
Returns
A new MenuAttributeIter.

◆ iterate_item_attributes() [2/2]

Glib::RefPtr< const MenuAttributeIter > Gio::MenuModel::iterate_item_attributes ( int  item_index) const

Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.

You must free the iterator with Glib::object_unref() when you are done.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
Returns
A new MenuAttributeIter.

◆ iterate_item_links() [1/2]

Glib::RefPtr< MenuLinkIter > Gio::MenuModel::iterate_item_links ( int  item_index)

Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.

You must free the iterator with Glib::object_unref() when you are done.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
Returns
A new MenuLinkIter.

◆ iterate_item_links() [2/2]

Glib::RefPtr< const MenuLinkIter > Gio::MenuModel::iterate_item_links ( int  item_index) const

Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.

You must free the iterator with Glib::object_unref() when you are done.

Since glibmm 2.32:
Parameters
item_indexThe index of the item.
Returns
A new MenuLinkIter.

◆ operator=()

MenuModel & Gio::MenuModel::operator= ( MenuModel &&  src)
noexcept

◆ signal_items_changed()

Glib::SignalProxy< void(int, int, int)> Gio::MenuModel::signal_items_changed ( )
Slot Prototype:
void on_my_items_changed(int position, int removed, int added)

Flags: Run Last

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.

Parameters
positionThe position of the change.
removedThe number of items removed.
addedThe number of items added.

Friends And Related Symbol Documentation

◆ wrap()

Glib::RefPtr< Gio::MenuModel > wrap ( GMenuModel object,
bool  take_copy = false 
)
related

A Glib::wrap() method for this object.

Parameters
objectThe C instance.
take_copyFalse if the result should take ownership of the C instance. True if it should take a new copy or ref.
Returns
A C++ instance that wraps this C instance.