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 this library. If not, see <http://www.gnu.org/licenses/>.
21 * Author: James Henstridge <james@daa.com.au>
23 * Modified by the GTK+ Team and others 2003. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkaccellabel.h"
33 #include "gtkactivatable.h"
34 #include "gtkbuildable.h"
35 #include "gtkimagemenuitem.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenushellprivate.h"
40 #include "gtkmenubar.h"
41 #include "gtkmenutoolbutton.h"
42 #include "gtkseparatormenuitem.h"
43 #include "gtkseparatortoolitem.h"
44 #include "gtktoolbar.h"
45 #include "gtkwindow.h"
46 #include "gtkprivate.h"
48 #include "gtkuimanager.h"
49 #include "deprecated/gtktearoffmenuitem.h"
52 * SECTION:gtkuimanager
53 * @Short_description: Constructing menus and toolbars from an XML description
54 * @Title: GtkUIManager
55 * @See_also:#GtkBuilder
57 * A #GtkUIManager constructs a user interface (menus and toolbars) from
58 * one or more UI definitions, which reference actions from one or more
61 * <refsect2 id="XML-UI">
62 * <title>UI Definitions</title>
64 * The UI definitions are specified in an XML format which can be
65 * roughly described by the following DTD.
68 * Do not confuse the GtkUIManager UI Definitions described here with
69 * the similarly named <link linkend="BUILDER-UI">GtkBuilder UI
75 * <!ELEMENT ui (menubar|toolbar|popup|accelerator)* >
76 * <!ELEMENT menubar (menuitem|separator|placeholder|menu)* >
77 * <!ELEMENT menu (menuitem|separator|placeholder|menu)* >
78 * <!ELEMENT popup (menuitem|separator|placeholder|menu)* >
79 * <!ELEMENT toolbar (toolitem|separator|placeholder)* >
80 * <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* >
81 * <!ELEMENT menuitem EMPTY >
82 * <!ELEMENT toolitem (menu?) >
83 * <!ELEMENT separator EMPTY >
84 * <!ELEMENT accelerator EMPTY >
85 * <!ATTLIST menubar name #IMPLIED
87 * <!ATTLIST toolbar name #IMPLIED
89 * <!ATTLIST popup name #IMPLIED
91 * accelerators (true|false) #IMPLIED >
92 * <!ATTLIST placeholder name #IMPLIED
94 * <!ATTLIST separator name #IMPLIED
96 * expand (true|false) #IMPLIED >
97 * <!ATTLIST menu name #IMPLIED
99 * position (top|bot) #IMPLIED >
100 * <!ATTLIST menuitem name #IMPLIED
102 * position (top|bot) #IMPLIED
103 * always-show-image (true|false) #IMPLIED >
104 * <!ATTLIST toolitem name #IMPLIED
106 * position (top|bot) #IMPLIED >
107 * <!ATTLIST accelerator name #IMPLIED
111 * There are some additional restrictions beyond those specified in the
112 * DTD, e.g. every toolitem must have a toolbar in its anchestry and
113 * every menuitem must have a menubar or popup in its anchestry. Since
114 * a #GMarkup parser is used to parse the UI description, it must not only
115 * be valid XML, but valid #GMarkup.
117 * If a name is not specified, it defaults to the action. If an action is
118 * not specified either, the element name is used. The name and action
119 * attributes must not contain '/' characters after parsing (since that
120 * would mess up path lookup) and must be usable as XML attributes when
121 * enclosed in doublequotes, thus they must not '"' characters or references
122 * to the " entity.
125 * <title>A UI definition</title>
129 * <menu name="FileMenu" action="FileMenuAction">
130 * <menuitem name="New" action="New2Action" />
131 * <placeholder name="FileMenuAdditions" />
133 * <menu name="JustifyMenu" action="JustifyMenuAction">
134 * <menuitem name="Left" action="justify-left"/>
135 * <menuitem name="Centre" action="justify-center"/>
136 * <menuitem name="Right" action="justify-right"/>
137 * <menuitem name="Fill" action="justify-fill"/>
140 * <toolbar action="toolbar1">
141 * <placeholder name="JustifyToolItems">
143 * <toolitem name="Left" action="justify-left"/>
144 * <toolitem name="Centre" action="justify-center"/>
145 * <toolitem name="Right" action="justify-right"/>
146 * <toolitem name="Fill" action="justify-fill"/>
154 * The constructed widget hierarchy is very similar to the element tree
155 * of the XML, with the exception that placeholders are merged into their
156 * parents. The correspondence of XML elements to widgets should be
160 * <term>menubar</term>
161 * <listitem><para>a #GtkMenuBar</para></listitem>
164 * <term>toolbar</term>
165 * <listitem><para>a #GtkToolbar</para></listitem>
169 * <listitem><para>a toplevel #GtkMenu</para></listitem>
173 * <listitem><para>a #GtkMenu attached to a menuitem</para></listitem>
176 * <term>menuitem</term>
177 * <listitem><para>a #GtkMenuItem subclass, the exact type depends on the
178 * action</para></listitem>
181 * <term>toolitem</term>
182 * <listitem><para>a #GtkToolItem subclass, the exact type depends on the
183 * action. Note that toolitem elements may contain a menu element, but only
184 * if their associated action specifies a #GtkMenuToolButton as proxy.</para></listitem>
187 * <term>separator</term>
188 * <listitem><para>a #GtkSeparatorMenuItem or
189 * #GtkSeparatorToolItem</para></listitem>
192 * <term>accelerator</term>
193 * <listitem><para>a keyboard accelerator</para></listitem>
197 * The "position" attribute determines where a constructed widget is positioned
198 * wrt. to its siblings in the partially constructed tree. If it is
199 * "top", the widget is prepended, otherwise it is appended.
202 * <refsect2 id="UI-Merging">
203 * <title>UI Merging</title>
205 * The most remarkable feature of #GtkUIManager is that it can overlay a set
206 * of menuitems and toolitems over another one, and demerge them later.
208 * Merging is done based on the names of the XML elements. Each element is
209 * identified by a path which consists of the names of its anchestors, separated
210 * by slashes. For example, the menuitem named "Left" in the example above
211 * has the path <literal>/ui/menubar/JustifyMenu/Left</literal> and the
212 * toolitem with the same name has path
213 * <literal>/ui/toolbar1/JustifyToolItems/Left</literal>.
217 * <title>Accelerators</title>
219 * Every action has an accelerator path. Accelerators are installed together with
220 * menuitem proxies, but they can also be explicitly added with <accelerator>
221 * elements in the UI definition. This makes it possible to have accelerators for
222 * actions even if they have no visible proxies.
225 * <refsect2 id="Smart-Separators">
226 * <title>Smart Separators</title>
228 * The separators created by #GtkUIManager are "smart", i.e. they do not show up
229 * in the UI unless they end up between two visible menu or tool items. Separators
230 * which are located at the very beginning or end of the menu or toolbar
231 * containing them, or multiple separators next to each other, are hidden. This
232 * is a useful feature, since the merging of UI elements from multiple sources
233 * can make it hard or impossible to determine in advance whether a separator
234 * will end up in such an unfortunate position.
236 * For separators in toolbars, you can set <literal>expand="true"</literal> to
237 * turn them from a small, visible separator to an expanding, invisible one.
238 * Toolitems following an expanding separator are effectively right-aligned.
242 * <title>Empty Menus</title>
244 * Submenus pose similar problems to separators inconnection with merging. It is
245 * impossible to know in advance whether they will end up empty after merging.
246 * #GtkUIManager offers two ways to treat empty submenus:
249 * <para>make them disappear by hiding the menu item they're attached to</para>
252 * <para>add an insensitive "Empty" item</para>
255 * The behaviour is chosen based on the "hide_if_empty" property of the action
256 * to which the submenu is associated.
259 * <refsect2 id="GtkUIManager-BUILDER-UI">
260 * <title>GtkUIManager as GtkBuildable</title>
262 * The GtkUIManager implementation of the GtkBuildable interface accepts
263 * GtkActionGroup objects as <child> elements in UI definitions.
265 * A GtkUIManager UI definition as described above can be embedded in
266 * an GtkUIManager <object> element in a GtkBuilder UI definition.
268 * The widgets that are constructed by a GtkUIManager can be embedded in
269 * other parts of the constructed user interface with the help of the
270 * "constructor" attribute. See the example below.
273 * <title>An embedded GtkUIManager UI definition</title>
274 * <programlisting><![CDATA[
275 * <object class="GtkUIManager" id="uiman">
277 * <object class="GtkActionGroup" id="actiongroup">
279 * <object class="GtkAction" id="file">
280 * <property name="label">_File</property>
286 * <menubar name="menubar1">
287 * <menu action="file">
292 * <object class="GtkWindow" id="main-window">
294 * <object class="GtkMenuBar" id="menubar1" constructor="uiman"/>
297 * ]]></programlisting>
304 #undef DEBUG_UI_MANAGER
313 NODE_TYPE_MENU_PLACEHOLDER,
314 NODE_TYPE_TOOLBAR_PLACEHOLDER,
319 NODE_TYPE_ACCELERATOR
322 typedef struct _Node Node;
332 GtkWidget *extra; /* second separator for placeholders */
337 guint expand : 1; /* used for separators */
338 guint popup_accels : 1;
339 guint always_show_image_set : 1; /* used for menu items */
340 guint always_show_image : 1; /* used for menu items */
344 struct _GtkUIManagerPrivate
346 GtkAccelGroup *accel_group;
349 GList *action_groups;
355 gboolean add_tearoffs;
358 #define NODE_INFO(node) ((Node *)node->data)
360 typedef struct _NodeUIReference NodeUIReference;
362 struct _NodeUIReference
368 static void gtk_ui_manager_finalize (GObject *object);
369 static void gtk_ui_manager_set_property (GObject *object,
373 static void gtk_ui_manager_get_property (GObject *object,
377 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
379 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
381 static void queue_update (GtkUIManager *manager);
382 static void dirty_all_nodes (GtkUIManager *manager);
383 static void mark_node_dirty (GNode *node);
384 static GNode * get_child_node (GtkUIManager *manager,
387 const gchar *childname,
388 gint childname_length,
392 static GNode * get_node (GtkUIManager *manager,
396 static gboolean free_node (GNode *node);
397 static void node_prepend_ui_reference (GNode *node,
399 GQuark action_quark);
400 static void node_remove_ui_reference (GNode *node,
404 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
405 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
409 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
412 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
415 const gchar *tagname,
416 GMarkupParser *parser,
418 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
421 const gchar *tagname,
423 static void gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
424 gboolean add_tearoffs);
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 * Deprecated: 3.4: Tearoff menus are deprecated and should not
477 * be used in newly written code.
479 g_object_class_install_property (gobject_class,
481 g_param_spec_boolean ("add-tearoffs",
482 P_("Add tearoffs to menus"),
483 P_("Whether tearoff menu items should be added to menus"),
485 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
487 g_object_class_install_property (gobject_class,
489 g_param_spec_string ("ui",
490 P_("Merged UI definition"),
491 P_("An XML string describing the merged UI"),
493 GTK_PARAM_READABLE));
497 * GtkUIManager::add-widget:
498 * @manager: a #GtkUIManager
499 * @widget: the added widget
501 * The ::add-widget signal is emitted for each generated menubar and toolbar.
502 * It is not emitted for generated popup menus, which can be obtained by
503 * gtk_ui_manager_get_widget().
507 ui_manager_signals[ADD_WIDGET] =
508 g_signal_new (I_("add-widget"),
509 G_OBJECT_CLASS_TYPE (klass),
510 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
511 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
513 g_cclosure_marshal_VOID__OBJECT,
518 * GtkUIManager::actions-changed:
519 * @manager: a #GtkUIManager
521 * The ::actions-changed signal is emitted whenever the set of actions
526 ui_manager_signals[ACTIONS_CHANGED] =
527 g_signal_new (I_("actions-changed"),
528 G_OBJECT_CLASS_TYPE (klass),
529 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
530 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
532 g_cclosure_marshal_VOID__VOID,
536 * GtkUIManager::connect-proxy:
537 * @manager: the ui manager
538 * @action: the action
541 * The ::connect-proxy signal is emitted after connecting a proxy to
542 * an action in the group.
544 * This is intended for simple customizations for which a custom action
545 * class would be too clumsy, e.g. showing tooltips for menuitems in the
550 ui_manager_signals[CONNECT_PROXY] =
551 g_signal_new (I_("connect-proxy"),
552 G_OBJECT_CLASS_TYPE (klass),
553 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
554 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
556 _gtk_marshal_VOID__OBJECT_OBJECT,
562 * GtkUIManager::disconnect-proxy:
563 * @manager: the ui manager
564 * @action: the action
567 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
568 * from an action in the group.
572 ui_manager_signals[DISCONNECT_PROXY] =
573 g_signal_new (I_("disconnect-proxy"),
574 G_OBJECT_CLASS_TYPE (klass),
575 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
576 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
578 _gtk_marshal_VOID__OBJECT_OBJECT,
584 * GtkUIManager::pre-activate:
585 * @manager: the ui manager
586 * @action: the action
588 * The ::pre-activate signal is emitted just before the @action
591 * This is intended for applications to get notification
592 * just before any action is activated.
596 ui_manager_signals[PRE_ACTIVATE] =
597 g_signal_new (I_("pre-activate"),
598 G_OBJECT_CLASS_TYPE (klass),
599 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
600 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
602 _gtk_marshal_VOID__OBJECT,
607 * GtkUIManager::post-activate:
608 * @manager: the ui manager
609 * @action: the action
611 * The ::post-activate signal is emitted just after the @action
614 * This is intended for applications to get notification
615 * just after any action is activated.
619 ui_manager_signals[POST_ACTIVATE] =
620 g_signal_new (I_("post-activate"),
621 G_OBJECT_CLASS_TYPE (klass),
622 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
623 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
625 _gtk_marshal_VOID__OBJECT,
629 klass->add_widget = NULL;
630 klass->actions_changed = NULL;
631 klass->connect_proxy = NULL;
632 klass->disconnect_proxy = NULL;
633 klass->pre_activate = NULL;
634 klass->post_activate = NULL;
636 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
641 gtk_ui_manager_init (GtkUIManager *manager)
646 manager->private_data = G_TYPE_INSTANCE_GET_PRIVATE (manager,
648 GtkUIManagerPrivate);
650 manager->private_data->accel_group = gtk_accel_group_new ();
652 manager->private_data->root_node = NULL;
653 manager->private_data->action_groups = NULL;
655 manager->private_data->last_merge_id = 0;
656 manager->private_data->add_tearoffs = FALSE;
658 merge_id = gtk_ui_manager_new_merge_id (manager);
659 node = get_child_node (manager, NULL, NULL, "ui", 2,
660 NODE_TYPE_ROOT, TRUE, FALSE);
661 node_prepend_ui_reference (node, merge_id, 0);
665 gtk_ui_manager_finalize (GObject *object)
667 GtkUIManager *manager = GTK_UI_MANAGER (object);
669 if (manager->private_data->update_tag != 0)
671 g_source_remove (manager->private_data->update_tag);
672 manager->private_data->update_tag = 0;
675 g_node_traverse (manager->private_data->root_node,
676 G_POST_ORDER, G_TRAVERSE_ALL, -1,
677 (GNodeTraverseFunc)free_node, NULL);
678 g_node_destroy (manager->private_data->root_node);
679 manager->private_data->root_node = NULL;
681 g_list_free_full (manager->private_data->action_groups, g_object_unref);
682 manager->private_data->action_groups = NULL;
684 g_object_unref (manager->private_data->accel_group);
685 manager->private_data->accel_group = NULL;
687 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
691 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
693 iface->add_child = gtk_ui_manager_buildable_add_child;
694 iface->construct_child = gtk_ui_manager_buildable_construct_child;
695 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
696 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
700 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
705 GtkUIManager *manager = GTK_UI_MANAGER (buildable);
708 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
710 pos = g_list_length (manager->private_data->action_groups);
712 g_object_ref (child);
713 gtk_ui_manager_insert_action_group (manager,
714 GTK_ACTION_GROUP (child),
719 child_hierarchy_changed_cb (GtkWidget *widget,
724 GtkAccelGroup *group;
727 toplevel = gtk_widget_get_toplevel (widget);
728 if (!toplevel || !GTK_IS_WINDOW (toplevel))
731 group = gtk_ui_manager_get_accel_group (uimgr);
732 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
733 if (g_slist_find (groups, group) == NULL)
734 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
736 g_signal_handlers_disconnect_by_func (widget,
737 child_hierarchy_changed_cb,
742 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
749 name = g_strdup_printf ("ui/%s", id);
750 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
753 g_error ("Unknown ui manager child: %s\n", name);
759 g_signal_connect (widget, "hierarchy-changed",
760 G_CALLBACK (child_hierarchy_changed_cb),
761 GTK_UI_MANAGER (buildable));
762 return g_object_ref (widget);
766 gtk_ui_manager_set_property (GObject *object,
771 GtkUIManager *manager = GTK_UI_MANAGER (object);
775 case PROP_ADD_TEAROFFS:
776 gtk_ui_manager_do_set_add_tearoffs (manager, g_value_get_boolean (value));
779 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
785 gtk_ui_manager_get_property (GObject *object,
790 GtkUIManager *manager = GTK_UI_MANAGER (object);
794 case PROP_ADD_TEAROFFS:
795 g_value_set_boolean (value, manager->private_data->add_tearoffs);
798 g_value_take_string (value, gtk_ui_manager_get_ui (manager));
801 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
807 gtk_ui_manager_real_get_widget (GtkUIManager *manager,
812 /* ensure that there are no pending updates before we get the
814 gtk_ui_manager_ensure_update (manager);
816 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
821 return NODE_INFO (node)->proxy;
825 gtk_ui_manager_real_get_action (GtkUIManager *manager,
830 /* ensure that there are no pending updates before we get
832 gtk_ui_manager_ensure_update (manager);
834 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
839 return NODE_INFO (node)->action;
844 * gtk_ui_manager_new:
846 * Creates a new ui manager object.
848 * Return value: a new ui manager object.
853 gtk_ui_manager_new (void)
855 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
860 * gtk_ui_manager_get_add_tearoffs:
861 * @manager: a #GtkUIManager
863 * Returns whether menus generated by this #GtkUIManager
864 * will have tearoff menu items.
866 * Return value: whether tearoff menu items are added
870 * Deprecated: 3.4: Tearoff menus are deprecated and should not
871 * be used in newly written code.
874 gtk_ui_manager_get_add_tearoffs (GtkUIManager *manager)
876 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), FALSE);
878 return manager->private_data->add_tearoffs;
883 * gtk_ui_manager_set_add_tearoffs:
884 * @manager: a #GtkUIManager
885 * @add_tearoffs: whether tearoff menu items are added
887 * Sets the "add_tearoffs" property, which controls whether menus
888 * generated by this #GtkUIManager will have tearoff menu items.
890 * Note that this only affects regular menus. Generated popup
891 * menus never have tearoff menu items.
895 * Deprecated: 3.4: Tearoff menus are deprecated and should not
896 * be used in newly written code.
899 gtk_ui_manager_set_add_tearoffs (GtkUIManager *manager,
900 gboolean add_tearoffs)
902 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
904 gtk_ui_manager_do_set_add_tearoffs (manager, add_tearoffs);
908 gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
909 gboolean add_tearoffs)
911 add_tearoffs = add_tearoffs != FALSE;
913 if (add_tearoffs != manager->private_data->add_tearoffs)
915 manager->private_data->add_tearoffs = add_tearoffs;
917 dirty_all_nodes (manager);
919 g_object_notify (G_OBJECT (manager), "add-tearoffs");
924 cb_proxy_connect_proxy (GtkActionGroup *group,
927 GtkUIManager *manager)
929 g_signal_emit (manager, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
933 cb_proxy_disconnect_proxy (GtkActionGroup *group,
936 GtkUIManager *manager)
938 g_signal_emit (manager, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
942 cb_proxy_pre_activate (GtkActionGroup *group,
944 GtkUIManager *manager)
946 g_signal_emit (manager, ui_manager_signals[PRE_ACTIVATE], 0, action);
950 cb_proxy_post_activate (GtkActionGroup *group,
952 GtkUIManager *manager)
954 g_signal_emit (manager, ui_manager_signals[POST_ACTIVATE], 0, action);
958 * gtk_ui_manager_insert_action_group:
959 * @manager: a #GtkUIManager object
960 * @action_group: the action group to be inserted
961 * @pos: the position at which the group will be inserted.
963 * Inserts an action group into the list of action groups associated
964 * with @manager. Actions in earlier groups hide actions with the same
965 * name in later groups.
967 * If @pos is larger than the number of action groups in @manager, or
968 * negative, @action_group will be inserted at the end of the internal
974 gtk_ui_manager_insert_action_group (GtkUIManager *manager,
975 GtkActionGroup *action_group,
978 #ifdef G_ENABLE_DEBUG
980 const char *group_name;
983 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
984 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
985 g_return_if_fail (g_list_find (manager->private_data->action_groups,
986 action_group) == NULL);
988 #ifdef G_ENABLE_DEBUG
989 group_name = gtk_action_group_get_name (action_group);
991 for (l = manager->private_data->action_groups; l; l = l->next)
993 GtkActionGroup *group = l->data;
995 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
997 g_warning ("Inserting action group '%s' into UI manager which "
998 "already has a group with this name\n", group_name);
1002 #endif /* G_ENABLE_DEBUG */
1004 g_object_ref (action_group);
1005 manager->private_data->action_groups =
1006 g_list_insert (manager->private_data->action_groups, action_group, pos);
1007 g_object_connect (action_group,
1008 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1009 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1010 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1011 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1014 /* dirty all nodes, as action bindings may change */
1015 dirty_all_nodes (manager);
1017 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1021 * gtk_ui_manager_remove_action_group:
1022 * @manager: a #GtkUIManager object
1023 * @action_group: the action group to be removed
1025 * Removes an action group from the list of action groups associated
1031 gtk_ui_manager_remove_action_group (GtkUIManager *manager,
1032 GtkActionGroup *action_group)
1034 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
1035 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1036 g_return_if_fail (g_list_find (manager->private_data->action_groups,
1037 action_group) != NULL);
1039 manager->private_data->action_groups =
1040 g_list_remove (manager->private_data->action_groups, action_group);
1042 g_object_disconnect (action_group,
1043 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1044 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1045 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1046 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1048 g_object_unref (action_group);
1050 /* dirty all nodes, as action bindings may change */
1051 dirty_all_nodes (manager);
1053 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1057 * gtk_ui_manager_get_action_groups:
1058 * @manager: a #GtkUIManager object
1060 * Returns the list of action groups associated with @manager.
1062 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
1063 * action groups. The list is owned by GTK+
1064 * and should not be modified.
1069 gtk_ui_manager_get_action_groups (GtkUIManager *manager)
1071 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1073 return manager->private_data->action_groups;
1077 * gtk_ui_manager_get_accel_group:
1078 * @manager: a #GtkUIManager object
1080 * Returns the #GtkAccelGroup associated with @manager.
1082 * Return value: (transfer none): the #GtkAccelGroup.
1087 gtk_ui_manager_get_accel_group (GtkUIManager *manager)
1089 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1091 return manager->private_data->accel_group;
1095 * gtk_ui_manager_get_widget:
1096 * @manager: a #GtkUIManager
1099 * Looks up a widget by following a path.
1100 * The path consists of the names specified in the XML description of the UI.
1101 * separated by '/'. Elements which don't have a name or action attribute in
1102 * the XML (e.g. <popup>) can be addressed by their XML element name
1103 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
1105 * Note that the widget found by following a path that ends in a <menu>
1106 * element is the menuitem to which the menu is attached, not the menu itmanager.
1108 * Also note that the widgets constructed by a ui manager are not tied to
1109 * the lifecycle of the ui manager. If you add the widgets returned by this
1110 * function to some container or explicitly ref them, they will survive the
1111 * destruction of the ui manager.
1113 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
1119 gtk_ui_manager_get_widget (GtkUIManager *manager,
1122 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1123 g_return_val_if_fail (path != NULL, NULL);
1125 return GTK_UI_MANAGER_GET_CLASS (manager)->get_widget (manager, path);
1129 GtkUIManagerItemType types;
1134 collect_toplevels (GNode *node,
1137 ToplevelData *data = user_data;
1139 if (NODE_INFO (node)->proxy)
1141 switch (NODE_INFO (node)->type)
1143 case NODE_TYPE_MENUBAR:
1144 if (data->types & GTK_UI_MANAGER_MENUBAR)
1145 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1147 case NODE_TYPE_TOOLBAR:
1148 if (data->types & GTK_UI_MANAGER_TOOLBAR)
1149 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1151 case NODE_TYPE_POPUP:
1152 if (data->types & GTK_UI_MANAGER_POPUP)
1153 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1161 * gtk_ui_manager_get_toplevels:
1162 * @manager: a #GtkUIManager
1163 * @types: specifies the types of toplevel widgets to include. Allowed
1164 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
1165 * #GTK_UI_MANAGER_POPUP.
1167 * Obtains a list of all toplevel widgets of the requested types.
1169 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
1170 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
1175 gtk_ui_manager_get_toplevels (GtkUIManager *manager,
1176 GtkUIManagerItemType types)
1180 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1181 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
1182 GTK_UI_MANAGER_TOOLBAR |
1183 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
1189 g_node_children_foreach (manager->private_data->root_node,
1191 collect_toplevels, &data);
1198 * gtk_ui_manager_get_action:
1199 * @manager: a #GtkUIManager
1202 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
1203 * for more information about paths.
1205 * Return value: (transfer none): the action whose proxy widget is found by following the path,
1206 * or %NULL if no widget was found.
1211 gtk_ui_manager_get_action (GtkUIManager *manager,
1214 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1215 g_return_val_if_fail (path != NULL, NULL);
1217 return GTK_UI_MANAGER_GET_CLASS (manager)->get_action (manager, path);
1221 node_is_dead (GNode *node)
1225 if (NODE_INFO (node)->uifiles != NULL)
1228 for (child = node->children; child != NULL; child = child->next)
1230 if (!node_is_dead (child))
1238 get_child_node (GtkUIManager *manager,
1241 const gchar *childname,
1242 gint childname_length,
1247 GNode *child = NULL;
1253 for (child = parent->children; child != NULL; child = child->next)
1255 if (NODE_INFO (child)->name &&
1256 strlen (NODE_INFO (child)->name) == childname_length &&
1257 !strncmp (NODE_INFO (child)->name, childname, childname_length))
1259 /* if undecided about node type, set it */
1260 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
1261 NODE_INFO (child)->type = node_type;
1263 /* warn about type mismatch */
1264 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
1265 node_type != NODE_TYPE_UNDECIDED &&
1266 NODE_INFO (child)->type != node_type)
1267 g_warning ("node type doesn't match %d (%s is type %d)",
1269 NODE_INFO (child)->name,
1270 NODE_INFO (child)->type);
1272 if (node_is_dead (child))
1274 /* This node was removed but is still dirty so
1275 * it is still in the tree. We want to treat this
1276 * as if it didn't exist, which means we move it
1277 * to the position it would have been created at.
1279 g_node_unlink (child);
1287 if (!child && create)
1291 mnode = g_slice_new0 (Node);
1292 mnode->type = node_type;
1293 mnode->name = g_strndup (childname, childname_length);
1295 child = g_node_new (mnode);
1300 g_node_insert_before (parent, sibling, child);
1302 g_node_insert_after (parent, sibling, child);
1307 g_node_prepend (parent, child);
1309 g_node_append (parent, child);
1312 mark_node_dirty (child);
1317 /* handle root node */
1318 if (manager->private_data->root_node)
1320 child = manager->private_data->root_node;
1321 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1322 g_warning ("root node name '%s' doesn't match '%s'",
1323 childname, NODE_INFO (child)->name);
1324 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1325 g_warning ("base element must be of type ROOT");
1331 mnode = g_slice_new0 (Node);
1332 mnode->type = node_type;
1333 mnode->name = g_strndup (childname, childname_length);
1334 mnode->dirty = TRUE;
1336 child = manager->private_data->root_node = g_node_new (mnode);
1344 get_node (GtkUIManager *manager,
1349 const gchar *pos, *end;
1350 GNode *parent, *node;
1352 if (strncmp ("/ui", path, 3) == 0)
1355 end = path + strlen (path);
1357 parent = node = NULL;
1363 slash = strchr (pos, '/');
1365 length = slash - pos;
1367 length = strlen (pos);
1369 node = get_child_node (manager, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1374 pos += length + 1; /* move past the node name and the slash too */
1378 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1379 NODE_INFO (node)->type = node_type;
1385 node_ui_reference_free (gpointer data)
1387 g_slice_free (NodeUIReference, data);
1391 free_node (GNode *node)
1393 Node *info = NODE_INFO (node);
1395 g_list_free_full (info->uifiles, node_ui_reference_free);
1398 g_object_unref (info->action);
1400 g_object_unref (info->proxy);
1402 g_object_unref (info->extra);
1403 g_free (info->name);
1404 g_slice_free (Node, info);
1410 * gtk_ui_manager_new_merge_id:
1411 * @manager: a #GtkUIManager
1413 * Returns an unused merge id, suitable for use with
1414 * gtk_ui_manager_add_ui().
1416 * Return value: an unused merge id.
1421 gtk_ui_manager_new_merge_id (GtkUIManager *manager)
1423 manager->private_data->last_merge_id++;
1425 return manager->private_data->last_merge_id;
1429 node_prepend_ui_reference (GNode *gnode,
1431 GQuark action_quark)
1433 Node *node = NODE_INFO (gnode);
1434 NodeUIReference *reference = NULL;
1436 if (node->uifiles &&
1437 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1438 reference = node->uifiles->data;
1441 reference = g_slice_new (NodeUIReference);
1442 node->uifiles = g_list_prepend (node->uifiles, reference);
1445 reference->merge_id = merge_id;
1446 reference->action_quark = action_quark;
1448 mark_node_dirty (gnode);
1452 node_remove_ui_reference (GNode *gnode,
1455 Node *node = NODE_INFO (gnode);
1458 for (p = node->uifiles; p != NULL; p = p->next)
1460 NodeUIReference *reference = p->data;
1462 if (reference->merge_id == merge_id)
1464 if (p == node->uifiles)
1465 mark_node_dirty (gnode);
1466 node->uifiles = g_list_delete_link (node->uifiles, p);
1467 g_slice_free (NodeUIReference, reference);
1474 /* -------------------- The UI file parser -------------------- */
1488 typedef struct _ParseContext ParseContext;
1489 struct _ParseContext
1492 ParseState prev_state;
1494 GtkUIManager *manager;
1502 start_element_handler (GMarkupParseContext *context,
1503 const gchar *element_name,
1504 const gchar **attribute_names,
1505 const gchar **attribute_values,
1509 ParseContext *ctx = user_data;
1510 GtkUIManager *manager = ctx->manager;
1513 const gchar *node_name;
1514 const gchar *action;
1515 GQuark action_quark;
1517 gboolean expand = FALSE;
1518 gboolean accelerators = FALSE;
1519 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1521 gboolean raise_error = TRUE;
1528 for (i = 0; attribute_names[i] != NULL; i++)
1530 if (!strcmp (attribute_names[i], "name"))
1532 node_name = attribute_values[i];
1534 else if (!strcmp (attribute_names[i], "action"))
1536 action = attribute_values[i];
1537 action_quark = g_quark_from_string (attribute_values[i]);
1539 else if (!strcmp (attribute_names[i], "position"))
1541 top = !strcmp (attribute_values[i], "top");
1543 else if (!strcmp (attribute_names[i], "expand"))
1545 expand = !strcmp (attribute_values[i], "true");
1547 else if (!strcmp (attribute_names[i], "accelerators"))
1549 accelerators = !strcmp (attribute_values[i], "true");
1551 else if (!strcmp (attribute_names[i], "always-show-image"))
1553 always_show_image_set = TRUE;
1554 always_show_image = !strcmp (attribute_values[i], "true");
1556 /* else silently skip unknown attributes to be compatible with
1557 * future additional attributes.
1561 /* Work out a name for this node. Either the name attribute, or
1562 * the action, or the element name */
1563 if (node_name == NULL)
1568 node_name = element_name;
1571 switch (element_name[0])
1574 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1576 ctx->state = STATE_ACCELERATOR;
1577 ctx->current = get_child_node (manager, ctx->current, NULL,
1578 node_name, strlen (node_name),
1579 NODE_TYPE_ACCELERATOR,
1581 if (NODE_INFO (ctx->current)->action_name == 0)
1582 NODE_INFO (ctx->current)->action_name = action_quark;
1584 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1586 raise_error = FALSE;
1590 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1592 ctx->state = STATE_ROOT;
1593 ctx->current = manager->private_data->root_node;
1594 raise_error = FALSE;
1596 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1600 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1602 ctx->state = STATE_MENU;
1603 ctx->current = get_child_node (manager, ctx->current, NULL,
1604 node_name, strlen (node_name),
1607 if (NODE_INFO (ctx->current)->action_name == 0)
1608 NODE_INFO (ctx->current)->action_name = action_quark;
1610 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1611 mark_node_dirty (ctx->current);
1613 raise_error = FALSE;
1615 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1617 ctx->current = get_child_node (manager, ctx->current, NULL,
1618 node_name, strlen (node_name),
1621 if (NODE_INFO (ctx->current)->action_name == 0)
1622 NODE_INFO (ctx->current)->action_name = action_quark;
1624 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1626 raise_error = FALSE;
1628 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1630 ctx->state = STATE_MENU;
1632 ctx->current = get_child_node (manager, g_node_last_child (ctx->current), NULL,
1633 node_name, strlen (node_name),
1636 if (NODE_INFO (ctx->current)->action_name == 0)
1637 NODE_INFO (ctx->current)->action_name = action_quark;
1639 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1641 raise_error = FALSE;
1643 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1647 ctx->state = STATE_MENUITEM;
1648 node = get_child_node (manager, ctx->current, NULL,
1649 node_name, strlen (node_name),
1652 if (NODE_INFO (node)->action_name == 0)
1653 NODE_INFO (node)->action_name = action_quark;
1655 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1656 NODE_INFO (node)->always_show_image = always_show_image;
1658 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1660 raise_error = FALSE;
1664 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1666 ctx->state = STATE_MENU;
1667 ctx->current = get_child_node (manager, ctx->current, NULL,
1668 node_name, strlen (node_name),
1672 NODE_INFO (ctx->current)->popup_accels = accelerators;
1674 if (NODE_INFO (ctx->current)->action_name == 0)
1675 NODE_INFO (ctx->current)->action_name = action_quark;
1677 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1679 raise_error = FALSE;
1681 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1682 !strcmp (element_name, "placeholder"))
1684 if (ctx->state == STATE_TOOLBAR)
1685 ctx->current = get_child_node (manager, ctx->current, NULL,
1686 node_name, strlen (node_name),
1687 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1690 ctx->current = get_child_node (manager, ctx->current, NULL,
1691 node_name, strlen (node_name),
1692 NODE_TYPE_MENU_PLACEHOLDER,
1695 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1697 raise_error = FALSE;
1701 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1702 !strcmp (element_name, "separator"))
1707 if (ctx->state == STATE_TOOLBAR)
1708 ctx->state = STATE_TOOLITEM;
1710 ctx->state = STATE_MENUITEM;
1711 if (!strcmp (node_name, "separator"))
1717 length = strlen (node_name);
1718 node = get_child_node (manager, ctx->current, NULL,
1720 NODE_TYPE_SEPARATOR,
1723 NODE_INFO (node)->expand = expand;
1725 if (NODE_INFO (node)->action_name == 0)
1726 NODE_INFO (node)->action_name = action_quark;
1728 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1730 raise_error = FALSE;
1734 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1736 ctx->state = STATE_TOOLBAR;
1737 ctx->current = get_child_node (manager, ctx->current, NULL,
1738 node_name, strlen (node_name),
1741 if (NODE_INFO (ctx->current)->action_name == 0)
1742 NODE_INFO (ctx->current)->action_name = action_quark;
1744 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1746 raise_error = FALSE;
1748 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1752 ctx->state = STATE_TOOLITEM;
1753 node = get_child_node (manager, ctx->current, NULL,
1754 node_name, strlen (node_name),
1757 if (NODE_INFO (node)->action_name == 0)
1758 NODE_INFO (node)->action_name = action_quark;
1760 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1762 raise_error = FALSE;
1770 gint line_number, char_number;
1772 g_markup_parse_context_get_position (context,
1773 &line_number, &char_number);
1776 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1777 _("Unexpected start tag '%s' on line %d char %d"),
1779 line_number, char_number);
1784 end_element_handler (GMarkupParseContext *context,
1785 const gchar *element_name,
1789 ParseContext *ctx = user_data;
1795 /* no need to GError here, GMarkup already catches this */
1798 ctx->current = NULL;
1799 ctx->state = STATE_END;
1803 case STATE_ACCELERATOR:
1804 ctx->current = ctx->current->parent;
1805 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1806 ctx->state = STATE_ROOT;
1807 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1809 ctx->current = ctx->current->parent;
1810 ctx->state = STATE_TOOLITEM;
1812 /* else, stay in same state */
1814 case STATE_MENUITEM:
1815 ctx->state = STATE_MENU;
1817 case STATE_TOOLITEM:
1818 ctx->state = STATE_TOOLBAR;
1824 cleanup (GMarkupParseContext *context,
1828 ParseContext *ctx = user_data;
1830 ctx->current = NULL;
1831 /* should also walk through the tree and get rid of nodes related to
1832 * this UI file's tag */
1834 gtk_ui_manager_remove_ui (ctx->manager, ctx->merge_id);
1838 xml_isspace (char c)
1840 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1844 text_handler (GMarkupParseContext *context,
1854 end = text + text_len;
1855 while (p != end && xml_isspace (*p))
1860 gint line_number, char_number;
1862 g_markup_parse_context_get_position (context,
1863 &line_number, &char_number);
1866 G_MARKUP_ERROR_INVALID_CONTENT,
1867 _("Unexpected character data on line %d char %d"),
1868 line_number, char_number);
1873 static const GMarkupParser ui_parser = {
1874 start_element_handler,
1875 end_element_handler,
1882 add_ui_from_string (GtkUIManager *manager,
1883 const gchar *buffer,
1885 gboolean needs_root,
1888 ParseContext ctx = { 0 };
1889 GMarkupParseContext *context;
1891 ctx.state = STATE_START;
1892 ctx.manager = manager;
1894 ctx.merge_id = gtk_ui_manager_new_merge_id (manager);
1896 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1899 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1902 if (!g_markup_parse_context_parse (context, buffer, length, error))
1906 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1909 if (!g_markup_parse_context_end_parse (context, error))
1912 g_markup_parse_context_free (context);
1914 queue_update (manager);
1916 g_object_notify (G_OBJECT (manager), "ui");
1918 return ctx.merge_id;
1922 g_markup_parse_context_free (context);
1928 * gtk_ui_manager_add_ui_from_string:
1929 * @manager: a #GtkUIManager object
1930 * @buffer: the string to parse
1931 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1932 * @error: return location for an error
1934 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1935 * merges it with the current contents of @manager. An enclosing <ui>
1936 * element is added if it is missing.
1938 * Return value: The merge id for the merged UI. The merge id can be used
1939 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1940 * the return value is 0.
1945 gtk_ui_manager_add_ui_from_string (GtkUIManager *manager,
1946 const gchar *buffer,
1950 gboolean needs_root = TRUE;
1954 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1955 g_return_val_if_fail (buffer != NULL, 0);
1958 length = strlen (buffer);
1961 end = buffer + length;
1962 while (p != end && xml_isspace (*p))
1965 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1968 return add_ui_from_string (manager, buffer, length, needs_root, error);
1972 * gtk_ui_manager_add_ui_from_file:
1973 * @manager: a #GtkUIManager object
1974 * @filename: (type filename): the name of the file to parse
1975 * @error: return location for an error
1977 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1978 * merges it with the current contents of @manager.
1980 * Return value: The merge id for the merged UI. The merge id can be used
1981 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1982 * the return value is 0.
1987 gtk_ui_manager_add_ui_from_file (GtkUIManager *manager,
1988 const gchar *filename,
1995 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1997 if (!g_file_get_contents (filename, &buffer, &length, error))
2000 res = add_ui_from_string (manager, buffer, length, FALSE, error);
2007 * gtk_ui_manager_add_ui_from_resource:
2008 * @manager: a #GtkUIManager object
2009 * @resource_path: the resource path of the file to parse
2010 * @error: return location for an error
2012 * Parses a resource file containing a <link linkend="XML-UI">UI definition</link> and
2013 * merges it with the current contents of @manager.
2015 * Return value: The merge id for the merged UI. The merge id can be used
2016 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
2017 * the return value is 0.
2022 gtk_ui_manager_add_ui_from_resource (GtkUIManager *manager,
2023 const gchar *resource_path,
2029 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2031 data = g_resources_lookup_data (resource_path, 0, error);
2035 res = add_ui_from_string (manager, g_bytes_get_data (data, NULL), g_bytes_get_size (data), FALSE, error);
2036 g_bytes_unref (data);
2042 * gtk_ui_manager_add_ui:
2043 * @manager: a #GtkUIManager
2044 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
2046 * @name: the name for the added UI element
2047 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
2048 * @type: the type of UI element to add.
2049 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
2050 * is added after its siblings.
2052 * Adds a UI element to the current contents of @manager.
2054 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
2055 * separator if such an element can be inserted at the place determined by
2056 * @path. Otherwise @type must indicate an element that can be inserted at
2057 * the place determined by @path.
2059 * If @path points to a menuitem or toolitem, the new element will be inserted
2060 * before or after this item, depending on @top.
2065 gtk_ui_manager_add_ui (GtkUIManager *manager,
2069 const gchar *action,
2070 GtkUIManagerItemType type,
2077 GQuark action_quark = 0;
2079 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2080 g_return_if_fail (merge_id > 0);
2081 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
2083 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
2089 node_type = NODE_TYPE_UNDECIDED;
2092 switch (NODE_INFO (node)->type)
2094 case NODE_TYPE_SEPARATOR:
2095 case NODE_TYPE_MENUITEM:
2096 case NODE_TYPE_TOOLITEM:
2098 node = node->parent;
2100 case NODE_TYPE_MENUBAR:
2101 case NODE_TYPE_MENU:
2102 case NODE_TYPE_POPUP:
2103 case NODE_TYPE_MENU_PLACEHOLDER:
2106 case GTK_UI_MANAGER_AUTO:
2108 node_type = NODE_TYPE_MENUITEM;
2110 node_type = NODE_TYPE_SEPARATOR;
2112 case GTK_UI_MANAGER_MENU:
2113 node_type = NODE_TYPE_MENU;
2115 case GTK_UI_MANAGER_MENUITEM:
2116 node_type = NODE_TYPE_MENUITEM;
2118 case GTK_UI_MANAGER_SEPARATOR:
2119 node_type = NODE_TYPE_SEPARATOR;
2121 case GTK_UI_MANAGER_PLACEHOLDER:
2122 node_type = NODE_TYPE_MENU_PLACEHOLDER;
2128 case NODE_TYPE_TOOLBAR:
2129 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2132 case GTK_UI_MANAGER_AUTO:
2134 node_type = NODE_TYPE_TOOLITEM;
2136 node_type = NODE_TYPE_SEPARATOR;
2138 case GTK_UI_MANAGER_TOOLITEM:
2139 node_type = NODE_TYPE_TOOLITEM;
2141 case GTK_UI_MANAGER_SEPARATOR:
2142 node_type = NODE_TYPE_SEPARATOR;
2144 case GTK_UI_MANAGER_PLACEHOLDER:
2145 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
2151 case NODE_TYPE_ROOT:
2154 case GTK_UI_MANAGER_MENUBAR:
2155 node_type = NODE_TYPE_MENUBAR;
2157 case GTK_UI_MANAGER_TOOLBAR:
2158 node_type = NODE_TYPE_TOOLBAR;
2160 case GTK_UI_MANAGER_POPUP:
2161 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
2162 node_type = NODE_TYPE_POPUP;
2164 case GTK_UI_MANAGER_ACCELERATOR:
2165 node_type = NODE_TYPE_ACCELERATOR;
2175 if (node_type == NODE_TYPE_UNDECIDED)
2177 g_warning ("item type %d not suitable for adding at '%s'",
2182 child = get_child_node (manager, node, sibling,
2183 name, name ? strlen (name) : 0,
2184 node_type, TRUE, top);
2186 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
2187 NODE_INFO (child)->popup_accels = TRUE;
2190 action_quark = g_quark_from_string (action);
2192 node_prepend_ui_reference (child, merge_id, action_quark);
2194 if (NODE_INFO (child)->action_name == 0)
2195 NODE_INFO (child)->action_name = action_quark;
2197 queue_update (manager);
2199 g_object_notify (G_OBJECT (manager), "ui");
2203 remove_ui (GNode *node,
2206 guint merge_id = GPOINTER_TO_UINT (user_data);
2208 node_remove_ui_reference (node, merge_id);
2210 return FALSE; /* continue */
2214 * gtk_ui_manager_remove_ui:
2215 * @manager: a #GtkUIManager object
2216 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
2218 * Unmerges the part of @manager<!-- -->s content identified by @merge_id.
2223 gtk_ui_manager_remove_ui (GtkUIManager *manager,
2226 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2228 g_node_traverse (manager->private_data->root_node,
2229 G_POST_ORDER, G_TRAVERSE_ALL, -1,
2230 remove_ui, GUINT_TO_POINTER (merge_id));
2232 queue_update (manager);
2234 g_object_notify (G_OBJECT (manager), "ui");
2237 /* -------------------- Updates -------------------- */
2241 get_action_by_name (GtkUIManager *merge,
2242 const gchar *action_name)
2250 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
2252 GtkActionGroup *action_group = tmp->data;
2255 action = gtk_action_group_get_action (action_group, action_name);
2265 find_menu_position (GNode *node,
2266 GtkWidget **menushell_p,
2269 GtkWidget *menushell;
2272 g_return_val_if_fail (node != NULL, FALSE);
2273 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
2274 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
2275 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
2276 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
2277 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2280 /* first sibling -- look at parent */
2281 if (node->prev == NULL)
2286 parent = node->parent;
2287 switch (NODE_INFO (parent)->type)
2289 case NODE_TYPE_MENUBAR:
2290 case NODE_TYPE_POPUP:
2291 menushell = NODE_INFO (parent)->proxy;
2294 case NODE_TYPE_MENU:
2295 menushell = NODE_INFO (parent)->proxy;
2296 if (GTK_IS_MENU_ITEM (menushell))
2297 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
2298 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
2299 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2303 g_list_free (siblings);
2305 case NODE_TYPE_MENU_PLACEHOLDER:
2306 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2307 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2308 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children,
2309 NODE_INFO (parent)->proxy) + 1;
2312 g_warning ("%s: bad parent node type %d", G_STRLOC,
2313 NODE_INFO (parent)->type);
2319 GtkWidget *prev_child;
2322 sibling = node->prev;
2323 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2324 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2326 prev_child = NODE_INFO (sibling)->proxy;
2328 if (!GTK_IS_WIDGET (prev_child))
2331 menushell = gtk_widget_get_parent (prev_child);
2332 if (!GTK_IS_MENU_SHELL (menushell))
2335 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children, prev_child) + 1;
2339 *menushell_p = menushell;
2347 find_toolbar_position (GNode *node,
2348 GtkWidget **toolbar_p,
2354 g_return_val_if_fail (node != NULL, FALSE);
2355 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2356 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2357 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2358 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2361 /* first sibling -- look at parent */
2362 if (node->prev == NULL)
2366 parent = node->parent;
2367 switch (NODE_INFO (parent)->type)
2369 case NODE_TYPE_TOOLBAR:
2370 toolbar = NODE_INFO (parent)->proxy;
2373 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2374 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2375 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2376 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2377 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2380 g_warning ("%s: bad parent node type %d", G_STRLOC,
2381 NODE_INFO (parent)->type);
2387 GtkWidget *prev_child;
2390 sibling = node->prev;
2391 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2392 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2394 prev_child = NODE_INFO (sibling)->proxy;
2396 if (!GTK_IS_WIDGET (prev_child))
2399 toolbar = gtk_widget_get_parent (prev_child);
2400 if (!GTK_IS_TOOLBAR (toolbar))
2403 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2404 GTK_TOOL_ITEM (prev_child)) + 1;
2408 *toolbar_p = toolbar;
2416 * _gtk_menu_is_empty:
2417 * @menu: (allow-none): a #GtkMenu or %NULL
2419 * Determines whether @menu is empty. A menu is considered empty if it
2420 * the only visible children are tearoff menu items or "filler" menu
2421 * items which were inserted to mark the menu as empty.
2423 * This function is used by #GtkAction.
2425 * Return value: whether @menu is empty.
2428 _gtk_menu_is_empty (GtkWidget *menu)
2430 GList *children, *cur;
2431 gboolean result = TRUE;
2433 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2438 children = gtk_container_get_children (GTK_CONTAINER (menu));
2443 if (gtk_widget_get_visible (cur->data))
2445 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2446 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2454 g_list_free (children);
2460 SEPARATOR_MODE_SMART,
2461 SEPARATOR_MODE_VISIBLE,
2462 SEPARATOR_MODE_HIDDEN
2466 update_smart_separators (GtkWidget *proxy)
2468 GtkWidget *parent = NULL;
2470 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2472 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2473 parent = gtk_widget_get_parent (proxy);
2479 GList *children, *cur, *last;
2482 children = gtk_container_get_children (GTK_CONTAINER (parent));
2492 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2496 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2497 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2500 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2501 "gtk-separator-mode"));
2504 case SEPARATOR_MODE_VISIBLE:
2505 gtk_widget_show (GTK_WIDGET (cur->data));
2509 case SEPARATOR_MODE_HIDDEN:
2510 gtk_widget_hide (GTK_WIDGET (cur->data));
2512 case SEPARATOR_MODE_SMART:
2515 gtk_widget_show (GTK_WIDGET (cur->data));
2520 gtk_widget_hide (GTK_WIDGET (cur->data));
2524 else if (gtk_widget_get_visible (cur->data))
2527 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2540 gtk_widget_hide (GTK_WIDGET (last->data));
2542 if (GTK_IS_MENU (parent))
2546 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2547 if (GTK_IS_MENU_ITEM (item))
2548 _gtk_action_sync_menu_visible (NULL, item, empty);
2549 if (GTK_IS_WIDGET (filler))
2552 gtk_widget_show (filler);
2554 gtk_widget_hide (filler);
2558 g_list_free (children);
2563 update_node (GtkUIManager *manager,
2566 gboolean popup_accels)
2571 const gchar *action_name;
2572 NodeUIReference *ref;
2574 #ifdef DEBUG_UI_MANAGER
2578 g_return_if_fail (node != NULL);
2579 g_return_if_fail (NODE_INFO (node) != NULL);
2581 info = NODE_INFO (node);
2586 if (info->type == NODE_TYPE_POPUP)
2589 popup_accels = info->popup_accels;
2592 #ifdef DEBUG_UI_MANAGER
2593 g_print ("update_node name=%s dirty=%d popup %d (",
2594 info->name, info->dirty, in_popup);
2595 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2597 NodeUIReference *ref = tmp->data;
2598 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2605 if (info->uifiles == NULL) {
2606 /* We may need to remove this node.
2607 * This must be done in post order
2609 goto recurse_children;
2612 ref = info->uifiles->data;
2613 action_name = g_quark_to_string (ref->action_quark);
2614 action = get_action_by_name (manager, action_name);
2616 info->dirty = FALSE;
2618 /* Check if the node doesn't have an action and must have an action */
2619 if (action == NULL &&
2620 info->type != NODE_TYPE_ROOT &&
2621 info->type != NODE_TYPE_MENUBAR &&
2622 info->type != NODE_TYPE_TOOLBAR &&
2623 info->type != NODE_TYPE_POPUP &&
2624 info->type != NODE_TYPE_SEPARATOR &&
2625 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2626 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2628 g_warning ("%s: missing action %s", info->name, action_name);
2634 gtk_action_set_accel_group (action, manager->private_data->accel_group);
2636 /* If the widget already has a proxy and the action hasn't changed, then
2637 * we only have to update the tearoff menu items.
2639 if (info->proxy != NULL && action == info->action)
2641 if (info->type == NODE_TYPE_MENU)
2646 if (GTK_IS_MENU (info->proxy))
2649 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2650 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2651 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2653 if (manager->private_data->add_tearoffs && !in_popup)
2654 gtk_widget_show (GTK_WIDGET (siblings->data));
2656 gtk_widget_hide (GTK_WIDGET (siblings->data));
2658 g_list_free (siblings);
2661 goto recurse_children;
2666 case NODE_TYPE_MENUBAR:
2667 if (info->proxy == NULL)
2669 info->proxy = gtk_menu_bar_new ();
2670 g_object_ref_sink (info->proxy);
2671 gtk_widget_set_name (info->proxy, info->name);
2672 gtk_widget_show (info->proxy);
2673 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2676 case NODE_TYPE_POPUP:
2677 if (info->proxy == NULL)
2679 info->proxy = gtk_menu_new ();
2680 g_object_ref_sink (info->proxy);
2682 gtk_widget_set_name (info->proxy, info->name);
2684 case NODE_TYPE_MENU:
2686 GtkWidget *prev_submenu = NULL;
2687 GtkWidget *menu = NULL;
2690 /* remove the proxy if it is of the wrong type ... */
2692 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2694 if (GTK_IS_MENU_ITEM (info->proxy))
2696 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2699 g_object_ref (prev_submenu);
2700 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2704 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2705 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2707 g_object_unref (info->proxy);
2711 /* create proxy if needed ... */
2712 if (info->proxy == NULL)
2714 /* ... if the action already provides a menu, then use
2715 * that menu instead of creating an empty one
2717 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2718 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2719 GTK_ACTION_GET_CLASS (action)->create_menu)
2721 menu = gtk_action_create_menu (action);
2729 menu = gtk_menu_new ();
2730 gtk_widget_set_name (menu, info->name);
2731 tearoff = gtk_tearoff_menu_item_new ();
2732 gtk_widget_set_no_show_all (tearoff, TRUE);
2733 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2734 filler = gtk_menu_item_new_with_label (_("Empty"));
2735 g_object_set_data (G_OBJECT (filler),
2736 I_("gtk-empty-menu-item"),
2737 GINT_TO_POINTER (TRUE));
2738 gtk_widget_set_sensitive (filler, FALSE);
2739 gtk_widget_set_no_show_all (filler, TRUE);
2740 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2743 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2746 g_object_ref_sink (info->proxy);
2747 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2752 GtkWidget *menushell;
2755 if (find_menu_position (node, &menushell, &pos))
2757 info->proxy = gtk_action_create_menu_item (action);
2758 g_object_ref_sink (info->proxy);
2759 g_signal_connect (info->proxy, "notify::visible",
2760 G_CALLBACK (update_smart_separators), NULL);
2761 gtk_widget_set_name (info->proxy, info->name);
2763 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2764 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2769 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2773 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2775 g_object_unref (prev_submenu);
2778 if (GTK_IS_MENU (info->proxy))
2781 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2783 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2784 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2786 if (manager->private_data->add_tearoffs && !in_popup)
2787 gtk_widget_show (GTK_WIDGET (siblings->data));
2789 gtk_widget_hide (GTK_WIDGET (siblings->data));
2791 g_list_free (siblings);
2794 case NODE_TYPE_UNDECIDED:
2795 g_warning ("found undecided node!");
2797 case NODE_TYPE_ROOT:
2799 case NODE_TYPE_TOOLBAR:
2800 if (info->proxy == NULL)
2802 info->proxy = gtk_toolbar_new ();
2803 g_object_ref_sink (info->proxy);
2804 gtk_widget_set_name (info->proxy, info->name);
2805 gtk_widget_show (info->proxy);
2806 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2809 case NODE_TYPE_MENU_PLACEHOLDER:
2810 /* create menu items for placeholders if necessary ... */
2811 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2812 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2816 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2818 g_object_unref (info->proxy);
2823 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2825 g_object_unref (info->extra);
2829 if (info->proxy == NULL)
2831 GtkWidget *menushell;
2834 if (find_menu_position (node, &menushell, &pos))
2836 info->proxy = gtk_separator_menu_item_new ();
2837 g_object_ref_sink (info->proxy);
2838 g_object_set_data (G_OBJECT (info->proxy),
2839 I_("gtk-separator-mode"),
2840 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2841 gtk_widget_set_no_show_all (info->proxy, TRUE);
2842 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2843 NODE_INFO (node)->proxy, pos);
2845 info->extra = gtk_separator_menu_item_new ();
2846 g_object_ref_sink (info->extra);
2847 g_object_set_data (G_OBJECT (info->extra),
2848 I_("gtk-separator-mode"),
2849 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2850 gtk_widget_set_no_show_all (info->extra, TRUE);
2851 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2852 NODE_INFO (node)->extra, pos + 1);
2856 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2857 /* create toolbar items for placeholders if necessary ... */
2858 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2859 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2863 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2865 g_object_unref (info->proxy);
2870 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2872 g_object_unref (info->extra);
2876 if (info->proxy == NULL)
2882 if (find_toolbar_position (node, &toolbar, &pos))
2884 item = gtk_separator_tool_item_new ();
2885 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2886 info->proxy = GTK_WIDGET (item);
2887 g_object_ref_sink (info->proxy);
2888 g_object_set_data (G_OBJECT (info->proxy),
2889 I_("gtk-separator-mode"),
2890 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2891 gtk_widget_set_no_show_all (info->proxy, TRUE);
2893 item = gtk_separator_tool_item_new ();
2894 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2895 info->extra = GTK_WIDGET (item);
2896 g_object_ref_sink (info->extra);
2897 g_object_set_data (G_OBJECT (info->extra),
2898 I_("gtk-separator-mode"),
2899 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2900 gtk_widget_set_no_show_all (info->extra, TRUE);
2904 case NODE_TYPE_MENUITEM:
2905 /* remove the proxy if it is of the wrong type ... */
2907 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2909 g_signal_handlers_disconnect_by_func (info->proxy,
2910 G_CALLBACK (update_smart_separators),
2912 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2913 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2915 g_object_unref (info->proxy);
2918 /* create proxy if needed ... */
2919 if (info->proxy == NULL)
2921 GtkWidget *menushell;
2924 if (find_menu_position (node, &menushell, &pos))
2926 info->proxy = gtk_action_create_menu_item (action);
2927 g_object_ref_sink (info->proxy);
2928 gtk_widget_set_name (info->proxy, info->name);
2930 if (info->always_show_image_set &&
2931 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2932 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2933 info->always_show_image);
2935 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2941 g_signal_handlers_disconnect_by_func (info->proxy,
2942 G_CALLBACK (update_smart_separators),
2944 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
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);
2952 if (in_popup && !popup_accels)
2954 /* don't show accels in popups */
2955 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2956 if (GTK_IS_ACCEL_LABEL (child))
2957 g_object_set (child, "accel-closure", NULL, NULL);
2962 case NODE_TYPE_TOOLITEM:
2963 /* remove the proxy if it is of the wrong type ... */
2965 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2967 g_signal_handlers_disconnect_by_func (info->proxy,
2968 G_CALLBACK (update_smart_separators),
2970 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2971 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2973 g_object_unref (info->proxy);
2976 /* create proxy if needed ... */
2977 if (info->proxy == NULL)
2982 if (find_toolbar_position (node, &toolbar, &pos))
2984 info->proxy = gtk_action_create_tool_item (action);
2985 g_object_ref_sink (info->proxy);
2986 gtk_widget_set_name (info->proxy, info->name);
2988 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2989 GTK_TOOL_ITEM (info->proxy), pos);
2994 g_signal_handlers_disconnect_by_func (info->proxy,
2995 G_CALLBACK (update_smart_separators),
2997 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
3002 g_signal_connect (info->proxy, "notify::visible",
3003 G_CALLBACK (update_smart_separators), NULL);
3006 case NODE_TYPE_SEPARATOR:
3007 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
3008 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
3012 gint separator_mode;
3015 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
3017 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3019 g_object_unref (info->proxy);
3023 if (find_toolbar_position (node, &toolbar, &pos))
3025 item = gtk_separator_tool_item_new ();
3026 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
3027 info->proxy = GTK_WIDGET (item);
3028 g_object_ref_sink (info->proxy);
3029 gtk_widget_set_no_show_all (info->proxy, TRUE);
3032 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
3033 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
3034 separator_mode = SEPARATOR_MODE_VISIBLE;
3037 separator_mode = SEPARATOR_MODE_SMART;
3039 g_object_set_data (G_OBJECT (info->proxy),
3040 I_("gtk-separator-mode"),
3041 GINT_TO_POINTER (separator_mode));
3042 gtk_widget_show (info->proxy);
3047 GtkWidget *menushell;
3050 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
3052 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3054 g_object_unref (info->proxy);
3058 if (find_menu_position (node, &menushell, &pos))
3060 info->proxy = gtk_separator_menu_item_new ();
3061 g_object_ref_sink (info->proxy);
3062 gtk_widget_set_no_show_all (info->proxy, TRUE);
3063 g_object_set_data (G_OBJECT (info->proxy),
3064 I_("gtk-separator-mode"),
3065 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
3066 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
3068 gtk_widget_show (info->proxy);
3072 case NODE_TYPE_ACCELERATOR:
3073 gtk_action_connect_accelerator (action);
3078 g_object_ref (action);
3080 g_object_unref (info->action);
3081 info->action = action;
3084 /* process children */
3085 child = node->children;
3091 child = current->next;
3092 update_node (manager, current, in_popup, popup_accels);
3097 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
3098 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
3099 else if (info->type == NODE_TYPE_MENU ||
3100 info->type == NODE_TYPE_TOOLBAR ||
3101 info->type == NODE_TYPE_POPUP)
3102 update_smart_separators (info->proxy);
3105 /* handle cleanup of dead nodes */
3106 if (node->children == NULL && info->uifiles == NULL)
3109 gtk_widget_destroy (info->proxy);
3111 gtk_widget_destroy (info->extra);
3112 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
3113 gtk_action_disconnect_accelerator (info->action);
3115 g_node_destroy (node);
3120 do_updates (GtkUIManager *manager)
3122 /* this function needs to check through the tree for dirty nodes.
3123 * For such nodes, it needs to do the following:
3125 * 1) check if they are referenced by any loaded UI files anymore.
3126 * In which case, the proxy widget should be destroyed, unless
3127 * there are any subnodes.
3129 * 2) lookup the action for this node again. If it is different to
3130 * the current one (or if no previous action has been looked up),
3131 * the proxy is reconnected to the new action (or a new proxy widget
3132 * is created and added to the parent container).
3134 update_node (manager, manager->private_data->root_node, FALSE, FALSE);
3136 manager->private_data->update_tag = 0;
3142 do_updates_idle (GtkUIManager *manager)
3144 do_updates (manager);
3150 queue_update (GtkUIManager *manager)
3152 if (manager->private_data->update_tag != 0)
3155 manager->private_data->update_tag = gdk_threads_add_idle (
3156 (GSourceFunc)do_updates_idle,
3162 * gtk_ui_manager_ensure_update:
3163 * @manager: a #GtkUIManager
3165 * Makes sure that all pending updates to the UI have been completed.
3167 * This may occasionally be necessary, since #GtkUIManager updates the
3168 * UI in an idle function. A typical example where this function is
3169 * useful is to enforce that the menubar and toolbar have been added to
3170 * the main window before showing it:
3172 * gtk_container_add (GTK_CONTAINER (window), vbox);
3173 * g_signal_connect (merge, "add-widget",
3174 * G_CALLBACK (add_widget), vbox);
3175 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
3176 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
3177 * gtk_ui_manager_ensure_update (merge);
3178 * gtk_widget_show (window);
3184 gtk_ui_manager_ensure_update (GtkUIManager *manager)
3186 if (manager->private_data->update_tag != 0)
3188 g_source_remove (manager->private_data->update_tag);
3189 do_updates (manager);
3194 dirty_traverse_func (GNode *node,
3197 NODE_INFO (node)->dirty = TRUE;
3202 dirty_all_nodes (GtkUIManager *manager)
3204 g_node_traverse (manager->private_data->root_node,
3205 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3206 dirty_traverse_func, NULL);
3207 queue_update (manager);
3211 mark_node_dirty (GNode *node)
3215 /* FIXME could optimize this */
3216 for (p = node; p; p = p->parent)
3217 NODE_INFO (p)->dirty = TRUE;
3220 static const gchar *
3221 open_tag_format (NodeType type)
3225 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
3226 case NODE_TYPE_ROOT: return "%*s<ui";
3227 case NODE_TYPE_MENUBAR: return "%*s<menubar";
3228 case NODE_TYPE_MENU: return "%*s<menu";
3229 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
3230 case NODE_TYPE_MENU_PLACEHOLDER:
3231 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
3232 case NODE_TYPE_POPUP: return "%*s<popup";
3233 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
3234 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
3235 case NODE_TYPE_SEPARATOR: return "%*s<separator";
3236 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
3237 default: return NULL;
3241 static const gchar *
3242 close_tag_format (NodeType type)
3246 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
3247 case NODE_TYPE_ROOT: return "%*s</ui>\n";
3248 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
3249 case NODE_TYPE_MENU: return "%*s</menu>\n";
3250 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
3251 case NODE_TYPE_MENU_PLACEHOLDER:
3252 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
3253 case NODE_TYPE_POPUP: return "%*s</popup>\n";
3254 default: return NULL;
3259 print_node (GtkUIManager *manager,
3266 const gchar *open_fmt;
3267 const gchar *close_fmt;
3271 open_fmt = open_tag_format (mnode->type);
3272 close_fmt = close_tag_format (mnode->type);
3274 g_string_append_printf (buffer, open_fmt, indent_level, "");
3276 if (mnode->type != NODE_TYPE_ROOT)
3279 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
3281 if (mnode->action_name)
3282 g_string_append_printf (buffer, " action=\"%s\"",
3283 g_quark_to_string (mnode->action_name));
3286 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
3288 for (child = node->children; child != NULL; child = child->next)
3289 print_node (manager, child, indent_level + 2, buffer);
3292 g_string_append_printf (buffer, close_fmt, indent_level, "");
3296 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
3297 GtkBuilder *builder,
3299 const gchar *tagname,
3300 GMarkupParser *parser,
3306 if (strcmp (tagname, "ui") == 0)
3310 ctx = g_new0 (ParseContext, 1);
3311 ctx->state = STATE_START;
3312 ctx->manager = GTK_UI_MANAGER (buildable);
3313 ctx->current = NULL;
3314 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3317 *parser = ui_parser;
3327 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3328 GtkBuilder *builder,
3330 const gchar *tagname,
3333 queue_update (GTK_UI_MANAGER (buildable));
3334 g_object_notify (G_OBJECT (buildable), "ui");
3339 * gtk_ui_manager_get_ui:
3340 * @manager: a #GtkUIManager
3342 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3344 * Return value: A newly allocated string containing an XML representation of
3350 gtk_ui_manager_get_ui (GtkUIManager *manager)
3354 buffer = g_string_new (NULL);
3356 gtk_ui_manager_ensure_update (manager);
3358 print_node (manager, manager->private_data->root_node, 0, buffer);
3360 return g_string_free (buffer, FALSE);