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>
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"/>
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 g_object_ref (child);
718 gtk_ui_manager_insert_action_group (manager,
719 GTK_ACTION_GROUP (child),
724 child_hierarchy_changed_cb (GtkWidget *widget,
729 GtkAccelGroup *group;
732 toplevel = gtk_widget_get_toplevel (widget);
733 if (!toplevel || !GTK_IS_WINDOW (toplevel))
736 group = gtk_ui_manager_get_accel_group (uimgr);
737 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
738 if (g_slist_find (groups, group) == NULL)
739 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
741 g_signal_handlers_disconnect_by_func (widget,
742 child_hierarchy_changed_cb,
747 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
754 name = g_strdup_printf ("ui/%s", id);
755 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
758 g_error ("Unknown ui manager child: %s\n", name);
764 g_signal_connect (widget, "hierarchy-changed",
765 G_CALLBACK (child_hierarchy_changed_cb),
766 GTK_UI_MANAGER (buildable));
767 return g_object_ref (widget);
771 gtk_ui_manager_set_property (GObject *object,
776 GtkUIManager *manager = GTK_UI_MANAGER (object);
780 case PROP_ADD_TEAROFFS:
781 gtk_ui_manager_do_set_add_tearoffs (manager, g_value_get_boolean (value));
784 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
790 gtk_ui_manager_get_property (GObject *object,
795 GtkUIManager *manager = GTK_UI_MANAGER (object);
799 case PROP_ADD_TEAROFFS:
800 g_value_set_boolean (value, manager->private_data->add_tearoffs);
803 g_value_take_string (value, gtk_ui_manager_get_ui (manager));
806 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
812 gtk_ui_manager_real_get_widget (GtkUIManager *manager,
817 /* ensure that there are no pending updates before we get the
819 gtk_ui_manager_ensure_update (manager);
821 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
826 return NODE_INFO (node)->proxy;
830 gtk_ui_manager_real_get_action (GtkUIManager *manager,
835 /* ensure that there are no pending updates before we get
837 gtk_ui_manager_ensure_update (manager);
839 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
844 return NODE_INFO (node)->action;
849 * gtk_ui_manager_new:
851 * Creates a new ui manager object.
853 * Return value: a new ui manager object.
858 gtk_ui_manager_new (void)
860 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
865 * gtk_ui_manager_get_add_tearoffs:
866 * @manager: a #GtkUIManager
868 * Returns whether menus generated by this #GtkUIManager
869 * will have tearoff menu items.
871 * Return value: whether tearoff menu items are added
875 * Deprecated: 3.4: Tearoff menus are deprecated and should not
876 * be used in newly written code.
879 gtk_ui_manager_get_add_tearoffs (GtkUIManager *manager)
881 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), FALSE);
883 return manager->private_data->add_tearoffs;
888 * gtk_ui_manager_set_add_tearoffs:
889 * @manager: a #GtkUIManager
890 * @add_tearoffs: whether tearoff menu items are added
892 * Sets the "add_tearoffs" property, which controls whether menus
893 * generated by this #GtkUIManager will have tearoff menu items.
895 * Note that this only affects regular menus. Generated popup
896 * menus never have tearoff menu items.
900 * Deprecated: 3.4: Tearoff menus are deprecated and should not
901 * be used in newly written code.
904 gtk_ui_manager_set_add_tearoffs (GtkUIManager *manager,
905 gboolean add_tearoffs)
907 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
909 gtk_ui_manager_do_set_add_tearoffs (manager, add_tearoffs);
913 gtk_ui_manager_do_set_add_tearoffs (GtkUIManager *manager,
914 gboolean add_tearoffs)
916 add_tearoffs = add_tearoffs != FALSE;
918 if (add_tearoffs != manager->private_data->add_tearoffs)
920 manager->private_data->add_tearoffs = add_tearoffs;
922 dirty_all_nodes (manager);
924 g_object_notify (G_OBJECT (manager), "add-tearoffs");
929 cb_proxy_connect_proxy (GtkActionGroup *group,
932 GtkUIManager *manager)
934 g_signal_emit (manager, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
938 cb_proxy_disconnect_proxy (GtkActionGroup *group,
941 GtkUIManager *manager)
943 g_signal_emit (manager, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
947 cb_proxy_pre_activate (GtkActionGroup *group,
949 GtkUIManager *manager)
951 g_signal_emit (manager, ui_manager_signals[PRE_ACTIVATE], 0, action);
955 cb_proxy_post_activate (GtkActionGroup *group,
957 GtkUIManager *manager)
959 g_signal_emit (manager, ui_manager_signals[POST_ACTIVATE], 0, action);
963 * gtk_ui_manager_insert_action_group:
964 * @manager: a #GtkUIManager object
965 * @action_group: the action group to be inserted
966 * @pos: the position at which the group will be inserted.
968 * Inserts an action group into the list of action groups associated
969 * with @manager. Actions in earlier groups hide actions with the same
970 * name in later groups.
972 * If @pos is larger than the number of action groups in @manager, or
973 * negative, @action_group will be inserted at the end of the internal
979 gtk_ui_manager_insert_action_group (GtkUIManager *manager,
980 GtkActionGroup *action_group,
983 #ifdef G_ENABLE_DEBUG
985 const char *group_name;
988 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
989 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
990 g_return_if_fail (g_list_find (manager->private_data->action_groups,
991 action_group) == NULL);
993 #ifdef G_ENABLE_DEBUG
994 group_name = gtk_action_group_get_name (action_group);
996 for (l = manager->private_data->action_groups; l; l = l->next)
998 GtkActionGroup *group = l->data;
1000 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
1002 g_warning ("Inserting action group '%s' into UI manager which "
1003 "already has a group with this name\n", group_name);
1007 #endif /* G_ENABLE_DEBUG */
1009 g_object_ref (action_group);
1010 manager->private_data->action_groups =
1011 g_list_insert (manager->private_data->action_groups, action_group, pos);
1012 g_object_connect (action_group,
1013 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1014 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1015 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1016 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1019 /* dirty all nodes, as action bindings may change */
1020 dirty_all_nodes (manager);
1022 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1026 * gtk_ui_manager_remove_action_group:
1027 * @manager: a #GtkUIManager object
1028 * @action_group: the action group to be removed
1030 * Removes an action group from the list of action groups associated
1036 gtk_ui_manager_remove_action_group (GtkUIManager *manager,
1037 GtkActionGroup *action_group)
1039 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
1040 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1041 g_return_if_fail (g_list_find (manager->private_data->action_groups,
1042 action_group) != NULL);
1044 manager->private_data->action_groups =
1045 g_list_remove (manager->private_data->action_groups, action_group);
1047 g_object_disconnect (action_group,
1048 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), manager,
1049 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), manager,
1050 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), manager,
1051 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), manager,
1053 g_object_unref (action_group);
1055 /* dirty all nodes, as action bindings may change */
1056 dirty_all_nodes (manager);
1058 g_signal_emit (manager, ui_manager_signals[ACTIONS_CHANGED], 0);
1062 * gtk_ui_manager_get_action_groups:
1063 * @manager: a #GtkUIManager object
1065 * Returns the list of action groups associated with @manager.
1067 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
1068 * action groups. The list is owned by GTK+
1069 * and should not be modified.
1074 gtk_ui_manager_get_action_groups (GtkUIManager *manager)
1076 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1078 return manager->private_data->action_groups;
1082 * gtk_ui_manager_get_accel_group:
1083 * @manager: a #GtkUIManager object
1085 * Returns the #GtkAccelGroup associated with @manager.
1087 * Return value: (transfer none): the #GtkAccelGroup.
1092 gtk_ui_manager_get_accel_group (GtkUIManager *manager)
1094 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1096 return manager->private_data->accel_group;
1100 * gtk_ui_manager_get_widget:
1101 * @manager: a #GtkUIManager
1104 * Looks up a widget by following a path.
1105 * The path consists of the names specified in the XML description of the UI.
1106 * separated by '/'. Elements which don't have a name or action attribute in
1107 * the XML (e.g. <popup>) can be addressed by their XML element name
1108 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
1110 * Note that the widget found by following a path that ends in a <menu>
1111 * element is the menuitem to which the menu is attached, not the menu itmanager.
1113 * Also note that the widgets constructed by a ui manager are not tied to
1114 * the lifecycle of the ui manager. If you add the widgets returned by this
1115 * function to some container or explicitly ref them, they will survive the
1116 * destruction of the ui manager.
1118 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
1124 gtk_ui_manager_get_widget (GtkUIManager *manager,
1127 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1128 g_return_val_if_fail (path != NULL, NULL);
1130 return GTK_UI_MANAGER_GET_CLASS (manager)->get_widget (manager, path);
1134 GtkUIManagerItemType types;
1139 collect_toplevels (GNode *node,
1142 ToplevelData *data = user_data;
1144 if (NODE_INFO (node)->proxy)
1146 switch (NODE_INFO (node)->type)
1148 case NODE_TYPE_MENUBAR:
1149 if (data->types & GTK_UI_MANAGER_MENUBAR)
1150 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1152 case NODE_TYPE_TOOLBAR:
1153 if (data->types & GTK_UI_MANAGER_TOOLBAR)
1154 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1156 case NODE_TYPE_POPUP:
1157 if (data->types & GTK_UI_MANAGER_POPUP)
1158 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
1166 * gtk_ui_manager_get_toplevels:
1167 * @manager: a #GtkUIManager
1168 * @types: specifies the types of toplevel widgets to include. Allowed
1169 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
1170 * #GTK_UI_MANAGER_POPUP.
1172 * Obtains a list of all toplevel widgets of the requested types.
1174 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
1175 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
1180 gtk_ui_manager_get_toplevels (GtkUIManager *manager,
1181 GtkUIManagerItemType types)
1185 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1186 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
1187 GTK_UI_MANAGER_TOOLBAR |
1188 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
1194 g_node_children_foreach (manager->private_data->root_node,
1196 collect_toplevels, &data);
1203 * gtk_ui_manager_get_action:
1204 * @manager: a #GtkUIManager
1207 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
1208 * for more information about paths.
1210 * Return value: (transfer none): the action whose proxy widget is found by following the path,
1211 * or %NULL if no widget was found.
1216 gtk_ui_manager_get_action (GtkUIManager *manager,
1219 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), NULL);
1220 g_return_val_if_fail (path != NULL, NULL);
1222 return GTK_UI_MANAGER_GET_CLASS (manager)->get_action (manager, path);
1226 node_is_dead (GNode *node)
1230 if (NODE_INFO (node)->uifiles != NULL)
1233 for (child = node->children; child != NULL; child = child->next)
1235 if (!node_is_dead (child))
1243 get_child_node (GtkUIManager *manager,
1246 const gchar *childname,
1247 gint childname_length,
1252 GNode *child = NULL;
1258 for (child = parent->children; child != NULL; child = child->next)
1260 if (NODE_INFO (child)->name &&
1261 strlen (NODE_INFO (child)->name) == childname_length &&
1262 !strncmp (NODE_INFO (child)->name, childname, childname_length))
1264 /* if undecided about node type, set it */
1265 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
1266 NODE_INFO (child)->type = node_type;
1268 /* warn about type mismatch */
1269 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
1270 node_type != NODE_TYPE_UNDECIDED &&
1271 NODE_INFO (child)->type != node_type)
1272 g_warning ("node type doesn't match %d (%s is type %d)",
1274 NODE_INFO (child)->name,
1275 NODE_INFO (child)->type);
1277 if (node_is_dead (child))
1279 /* This node was removed but is still dirty so
1280 * it is still in the tree. We want to treat this
1281 * as if it didn't exist, which means we move it
1282 * to the position it would have been created at.
1284 g_node_unlink (child);
1292 if (!child && create)
1296 mnode = g_slice_new0 (Node);
1297 mnode->type = node_type;
1298 mnode->name = g_strndup (childname, childname_length);
1300 child = g_node_new (mnode);
1305 g_node_insert_before (parent, sibling, child);
1307 g_node_insert_after (parent, sibling, child);
1312 g_node_prepend (parent, child);
1314 g_node_append (parent, child);
1317 mark_node_dirty (child);
1322 /* handle root node */
1323 if (manager->private_data->root_node)
1325 child = manager->private_data->root_node;
1326 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1327 g_warning ("root node name '%s' doesn't match '%s'",
1328 childname, NODE_INFO (child)->name);
1329 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1330 g_warning ("base element must be of type ROOT");
1336 mnode = g_slice_new0 (Node);
1337 mnode->type = node_type;
1338 mnode->name = g_strndup (childname, childname_length);
1339 mnode->dirty = TRUE;
1341 child = manager->private_data->root_node = g_node_new (mnode);
1349 get_node (GtkUIManager *manager,
1354 const gchar *pos, *end;
1355 GNode *parent, *node;
1357 if (strncmp ("/ui", path, 3) == 0)
1360 end = path + strlen (path);
1362 parent = node = NULL;
1368 slash = strchr (pos, '/');
1370 length = slash - pos;
1372 length = strlen (pos);
1374 node = get_child_node (manager, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1379 pos += length + 1; /* move past the node name and the slash too */
1383 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1384 NODE_INFO (node)->type = node_type;
1390 node_ui_reference_free (gpointer data)
1392 g_slice_free (NodeUIReference, data);
1396 free_node (GNode *node)
1398 Node *info = NODE_INFO (node);
1400 g_list_free_full (info->uifiles, node_ui_reference_free);
1401 info->uifiles = NULL;
1403 g_clear_object (&info->action);
1404 g_clear_object (&info->proxy);
1405 g_clear_object (&info->extra);
1406 g_clear_pointer (&info->name, g_free);
1407 g_slice_free (Node, info);
1414 * gtk_ui_manager_new_merge_id:
1415 * @manager: a #GtkUIManager
1417 * Returns an unused merge id, suitable for use with
1418 * gtk_ui_manager_add_ui().
1420 * Return value: an unused merge id.
1425 gtk_ui_manager_new_merge_id (GtkUIManager *manager)
1427 manager->private_data->last_merge_id++;
1429 return manager->private_data->last_merge_id;
1433 node_prepend_ui_reference (GNode *gnode,
1435 GQuark action_quark)
1437 Node *node = NODE_INFO (gnode);
1438 NodeUIReference *reference = NULL;
1440 if (node->uifiles &&
1441 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1442 reference = node->uifiles->data;
1445 reference = g_slice_new (NodeUIReference);
1446 node->uifiles = g_list_prepend (node->uifiles, reference);
1449 reference->merge_id = merge_id;
1450 reference->action_quark = action_quark;
1452 mark_node_dirty (gnode);
1456 node_remove_ui_reference (GNode *gnode,
1459 Node *node = NODE_INFO (gnode);
1462 for (p = node->uifiles; p != NULL; p = p->next)
1464 NodeUIReference *reference = p->data;
1466 if (reference->merge_id == merge_id)
1468 if (p == node->uifiles)
1469 mark_node_dirty (gnode);
1470 node->uifiles = g_list_delete_link (node->uifiles, p);
1471 g_slice_free (NodeUIReference, reference);
1478 /* -------------------- The UI file parser -------------------- */
1492 typedef struct _ParseContext ParseContext;
1493 struct _ParseContext
1496 ParseState prev_state;
1498 GtkUIManager *manager;
1506 start_element_handler (GMarkupParseContext *context,
1507 const gchar *element_name,
1508 const gchar **attribute_names,
1509 const gchar **attribute_values,
1513 ParseContext *ctx = user_data;
1514 GtkUIManager *manager = ctx->manager;
1517 const gchar *node_name;
1518 const gchar *action;
1519 GQuark action_quark;
1521 gboolean expand = FALSE;
1522 gboolean accelerators = FALSE;
1523 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1525 gboolean raise_error = TRUE;
1532 for (i = 0; attribute_names[i] != NULL; i++)
1534 if (!strcmp (attribute_names[i], "name"))
1536 node_name = attribute_values[i];
1538 else if (!strcmp (attribute_names[i], "action"))
1540 action = attribute_values[i];
1541 action_quark = g_quark_from_string (attribute_values[i]);
1543 else if (!strcmp (attribute_names[i], "position"))
1545 top = !strcmp (attribute_values[i], "top");
1547 else if (!strcmp (attribute_names[i], "expand"))
1549 expand = !strcmp (attribute_values[i], "true");
1551 else if (!strcmp (attribute_names[i], "accelerators"))
1553 accelerators = !strcmp (attribute_values[i], "true");
1555 else if (!strcmp (attribute_names[i], "always-show-image"))
1557 always_show_image_set = TRUE;
1558 always_show_image = !strcmp (attribute_values[i], "true");
1560 /* else silently skip unknown attributes to be compatible with
1561 * future additional attributes.
1565 /* Work out a name for this node. Either the name attribute, or
1566 * the action, or the element name */
1567 if (node_name == NULL)
1572 node_name = element_name;
1575 switch (element_name[0])
1578 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1580 ctx->state = STATE_ACCELERATOR;
1581 ctx->current = get_child_node (manager, ctx->current, NULL,
1582 node_name, strlen (node_name),
1583 NODE_TYPE_ACCELERATOR,
1585 if (NODE_INFO (ctx->current)->action_name == 0)
1586 NODE_INFO (ctx->current)->action_name = action_quark;
1588 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1590 raise_error = FALSE;
1594 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1596 ctx->state = STATE_ROOT;
1597 ctx->current = manager->private_data->root_node;
1598 raise_error = FALSE;
1600 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1604 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1606 ctx->state = STATE_MENU;
1607 ctx->current = get_child_node (manager, ctx->current, NULL,
1608 node_name, strlen (node_name),
1611 if (NODE_INFO (ctx->current)->action_name == 0)
1612 NODE_INFO (ctx->current)->action_name = action_quark;
1614 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1615 mark_node_dirty (ctx->current);
1617 raise_error = FALSE;
1619 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1621 ctx->current = get_child_node (manager, ctx->current, NULL,
1622 node_name, strlen (node_name),
1625 if (NODE_INFO (ctx->current)->action_name == 0)
1626 NODE_INFO (ctx->current)->action_name = action_quark;
1628 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1630 raise_error = FALSE;
1632 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1634 ctx->state = STATE_MENU;
1636 ctx->current = get_child_node (manager, g_node_last_child (ctx->current), NULL,
1637 node_name, strlen (node_name),
1640 if (NODE_INFO (ctx->current)->action_name == 0)
1641 NODE_INFO (ctx->current)->action_name = action_quark;
1643 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1645 raise_error = FALSE;
1647 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1651 ctx->state = STATE_MENUITEM;
1652 node = get_child_node (manager, ctx->current, NULL,
1653 node_name, strlen (node_name),
1656 if (NODE_INFO (node)->action_name == 0)
1657 NODE_INFO (node)->action_name = action_quark;
1659 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1660 NODE_INFO (node)->always_show_image = always_show_image;
1662 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1664 raise_error = FALSE;
1668 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1670 ctx->state = STATE_MENU;
1671 ctx->current = get_child_node (manager, ctx->current, NULL,
1672 node_name, strlen (node_name),
1676 NODE_INFO (ctx->current)->popup_accels = accelerators;
1678 if (NODE_INFO (ctx->current)->action_name == 0)
1679 NODE_INFO (ctx->current)->action_name = action_quark;
1681 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1683 raise_error = FALSE;
1685 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1686 !strcmp (element_name, "placeholder"))
1688 if (ctx->state == STATE_TOOLBAR)
1689 ctx->current = get_child_node (manager, ctx->current, NULL,
1690 node_name, strlen (node_name),
1691 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1694 ctx->current = get_child_node (manager, ctx->current, NULL,
1695 node_name, strlen (node_name),
1696 NODE_TYPE_MENU_PLACEHOLDER,
1699 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1701 raise_error = FALSE;
1705 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1706 !strcmp (element_name, "separator"))
1711 if (ctx->state == STATE_TOOLBAR)
1712 ctx->state = STATE_TOOLITEM;
1714 ctx->state = STATE_MENUITEM;
1715 if (!strcmp (node_name, "separator"))
1721 length = strlen (node_name);
1722 node = get_child_node (manager, ctx->current, NULL,
1724 NODE_TYPE_SEPARATOR,
1727 NODE_INFO (node)->expand = expand;
1729 if (NODE_INFO (node)->action_name == 0)
1730 NODE_INFO (node)->action_name = action_quark;
1732 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1734 raise_error = FALSE;
1738 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1740 ctx->state = STATE_TOOLBAR;
1741 ctx->current = get_child_node (manager, ctx->current, NULL,
1742 node_name, strlen (node_name),
1745 if (NODE_INFO (ctx->current)->action_name == 0)
1746 NODE_INFO (ctx->current)->action_name = action_quark;
1748 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1750 raise_error = FALSE;
1752 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1756 ctx->state = STATE_TOOLITEM;
1757 node = get_child_node (manager, ctx->current, NULL,
1758 node_name, strlen (node_name),
1761 if (NODE_INFO (node)->action_name == 0)
1762 NODE_INFO (node)->action_name = action_quark;
1764 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1766 raise_error = FALSE;
1774 gint line_number, char_number;
1776 g_markup_parse_context_get_position (context,
1777 &line_number, &char_number);
1780 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1781 _("Unexpected start tag '%s' on line %d char %d"),
1783 line_number, char_number);
1788 end_element_handler (GMarkupParseContext *context,
1789 const gchar *element_name,
1793 ParseContext *ctx = user_data;
1799 /* no need to GError here, GMarkup already catches this */
1802 ctx->current = NULL;
1803 ctx->state = STATE_END;
1807 case STATE_ACCELERATOR:
1808 ctx->current = ctx->current->parent;
1809 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1810 ctx->state = STATE_ROOT;
1811 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1813 ctx->current = ctx->current->parent;
1814 ctx->state = STATE_TOOLITEM;
1816 /* else, stay in same state */
1818 case STATE_MENUITEM:
1819 ctx->state = STATE_MENU;
1821 case STATE_TOOLITEM:
1822 ctx->state = STATE_TOOLBAR;
1828 cleanup (GMarkupParseContext *context,
1832 ParseContext *ctx = user_data;
1834 ctx->current = NULL;
1835 /* should also walk through the tree and get rid of nodes related to
1836 * this UI file's tag */
1838 gtk_ui_manager_remove_ui (ctx->manager, ctx->merge_id);
1842 xml_isspace (char c)
1844 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1848 text_handler (GMarkupParseContext *context,
1858 end = text + text_len;
1859 while (p != end && xml_isspace (*p))
1864 gint line_number, char_number;
1866 g_markup_parse_context_get_position (context,
1867 &line_number, &char_number);
1870 G_MARKUP_ERROR_INVALID_CONTENT,
1871 _("Unexpected character data on line %d char %d"),
1872 line_number, char_number);
1877 static const GMarkupParser ui_parser = {
1878 start_element_handler,
1879 end_element_handler,
1886 add_ui_from_string (GtkUIManager *manager,
1887 const gchar *buffer,
1889 gboolean needs_root,
1892 ParseContext ctx = { 0 };
1893 GMarkupParseContext *context;
1895 ctx.state = STATE_START;
1896 ctx.manager = manager;
1898 ctx.merge_id = gtk_ui_manager_new_merge_id (manager);
1900 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1903 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1906 if (!g_markup_parse_context_parse (context, buffer, length, error))
1910 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1913 if (!g_markup_parse_context_end_parse (context, error))
1916 g_markup_parse_context_free (context);
1918 queue_update (manager);
1920 g_object_notify (G_OBJECT (manager), "ui");
1922 return ctx.merge_id;
1926 g_markup_parse_context_free (context);
1932 * gtk_ui_manager_add_ui_from_string:
1933 * @manager: a #GtkUIManager object
1934 * @buffer: the string to parse
1935 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1936 * @error: return location for an error
1938 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1939 * merges it with the current contents of @manager. An enclosing <ui>
1940 * element is added if it is missing.
1942 * Return value: The merge id for the merged UI. The merge id can be used
1943 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1944 * the return value is 0.
1949 gtk_ui_manager_add_ui_from_string (GtkUIManager *manager,
1950 const gchar *buffer,
1954 gboolean needs_root = TRUE;
1958 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
1959 g_return_val_if_fail (buffer != NULL, 0);
1962 length = strlen (buffer);
1965 end = buffer + length;
1966 while (p != end && xml_isspace (*p))
1969 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1972 return add_ui_from_string (manager, buffer, length, needs_root, error);
1976 * gtk_ui_manager_add_ui_from_file:
1977 * @manager: a #GtkUIManager object
1978 * @filename: (type filename): the name of the file to parse
1979 * @error: return location for an error
1981 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1982 * merges it with the current contents of @manager.
1984 * Return value: The merge id for the merged UI. The merge id can be used
1985 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1986 * the return value is 0.
1991 gtk_ui_manager_add_ui_from_file (GtkUIManager *manager,
1992 const gchar *filename,
1999 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2001 if (!g_file_get_contents (filename, &buffer, &length, error))
2004 res = add_ui_from_string (manager, buffer, length, FALSE, error);
2011 * gtk_ui_manager_add_ui_from_resource:
2012 * @manager: a #GtkUIManager object
2013 * @resource_path: the resource path of the file to parse
2014 * @error: return location for an error
2016 * Parses a resource file containing a <link linkend="XML-UI">UI definition</link> and
2017 * merges it with the current contents of @manager.
2019 * Return value: The merge id for the merged UI. The merge id can be used
2020 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
2021 * the return value is 0.
2026 gtk_ui_manager_add_ui_from_resource (GtkUIManager *manager,
2027 const gchar *resource_path,
2033 g_return_val_if_fail (GTK_IS_UI_MANAGER (manager), 0);
2035 data = g_resources_lookup_data (resource_path, 0, error);
2039 res = add_ui_from_string (manager, g_bytes_get_data (data, NULL), g_bytes_get_size (data), FALSE, error);
2040 g_bytes_unref (data);
2046 * gtk_ui_manager_add_ui:
2047 * @manager: a #GtkUIManager
2048 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
2050 * @name: the name for the added UI element
2051 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
2052 * @type: the type of UI element to add.
2053 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
2054 * is added after its siblings.
2056 * Adds a UI element to the current contents of @manager.
2058 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
2059 * separator if such an element can be inserted at the place determined by
2060 * @path. Otherwise @type must indicate an element that can be inserted at
2061 * the place determined by @path.
2063 * If @path points to a menuitem or toolitem, the new element will be inserted
2064 * before or after this item, depending on @top.
2069 gtk_ui_manager_add_ui (GtkUIManager *manager,
2073 const gchar *action,
2074 GtkUIManagerItemType type,
2081 GQuark action_quark = 0;
2083 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2084 g_return_if_fail (merge_id > 0);
2085 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
2087 node = get_node (manager, path, NODE_TYPE_UNDECIDED, FALSE);
2093 node_type = NODE_TYPE_UNDECIDED;
2096 switch (NODE_INFO (node)->type)
2098 case NODE_TYPE_SEPARATOR:
2099 case NODE_TYPE_MENUITEM:
2100 case NODE_TYPE_TOOLITEM:
2102 node = node->parent;
2104 case NODE_TYPE_MENUBAR:
2105 case NODE_TYPE_MENU:
2106 case NODE_TYPE_POPUP:
2107 case NODE_TYPE_MENU_PLACEHOLDER:
2110 case GTK_UI_MANAGER_AUTO:
2112 node_type = NODE_TYPE_MENUITEM;
2114 node_type = NODE_TYPE_SEPARATOR;
2116 case GTK_UI_MANAGER_MENU:
2117 node_type = NODE_TYPE_MENU;
2119 case GTK_UI_MANAGER_MENUITEM:
2120 node_type = NODE_TYPE_MENUITEM;
2122 case GTK_UI_MANAGER_SEPARATOR:
2123 node_type = NODE_TYPE_SEPARATOR;
2125 case GTK_UI_MANAGER_PLACEHOLDER:
2126 node_type = NODE_TYPE_MENU_PLACEHOLDER;
2132 case NODE_TYPE_TOOLBAR:
2133 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2136 case GTK_UI_MANAGER_AUTO:
2138 node_type = NODE_TYPE_TOOLITEM;
2140 node_type = NODE_TYPE_SEPARATOR;
2142 case GTK_UI_MANAGER_TOOLITEM:
2143 node_type = NODE_TYPE_TOOLITEM;
2145 case GTK_UI_MANAGER_SEPARATOR:
2146 node_type = NODE_TYPE_SEPARATOR;
2148 case GTK_UI_MANAGER_PLACEHOLDER:
2149 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
2155 case NODE_TYPE_ROOT:
2158 case GTK_UI_MANAGER_MENUBAR:
2159 node_type = NODE_TYPE_MENUBAR;
2161 case GTK_UI_MANAGER_TOOLBAR:
2162 node_type = NODE_TYPE_TOOLBAR;
2164 case GTK_UI_MANAGER_POPUP:
2165 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
2166 node_type = NODE_TYPE_POPUP;
2168 case GTK_UI_MANAGER_ACCELERATOR:
2169 node_type = NODE_TYPE_ACCELERATOR;
2179 if (node_type == NODE_TYPE_UNDECIDED)
2181 g_warning ("item type %d not suitable for adding at '%s'",
2186 child = get_child_node (manager, node, sibling,
2187 name, name ? strlen (name) : 0,
2188 node_type, TRUE, top);
2190 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
2191 NODE_INFO (child)->popup_accels = TRUE;
2194 action_quark = g_quark_from_string (action);
2196 node_prepend_ui_reference (child, merge_id, action_quark);
2198 if (NODE_INFO (child)->action_name == 0)
2199 NODE_INFO (child)->action_name = action_quark;
2201 queue_update (manager);
2203 g_object_notify (G_OBJECT (manager), "ui");
2207 remove_ui (GNode *node,
2210 guint merge_id = GPOINTER_TO_UINT (user_data);
2212 node_remove_ui_reference (node, merge_id);
2214 return FALSE; /* continue */
2218 * gtk_ui_manager_remove_ui:
2219 * @manager: a #GtkUIManager object
2220 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
2222 * Unmerges the part of @manager<!-- -->s content identified by @merge_id.
2227 gtk_ui_manager_remove_ui (GtkUIManager *manager,
2230 g_return_if_fail (GTK_IS_UI_MANAGER (manager));
2232 g_node_traverse (manager->private_data->root_node,
2233 G_POST_ORDER, G_TRAVERSE_ALL, -1,
2234 remove_ui, GUINT_TO_POINTER (merge_id));
2236 queue_update (manager);
2238 g_object_notify (G_OBJECT (manager), "ui");
2241 /* -------------------- Updates -------------------- */
2245 get_action_by_name (GtkUIManager *merge,
2246 const gchar *action_name)
2254 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
2256 GtkActionGroup *action_group = tmp->data;
2259 action = gtk_action_group_get_action (action_group, action_name);
2269 find_menu_position (GNode *node,
2270 GtkWidget **menushell_p,
2273 GtkWidget *menushell;
2276 g_return_val_if_fail (node != NULL, FALSE);
2277 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
2278 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
2279 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
2280 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
2281 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2284 /* first sibling -- look at parent */
2285 if (node->prev == NULL)
2290 parent = node->parent;
2291 switch (NODE_INFO (parent)->type)
2293 case NODE_TYPE_MENUBAR:
2294 case NODE_TYPE_POPUP:
2295 menushell = NODE_INFO (parent)->proxy;
2298 case NODE_TYPE_MENU:
2299 menushell = NODE_INFO (parent)->proxy;
2300 if (GTK_IS_MENU_ITEM (menushell))
2301 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
2302 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
2303 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2307 g_list_free (siblings);
2309 case NODE_TYPE_MENU_PLACEHOLDER:
2310 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2311 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2312 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children,
2313 NODE_INFO (parent)->proxy) + 1;
2316 g_warning ("%s: bad parent node type %d", G_STRLOC,
2317 NODE_INFO (parent)->type);
2323 GtkWidget *prev_child;
2326 sibling = node->prev;
2327 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2328 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2330 prev_child = NODE_INFO (sibling)->proxy;
2332 if (!GTK_IS_WIDGET (prev_child))
2335 menushell = gtk_widget_get_parent (prev_child);
2336 if (!GTK_IS_MENU_SHELL (menushell))
2339 pos = g_list_index (GTK_MENU_SHELL (menushell)->priv->children, prev_child) + 1;
2343 *menushell_p = menushell;
2351 find_toolbar_position (GNode *node,
2352 GtkWidget **toolbar_p,
2358 g_return_val_if_fail (node != NULL, FALSE);
2359 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2360 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2361 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2362 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2365 /* first sibling -- look at parent */
2366 if (node->prev == NULL)
2370 parent = node->parent;
2371 switch (NODE_INFO (parent)->type)
2373 case NODE_TYPE_TOOLBAR:
2374 toolbar = NODE_INFO (parent)->proxy;
2377 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2378 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2379 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2380 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2381 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2384 g_warning ("%s: bad parent node type %d", G_STRLOC,
2385 NODE_INFO (parent)->type);
2391 GtkWidget *prev_child;
2394 sibling = node->prev;
2395 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2396 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2398 prev_child = NODE_INFO (sibling)->proxy;
2400 if (!GTK_IS_WIDGET (prev_child))
2403 toolbar = gtk_widget_get_parent (prev_child);
2404 if (!GTK_IS_TOOLBAR (toolbar))
2407 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2408 GTK_TOOL_ITEM (prev_child)) + 1;
2412 *toolbar_p = toolbar;
2420 * _gtk_menu_is_empty:
2421 * @menu: (allow-none): a #GtkMenu or %NULL
2423 * Determines whether @menu is empty. A menu is considered empty if it
2424 * the only visible children are tearoff menu items or "filler" menu
2425 * items which were inserted to mark the menu as empty.
2427 * This function is used by #GtkAction.
2429 * Return value: whether @menu is empty.
2432 _gtk_menu_is_empty (GtkWidget *menu)
2434 GList *children, *cur;
2435 gboolean result = TRUE;
2437 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2442 children = gtk_container_get_children (GTK_CONTAINER (menu));
2447 if (gtk_widget_get_visible (cur->data))
2449 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2450 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2458 g_list_free (children);
2464 SEPARATOR_MODE_SMART,
2465 SEPARATOR_MODE_VISIBLE,
2466 SEPARATOR_MODE_HIDDEN
2470 update_smart_separators (GtkWidget *proxy)
2472 GtkWidget *parent = NULL;
2474 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2476 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2477 parent = gtk_widget_get_parent (proxy);
2483 GList *children, *cur, *last;
2486 children = gtk_container_get_children (GTK_CONTAINER (parent));
2496 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2500 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2501 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2504 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2505 "gtk-separator-mode"));
2508 case SEPARATOR_MODE_VISIBLE:
2509 gtk_widget_show (GTK_WIDGET (cur->data));
2513 case SEPARATOR_MODE_HIDDEN:
2514 gtk_widget_hide (GTK_WIDGET (cur->data));
2516 case SEPARATOR_MODE_SMART:
2519 gtk_widget_show (GTK_WIDGET (cur->data));
2524 gtk_widget_hide (GTK_WIDGET (cur->data));
2528 else if (gtk_widget_get_visible (cur->data))
2531 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2544 gtk_widget_hide (GTK_WIDGET (last->data));
2546 if (GTK_IS_MENU (parent))
2550 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2551 if (GTK_IS_MENU_ITEM (item))
2552 _gtk_action_sync_menu_visible (NULL, item, empty);
2553 if (GTK_IS_WIDGET (filler))
2556 gtk_widget_show (filler);
2558 gtk_widget_hide (filler);
2562 g_list_free (children);
2567 update_node (GtkUIManager *manager,
2570 gboolean popup_accels)
2575 const gchar *action_name;
2576 NodeUIReference *ref;
2578 #ifdef DEBUG_UI_MANAGER
2582 g_return_if_fail (node != NULL);
2583 g_return_if_fail (NODE_INFO (node) != NULL);
2585 info = NODE_INFO (node);
2590 if (info->type == NODE_TYPE_POPUP)
2593 popup_accels = info->popup_accels;
2596 #ifdef DEBUG_UI_MANAGER
2597 g_print ("update_node name=%s dirty=%d popup %d (",
2598 info->name, info->dirty, in_popup);
2599 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2601 NodeUIReference *ref = tmp->data;
2602 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2609 if (info->uifiles == NULL) {
2610 /* We may need to remove this node.
2611 * This must be done in post order
2613 goto recurse_children;
2616 ref = info->uifiles->data;
2617 action_name = g_quark_to_string (ref->action_quark);
2618 action = get_action_by_name (manager, action_name);
2620 info->dirty = FALSE;
2622 /* Check if the node doesn't have an action and must have an action */
2623 if (action == NULL &&
2624 info->type != NODE_TYPE_ROOT &&
2625 info->type != NODE_TYPE_MENUBAR &&
2626 info->type != NODE_TYPE_TOOLBAR &&
2627 info->type != NODE_TYPE_POPUP &&
2628 info->type != NODE_TYPE_SEPARATOR &&
2629 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2630 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2632 g_warning ("%s: missing action %s", info->name, action_name);
2638 gtk_action_set_accel_group (action, manager->private_data->accel_group);
2640 /* If the widget already has a proxy and the action hasn't changed, then
2641 * we only have to update the tearoff menu items.
2643 if (info->proxy != NULL && action == info->action)
2645 if (info->type == NODE_TYPE_MENU)
2650 if (GTK_IS_MENU (info->proxy))
2653 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2654 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2655 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2657 if (manager->private_data->add_tearoffs && !in_popup)
2658 gtk_widget_show (GTK_WIDGET (siblings->data));
2660 gtk_widget_hide (GTK_WIDGET (siblings->data));
2662 g_list_free (siblings);
2665 goto recurse_children;
2670 case NODE_TYPE_MENUBAR:
2671 if (info->proxy == NULL)
2673 info->proxy = gtk_menu_bar_new ();
2674 g_object_ref_sink (info->proxy);
2675 gtk_widget_set_name (info->proxy, info->name);
2676 gtk_widget_show (info->proxy);
2677 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2680 case NODE_TYPE_POPUP:
2681 if (info->proxy == NULL)
2683 info->proxy = gtk_menu_new ();
2684 g_object_ref_sink (info->proxy);
2686 gtk_widget_set_name (info->proxy, info->name);
2688 case NODE_TYPE_MENU:
2690 GtkWidget *prev_submenu = NULL;
2691 GtkWidget *menu = NULL;
2694 /* remove the proxy if it is of the wrong type ... */
2696 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2698 if (GTK_IS_MENU_ITEM (info->proxy))
2700 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2703 g_object_ref (prev_submenu);
2704 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2708 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2709 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2711 g_object_unref (info->proxy);
2715 /* create proxy if needed ... */
2716 if (info->proxy == NULL)
2718 /* ... if the action already provides a menu, then use
2719 * that menu instead of creating an empty one
2721 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2722 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2723 GTK_ACTION_GET_CLASS (action)->create_menu)
2725 menu = gtk_action_create_menu (action);
2733 menu = gtk_menu_new ();
2734 gtk_widget_set_name (menu, info->name);
2735 tearoff = gtk_tearoff_menu_item_new ();
2736 gtk_widget_set_no_show_all (tearoff, TRUE);
2737 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2738 filler = gtk_menu_item_new_with_label (_("Empty"));
2739 g_object_set_data (G_OBJECT (filler),
2740 I_("gtk-empty-menu-item"),
2741 GINT_TO_POINTER (TRUE));
2742 gtk_widget_set_sensitive (filler, FALSE);
2743 gtk_widget_set_no_show_all (filler, TRUE);
2744 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2747 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2750 g_object_ref_sink (info->proxy);
2751 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2756 GtkWidget *menushell;
2759 if (find_menu_position (node, &menushell, &pos))
2761 info->proxy = gtk_action_create_menu_item (action);
2762 g_object_ref_sink (info->proxy);
2763 g_signal_connect (info->proxy, "notify::visible",
2764 G_CALLBACK (update_smart_separators), NULL);
2765 gtk_widget_set_name (info->proxy, info->name);
2767 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2768 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2773 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2777 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2779 g_object_unref (prev_submenu);
2782 if (GTK_IS_MENU (info->proxy))
2785 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2787 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2788 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2790 if (manager->private_data->add_tearoffs && !in_popup)
2791 gtk_widget_show (GTK_WIDGET (siblings->data));
2793 gtk_widget_hide (GTK_WIDGET (siblings->data));
2795 g_list_free (siblings);
2798 case NODE_TYPE_UNDECIDED:
2799 g_warning ("found undecided node!");
2801 case NODE_TYPE_ROOT:
2803 case NODE_TYPE_TOOLBAR:
2804 if (info->proxy == NULL)
2806 info->proxy = gtk_toolbar_new ();
2807 g_object_ref_sink (info->proxy);
2808 gtk_widget_set_name (info->proxy, info->name);
2809 gtk_widget_show (info->proxy);
2810 g_signal_emit (manager, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2813 case NODE_TYPE_MENU_PLACEHOLDER:
2814 /* create menu items for placeholders if necessary ... */
2815 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2816 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2820 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2822 g_object_unref (info->proxy);
2827 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2829 g_object_unref (info->extra);
2833 if (info->proxy == NULL)
2835 GtkWidget *menushell;
2838 if (find_menu_position (node, &menushell, &pos))
2840 info->proxy = gtk_separator_menu_item_new ();
2841 g_object_ref_sink (info->proxy);
2842 g_object_set_data (G_OBJECT (info->proxy),
2843 I_("gtk-separator-mode"),
2844 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2845 gtk_widget_set_no_show_all (info->proxy, TRUE);
2846 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2847 NODE_INFO (node)->proxy, pos);
2849 info->extra = gtk_separator_menu_item_new ();
2850 g_object_ref_sink (info->extra);
2851 g_object_set_data (G_OBJECT (info->extra),
2852 I_("gtk-separator-mode"),
2853 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2854 gtk_widget_set_no_show_all (info->extra, TRUE);
2855 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2856 NODE_INFO (node)->extra, pos + 1);
2860 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2861 /* create toolbar items for placeholders if necessary ... */
2862 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2863 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2867 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2869 g_object_unref (info->proxy);
2874 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->extra)),
2876 g_object_unref (info->extra);
2880 if (info->proxy == NULL)
2886 if (find_toolbar_position (node, &toolbar, &pos))
2888 item = gtk_separator_tool_item_new ();
2889 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2890 info->proxy = GTK_WIDGET (item);
2891 g_object_ref_sink (info->proxy);
2892 g_object_set_data (G_OBJECT (info->proxy),
2893 I_("gtk-separator-mode"),
2894 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2895 gtk_widget_set_no_show_all (info->proxy, TRUE);
2897 item = gtk_separator_tool_item_new ();
2898 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2899 info->extra = GTK_WIDGET (item);
2900 g_object_ref_sink (info->extra);
2901 g_object_set_data (G_OBJECT (info->extra),
2902 I_("gtk-separator-mode"),
2903 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2904 gtk_widget_set_no_show_all (info->extra, TRUE);
2908 case NODE_TYPE_MENUITEM:
2909 /* remove the proxy if it is of the wrong type ... */
2911 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2913 g_signal_handlers_disconnect_by_func (info->proxy,
2914 G_CALLBACK (update_smart_separators),
2916 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2917 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2919 g_object_unref (info->proxy);
2922 /* create proxy if needed ... */
2923 if (info->proxy == NULL)
2925 GtkWidget *menushell;
2928 if (find_menu_position (node, &menushell, &pos))
2930 info->proxy = gtk_action_create_menu_item (action);
2931 g_object_ref_sink (info->proxy);
2932 gtk_widget_set_name (info->proxy, info->name);
2934 if (info->always_show_image_set &&
2935 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2936 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2937 info->always_show_image);
2939 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2945 g_signal_handlers_disconnect_by_func (info->proxy,
2946 G_CALLBACK (update_smart_separators),
2948 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2949 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2954 g_signal_connect (info->proxy, "notify::visible",
2955 G_CALLBACK (update_smart_separators), NULL);
2956 if (in_popup && !popup_accels)
2958 /* don't show accels in popups */
2959 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2960 if (GTK_IS_ACCEL_LABEL (child))
2961 g_object_set (child, "accel-closure", NULL, NULL);
2966 case NODE_TYPE_TOOLITEM:
2967 /* remove the proxy if it is of the wrong type ... */
2969 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2971 g_signal_handlers_disconnect_by_func (info->proxy,
2972 G_CALLBACK (update_smart_separators),
2974 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2975 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
2977 g_object_unref (info->proxy);
2980 /* create proxy if needed ... */
2981 if (info->proxy == NULL)
2986 if (find_toolbar_position (node, &toolbar, &pos))
2988 info->proxy = gtk_action_create_tool_item (action);
2989 g_object_ref_sink (info->proxy);
2990 gtk_widget_set_name (info->proxy, info->name);
2992 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2993 GTK_TOOL_ITEM (info->proxy), pos);
2998 g_signal_handlers_disconnect_by_func (info->proxy,
2999 G_CALLBACK (update_smart_separators),
3001 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
3006 g_signal_connect (info->proxy, "notify::visible",
3007 G_CALLBACK (update_smart_separators), NULL);
3010 case NODE_TYPE_SEPARATOR:
3011 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
3012 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
3016 gint separator_mode;
3019 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
3021 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3023 g_object_unref (info->proxy);
3027 if (find_toolbar_position (node, &toolbar, &pos))
3029 item = gtk_separator_tool_item_new ();
3030 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
3031 info->proxy = GTK_WIDGET (item);
3032 g_object_ref_sink (info->proxy);
3033 gtk_widget_set_no_show_all (info->proxy, TRUE);
3036 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
3037 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
3038 separator_mode = SEPARATOR_MODE_VISIBLE;
3041 separator_mode = SEPARATOR_MODE_SMART;
3043 g_object_set_data (G_OBJECT (info->proxy),
3044 I_("gtk-separator-mode"),
3045 GINT_TO_POINTER (separator_mode));
3046 gtk_widget_show (info->proxy);
3051 GtkWidget *menushell;
3054 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
3056 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (info->proxy)),
3058 g_object_unref (info->proxy);
3062 if (find_menu_position (node, &menushell, &pos))
3064 info->proxy = gtk_separator_menu_item_new ();
3065 g_object_ref_sink (info->proxy);
3066 gtk_widget_set_no_show_all (info->proxy, TRUE);
3067 g_object_set_data (G_OBJECT (info->proxy),
3068 I_("gtk-separator-mode"),
3069 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
3070 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
3072 gtk_widget_show (info->proxy);
3076 case NODE_TYPE_ACCELERATOR:
3077 gtk_action_connect_accelerator (action);
3082 g_object_ref (action);
3084 g_object_unref (info->action);
3085 info->action = action;
3088 /* process children */
3089 child = node->children;
3095 child = current->next;
3096 update_node (manager, current, in_popup, popup_accels);
3101 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
3102 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
3103 else if (info->type == NODE_TYPE_MENU ||
3104 info->type == NODE_TYPE_TOOLBAR ||
3105 info->type == NODE_TYPE_POPUP)
3106 update_smart_separators (info->proxy);
3109 /* handle cleanup of dead nodes */
3110 if (node->children == NULL && info->uifiles == NULL)
3113 gtk_widget_destroy (info->proxy);
3115 gtk_widget_destroy (info->extra);
3116 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
3117 gtk_action_disconnect_accelerator (info->action);
3119 g_node_destroy (node);
3124 do_updates (GtkUIManager *manager)
3126 /* this function needs to check through the tree for dirty nodes.
3127 * For such nodes, it needs to do the following:
3129 * 1) check if they are referenced by any loaded UI files anymore.
3130 * In which case, the proxy widget should be destroyed, unless
3131 * there are any subnodes.
3133 * 2) lookup the action for this node again. If it is different to
3134 * the current one (or if no previous action has been looked up),
3135 * the proxy is reconnected to the new action (or a new proxy widget
3136 * is created and added to the parent container).
3138 update_node (manager, manager->private_data->root_node, FALSE, FALSE);
3140 manager->private_data->update_tag = 0;
3146 do_updates_idle (GtkUIManager *manager)
3148 do_updates (manager);
3154 queue_update (GtkUIManager *manager)
3156 if (manager->private_data->update_tag != 0)
3159 manager->private_data->update_tag = gdk_threads_add_idle (
3160 (GSourceFunc)do_updates_idle,
3166 * gtk_ui_manager_ensure_update:
3167 * @manager: a #GtkUIManager
3169 * Makes sure that all pending updates to the UI have been completed.
3171 * This may occasionally be necessary, since #GtkUIManager updates the
3172 * UI in an idle function. A typical example where this function is
3173 * useful is to enforce that the menubar and toolbar have been added to
3174 * the main window before showing it:
3176 * gtk_container_add (GTK_CONTAINER (window), vbox);
3177 * g_signal_connect (merge, "add-widget",
3178 * G_CALLBACK (add_widget), vbox);
3179 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
3180 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
3181 * gtk_ui_manager_ensure_update (merge);
3182 * gtk_widget_show (window);
3188 gtk_ui_manager_ensure_update (GtkUIManager *manager)
3190 if (manager->private_data->update_tag != 0)
3192 g_source_remove (manager->private_data->update_tag);
3193 do_updates (manager);
3198 dirty_traverse_func (GNode *node,
3201 NODE_INFO (node)->dirty = TRUE;
3206 dirty_all_nodes (GtkUIManager *manager)
3208 g_node_traverse (manager->private_data->root_node,
3209 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
3210 dirty_traverse_func, NULL);
3211 queue_update (manager);
3215 mark_node_dirty (GNode *node)
3219 /* FIXME could optimize this */
3220 for (p = node; p; p = p->parent)
3221 NODE_INFO (p)->dirty = TRUE;
3224 static const gchar *
3225 open_tag_format (NodeType type)
3229 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
3230 case NODE_TYPE_ROOT: return "%*s<ui";
3231 case NODE_TYPE_MENUBAR: return "%*s<menubar";
3232 case NODE_TYPE_MENU: return "%*s<menu";
3233 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
3234 case NODE_TYPE_MENU_PLACEHOLDER:
3235 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
3236 case NODE_TYPE_POPUP: return "%*s<popup";
3237 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
3238 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
3239 case NODE_TYPE_SEPARATOR: return "%*s<separator";
3240 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
3241 default: return NULL;
3245 static const gchar *
3246 close_tag_format (NodeType type)
3250 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
3251 case NODE_TYPE_ROOT: return "%*s</ui>\n";
3252 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
3253 case NODE_TYPE_MENU: return "%*s</menu>\n";
3254 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
3255 case NODE_TYPE_MENU_PLACEHOLDER:
3256 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
3257 case NODE_TYPE_POPUP: return "%*s</popup>\n";
3258 default: return NULL;
3263 print_node (GtkUIManager *manager,
3270 const gchar *open_fmt;
3271 const gchar *close_fmt;
3275 open_fmt = open_tag_format (mnode->type);
3276 close_fmt = close_tag_format (mnode->type);
3278 g_string_append_printf (buffer, open_fmt, indent_level, "");
3280 if (mnode->type != NODE_TYPE_ROOT)
3283 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
3285 if (mnode->action_name)
3286 g_string_append_printf (buffer, " action=\"%s\"",
3287 g_quark_to_string (mnode->action_name));
3290 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
3292 for (child = node->children; child != NULL; child = child->next)
3293 print_node (manager, child, indent_level + 2, buffer);
3296 g_string_append_printf (buffer, close_fmt, indent_level, "");
3300 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
3301 GtkBuilder *builder,
3303 const gchar *tagname,
3304 GMarkupParser *parser,
3310 if (strcmp (tagname, "ui") == 0)
3314 ctx = g_new0 (ParseContext, 1);
3315 ctx->state = STATE_START;
3316 ctx->manager = GTK_UI_MANAGER (buildable);
3317 ctx->current = NULL;
3318 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3321 *parser = ui_parser;
3331 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3332 GtkBuilder *builder,
3334 const gchar *tagname,
3337 queue_update (GTK_UI_MANAGER (buildable));
3338 g_object_notify (G_OBJECT (buildable), "ui");
3343 * gtk_ui_manager_get_ui:
3344 * @manager: a #GtkUIManager
3346 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3348 * Return value: A newly allocated string containing an XML representation of
3354 gtk_ui_manager_get_ui (GtkUIManager *manager)
3358 buffer = g_string_new (NULL);
3360 gtk_ui_manager_ensure_update (manager);
3362 print_node (manager, manager->private_data->root_node, 0, buffer);
3364 return g_string_free (buffer, FALSE);