2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #include "gtkaccellabel.h"
35 #include "gtkactivatable.h"
36 #include "gtkbuildable.h"
37 #include "gtkimagemenuitem.h"
39 #include "gtkmarshalers.h"
41 #include "gtkmenushellprivate.h"
42 #include "gtkmenubar.h"
43 #include "gtkmenutoolbutton.h"
44 #include "gtkseparatormenuitem.h"
45 #include "gtkseparatortoolitem.h"
46 #include "gtktearoffmenuitem.h"
47 #include "gtktoolbar.h"
48 #include "gtkuimanager.h"
49 #include "gtkwindow.h"
50 #include "gtkprivate.h"
54 * SECTION:gtkuimanager
55 * @Short_description: Constructing menus and toolbars from an XML description
56 * @Title: GtkUIManager
57 * @See_also:#GtkBuilder
59 * A #GtkUIManager constructs a user interface (menus and toolbars) from
60 * one or more UI definitions, which reference actions from one or more
63 * <refsect2 id="XML-UI">
64 * <title>UI Definitions</title>
66 * The UI definitions are specified in an XML format which can be
67 * roughly described by the following DTD.
70 * Do not confuse the GtkUIManager UI Definitions described here with
71 * the similarly named <link linkend="BUILDER-UI">GtkBuilder UI
77 * <!ELEMENT ui (menubar|toolbar|popup|accelerator)* >
78 * <!ELEMENT menubar (menuitem|separator|placeholder|menu)* >
79 * <!ELEMENT menu (menuitem|separator|placeholder|menu)* >
80 * <!ELEMENT popup (menuitem|separator|placeholder|menu)* >
81 * <!ELEMENT toolbar (toolitem|separator|placeholder)* >
82 * <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* >
83 * <!ELEMENT menuitem EMPTY >
84 * <!ELEMENT toolitem (menu?) >
85 * <!ELEMENT separator EMPTY >
86 * <!ELEMENT accelerator EMPTY >
87 * <!ATTLIST menubar name #IMPLIED
89 * <!ATTLIST toolbar name #IMPLIED
91 * <!ATTLIST popup name #IMPLIED
93 * accelerators (true|false) #IMPLIED >
94 * <!ATTLIST placeholder name #IMPLIED
96 * <!ATTLIST separator name #IMPLIED
98 * expand (true|false) #IMPLIED >
99 * <!ATTLIST menu name #IMPLIED
101 * position (top|bot) #IMPLIED >
102 * <!ATTLIST menuitem name #IMPLIED
104 * position (top|bot) #IMPLIED
105 * always-show-image (true|false) #IMPLIED >
106 * <!ATTLIST toolitem name #IMPLIED
108 * position (top|bot) #IMPLIED >
109 * <!ATTLIST accelerator name #IMPLIED
113 * There are some additional restrictions beyond those specified in the
114 * DTD, e.g. every toolitem must have a toolbar in its anchestry and
115 * every menuitem must have a menubar or popup in its anchestry. Since
116 * a #GMarkup parser is used to parse the UI description, it must not only
117 * be valid XML, but valid #GMarkup.
119 * If a name is not specified, it defaults to the action. If an action is
120 * not specified either, the element name is used. The name and action
121 * attributes must not contain '/' characters after parsing (since that
122 * would mess up path lookup) and must be usable as XML attributes when
123 * enclosed in doublequotes, thus they must not '"' characters or references
124 * to the " entity.
127 * <title>A UI definition</title>
131 * <menu name="FileMenu" action="FileMenuAction">
132 * <menuitem name="New" action="New2Action" />
133 * <placeholder name="FileMenuAdditions" />
135 * <menu name="JustifyMenu" action="JustifyMenuAction">
136 * <menuitem name="Left" action="justify-left"/>
137 * <menuitem name="Centre" action="justify-center"/>
138 * <menuitem name="Right" action="justify-right"/>
139 * <menuitem name="Fill" action="justify-fill"/>
142 * <toolbar action="toolbar1">
143 * <placeholder name="JustifyToolItems">
145 * <toolitem name="Left" action="justify-left"/>
146 * <toolitem name="Centre" action="justify-center"/>
147 * <toolitem name="Right" action="justify-right"/>
148 * <toolitem name="Fill" action="justify-fill"/>
156 * The constructed widget hierarchy is very similar to the element tree
157 * of the XML, with the exception that placeholders are merged into their
158 * parents. The correspondence of XML elements to widgets should be
162 * <term>menubar</term>
163 * <listitem><para>a #GtkMenuBar</para></listitem>
166 * <term>toolbar</term>
167 * <listitem><para>a #GtkToolbar</para></listitem>
171 * <listitem><para>a toplevel #GtkMenu</para></listitem>
175 * <listitem><para>a #GtkMenu attached to a menuitem</para></listitem>
178 * <term>menuitem</term>
179 * <listitem><para>a #GtkMenuItem subclass, the exact type depends on the
180 * action</para></listitem>
183 * <term>toolitem</term>
184 * <listitem><para>a #GtkToolItem subclass, the exact type depends on the
185 * action. Note that toolitem elements may contain a menu element, but only
186 * if their associated action specifies a #GtkMenuToolButton as proxy.</para></listitem>
189 * <term>separator</term>
190 * <listitem><para>a #GtkSeparatorMenuItem or
191 * #GtkSeparatorToolItem</para></listitem>
194 * <term>accelerator</term>
195 * <listitem><para>a keyboard accelerator</para></listitem>
199 * The "position" attribute determines where a constructed widget is positioned
200 * wrt. to its siblings in the partially constructed tree. If it is
201 * "top", the widget is prepended, otherwise it is appended.
204 * <refsect2 id="UI-Merging">
205 * <title>UI Merging</title>
207 * The most remarkable feature of #GtkUIManager is that it can overlay a set
208 * of menuitems and toolitems over another one, and demerge them later.
210 * Merging is done based on the names of the XML elements. Each element is
211 * identified by a path which consists of the names of its anchestors, separated
212 * by slashes. For example, the menuitem named "Left" in the example above
213 * has the path <literal>/ui/menubar/JustifyMenu/Left</literal> and the
214 * toolitem with the same name has path
215 * <literal>/ui/toolbar1/JustifyToolItems/Left</literal>.
219 * <title>Accelerators</title>
221 * Every action has an accelerator path. Accelerators are installed together with
222 * menuitem proxies, but they can also be explicitly added with <accelerator>
223 * elements in the UI definition. This makes it possible to have accelerators for
224 * actions even if they have no visible proxies.
227 * <refsect2 id="Smart-Separators">
228 * <title>Smart Separators</title>
230 * The separators created by #GtkUIManager are "smart", i.e. they do not show up
231 * in the UI unless they end up between two visible menu or tool items. Separators
232 * which are located at the very beginning or end of the menu or toolbar
233 * containing them, or multiple separators next to each other, are hidden. This
234 * is a useful feature, since the merging of UI elements from multiple sources
235 * can make it hard or impossible to determine in advance whether a separator
236 * will end up in such an unfortunate position.
238 * For separators in toolbars, you can set <literal>expand="true"</literal> to
239 * turn them from a small, visible separator to an expanding, invisible one.
240 * Toolitems following an expanding separator are effectively right-aligned.
244 * <title>Empty Menus</title>
246 * Submenus pose similar problems to separators inconnection with merging. It is
247 * impossible to know in advance whether they will end up empty after merging.
248 * #GtkUIManager offers two ways to treat empty submenus:
251 * <para>make them disappear by hiding the menu item they're attached to</para>
254 * <para>add an insensitive "Empty" item</para>
257 * The behaviour is chosen based on the "hide_if_empty" property of the action
258 * to which the submenu is associated.
261 * <refsect2 id="GtkUIManager-BUILDER-UI">
262 * <title>GtkUIManager as GtkBuildable</title>
264 * The GtkUIManager implementation of the GtkBuildable interface accepts
265 * GtkActionGroup objects as <child> elements in UI definitions.
267 * A GtkUIManager UI definition as described above can be embedded in
268 * an GtkUIManager <object> element in a GtkBuilder UI definition.
270 * The widgets that are constructed by a GtkUIManager can be embedded in
271 * other parts of the constructed user interface with the help of the
272 * "constructor" attribute. See the example below.
275 * <title>An embedded GtkUIManager UI definition</title>
276 * <programlisting><![CDATA[
277 * <object class="GtkUIManager" id="uiman">
279 * <object class="GtkActionGroup" id="actiongroup">
281 * <object class="GtkAction" id="file">
282 * <property name="label">_File</property>
288 * <menubar name="menubar1">
289 * <menu action="file">
294 * <object class="GtkWindow" id="main-window">
296 * <object class="GtkMenuBar" id="menubar1" constructor="uiman"/>
299 * ]]></programlisting>
306 #undef DEBUG_UI_MANAGER
315 NODE_TYPE_MENU_PLACEHOLDER,
316 NODE_TYPE_TOOLBAR_PLACEHOLDER,
321 NODE_TYPE_ACCELERATOR
324 typedef struct _Node Node;
334 GtkWidget *extra; /* second separator for placeholders */
339 guint expand : 1; /* used for separators */
340 guint popup_accels : 1;
341 guint always_show_image_set : 1; /* used for menu items */
342 guint always_show_image : 1; /* used for menu items */
346 struct _GtkUIManagerPrivate
348 GtkAccelGroup *accel_group;
351 GList *action_groups;
357 gboolean add_tearoffs;
360 #define NODE_INFO(node) ((Node *)node->data)
362 typedef struct _NodeUIReference NodeUIReference;
364 struct _NodeUIReference
370 static void gtk_ui_manager_finalize (GObject *object);
371 static void gtk_ui_manager_set_property (GObject *object,
375 static void gtk_ui_manager_get_property (GObject *object,
379 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
381 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
383 static void queue_update (GtkUIManager *manager);
384 static void dirty_all_nodes (GtkUIManager *manager);
385 static void mark_node_dirty (GNode *node);
386 static GNode * get_child_node (GtkUIManager *manager,
389 const gchar *childname,
390 gint childname_length,
394 static GNode * get_node (GtkUIManager *manager,
398 static gboolean free_node (GNode *node);
399 static void node_prepend_ui_reference (GNode *node,
401 GQuark action_quark);
402 static void node_remove_ui_reference (GNode *node,
406 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
407 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
411 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
414 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
417 const gchar *tagname,
418 GMarkupParser *parser,
420 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
423 const gchar *tagname,
446 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
448 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
449 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
450 gtk_ui_manager_buildable_init))
453 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
455 GObjectClass *gobject_class;
457 gobject_class = G_OBJECT_CLASS (klass);
459 gobject_class->finalize = gtk_ui_manager_finalize;
460 gobject_class->set_property = gtk_ui_manager_set_property;
461 gobject_class->get_property = gtk_ui_manager_get_property;
462 klass->get_widget = gtk_ui_manager_real_get_widget;
463 klass->get_action = gtk_ui_manager_real_get_action;
466 * GtkUIManager:add-tearoffs:
468 * The "add-tearoffs" property controls whether generated menus
469 * have tearoff menu items.
471 * Note that this only affects regular menus. Generated popup
472 * menus never have tearoff menu items.
476 g_object_class_install_property (gobject_class,
478 g_param_spec_boolean ("add-tearoffs",
479 P_("Add tearoffs to menus"),
480 P_("Whether tearoff menu items should be added to menus"),
482 GTK_PARAM_READWRITE));
484 g_object_class_install_property (gobject_class,
486 g_param_spec_string ("ui",
487 P_("Merged UI definition"),
488 P_("An XML string describing the merged UI"),
490 GTK_PARAM_READABLE));
494 * GtkUIManager::add-widget:
495 * @manager: a #GtkUIManager
496 * @widget: the added widget
498 * The ::add-widget signal is emitted for each generated menubar and toolbar.
499 * It is not emitted for generated popup menus, which can be obtained by
500 * gtk_ui_manager_get_widget().
504 ui_manager_signals[ADD_WIDGET] =
505 g_signal_new (I_("add-widget"),
506 G_OBJECT_CLASS_TYPE (klass),
507 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
508 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
510 g_cclosure_marshal_VOID__OBJECT,
515 * GtkUIManager::actions-changed:
516 * @manager: a #GtkUIManager
518 * The ::actions-changed signal is emitted whenever the set of actions
523 ui_manager_signals[ACTIONS_CHANGED] =
524 g_signal_new (I_("actions-changed"),
525 G_OBJECT_CLASS_TYPE (klass),
526 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
527 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
529 g_cclosure_marshal_VOID__VOID,
533 * GtkUIManager::connect-proxy:
534 * @manager: the ui manager
535 * @action: the action
538 * The ::connect-proxy signal is emitted after connecting a proxy to
539 * an action in the group.
541 * This is intended for simple customizations for which a custom action
542 * class would be too clumsy, e.g. showing tooltips for menuitems in the
547 ui_manager_signals[CONNECT_PROXY] =
548 g_signal_new (I_("connect-proxy"),
549 G_OBJECT_CLASS_TYPE (klass),
550 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
551 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
553 _gtk_marshal_VOID__OBJECT_OBJECT,
559 * GtkUIManager::disconnect-proxy:
560 * @manager: the ui manager
561 * @action: the action
564 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
565 * from an action in the group.
569 ui_manager_signals[DISCONNECT_PROXY] =
570 g_signal_new (I_("disconnect-proxy"),
571 G_OBJECT_CLASS_TYPE (klass),
572 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
573 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
575 _gtk_marshal_VOID__OBJECT_OBJECT,
581 * GtkUIManager::pre-activate:
582 * @manager: the ui manager
583 * @action: the action
585 * The ::pre-activate signal is emitted just before the @action
588 * This is intended for applications to get notification
589 * just before any action is activated.
593 ui_manager_signals[PRE_ACTIVATE] =
594 g_signal_new (I_("pre-activate"),
595 G_OBJECT_CLASS_TYPE (klass),
596 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
597 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
599 _gtk_marshal_VOID__OBJECT,
604 * GtkUIManager::post-activate:
605 * @manager: the ui manager
606 * @action: the action
608 * The ::post-activate signal is emitted just after the @action
611 * This is intended for applications to get notification
612 * just after any action is activated.
616 ui_manager_signals[POST_ACTIVATE] =
617 g_signal_new (I_("post-activate"),
618 G_OBJECT_CLASS_TYPE (klass),
619 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
620 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
622 _gtk_marshal_VOID__OBJECT,
626 klass->add_widget = NULL;
627 klass->actions_changed = NULL;
628 klass->connect_proxy = NULL;
629 klass->disconnect_proxy = NULL;
630 klass->pre_activate = NULL;
631 klass->post_activate = NULL;
633 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
638 gtk_ui_manager_init (GtkUIManager *manager)
643 manager->private_data = G_TYPE_INSTANCE_GET_PRIVATE (manager,
645 GtkUIManagerPrivate);
647 manager->private_data->accel_group = gtk_accel_group_new ();
649 manager->private_data->root_node = NULL;
650 manager->private_data->action_groups = NULL;
652 manager->private_data->last_merge_id = 0;
653 manager->private_data->add_tearoffs = FALSE;
655 merge_id = gtk_ui_manager_new_merge_id (manager);
656 node = get_child_node (manager, NULL, NULL, "ui", 2,
657 NODE_TYPE_ROOT, TRUE, FALSE);
658 node_prepend_ui_reference (node, merge_id, 0);
662 gtk_ui_manager_finalize (GObject *object)
664 GtkUIManager *manager = GTK_UI_MANAGER (object);
666 if (manager->private_data->update_tag != 0)
668 g_source_remove (manager->private_data->update_tag);
669 manager->private_data->update_tag = 0;
672 g_node_traverse (manager->private_data->root_node,
673 G_POST_ORDER, G_TRAVERSE_ALL, -1,
674 (GNodeTraverseFunc)free_node, NULL);
675 g_node_destroy (manager->private_data->root_node);
676 manager->private_data->root_node = NULL;
678 g_list_foreach (manager->private_data->action_groups,
679 (GFunc) g_object_unref, NULL);
680 g_list_free (manager->private_data->action_groups);
681 manager->private_data->action_groups = NULL;
683 g_object_unref (manager->private_data->accel_group);
684 manager->private_data->accel_group = NULL;
686 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
690 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
692 iface->add_child = gtk_ui_manager_buildable_add_child;
693 iface->construct_child = gtk_ui_manager_buildable_construct_child;
694 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
695 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
699 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
704 GtkUIManager *manager = GTK_UI_MANAGER (buildable);
707 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
709 pos = g_list_length (manager->private_data->action_groups);
711 g_object_ref (child);
712 gtk_ui_manager_insert_action_group (manager,
713 GTK_ACTION_GROUP (child),
718 child_hierarchy_changed_cb (GtkWidget *widget,
723 GtkAccelGroup *group;
726 toplevel = gtk_widget_get_toplevel (widget);
727 if (!toplevel || !GTK_IS_WINDOW (toplevel))
730 group = gtk_ui_manager_get_accel_group (uimgr);
731 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
732 if (g_slist_find (groups, group) == NULL)
733 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
735 g_signal_handlers_disconnect_by_func (widget,
736 child_hierarchy_changed_cb,
741 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
748 name = g_strdup_printf ("ui/%s", id);
749 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
752 g_error ("Unknown ui manager child: %s\n", name);
758 g_signal_connect (widget, "hierarchy-changed",
759 G_CALLBACK (child_hierarchy_changed_cb),
760 GTK_UI_MANAGER (buildable));
761 return g_object_ref (widget);
765 gtk_ui_manager_set_property (GObject *object,
770 GtkUIManager *manager = GTK_UI_MANAGER (object);
774 case PROP_ADD_TEAROFFS:
775 gtk_ui_manager_set_add_tearoffs (manager, g_value_get_boolean (value));
778 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
784 gtk_ui_manager_get_property (GObject *object,
789 GtkUIManager *manager = GTK_UI_MANAGER (object);
793 case PROP_ADD_TEAROFFS:
794 g_value_set_boolean (value, manager->private_data->add_tearoffs);
797 g_value_take_string (value, gtk_ui_manager_get_ui (manager));
800 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
806 gtk_ui_manager_real_get_widget (GtkUIManager *manager,
811 /* ensure that there are no pending updates before we get the
813 gtk_ui_manager_ensure_update (manager);
815 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
820 return NODE_INFO (node)->proxy;
824 gtk_ui_manager_real_get_action (GtkUIManager *manager,
829 /* ensure that there are no pending updates before we get
831 gtk_ui_manager_ensure_update (manager);
833 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
838 return NODE_INFO (node)->action;
843 * gtk_ui_manager_new:
845 * Creates a new ui manager object.
847 * Return value: a new ui manager object.
852 gtk_ui_manager_new (void)
854 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
859 * gtk_ui_manager_get_add_tearoffs:
860 * @manager: a #GtkUIManager
862 * Returns whether menus generated by this #GtkUIManager
863 * will have tearoff menu items.
865 * Return value: whether tearoff menu items are added
870 gtk_ui_manager_get_add_tearoffs (GtkUIManager *manager)
872 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), FALSE);
874 return manager->private_data->add_tearoffs;
879 * gtk_ui_manager_set_add_tearoffs:
880 * @manager: a #GtkUIManager
881 * @add_tearoffs: whether tearoff menu items are added
883 * Sets the "add_tearoffs" property, which controls whether menus
884 * generated by this #GtkUIManager will have tearoff menu items.
886 * Note that this only affects regular menus. Generated popup
887 * menus never have tearoff menu items.
892 gtk_ui_manager_set_add_tearoffs (GtkUIManager *manager,
893 gboolean add_tearoffs)
895 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
897 add_tearoffs = add_tearoffs != FALSE;
899 if (add_tearoffs != manager->private_data->add_tearoffs)
901 manager->private_data->add_tearoffs = add_tearoffs;
903 dirty_all_nodes (manager);
905 g_object_notify (G_OBJECT (manager), "add-tearoffs");
910 cb_proxy_connect_proxy (GtkActionGroup *group,
913 GtkUIManager *manager)
915 g_signal_emit (manager, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
919 cb_proxy_disconnect_proxy (GtkActionGroup *group,
922 GtkUIManager *manager)
924 g_signal_emit (manager, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
928 cb_proxy_pre_activate (GtkActionGroup *group,
930 GtkUIManager *manager)
932 g_signal_emit (manager, ui_manager_signals[PRE_ACTIVATE], 0, action);
936 cb_proxy_post_activate (GtkActionGroup *group,
938 GtkUIManager *manager)
940 g_signal_emit (manager, ui_manager_signals[POST_ACTIVATE], 0, action);
944 * gtk_ui_manager_insert_action_group:
945 * @manager: a #GtkUIManager object
946 * @action_group: the action group to be inserted
947 * @pos: the position at which the group will be inserted.
949 * Inserts an action group into the list of action groups associated
950 * with @manager. Actions in earlier groups hide actions with the same
951 * name in later groups.
956 gtk_ui_manager_insert_action_group (GtkUIManager *manager,
957 GtkActionGroup *action_group,
960 #ifdef G_ENABLE_DEBUG
962 const char *group_name;
965 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
966 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
967 g_return_if_fail (g_list_find (manager->private_data->action_groups,
968 action_group) == NULL);
970 #ifdef G_ENABLE_DEBUG
971 group_name = gtk_action_group_get_name (action_group);
973 for (l = manager->private_data->action_groups; l; l = l->next)
975 GtkActionGroup *group = l->data;
977 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
979 g_warning ("Inserting action group '%s' into UI manager which "
980 "already has a group with this name\n", group_name);
984 #endif /* G_ENABLE_DEBUG */
986 g_object_ref (action_group);
987 manager->private_data->action_groups =
988 g_list_insert (manager->private_data->action_groups, action_group, pos);
989 g_object_connect (action_group,
990 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
991 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
992 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
993 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
996 /* dirty all nodes, as action bindings may change */
997 dirty_all_nodes (manager);
999 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1003 * gtk_ui_manager_remove_action_group:
1004 * @manager: a #GtkUIManager object
1005 * @action_group: the action group to be removed
1007 * Removes an action group from the list of action groups associated
1013 gtk_ui_manager_remove_action_group (GtkUIManager *manager,
1014 GtkActionGroup *action_group)
1016 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
1017 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1018 g_return_if_fail (g_list_find (manager->private_data->action_groups,
1019 action_group) != NULL);
1021 manager->private_data->action_groups =
1022 g_list_remove (manager->private_data->action_groups, action_group);
1024 g_object_disconnect (action_group,
1025 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1026 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1027 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1028 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1030 g_object_unref (action_group);
1032 /* dirty all nodes, as action bindings may change */
1033 dirty_all_nodes (manager);
1035 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1039 * gtk_ui_manager_get_action_groups:
1040 * @manager: a #GtkUIManager object
1042 * Returns the list of action groups associated with @manager.
1044 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
1045 * action groups. The list is owned by GTK+
1046 * and should not be modified.
1051 gtk_ui_manager_get_action_groups (GtkUIManager *manager)
1053 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1055 return manager->private_data->action_groups;
1059 * gtk_ui_manager_get_accel_group:
1060 * @manager: a #GtkUIManager object
1062 * Returns the #GtkAccelGroup associated with @manager.
1064 * Return value: (transfer none): the #GtkAccelGroup.
1069 gtk_ui_manager_get_accel_group (GtkUIManager *manager)
1071 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1073 return manager->private_data->accel_group;
1077 * gtk_ui_manager_get_widget:
1078 * @manager: a #GtkUIManager
1081 * Looks up a widget by following a path.
1082 * The path consists of the names specified in the XML description of the UI.
1083 * separated by '/'. Elements which don't have a name or action attribute in
1084 * the XML (e.g. <popup>) can be addressed by their XML element name
1085 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
1087 * Note that the widget found by following a path that ends in a <menu>
1088 * element is the menuitem to which the menu is attached, not the menu itmanager.
1090 * Also note that the widgets constructed by a ui manager are not tied to
1091 * the lifecycle of the ui manager. If you add the widgets returned by this
1092 * function to some container or explicitly ref them, they will survive the
1093 * destruction of the ui manager.
1095 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
1101 gtk_ui_manager_get_widget (GtkUIManager *manager,
1104 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1105 g_return_val_if_fail (path != NULL, NULL);
1107 return GTK_UI_MANAGER_GET_CLASS (manager)->get_widget (manager, path);
1111 GtkUIManagerItemType types;
1116 collect_toplevels (GNode *node,
1119 ToplevelData *data = user_data;
1121 if (NODE_INFO (node)->proxy)
1123 switch (NODE_INFO (node)->type)
1125 case NODE_TYPE_MENUBAR:
1126 if (data->types & GTK_UI_MANAGER_MENUBAR)
1127 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1129 case NODE_TYPE_TOOLBAR:
1130 if (data->types & GTK_UI_MANAGER_TOOLBAR)
1131 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1133 case NODE_TYPE_POPUP:
1134 if (data->types & GTK_UI_MANAGER_POPUP)
1135 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1143 * gtk_ui_manager_get_toplevels:
1144 * @manager: a #GtkUIManager
1145 * @types: specifies the types of toplevel widgets to include. Allowed
1146 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
1147 * #GTK_UI_MANAGER_POPUP.
1149 * Obtains a list of all toplevel widgets of the requested types.
1151 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
1152 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
1157 gtk_ui_manager_get_toplevels (GtkUIManager *manager,
1158 GtkUIManagerItemType types)
1162 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1163 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
1164 GTK_UI_MANAGER_TOOLBAR |
1165 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
1171 g_node_children_foreach (manager->private_data->root_node,
1173 collect_toplevels, &data);
1180 * gtk_ui_manager_get_action:
1181 * @manager: a #GtkUIManager
1184 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
1185 * for more information about paths.
1187 * Return value: (transfer none): the action whose proxy widget is found by following the path,
1188 * or %NULL if no widget was found.
1193 gtk_ui_manager_get_action (GtkUIManager *manager,
1196 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1197 g_return_val_if_fail (path != NULL, NULL);
1199 return GTK_UI_MANAGER_GET_CLASS (manager)->get_action (manager, path);
1203 node_is_dead (GNode *node)
1207 if (NODE_INFO (node)->uifiles != NULL)
1210 for (child = node->children; child != NULL; child = child->next)
1212 if (!node_is_dead (child))
1220 get_child_node (GtkUIManager *manager,
1223 const gchar *childname,
1224 gint childname_length,
1229 GNode *child = NULL;
1235 for (child = parent->children; child != NULL; child = child->next)
1237 if (NODE_INFO (child)->name &&
1238 strlen (NODE_INFO (child)->name) == childname_length &&
1239 !strncmp (NODE_INFO (child)->name, childname, childname_length))
1241 /* if undecided about node type, set it */
1242 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
1243 NODE_INFO (child)->type = node_type;
1245 /* warn about type mismatch */
1246 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
1247 node_type != NODE_TYPE_UNDECIDED &&
1248 NODE_INFO (child)->type != node_type)
1249 g_warning ("node type doesn't match %d (%s is type %d)",
1251 NODE_INFO (child)->name,
1252 NODE_INFO (child)->type);
1254 if (node_is_dead (child))
1256 /* This node was removed but is still dirty so
1257 * it is still in the tree. We want to treat this
1258 * as if it didn't exist, which means we move it
1259 * to the position it would have been created at.
1261 g_node_unlink (child);
1269 if (!child && create)
1273 mnode = g_slice_new0 (Node);
1274 mnode->type = node_type;
1275 mnode->name = g_strndup (childname, childname_length);
1277 child = g_node_new (mnode);
1282 g_node_insert_before (parent, sibling, child);
1284 g_node_insert_after (parent, sibling, child);
1289 g_node_prepend (parent, child);
1291 g_node_append (parent, child);
1294 mark_node_dirty (child);
1299 /* handle root node */
1300 if (manager->private_data->root_node)
1302 child = manager->private_data->root_node;
1303 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1304 g_warning ("root node name '%s' doesn't match '%s'",
1305 childname, NODE_INFO (child)->name);
1306 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1307 g_warning ("base element must be of type ROOT");
1313 mnode = g_slice_new0 (Node);
1314 mnode->type = node_type;
1315 mnode->name = g_strndup (childname, childname_length);
1316 mnode->dirty = TRUE;
1318 child = manager->private_data->root_node = g_node_new (mnode);
1326 get_node (GtkUIManager *manager,
1331 const gchar *pos, *end;
1332 GNode *parent, *node;
1334 if (strncmp ("/ui", path, 3) == 0)
1337 end = path + strlen (path);
1339 parent = node = NULL;
1345 slash = strchr (pos, '/');
1347 length = slash - pos;
1349 length = strlen (pos);
1351 node = get_child_node (manager, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1356 pos += length + 1; /* move past the node name and the slash too */
1360 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1361 NODE_INFO (node)->type = node_type;
1367 node_ui_reference_free (gpointer data)
1369 g_slice_free (NodeUIReference, data);
1373 free_node (GNode *node)
1375 Node *info = NODE_INFO (node);
1377 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1378 g_list_free (info->uifiles);
1381 g_object_unref (info->action);
1383 g_object_unref (info->proxy);
1385 g_object_unref (info->extra);
1386 g_free (info->name);
1387 g_slice_free (Node, info);
1393 * gtk_ui_manager_new_merge_id:
1394 * @manager: a #GtkUIManager
1396 * Returns an unused merge id, suitable for use with
1397 * gtk_ui_manager_add_ui().
1399 * Return value: an unused merge id.
1404 gtk_ui_manager_new_merge_id (GtkUIManager *manager)
1406 manager->private_data->last_merge_id++;
1408 return manager->private_data->last_merge_id;
1412 node_prepend_ui_reference (GNode *gnode,
1414 GQuark action_quark)
1416 Node *node = NODE_INFO (gnode);
1417 NodeUIReference *reference = NULL;
1419 if (node->uifiles &&
1420 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1421 reference = node->uifiles->data;
1424 reference = g_slice_new (NodeUIReference);
1425 node->uifiles = g_list_prepend (node->uifiles, reference);
1428 reference->merge_id = merge_id;
1429 reference->action_quark = action_quark;
1431 mark_node_dirty (gnode);
1435 node_remove_ui_reference (GNode *gnode,
1438 Node *node = NODE_INFO (gnode);
1441 for (p = node->uifiles; p != NULL; p = p->next)
1443 NodeUIReference *reference = p->data;
1445 if (reference->merge_id == merge_id)
1447 if (p == node->uifiles)
1448 mark_node_dirty (gnode);
1449 node->uifiles = g_list_delete_link (node->uifiles, p);
1450 g_slice_free (NodeUIReference, reference);
1457 /* -------------------- The UI file parser -------------------- */
1471 typedef struct _ParseContext ParseContext;
1472 struct _ParseContext
1475 ParseState prev_state;
1477 GtkUIManager *manager;
1485 start_element_handler (GMarkupParseContext *context,
1486 const gchar *element_name,
1487 const gchar **attribute_names,
1488 const gchar **attribute_values,
1492 ParseContext *ctx = user_data;
1493 GtkUIManager *manager = ctx->manager;
1496 const gchar *node_name;
1497 const gchar *action;
1498 GQuark action_quark;
1500 gboolean expand = FALSE;
1501 gboolean accelerators = FALSE;
1502 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1504 gboolean raise_error = TRUE;
1511 for (i = 0; attribute_names[i] != NULL; i++)
1513 if (!strcmp (attribute_names[i], "name"))
1515 node_name = attribute_values[i];
1517 else if (!strcmp (attribute_names[i], "action"))
1519 action = attribute_values[i];
1520 action_quark = g_quark_from_string (attribute_values[i]);
1522 else if (!strcmp (attribute_names[i], "position"))
1524 top = !strcmp (attribute_values[i], "top");
1526 else if (!strcmp (attribute_names[i], "expand"))
1528 expand = !strcmp (attribute_values[i], "true");
1530 else if (!strcmp (attribute_names[i], "accelerators"))
1532 accelerators = !strcmp (attribute_values[i], "true");
1534 else if (!strcmp (attribute_names[i], "always-show-image"))
1536 always_show_image_set = TRUE;
1537 always_show_image = !strcmp (attribute_values[i], "true");
1539 /* else silently skip unknown attributes to be compatible with
1540 * future additional attributes.
1544 /* Work out a name for this node. Either the name attribute, or
1545 * the action, or the element name */
1546 if (node_name == NULL)
1551 node_name = element_name;
1554 switch (element_name[0])
1557 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1559 ctx->state = STATE_ACCELERATOR;
1560 ctx->current = get_child_node (manager, ctx->current, NULL,
1561 node_name, strlen (node_name),
1562 NODE_TYPE_ACCELERATOR,
1564 if (NODE_INFO (ctx->current)->action_name == 0)
1565 NODE_INFO (ctx->current)->action_name = action_quark;
1567 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1569 raise_error = FALSE;
1573 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1575 ctx->state = STATE_ROOT;
1576 ctx->current = manager->private_data->root_node;
1577 raise_error = FALSE;
1579 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1583 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1585 ctx->state = STATE_MENU;
1586 ctx->current = get_child_node (manager, ctx->current, NULL,
1587 node_name, strlen (node_name),
1590 if (NODE_INFO (ctx->current)->action_name == 0)
1591 NODE_INFO (ctx->current)->action_name = action_quark;
1593 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1594 mark_node_dirty (ctx->current);
1596 raise_error = FALSE;
1598 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1600 ctx->current = get_child_node (manager, ctx->current, NULL,
1601 node_name, strlen (node_name),
1604 if (NODE_INFO (ctx->current)->action_name == 0)
1605 NODE_INFO (ctx->current)->action_name = action_quark;
1607 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1609 raise_error = FALSE;
1611 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1613 ctx->state = STATE_MENU;
1615 ctx->current = get_child_node (manager, g_node_last_child (ctx->current), NULL,
1616 node_name, strlen (node_name),
1619 if (NODE_INFO (ctx->current)->action_name == 0)
1620 NODE_INFO (ctx->current)->action_name = action_quark;
1622 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1624 raise_error = FALSE;
1626 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1630 ctx->state = STATE_MENUITEM;
1631 node = get_child_node (manager, ctx->current, NULL,
1632 node_name, strlen (node_name),
1635 if (NODE_INFO (node)->action_name == 0)
1636 NODE_INFO (node)->action_name = action_quark;
1638 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1639 NODE_INFO (node)->always_show_image = always_show_image;
1641 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1643 raise_error = FALSE;
1647 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1649 ctx->state = STATE_MENU;
1650 ctx->current = get_child_node (manager, ctx->current, NULL,
1651 node_name, strlen (node_name),
1655 NODE_INFO (ctx->current)->popup_accels = accelerators;
1657 if (NODE_INFO (ctx->current)->action_name == 0)
1658 NODE_INFO (ctx->current)->action_name = action_quark;
1660 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1662 raise_error = FALSE;
1664 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1665 !strcmp (element_name, "placeholder"))
1667 if (ctx->state == STATE_TOOLBAR)
1668 ctx->current = get_child_node (manager, ctx->current, NULL,
1669 node_name, strlen (node_name),
1670 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1673 ctx->current = get_child_node (manager, ctx->current, NULL,
1674 node_name, strlen (node_name),
1675 NODE_TYPE_MENU_PLACEHOLDER,
1678 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1680 raise_error = FALSE;
1684 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1685 !strcmp (element_name, "separator"))
1690 if (ctx->state == STATE_TOOLBAR)
1691 ctx->state = STATE_TOOLITEM;
1693 ctx->state = STATE_MENUITEM;
1694 if (!strcmp (node_name, "separator"))
1700 length = strlen (node_name);
1701 node = get_child_node (manager, ctx->current, NULL,
1703 NODE_TYPE_SEPARATOR,
1706 NODE_INFO (node)->expand = expand;
1708 if (NODE_INFO (node)->action_name == 0)
1709 NODE_INFO (node)->action_name = action_quark;
1711 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1713 raise_error = FALSE;
1717 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1719 ctx->state = STATE_TOOLBAR;
1720 ctx->current = get_child_node (manager, ctx->current, NULL,
1721 node_name, strlen (node_name),
1724 if (NODE_INFO (ctx->current)->action_name == 0)
1725 NODE_INFO (ctx->current)->action_name = action_quark;
1727 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1729 raise_error = FALSE;
1731 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1735 ctx->state = STATE_TOOLITEM;
1736 node = get_child_node (manager, ctx->current, NULL,
1737 node_name, strlen (node_name),
1740 if (NODE_INFO (node)->action_name == 0)
1741 NODE_INFO (node)->action_name = action_quark;
1743 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1745 raise_error = FALSE;
1753 gint line_number, char_number;
1755 g_markup_parse_context_get_position (context,
1756 &line_number, &char_number);
1759 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1760 _("Unexpected start tag '%s' on line %d char %d"),
1762 line_number, char_number);
1767 end_element_handler (GMarkupParseContext *context,
1768 const gchar *element_name,
1772 ParseContext *ctx = user_data;
1778 /* no need to GError here, GMarkup already catches this */
1781 ctx->current = NULL;
1782 ctx->state = STATE_END;
1786 case STATE_ACCELERATOR:
1787 ctx->current = ctx->current->parent;
1788 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1789 ctx->state = STATE_ROOT;
1790 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1792 ctx->current = ctx->current->parent;
1793 ctx->state = STATE_TOOLITEM;
1795 /* else, stay in same state */
1797 case STATE_MENUITEM:
1798 ctx->state = STATE_MENU;
1800 case STATE_TOOLITEM:
1801 ctx->state = STATE_TOOLBAR;
1807 cleanup (GMarkupParseContext *context,
1811 ParseContext *ctx = user_data;
1813 ctx->current = NULL;
1814 /* should also walk through the tree and get rid of nodes related to
1815 * this UI file's tag */
1817 gtk_ui_manager_remove_ui (ctx->manager, ctx->merge_id);
1821 xml_isspace (char c)
1823 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1827 text_handler (GMarkupParseContext *context,
1837 end = text + text_len;
1838 while (p != end && xml_isspace (*p))
1843 gint line_number, char_number;
1845 g_markup_parse_context_get_position (context,
1846 &line_number, &char_number);
1849 G_MARKUP_ERROR_INVALID_CONTENT,
1850 _("Unexpected character data on line %d char %d"),
1851 line_number, char_number);
1856 static const GMarkupParser ui_parser = {
1857 start_element_handler,
1858 end_element_handler,
1865 add_ui_from_string (GtkUIManager *manager,
1866 const gchar *buffer,
1868 gboolean needs_root,
1871 ParseContext ctx = { 0 };
1872 GMarkupParseContext *context;
1874 ctx.state = STATE_START;
1875 ctx.manager = manager;
1877 ctx.merge_id = gtk_ui_manager_new_merge_id (manager);
1879 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1882 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1885 if (!g_markup_parse_context_parse (context, buffer, length, error))
1889 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1892 if (!g_markup_parse_context_end_parse (context, error))
1895 g_markup_parse_context_free (context);
1897 queue_update (manager);
1899 g_object_notify (G_OBJECT (manager), "ui");
1901 return ctx.merge_id;
1905 g_markup_parse_context_free (context);
1911 * gtk_ui_manager_add_ui_from_string:
1912 * @manager: a #GtkUIManager object
1913 * @buffer: the string to parse
1914 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1915 * @error: return location for an error
1917 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1918 * merges it with the current contents of @manager. An enclosing <ui>
1919 * element is added if it is missing.
1921 * Return value: The merge id for the merged UI. The merge id can be used
1922 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1923 * the return value is 0.
1928 gtk_ui_manager_add_ui_from_string (GtkUIManager *manager,
1929 const gchar *buffer,
1933 gboolean needs_root = TRUE;
1937 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1938 g_return_val_if_fail (buffer != NULL, 0);
1941 length = strlen (buffer);
1944 end = buffer + length;
1945 while (p != end && xml_isspace (*p))
1948 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1951 return add_ui_from_string (manager, buffer, length, needs_root, error);
1955 * gtk_ui_manager_add_ui_from_file:
1956 * @manager: a #GtkUIManager object
1957 * @filename: (type filename): the name of the file to parse
1958 * @error: return location for an error
1960 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1961 * merges it with the current contents of @manager.
1963 * Return value: The merge id for the merged UI. The merge id can be used
1964 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1965 * the return value is 0.
1970 gtk_ui_manager_add_ui_from_file (GtkUIManager *manager,
1971 const gchar *filename,
1978 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1980 if (!g_file_get_contents (filename, &buffer, &length, error))
1983 res = add_ui_from_string (manager, buffer, length, FALSE, error);
1990 * gtk_ui_manager_add_ui:
1991 * @manager: a #GtkUIManager
1992 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1994 * @name: the name for the added UI element
1995 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
1996 * @type: the type of UI element to add.
1997 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1998 * is added after its siblings.
2000 * Adds a UI element to the current contents of @manager.
2002 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
2003 * separator if such an element can be inserted at the place determined by
2004 * @path. Otherwise @type must indicate an element that can be inserted at
2005 * the place determined by @path.
2007 * If @path points to a menuitem or toolitem, the new element will be inserted
2008 * before or after this item, depending on @top.
2013 gtk_ui_manager_add_ui (GtkUIManager *manager,
2017 const gchar *action,
2018 GtkUIManagerItemType type,
2025 GQuark action_quark = 0;
2027 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2028 g_return_if_fail (merge_id > 0);
2029 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
2031 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
2037 node_type = NODE_TYPE_UNDECIDED;
2040 switch (NODE_INFO (node)->type)
2042 case NODE_TYPE_SEPARATOR:
2043 case NODE_TYPE_MENUITEM:
2044 case NODE_TYPE_TOOLITEM:
2046 node = node->parent;
2048 case NODE_TYPE_MENUBAR:
2049 case NODE_TYPE_MENU:
2050 case NODE_TYPE_POPUP:
2051 case NODE_TYPE_MENU_PLACEHOLDER:
2054 case GTK_UI_MANAGER_AUTO:
2056 node_type = NODE_TYPE_MENUITEM;
2058 node_type = NODE_TYPE_SEPARATOR;
2060 case GTK_UI_MANAGER_MENU:
2061 node_type = NODE_TYPE_MENU;
2063 case GTK_UI_MANAGER_MENUITEM:
2064 node_type = NODE_TYPE_MENUITEM;
2066 case GTK_UI_MANAGER_SEPARATOR:
2067 node_type = NODE_TYPE_SEPARATOR;
2069 case GTK_UI_MANAGER_PLACEHOLDER:
2070 node_type = NODE_TYPE_MENU_PLACEHOLDER;
2076 case NODE_TYPE_TOOLBAR:
2077 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2080 case GTK_UI_MANAGER_AUTO:
2082 node_type = NODE_TYPE_TOOLITEM;
2084 node_type = NODE_TYPE_SEPARATOR;
2086 case GTK_UI_MANAGER_TOOLITEM:
2087 node_type = NODE_TYPE_TOOLITEM;
2089 case GTK_UI_MANAGER_SEPARATOR:
2090 node_type = NODE_TYPE_SEPARATOR;
2092 case GTK_UI_MANAGER_PLACEHOLDER:
2093 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
2099 case NODE_TYPE_ROOT:
2102 case GTK_UI_MANAGER_MENUBAR:
2103 node_type = NODE_TYPE_MENUBAR;
2105 case GTK_UI_MANAGER_TOOLBAR:
2106 node_type = NODE_TYPE_TOOLBAR;
2108 case GTK_UI_MANAGER_POPUP:
2109 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
2110 node_type = NODE_TYPE_POPUP;
2112 case GTK_UI_MANAGER_ACCELERATOR:
2113 node_type = NODE_TYPE_ACCELERATOR;
2123 if (node_type == NODE_TYPE_UNDECIDED)
2125 g_warning ("item type %d not suitable for adding at '%s'",
2130 child = get_child_node (manager, node, sibling,
2131 name, name ? strlen (name) : 0,
2132 node_type, TRUE, top);
2134 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
2135 NODE_INFO (child)->popup_accels = TRUE;
2138 action_quark = g_quark_from_string (action);
2140 node_prepend_ui_reference (child, merge_id, action_quark);
2142 if (NODE_INFO (child)->action_name == 0)
2143 NODE_INFO (child)->action_name = action_quark;
2145 queue_update (manager);
2147 g_object_notify (G_OBJECT (manager), "ui");
2151 remove_ui (GNode *node,
2154 guint merge_id = GPOINTER_TO_UINT (user_data);
2156 node_remove_ui_reference (node, merge_id);
2158 return FALSE; /* continue */
2162 * gtk_ui_manager_remove_ui:
2163 * @manager: a #GtkUIManager object
2164 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
2166 * Unmerges the part of @manager<!-- -->s content identified by @merge_id.
2171 gtk_ui_manager_remove_ui (GtkUIManager *manager,
2174 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2176 g_node_traverse (manager->private_data->root_node,
2177 G_POST_ORDER, G_TRAVERSE_ALL, -1,
2178 remove_ui, GUINT_TO_POINTER (merge_id));
2180 queue_update (manager);
2182 g_object_notify (G_OBJECT (manager), "ui");
2185 /* -------------------- Updates -------------------- */
2189 get_action_by_name (GtkUIManager *merge,
2190 const gchar *action_name)
2198 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
2200 GtkActionGroup *action_group = tmp->data;
2203 action = gtk_action_group_get_action (action_group, action_name);
2213 find_menu_position (GNode *node,
2214 GtkWidget **menushell_p,
2217 GtkWidget *menushell;
2220 g_return_val_if_fail (node != NULL, FALSE);
2221 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
2222 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
2223 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
2224 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
2225 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2228 /* first sibling -- look at parent */
2229 if (node->prev == NULL)
2234 parent = node->parent;
2235 switch (NODE_INFO (parent)->type)
2237 case NODE_TYPE_MENUBAR:
2238 case NODE_TYPE_POPUP:
2239 menushell = NODE_INFO (parent)->proxy;
2242 case NODE_TYPE_MENU:
2243 menushell = NODE_INFO (parent)->proxy;
2244 if (GTK_IS_MENU_ITEM (menushell))
2245 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
2246 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
2247 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2251 g_list_free (siblings);
2253 case NODE_TYPE_MENU_PLACEHOLDER:
2254 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2255 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2256 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children,
2257 NODE_INFO (parent)->proxy) + 1;
2260 g_warning ("%s: bad parent node type %d", G_STRLOC,
2261 NODE_INFO (parent)->type);
2267 GtkWidget *prev_child;
2270 sibling = node->prev;
2271 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2272 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2274 prev_child = NODE_INFO (sibling)->proxy;
2276 if (!GTK_IS_WIDGET (prev_child))
2279 menushell = gtk_widget_get_parent (prev_child);
2280 if (!GTK_IS_MENU_SHELL (menushell))
2283 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children, prev_child) + 1;
2287 *menushell_p = menushell;
2295 find_toolbar_position (GNode *node,
2296 GtkWidget **toolbar_p,
2302 g_return_val_if_fail (node != NULL, FALSE);
2303 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2304 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2305 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2306 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2309 /* first sibling -- look at parent */
2310 if (node->prev == NULL)
2314 parent = node->parent;
2315 switch (NODE_INFO (parent)->type)
2317 case NODE_TYPE_TOOLBAR:
2318 toolbar = NODE_INFO (parent)->proxy;
2321 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2322 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2323 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2324 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2325 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2328 g_warning ("%s: bad parent node type %d", G_STRLOC,
2329 NODE_INFO (parent)->type);
2335 GtkWidget *prev_child;
2338 sibling = node->prev;
2339 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2340 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2342 prev_child = NODE_INFO (sibling)->proxy;
2344 if (!GTK_IS_WIDGET (prev_child))
2347 toolbar = gtk_widget_get_parent (prev_child);
2348 if (!GTK_IS_TOOLBAR (toolbar))
2351 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2352 GTK_TOOL_ITEM (prev_child)) + 1;
2356 *toolbar_p = toolbar;
2364 * _gtk_menu_is_empty:
2365 * @menu: (allow-none): a #GtkMenu or %NULL
2367 * Determines whether @menu is empty. A menu is considered empty if it
2368 * the only visible children are tearoff menu items or "filler" menu
2369 * items which were inserted to mark the menu as empty.
2371 * This function is used by #GtkAction.
2373 * Return value: whether @menu is empty.
2376 _gtk_menu_is_empty (GtkWidget *menu)
2378 GList *children, *cur;
2379 gboolean result = TRUE;
2381 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2386 children = gtk_container_get_children (GTK_CONTAINER (menu));
2391 if (gtk_widget_get_visible (cur->data))
2393 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2394 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2402 g_list_free (children);
2408 SEPARATOR_MODE_SMART,
2409 SEPARATOR_MODE_VISIBLE,
2410 SEPARATOR_MODE_HIDDEN
2414 update_smart_separators (GtkWidget *proxy)
2416 GtkWidget *parent = NULL;
2418 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2420 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2421 parent = gtk_widget_get_parent (proxy);
2427 GList *children, *cur, *last;
2430 children = gtk_container_get_children (GTK_CONTAINER (parent));
2440 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2444 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2445 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2448 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2449 "gtk-separator-mode"));
2452 case SEPARATOR_MODE_VISIBLE:
2453 gtk_widget_show (GTK_WIDGET (cur->data));
2457 case SEPARATOR_MODE_HIDDEN:
2458 gtk_widget_hide (GTK_WIDGET (cur->data));
2460 case SEPARATOR_MODE_SMART:
2463 gtk_widget_show (GTK_WIDGET (cur->data));
2468 gtk_widget_hide (GTK_WIDGET (cur->data));
2472 else if (gtk_widget_get_visible (cur->data))
2475 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2488 gtk_widget_hide (GTK_WIDGET (last->data));
2490 if (GTK_IS_MENU (parent))
2494 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2495 if (GTK_IS_MENU_ITEM (item))
2496 _gtk_action_sync_menu_visible (NULL, item, empty);
2497 if (GTK_IS_WIDGET (filler))
2500 gtk_widget_show (filler);
2502 gtk_widget_hide (filler);
2506 g_list_free (children);
2511 update_node (GtkUIManager *manager,
2514 gboolean popup_accels)
2519 const gchar *action_name;
2520 NodeUIReference *ref;
2522 #ifdef DEBUG_UI_MANAGER
2526 g_return_if_fail (node != NULL);
2527 g_return_if_fail (NODE_INFO (node) != NULL);
2529 info = NODE_INFO (node);
2534 if (info->type == NODE_TYPE_POPUP)
2537 popup_accels = info->popup_accels;
2540 #ifdef DEBUG_UI_MANAGER
2541 g_print ("update_node name=%s dirty=%d popup %d (",
2542 info->name, info->dirty, in_popup);
2543 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2545 NodeUIReference *ref = tmp->data;
2546 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2553 if (info->uifiles == NULL) {
2554 /* We may need to remove this node.
2555 * This must be done in post order
2557 goto recurse_children;
2560 ref = info->uifiles->data;
2561 action_name = g_quark_to_string (ref->action_quark);
2562 action = get_action_by_name (manager, action_name);
2564 info->dirty = FALSE;
2566 /* Check if the node doesn't have an action and must have an action */
2567 if (action == NULL &&
2568 info->type != NODE_TYPE_ROOT &&
2569 info->type != NODE_TYPE_MENUBAR &&
2570 info->type != NODE_TYPE_TOOLBAR &&
2571 info->type != NODE_TYPE_POPUP &&
2572 info->type != NODE_TYPE_SEPARATOR &&
2573 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2574 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2576 g_warning ("%s: missing action %s", info->name, action_name);
2582 gtk_action_set_accel_group (action, manager->private_data->accel_group);
2584 /* If the widget already has a proxy and the action hasn't changed, then
2585 * we only have to update the tearoff menu items.
2587 if (info->proxy != NULL && action == info->action)
2589 if (info->type == NODE_TYPE_MENU)
2594 if (GTK_IS_MENU (info->proxy))
2597 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2598 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2599 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2601 if (manager->private_data->add_tearoffs && !in_popup)
2602 gtk_widget_show (GTK_WIDGET (siblings->data));
2604 gtk_widget_hide (GTK_WIDGET (siblings->data));
2606 g_list_free (siblings);
2609 goto recurse_children;
2614 case NODE_TYPE_MENUBAR:
2615 if (info->proxy == NULL)
2617 info->proxy = gtk_menu_bar_new ();
2618 g_object_ref_sink (info->proxy);
2619 gtk_widget_set_name (info->proxy, info->name);
2620 gtk_widget_show (info->proxy);
2621 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2624 case NODE_TYPE_POPUP:
2625 if (info->proxy == NULL)
2627 info->proxy = gtk_menu_new ();
2628 g_object_ref_sink (info->proxy);
2630 gtk_widget_set_name (info->proxy, info->name);
2632 case NODE_TYPE_MENU:
2634 GtkWidget *prev_submenu = NULL;
2635 GtkWidget *menu = NULL;
2638 /* remove the proxy if it is of the wrong type ... */
2640 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2642 if (GTK_IS_MENU_ITEM (info->proxy))
2644 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2647 g_object_ref (prev_submenu);
2648 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2652 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2653 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2655 g_object_unref (info->proxy);
2659 /* create proxy if needed ... */
2660 if (info->proxy == NULL)
2662 /* ... if the action already provides a menu, then use
2663 * that menu instead of creating an empty one
2665 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2666 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2667 GTK_ACTION_GET_CLASS (action)->create_menu)
2669 menu = gtk_action_create_menu (action);
2677 menu = gtk_menu_new ();
2678 gtk_widget_set_name (menu, info->name);
2679 tearoff = gtk_tearoff_menu_item_new ();
2680 gtk_widget_set_no_show_all (tearoff, TRUE);
2681 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2682 filler = gtk_menu_item_new_with_label (_("Empty"));
2683 g_object_set_data (G_OBJECT (filler),
2684 I_("gtk-empty-menu-item"),
2685 GINT_TO_POINTER (TRUE));
2686 gtk_widget_set_sensitive (filler, FALSE);
2687 gtk_widget_set_no_show_all (filler, TRUE);
2688 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2691 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2694 g_object_ref_sink (info->proxy);
2695 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2700 GtkWidget *menushell;
2703 if (find_menu_position (node, &menushell, &pos))
2705 info->proxy = gtk_action_create_menu_item (action);
2706 g_object_ref_sink (info->proxy);
2707 g_signal_connect (info->proxy, "notify::visible",
2708 G_CALLBACK (update_smart_separators), NULL);
2709 gtk_widget_set_name (info->proxy, info->name);
2711 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2712 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2717 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2721 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2723 g_object_unref (prev_submenu);
2726 if (GTK_IS_MENU (info->proxy))
2729 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2731 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2732 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2734 if (manager->private_data->add_tearoffs && !in_popup)
2735 gtk_widget_show (GTK_WIDGET (siblings->data));
2737 gtk_widget_hide (GTK_WIDGET (siblings->data));
2739 g_list_free (siblings);
2742 case NODE_TYPE_UNDECIDED:
2743 g_warning ("found undecided node!");
2745 case NODE_TYPE_ROOT:
2747 case NODE_TYPE_TOOLBAR:
2748 if (info->proxy == NULL)
2750 info->proxy = gtk_toolbar_new ();
2751 g_object_ref_sink (info->proxy);
2752 gtk_widget_set_name (info->proxy, info->name);
2753 gtk_widget_show (info->proxy);
2754 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2757 case NODE_TYPE_MENU_PLACEHOLDER:
2758 /* create menu items for placeholders if necessary ... */
2759 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2760 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2764 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2766 g_object_unref (info->proxy);
2771 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2773 g_object_unref (info->extra);
2777 if (info->proxy == NULL)
2779 GtkWidget *menushell;
2782 if (find_menu_position (node, &menushell, &pos))
2784 info->proxy = gtk_separator_menu_item_new ();
2785 g_object_ref_sink (info->proxy);
2786 g_object_set_data (G_OBJECT (info->proxy),
2787 I_("gtk-separator-mode"),
2788 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2789 gtk_widget_set_no_show_all (info->proxy, TRUE);
2790 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2791 NODE_INFO (node)->proxy, pos);
2793 info->extra = gtk_separator_menu_item_new ();
2794 g_object_ref_sink (info->extra);
2795 g_object_set_data (G_OBJECT (info->extra),
2796 I_("gtk-separator-mode"),
2797 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2798 gtk_widget_set_no_show_all (info->extra, TRUE);
2799 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2800 NODE_INFO (node)->extra, pos + 1);
2804 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2805 /* create toolbar items for placeholders if necessary ... */
2806 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2807 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2811 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2813 g_object_unref (info->proxy);
2818 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2820 g_object_unref (info->extra);
2824 if (info->proxy == NULL)
2830 if (find_toolbar_position (node, &toolbar, &pos))
2832 item = gtk_separator_tool_item_new ();
2833 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2834 info->proxy = GTK_WIDGET (item);
2835 g_object_ref_sink (info->proxy);
2836 g_object_set_data (G_OBJECT (info->proxy),
2837 I_("gtk-separator-mode"),
2838 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2839 gtk_widget_set_no_show_all (info->proxy, TRUE);
2841 item = gtk_separator_tool_item_new ();
2842 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2843 info->extra = GTK_WIDGET (item);
2844 g_object_ref_sink (info->extra);
2845 g_object_set_data (G_OBJECT (info->extra),
2846 I_("gtk-separator-mode"),
2847 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2848 gtk_widget_set_no_show_all (info->extra, TRUE);
2852 case NODE_TYPE_MENUITEM:
2853 /* remove the proxy if it is of the wrong type ... */
2855 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2857 g_signal_handlers_disconnect_by_func (info->proxy,
2858 G_CALLBACK (update_smart_separators),
2860 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2861 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2863 g_object_unref (info->proxy);
2866 /* create proxy if needed ... */
2867 if (info->proxy == NULL)
2869 GtkWidget *menushell;
2872 if (find_menu_position (node, &menushell, &pos))
2874 info->proxy = gtk_action_create_menu_item (action);
2875 g_object_ref_sink (info->proxy);
2876 gtk_widget_set_name (info->proxy, info->name);
2878 if (info->always_show_image_set &&
2879 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2880 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2881 info->always_show_image);
2883 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2889 g_signal_handlers_disconnect_by_func (info->proxy,
2890 G_CALLBACK (update_smart_separators),
2892 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2893 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2898 g_signal_connect (info->proxy, "notify::visible",
2899 G_CALLBACK (update_smart_separators), NULL);
2900 if (in_popup && !popup_accels)
2902 /* don't show accels in popups */
2903 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2904 if (GTK_IS_ACCEL_LABEL (child))
2905 g_object_set (child, "accel-closure", NULL, NULL);
2910 case NODE_TYPE_TOOLITEM:
2911 /* remove the proxy if it is of the wrong type ... */
2913 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2915 g_signal_handlers_disconnect_by_func (info->proxy,
2916 G_CALLBACK (update_smart_separators),
2918 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2919 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2921 g_object_unref (info->proxy);
2924 /* create proxy if needed ... */
2925 if (info->proxy == NULL)
2930 if (find_toolbar_position (node, &toolbar, &pos))
2932 info->proxy = gtk_action_create_tool_item (action);
2933 g_object_ref_sink (info->proxy);
2934 gtk_widget_set_name (info->proxy, info->name);
2936 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2937 GTK_TOOL_ITEM (info->proxy), pos);
2942 g_signal_handlers_disconnect_by_func (info->proxy,
2943 G_CALLBACK (update_smart_separators),
2945 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2950 g_signal_connect (info->proxy, "notify::visible",
2951 G_CALLBACK (update_smart_separators), NULL);
2954 case NODE_TYPE_SEPARATOR:
2955 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2956 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2960 gint separator_mode;
2963 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2965 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2967 g_object_unref (info->proxy);
2971 if (find_toolbar_position (node, &toolbar, &pos))
2973 item = gtk_separator_tool_item_new ();
2974 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2975 info->proxy = GTK_WIDGET (item);
2976 g_object_ref_sink (info->proxy);
2977 gtk_widget_set_no_show_all (info->proxy, TRUE);
2980 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2981 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2982 separator_mode = SEPARATOR_MODE_VISIBLE;
2985 separator_mode = SEPARATOR_MODE_SMART;
2987 g_object_set_data (G_OBJECT (info->proxy),
2988 I_("gtk-separator-mode"),
2989 GINT_TO_POINTER (separator_mode));
2990 gtk_widget_show (info->proxy);
2995 GtkWidget *menushell;
2998 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
3000 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3002 g_object_unref (info->proxy);
3006 if (find_menu_position (node, &menushell, &pos))
3008 info->proxy = gtk_separator_menu_item_new ();
3009 g_object_ref_sink (info->proxy);
3010 gtk_widget_set_no_show_all (info->proxy, TRUE);
3011 g_object_set_data (G_OBJECT (info->proxy),
3012 I_("gtk-separator-mode"),
3013 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
3014 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
3016 gtk_widget_show (info->proxy);
3020 case NODE_TYPE_ACCELERATOR:
3021 gtk_action_connect_accelerator (action);
3026 g_object_ref (action);
3028 g_object_unref (info->action);
3029 info->action = action;
3032 /* process children */
3033 child = node->children;
3039 child = current->next;
3040 update_node (manager, current, in_popup, popup_accels);
3045 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
3046 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
3047 else if (info->type == NODE_TYPE_MENU ||
3048 info->type == NODE_TYPE_TOOLBAR ||
3049 info->type == NODE_TYPE_POPUP)
3050 update_smart_separators (info->proxy);
3053 /* handle cleanup of dead nodes */
3054 if (node->children == NULL && info->uifiles == NULL)
3057 gtk_widget_destroy (info->proxy);
3059 gtk_widget_destroy (info->extra);
3060 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
3061 gtk_action_disconnect_accelerator (info->action);
3063 g_node_destroy (node);
3068 do_updates (GtkUIManager *manager)
3070 /* this function needs to check through the tree for dirty nodes.
3071 * For such nodes, it needs to do the following:
3073 * 1) check if they are referenced by any loaded UI files anymore.
3074 * In which case, the proxy widget should be destroyed, unless
3075 * there are any subnodes.
3077 * 2) lookup the action for this node again. If it is different to
3078 * the current one (or if no previous action has been looked up),
3079 * the proxy is reconnected to the new action (or a new proxy widget
3080 * is created and added to the parent container).
3082 update_node (manager, manager->private_data->root_node, FALSE, FALSE);
3084 manager->private_data->update_tag = 0;
3090 do_updates_idle (GtkUIManager *manager)
3092 do_updates (manager);
3098 queue_update (GtkUIManager *manager)
3100 if (manager->private_data->update_tag != 0)
3103 manager->private_data->update_tag = gdk_threads_add_idle (
3104 (GSourceFunc)do_updates_idle,
3110 * gtk_ui_manager_ensure_update:
3111 * @manager: a #GtkUIManager
3113 * Makes sure that all pending updates to the UI have been completed.
3115 * This may occasionally be necessary, since #GtkUIManager updates the
3116 * UI in an idle function. A typical example where this function is
3117 * useful is to enforce that the menubar and toolbar have been added to
3118 * the main window before showing it:
3120 * gtk_container_add (GTK_CONTAINER (window), vbox);
3121 * g_signal_connect (merge, "add-widget",
3122 * G_CALLBACK (add_widget), vbox);
3123 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
3124 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
3125 * gtk_ui_manager_ensure_update (merge);
3126 * gtk_widget_show (window);
3132 gtk_ui_manager_ensure_update (GtkUIManager *manager)
3134 if (manager->private_data->update_tag != 0)
3136 g_source_remove (manager->private_data->update_tag);
3137 do_updates (manager);
3142 dirty_traverse_func (GNode *node,
3145 NODE_INFO (node)->dirty = TRUE;
3150 dirty_all_nodes (GtkUIManager *manager)
3152 g_node_traverse (manager->private_data->root_node,
3153 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3154 dirty_traverse_func, NULL);
3155 queue_update (manager);
3159 mark_node_dirty (GNode *node)
3163 /* FIXME could optimize this */
3164 for (p = node; p; p = p->parent)
3165 NODE_INFO (p)->dirty = TRUE;
3168 static const gchar *
3169 open_tag_format (NodeType type)
3173 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
3174 case NODE_TYPE_ROOT: return "%*s<ui";
3175 case NODE_TYPE_MENUBAR: return "%*s<menubar";
3176 case NODE_TYPE_MENU: return "%*s<menu";
3177 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
3178 case NODE_TYPE_MENU_PLACEHOLDER:
3179 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
3180 case NODE_TYPE_POPUP: return "%*s<popup";
3181 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
3182 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
3183 case NODE_TYPE_SEPARATOR: return "%*s<separator";
3184 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
3185 default: return NULL;
3189 static const gchar *
3190 close_tag_format (NodeType type)
3194 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
3195 case NODE_TYPE_ROOT: return "%*s</ui>\n";
3196 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
3197 case NODE_TYPE_MENU: return "%*s</menu>\n";
3198 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
3199 case NODE_TYPE_MENU_PLACEHOLDER:
3200 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
3201 case NODE_TYPE_POPUP: return "%*s</popup>\n";
3202 default: return NULL;
3207 print_node (GtkUIManager *manager,
3214 const gchar *open_fmt;
3215 const gchar *close_fmt;
3219 open_fmt = open_tag_format (mnode->type);
3220 close_fmt = close_tag_format (mnode->type);
3222 g_string_append_printf (buffer, open_fmt, indent_level, "");
3224 if (mnode->type != NODE_TYPE_ROOT)
3227 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
3229 if (mnode->action_name)
3230 g_string_append_printf (buffer, " action=\"%s\"",
3231 g_quark_to_string (mnode->action_name));
3234 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
3236 for (child = node->children; child != NULL; child = child->next)
3237 print_node (manager, child, indent_level + 2, buffer);
3240 g_string_append_printf (buffer, close_fmt, indent_level, "");
3244 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
3245 GtkBuilder *builder,
3247 const gchar *tagname,
3248 GMarkupParser *parser,
3254 if (strcmp (tagname, "ui") == 0)
3258 ctx = g_new0 (ParseContext, 1);
3259 ctx->state = STATE_START;
3260 ctx->manager = GTK_UI_MANAGER (buildable);
3261 ctx->current = NULL;
3262 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3265 *parser = ui_parser;
3275 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3276 GtkBuilder *builder,
3278 const gchar *tagname,
3281 queue_update (GTK_UI_MANAGER (buildable));
3282 g_object_notify (G_OBJECT (buildable), "ui");
3287 * gtk_ui_manager_get_ui:
3288 * @manager: a #GtkUIManager
3290 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3292 * Return value: A newly allocated string containing an XML representation of
3298 gtk_ui_manager_get_ui (GtkUIManager *manager)
3302 buffer = g_string_new (NULL);
3304 gtk_ui_manager_ensure_update (manager);
3306 print_node (manager, manager->private_data->root_node, 0, buffer);
3308 return g_string_free (buffer, FALSE);