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 "gtktoolbar.h"
47 #include "gtkwindow.h"
48 #include "gtkprivate.h"
50 #include "gtkuimanager.h"
51 #include "deprecated/gtktearoffmenuitem.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,
425 static void gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
426 gboolean add_tearoffs);
448 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
450 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
451 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
452 gtk_ui_manager_buildable_init))
455 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
457 GObjectClass *gobject_class;
459 gobject_class = G_OBJECT_CLASS (klass);
461 gobject_class->finalize = gtk_ui_manager_finalize;
462 gobject_class->set_property = gtk_ui_manager_set_property;
463 gobject_class->get_property = gtk_ui_manager_get_property;
464 klass->get_widget = gtk_ui_manager_real_get_widget;
465 klass->get_action = gtk_ui_manager_real_get_action;
468 * GtkUIManager:add-tearoffs:
470 * The "add-tearoffs" property controls whether generated menus
471 * have tearoff menu items.
473 * Note that this only affects regular menus. Generated popup
474 * menus never have tearoff menu items.
478 * Deprecated: 3.4: Tearoff menus are deprecated and should not
479 * be used in newly written code.
481 g_object_class_install_property (gobject_class,
483 g_param_spec_boolean ("add-tearoffs",
484 P_("Add tearoffs to menus"),
485 P_("Whether tearoff menu items should be added to menus"),
487 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
489 g_object_class_install_property (gobject_class,
491 g_param_spec_string ("ui",
492 P_("Merged UI definition"),
493 P_("An XML string describing the merged UI"),
495 GTK_PARAM_READABLE));
499 * GtkUIManager::add-widget:
500 * @manager: a #GtkUIManager
501 * @widget: the added widget
503 * The ::add-widget signal is emitted for each generated menubar and toolbar.
504 * It is not emitted for generated popup menus, which can be obtained by
505 * gtk_ui_manager_get_widget().
509 ui_manager_signals[ADD_WIDGET] =
510 g_signal_new (I_("add-widget"),
511 G_OBJECT_CLASS_TYPE (klass),
512 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
513 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
515 g_cclosure_marshal_VOID__OBJECT,
520 * GtkUIManager::actions-changed:
521 * @manager: a #GtkUIManager
523 * The ::actions-changed signal is emitted whenever the set of actions
528 ui_manager_signals[ACTIONS_CHANGED] =
529 g_signal_new (I_("actions-changed"),
530 G_OBJECT_CLASS_TYPE (klass),
531 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
532 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
534 g_cclosure_marshal_VOID__VOID,
538 * GtkUIManager::connect-proxy:
539 * @manager: the ui manager
540 * @action: the action
543 * The ::connect-proxy signal is emitted after connecting a proxy to
544 * an action in the group.
546 * This is intended for simple customizations for which a custom action
547 * class would be too clumsy, e.g. showing tooltips for menuitems in the
552 ui_manager_signals[CONNECT_PROXY] =
553 g_signal_new (I_("connect-proxy"),
554 G_OBJECT_CLASS_TYPE (klass),
555 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
556 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
558 _gtk_marshal_VOID__OBJECT_OBJECT,
564 * GtkUIManager::disconnect-proxy:
565 * @manager: the ui manager
566 * @action: the action
569 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
570 * from an action in the group.
574 ui_manager_signals[DISCONNECT_PROXY] =
575 g_signal_new (I_("disconnect-proxy"),
576 G_OBJECT_CLASS_TYPE (klass),
577 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
578 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
580 _gtk_marshal_VOID__OBJECT_OBJECT,
586 * GtkUIManager::pre-activate:
587 * @manager: the ui manager
588 * @action: the action
590 * The ::pre-activate signal is emitted just before the @action
593 * This is intended for applications to get notification
594 * just before any action is activated.
598 ui_manager_signals[PRE_ACTIVATE] =
599 g_signal_new (I_("pre-activate"),
600 G_OBJECT_CLASS_TYPE (klass),
601 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
602 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
604 _gtk_marshal_VOID__OBJECT,
609 * GtkUIManager::post-activate:
610 * @manager: the ui manager
611 * @action: the action
613 * The ::post-activate signal is emitted just after the @action
616 * This is intended for applications to get notification
617 * just after any action is activated.
621 ui_manager_signals[POST_ACTIVATE] =
622 g_signal_new (I_("post-activate"),
623 G_OBJECT_CLASS_TYPE (klass),
624 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
625 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
627 _gtk_marshal_VOID__OBJECT,
631 klass->add_widget = NULL;
632 klass->actions_changed = NULL;
633 klass->connect_proxy = NULL;
634 klass->disconnect_proxy = NULL;
635 klass->pre_activate = NULL;
636 klass->post_activate = NULL;
638 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
643 gtk_ui_manager_init (GtkUIManager *manager)
648 manager->private_data = G_TYPE_INSTANCE_GET_PRIVATE (manager,
650 GtkUIManagerPrivate);
652 manager->private_data->accel_group = gtk_accel_group_new ();
654 manager->private_data->root_node = NULL;
655 manager->private_data->action_groups = NULL;
657 manager->private_data->last_merge_id = 0;
658 manager->private_data->add_tearoffs = FALSE;
660 merge_id = gtk_ui_manager_new_merge_id (manager);
661 node = get_child_node (manager, NULL, NULL, "ui", 2,
662 NODE_TYPE_ROOT, TRUE, FALSE);
663 node_prepend_ui_reference (node, merge_id, 0);
667 gtk_ui_manager_finalize (GObject *object)
669 GtkUIManager *manager = GTK_UI_MANAGER (object);
671 if (manager->private_data->update_tag != 0)
673 g_source_remove (manager->private_data->update_tag);
674 manager->private_data->update_tag = 0;
677 g_node_traverse (manager->private_data->root_node,
678 G_POST_ORDER, G_TRAVERSE_ALL, -1,
679 (GNodeTraverseFunc)free_node, NULL);
680 g_node_destroy (manager->private_data->root_node);
681 manager->private_data->root_node = NULL;
683 g_list_free_full (manager->private_data->action_groups, g_object_unref);
684 manager->private_data->action_groups = NULL;
686 g_object_unref (manager->private_data->accel_group);
687 manager->private_data->accel_group = NULL;
689 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
693 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
695 iface->add_child = gtk_ui_manager_buildable_add_child;
696 iface->construct_child = gtk_ui_manager_buildable_construct_child;
697 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
698 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
702 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
707 GtkUIManager *manager = GTK_UI_MANAGER (buildable);
710 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
712 pos = g_list_length (manager->private_data->action_groups);
714 g_object_ref (child);
715 gtk_ui_manager_insert_action_group (manager,
716 GTK_ACTION_GROUP (child),
721 child_hierarchy_changed_cb (GtkWidget *widget,
726 GtkAccelGroup *group;
729 toplevel = gtk_widget_get_toplevel (widget);
730 if (!toplevel || !GTK_IS_WINDOW (toplevel))
733 group = gtk_ui_manager_get_accel_group (uimgr);
734 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
735 if (g_slist_find (groups, group) == NULL)
736 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
738 g_signal_handlers_disconnect_by_func (widget,
739 child_hierarchy_changed_cb,
744 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
751 name = g_strdup_printf ("ui/%s", id);
752 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
755 g_error ("Unknown ui manager child: %s\n", name);
761 g_signal_connect (widget, "hierarchy-changed",
762 G_CALLBACK (child_hierarchy_changed_cb),
763 GTK_UI_MANAGER (buildable));
764 return g_object_ref (widget);
768 gtk_ui_manager_set_property (GObject *object,
773 GtkUIManager *manager = GTK_UI_MANAGER (object);
777 case PROP_ADD_TEAROFFS:
778 gtk_ui_manager_do_set_add_tearoffs (manager, g_value_get_boolean (value));
781 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
787 gtk_ui_manager_get_property (GObject *object,
792 GtkUIManager *manager = GTK_UI_MANAGER (object);
796 case PROP_ADD_TEAROFFS:
797 g_value_set_boolean (value, manager->private_data->add_tearoffs);
800 g_value_take_string (value, gtk_ui_manager_get_ui (manager));
803 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
809 gtk_ui_manager_real_get_widget (GtkUIManager *manager,
814 /* ensure that there are no pending updates before we get the
816 gtk_ui_manager_ensure_update (manager);
818 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
823 return NODE_INFO (node)->proxy;
827 gtk_ui_manager_real_get_action (GtkUIManager *manager,
832 /* ensure that there are no pending updates before we get
834 gtk_ui_manager_ensure_update (manager);
836 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
841 return NODE_INFO (node)->action;
846 * gtk_ui_manager_new:
848 * Creates a new ui manager object.
850 * Return value: a new ui manager object.
855 gtk_ui_manager_new (void)
857 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
862 * gtk_ui_manager_get_add_tearoffs:
863 * @manager: a #GtkUIManager
865 * Returns whether menus generated by this #GtkUIManager
866 * will have tearoff menu items.
868 * Return value: whether tearoff menu items are added
872 * Deprecated: 3.4: Tearoff menus are deprecated and should not
873 * be used in newly written code.
876 gtk_ui_manager_get_add_tearoffs (GtkUIManager *manager)
878 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), FALSE);
880 return manager->private_data->add_tearoffs;
885 * gtk_ui_manager_set_add_tearoffs:
886 * @manager: a #GtkUIManager
887 * @add_tearoffs: whether tearoff menu items are added
889 * Sets the "add_tearoffs" property, which controls whether menus
890 * generated by this #GtkUIManager will have tearoff menu items.
892 * Note that this only affects regular menus. Generated popup
893 * menus never have tearoff menu items.
897 * Deprecated: 3.4: Tearoff menus are deprecated and should not
898 * be used in newly written code.
901 gtk_ui_manager_set_add_tearoffs (GtkUIManager *manager,
902 gboolean add_tearoffs)
904 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
906 gtk_ui_manager_do_set_add_tearoffs (manager, add_tearoffs);
910 gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
911 gboolean add_tearoffs)
913 add_tearoffs = add_tearoffs != FALSE;
915 if (add_tearoffs != manager->private_data->add_tearoffs)
917 manager->private_data->add_tearoffs = add_tearoffs;
919 dirty_all_nodes (manager);
921 g_object_notify (G_OBJECT (manager), "add-tearoffs");
926 cb_proxy_connect_proxy (GtkActionGroup *group,
929 GtkUIManager *manager)
931 g_signal_emit (manager, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
935 cb_proxy_disconnect_proxy (GtkActionGroup *group,
938 GtkUIManager *manager)
940 g_signal_emit (manager, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
944 cb_proxy_pre_activate (GtkActionGroup *group,
946 GtkUIManager *manager)
948 g_signal_emit (manager, ui_manager_signals[PRE_ACTIVATE], 0, action);
952 cb_proxy_post_activate (GtkActionGroup *group,
954 GtkUIManager *manager)
956 g_signal_emit (manager, ui_manager_signals[POST_ACTIVATE], 0, action);
960 * gtk_ui_manager_insert_action_group:
961 * @manager: a #GtkUIManager object
962 * @action_group: the action group to be inserted
963 * @pos: the position at which the group will be inserted.
965 * Inserts an action group into the list of action groups associated
966 * with @manager. Actions in earlier groups hide actions with the same
967 * name in later groups.
969 * If @pos is larger than the number of action groups in @manager, or
970 * negative, @action_group will be inserted at the end of the internal
976 gtk_ui_manager_insert_action_group (GtkUIManager *manager,
977 GtkActionGroup *action_group,
980 #ifdef G_ENABLE_DEBUG
982 const char *group_name;
985 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
986 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
987 g_return_if_fail (g_list_find (manager->private_data->action_groups,
988 action_group) == NULL);
990 #ifdef G_ENABLE_DEBUG
991 group_name = gtk_action_group_get_name (action_group);
993 for (l = manager->private_data->action_groups; l; l = l->next)
995 GtkActionGroup *group = l->data;
997 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
999 g_warning ("Inserting action group '%s' into UI manager which "
1000 "already has a group with this name\n", group_name);
1004 #endif /* G_ENABLE_DEBUG */
1006 g_object_ref (action_group);
1007 manager->private_data->action_groups =
1008 g_list_insert (manager->private_data->action_groups, action_group, pos);
1009 g_object_connect (action_group,
1010 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1011 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1012 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1013 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1016 /* dirty all nodes, as action bindings may change */
1017 dirty_all_nodes (manager);
1019 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1023 * gtk_ui_manager_remove_action_group:
1024 * @manager: a #GtkUIManager object
1025 * @action_group: the action group to be removed
1027 * Removes an action group from the list of action groups associated
1033 gtk_ui_manager_remove_action_group (GtkUIManager *manager,
1034 GtkActionGroup *action_group)
1036 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
1037 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1038 g_return_if_fail (g_list_find (manager->private_data->action_groups,
1039 action_group) != NULL);
1041 manager->private_data->action_groups =
1042 g_list_remove (manager->private_data->action_groups, action_group);
1044 g_object_disconnect (action_group,
1045 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1046 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1047 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1048 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1050 g_object_unref (action_group);
1052 /* dirty all nodes, as action bindings may change */
1053 dirty_all_nodes (manager);
1055 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1059 * gtk_ui_manager_get_action_groups:
1060 * @manager: a #GtkUIManager object
1062 * Returns the list of action groups associated with @manager.
1064 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
1065 * action groups. The list is owned by GTK+
1066 * and should not be modified.
1071 gtk_ui_manager_get_action_groups (GtkUIManager *manager)
1073 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1075 return manager->private_data->action_groups;
1079 * gtk_ui_manager_get_accel_group:
1080 * @manager: a #GtkUIManager object
1082 * Returns the #GtkAccelGroup associated with @manager.
1084 * Return value: (transfer none): the #GtkAccelGroup.
1089 gtk_ui_manager_get_accel_group (GtkUIManager *manager)
1091 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1093 return manager->private_data->accel_group;
1097 * gtk_ui_manager_get_widget:
1098 * @manager: a #GtkUIManager
1101 * Looks up a widget by following a path.
1102 * The path consists of the names specified in the XML description of the UI.
1103 * separated by '/'. Elements which don't have a name or action attribute in
1104 * the XML (e.g. <popup>) can be addressed by their XML element name
1105 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
1107 * Note that the widget found by following a path that ends in a <menu>
1108 * element is the menuitem to which the menu is attached, not the menu itmanager.
1110 * Also note that the widgets constructed by a ui manager are not tied to
1111 * the lifecycle of the ui manager. If you add the widgets returned by this
1112 * function to some container or explicitly ref them, they will survive the
1113 * destruction of the ui manager.
1115 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
1121 gtk_ui_manager_get_widget (GtkUIManager *manager,
1124 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1125 g_return_val_if_fail (path != NULL, NULL);
1127 return GTK_UI_MANAGER_GET_CLASS (manager)->get_widget (manager, path);
1131 GtkUIManagerItemType types;
1136 collect_toplevels (GNode *node,
1139 ToplevelData *data = user_data;
1141 if (NODE_INFO (node)->proxy)
1143 switch (NODE_INFO (node)->type)
1145 case NODE_TYPE_MENUBAR:
1146 if (data->types & GTK_UI_MANAGER_MENUBAR)
1147 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1149 case NODE_TYPE_TOOLBAR:
1150 if (data->types & GTK_UI_MANAGER_TOOLBAR)
1151 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1153 case NODE_TYPE_POPUP:
1154 if (data->types & GTK_UI_MANAGER_POPUP)
1155 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1163 * gtk_ui_manager_get_toplevels:
1164 * @manager: a #GtkUIManager
1165 * @types: specifies the types of toplevel widgets to include. Allowed
1166 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
1167 * #GTK_UI_MANAGER_POPUP.
1169 * Obtains a list of all toplevel widgets of the requested types.
1171 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
1172 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
1177 gtk_ui_manager_get_toplevels (GtkUIManager *manager,
1178 GtkUIManagerItemType types)
1182 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1183 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
1184 GTK_UI_MANAGER_TOOLBAR |
1185 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
1191 g_node_children_foreach (manager->private_data->root_node,
1193 collect_toplevels, &data);
1200 * gtk_ui_manager_get_action:
1201 * @manager: a #GtkUIManager
1204 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
1205 * for more information about paths.
1207 * Return value: (transfer none): the action whose proxy widget is found by following the path,
1208 * or %NULL if no widget was found.
1213 gtk_ui_manager_get_action (GtkUIManager *manager,
1216 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1217 g_return_val_if_fail (path != NULL, NULL);
1219 return GTK_UI_MANAGER_GET_CLASS (manager)->get_action (manager, path);
1223 node_is_dead (GNode *node)
1227 if (NODE_INFO (node)->uifiles != NULL)
1230 for (child = node->children; child != NULL; child = child->next)
1232 if (!node_is_dead (child))
1240 get_child_node (GtkUIManager *manager,
1243 const gchar *childname,
1244 gint childname_length,
1249 GNode *child = NULL;
1255 for (child = parent->children; child != NULL; child = child->next)
1257 if (NODE_INFO (child)->name &&
1258 strlen (NODE_INFO (child)->name) == childname_length &&
1259 !strncmp (NODE_INFO (child)->name, childname, childname_length))
1261 /* if undecided about node type, set it */
1262 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
1263 NODE_INFO (child)->type = node_type;
1265 /* warn about type mismatch */
1266 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
1267 node_type != NODE_TYPE_UNDECIDED &&
1268 NODE_INFO (child)->type != node_type)
1269 g_warning ("node type doesn't match %d (%s is type %d)",
1271 NODE_INFO (child)->name,
1272 NODE_INFO (child)->type);
1274 if (node_is_dead (child))
1276 /* This node was removed but is still dirty so
1277 * it is still in the tree. We want to treat this
1278 * as if it didn't exist, which means we move it
1279 * to the position it would have been created at.
1281 g_node_unlink (child);
1289 if (!child && create)
1293 mnode = g_slice_new0 (Node);
1294 mnode->type = node_type;
1295 mnode->name = g_strndup (childname, childname_length);
1297 child = g_node_new (mnode);
1302 g_node_insert_before (parent, sibling, child);
1304 g_node_insert_after (parent, sibling, child);
1309 g_node_prepend (parent, child);
1311 g_node_append (parent, child);
1314 mark_node_dirty (child);
1319 /* handle root node */
1320 if (manager->private_data->root_node)
1322 child = manager->private_data->root_node;
1323 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1324 g_warning ("root node name '%s' doesn't match '%s'",
1325 childname, NODE_INFO (child)->name);
1326 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1327 g_warning ("base element must be of type ROOT");
1333 mnode = g_slice_new0 (Node);
1334 mnode->type = node_type;
1335 mnode->name = g_strndup (childname, childname_length);
1336 mnode->dirty = TRUE;
1338 child = manager->private_data->root_node = g_node_new (mnode);
1346 get_node (GtkUIManager *manager,
1351 const gchar *pos, *end;
1352 GNode *parent, *node;
1354 if (strncmp ("/ui", path, 3) == 0)
1357 end = path + strlen (path);
1359 parent = node = NULL;
1365 slash = strchr (pos, '/');
1367 length = slash - pos;
1369 length = strlen (pos);
1371 node = get_child_node (manager, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1376 pos += length + 1; /* move past the node name and the slash too */
1380 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1381 NODE_INFO (node)->type = node_type;
1387 node_ui_reference_free (gpointer data)
1389 g_slice_free (NodeUIReference, data);
1393 free_node (GNode *node)
1395 Node *info = NODE_INFO (node);
1397 g_list_free_full (info->uifiles, node_ui_reference_free);
1400 g_object_unref (info->action);
1402 g_object_unref (info->proxy);
1404 g_object_unref (info->extra);
1405 g_free (info->name);
1406 g_slice_free (Node, info);
1412 * gtk_ui_manager_new_merge_id:
1413 * @manager: a #GtkUIManager
1415 * Returns an unused merge id, suitable for use with
1416 * gtk_ui_manager_add_ui().
1418 * Return value: an unused merge id.
1423 gtk_ui_manager_new_merge_id (GtkUIManager *manager)
1425 manager->private_data->last_merge_id++;
1427 return manager->private_data->last_merge_id;
1431 node_prepend_ui_reference (GNode *gnode,
1433 GQuark action_quark)
1435 Node *node = NODE_INFO (gnode);
1436 NodeUIReference *reference = NULL;
1438 if (node->uifiles &&
1439 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1440 reference = node->uifiles->data;
1443 reference = g_slice_new (NodeUIReference);
1444 node->uifiles = g_list_prepend (node->uifiles, reference);
1447 reference->merge_id = merge_id;
1448 reference->action_quark = action_quark;
1450 mark_node_dirty (gnode);
1454 node_remove_ui_reference (GNode *gnode,
1457 Node *node = NODE_INFO (gnode);
1460 for (p = node->uifiles; p != NULL; p = p->next)
1462 NodeUIReference *reference = p->data;
1464 if (reference->merge_id == merge_id)
1466 if (p == node->uifiles)
1467 mark_node_dirty (gnode);
1468 node->uifiles = g_list_delete_link (node->uifiles, p);
1469 g_slice_free (NodeUIReference, reference);
1476 /* -------------------- The UI file parser -------------------- */
1490 typedef struct _ParseContext ParseContext;
1491 struct _ParseContext
1494 ParseState prev_state;
1496 GtkUIManager *manager;
1504 start_element_handler (GMarkupParseContext *context,
1505 const gchar *element_name,
1506 const gchar **attribute_names,
1507 const gchar **attribute_values,
1511 ParseContext *ctx = user_data;
1512 GtkUIManager *manager = ctx->manager;
1515 const gchar *node_name;
1516 const gchar *action;
1517 GQuark action_quark;
1519 gboolean expand = FALSE;
1520 gboolean accelerators = FALSE;
1521 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1523 gboolean raise_error = TRUE;
1530 for (i = 0; attribute_names[i] != NULL; i++)
1532 if (!strcmp (attribute_names[i], "name"))
1534 node_name = attribute_values[i];
1536 else if (!strcmp (attribute_names[i], "action"))
1538 action = attribute_values[i];
1539 action_quark = g_quark_from_string (attribute_values[i]);
1541 else if (!strcmp (attribute_names[i], "position"))
1543 top = !strcmp (attribute_values[i], "top");
1545 else if (!strcmp (attribute_names[i], "expand"))
1547 expand = !strcmp (attribute_values[i], "true");
1549 else if (!strcmp (attribute_names[i], "accelerators"))
1551 accelerators = !strcmp (attribute_values[i], "true");
1553 else if (!strcmp (attribute_names[i], "always-show-image"))
1555 always_show_image_set = TRUE;
1556 always_show_image = !strcmp (attribute_values[i], "true");
1558 /* else silently skip unknown attributes to be compatible with
1559 * future additional attributes.
1563 /* Work out a name for this node. Either the name attribute, or
1564 * the action, or the element name */
1565 if (node_name == NULL)
1570 node_name = element_name;
1573 switch (element_name[0])
1576 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1578 ctx->state = STATE_ACCELERATOR;
1579 ctx->current = get_child_node (manager, ctx->current, NULL,
1580 node_name, strlen (node_name),
1581 NODE_TYPE_ACCELERATOR,
1583 if (NODE_INFO (ctx->current)->action_name == 0)
1584 NODE_INFO (ctx->current)->action_name = action_quark;
1586 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1588 raise_error = FALSE;
1592 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1594 ctx->state = STATE_ROOT;
1595 ctx->current = manager->private_data->root_node;
1596 raise_error = FALSE;
1598 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1602 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1604 ctx->state = STATE_MENU;
1605 ctx->current = get_child_node (manager, ctx->current, NULL,
1606 node_name, strlen (node_name),
1609 if (NODE_INFO (ctx->current)->action_name == 0)
1610 NODE_INFO (ctx->current)->action_name = action_quark;
1612 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1613 mark_node_dirty (ctx->current);
1615 raise_error = FALSE;
1617 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1619 ctx->current = get_child_node (manager, ctx->current, NULL,
1620 node_name, strlen (node_name),
1623 if (NODE_INFO (ctx->current)->action_name == 0)
1624 NODE_INFO (ctx->current)->action_name = action_quark;
1626 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1628 raise_error = FALSE;
1630 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1632 ctx->state = STATE_MENU;
1634 ctx->current = get_child_node (manager, g_node_last_child (ctx->current), NULL,
1635 node_name, strlen (node_name),
1638 if (NODE_INFO (ctx->current)->action_name == 0)
1639 NODE_INFO (ctx->current)->action_name = action_quark;
1641 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1643 raise_error = FALSE;
1645 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1649 ctx->state = STATE_MENUITEM;
1650 node = get_child_node (manager, ctx->current, NULL,
1651 node_name, strlen (node_name),
1654 if (NODE_INFO (node)->action_name == 0)
1655 NODE_INFO (node)->action_name = action_quark;
1657 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1658 NODE_INFO (node)->always_show_image = always_show_image;
1660 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1662 raise_error = FALSE;
1666 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1668 ctx->state = STATE_MENU;
1669 ctx->current = get_child_node (manager, ctx->current, NULL,
1670 node_name, strlen (node_name),
1674 NODE_INFO (ctx->current)->popup_accels = accelerators;
1676 if (NODE_INFO (ctx->current)->action_name == 0)
1677 NODE_INFO (ctx->current)->action_name = action_quark;
1679 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1681 raise_error = FALSE;
1683 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1684 !strcmp (element_name, "placeholder"))
1686 if (ctx->state == STATE_TOOLBAR)
1687 ctx->current = get_child_node (manager, ctx->current, NULL,
1688 node_name, strlen (node_name),
1689 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1692 ctx->current = get_child_node (manager, ctx->current, NULL,
1693 node_name, strlen (node_name),
1694 NODE_TYPE_MENU_PLACEHOLDER,
1697 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1699 raise_error = FALSE;
1703 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1704 !strcmp (element_name, "separator"))
1709 if (ctx->state == STATE_TOOLBAR)
1710 ctx->state = STATE_TOOLITEM;
1712 ctx->state = STATE_MENUITEM;
1713 if (!strcmp (node_name, "separator"))
1719 length = strlen (node_name);
1720 node = get_child_node (manager, ctx->current, NULL,
1722 NODE_TYPE_SEPARATOR,
1725 NODE_INFO (node)->expand = expand;
1727 if (NODE_INFO (node)->action_name == 0)
1728 NODE_INFO (node)->action_name = action_quark;
1730 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1732 raise_error = FALSE;
1736 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1738 ctx->state = STATE_TOOLBAR;
1739 ctx->current = get_child_node (manager, ctx->current, NULL,
1740 node_name, strlen (node_name),
1743 if (NODE_INFO (ctx->current)->action_name == 0)
1744 NODE_INFO (ctx->current)->action_name = action_quark;
1746 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1748 raise_error = FALSE;
1750 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1754 ctx->state = STATE_TOOLITEM;
1755 node = get_child_node (manager, ctx->current, NULL,
1756 node_name, strlen (node_name),
1759 if (NODE_INFO (node)->action_name == 0)
1760 NODE_INFO (node)->action_name = action_quark;
1762 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1764 raise_error = FALSE;
1772 gint line_number, char_number;
1774 g_markup_parse_context_get_position (context,
1775 &line_number, &char_number);
1778 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1779 _("Unexpected start tag '%s' on line %d char %d"),
1781 line_number, char_number);
1786 end_element_handler (GMarkupParseContext *context,
1787 const gchar *element_name,
1791 ParseContext *ctx = user_data;
1797 /* no need to GError here, GMarkup already catches this */
1800 ctx->current = NULL;
1801 ctx->state = STATE_END;
1805 case STATE_ACCELERATOR:
1806 ctx->current = ctx->current->parent;
1807 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1808 ctx->state = STATE_ROOT;
1809 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1811 ctx->current = ctx->current->parent;
1812 ctx->state = STATE_TOOLITEM;
1814 /* else, stay in same state */
1816 case STATE_MENUITEM:
1817 ctx->state = STATE_MENU;
1819 case STATE_TOOLITEM:
1820 ctx->state = STATE_TOOLBAR;
1826 cleanup (GMarkupParseContext *context,
1830 ParseContext *ctx = user_data;
1832 ctx->current = NULL;
1833 /* should also walk through the tree and get rid of nodes related to
1834 * this UI file's tag */
1836 gtk_ui_manager_remove_ui (ctx->manager, ctx->merge_id);
1840 xml_isspace (char c)
1842 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1846 text_handler (GMarkupParseContext *context,
1856 end = text + text_len;
1857 while (p != end && xml_isspace (*p))
1862 gint line_number, char_number;
1864 g_markup_parse_context_get_position (context,
1865 &line_number, &char_number);
1868 G_MARKUP_ERROR_INVALID_CONTENT,
1869 _("Unexpected character data on line %d char %d"),
1870 line_number, char_number);
1875 static const GMarkupParser ui_parser = {
1876 start_element_handler,
1877 end_element_handler,
1884 add_ui_from_string (GtkUIManager *manager,
1885 const gchar *buffer,
1887 gboolean needs_root,
1890 ParseContext ctx = { 0 };
1891 GMarkupParseContext *context;
1893 ctx.state = STATE_START;
1894 ctx.manager = manager;
1896 ctx.merge_id = gtk_ui_manager_new_merge_id (manager);
1898 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1901 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1904 if (!g_markup_parse_context_parse (context, buffer, length, error))
1908 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1911 if (!g_markup_parse_context_end_parse (context, error))
1914 g_markup_parse_context_free (context);
1916 queue_update (manager);
1918 g_object_notify (G_OBJECT (manager), "ui");
1920 return ctx.merge_id;
1924 g_markup_parse_context_free (context);
1930 * gtk_ui_manager_add_ui_from_string:
1931 * @manager: a #GtkUIManager object
1932 * @buffer: the string to parse
1933 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1934 * @error: return location for an error
1936 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1937 * merges it with the current contents of @manager. An enclosing <ui>
1938 * element is added if it is missing.
1940 * Return value: The merge id for the merged UI. The merge id can be used
1941 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1942 * the return value is 0.
1947 gtk_ui_manager_add_ui_from_string (GtkUIManager *manager,
1948 const gchar *buffer,
1952 gboolean needs_root = TRUE;
1956 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1957 g_return_val_if_fail (buffer != NULL, 0);
1960 length = strlen (buffer);
1963 end = buffer + length;
1964 while (p != end && xml_isspace (*p))
1967 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1970 return add_ui_from_string (manager, buffer, length, needs_root, error);
1974 * gtk_ui_manager_add_ui_from_file:
1975 * @manager: a #GtkUIManager object
1976 * @filename: (type filename): the name of the file to parse
1977 * @error: return location for an error
1979 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1980 * merges it with the current contents of @manager.
1982 * Return value: The merge id for the merged UI. The merge id can be used
1983 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1984 * the return value is 0.
1989 gtk_ui_manager_add_ui_from_file (GtkUIManager *manager,
1990 const gchar *filename,
1997 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1999 if (!g_file_get_contents (filename, &buffer, &length, error))
2002 res = add_ui_from_string (manager, buffer, length, FALSE, error);
2009 * gtk_ui_manager_add_ui_from_resource:
2010 * @manager: a #GtkUIManager object
2011 * @resource_path: the resource path of the file to parse
2012 * @error: return location for an error
2014 * Parses a resource file containing a <link linkend="XML-UI">UI definition</link> and
2015 * merges it with the current contents of @manager.
2017 * Return value: The merge id for the merged UI. The merge id can be used
2018 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
2019 * the return value is 0.
2024 gtk_ui_manager_add_ui_from_resource (GtkUIManager *manager,
2025 const gchar *resource_path,
2031 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2033 data = g_resources_lookup_data (resource_path, 0, error);
2037 res = add_ui_from_string (manager, g_bytes_get_data (data, NULL), g_bytes_get_size (data), FALSE, error);
2038 g_bytes_unref (data);
2044 * gtk_ui_manager_add_ui:
2045 * @manager: a #GtkUIManager
2046 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
2048 * @name: the name for the added UI element
2049 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
2050 * @type: the type of UI element to add.
2051 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
2052 * is added after its siblings.
2054 * Adds a UI element to the current contents of @manager.
2056 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
2057 * separator if such an element can be inserted at the place determined by
2058 * @path. Otherwise @type must indicate an element that can be inserted at
2059 * the place determined by @path.
2061 * If @path points to a menuitem or toolitem, the new element will be inserted
2062 * before or after this item, depending on @top.
2067 gtk_ui_manager_add_ui (GtkUIManager *manager,
2071 const gchar *action,
2072 GtkUIManagerItemType type,
2079 GQuark action_quark = 0;
2081 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2082 g_return_if_fail (merge_id > 0);
2083 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
2085 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
2091 node_type = NODE_TYPE_UNDECIDED;
2094 switch (NODE_INFO (node)->type)
2096 case NODE_TYPE_SEPARATOR:
2097 case NODE_TYPE_MENUITEM:
2098 case NODE_TYPE_TOOLITEM:
2100 node = node->parent;
2102 case NODE_TYPE_MENUBAR:
2103 case NODE_TYPE_MENU:
2104 case NODE_TYPE_POPUP:
2105 case NODE_TYPE_MENU_PLACEHOLDER:
2108 case GTK_UI_MANAGER_AUTO:
2110 node_type = NODE_TYPE_MENUITEM;
2112 node_type = NODE_TYPE_SEPARATOR;
2114 case GTK_UI_MANAGER_MENU:
2115 node_type = NODE_TYPE_MENU;
2117 case GTK_UI_MANAGER_MENUITEM:
2118 node_type = NODE_TYPE_MENUITEM;
2120 case GTK_UI_MANAGER_SEPARATOR:
2121 node_type = NODE_TYPE_SEPARATOR;
2123 case GTK_UI_MANAGER_PLACEHOLDER:
2124 node_type = NODE_TYPE_MENU_PLACEHOLDER;
2130 case NODE_TYPE_TOOLBAR:
2131 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2134 case GTK_UI_MANAGER_AUTO:
2136 node_type = NODE_TYPE_TOOLITEM;
2138 node_type = NODE_TYPE_SEPARATOR;
2140 case GTK_UI_MANAGER_TOOLITEM:
2141 node_type = NODE_TYPE_TOOLITEM;
2143 case GTK_UI_MANAGER_SEPARATOR:
2144 node_type = NODE_TYPE_SEPARATOR;
2146 case GTK_UI_MANAGER_PLACEHOLDER:
2147 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
2153 case NODE_TYPE_ROOT:
2156 case GTK_UI_MANAGER_MENUBAR:
2157 node_type = NODE_TYPE_MENUBAR;
2159 case GTK_UI_MANAGER_TOOLBAR:
2160 node_type = NODE_TYPE_TOOLBAR;
2162 case GTK_UI_MANAGER_POPUP:
2163 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
2164 node_type = NODE_TYPE_POPUP;
2166 case GTK_UI_MANAGER_ACCELERATOR:
2167 node_type = NODE_TYPE_ACCELERATOR;
2177 if (node_type == NODE_TYPE_UNDECIDED)
2179 g_warning ("item type %d not suitable for adding at '%s'",
2184 child = get_child_node (manager, node, sibling,
2185 name, name ? strlen (name) : 0,
2186 node_type, TRUE, top);
2188 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
2189 NODE_INFO (child)->popup_accels = TRUE;
2192 action_quark = g_quark_from_string (action);
2194 node_prepend_ui_reference (child, merge_id, action_quark);
2196 if (NODE_INFO (child)->action_name == 0)
2197 NODE_INFO (child)->action_name = action_quark;
2199 queue_update (manager);
2201 g_object_notify (G_OBJECT (manager), "ui");
2205 remove_ui (GNode *node,
2208 guint merge_id = GPOINTER_TO_UINT (user_data);
2210 node_remove_ui_reference (node, merge_id);
2212 return FALSE; /* continue */
2216 * gtk_ui_manager_remove_ui:
2217 * @manager: a #GtkUIManager object
2218 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
2220 * Unmerges the part of @manager<!-- -->s content identified by @merge_id.
2225 gtk_ui_manager_remove_ui (GtkUIManager *manager,
2228 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2230 g_node_traverse (manager->private_data->root_node,
2231 G_POST_ORDER, G_TRAVERSE_ALL, -1,
2232 remove_ui, GUINT_TO_POINTER (merge_id));
2234 queue_update (manager);
2236 g_object_notify (G_OBJECT (manager), "ui");
2239 /* -------------------- Updates -------------------- */
2243 get_action_by_name (GtkUIManager *merge,
2244 const gchar *action_name)
2252 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
2254 GtkActionGroup *action_group = tmp->data;
2257 action = gtk_action_group_get_action (action_group, action_name);
2267 find_menu_position (GNode *node,
2268 GtkWidget **menushell_p,
2271 GtkWidget *menushell;
2274 g_return_val_if_fail (node != NULL, FALSE);
2275 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
2276 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
2277 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
2278 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
2279 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2282 /* first sibling -- look at parent */
2283 if (node->prev == NULL)
2288 parent = node->parent;
2289 switch (NODE_INFO (parent)->type)
2291 case NODE_TYPE_MENUBAR:
2292 case NODE_TYPE_POPUP:
2293 menushell = NODE_INFO (parent)->proxy;
2296 case NODE_TYPE_MENU:
2297 menushell = NODE_INFO (parent)->proxy;
2298 if (GTK_IS_MENU_ITEM (menushell))
2299 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
2300 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
2301 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2305 g_list_free (siblings);
2307 case NODE_TYPE_MENU_PLACEHOLDER:
2308 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2309 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2310 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children,
2311 NODE_INFO (parent)->proxy) + 1;
2314 g_warning ("%s: bad parent node type %d", G_STRLOC,
2315 NODE_INFO (parent)->type);
2321 GtkWidget *prev_child;
2324 sibling = node->prev;
2325 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2326 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2328 prev_child = NODE_INFO (sibling)->proxy;
2330 if (!GTK_IS_WIDGET (prev_child))
2333 menushell = gtk_widget_get_parent (prev_child);
2334 if (!GTK_IS_MENU_SHELL (menushell))
2337 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children, prev_child) + 1;
2341 *menushell_p = menushell;
2349 find_toolbar_position (GNode *node,
2350 GtkWidget **toolbar_p,
2356 g_return_val_if_fail (node != NULL, FALSE);
2357 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2358 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2359 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2360 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2363 /* first sibling -- look at parent */
2364 if (node->prev == NULL)
2368 parent = node->parent;
2369 switch (NODE_INFO (parent)->type)
2371 case NODE_TYPE_TOOLBAR:
2372 toolbar = NODE_INFO (parent)->proxy;
2375 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2376 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2377 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2378 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2379 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2382 g_warning ("%s: bad parent node type %d", G_STRLOC,
2383 NODE_INFO (parent)->type);
2389 GtkWidget *prev_child;
2392 sibling = node->prev;
2393 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2394 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2396 prev_child = NODE_INFO (sibling)->proxy;
2398 if (!GTK_IS_WIDGET (prev_child))
2401 toolbar = gtk_widget_get_parent (prev_child);
2402 if (!GTK_IS_TOOLBAR (toolbar))
2405 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2406 GTK_TOOL_ITEM (prev_child)) + 1;
2410 *toolbar_p = toolbar;
2418 * _gtk_menu_is_empty:
2419 * @menu: (allow-none): a #GtkMenu or %NULL
2421 * Determines whether @menu is empty. A menu is considered empty if it
2422 * the only visible children are tearoff menu items or "filler" menu
2423 * items which were inserted to mark the menu as empty.
2425 * This function is used by #GtkAction.
2427 * Return value: whether @menu is empty.
2430 _gtk_menu_is_empty (GtkWidget *menu)
2432 GList *children, *cur;
2433 gboolean result = TRUE;
2435 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2440 children = gtk_container_get_children (GTK_CONTAINER (menu));
2445 if (gtk_widget_get_visible (cur->data))
2447 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2448 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2456 g_list_free (children);
2462 SEPARATOR_MODE_SMART,
2463 SEPARATOR_MODE_VISIBLE,
2464 SEPARATOR_MODE_HIDDEN
2468 update_smart_separators (GtkWidget *proxy)
2470 GtkWidget *parent = NULL;
2472 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2474 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2475 parent = gtk_widget_get_parent (proxy);
2481 GList *children, *cur, *last;
2484 children = gtk_container_get_children (GTK_CONTAINER (parent));
2494 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2498 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2499 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2502 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2503 "gtk-separator-mode"));
2506 case SEPARATOR_MODE_VISIBLE:
2507 gtk_widget_show (GTK_WIDGET (cur->data));
2511 case SEPARATOR_MODE_HIDDEN:
2512 gtk_widget_hide (GTK_WIDGET (cur->data));
2514 case SEPARATOR_MODE_SMART:
2517 gtk_widget_show (GTK_WIDGET (cur->data));
2522 gtk_widget_hide (GTK_WIDGET (cur->data));
2526 else if (gtk_widget_get_visible (cur->data))
2529 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2542 gtk_widget_hide (GTK_WIDGET (last->data));
2544 if (GTK_IS_MENU (parent))
2548 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2549 if (GTK_IS_MENU_ITEM (item))
2550 _gtk_action_sync_menu_visible (NULL, item, empty);
2551 if (GTK_IS_WIDGET (filler))
2554 gtk_widget_show (filler);
2556 gtk_widget_hide (filler);
2560 g_list_free (children);
2565 update_node (GtkUIManager *manager,
2568 gboolean popup_accels)
2573 const gchar *action_name;
2574 NodeUIReference *ref;
2576 #ifdef DEBUG_UI_MANAGER
2580 g_return_if_fail (node != NULL);
2581 g_return_if_fail (NODE_INFO (node) != NULL);
2583 info = NODE_INFO (node);
2588 if (info->type == NODE_TYPE_POPUP)
2591 popup_accels = info->popup_accels;
2594 #ifdef DEBUG_UI_MANAGER
2595 g_print ("update_node name=%s dirty=%d popup %d (",
2596 info->name, info->dirty, in_popup);
2597 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2599 NodeUIReference *ref = tmp->data;
2600 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2607 if (info->uifiles == NULL) {
2608 /* We may need to remove this node.
2609 * This must be done in post order
2611 goto recurse_children;
2614 ref = info->uifiles->data;
2615 action_name = g_quark_to_string (ref->action_quark);
2616 action = get_action_by_name (manager, action_name);
2618 info->dirty = FALSE;
2620 /* Check if the node doesn't have an action and must have an action */
2621 if (action == NULL &&
2622 info->type != NODE_TYPE_ROOT &&
2623 info->type != NODE_TYPE_MENUBAR &&
2624 info->type != NODE_TYPE_TOOLBAR &&
2625 info->type != NODE_TYPE_POPUP &&
2626 info->type != NODE_TYPE_SEPARATOR &&
2627 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2628 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2630 g_warning ("%s: missing action %s", info->name, action_name);
2636 gtk_action_set_accel_group (action, manager->private_data->accel_group);
2638 /* If the widget already has a proxy and the action hasn't changed, then
2639 * we only have to update the tearoff menu items.
2641 if (info->proxy != NULL && action == info->action)
2643 if (info->type == NODE_TYPE_MENU)
2648 if (GTK_IS_MENU (info->proxy))
2651 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2652 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2653 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2655 if (manager->private_data->add_tearoffs && !in_popup)
2656 gtk_widget_show (GTK_WIDGET (siblings->data));
2658 gtk_widget_hide (GTK_WIDGET (siblings->data));
2660 g_list_free (siblings);
2663 goto recurse_children;
2668 case NODE_TYPE_MENUBAR:
2669 if (info->proxy == NULL)
2671 info->proxy = gtk_menu_bar_new ();
2672 g_object_ref_sink (info->proxy);
2673 gtk_widget_set_name (info->proxy, info->name);
2674 gtk_widget_show (info->proxy);
2675 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2678 case NODE_TYPE_POPUP:
2679 if (info->proxy == NULL)
2681 info->proxy = gtk_menu_new ();
2682 g_object_ref_sink (info->proxy);
2684 gtk_widget_set_name (info->proxy, info->name);
2686 case NODE_TYPE_MENU:
2688 GtkWidget *prev_submenu = NULL;
2689 GtkWidget *menu = NULL;
2692 /* remove the proxy if it is of the wrong type ... */
2694 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2696 if (GTK_IS_MENU_ITEM (info->proxy))
2698 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2701 g_object_ref (prev_submenu);
2702 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2706 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2707 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2709 g_object_unref (info->proxy);
2713 /* create proxy if needed ... */
2714 if (info->proxy == NULL)
2716 /* ... if the action already provides a menu, then use
2717 * that menu instead of creating an empty one
2719 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2720 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2721 GTK_ACTION_GET_CLASS (action)->create_menu)
2723 menu = gtk_action_create_menu (action);
2731 menu = gtk_menu_new ();
2732 gtk_widget_set_name (menu, info->name);
2733 tearoff = gtk_tearoff_menu_item_new ();
2734 gtk_widget_set_no_show_all (tearoff, TRUE);
2735 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2736 filler = gtk_menu_item_new_with_label (_("Empty"));
2737 g_object_set_data (G_OBJECT (filler),
2738 I_("gtk-empty-menu-item"),
2739 GINT_TO_POINTER (TRUE));
2740 gtk_widget_set_sensitive (filler, FALSE);
2741 gtk_widget_set_no_show_all (filler, TRUE);
2742 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2745 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2748 g_object_ref_sink (info->proxy);
2749 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2754 GtkWidget *menushell;
2757 if (find_menu_position (node, &menushell, &pos))
2759 info->proxy = gtk_action_create_menu_item (action);
2760 g_object_ref_sink (info->proxy);
2761 g_signal_connect (info->proxy, "notify::visible",
2762 G_CALLBACK (update_smart_separators), NULL);
2763 gtk_widget_set_name (info->proxy, info->name);
2765 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2766 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2771 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2775 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2777 g_object_unref (prev_submenu);
2780 if (GTK_IS_MENU (info->proxy))
2783 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2785 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2786 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2788 if (manager->private_data->add_tearoffs && !in_popup)
2789 gtk_widget_show (GTK_WIDGET (siblings->data));
2791 gtk_widget_hide (GTK_WIDGET (siblings->data));
2793 g_list_free (siblings);
2796 case NODE_TYPE_UNDECIDED:
2797 g_warning ("found undecided node!");
2799 case NODE_TYPE_ROOT:
2801 case NODE_TYPE_TOOLBAR:
2802 if (info->proxy == NULL)
2804 info->proxy = gtk_toolbar_new ();
2805 g_object_ref_sink (info->proxy);
2806 gtk_widget_set_name (info->proxy, info->name);
2807 gtk_widget_show (info->proxy);
2808 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2811 case NODE_TYPE_MENU_PLACEHOLDER:
2812 /* create menu items for placeholders if necessary ... */
2813 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2814 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2818 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2820 g_object_unref (info->proxy);
2825 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2827 g_object_unref (info->extra);
2831 if (info->proxy == NULL)
2833 GtkWidget *menushell;
2836 if (find_menu_position (node, &menushell, &pos))
2838 info->proxy = gtk_separator_menu_item_new ();
2839 g_object_ref_sink (info->proxy);
2840 g_object_set_data (G_OBJECT (info->proxy),
2841 I_("gtk-separator-mode"),
2842 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2843 gtk_widget_set_no_show_all (info->proxy, TRUE);
2844 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2845 NODE_INFO (node)->proxy, pos);
2847 info->extra = gtk_separator_menu_item_new ();
2848 g_object_ref_sink (info->extra);
2849 g_object_set_data (G_OBJECT (info->extra),
2850 I_("gtk-separator-mode"),
2851 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2852 gtk_widget_set_no_show_all (info->extra, TRUE);
2853 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2854 NODE_INFO (node)->extra, pos + 1);
2858 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2859 /* create toolbar items for placeholders if necessary ... */
2860 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2861 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2865 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2867 g_object_unref (info->proxy);
2872 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2874 g_object_unref (info->extra);
2878 if (info->proxy == NULL)
2884 if (find_toolbar_position (node, &toolbar, &pos))
2886 item = gtk_separator_tool_item_new ();
2887 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2888 info->proxy = GTK_WIDGET (item);
2889 g_object_ref_sink (info->proxy);
2890 g_object_set_data (G_OBJECT (info->proxy),
2891 I_("gtk-separator-mode"),
2892 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2893 gtk_widget_set_no_show_all (info->proxy, TRUE);
2895 item = gtk_separator_tool_item_new ();
2896 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2897 info->extra = GTK_WIDGET (item);
2898 g_object_ref_sink (info->extra);
2899 g_object_set_data (G_OBJECT (info->extra),
2900 I_("gtk-separator-mode"),
2901 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2902 gtk_widget_set_no_show_all (info->extra, TRUE);
2906 case NODE_TYPE_MENUITEM:
2907 /* remove the proxy if it is of the wrong type ... */
2909 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2911 g_signal_handlers_disconnect_by_func (info->proxy,
2912 G_CALLBACK (update_smart_separators),
2914 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2915 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2917 g_object_unref (info->proxy);
2920 /* create proxy if needed ... */
2921 if (info->proxy == NULL)
2923 GtkWidget *menushell;
2926 if (find_menu_position (node, &menushell, &pos))
2928 info->proxy = gtk_action_create_menu_item (action);
2929 g_object_ref_sink (info->proxy);
2930 gtk_widget_set_name (info->proxy, info->name);
2932 if (info->always_show_image_set &&
2933 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2934 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2935 info->always_show_image);
2937 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2943 g_signal_handlers_disconnect_by_func (info->proxy,
2944 G_CALLBACK (update_smart_separators),
2946 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2947 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2952 g_signal_connect (info->proxy, "notify::visible",
2953 G_CALLBACK (update_smart_separators), NULL);
2954 if (in_popup && !popup_accels)
2956 /* don't show accels in popups */
2957 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2958 if (GTK_IS_ACCEL_LABEL (child))
2959 g_object_set (child, "accel-closure", NULL, NULL);
2964 case NODE_TYPE_TOOLITEM:
2965 /* remove the proxy if it is of the wrong type ... */
2967 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2969 g_signal_handlers_disconnect_by_func (info->proxy,
2970 G_CALLBACK (update_smart_separators),
2972 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2973 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2975 g_object_unref (info->proxy);
2978 /* create proxy if needed ... */
2979 if (info->proxy == NULL)
2984 if (find_toolbar_position (node, &toolbar, &pos))
2986 info->proxy = gtk_action_create_tool_item (action);
2987 g_object_ref_sink (info->proxy);
2988 gtk_widget_set_name (info->proxy, info->name);
2990 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2991 GTK_TOOL_ITEM (info->proxy), pos);
2996 g_signal_handlers_disconnect_by_func (info->proxy,
2997 G_CALLBACK (update_smart_separators),
2999 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
3004 g_signal_connect (info->proxy, "notify::visible",
3005 G_CALLBACK (update_smart_separators), NULL);
3008 case NODE_TYPE_SEPARATOR:
3009 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
3010 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
3014 gint separator_mode;
3017 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
3019 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3021 g_object_unref (info->proxy);
3025 if (find_toolbar_position (node, &toolbar, &pos))
3027 item = gtk_separator_tool_item_new ();
3028 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
3029 info->proxy = GTK_WIDGET (item);
3030 g_object_ref_sink (info->proxy);
3031 gtk_widget_set_no_show_all (info->proxy, TRUE);
3034 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
3035 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
3036 separator_mode = SEPARATOR_MODE_VISIBLE;
3039 separator_mode = SEPARATOR_MODE_SMART;
3041 g_object_set_data (G_OBJECT (info->proxy),
3042 I_("gtk-separator-mode"),
3043 GINT_TO_POINTER (separator_mode));
3044 gtk_widget_show (info->proxy);
3049 GtkWidget *menushell;
3052 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
3054 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3056 g_object_unref (info->proxy);
3060 if (find_menu_position (node, &menushell, &pos))
3062 info->proxy = gtk_separator_menu_item_new ();
3063 g_object_ref_sink (info->proxy);
3064 gtk_widget_set_no_show_all (info->proxy, TRUE);
3065 g_object_set_data (G_OBJECT (info->proxy),
3066 I_("gtk-separator-mode"),
3067 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
3068 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
3070 gtk_widget_show (info->proxy);
3074 case NODE_TYPE_ACCELERATOR:
3075 gtk_action_connect_accelerator (action);
3080 g_object_ref (action);
3082 g_object_unref (info->action);
3083 info->action = action;
3086 /* process children */
3087 child = node->children;
3093 child = current->next;
3094 update_node (manager, current, in_popup, popup_accels);
3099 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
3100 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
3101 else if (info->type == NODE_TYPE_MENU ||
3102 info->type == NODE_TYPE_TOOLBAR ||
3103 info->type == NODE_TYPE_POPUP)
3104 update_smart_separators (info->proxy);
3107 /* handle cleanup of dead nodes */
3108 if (node->children == NULL && info->uifiles == NULL)
3111 gtk_widget_destroy (info->proxy);
3113 gtk_widget_destroy (info->extra);
3114 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
3115 gtk_action_disconnect_accelerator (info->action);
3117 g_node_destroy (node);
3122 do_updates (GtkUIManager *manager)
3124 /* this function needs to check through the tree for dirty nodes.
3125 * For such nodes, it needs to do the following:
3127 * 1) check if they are referenced by any loaded UI files anymore.
3128 * In which case, the proxy widget should be destroyed, unless
3129 * there are any subnodes.
3131 * 2) lookup the action for this node again. If it is different to
3132 * the current one (or if no previous action has been looked up),
3133 * the proxy is reconnected to the new action (or a new proxy widget
3134 * is created and added to the parent container).
3136 update_node (manager, manager->private_data->root_node, FALSE, FALSE);
3138 manager->private_data->update_tag = 0;
3144 do_updates_idle (GtkUIManager *manager)
3146 do_updates (manager);
3152 queue_update (GtkUIManager *manager)
3154 if (manager->private_data->update_tag != 0)
3157 manager->private_data->update_tag = gdk_threads_add_idle (
3158 (GSourceFunc)do_updates_idle,
3164 * gtk_ui_manager_ensure_update:
3165 * @manager: a #GtkUIManager
3167 * Makes sure that all pending updates to the UI have been completed.
3169 * This may occasionally be necessary, since #GtkUIManager updates the
3170 * UI in an idle function. A typical example where this function is
3171 * useful is to enforce that the menubar and toolbar have been added to
3172 * the main window before showing it:
3174 * gtk_container_add (GTK_CONTAINER (window), vbox);
3175 * g_signal_connect (merge, "add-widget",
3176 * G_CALLBACK (add_widget), vbox);
3177 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
3178 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
3179 * gtk_ui_manager_ensure_update (merge);
3180 * gtk_widget_show (window);
3186 gtk_ui_manager_ensure_update (GtkUIManager *manager)
3188 if (manager->private_data->update_tag != 0)
3190 g_source_remove (manager->private_data->update_tag);
3191 do_updates (manager);
3196 dirty_traverse_func (GNode *node,
3199 NODE_INFO (node)->dirty = TRUE;
3204 dirty_all_nodes (GtkUIManager *manager)
3206 g_node_traverse (manager->private_data->root_node,
3207 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3208 dirty_traverse_func, NULL);
3209 queue_update (manager);
3213 mark_node_dirty (GNode *node)
3217 /* FIXME could optimize this */
3218 for (p = node; p; p = p->parent)
3219 NODE_INFO (p)->dirty = TRUE;
3222 static const gchar *
3223 open_tag_format (NodeType type)
3227 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
3228 case NODE_TYPE_ROOT: return "%*s<ui";
3229 case NODE_TYPE_MENUBAR: return "%*s<menubar";
3230 case NODE_TYPE_MENU: return "%*s<menu";
3231 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
3232 case NODE_TYPE_MENU_PLACEHOLDER:
3233 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
3234 case NODE_TYPE_POPUP: return "%*s<popup";
3235 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
3236 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
3237 case NODE_TYPE_SEPARATOR: return "%*s<separator";
3238 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
3239 default: return NULL;
3243 static const gchar *
3244 close_tag_format (NodeType type)
3248 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
3249 case NODE_TYPE_ROOT: return "%*s</ui>\n";
3250 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
3251 case NODE_TYPE_MENU: return "%*s</menu>\n";
3252 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
3253 case NODE_TYPE_MENU_PLACEHOLDER:
3254 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
3255 case NODE_TYPE_POPUP: return "%*s</popup>\n";
3256 default: return NULL;
3261 print_node (GtkUIManager *manager,
3268 const gchar *open_fmt;
3269 const gchar *close_fmt;
3273 open_fmt = open_tag_format (mnode->type);
3274 close_fmt = close_tag_format (mnode->type);
3276 g_string_append_printf (buffer, open_fmt, indent_level, "");
3278 if (mnode->type != NODE_TYPE_ROOT)
3281 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
3283 if (mnode->action_name)
3284 g_string_append_printf (buffer, " action=\"%s\"",
3285 g_quark_to_string (mnode->action_name));
3288 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
3290 for (child = node->children; child != NULL; child = child->next)
3291 print_node (manager, child, indent_level + 2, buffer);
3294 g_string_append_printf (buffer, close_fmt, indent_level, "");
3298 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
3299 GtkBuilder *builder,
3301 const gchar *tagname,
3302 GMarkupParser *parser,
3308 if (strcmp (tagname, "ui") == 0)
3312 ctx = g_new0 (ParseContext, 1);
3313 ctx->state = STATE_START;
3314 ctx->manager = GTK_UI_MANAGER (buildable);
3315 ctx->current = NULL;
3316 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3319 *parser = ui_parser;
3329 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3330 GtkBuilder *builder,
3332 const gchar *tagname,
3335 queue_update (GTK_UI_MANAGER (buildable));
3336 g_object_notify (G_OBJECT (buildable), "ui");
3341 * gtk_ui_manager_get_ui:
3342 * @manager: a #GtkUIManager
3344 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3346 * Return value: A newly allocated string containing an XML representation of
3352 gtk_ui_manager_get_ui (GtkUIManager *manager)
3356 buffer = g_string_new (NULL);
3358 gtk_ui_manager_ensure_update (manager);
3360 print_node (manager, manager->private_data->root_node, 0, buffer);
3362 return g_string_free (buffer, FALSE);