Next: , Previous: GtkRadioToolButton, Up: Top


72 GtkUIManager

Constructing menus and toolbars from an XML description

72.1 Overview

A <gtk-ui-manager> constructs a user interface (menus and toolbars) from one or more UI definitions, which reference actions from one or more action groups.

72.2 UI Definitions

The UI definitions are specified in an XML format which can be roughly described by the following DTD. There are some additional restrictions beyond those specified in the DTD, e.g. every toolitem must have a toolbar in its anchestry and every menuitem must have a menubar or popup in its anchestry. Since a <g-markup> parser is used to parse the UI description, it must not only be valid XML, but valid <g-markup>.

     
     <!ELEMENT ui          (menubar|toolbar|popup|accelerator)* >
     <!ELEMENT menubar     (menuitem|separator|placeholder|menu)* >
     <!ELEMENT menu        (menuitem|separator|placeholder|menu)* >
     <!ELEMENT popup       (menuitem|separator|placeholder|menu)* >
     <!ELEMENT toolbar     (toolitem|separator|placeholder)* >
     <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* >
     <!ELEMENT menuitem     EMPTY >
     <!ELEMENT toolitem     (menu?) >
     <!ELEMENT separator    EMPTY >
     <!ELEMENT accelerator  EMPTY >
     <!ATTLIST menubar      name                  &#x0023;IMPLIED
                            action                &#x0023;IMPLIED >
     <!ATTLIST toolbar      name                  &#x0023;IMPLIED
                            action                &#x0023;IMPLIED >
     <!ATTLIST popup        name                  &#x0023;IMPLIED
                            action                &#x0023;IMPLIED >
     <!ATTLIST placeholder  name                  &#x0023;IMPLIED
                            action                &#x0023;IMPLIED >
     <!ATTLIST separator    name                  &#x0023;IMPLIED
                            action                &#x0023;IMPLIED
                            expand   (true|false) &#x0023;IMPLIED >
     <!ATTLIST menu         name                  &#x0023;IMPLIED
                            action                &#x0023;REQUIRED
                            position (top|bot)    &#x0023;IMPLIED >
     <!ATTLIST menuitem     name                  &#x0023;IMPLIED
                            action                &#x0023;REQUIRED
                            position (top|bot)    &#x0023;IMPLIED >
     <!ATTLIST toolitem     name                  &#x0023;IMPLIED
                            action                &#x0023;REQUIRED
                            position (top|bot)    &#x0023;IMPLIED >
     <!ATTLIST accelerator  name                  &#x0023;IMPLIED
                            action                &#x0023;REQUIRED >

If a name is not specified, it defaults to the action. If an action is not specified either, the element name is used. The name and action attributes must not contain '/' characters after parsing (since that would mess up path lookup) and must be usable as XML attributes when enclosed in doublequotes, thus they must not '"' characters or references to the &quot; entity.

     
     <ui>
       <menubar>
         <menu name="FileMenu" action="FileMenuAction">
           <menuitem name="New" action="New2Action" />
           <placeholder name="FileMenuAdditions" />
         </menu>
         <menu name="JustifyMenu" action="JustifyMenuAction">
           <menuitem name="Left" action="justify-left"/>
           <menuitem name="Centre" action="justify-center"/>
           <menuitem name="Right" action="justify-right"/>
           <menuitem name="Fill" action="justify-fill"/>
         </menu>
       </menubar>
       <toolbar action="toolbar1">
         <placeholder name="JustifyToolItems">
           <separator/>
           <toolitem name="Left" action="justify-left"/>
           <toolitem name="Centre" action="justify-center"/>
           <toolitem name="Right" action="justify-right"/>
           <toolitem name="Fill" action="justify-fill"/>
           <separator/>
         </placeholder>
       </toolbar>
     </ui>

The constructed widget hierarchy is very similar to the element tree of the XML, with the exception that placeholders are merged into their parents. The correspondence of XML elements to widgets should be almost obvious:

toolbar

popup

menu

menuitem

toolitem

separator

accelerator

a <gtk-menu-bar>

a <gtk-toolbar>

a toplevel <gtk-menu>

a <gtk-menu> attached to a menuitem

a <gtk-menu-item> subclass, the exact type depends on the action

a <gtk-tool-item> subclass, the exact type depends on the action. Note that toolitem elements may contain a menu element, but only if their associated action specifies a <gtk-menu-tool-button> as proxy.

a <gtk-separator-menu-item> or <gtk-separator-tool-item>

a keyboard accelerator

The "position" attribute determines where a constructed widget is positioned wrt. to its siblings in the partially constructed tree. If it is "top", the widget is prepended, otherwise it is appended.

72.3 UI Merging

The most remarkable feature of <gtk-ui-manager> is that it can overlay a set of menuitems and toolitems over another one, and demerge them later.

Merging is done based on the names of the XML elements. Each element is identified by a path which consists of the names of its anchestors, separated by slashes. For example, the menuitem named "Left" in the example above has the path ‘/ui/menubar/JustifyMenu/Left’ and the toolitem with the same name has path ‘/ui/toolbar1/JustifyToolItems/Left’.

72.4 Accelerators

Every action has an accelerator path. Accelerators are installed together with menuitem proxies, but they can also be explicitly added with <accelerator> elements in the UI definition. This makes it possible to have accelerators for actions even if they have no visible proxies.

72.5 Smart Separators

The separators created by <gtk-ui-manager> are "smart", i.e. they do not show up in the UI unless they end up between two visible menu or tool items. Separators which are located at the very beginning or end of the menu or toolbar containing them, or multiple separators next to each other, are hidden. This is a useful feature, since the merging of UI elements from multiple sources can make it hard or impossible to determine in advance whether a separator will end up in such an unfortunate position.

For separators in toolbars, you can set ‘expand="true"’ to turn them from a small, visible separator to an expanding, invisible one. Toolitems following an expanding separator are effectively right-aligned.

72.6 Empty Menus

Submenus pose similar problems to separators inconnection with merging. It is impossible to know in advance whether they will end up empty after merging. <gtk-ui-manager> offers two ways to treat empty submenus: The behaviour is chosen based on the "hide_if_empty" property of the action to which the submenu is associated.

make them disappear by hiding the menu item they're attached to

add an insensitive "Empty" item

72.7 Usage

— Class: <gtk-ui-manager>

Derives from <gtk-buildable>, <gobject>.

This class defines the following slots:

add-tearoffs
Whether tearoff menu items should be added to menus
ui
An XML string describing the merged UI
— Signal on <gtk-ui-manager>: connect-proxy (arg0 <gtk-action>) (arg1 <gtk-widget>)

The connect_proxy signal is emitted after connecting a proxy to an action in the group.

This is intended for simple customizations for which a custom action class would be too clumsy, e.g. showing tooltips for menuitems in the statusbar.

Since 2.4

— Signal on <gtk-ui-manager>: disconnect-proxy (arg0 <gtk-action>) (arg1 <gtk-widget>)

The disconnect_proxy signal is emitted after disconnecting a proxy from an action in the group.

Since 2.4

— Signal on <gtk-ui-manager>: pre-activate (arg0 <gtk-action>)

The pre_activate signal is emitted just before the action is activated.

This is intended for applications to get notification just before any action is activated.

Since 2.4

— Signal on <gtk-ui-manager>: post-activate (arg0 <gtk-action>)

The post_activate signal is emitted just after the action is activated.

This is intended for applications to get notification just after any action is activated.

Since 2.4

— Signal on <gtk-ui-manager>: add-widget (arg0 <gtk-widget>)

The add_widget signal is emitted for each generated menubar and toolbar. It is not emitted for generated popup menus, which can be obtained by gtk-ui-manager-get-widget.

Since 2.4

— Signal on <gtk-ui-manager>: actions-changed

The "actions-changed" signal is emitted whenever the set of actions changes.

Since 2.4

— Function: gtk-ui-manager-new ⇒  (ret <gtk-ui-manager>)

Creates a new ui manager object.

ret
a new ui manager object.

Since 2.4

— Function: gtk-ui-manager-set-add-tearoffs (self <gtk-ui-manager>) (add_tearoffs bool)
— Method: set-add-tearoffs

Sets the "add_tearoffs" property, which controls whether menus generated by this <gtk-ui-manager> will have tearoff menu items.

Note that this only affects regular menus. Generated popup menus never have tearoff menu items.

self
a <gtk-ui-manager>
add-tearoffs
whether tearoff menu items are added

Since 2.4

— Function: gtk-ui-manager-get-add-tearoffs (self <gtk-ui-manager>) ⇒  (ret bool)
— Method: get-add-tearoffs

Returns whether menus generated by this <gtk-ui-manager> will have tearoff menu items.

self
a <gtk-ui-manager>
ret
whether tearoff menu items are added

Since 2.4

— Function: gtk-ui-manager-insert-action-group (self <gtk-ui-manager>) (action_group <gtk-action-group>) (pos int)
— Method: insert-action-group

Inserts an action group into the list of action groups associated with self. Actions in earlier groups hide actions with the same name in later groups.

self
a <gtk-ui-manager> object
action-group
the action group to be inserted
pos
the position at which the group will be inserted.

Since 2.4

— Function: gtk-ui-manager-remove-action-group (self <gtk-ui-manager>) (action_group <gtk-action-group>)
— Method: remove-action-group

Removes an action group from the list of action groups associated with self.

self
a <gtk-ui-manager> object
action-group
the action group to be removed

Since 2.4

— Function: gtk-ui-manager-get-action-groups (self <gtk-ui-manager>) ⇒  (ret glist-of)
— Method: get-action-groups

Returns the list of action groups associated with self.

self
a <gtk-ui-manager> object
ret
a <g-list> of action groups. The list is owned by GTK+ and should not be modified.

Since 2.4

— Function: gtk-ui-manager-get-accel-group (self <gtk-ui-manager>) ⇒  (ret <gtk-accel-group>)
— Method: get-accel-group

Returns the <gtk-accel-group> associated with self.

self
a <gtk-ui-manager> object
ret
the <gtk-accel-group>.

Since 2.4

— Function: gtk-ui-manager-get-widget (self <gtk-ui-manager>) (path mchars) ⇒  (ret <gtk-widget>)
— Method: get-widget

Looks up a widget by following a path. The path consists of the names specified in the XML description of the UI. separated by '/'. Elements which don't have a name or action attribute in the XML (e.g. <popup>) can be addressed by their XML element name (e.g. "popup"). The root element ("/ui") can be omitted in the path.

Note that the widget found by following a path that ends in a <menu> element is the menuitem to which the menu is attached, not the menu itself.

Also note that the widgets constructed by a ui manager are not tied to the lifecycle of the ui manager. If you add the widgets returned by this function to some container or explicitly ref them, they will survive the destruction of the ui manager.

self
a <gtk-ui-manager>
path
a path
ret
the widget found by following the path, or ‘#f’ if no widget was found.

Since 2.4

— Function: gtk-ui-manager-get-toplevels (self <gtk-ui-manager>) (types <gtk-ui-manager-item-type>) ⇒  (ret gslist-of)
— Method: get-toplevels

Obtains a list of all toplevel widgets of the requested types.

self
a <gtk-ui-manager>
types
specifies the types of toplevel widgets to include. Allowed types are <gtk-ui-manager-menubar>, <gtk-ui-manager-toolbar> and <gtk-ui-manager-popup>.
ret
a newly-allocated of all toplevel widgets of the requested types.

Since 2.4

— Function: gtk-ui-manager-get-action (self <gtk-ui-manager>) (path mchars) ⇒  (ret <gtk-action>)
— Method: get-action

Looks up an action by following a path. See gtk-ui-manager-get-widget for more information about paths.

self
a <gtk-ui-manager>
path
a path
ret
the action whose proxy widget is found by following the path, or ‘#f’ if no widget was found.

Since 2.4

— Function: gtk-ui-manager-add-ui-from-string (self <gtk-ui-manager>) (buffer mchars) ⇒  (ret unsigned-int)
— Method: add-ui-from-string

Parses a string containing a UI definition and merges it with the current contents of self. An enclosing <ui> element is added if it is missing.

self
a <gtk-ui-manager> object
buffer
the string to parse
length
the length of buffer (may be -1 if buffer is nul-terminated)
error
return location for an error
ret
The merge id for the merged UI. The merge id can be used to unmerge the UI with gtk-ui-manager-remove-ui. If an error occurred, the return value is 0.

Since 2.4

— Function: gtk-ui-manager-add-ui-from-file (self <gtk-ui-manager>) (filename mchars) ⇒  (ret unsigned-int)
— Method: add-ui-from-file

Parses a file containing a UI definition and merges it with the current contents of self.

self
a <gtk-ui-manager> object
filename
the name of the file to parse
error
return location for an error
ret
The merge id for the merged UI. The merge id can be used to unmerge the UI with gtk-ui-manager-remove-ui. If an error occurred, the return value is 0.

Since 2.4

— Function: gtk-ui-manager-new-merge-id (self <gtk-ui-manager>) ⇒  (ret unsigned-int)
— Method: new-merge-id

Returns an unused merge id, suitable for use with gtk-ui-manager-add-ui.

self
a <gtk-ui-manager>
ret
an unused merge id.

Since 2.4

— Function: gtk-ui-manager-add-ui (self <gtk-ui-manager>) (merge_id unsigned-int) (path mchars) (name mchars) (action mchars) (type <gtk-ui-manager-item-type>) (top bool)
— Method: add-ui

Adds a UI element to the current contents of self.

If type is ‘GTK_UI_MANAGER_AUTO’, GTK+ inserts a menuitem, toolitem or separator if such an element can be inserted at the place determined by path. Otherwise type must indicate an element that can be inserted at the place determined by path.

If path points to a menuitem or toolitem, the new element will be inserted before or after this item, depending on top.

self
a <gtk-ui-manager>
merge-id
the merge id for the merged UI, see gtk-ui-manager-new-merge-id
path
a path
name
the name for the added UI element
action
the name of the action to be proxied, or ‘#f’ to add a separator
type
the type of UI element to add.
top
if ‘#t’, the UI element is added before its siblings, otherwise it is added after its siblings.

Since 2.4

— Function: gtk-ui-manager-remove-ui (self <gtk-ui-manager>) (merge_id unsigned-int)
— Method: remove-ui

Unmerges the part of selfs content identified by merge-id.

self
a <gtk-ui-manager> object
merge-id
a merge id as returned by gtk-ui-manager-add-ui-from-string

Since 2.4

— Function: gtk-ui-manager-get-ui (self <gtk-ui-manager>) ⇒  (ret mchars)
— Method: get-ui

Creates a UI definition of the merged UI.

self
a <gtk-ui-manager>
ret
A newly allocated string containing an XML representation of the merged UI.

Since 2.4

— Function: gtk-ui-manager-ensure-update (self <gtk-ui-manager>)
— Method: ensure-update

Makes sure that all pending updates to the UI have been completed.

This may occasionally be necessary, since <gtk-ui-manager> updates the UI in an idle function. A typical example where this function is useful is to enforce that the menubar and toolbar have been added to the main window before showing it:

          
          gtk_container_add (GTK_CONTAINER (window), vbox);
          g_signal_connect (merge, "add_widget",
                            G_CALLBACK (add_widget), vbox);
          gtk_ui_manager_add_ui_from_file (merge, "my-menus");
          gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
          gtk_ui_manager_ensure_update (merge);
          gtk_widget_show (window);
self
a <gtk-ui-manager>

Since 2.4