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"
49 #undef GDK_DEPRECATED_FOR
50 #define GDK_DEPRECATED
51 #define GDK_DEPRECATED_FOR(f)
53 #include "gtkuimanager.h"
54 #include "deprecated/gtktearoffmenuitem.h"
57 * SECTION:gtkuimanager
58 * @Short_description: Constructing menus and toolbars from an XML description
59 * @Title: GtkUIManager
60 * @See_also:#GtkBuilder
62 * A #GtkUIManager constructs a user interface (menus and toolbars) from
63 * one or more UI definitions, which reference actions from one or more
66 * <refsect2 id="XML-UI">
67 * <title>UI Definitions</title>
69 * The UI definitions are specified in an XML format which can be
70 * roughly described by the following DTD.
73 * Do not confuse the GtkUIManager UI Definitions described here with
74 * the similarly named <link linkend="BUILDER-UI">GtkBuilder UI
80 * <!ELEMENT ui (menubar|toolbar|popup|accelerator)* >
81 * <!ELEMENT menubar (menuitem|separator|placeholder|menu)* >
82 * <!ELEMENT menu (menuitem|separator|placeholder|menu)* >
83 * <!ELEMENT popup (menuitem|separator|placeholder|menu)* >
84 * <!ELEMENT toolbar (toolitem|separator|placeholder)* >
85 * <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* >
86 * <!ELEMENT menuitem EMPTY >
87 * <!ELEMENT toolitem (menu?) >
88 * <!ELEMENT separator EMPTY >
89 * <!ELEMENT accelerator EMPTY >
90 * <!ATTLIST menubar name #IMPLIED
92 * <!ATTLIST toolbar name #IMPLIED
94 * <!ATTLIST popup name #IMPLIED
96 * accelerators (true|false) #IMPLIED >
97 * <!ATTLIST placeholder name #IMPLIED
99 * <!ATTLIST separator name #IMPLIED
101 * expand (true|false) #IMPLIED >
102 * <!ATTLIST menu name #IMPLIED
104 * position (top|bot) #IMPLIED >
105 * <!ATTLIST menuitem name #IMPLIED
107 * position (top|bot) #IMPLIED
108 * always-show-image (true|false) #IMPLIED >
109 * <!ATTLIST toolitem name #IMPLIED
111 * position (top|bot) #IMPLIED >
112 * <!ATTLIST accelerator name #IMPLIED
116 * There are some additional restrictions beyond those specified in the
117 * DTD, e.g. every toolitem must have a toolbar in its anchestry and
118 * every menuitem must have a menubar or popup in its anchestry. Since
119 * a #GMarkup parser is used to parse the UI description, it must not only
120 * be valid XML, but valid #GMarkup.
122 * If a name is not specified, it defaults to the action. If an action is
123 * not specified either, the element name is used. The name and action
124 * attributes must not contain '/' characters after parsing (since that
125 * would mess up path lookup) and must be usable as XML attributes when
126 * enclosed in doublequotes, thus they must not '"' characters or references
127 * to the " entity.
130 * <title>A UI definition</title>
131 * <programlisting><![CDATA[
134 * <menu name="FileMenu" action="FileMenuAction">
135 * <menuitem name="New" action="New2Action" />
136 * <placeholder name="FileMenuAdditions" />
138 * <menu name="JustifyMenu" action="JustifyMenuAction">
139 * <menuitem name="Left" action="justify-left"/>
140 * <menuitem name="Centre" action="justify-center"/>
141 * <menuitem name="Right" action="justify-right"/>
142 * <menuitem name="Fill" action="justify-fill"/>
145 * <toolbar action="toolbar1">
146 * <placeholder name="JustifyToolItems">
148 * <toolitem name="Left" action="justify-left"/>
149 * <toolitem name="Centre" action="justify-center"/>
150 * <toolitem name="Right" action="justify-right"/>
151 * <toolitem name="Fill" action="justify-fill"/>
156 * ]]></programlisting>
159 * The constructed widget hierarchy is very similar to the element tree
160 * of the XML, with the exception that placeholders are merged into their
161 * parents. The correspondence of XML elements to widgets should be
165 * <term>menubar</term>
166 * <listitem><para>a #GtkMenuBar</para></listitem>
169 * <term>toolbar</term>
170 * <listitem><para>a #GtkToolbar</para></listitem>
174 * <listitem><para>a toplevel #GtkMenu</para></listitem>
178 * <listitem><para>a #GtkMenu attached to a menuitem</para></listitem>
181 * <term>menuitem</term>
182 * <listitem><para>a #GtkMenuItem subclass, the exact type depends on the
183 * action</para></listitem>
186 * <term>toolitem</term>
187 * <listitem><para>a #GtkToolItem subclass, the exact type depends on the
188 * action. Note that toolitem elements may contain a menu element, but only
189 * if their associated action specifies a #GtkMenuToolButton as proxy.</para></listitem>
192 * <term>separator</term>
193 * <listitem><para>a #GtkSeparatorMenuItem or
194 * #GtkSeparatorToolItem</para></listitem>
197 * <term>accelerator</term>
198 * <listitem><para>a keyboard accelerator</para></listitem>
202 * The "position" attribute determines where a constructed widget is positioned
203 * wrt. to its siblings in the partially constructed tree. If it is
204 * "top", the widget is prepended, otherwise it is appended.
207 * <refsect2 id="UI-Merging">
208 * <title>UI Merging</title>
210 * The most remarkable feature of #GtkUIManager is that it can overlay a set
211 * of menuitems and toolitems over another one, and demerge them later.
213 * Merging is done based on the names of the XML elements. Each element is
214 * identified by a path which consists of the names of its anchestors, separated
215 * by slashes. For example, the menuitem named "Left" in the example above
216 * has the path <literal>/ui/menubar/JustifyMenu/Left</literal> and the
217 * toolitem with the same name has path
218 * <literal>/ui/toolbar1/JustifyToolItems/Left</literal>.
222 * <title>Accelerators</title>
224 * Every action has an accelerator path. Accelerators are installed together with
225 * menuitem proxies, but they can also be explicitly added with <accelerator>
226 * elements in the UI definition. This makes it possible to have accelerators for
227 * actions even if they have no visible proxies.
230 * <refsect2 id="Smart-Separators">
231 * <title>Smart Separators</title>
233 * The separators created by #GtkUIManager are "smart", i.e. they do not show up
234 * in the UI unless they end up between two visible menu or tool items. Separators
235 * which are located at the very beginning or end of the menu or toolbar
236 * containing them, or multiple separators next to each other, are hidden. This
237 * is a useful feature, since the merging of UI elements from multiple sources
238 * can make it hard or impossible to determine in advance whether a separator
239 * will end up in such an unfortunate position.
241 * For separators in toolbars, you can set <literal>expand="true"</literal> to
242 * turn them from a small, visible separator to an expanding, invisible one.
243 * Toolitems following an expanding separator are effectively right-aligned.
247 * <title>Empty Menus</title>
249 * Submenus pose similar problems to separators inconnection with merging. It is
250 * impossible to know in advance whether they will end up empty after merging.
251 * #GtkUIManager offers two ways to treat empty submenus:
254 * <para>make them disappear by hiding the menu item they're attached to</para>
257 * <para>add an insensitive "Empty" item</para>
260 * The behaviour is chosen based on the "hide_if_empty" property of the action
261 * to which the submenu is associated.
264 * <refsect2 id="GtkUIManager-BUILDER-UI">
265 * <title>GtkUIManager as GtkBuildable</title>
267 * The GtkUIManager implementation of the GtkBuildable interface accepts
268 * GtkActionGroup objects as <child> elements in UI definitions.
270 * A GtkUIManager UI definition as described above can be embedded in
271 * an GtkUIManager <object> element in a GtkBuilder UI definition.
273 * The widgets that are constructed by a GtkUIManager can be embedded in
274 * other parts of the constructed user interface with the help of the
275 * "constructor" attribute. See the example below.
278 * <title>An embedded GtkUIManager UI definition</title>
279 * <programlisting><![CDATA[
280 * <object class="GtkUIManager" id="uiman">
282 * <object class="GtkActionGroup" id="actiongroup">
284 * <object class="GtkAction" id="file">
285 * <property name="label">_File</property>
291 * <menubar name="menubar1">
292 * <menu action="file">
297 * <object class="GtkWindow" id="main-window">
299 * <object class="GtkMenuBar" id="menubar1" constructor="uiman"/>
302 * ]]></programlisting>
309 #undef DEBUG_UI_MANAGER
318 NODE_TYPE_MENU_PLACEHOLDER,
319 NODE_TYPE_TOOLBAR_PLACEHOLDER,
324 NODE_TYPE_ACCELERATOR
327 typedef struct _Node Node;
337 GtkWidget *extra; /* second separator for placeholders */
342 guint expand : 1; /* used for separators */
343 guint popup_accels : 1;
344 guint always_show_image_set : 1; /* used for menu items */
345 guint always_show_image : 1; /* used for menu items */
349 struct _GtkUIManagerPrivate
351 GtkAccelGroup *accel_group;
354 GList *action_groups;
360 gboolean add_tearoffs;
363 #define NODE_INFO(node) ((Node *)node->data)
365 typedef struct _NodeUIReference NodeUIReference;
367 struct _NodeUIReference
373 static void gtk_ui_manager_finalize (GObject *object);
374 static void gtk_ui_manager_set_property (GObject *object,
378 static void gtk_ui_manager_get_property (GObject *object,
382 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
384 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
386 static void queue_update (GtkUIManager *manager);
387 static void dirty_all_nodes (GtkUIManager *manager);
388 static void mark_node_dirty (GNode *node);
389 static GNode * get_child_node (GtkUIManager *manager,
392 const gchar *childname,
393 gint childname_length,
397 static GNode * get_node (GtkUIManager *manager,
401 static gboolean free_node (GNode *node);
402 static void node_prepend_ui_reference (GNode *node,
404 GQuark action_quark);
405 static void node_remove_ui_reference (GNode *node,
409 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
410 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
414 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
417 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
420 const gchar *tagname,
421 GMarkupParser *parser,
423 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
426 const gchar *tagname,
428 static void gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
429 gboolean add_tearoffs);
451 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
453 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
454 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
455 gtk_ui_manager_buildable_init))
458 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
460 GObjectClass *gobject_class;
462 gobject_class = G_OBJECT_CLASS (klass);
464 gobject_class->finalize = gtk_ui_manager_finalize;
465 gobject_class->set_property = gtk_ui_manager_set_property;
466 gobject_class->get_property = gtk_ui_manager_get_property;
467 klass->get_widget = gtk_ui_manager_real_get_widget;
468 klass->get_action = gtk_ui_manager_real_get_action;
471 * GtkUIManager:add-tearoffs:
473 * The "add-tearoffs" property controls whether generated menus
474 * have tearoff menu items.
476 * Note that this only affects regular menus. Generated popup
477 * menus never have tearoff menu items.
481 * Deprecated: 3.4: Tearoff menus are deprecated and should not
482 * be used in newly written code.
484 g_object_class_install_property (gobject_class,
486 g_param_spec_boolean ("add-tearoffs",
487 P_("Add tearoffs to menus"),
488 P_("Whether tearoff menu items should be added to menus"),
490 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
492 g_object_class_install_property (gobject_class,
494 g_param_spec_string ("ui",
495 P_("Merged UI definition"),
496 P_("An XML string describing the merged UI"),
498 GTK_PARAM_READABLE));
502 * GtkUIManager::add-widget:
503 * @manager: a #GtkUIManager
504 * @widget: the added widget
506 * The ::add-widget signal is emitted for each generated menubar and toolbar.
507 * It is not emitted for generated popup menus, which can be obtained by
508 * gtk_ui_manager_get_widget().
512 ui_manager_signals[ADD_WIDGET] =
513 g_signal_new (I_("add-widget"),
514 G_OBJECT_CLASS_TYPE (klass),
515 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
516 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
518 g_cclosure_marshal_VOID__OBJECT,
523 * GtkUIManager::actions-changed:
524 * @manager: a #GtkUIManager
526 * The ::actions-changed signal is emitted whenever the set of actions
531 ui_manager_signals[ACTIONS_CHANGED] =
532 g_signal_new (I_("actions-changed"),
533 G_OBJECT_CLASS_TYPE (klass),
534 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
535 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
537 g_cclosure_marshal_VOID__VOID,
541 * GtkUIManager::connect-proxy:
542 * @manager: the ui manager
543 * @action: the action
546 * The ::connect-proxy signal is emitted after connecting a proxy to
547 * an action in the group.
549 * This is intended for simple customizations for which a custom action
550 * class would be too clumsy, e.g. showing tooltips for menuitems in the
555 ui_manager_signals[CONNECT_PROXY] =
556 g_signal_new (I_("connect-proxy"),
557 G_OBJECT_CLASS_TYPE (klass),
558 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
559 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
561 _gtk_marshal_VOID__OBJECT_OBJECT,
567 * GtkUIManager::disconnect-proxy:
568 * @manager: the ui manager
569 * @action: the action
572 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
573 * from an action in the group.
577 ui_manager_signals[DISCONNECT_PROXY] =
578 g_signal_new (I_("disconnect-proxy"),
579 G_OBJECT_CLASS_TYPE (klass),
580 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
581 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
583 _gtk_marshal_VOID__OBJECT_OBJECT,
589 * GtkUIManager::pre-activate:
590 * @manager: the ui manager
591 * @action: the action
593 * The ::pre-activate signal is emitted just before the @action
596 * This is intended for applications to get notification
597 * just before any action is activated.
601 ui_manager_signals[PRE_ACTIVATE] =
602 g_signal_new (I_("pre-activate"),
603 G_OBJECT_CLASS_TYPE (klass),
604 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
605 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
607 _gtk_marshal_VOID__OBJECT,
612 * GtkUIManager::post-activate:
613 * @manager: the ui manager
614 * @action: the action
616 * The ::post-activate signal is emitted just after the @action
619 * This is intended for applications to get notification
620 * just after any action is activated.
624 ui_manager_signals[POST_ACTIVATE] =
625 g_signal_new (I_("post-activate"),
626 G_OBJECT_CLASS_TYPE (klass),
627 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
628 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
630 _gtk_marshal_VOID__OBJECT,
634 klass->add_widget = NULL;
635 klass->actions_changed = NULL;
636 klass->connect_proxy = NULL;
637 klass->disconnect_proxy = NULL;
638 klass->pre_activate = NULL;
639 klass->post_activate = NULL;
641 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
646 gtk_ui_manager_init (GtkUIManager *manager)
651 manager->private_data = G_TYPE_INSTANCE_GET_PRIVATE (manager,
653 GtkUIManagerPrivate);
655 manager->private_data->accel_group = gtk_accel_group_new ();
657 manager->private_data->root_node = NULL;
658 manager->private_data->action_groups = NULL;
660 manager->private_data->last_merge_id = 0;
661 manager->private_data->add_tearoffs = FALSE;
663 merge_id = gtk_ui_manager_new_merge_id (manager);
664 node = get_child_node (manager, NULL, NULL, "ui", 2,
665 NODE_TYPE_ROOT, TRUE, FALSE);
666 node_prepend_ui_reference (node, merge_id, 0);
670 gtk_ui_manager_finalize (GObject *object)
672 GtkUIManager *manager = GTK_UI_MANAGER (object);
674 if (manager->private_data->update_tag != 0)
676 g_source_remove (manager->private_data->update_tag);
677 manager->private_data->update_tag = 0;
680 g_node_traverse (manager->private_data->root_node,
681 G_POST_ORDER, G_TRAVERSE_ALL, -1,
682 (GNodeTraverseFunc)free_node, NULL);
683 g_node_destroy (manager->private_data->root_node);
684 manager->private_data->root_node = NULL;
686 g_list_free_full (manager->private_data->action_groups, g_object_unref);
687 manager->private_data->action_groups = NULL;
689 g_object_unref (manager->private_data->accel_group);
690 manager->private_data->accel_group = NULL;
692 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
696 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
698 iface->add_child = gtk_ui_manager_buildable_add_child;
699 iface->construct_child = gtk_ui_manager_buildable_construct_child;
700 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
701 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
705 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
710 GtkUIManager *manager = GTK_UI_MANAGER (buildable);
713 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
715 pos = g_list_length (manager->private_data->action_groups);
717 gtk_ui_manager_insert_action_group (manager,
718 GTK_ACTION_GROUP (child),
723 child_hierarchy_changed_cb (GtkWidget *widget,
728 GtkAccelGroup *group;
731 toplevel = gtk_widget_get_toplevel (widget);
732 if (!toplevel || !GTK_IS_WINDOW (toplevel))
735 group = gtk_ui_manager_get_accel_group (uimgr);
736 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
737 if (g_slist_find (groups, group) == NULL)
738 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
740 g_signal_handlers_disconnect_by_func (widget,
741 child_hierarchy_changed_cb,
746 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
753 name = g_strdup_printf ("ui/%s", id);
754 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
757 g_error ("Unknown ui manager child: %s\n", name);
763 g_signal_connect (widget, "hierarchy-changed",
764 G_CALLBACK (child_hierarchy_changed_cb),
765 GTK_UI_MANAGER (buildable));
766 return g_object_ref (widget);
770 gtk_ui_manager_set_property (GObject *object,
775 GtkUIManager *manager = GTK_UI_MANAGER (object);
779 case PROP_ADD_TEAROFFS:
780 gtk_ui_manager_do_set_add_tearoffs (manager, g_value_get_boolean (value));
783 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
789 gtk_ui_manager_get_property (GObject *object,
794 GtkUIManager *manager = GTK_UI_MANAGER (object);
798 case PROP_ADD_TEAROFFS:
799 g_value_set_boolean (value, manager->private_data->add_tearoffs);
802 g_value_take_string (value, gtk_ui_manager_get_ui (manager));
805 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
811 gtk_ui_manager_real_get_widget (GtkUIManager *manager,
816 /* ensure that there are no pending updates before we get the
818 gtk_ui_manager_ensure_update (manager);
820 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
825 return NODE_INFO (node)->proxy;
829 gtk_ui_manager_real_get_action (GtkUIManager *manager,
834 /* ensure that there are no pending updates before we get
836 gtk_ui_manager_ensure_update (manager);
838 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
843 return NODE_INFO (node)->action;
848 * gtk_ui_manager_new:
850 * Creates a new ui manager object.
852 * Return value: a new ui manager object.
857 gtk_ui_manager_new (void)
859 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
864 * gtk_ui_manager_get_add_tearoffs:
865 * @manager: a #GtkUIManager
867 * Returns whether menus generated by this #GtkUIManager
868 * will have tearoff menu items.
870 * Return value: whether tearoff menu items are added
874 * Deprecated: 3.4: Tearoff menus are deprecated and should not
875 * be used in newly written code.
878 gtk_ui_manager_get_add_tearoffs (GtkUIManager *manager)
880 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), FALSE);
882 return manager->private_data->add_tearoffs;
887 * gtk_ui_manager_set_add_tearoffs:
888 * @manager: a #GtkUIManager
889 * @add_tearoffs: whether tearoff menu items are added
891 * Sets the "add_tearoffs" property, which controls whether menus
892 * generated by this #GtkUIManager will have tearoff menu items.
894 * Note that this only affects regular menus. Generated popup
895 * menus never have tearoff menu items.
899 * Deprecated: 3.4: Tearoff menus are deprecated and should not
900 * be used in newly written code.
903 gtk_ui_manager_set_add_tearoffs (GtkUIManager *manager,
904 gboolean add_tearoffs)
906 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
908 gtk_ui_manager_do_set_add_tearoffs (manager, add_tearoffs);
912 gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
913 gboolean add_tearoffs)
915 add_tearoffs = add_tearoffs != FALSE;
917 if (add_tearoffs != manager->private_data->add_tearoffs)
919 manager->private_data->add_tearoffs = add_tearoffs;
921 dirty_all_nodes (manager);
923 g_object_notify (G_OBJECT (manager), "add-tearoffs");
928 cb_proxy_connect_proxy (GtkActionGroup *group,
931 GtkUIManager *manager)
933 g_signal_emit (manager, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
937 cb_proxy_disconnect_proxy (GtkActionGroup *group,
940 GtkUIManager *manager)
942 g_signal_emit (manager, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
946 cb_proxy_pre_activate (GtkActionGroup *group,
948 GtkUIManager *manager)
950 g_signal_emit (manager, ui_manager_signals[PRE_ACTIVATE], 0, action);
954 cb_proxy_post_activate (GtkActionGroup *group,
956 GtkUIManager *manager)
958 g_signal_emit (manager, ui_manager_signals[POST_ACTIVATE], 0, action);
962 * gtk_ui_manager_insert_action_group:
963 * @manager: a #GtkUIManager object
964 * @action_group: the action group to be inserted
965 * @pos: the position at which the group will be inserted.
967 * Inserts an action group into the list of action groups associated
968 * with @manager. Actions in earlier groups hide actions with the same
969 * name in later groups.
971 * If @pos is larger than the number of action groups in @manager, or
972 * negative, @action_group will be inserted at the end of the internal
978 gtk_ui_manager_insert_action_group (GtkUIManager *manager,
979 GtkActionGroup *action_group,
982 #ifdef G_ENABLE_DEBUG
984 const char *group_name;
987 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
988 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
989 g_return_if_fail (g_list_find (manager->private_data->action_groups,
990 action_group) == NULL);
992 #ifdef G_ENABLE_DEBUG
993 group_name = gtk_action_group_get_name (action_group);
995 for (l = manager->private_data->action_groups; l; l = l->next)
997 GtkActionGroup *group = l->data;
999 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
1001 g_warning ("Inserting action group '%s' into UI manager which "
1002 "already has a group with this name\n", group_name);
1006 #endif /* G_ENABLE_DEBUG */
1008 g_object_ref (action_group);
1009 manager->private_data->action_groups =
1010 g_list_insert (manager->private_data->action_groups, action_group, pos);
1011 g_object_connect (action_group,
1012 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1013 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1014 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1015 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1018 /* dirty all nodes, as action bindings may change */
1019 dirty_all_nodes (manager);
1021 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1025 * gtk_ui_manager_remove_action_group:
1026 * @manager: a #GtkUIManager object
1027 * @action_group: the action group to be removed
1029 * Removes an action group from the list of action groups associated
1035 gtk_ui_manager_remove_action_group (GtkUIManager *manager,
1036 GtkActionGroup *action_group)
1038 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
1039 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1040 g_return_if_fail (g_list_find (manager->private_data->action_groups,
1041 action_group) != NULL);
1043 manager->private_data->action_groups =
1044 g_list_remove (manager->private_data->action_groups, action_group);
1046 g_object_disconnect (action_group,
1047 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1048 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1049 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1050 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1052 g_object_unref (action_group);
1054 /* dirty all nodes, as action bindings may change */
1055 dirty_all_nodes (manager);
1057 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1061 * gtk_ui_manager_get_action_groups:
1062 * @manager: a #GtkUIManager object
1064 * Returns the list of action groups associated with @manager.
1066 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
1067 * action groups. The list is owned by GTK+
1068 * and should not be modified.
1073 gtk_ui_manager_get_action_groups (GtkUIManager *manager)
1075 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1077 return manager->private_data->action_groups;
1081 * gtk_ui_manager_get_accel_group:
1082 * @manager: a #GtkUIManager object
1084 * Returns the #GtkAccelGroup associated with @manager.
1086 * Return value: (transfer none): the #GtkAccelGroup.
1091 gtk_ui_manager_get_accel_group (GtkUIManager *manager)
1093 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1095 return manager->private_data->accel_group;
1099 * gtk_ui_manager_get_widget:
1100 * @manager: a #GtkUIManager
1103 * Looks up a widget by following a path.
1104 * The path consists of the names specified in the XML description of the UI.
1105 * separated by '/'. Elements which don't have a name or action attribute in
1106 * the XML (e.g. <popup>) can be addressed by their XML element name
1107 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
1109 * Note that the widget found by following a path that ends in a <menu>
1110 * element is the menuitem to which the menu is attached, not the menu itmanager.
1112 * Also note that the widgets constructed by a ui manager are not tied to
1113 * the lifecycle of the ui manager. If you add the widgets returned by this
1114 * function to some container or explicitly ref them, they will survive the
1115 * destruction of the ui manager.
1117 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
1123 gtk_ui_manager_get_widget (GtkUIManager *manager,
1126 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1127 g_return_val_if_fail (path != NULL, NULL);
1129 return GTK_UI_MANAGER_GET_CLASS (manager)->get_widget (manager, path);
1133 GtkUIManagerItemType types;
1138 collect_toplevels (GNode *node,
1141 ToplevelData *data = user_data;
1143 if (NODE_INFO (node)->proxy)
1145 switch (NODE_INFO (node)->type)
1147 case NODE_TYPE_MENUBAR:
1148 if (data->types & GTK_UI_MANAGER_MENUBAR)
1149 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1151 case NODE_TYPE_TOOLBAR:
1152 if (data->types & GTK_UI_MANAGER_TOOLBAR)
1153 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1155 case NODE_TYPE_POPUP:
1156 if (data->types & GTK_UI_MANAGER_POPUP)
1157 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1165 * gtk_ui_manager_get_toplevels:
1166 * @manager: a #GtkUIManager
1167 * @types: specifies the types of toplevel widgets to include. Allowed
1168 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
1169 * #GTK_UI_MANAGER_POPUP.
1171 * Obtains a list of all toplevel widgets of the requested types.
1173 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
1174 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
1179 gtk_ui_manager_get_toplevels (GtkUIManager *manager,
1180 GtkUIManagerItemType types)
1184 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1185 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
1186 GTK_UI_MANAGER_TOOLBAR |
1187 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
1193 g_node_children_foreach (manager->private_data->root_node,
1195 collect_toplevels, &data);
1202 * gtk_ui_manager_get_action:
1203 * @manager: a #GtkUIManager
1206 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
1207 * for more information about paths.
1209 * Return value: (transfer none): the action whose proxy widget is found by following the path,
1210 * or %NULL if no widget was found.
1215 gtk_ui_manager_get_action (GtkUIManager *manager,
1218 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1219 g_return_val_if_fail (path != NULL, NULL);
1221 return GTK_UI_MANAGER_GET_CLASS (manager)->get_action (manager, path);
1225 node_is_dead (GNode *node)
1229 if (NODE_INFO (node)->uifiles != NULL)
1232 for (child = node->children; child != NULL; child = child->next)
1234 if (!node_is_dead (child))
1242 get_child_node (GtkUIManager *manager,
1245 const gchar *childname,
1246 gint childname_length,
1251 GNode *child = NULL;
1257 for (child = parent->children; child != NULL; child = child->next)
1259 if (NODE_INFO (child)->name &&
1260 strlen (NODE_INFO (child)->name) == childname_length &&
1261 !strncmp (NODE_INFO (child)->name, childname, childname_length))
1263 /* if undecided about node type, set it */
1264 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
1265 NODE_INFO (child)->type = node_type;
1267 /* warn about type mismatch */
1268 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
1269 node_type != NODE_TYPE_UNDECIDED &&
1270 NODE_INFO (child)->type != node_type)
1271 g_warning ("node type doesn't match %d (%s is type %d)",
1273 NODE_INFO (child)->name,
1274 NODE_INFO (child)->type);
1276 if (node_is_dead (child))
1278 /* This node was removed but is still dirty so
1279 * it is still in the tree. We want to treat this
1280 * as if it didn't exist, which means we move it
1281 * to the position it would have been created at.
1283 g_node_unlink (child);
1291 if (!child && create)
1295 mnode = g_slice_new0 (Node);
1296 mnode->type = node_type;
1297 mnode->name = g_strndup (childname, childname_length);
1299 child = g_node_new (mnode);
1304 g_node_insert_before (parent, sibling, child);
1306 g_node_insert_after (parent, sibling, child);
1311 g_node_prepend (parent, child);
1313 g_node_append (parent, child);
1316 mark_node_dirty (child);
1321 /* handle root node */
1322 if (manager->private_data->root_node)
1324 child = manager->private_data->root_node;
1325 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1326 g_warning ("root node name '%s' doesn't match '%s'",
1327 childname, NODE_INFO (child)->name);
1328 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1329 g_warning ("base element must be of type ROOT");
1335 mnode = g_slice_new0 (Node);
1336 mnode->type = node_type;
1337 mnode->name = g_strndup (childname, childname_length);
1338 mnode->dirty = TRUE;
1340 child = manager->private_data->root_node = g_node_new (mnode);
1348 get_node (GtkUIManager *manager,
1353 const gchar *pos, *end;
1354 GNode *parent, *node;
1356 if (strncmp ("/ui", path, 3) == 0)
1359 end = path + strlen (path);
1361 parent = node = NULL;
1367 slash = strchr (pos, '/');
1369 length = slash - pos;
1371 length = strlen (pos);
1373 node = get_child_node (manager, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1378 pos += length + 1; /* move past the node name and the slash too */
1382 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1383 NODE_INFO (node)->type = node_type;
1389 node_ui_reference_free (gpointer data)
1391 g_slice_free (NodeUIReference, data);
1395 free_node (GNode *node)
1397 Node *info = NODE_INFO (node);
1399 g_list_free_full (info->uifiles, node_ui_reference_free);
1400 info->uifiles = NULL;
1402 g_clear_object (&info->action);
1403 g_clear_object (&info->proxy);
1404 g_clear_object (&info->extra);
1405 g_clear_pointer (&info->name, g_free);
1406 g_slice_free (Node, info);
1413 * gtk_ui_manager_new_merge_id:
1414 * @manager: a #GtkUIManager
1416 * Returns an unused merge id, suitable for use with
1417 * gtk_ui_manager_add_ui().
1419 * Return value: an unused merge id.
1424 gtk_ui_manager_new_merge_id (GtkUIManager *manager)
1426 manager->private_data->last_merge_id++;
1428 return manager->private_data->last_merge_id;
1432 node_prepend_ui_reference (GNode *gnode,
1434 GQuark action_quark)
1436 Node *node = NODE_INFO (gnode);
1437 NodeUIReference *reference = NULL;
1439 if (node->uifiles &&
1440 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1441 reference = node->uifiles->data;
1444 reference = g_slice_new (NodeUIReference);
1445 node->uifiles = g_list_prepend (node->uifiles, reference);
1448 reference->merge_id = merge_id;
1449 reference->action_quark = action_quark;
1451 mark_node_dirty (gnode);
1455 node_remove_ui_reference (GNode *gnode,
1458 Node *node = NODE_INFO (gnode);
1461 for (p = node->uifiles; p != NULL; p = p->next)
1463 NodeUIReference *reference = p->data;
1465 if (reference->merge_id == merge_id)
1467 if (p == node->uifiles)
1468 mark_node_dirty (gnode);
1469 node->uifiles = g_list_delete_link (node->uifiles, p);
1470 g_slice_free (NodeUIReference, reference);
1477 /* -------------------- The UI file parser -------------------- */
1491 typedef struct _ParseContext ParseContext;
1492 struct _ParseContext
1495 ParseState prev_state;
1497 GtkUIManager *manager;
1505 start_element_handler (GMarkupParseContext *context,
1506 const gchar *element_name,
1507 const gchar **attribute_names,
1508 const gchar **attribute_values,
1512 ParseContext *ctx = user_data;
1513 GtkUIManager *manager = ctx->manager;
1516 const gchar *node_name;
1517 const gchar *action;
1518 GQuark action_quark;
1520 gboolean expand = FALSE;
1521 gboolean accelerators = FALSE;
1522 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1524 gboolean raise_error = TRUE;
1531 for (i = 0; attribute_names[i] != NULL; i++)
1533 if (!strcmp (attribute_names[i], "name"))
1535 node_name = attribute_values[i];
1537 else if (!strcmp (attribute_names[i], "action"))
1539 action = attribute_values[i];
1540 action_quark = g_quark_from_string (attribute_values[i]);
1542 else if (!strcmp (attribute_names[i], "position"))
1544 top = !strcmp (attribute_values[i], "top");
1546 else if (!strcmp (attribute_names[i], "expand"))
1548 expand = !strcmp (attribute_values[i], "true");
1550 else if (!strcmp (attribute_names[i], "accelerators"))
1552 accelerators = !strcmp (attribute_values[i], "true");
1554 else if (!strcmp (attribute_names[i], "always-show-image"))
1556 always_show_image_set = TRUE;
1557 always_show_image = !strcmp (attribute_values[i], "true");
1559 /* else silently skip unknown attributes to be compatible with
1560 * future additional attributes.
1564 /* Work out a name for this node. Either the name attribute, or
1565 * the action, or the element name */
1566 if (node_name == NULL)
1571 node_name = element_name;
1574 switch (element_name[0])
1577 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1579 ctx->state = STATE_ACCELERATOR;
1580 ctx->current = get_child_node (manager, ctx->current, NULL,
1581 node_name, strlen (node_name),
1582 NODE_TYPE_ACCELERATOR,
1584 if (NODE_INFO (ctx->current)->action_name == 0)
1585 NODE_INFO (ctx->current)->action_name = action_quark;
1587 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1589 raise_error = FALSE;
1593 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1595 ctx->state = STATE_ROOT;
1596 ctx->current = manager->private_data->root_node;
1597 raise_error = FALSE;
1599 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1603 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1605 ctx->state = STATE_MENU;
1606 ctx->current = get_child_node (manager, ctx->current, NULL,
1607 node_name, strlen (node_name),
1610 if (NODE_INFO (ctx->current)->action_name == 0)
1611 NODE_INFO (ctx->current)->action_name = action_quark;
1613 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1614 mark_node_dirty (ctx->current);
1616 raise_error = FALSE;
1618 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1620 ctx->current = get_child_node (manager, ctx->current, NULL,
1621 node_name, strlen (node_name),
1624 if (NODE_INFO (ctx->current)->action_name == 0)
1625 NODE_INFO (ctx->current)->action_name = action_quark;
1627 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1629 raise_error = FALSE;
1631 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1633 ctx->state = STATE_MENU;
1635 ctx->current = get_child_node (manager, g_node_last_child (ctx->current), NULL,
1636 node_name, strlen (node_name),
1639 if (NODE_INFO (ctx->current)->action_name == 0)
1640 NODE_INFO (ctx->current)->action_name = action_quark;
1642 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1644 raise_error = FALSE;
1646 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1650 ctx->state = STATE_MENUITEM;
1651 node = get_child_node (manager, ctx->current, NULL,
1652 node_name, strlen (node_name),
1655 if (NODE_INFO (node)->action_name == 0)
1656 NODE_INFO (node)->action_name = action_quark;
1658 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1659 NODE_INFO (node)->always_show_image = always_show_image;
1661 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1663 raise_error = FALSE;
1667 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1669 ctx->state = STATE_MENU;
1670 ctx->current = get_child_node (manager, ctx->current, NULL,
1671 node_name, strlen (node_name),
1675 NODE_INFO (ctx->current)->popup_accels = accelerators;
1677 if (NODE_INFO (ctx->current)->action_name == 0)
1678 NODE_INFO (ctx->current)->action_name = action_quark;
1680 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1682 raise_error = FALSE;
1684 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1685 !strcmp (element_name, "placeholder"))
1687 if (ctx->state == STATE_TOOLBAR)
1688 ctx->current = get_child_node (manager, ctx->current, NULL,
1689 node_name, strlen (node_name),
1690 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1693 ctx->current = get_child_node (manager, ctx->current, NULL,
1694 node_name, strlen (node_name),
1695 NODE_TYPE_MENU_PLACEHOLDER,
1698 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1700 raise_error = FALSE;
1704 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1705 !strcmp (element_name, "separator"))
1710 if (ctx->state == STATE_TOOLBAR)
1711 ctx->state = STATE_TOOLITEM;
1713 ctx->state = STATE_MENUITEM;
1714 if (!strcmp (node_name, "separator"))
1720 length = strlen (node_name);
1721 node = get_child_node (manager, ctx->current, NULL,
1723 NODE_TYPE_SEPARATOR,
1726 NODE_INFO (node)->expand = expand;
1728 if (NODE_INFO (node)->action_name == 0)
1729 NODE_INFO (node)->action_name = action_quark;
1731 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1733 raise_error = FALSE;
1737 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1739 ctx->state = STATE_TOOLBAR;
1740 ctx->current = get_child_node (manager, ctx->current, NULL,
1741 node_name, strlen (node_name),
1744 if (NODE_INFO (ctx->current)->action_name == 0)
1745 NODE_INFO (ctx->current)->action_name = action_quark;
1747 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1749 raise_error = FALSE;
1751 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1755 ctx->state = STATE_TOOLITEM;
1756 node = get_child_node (manager, ctx->current, NULL,
1757 node_name, strlen (node_name),
1760 if (NODE_INFO (node)->action_name == 0)
1761 NODE_INFO (node)->action_name = action_quark;
1763 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1765 raise_error = FALSE;
1773 gint line_number, char_number;
1775 g_markup_parse_context_get_position (context,
1776 &line_number, &char_number);
1779 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1780 _("Unexpected start tag '%s' on line %d char %d"),
1782 line_number, char_number);
1787 end_element_handler (GMarkupParseContext *context,
1788 const gchar *element_name,
1792 ParseContext *ctx = user_data;
1798 /* no need to GError here, GMarkup already catches this */
1801 ctx->current = NULL;
1802 ctx->state = STATE_END;
1806 case STATE_ACCELERATOR:
1807 ctx->current = ctx->current->parent;
1808 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1809 ctx->state = STATE_ROOT;
1810 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1812 ctx->current = ctx->current->parent;
1813 ctx->state = STATE_TOOLITEM;
1815 /* else, stay in same state */
1817 case STATE_MENUITEM:
1818 ctx->state = STATE_MENU;
1820 case STATE_TOOLITEM:
1821 ctx->state = STATE_TOOLBAR;
1827 cleanup (GMarkupParseContext *context,
1831 ParseContext *ctx = user_data;
1833 ctx->current = NULL;
1834 /* should also walk through the tree and get rid of nodes related to
1835 * this UI file's tag */
1837 gtk_ui_manager_remove_ui (ctx->manager, ctx->merge_id);
1841 xml_isspace (char c)
1843 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1847 text_handler (GMarkupParseContext *context,
1857 end = text + text_len;
1858 while (p != end && xml_isspace (*p))
1863 gint line_number, char_number;
1865 g_markup_parse_context_get_position (context,
1866 &line_number, &char_number);
1869 G_MARKUP_ERROR_INVALID_CONTENT,
1870 _("Unexpected character data on line %d char %d"),
1871 line_number, char_number);
1876 static const GMarkupParser ui_parser = {
1877 start_element_handler,
1878 end_element_handler,
1885 add_ui_from_string (GtkUIManager *manager,
1886 const gchar *buffer,
1888 gboolean needs_root,
1891 ParseContext ctx = { 0 };
1892 GMarkupParseContext *context;
1894 ctx.state = STATE_START;
1895 ctx.manager = manager;
1897 ctx.merge_id = gtk_ui_manager_new_merge_id (manager);
1899 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1902 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1905 if (!g_markup_parse_context_parse (context, buffer, length, error))
1909 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1912 if (!g_markup_parse_context_end_parse (context, error))
1915 g_markup_parse_context_free (context);
1917 queue_update (manager);
1919 g_object_notify (G_OBJECT (manager), "ui");
1921 return ctx.merge_id;
1925 g_markup_parse_context_free (context);
1931 * gtk_ui_manager_add_ui_from_string:
1932 * @manager: a #GtkUIManager object
1933 * @buffer: the string to parse
1934 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1935 * @error: return location for an error
1937 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1938 * merges it with the current contents of @manager. An enclosing <ui>
1939 * element is added if it is missing.
1941 * Return value: The merge id for the merged UI. The merge id can be used
1942 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1943 * the return value is 0.
1948 gtk_ui_manager_add_ui_from_string (GtkUIManager *manager,
1949 const gchar *buffer,
1953 gboolean needs_root = TRUE;
1957 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1958 g_return_val_if_fail (buffer != NULL, 0);
1961 length = strlen (buffer);
1964 end = buffer + length;
1965 while (p != end && xml_isspace (*p))
1968 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1971 return add_ui_from_string (manager, buffer, length, needs_root, error);
1975 * gtk_ui_manager_add_ui_from_file:
1976 * @manager: a #GtkUIManager object
1977 * @filename: (type filename): the name of the file to parse
1978 * @error: return location for an error
1980 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1981 * merges it with the current contents of @manager.
1983 * Return value: The merge id for the merged UI. The merge id can be used
1984 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1985 * the return value is 0.
1990 gtk_ui_manager_add_ui_from_file (GtkUIManager *manager,
1991 const gchar *filename,
1998 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2000 if (!g_file_get_contents (filename, &buffer, &length, error))
2003 res = add_ui_from_string (manager, buffer, length, FALSE, error);
2010 * gtk_ui_manager_add_ui_from_resource:
2011 * @manager: a #GtkUIManager object
2012 * @resource_path: the resource path of the file to parse
2013 * @error: return location for an error
2015 * Parses a resource file containing a <link linkend="XML-UI">UI definition</link> and
2016 * merges it with the current contents of @manager.
2018 * Return value: The merge id for the merged UI. The merge id can be used
2019 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
2020 * the return value is 0.
2025 gtk_ui_manager_add_ui_from_resource (GtkUIManager *manager,
2026 const gchar *resource_path,
2032 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2034 data = g_resources_lookup_data (resource_path, 0, error);
2038 res = add_ui_from_string (manager, g_bytes_get_data (data, NULL), g_bytes_get_size (data), FALSE, error);
2039 g_bytes_unref (data);
2045 * gtk_ui_manager_add_ui:
2046 * @manager: a #GtkUIManager
2047 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
2049 * @name: the name for the added UI element
2050 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
2051 * @type: the type of UI element to add.
2052 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
2053 * is added after its siblings.
2055 * Adds a UI element to the current contents of @manager.
2057 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
2058 * separator if such an element can be inserted at the place determined by
2059 * @path. Otherwise @type must indicate an element that can be inserted at
2060 * the place determined by @path.
2062 * If @path points to a menuitem or toolitem, the new element will be inserted
2063 * before or after this item, depending on @top.
2068 gtk_ui_manager_add_ui (GtkUIManager *manager,
2072 const gchar *action,
2073 GtkUIManagerItemType type,
2080 GQuark action_quark = 0;
2082 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2083 g_return_if_fail (merge_id > 0);
2084 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
2086 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
2092 node_type = NODE_TYPE_UNDECIDED;
2095 switch (NODE_INFO (node)->type)
2097 case NODE_TYPE_SEPARATOR:
2098 case NODE_TYPE_MENUITEM:
2099 case NODE_TYPE_TOOLITEM:
2101 node = node->parent;
2103 case NODE_TYPE_MENUBAR:
2104 case NODE_TYPE_MENU:
2105 case NODE_TYPE_POPUP:
2106 case NODE_TYPE_MENU_PLACEHOLDER:
2109 case GTK_UI_MANAGER_AUTO:
2111 node_type = NODE_TYPE_MENUITEM;
2113 node_type = NODE_TYPE_SEPARATOR;
2115 case GTK_UI_MANAGER_MENU:
2116 node_type = NODE_TYPE_MENU;
2118 case GTK_UI_MANAGER_MENUITEM:
2119 node_type = NODE_TYPE_MENUITEM;
2121 case GTK_UI_MANAGER_SEPARATOR:
2122 node_type = NODE_TYPE_SEPARATOR;
2124 case GTK_UI_MANAGER_PLACEHOLDER:
2125 node_type = NODE_TYPE_MENU_PLACEHOLDER;
2131 case NODE_TYPE_TOOLBAR:
2132 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2135 case GTK_UI_MANAGER_AUTO:
2137 node_type = NODE_TYPE_TOOLITEM;
2139 node_type = NODE_TYPE_SEPARATOR;
2141 case GTK_UI_MANAGER_TOOLITEM:
2142 node_type = NODE_TYPE_TOOLITEM;
2144 case GTK_UI_MANAGER_SEPARATOR:
2145 node_type = NODE_TYPE_SEPARATOR;
2147 case GTK_UI_MANAGER_PLACEHOLDER:
2148 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
2154 case NODE_TYPE_ROOT:
2157 case GTK_UI_MANAGER_MENUBAR:
2158 node_type = NODE_TYPE_MENUBAR;
2160 case GTK_UI_MANAGER_TOOLBAR:
2161 node_type = NODE_TYPE_TOOLBAR;
2163 case GTK_UI_MANAGER_POPUP:
2164 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
2165 node_type = NODE_TYPE_POPUP;
2167 case GTK_UI_MANAGER_ACCELERATOR:
2168 node_type = NODE_TYPE_ACCELERATOR;
2178 if (node_type == NODE_TYPE_UNDECIDED)
2180 g_warning ("item type %d not suitable for adding at '%s'",
2185 child = get_child_node (manager, node, sibling,
2186 name, name ? strlen (name) : 0,
2187 node_type, TRUE, top);
2189 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
2190 NODE_INFO (child)->popup_accels = TRUE;
2193 action_quark = g_quark_from_string (action);
2195 node_prepend_ui_reference (child, merge_id, action_quark);
2197 if (NODE_INFO (child)->action_name == 0)
2198 NODE_INFO (child)->action_name = action_quark;
2200 queue_update (manager);
2202 g_object_notify (G_OBJECT (manager), "ui");
2206 remove_ui (GNode *node,
2209 guint merge_id = GPOINTER_TO_UINT (user_data);
2211 node_remove_ui_reference (node, merge_id);
2213 return FALSE; /* continue */
2217 * gtk_ui_manager_remove_ui:
2218 * @manager: a #GtkUIManager object
2219 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
2221 * Unmerges the part of @manager<!-- -->s content identified by @merge_id.
2226 gtk_ui_manager_remove_ui (GtkUIManager *manager,
2229 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2231 g_node_traverse (manager->private_data->root_node,
2232 G_POST_ORDER, G_TRAVERSE_ALL, -1,
2233 remove_ui, GUINT_TO_POINTER (merge_id));
2235 queue_update (manager);
2237 g_object_notify (G_OBJECT (manager), "ui");
2240 /* -------------------- Updates -------------------- */
2244 get_action_by_name (GtkUIManager *merge,
2245 const gchar *action_name)
2253 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
2255 GtkActionGroup *action_group = tmp->data;
2258 action = gtk_action_group_get_action (action_group, action_name);
2268 find_menu_position (GNode *node,
2269 GtkWidget **menushell_p,
2272 GtkWidget *menushell;
2275 g_return_val_if_fail (node != NULL, FALSE);
2276 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
2277 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
2278 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
2279 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
2280 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2283 /* first sibling -- look at parent */
2284 if (node->prev == NULL)
2289 parent = node->parent;
2290 switch (NODE_INFO (parent)->type)
2292 case NODE_TYPE_MENUBAR:
2293 case NODE_TYPE_POPUP:
2294 menushell = NODE_INFO (parent)->proxy;
2297 case NODE_TYPE_MENU:
2298 menushell = NODE_INFO (parent)->proxy;
2299 if (GTK_IS_MENU_ITEM (menushell))
2300 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
2301 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
2302 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2306 g_list_free (siblings);
2308 case NODE_TYPE_MENU_PLACEHOLDER:
2309 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2310 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2311 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children,
2312 NODE_INFO (parent)->proxy) + 1;
2315 g_warning ("%s: bad parent node type %d", G_STRLOC,
2316 NODE_INFO (parent)->type);
2322 GtkWidget *prev_child;
2325 sibling = node->prev;
2326 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2327 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2329 prev_child = NODE_INFO (sibling)->proxy;
2331 if (!GTK_IS_WIDGET (prev_child))
2334 menushell = gtk_widget_get_parent (prev_child);
2335 if (!GTK_IS_MENU_SHELL (menushell))
2338 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children, prev_child) + 1;
2342 *menushell_p = menushell;
2350 find_toolbar_position (GNode *node,
2351 GtkWidget **toolbar_p,
2357 g_return_val_if_fail (node != NULL, FALSE);
2358 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2359 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2360 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2361 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2364 /* first sibling -- look at parent */
2365 if (node->prev == NULL)
2369 parent = node->parent;
2370 switch (NODE_INFO (parent)->type)
2372 case NODE_TYPE_TOOLBAR:
2373 toolbar = NODE_INFO (parent)->proxy;
2376 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2377 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2378 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2379 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2380 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2383 g_warning ("%s: bad parent node type %d", G_STRLOC,
2384 NODE_INFO (parent)->type);
2390 GtkWidget *prev_child;
2393 sibling = node->prev;
2394 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2395 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2397 prev_child = NODE_INFO (sibling)->proxy;
2399 if (!GTK_IS_WIDGET (prev_child))
2402 toolbar = gtk_widget_get_parent (prev_child);
2403 if (!GTK_IS_TOOLBAR (toolbar))
2406 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2407 GTK_TOOL_ITEM (prev_child)) + 1;
2411 *toolbar_p = toolbar;
2419 SEPARATOR_MODE_SMART,
2420 SEPARATOR_MODE_VISIBLE,
2421 SEPARATOR_MODE_HIDDEN
2425 update_smart_separators (GtkWidget *proxy)
2427 GtkWidget *parent = NULL;
2429 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2431 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2432 parent = gtk_widget_get_parent (proxy);
2438 GList *children, *cur, *last;
2441 children = gtk_container_get_children (GTK_CONTAINER (parent));
2451 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2455 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2456 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2459 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2460 "gtk-separator-mode"));
2463 case SEPARATOR_MODE_VISIBLE:
2464 gtk_widget_show (GTK_WIDGET (cur->data));
2468 case SEPARATOR_MODE_HIDDEN:
2469 gtk_widget_hide (GTK_WIDGET (cur->data));
2471 case SEPARATOR_MODE_SMART:
2474 gtk_widget_show (GTK_WIDGET (cur->data));
2479 gtk_widget_hide (GTK_WIDGET (cur->data));
2483 else if (gtk_widget_get_visible (cur->data))
2486 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2499 gtk_widget_hide (GTK_WIDGET (last->data));
2501 if (GTK_IS_MENU (parent))
2505 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2506 if (GTK_IS_MENU_ITEM (item))
2507 _gtk_action_sync_menu_visible (NULL, item, empty);
2508 if (GTK_IS_WIDGET (filler))
2511 gtk_widget_show (filler);
2513 gtk_widget_hide (filler);
2517 g_list_free (children);
2522 update_node (GtkUIManager *manager,
2525 gboolean popup_accels)
2530 const gchar *action_name;
2531 NodeUIReference *ref;
2533 #ifdef DEBUG_UI_MANAGER
2537 g_return_if_fail (node != NULL);
2538 g_return_if_fail (NODE_INFO (node) != NULL);
2540 info = NODE_INFO (node);
2545 if (info->type == NODE_TYPE_POPUP)
2548 popup_accels = info->popup_accels;
2551 #ifdef DEBUG_UI_MANAGER
2552 g_print ("update_node name=%s dirty=%d popup %d (",
2553 info->name, info->dirty, in_popup);
2554 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2556 NodeUIReference *ref = tmp->data;
2557 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2564 if (info->uifiles == NULL) {
2565 /* We may need to remove this node.
2566 * This must be done in post order
2568 goto recurse_children;
2571 ref = info->uifiles->data;
2572 action_name = g_quark_to_string (ref->action_quark);
2573 action = get_action_by_name (manager, action_name);
2575 info->dirty = FALSE;
2577 /* Check if the node doesn't have an action and must have an action */
2578 if (action == NULL &&
2579 info->type != NODE_TYPE_ROOT &&
2580 info->type != NODE_TYPE_MENUBAR &&
2581 info->type != NODE_TYPE_TOOLBAR &&
2582 info->type != NODE_TYPE_POPUP &&
2583 info->type != NODE_TYPE_SEPARATOR &&
2584 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2585 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2587 g_warning ("%s: missing action %s", info->name, action_name);
2593 gtk_action_set_accel_group (action, manager->private_data->accel_group);
2595 /* If the widget already has a proxy and the action hasn't changed, then
2596 * we only have to update the tearoff menu items.
2598 if (info->proxy != NULL && action == info->action)
2600 if (info->type == NODE_TYPE_MENU)
2605 if (GTK_IS_MENU (info->proxy))
2608 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2609 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2610 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2612 if (manager->private_data->add_tearoffs && !in_popup)
2613 gtk_widget_show (GTK_WIDGET (siblings->data));
2615 gtk_widget_hide (GTK_WIDGET (siblings->data));
2617 g_list_free (siblings);
2620 goto recurse_children;
2625 case NODE_TYPE_MENUBAR:
2626 if (info->proxy == NULL)
2628 info->proxy = gtk_menu_bar_new ();
2629 g_object_ref_sink (info->proxy);
2630 gtk_widget_set_name (info->proxy, info->name);
2631 gtk_widget_show (info->proxy);
2632 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2635 case NODE_TYPE_POPUP:
2636 if (info->proxy == NULL)
2638 info->proxy = gtk_menu_new ();
2639 g_object_ref_sink (info->proxy);
2641 gtk_widget_set_name (info->proxy, info->name);
2643 case NODE_TYPE_MENU:
2645 GtkWidget *prev_submenu = NULL;
2646 GtkWidget *menu = NULL;
2649 /* remove the proxy if it is of the wrong type ... */
2651 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2653 if (GTK_IS_MENU_ITEM (info->proxy))
2655 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2658 g_object_ref (prev_submenu);
2659 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2663 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2664 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2666 g_object_unref (info->proxy);
2670 /* create proxy if needed ... */
2671 if (info->proxy == NULL)
2673 /* ... if the action already provides a menu, then use
2674 * that menu instead of creating an empty one
2676 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2677 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2678 GTK_ACTION_GET_CLASS (action)->create_menu)
2680 menu = gtk_action_create_menu (action);
2688 menu = gtk_menu_new ();
2689 gtk_widget_set_name (menu, info->name);
2690 tearoff = gtk_tearoff_menu_item_new ();
2691 gtk_widget_set_no_show_all (tearoff, TRUE);
2692 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2693 filler = gtk_menu_item_new_with_label (_("Empty"));
2694 g_object_set_data (G_OBJECT (filler),
2695 I_("gtk-empty-menu-item"),
2696 GINT_TO_POINTER (TRUE));
2697 gtk_widget_set_sensitive (filler, FALSE);
2698 gtk_widget_set_no_show_all (filler, TRUE);
2699 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2702 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2705 g_object_ref_sink (info->proxy);
2706 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2711 GtkWidget *menushell;
2714 if (find_menu_position (node, &menushell, &pos))
2716 info->proxy = gtk_action_create_menu_item (action);
2717 g_object_ref_sink (info->proxy);
2718 g_signal_connect (info->proxy, "notify::visible",
2719 G_CALLBACK (update_smart_separators), NULL);
2720 gtk_widget_set_name (info->proxy, info->name);
2722 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2723 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2728 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2732 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2734 g_object_unref (prev_submenu);
2737 if (GTK_IS_MENU (info->proxy))
2740 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2742 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2743 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2745 if (manager->private_data->add_tearoffs && !in_popup)
2746 gtk_widget_show (GTK_WIDGET (siblings->data));
2748 gtk_widget_hide (GTK_WIDGET (siblings->data));
2750 g_list_free (siblings);
2753 case NODE_TYPE_UNDECIDED:
2754 g_warning ("found undecided node!");
2756 case NODE_TYPE_ROOT:
2758 case NODE_TYPE_TOOLBAR:
2759 if (info->proxy == NULL)
2761 info->proxy = gtk_toolbar_new ();
2762 g_object_ref_sink (info->proxy);
2763 gtk_widget_set_name (info->proxy, info->name);
2764 gtk_widget_show (info->proxy);
2765 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2768 case NODE_TYPE_MENU_PLACEHOLDER:
2769 /* create menu items for placeholders if necessary ... */
2770 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2771 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2775 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2777 g_object_unref (info->proxy);
2782 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2784 g_object_unref (info->extra);
2788 if (info->proxy == NULL)
2790 GtkWidget *menushell;
2793 if (find_menu_position (node, &menushell, &pos))
2795 info->proxy = gtk_separator_menu_item_new ();
2796 g_object_ref_sink (info->proxy);
2797 g_object_set_data (G_OBJECT (info->proxy),
2798 I_("gtk-separator-mode"),
2799 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2800 gtk_widget_set_no_show_all (info->proxy, TRUE);
2801 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2802 NODE_INFO (node)->proxy, pos);
2804 info->extra = gtk_separator_menu_item_new ();
2805 g_object_ref_sink (info->extra);
2806 g_object_set_data (G_OBJECT (info->extra),
2807 I_("gtk-separator-mode"),
2808 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2809 gtk_widget_set_no_show_all (info->extra, TRUE);
2810 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2811 NODE_INFO (node)->extra, pos + 1);
2815 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2816 /* create toolbar items for placeholders if necessary ... */
2817 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2818 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2822 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2824 g_object_unref (info->proxy);
2829 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2831 g_object_unref (info->extra);
2835 if (info->proxy == NULL)
2841 if (find_toolbar_position (node, &toolbar, &pos))
2843 item = gtk_separator_tool_item_new ();
2844 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2845 info->proxy = GTK_WIDGET (item);
2846 g_object_ref_sink (info->proxy);
2847 g_object_set_data (G_OBJECT (info->proxy),
2848 I_("gtk-separator-mode"),
2849 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2850 gtk_widget_set_no_show_all (info->proxy, TRUE);
2852 item = gtk_separator_tool_item_new ();
2853 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2854 info->extra = GTK_WIDGET (item);
2855 g_object_ref_sink (info->extra);
2856 g_object_set_data (G_OBJECT (info->extra),
2857 I_("gtk-separator-mode"),
2858 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2859 gtk_widget_set_no_show_all (info->extra, TRUE);
2863 case NODE_TYPE_MENUITEM:
2864 /* remove the proxy if it is of the wrong type ... */
2866 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2868 g_signal_handlers_disconnect_by_func (info->proxy,
2869 G_CALLBACK (update_smart_separators),
2871 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2872 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2874 g_object_unref (info->proxy);
2877 /* create proxy if needed ... */
2878 if (info->proxy == NULL)
2880 GtkWidget *menushell;
2883 if (find_menu_position (node, &menushell, &pos))
2885 info->proxy = gtk_action_create_menu_item (action);
2886 g_object_ref_sink (info->proxy);
2887 gtk_widget_set_name (info->proxy, info->name);
2889 if (info->always_show_image_set &&
2890 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2891 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2892 info->always_show_image);
2894 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2900 g_signal_handlers_disconnect_by_func (info->proxy,
2901 G_CALLBACK (update_smart_separators),
2903 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2904 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2909 g_signal_connect (info->proxy, "notify::visible",
2910 G_CALLBACK (update_smart_separators), NULL);
2911 if (in_popup && !popup_accels)
2913 /* don't show accels in popups */
2914 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2915 if (GTK_IS_ACCEL_LABEL (child))
2916 g_object_set (child, "accel-closure", NULL, NULL);
2921 case NODE_TYPE_TOOLITEM:
2922 /* remove the proxy if it is of the wrong type ... */
2924 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2926 g_signal_handlers_disconnect_by_func (info->proxy,
2927 G_CALLBACK (update_smart_separators),
2929 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2930 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2932 g_object_unref (info->proxy);
2935 /* create proxy if needed ... */
2936 if (info->proxy == NULL)
2941 if (find_toolbar_position (node, &toolbar, &pos))
2943 info->proxy = gtk_action_create_tool_item (action);
2944 g_object_ref_sink (info->proxy);
2945 gtk_widget_set_name (info->proxy, info->name);
2947 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2948 GTK_TOOL_ITEM (info->proxy), pos);
2953 g_signal_handlers_disconnect_by_func (info->proxy,
2954 G_CALLBACK (update_smart_separators),
2956 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2961 g_signal_connect (info->proxy, "notify::visible",
2962 G_CALLBACK (update_smart_separators), NULL);
2965 case NODE_TYPE_SEPARATOR:
2966 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2967 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2971 gint separator_mode;
2974 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2976 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2978 g_object_unref (info->proxy);
2982 if (find_toolbar_position (node, &toolbar, &pos))
2984 item = gtk_separator_tool_item_new ();
2985 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2986 info->proxy = GTK_WIDGET (item);
2987 g_object_ref_sink (info->proxy);
2988 gtk_widget_set_no_show_all (info->proxy, TRUE);
2991 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2992 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2993 separator_mode = SEPARATOR_MODE_VISIBLE;
2996 separator_mode = SEPARATOR_MODE_SMART;
2998 g_object_set_data (G_OBJECT (info->proxy),
2999 I_("gtk-separator-mode"),
3000 GINT_TO_POINTER (separator_mode));
3001 gtk_widget_show (info->proxy);
3006 GtkWidget *menushell;
3009 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
3011 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3013 g_object_unref (info->proxy);
3017 if (find_menu_position (node, &menushell, &pos))
3019 info->proxy = gtk_separator_menu_item_new ();
3020 g_object_ref_sink (info->proxy);
3021 gtk_widget_set_no_show_all (info->proxy, TRUE);
3022 g_object_set_data (G_OBJECT (info->proxy),
3023 I_("gtk-separator-mode"),
3024 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
3025 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
3027 gtk_widget_show (info->proxy);
3031 case NODE_TYPE_ACCELERATOR:
3032 gtk_action_connect_accelerator (action);
3037 g_object_ref (action);
3039 g_object_unref (info->action);
3040 info->action = action;
3043 /* process children */
3044 child = node->children;
3050 child = current->next;
3051 update_node (manager, current, in_popup, popup_accels);
3056 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
3057 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
3058 else if (info->type == NODE_TYPE_MENU ||
3059 info->type == NODE_TYPE_TOOLBAR ||
3060 info->type == NODE_TYPE_POPUP)
3061 update_smart_separators (info->proxy);
3064 /* handle cleanup of dead nodes */
3065 if (node->children == NULL && info->uifiles == NULL)
3068 gtk_widget_destroy (info->proxy);
3070 gtk_widget_destroy (info->extra);
3071 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
3072 gtk_action_disconnect_accelerator (info->action);
3074 g_node_destroy (node);
3079 do_updates (GtkUIManager *manager)
3081 /* this function needs to check through the tree for dirty nodes.
3082 * For such nodes, it needs to do the following:
3084 * 1) check if they are referenced by any loaded UI files anymore.
3085 * In which case, the proxy widget should be destroyed, unless
3086 * there are any subnodes.
3088 * 2) lookup the action for this node again. If it is different to
3089 * the current one (or if no previous action has been looked up),
3090 * the proxy is reconnected to the new action (or a new proxy widget
3091 * is created and added to the parent container).
3093 update_node (manager, manager->private_data->root_node, FALSE, FALSE);
3095 manager->private_data->update_tag = 0;
3101 do_updates_idle (GtkUIManager *manager)
3103 do_updates (manager);
3109 queue_update (GtkUIManager *manager)
3111 if (manager->private_data->update_tag != 0)
3114 manager->private_data->update_tag = gdk_threads_add_idle (
3115 (GSourceFunc)do_updates_idle,
3121 * gtk_ui_manager_ensure_update:
3122 * @manager: a #GtkUIManager
3124 * Makes sure that all pending updates to the UI have been completed.
3126 * This may occasionally be necessary, since #GtkUIManager updates the
3127 * UI in an idle function. A typical example where this function is
3128 * useful is to enforce that the menubar and toolbar have been added to
3129 * the main window before showing it:
3131 * gtk_container_add (GTK_CONTAINER (window), vbox);
3132 * g_signal_connect (merge, "add-widget",
3133 * G_CALLBACK (add_widget), vbox);
3134 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
3135 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
3136 * gtk_ui_manager_ensure_update (merge);
3137 * gtk_widget_show (window);
3143 gtk_ui_manager_ensure_update (GtkUIManager *manager)
3145 if (manager->private_data->update_tag != 0)
3147 g_source_remove (manager->private_data->update_tag);
3148 do_updates (manager);
3153 dirty_traverse_func (GNode *node,
3156 NODE_INFO (node)->dirty = TRUE;
3161 dirty_all_nodes (GtkUIManager *manager)
3163 g_node_traverse (manager->private_data->root_node,
3164 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3165 dirty_traverse_func, NULL);
3166 queue_update (manager);
3170 mark_node_dirty (GNode *node)
3174 /* FIXME could optimize this */
3175 for (p = node; p; p = p->parent)
3176 NODE_INFO (p)->dirty = TRUE;
3179 static const gchar *
3180 open_tag_format (NodeType type)
3184 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
3185 case NODE_TYPE_ROOT: return "%*s<ui";
3186 case NODE_TYPE_MENUBAR: return "%*s<menubar";
3187 case NODE_TYPE_MENU: return "%*s<menu";
3188 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
3189 case NODE_TYPE_MENU_PLACEHOLDER:
3190 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
3191 case NODE_TYPE_POPUP: return "%*s<popup";
3192 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
3193 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
3194 case NODE_TYPE_SEPARATOR: return "%*s<separator";
3195 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
3196 default: return NULL;
3200 static const gchar *
3201 close_tag_format (NodeType type)
3205 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
3206 case NODE_TYPE_ROOT: return "%*s</ui>\n";
3207 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
3208 case NODE_TYPE_MENU: return "%*s</menu>\n";
3209 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
3210 case NODE_TYPE_MENU_PLACEHOLDER:
3211 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
3212 case NODE_TYPE_POPUP: return "%*s</popup>\n";
3213 default: return NULL;
3218 print_node (GtkUIManager *manager,
3225 const gchar *open_fmt;
3226 const gchar *close_fmt;
3230 open_fmt = open_tag_format (mnode->type);
3231 close_fmt = close_tag_format (mnode->type);
3233 g_string_append_printf (buffer, open_fmt, indent_level, "");
3235 if (mnode->type != NODE_TYPE_ROOT)
3238 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
3240 if (mnode->action_name)
3241 g_string_append_printf (buffer, " action=\"%s\"",
3242 g_quark_to_string (mnode->action_name));
3245 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
3247 for (child = node->children; child != NULL; child = child->next)
3248 print_node (manager, child, indent_level + 2, buffer);
3251 g_string_append_printf (buffer, close_fmt, indent_level, "");
3255 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
3256 GtkBuilder *builder,
3258 const gchar *tagname,
3259 GMarkupParser *parser,
3265 if (strcmp (tagname, "ui") == 0)
3269 ctx = g_new0 (ParseContext, 1);
3270 ctx->state = STATE_START;
3271 ctx->manager = GTK_UI_MANAGER (buildable);
3272 ctx->current = NULL;
3273 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3276 *parser = ui_parser;
3286 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3287 GtkBuilder *builder,
3289 const gchar *tagname,
3292 queue_update (GTK_UI_MANAGER (buildable));
3293 g_object_notify (G_OBJECT (buildable), "ui");
3298 * gtk_ui_manager_get_ui:
3299 * @manager: a #GtkUIManager
3301 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3303 * Return value: A newly allocated string containing an XML representation of
3309 gtk_ui_manager_get_ui (GtkUIManager *manager)
3313 buffer = g_string_new (NULL);
3315 gtk_ui_manager_ensure_update (manager);
3317 print_node (manager, manager->private_data->root_node, 0, buffer);
3319 return g_string_free (buffer, FALSE);