2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #include "gtkbuildable.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenubar.h"
39 #include "gtkmenushell.h"
40 #include "gtkmenutoolbutton.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtkseparatortoolitem.h"
43 #include "gtktearoffmenuitem.h"
44 #include "gtktoolbar.h"
45 #include "gtkuimanager.h"
46 #include "gtkprivate.h"
49 #undef DEBUG_UI_MANAGER
58 NODE_TYPE_MENU_PLACEHOLDER,
59 NODE_TYPE_TOOLBAR_PLACEHOLDER,
67 typedef struct _Node Node;
77 GtkWidget *extra; /* second separator for placeholders */
82 guint expand : 1; /* used for separators */
85 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
87 struct _GtkUIManagerPrivate
89 GtkAccelGroup *accel_group;
98 gboolean add_tearoffs;
101 #define NODE_INFO(node) ((Node *)node->data)
103 typedef struct _NodeUIReference NodeUIReference;
105 struct _NodeUIReference
111 static void gtk_ui_manager_finalize (GObject *object);
112 static void gtk_ui_manager_set_property (GObject *object,
116 static void gtk_ui_manager_get_property (GObject *object,
120 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
122 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
124 static void queue_update (GtkUIManager *self);
125 static void dirty_all_nodes (GtkUIManager *self);
126 static void mark_node_dirty (GNode *node);
127 static GNode * get_child_node (GtkUIManager *self,
130 const gchar *childname,
131 gint childname_length,
135 static GNode * get_node (GtkUIManager *self,
139 static gboolean free_node (GNode *node);
140 static void node_prepend_ui_reference (GNode *node,
142 GQuark action_quark);
143 static void node_remove_ui_reference (GNode *node,
147 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
148 static void gtk_ui_manager_buildable_add (GtkBuildable *buildable,
152 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
155 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
158 const gchar *tagname,
159 GMarkupParser *parser,
161 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
164 const gchar *tagname,
187 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
189 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, GTK_TYPE_OBJECT,
190 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
191 gtk_ui_manager_buildable_init))
194 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
196 GObjectClass *gobject_class;
198 gobject_class = G_OBJECT_CLASS (klass);
200 gobject_class->finalize = gtk_ui_manager_finalize;
201 gobject_class->set_property = gtk_ui_manager_set_property;
202 gobject_class->get_property = gtk_ui_manager_get_property;
203 klass->get_widget = gtk_ui_manager_real_get_widget;
204 klass->get_action = gtk_ui_manager_real_get_action;
207 * GtkUIManager:add-tearoffs:
209 * The "add-tearoffs" property controls whether generated menus
210 * have tearoff menu items.
212 * Note that this only affects regular menus. Generated popup
213 * menus never have tearoff menu items.
217 g_object_class_install_property (gobject_class,
219 g_param_spec_boolean ("add-tearoffs",
220 P_("Add tearoffs to menus"),
221 P_("Whether tearoff menu items should be added to menus"),
223 GTK_PARAM_READWRITE));
225 g_object_class_install_property (gobject_class,
227 g_param_spec_string ("ui",
228 P_("Merged UI definition"),
229 P_("An XML string describing the merged UI"),
231 GTK_PARAM_READABLE));
235 * GtkUIManager::add-widget:
236 * @merge: a #GtkUIManager
237 * @widget: the added widget
239 * The add_widget signal is emitted for each generated menubar and toolbar.
240 * It is not emitted for generated popup menus, which can be obtained by
241 * gtk_ui_manager_get_widget().
245 ui_manager_signals[ADD_WIDGET] =
246 g_signal_new (I_("add_widget"),
247 G_OBJECT_CLASS_TYPE (klass),
248 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
249 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
251 g_cclosure_marshal_VOID__OBJECT,
256 * GtkUIManager::actions-changed:
257 * @merge: a #GtkUIManager
259 * The "actions-changed" signal is emitted whenever the set of actions
264 ui_manager_signals[ACTIONS_CHANGED] =
265 g_signal_new (I_("actions_changed"),
266 G_OBJECT_CLASS_TYPE (klass),
267 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
268 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
270 g_cclosure_marshal_VOID__VOID,
274 * GtkUIManager::connect-proxy:
275 * @uimanager: the ui manager
276 * @action: the action
279 * The connect_proxy signal is emitted after connecting a proxy to
280 * an action in the group.
282 * This is intended for simple customizations for which a custom action
283 * class would be too clumsy, e.g. showing tooltips for menuitems in the
288 ui_manager_signals[CONNECT_PROXY] =
289 g_signal_new (I_("connect_proxy"),
290 G_OBJECT_CLASS_TYPE (klass),
291 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
292 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
294 _gtk_marshal_VOID__OBJECT_OBJECT,
300 * GtkUIManager::disconnect-proxy:
301 * @uimanager: the ui manager
302 * @action: the action
305 * The disconnect_proxy signal is emitted after disconnecting a proxy
306 * from an action in the group.
310 ui_manager_signals[DISCONNECT_PROXY] =
311 g_signal_new (I_("disconnect_proxy"),
312 G_OBJECT_CLASS_TYPE (klass),
313 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
314 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
316 _gtk_marshal_VOID__OBJECT_OBJECT,
322 * GtkUIManager::pre-activate:
323 * @uimanager: the ui manager
324 * @action: the action
326 * The pre_activate signal is emitted just before the @action
329 * This is intended for applications to get notification
330 * just before any action is activated.
334 ui_manager_signals[PRE_ACTIVATE] =
335 g_signal_new (I_("pre_activate"),
336 G_OBJECT_CLASS_TYPE (klass),
337 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
338 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
340 _gtk_marshal_VOID__OBJECT,
345 * GtkUIManager::post-activate:
346 * @uimanager: the ui manager
347 * @action: the action
349 * The post_activate signal is emitted just after the @action
352 * This is intended for applications to get notification
353 * just after any action is activated.
357 ui_manager_signals[POST_ACTIVATE] =
358 g_signal_new (I_("post_activate"),
359 G_OBJECT_CLASS_TYPE (klass),
360 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
361 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
363 _gtk_marshal_VOID__OBJECT,
367 klass->add_widget = NULL;
368 klass->actions_changed = NULL;
369 klass->connect_proxy = NULL;
370 klass->disconnect_proxy = NULL;
371 klass->pre_activate = NULL;
372 klass->post_activate = NULL;
374 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
379 gtk_ui_manager_init (GtkUIManager *self)
384 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
386 self->private_data->accel_group = gtk_accel_group_new ();
388 self->private_data->root_node = NULL;
389 self->private_data->action_groups = NULL;
391 self->private_data->last_merge_id = 0;
392 self->private_data->add_tearoffs = FALSE;
394 merge_id = gtk_ui_manager_new_merge_id (self);
395 node = get_child_node (self, NULL, NULL, "ui", 2,
396 NODE_TYPE_ROOT, TRUE, FALSE);
397 node_prepend_ui_reference (node, merge_id, 0);
401 gtk_ui_manager_finalize (GObject *object)
403 GtkUIManager *self = GTK_UI_MANAGER (object);
405 if (self->private_data->update_tag != 0)
407 g_source_remove (self->private_data->update_tag);
408 self->private_data->update_tag = 0;
411 g_node_traverse (self->private_data->root_node,
412 G_POST_ORDER, G_TRAVERSE_ALL, -1,
413 (GNodeTraverseFunc)free_node, NULL);
414 g_node_destroy (self->private_data->root_node);
415 self->private_data->root_node = NULL;
417 g_list_foreach (self->private_data->action_groups,
418 (GFunc) g_object_unref, NULL);
419 g_list_free (self->private_data->action_groups);
420 self->private_data->action_groups = NULL;
422 g_object_unref (self->private_data->accel_group);
423 self->private_data->accel_group = NULL;
425 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
429 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
431 iface->add = gtk_ui_manager_buildable_add;
432 iface->construct_child = gtk_ui_manager_buildable_construct_child;
433 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
434 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
438 gtk_ui_manager_buildable_add (GtkBuildable *buildable,
443 GtkUIManager *self = GTK_UI_MANAGER (buildable);
446 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
448 pos = g_list_length (self->private_data->action_groups);
450 g_object_ref (child);
451 gtk_ui_manager_insert_action_group (self,
452 GTK_ACTION_GROUP (child),
457 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
464 name = g_strdup_printf ("ui/%s", id);
465 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
467 g_error ("Unknown ui manager child: %s\n", name);
471 return G_OBJECT (widget);
475 gtk_ui_manager_set_property (GObject *object,
480 GtkUIManager *self = GTK_UI_MANAGER (object);
484 case PROP_ADD_TEAROFFS:
485 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
488 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
494 gtk_ui_manager_get_property (GObject *object,
499 GtkUIManager *self = GTK_UI_MANAGER (object);
503 case PROP_ADD_TEAROFFS:
504 g_value_set_boolean (value, self->private_data->add_tearoffs);
507 g_value_take_string (value, gtk_ui_manager_get_ui (self));
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516 gtk_ui_manager_real_get_widget (GtkUIManager *self,
521 /* ensure that there are no pending updates before we get the
523 gtk_ui_manager_ensure_update (self);
525 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
530 return NODE_INFO (node)->proxy;
534 gtk_ui_manager_real_get_action (GtkUIManager *self,
539 /* ensure that there are no pending updates before we get
541 gtk_ui_manager_ensure_update (self);
543 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
548 return NODE_INFO (node)->action;
553 * gtk_ui_manager_new:
555 * Creates a new ui manager object.
557 * Return value: a new ui manager object.
562 gtk_ui_manager_new (void)
564 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
569 * gtk_ui_manager_get_add_tearoffs:
570 * @self: a #GtkUIManager
572 * Returns whether menus generated by this #GtkUIManager
573 * will have tearoff menu items.
575 * Return value: whether tearoff menu items are added
580 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
582 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
584 return self->private_data->add_tearoffs;
589 * gtk_ui_manager_set_add_tearoffs:
590 * @self: a #GtkUIManager
591 * @add_tearoffs: whether tearoff menu items are added
593 * Sets the "add_tearoffs" property, which controls whether menus
594 * generated by this #GtkUIManager will have tearoff menu items.
596 * Note that this only affects regular menus. Generated popup
597 * menus never have tearoff menu items.
602 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
603 gboolean add_tearoffs)
605 g_return_if_fail (GTK_IS_UI_MANAGER (self));
607 add_tearoffs = add_tearoffs != FALSE;
609 if (add_tearoffs != self->private_data->add_tearoffs)
611 self->private_data->add_tearoffs = add_tearoffs;
613 dirty_all_nodes (self);
615 g_object_notify (G_OBJECT (self), "add-tearoffs");
620 cb_proxy_connect_proxy (GtkActionGroup *group,
625 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
629 cb_proxy_disconnect_proxy (GtkActionGroup *group,
634 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
638 cb_proxy_pre_activate (GtkActionGroup *group,
642 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
646 cb_proxy_post_activate (GtkActionGroup *group,
650 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
654 * gtk_ui_manager_insert_action_group:
655 * @self: a #GtkUIManager object
656 * @action_group: the action group to be inserted
657 * @pos: the position at which the group will be inserted.
659 * Inserts an action group into the list of action groups associated
660 * with @self. Actions in earlier groups hide actions with the same
661 * name in later groups.
666 gtk_ui_manager_insert_action_group (GtkUIManager *self,
667 GtkActionGroup *action_group,
670 g_return_if_fail (GTK_IS_UI_MANAGER (self));
671 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
672 g_return_if_fail (g_list_find (self->private_data->action_groups,
673 action_group) == NULL);
675 g_object_ref (action_group);
676 self->private_data->action_groups =
677 g_list_insert (self->private_data->action_groups, action_group, pos);
678 g_object_connect (action_group,
679 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
680 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
681 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
682 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
685 /* dirty all nodes, as action bindings may change */
686 dirty_all_nodes (self);
688 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
692 * gtk_ui_manager_remove_action_group:
693 * @self: a #GtkUIManager object
694 * @action_group: the action group to be removed
696 * Removes an action group from the list of action groups associated
702 gtk_ui_manager_remove_action_group (GtkUIManager *self,
703 GtkActionGroup *action_group)
705 g_return_if_fail (GTK_IS_UI_MANAGER (self));
706 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
707 g_return_if_fail (g_list_find (self->private_data->action_groups,
708 action_group) != NULL);
710 self->private_data->action_groups =
711 g_list_remove (self->private_data->action_groups, action_group);
713 g_object_disconnect (action_group,
714 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
715 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
716 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
717 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
719 g_object_unref (action_group);
721 /* dirty all nodes, as action bindings may change */
722 dirty_all_nodes (self);
724 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
728 * gtk_ui_manager_get_action_groups:
729 * @self: a #GtkUIManager object
731 * Returns the list of action groups associated with @self.
733 * Return value: a #GList of action groups. The list is owned by GTK+
734 * and should not be modified.
739 gtk_ui_manager_get_action_groups (GtkUIManager *self)
741 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
743 return self->private_data->action_groups;
747 * gtk_ui_manager_get_accel_group:
748 * @self: a #GtkUIManager object
750 * Returns the #GtkAccelGroup associated with @self.
752 * Return value: the #GtkAccelGroup.
757 gtk_ui_manager_get_accel_group (GtkUIManager *self)
759 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
761 return self->private_data->accel_group;
765 * gtk_ui_manager_get_widget:
766 * @self: a #GtkUIManager
769 * Looks up a widget by following a path.
770 * The path consists of the names specified in the XML description of the UI.
771 * separated by '/'. Elements which don't have a name or action attribute in
772 * the XML (e.g. <popup>) can be addressed by their XML element name
773 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
775 * Note that the widget found by following a path that ends in a <menu>
776 * element is the menuitem to which the menu is attached, not the menu itself.
778 * Also note that the widgets constructed by a ui manager are not tied to
779 * the lifecycle of the ui manager. If you add the widgets returned by this
780 * function to some container or explicitly ref them, they will survive the
781 * destruction of the ui manager.
783 * Return value: the widget found by following the path, or %NULL if no widget
789 gtk_ui_manager_get_widget (GtkUIManager *self,
792 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
793 g_return_val_if_fail (path != NULL, NULL);
795 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
799 GtkUIManagerItemType types;
804 collect_toplevels (GNode *node,
807 ToplevelData *data = user_data;
809 if (NODE_INFO (node)->proxy)
811 switch (NODE_INFO (node)->type)
813 case NODE_TYPE_MENUBAR:
814 if (data->types & GTK_UI_MANAGER_MENUBAR)
815 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
817 case NODE_TYPE_TOOLBAR:
818 if (data->types & GTK_UI_MANAGER_TOOLBAR)
819 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
821 case NODE_TYPE_POPUP:
822 if (data->types & GTK_UI_MANAGER_POPUP)
823 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
831 * gtk_ui_manager_get_toplevels:
832 * @self: a #GtkUIManager
833 * @types: specifies the types of toplevel widgets to include. Allowed
834 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
835 * #GTK_UI_MANAGER_POPUP.
837 * Obtains a list of all toplevel widgets of the requested types.
839 * Return value: a newly-allocated of all toplevel widgets of the requested
845 gtk_ui_manager_get_toplevels (GtkUIManager *self,
846 GtkUIManagerItemType types)
850 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
851 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
852 GTK_UI_MANAGER_TOOLBAR |
853 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
859 g_node_children_foreach (self->private_data->root_node,
861 collect_toplevels, &data);
868 * gtk_ui_manager_get_action:
869 * @self: a #GtkUIManager
872 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
873 * for more information about paths.
875 * Return value: the action whose proxy widget is found by following the path,
876 * or %NULL if no widget was found.
881 gtk_ui_manager_get_action (GtkUIManager *self,
884 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
885 g_return_val_if_fail (path != NULL, NULL);
887 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
891 get_child_node (GtkUIManager *self,
894 const gchar *childname,
895 gint childname_length,
906 for (child = parent->children; child != NULL; child = child->next)
908 if (NODE_INFO (child)->name &&
909 strlen (NODE_INFO (child)->name) == childname_length &&
910 !strncmp (NODE_INFO (child)->name, childname, childname_length))
912 /* if undecided about node type, set it */
913 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
914 NODE_INFO (child)->type = node_type;
916 /* warn about type mismatch */
917 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
918 node_type != NODE_TYPE_UNDECIDED &&
919 NODE_INFO (child)->type != node_type)
920 g_warning ("node type doesn't match %d (%s is type %d)",
922 NODE_INFO (child)->name,
923 NODE_INFO (child)->type);
929 if (!child && create)
933 mnode = g_slice_new0 (Node);
934 mnode->type = node_type;
935 mnode->name = g_strndup (childname, childname_length);
940 child = g_node_insert_before (parent, sibling,
943 child = g_node_insert_after (parent, sibling,
949 child = g_node_prepend_data (parent, mnode);
951 child = g_node_append_data (parent, mnode);
954 mark_node_dirty (child);
959 /* handle root node */
960 if (self->private_data->root_node)
962 child = self->private_data->root_node;
963 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
964 g_warning ("root node name '%s' doesn't match '%s'",
965 childname, NODE_INFO (child)->name);
966 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
967 g_warning ("base element must be of type ROOT");
973 mnode = g_slice_new0 (Node);
974 mnode->type = node_type;
975 mnode->name = g_strndup (childname, childname_length);
978 child = self->private_data->root_node = g_node_new (mnode);
986 get_node (GtkUIManager *self,
991 const gchar *pos, *end;
992 GNode *parent, *node;
994 if (strncmp ("/ui", path, 3) == 0)
997 end = path + strlen (path);
999 parent = node = NULL;
1005 slash = strchr (pos, '/');
1007 length = slash - pos;
1009 length = strlen (pos);
1011 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1016 pos += length + 1; /* move past the node name and the slash too */
1020 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1021 NODE_INFO (node)->type = node_type;
1027 node_ui_reference_free (gpointer data)
1029 g_slice_free (NodeUIReference, data);
1033 free_node (GNode *node)
1035 Node *info = NODE_INFO (node);
1037 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1038 g_list_free (info->uifiles);
1041 g_object_unref (info->action);
1043 g_object_unref (info->proxy);
1045 g_object_unref (info->extra);
1046 g_free (info->name);
1047 g_slice_free (Node, info);
1053 * gtk_ui_manager_new_merge_id:
1054 * @self: a #GtkUIManager
1056 * Returns an unused merge id, suitable for use with
1057 * gtk_ui_manager_add_ui().
1059 * Return value: an unused merge id.
1064 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1066 self->private_data->last_merge_id++;
1068 return self->private_data->last_merge_id;
1072 node_prepend_ui_reference (GNode *gnode,
1074 GQuark action_quark)
1076 Node *node = NODE_INFO (gnode);
1077 NodeUIReference *reference = NULL;
1079 if (node->uifiles &&
1080 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1081 reference = node->uifiles->data;
1084 reference = g_slice_new (NodeUIReference);
1085 node->uifiles = g_list_prepend (node->uifiles, reference);
1088 reference->merge_id = merge_id;
1089 reference->action_quark = action_quark;
1091 mark_node_dirty (gnode);
1095 node_remove_ui_reference (GNode *gnode,
1098 Node *node = NODE_INFO (gnode);
1101 for (p = node->uifiles; p != NULL; p = p->next)
1103 NodeUIReference *reference = p->data;
1105 if (reference->merge_id == merge_id)
1107 if (p == node->uifiles)
1108 mark_node_dirty (gnode);
1109 node->uifiles = g_list_delete_link (node->uifiles, p);
1110 g_slice_free (NodeUIReference, reference);
1117 /* -------------------- The UI file parser -------------------- */
1131 typedef struct _ParseContext ParseContext;
1132 struct _ParseContext
1135 ParseState prev_state;
1145 start_element_handler (GMarkupParseContext *context,
1146 const gchar *element_name,
1147 const gchar **attribute_names,
1148 const gchar **attribute_values,
1152 ParseContext *ctx = user_data;
1153 GtkUIManager *self = ctx->self;
1156 const gchar *node_name;
1157 const gchar *action;
1158 GQuark action_quark;
1160 gboolean expand = FALSE;
1162 gboolean raise_error = TRUE;
1169 for (i = 0; attribute_names[i] != NULL; i++)
1171 if (!strcmp (attribute_names[i], "name"))
1173 node_name = attribute_values[i];
1175 else if (!strcmp (attribute_names[i], "action"))
1177 action = attribute_values[i];
1178 action_quark = g_quark_from_string (attribute_values[i]);
1180 else if (!strcmp (attribute_names[i], "position"))
1182 top = !strcmp (attribute_values[i], "top");
1184 else if (!strcmp (attribute_names[i], "expand"))
1186 expand = !strcmp (attribute_values[i], "true");
1190 gint line_number, char_number;
1192 g_markup_parse_context_get_position (context,
1193 &line_number, &char_number);
1196 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1197 _("Unknown attribute '%s' on line %d char %d"),
1199 line_number, char_number);
1204 /* Work out a name for this node. Either the name attribute, or
1205 * the action, or the element name */
1206 if (node_name == NULL)
1211 node_name = element_name;
1214 switch (element_name[0])
1217 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1219 ctx->state = STATE_ACCELERATOR;
1220 ctx->current = get_child_node (self, ctx->current, NULL,
1221 node_name, strlen (node_name),
1222 NODE_TYPE_ACCELERATOR,
1224 if (NODE_INFO (ctx->current)->action_name == 0)
1225 NODE_INFO (ctx->current)->action_name = action_quark;
1227 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1229 raise_error = FALSE;
1233 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1235 ctx->state = STATE_ROOT;
1236 ctx->current = self->private_data->root_node;
1237 raise_error = FALSE;
1239 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1243 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1245 ctx->state = STATE_MENU;
1246 ctx->current = get_child_node (self, ctx->current, NULL,
1247 node_name, strlen (node_name),
1250 if (NODE_INFO (ctx->current)->action_name == 0)
1251 NODE_INFO (ctx->current)->action_name = action_quark;
1253 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1254 mark_node_dirty (ctx->current);
1256 raise_error = FALSE;
1258 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1260 ctx->current = get_child_node (self, ctx->current, NULL,
1261 node_name, strlen (node_name),
1264 if (NODE_INFO (ctx->current)->action_name == 0)
1265 NODE_INFO (ctx->current)->action_name = action_quark;
1267 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1269 raise_error = FALSE;
1271 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1273 ctx->state = STATE_MENU;
1275 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1276 node_name, strlen (node_name),
1279 if (NODE_INFO (ctx->current)->action_name == 0)
1280 NODE_INFO (ctx->current)->action_name = action_quark;
1282 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1284 raise_error = FALSE;
1286 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1290 ctx->state = STATE_MENUITEM;
1291 node = get_child_node (self, ctx->current, NULL,
1292 node_name, strlen (node_name),
1295 if (NODE_INFO (node)->action_name == 0)
1296 NODE_INFO (node)->action_name = action_quark;
1298 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1300 raise_error = FALSE;
1304 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1306 ctx->state = STATE_MENU;
1307 ctx->current = get_child_node (self, ctx->current, NULL,
1308 node_name, strlen (node_name),
1311 if (NODE_INFO (ctx->current)->action_name == 0)
1312 NODE_INFO (ctx->current)->action_name = action_quark;
1314 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1316 raise_error = FALSE;
1318 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1319 !strcmp (element_name, "placeholder"))
1321 if (ctx->state == STATE_TOOLBAR)
1322 ctx->current = get_child_node (self, ctx->current, NULL,
1323 node_name, strlen (node_name),
1324 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1327 ctx->current = get_child_node (self, ctx->current, NULL,
1328 node_name, strlen (node_name),
1329 NODE_TYPE_MENU_PLACEHOLDER,
1332 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1334 raise_error = FALSE;
1338 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1339 !strcmp (element_name, "separator"))
1344 if (ctx->state == STATE_TOOLBAR)
1345 ctx->state = STATE_TOOLITEM;
1347 ctx->state = STATE_MENUITEM;
1348 if (!strcmp (node_name, "separator"))
1354 length = strlen (node_name);
1355 node = get_child_node (self, ctx->current, NULL,
1357 NODE_TYPE_SEPARATOR,
1360 NODE_INFO (node)->expand = expand;
1362 if (NODE_INFO (node)->action_name == 0)
1363 NODE_INFO (node)->action_name = action_quark;
1365 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1367 raise_error = FALSE;
1371 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1373 ctx->state = STATE_TOOLBAR;
1374 ctx->current = get_child_node (self, ctx->current, NULL,
1375 node_name, strlen (node_name),
1378 if (NODE_INFO (ctx->current)->action_name == 0)
1379 NODE_INFO (ctx->current)->action_name = action_quark;
1381 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1383 raise_error = FALSE;
1385 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1389 ctx->state = STATE_TOOLITEM;
1390 node = get_child_node (self, ctx->current, NULL,
1391 node_name, strlen (node_name),
1394 if (NODE_INFO (node)->action_name == 0)
1395 NODE_INFO (node)->action_name = action_quark;
1397 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1399 raise_error = FALSE;
1407 gint line_number, char_number;
1409 g_markup_parse_context_get_position (context,
1410 &line_number, &char_number);
1413 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1414 _("Unexpected start tag '%s' on line %d char %d"),
1416 line_number, char_number);
1421 end_element_handler (GMarkupParseContext *context,
1422 const gchar *element_name,
1426 ParseContext *ctx = user_data;
1432 /* no need to GError here, GMarkup already catches this */
1435 ctx->current = NULL;
1436 ctx->state = STATE_END;
1440 case STATE_ACCELERATOR:
1441 ctx->current = ctx->current->parent;
1442 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1443 ctx->state = STATE_ROOT;
1444 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1446 ctx->current = ctx->current->parent;
1447 ctx->state = STATE_TOOLITEM;
1449 /* else, stay in same state */
1451 case STATE_MENUITEM:
1452 ctx->state = STATE_MENU;
1454 case STATE_TOOLITEM:
1455 ctx->state = STATE_TOOLBAR;
1461 cleanup (GMarkupParseContext *context,
1465 ParseContext *ctx = user_data;
1467 ctx->current = NULL;
1468 /* should also walk through the tree and get rid of nodes related to
1469 * this UI file's tag */
1471 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1475 xml_isspace (char c)
1477 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1481 text_handler (GMarkupParseContext *context,
1491 end = text + text_len;
1492 while (p != end && xml_isspace (*p))
1497 gint line_number, char_number;
1499 g_markup_parse_context_get_position (context,
1500 &line_number, &char_number);
1503 G_MARKUP_ERROR_INVALID_CONTENT,
1504 _("Unexpected character data on line %d char %d"),
1505 line_number, char_number);
1510 static const GMarkupParser ui_parser = {
1511 start_element_handler,
1512 end_element_handler,
1519 add_ui_from_string (GtkUIManager *self,
1520 const gchar *buffer,
1522 gboolean needs_root,
1525 ParseContext ctx = { 0 };
1526 GMarkupParseContext *context;
1528 ctx.state = STATE_START;
1531 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1533 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1536 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1539 if (!g_markup_parse_context_parse (context, buffer, length, error))
1543 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1546 if (!g_markup_parse_context_end_parse (context, error))
1549 g_markup_parse_context_free (context);
1551 queue_update (self);
1553 g_object_notify (G_OBJECT (self), "ui");
1555 return ctx.merge_id;
1559 g_markup_parse_context_free (context);
1565 * gtk_ui_manager_add_ui_from_string:
1566 * @self: a #GtkUIManager object
1567 * @buffer: the string to parse
1568 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1569 * @error: return location for an error
1571 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1572 * merges it with the current contents of @self. An enclosing <ui>
1573 * element is added if it is missing.
1575 * Return value: The merge id for the merged UI. The merge id can be used
1576 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1577 * the return value is 0.
1582 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1583 const gchar *buffer,
1587 gboolean needs_root = TRUE;
1591 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1592 g_return_val_if_fail (buffer != NULL, 0);
1595 length = strlen (buffer);
1598 end = buffer + length;
1599 while (p != end && xml_isspace (*p))
1602 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1605 return add_ui_from_string (self, buffer, length, needs_root, error);
1609 * gtk_ui_manager_add_ui_from_file:
1610 * @self: a #GtkUIManager object
1611 * @filename: the name of the file to parse
1612 * @error: return location for an error
1614 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1615 * merges it with the current contents of @self.
1617 * Return value: The merge id for the merged UI. The merge id can be used
1618 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1619 * the return value is 0.
1624 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1625 const gchar *filename,
1632 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1634 if (!g_file_get_contents (filename, &buffer, &length, error))
1637 res = add_ui_from_string (self, buffer, length, FALSE, error);
1644 * gtk_ui_manager_add_ui:
1645 * @self: a #GtkUIManager
1646 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1648 * @name: the name for the added UI element
1649 * @action: the name of the action to be proxied, or %NULL to add a separator
1650 * @type: the type of UI element to add.
1651 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1652 * is added after its siblings.
1654 * Adds a UI element to the current contents of @self.
1656 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1657 * separator if such an element can be inserted at the place determined by
1658 * @path. Otherwise @type must indicate an element that can be inserted at
1659 * the place determined by @path.
1661 * If @path points to a menuitem or toolitem, the new element will be inserted
1662 * before or after this item, depending on @top.
1667 gtk_ui_manager_add_ui (GtkUIManager *self,
1671 const gchar *action,
1672 GtkUIManagerItemType type,
1679 GQuark action_quark = 0;
1681 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1682 g_return_if_fail (merge_id > 0);
1683 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1685 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1691 node_type = NODE_TYPE_UNDECIDED;
1694 switch (NODE_INFO (node)->type)
1696 case NODE_TYPE_SEPARATOR:
1697 case NODE_TYPE_MENUITEM:
1698 case NODE_TYPE_TOOLITEM:
1700 node = node->parent;
1702 case NODE_TYPE_MENUBAR:
1703 case NODE_TYPE_MENU:
1704 case NODE_TYPE_POPUP:
1705 case NODE_TYPE_MENU_PLACEHOLDER:
1708 case GTK_UI_MANAGER_AUTO:
1710 node_type = NODE_TYPE_MENUITEM;
1712 node_type = NODE_TYPE_SEPARATOR;
1714 case GTK_UI_MANAGER_MENU:
1715 node_type = NODE_TYPE_MENU;
1717 case GTK_UI_MANAGER_MENUITEM:
1718 node_type = NODE_TYPE_MENUITEM;
1720 case GTK_UI_MANAGER_SEPARATOR:
1721 node_type = NODE_TYPE_SEPARATOR;
1723 case GTK_UI_MANAGER_PLACEHOLDER:
1724 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1730 case NODE_TYPE_TOOLBAR:
1731 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1734 case GTK_UI_MANAGER_AUTO:
1736 node_type = NODE_TYPE_TOOLITEM;
1738 node_type = NODE_TYPE_SEPARATOR;
1740 case GTK_UI_MANAGER_TOOLITEM:
1741 node_type = NODE_TYPE_TOOLITEM;
1743 case GTK_UI_MANAGER_SEPARATOR:
1744 node_type = NODE_TYPE_SEPARATOR;
1746 case GTK_UI_MANAGER_PLACEHOLDER:
1747 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1753 case NODE_TYPE_ROOT:
1756 case GTK_UI_MANAGER_MENUBAR:
1757 node_type = NODE_TYPE_MENUBAR;
1759 case GTK_UI_MANAGER_TOOLBAR:
1760 node_type = NODE_TYPE_TOOLBAR;
1762 case GTK_UI_MANAGER_POPUP:
1763 node_type = NODE_TYPE_POPUP;
1765 case GTK_UI_MANAGER_ACCELERATOR:
1766 node_type = NODE_TYPE_ACCELERATOR;
1776 if (node_type == NODE_TYPE_UNDECIDED)
1778 g_warning ("item type %d not suitable for adding at '%s'",
1783 child = get_child_node (self, node, sibling,
1784 name, name ? strlen (name) : 0,
1785 node_type, TRUE, top);
1788 action_quark = g_quark_from_string (action);
1790 node_prepend_ui_reference (child, merge_id, action_quark);
1792 if (NODE_INFO (child)->action_name == 0)
1793 NODE_INFO (child)->action_name = action_quark;
1795 queue_update (self);
1797 g_object_notify (G_OBJECT (self), "ui");
1801 remove_ui (GNode *node,
1804 guint merge_id = GPOINTER_TO_UINT (user_data);
1806 node_remove_ui_reference (node, merge_id);
1808 return FALSE; /* continue */
1812 * gtk_ui_manager_remove_ui:
1813 * @self: a #GtkUIManager object
1814 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1816 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1821 gtk_ui_manager_remove_ui (GtkUIManager *self,
1824 g_node_traverse (self->private_data->root_node,
1825 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1826 remove_ui, GUINT_TO_POINTER (merge_id));
1828 queue_update (self);
1830 g_object_notify (G_OBJECT (self), "ui");
1833 /* -------------------- Updates -------------------- */
1837 get_action_by_name (GtkUIManager *merge,
1838 const gchar *action_name)
1846 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1848 GtkActionGroup *action_group = tmp->data;
1851 action = gtk_action_group_get_action (action_group, action_name);
1861 find_menu_position (GNode *node,
1862 GtkWidget **menushell_p,
1865 GtkWidget *menushell;
1868 g_return_val_if_fail (node != NULL, FALSE);
1869 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1870 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1871 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1872 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1873 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1876 /* first sibling -- look at parent */
1877 if (node->prev == NULL)
1882 parent = node->parent;
1883 switch (NODE_INFO (parent)->type)
1885 case NODE_TYPE_MENUBAR:
1886 case NODE_TYPE_POPUP:
1887 menushell = NODE_INFO (parent)->proxy;
1890 case NODE_TYPE_MENU:
1891 menushell = NODE_INFO (parent)->proxy;
1892 if (GTK_IS_MENU_ITEM (menushell))
1893 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1894 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1895 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1899 g_list_free (siblings);
1901 case NODE_TYPE_MENU_PLACEHOLDER:
1902 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1903 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1904 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1905 NODE_INFO (parent)->proxy) + 1;
1908 g_warning ("%s: bad parent node type %d", G_STRLOC,
1909 NODE_INFO (parent)->type);
1915 GtkWidget *prev_child;
1918 sibling = node->prev;
1919 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1920 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1922 prev_child = NODE_INFO (sibling)->proxy;
1924 if (!GTK_IS_WIDGET (prev_child))
1927 menushell = gtk_widget_get_parent (prev_child);
1928 if (!GTK_IS_MENU_SHELL (menushell))
1931 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1935 *menushell_p = menushell;
1943 find_toolbar_position (GNode *node,
1944 GtkWidget **toolbar_p,
1950 g_return_val_if_fail (node != NULL, FALSE);
1951 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1952 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1953 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1954 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1957 /* first sibling -- look at parent */
1958 if (node->prev == NULL)
1962 parent = node->parent;
1963 switch (NODE_INFO (parent)->type)
1965 case NODE_TYPE_TOOLBAR:
1966 toolbar = NODE_INFO (parent)->proxy;
1969 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1970 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1971 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1972 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1973 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1976 g_warning ("%s: bad parent node type %d", G_STRLOC,
1977 NODE_INFO (parent)->type);
1983 GtkWidget *prev_child;
1986 sibling = node->prev;
1987 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1988 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1990 prev_child = NODE_INFO (sibling)->proxy;
1992 if (!GTK_IS_WIDGET (prev_child))
1995 toolbar = gtk_widget_get_parent (prev_child);
1996 if (!GTK_IS_TOOLBAR (toolbar))
1999 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2000 GTK_TOOL_ITEM (prev_child)) + 1;
2004 *toolbar_p = toolbar;
2012 * _gtk_menu_is_empty:
2013 * @menu: a #GtkMenu or %NULL
2015 * Determines whether @menu is empty. A menu is considered empty if it
2016 * the only visible children are tearoff menu items or "filler" menu
2017 * items which were inserted to mark the menu as empty.
2019 * This function is used by #GtkAction.
2021 * Return value: whether @menu is empty.
2024 _gtk_menu_is_empty (GtkWidget *menu)
2026 GList *children, *cur;
2027 gboolean result = TRUE;
2029 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2034 children = gtk_container_get_children (GTK_CONTAINER (menu));
2039 if (GTK_WIDGET_VISIBLE (cur->data))
2041 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2042 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2050 g_list_free (children);
2056 SEPARATOR_MODE_SMART,
2057 SEPARATOR_MODE_VISIBLE,
2058 SEPARATOR_MODE_HIDDEN
2062 update_smart_separators (GtkWidget *proxy)
2064 GtkWidget *parent = NULL;
2066 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2068 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2069 parent = gtk_widget_get_parent (proxy);
2075 GList *children, *cur, *last;
2078 children = gtk_container_get_children (GTK_CONTAINER (parent));
2088 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2092 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2093 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2096 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2097 "gtk-separator-mode"));
2100 case SEPARATOR_MODE_VISIBLE:
2101 gtk_widget_show (GTK_WIDGET (cur->data));
2105 case SEPARATOR_MODE_HIDDEN:
2106 gtk_widget_hide (GTK_WIDGET (cur->data));
2108 case SEPARATOR_MODE_SMART:
2111 gtk_widget_show (GTK_WIDGET (cur->data));
2116 gtk_widget_hide (GTK_WIDGET (cur->data));
2120 else if (GTK_WIDGET_VISIBLE (cur->data))
2123 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2136 gtk_widget_hide (GTK_WIDGET (last->data));
2138 if (GTK_IS_MENU (parent))
2142 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2143 if (GTK_IS_MENU_ITEM (item))
2144 _gtk_action_sync_menu_visible (NULL, item, empty);
2145 if (GTK_IS_WIDGET (filler))
2148 gtk_widget_show (filler);
2150 gtk_widget_hide (filler);
2154 g_list_free (children);
2159 update_node (GtkUIManager *self,
2166 const gchar *action_name;
2167 NodeUIReference *ref;
2169 #ifdef DEBUG_UI_MANAGER
2173 g_return_if_fail (node != NULL);
2174 g_return_if_fail (NODE_INFO (node) != NULL);
2176 info = NODE_INFO (node);
2181 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2183 #ifdef DEBUG_UI_MANAGER
2184 g_print ("update_node name=%s dirty=%d popup %d (",
2185 info->name, info->dirty, in_popup);
2186 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2188 NodeUIReference *ref = tmp->data;
2189 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2196 if (info->uifiles == NULL) {
2197 /* We may need to remove this node.
2198 * This must be done in post order
2200 goto recurse_children;
2203 ref = info->uifiles->data;
2204 action_name = g_quark_to_string (ref->action_quark);
2205 action = get_action_by_name (self, action_name);
2207 info->dirty = FALSE;
2209 /* Check if the node doesn't have an action and must have an action */
2210 if (action == NULL &&
2211 info->type != NODE_TYPE_ROOT &&
2212 info->type != NODE_TYPE_MENUBAR &&
2213 info->type != NODE_TYPE_TOOLBAR &&
2214 info->type != NODE_TYPE_POPUP &&
2215 info->type != NODE_TYPE_SEPARATOR &&
2216 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2217 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2219 g_warning ("%s: missing action %s", info->name, action_name);
2225 gtk_action_set_accel_group (action, self->private_data->accel_group);
2227 /* If the widget already has a proxy and the action hasn't changed, then
2228 * we only have to update the tearoff menu items.
2230 if (info->proxy != NULL && action == info->action)
2232 if (info->type == NODE_TYPE_MENU)
2237 if (GTK_IS_MENU (info->proxy))
2240 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2241 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2242 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2244 if (self->private_data->add_tearoffs && !in_popup)
2245 gtk_widget_show (GTK_WIDGET (siblings->data));
2247 gtk_widget_hide (GTK_WIDGET (siblings->data));
2249 g_list_free (siblings);
2252 goto recurse_children;
2257 case NODE_TYPE_MENUBAR:
2258 if (info->proxy == NULL)
2260 info->proxy = gtk_menu_bar_new ();
2261 g_object_ref_sink (info->proxy);
2262 gtk_widget_set_name (info->proxy, info->name);
2263 gtk_widget_show (info->proxy);
2264 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2267 case NODE_TYPE_POPUP:
2268 if (info->proxy == NULL)
2270 info->proxy = gtk_menu_new ();
2271 g_object_ref_sink (info->proxy);
2273 gtk_widget_set_name (info->proxy, info->name);
2275 case NODE_TYPE_MENU:
2277 GtkWidget *prev_submenu = NULL;
2278 GtkWidget *menu = NULL;
2281 /* remove the proxy if it is of the wrong type ... */
2283 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2285 if (GTK_IS_MENU_ITEM (info->proxy))
2287 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2290 g_object_ref (prev_submenu);
2291 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2295 gtk_action_disconnect_proxy (info->action, info->proxy);
2296 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2298 g_object_unref (info->proxy);
2302 /* create proxy if needed ... */
2303 if (info->proxy == NULL)
2305 /* ... if the action already provides a menu, then use
2306 * that menu instead of creating an empty one
2308 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2309 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2310 GTK_ACTION_GET_CLASS (action)->create_menu)
2312 menu = gtk_action_create_menu (action);
2320 menu = gtk_menu_new ();
2321 gtk_widget_set_name (menu, info->name);
2322 tearoff = gtk_tearoff_menu_item_new ();
2323 gtk_widget_set_no_show_all (tearoff, TRUE);
2324 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2325 filler = gtk_menu_item_new_with_label (_("Empty"));
2326 g_object_set_data (G_OBJECT (filler),
2327 I_("gtk-empty-menu-item"),
2328 GINT_TO_POINTER (TRUE));
2329 gtk_widget_set_sensitive (filler, FALSE);
2330 gtk_widget_set_no_show_all (filler, TRUE);
2331 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2334 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2337 g_object_ref_sink (info->proxy);
2338 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2343 GtkWidget *menushell;
2346 if (find_menu_position (node, &menushell, &pos))
2348 info->proxy = gtk_action_create_menu_item (action);
2349 g_object_ref_sink (info->proxy);
2350 g_signal_connect (info->proxy, "notify::visible",
2351 G_CALLBACK (update_smart_separators), NULL);
2352 gtk_widget_set_name (info->proxy, info->name);
2354 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2355 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2360 gtk_action_connect_proxy (action, info->proxy);
2364 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2366 g_object_unref (prev_submenu);
2369 if (GTK_IS_MENU (info->proxy))
2372 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2374 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2375 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2377 if (self->private_data->add_tearoffs && !in_popup)
2378 gtk_widget_show (GTK_WIDGET (siblings->data));
2380 gtk_widget_hide (GTK_WIDGET (siblings->data));
2382 g_list_free (siblings);
2385 case NODE_TYPE_UNDECIDED:
2386 g_warning ("found undecided node!");
2388 case NODE_TYPE_ROOT:
2390 case NODE_TYPE_TOOLBAR:
2391 if (info->proxy == NULL)
2393 info->proxy = gtk_toolbar_new ();
2394 g_object_ref_sink (info->proxy);
2395 gtk_widget_set_name (info->proxy, info->name);
2396 gtk_widget_show (info->proxy);
2397 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2400 case NODE_TYPE_MENU_PLACEHOLDER:
2401 /* create menu items for placeholders if necessary ... */
2402 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2403 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2407 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2409 g_object_unref (info->proxy);
2414 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2416 g_object_unref (info->extra);
2420 if (info->proxy == NULL)
2422 GtkWidget *menushell;
2425 if (find_menu_position (node, &menushell, &pos))
2427 info->proxy = gtk_separator_menu_item_new ();
2428 g_object_ref_sink (info->proxy);
2429 g_object_set_data (G_OBJECT (info->proxy),
2430 I_("gtk-separator-mode"),
2431 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2432 gtk_widget_set_no_show_all (info->proxy, TRUE);
2433 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2434 NODE_INFO (node)->proxy, pos);
2436 info->extra = gtk_separator_menu_item_new ();
2437 g_object_ref_sink (info->extra);
2438 g_object_set_data (G_OBJECT (info->extra),
2439 I_("gtk-separator-mode"),
2440 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2441 gtk_widget_set_no_show_all (info->extra, TRUE);
2442 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2443 NODE_INFO (node)->extra, pos + 1);
2447 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2448 /* create toolbar items for placeholders if necessary ... */
2449 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2450 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2454 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2456 g_object_unref (info->proxy);
2461 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2463 g_object_unref (info->extra);
2467 if (info->proxy == NULL)
2473 if (find_toolbar_position (node, &toolbar, &pos))
2475 item = gtk_separator_tool_item_new ();
2476 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2477 info->proxy = GTK_WIDGET (item);
2478 g_object_ref_sink (info->proxy);
2479 g_object_set_data (G_OBJECT (info->proxy),
2480 I_("gtk-separator-mode"),
2481 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2482 gtk_widget_set_no_show_all (info->proxy, TRUE);
2484 item = gtk_separator_tool_item_new ();
2485 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2486 info->extra = GTK_WIDGET (item);
2487 g_object_ref_sink (info->extra);
2488 g_object_set_data (G_OBJECT (info->extra),
2489 I_("gtk-separator-mode"),
2490 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2491 gtk_widget_set_no_show_all (info->extra, TRUE);
2495 case NODE_TYPE_MENUITEM:
2496 /* remove the proxy if it is of the wrong type ... */
2498 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2500 g_signal_handlers_disconnect_by_func (info->proxy,
2501 G_CALLBACK (update_smart_separators),
2503 gtk_action_disconnect_proxy (info->action, info->proxy);
2504 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2506 g_object_unref (info->proxy);
2509 /* create proxy if needed ... */
2510 if (info->proxy == NULL)
2512 GtkWidget *menushell;
2515 if (find_menu_position (node, &menushell, &pos))
2517 info->proxy = gtk_action_create_menu_item (action);
2518 g_object_ref_sink (info->proxy);
2519 gtk_widget_set_name (info->proxy, info->name);
2521 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2527 g_signal_handlers_disconnect_by_func (info->proxy,
2528 G_CALLBACK (update_smart_separators),
2530 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2531 gtk_action_connect_proxy (action, info->proxy);
2536 g_signal_connect (info->proxy, "notify::visible",
2537 G_CALLBACK (update_smart_separators), NULL);
2540 /* don't show accels in popups */
2541 GtkWidget *label = GTK_BIN (info->proxy)->child;
2542 g_object_set (label, "accel-closure", NULL, NULL);
2547 case NODE_TYPE_TOOLITEM:
2548 /* remove the proxy if it is of the wrong type ... */
2550 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2552 g_signal_handlers_disconnect_by_func (info->proxy,
2553 G_CALLBACK (update_smart_separators),
2555 gtk_action_disconnect_proxy (info->action, info->proxy);
2556 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2558 g_object_unref (info->proxy);
2561 /* create proxy if needed ... */
2562 if (info->proxy == NULL)
2567 if (find_toolbar_position (node, &toolbar, &pos))
2569 info->proxy = gtk_action_create_tool_item (action);
2570 g_object_ref_sink (info->proxy);
2571 gtk_widget_set_name (info->proxy, info->name);
2573 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2574 GTK_TOOL_ITEM (info->proxy), pos);
2579 g_signal_handlers_disconnect_by_func (info->proxy,
2580 G_CALLBACK (update_smart_separators),
2582 gtk_action_connect_proxy (action, info->proxy);
2587 /* FIXME: we must re-set the tooltip, since tooltips on
2588 * toolitems can't be set before the toolitem is added
2593 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2594 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2597 g_signal_connect (info->proxy, "notify::visible",
2598 G_CALLBACK (update_smart_separators), NULL);
2601 case NODE_TYPE_SEPARATOR:
2602 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2603 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2607 gint separator_mode;
2610 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2612 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2614 g_object_unref (info->proxy);
2618 if (find_toolbar_position (node, &toolbar, &pos))
2620 item = gtk_separator_tool_item_new ();
2621 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2622 info->proxy = GTK_WIDGET (item);
2623 g_object_ref_sink (info->proxy);
2624 gtk_widget_set_no_show_all (info->proxy, TRUE);
2627 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2628 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2629 separator_mode = SEPARATOR_MODE_VISIBLE;
2632 separator_mode = SEPARATOR_MODE_SMART;
2634 g_object_set_data (G_OBJECT (info->proxy),
2635 I_("gtk-separator-mode"),
2636 GINT_TO_POINTER (separator_mode));
2637 gtk_widget_show (info->proxy);
2642 GtkWidget *menushell;
2645 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2647 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2649 g_object_unref (info->proxy);
2653 if (find_menu_position (node, &menushell, &pos))
2655 info->proxy = gtk_separator_menu_item_new ();
2656 g_object_ref_sink (info->proxy);
2657 gtk_widget_set_no_show_all (info->proxy, TRUE);
2658 g_object_set_data (G_OBJECT (info->proxy),
2659 I_("gtk-separator-mode"),
2660 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2661 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2663 gtk_widget_show (info->proxy);
2667 case NODE_TYPE_ACCELERATOR:
2668 gtk_action_connect_accelerator (action);
2673 g_object_ref (action);
2675 g_object_unref (info->action);
2676 info->action = action;
2679 /* process children */
2680 child = node->children;
2686 child = current->next;
2687 update_node (self, current, in_popup);
2692 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2693 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2694 else if (info->type == NODE_TYPE_MENU ||
2695 info->type == NODE_TYPE_TOOLBAR ||
2696 info->type == NODE_TYPE_POPUP)
2697 update_smart_separators (info->proxy);
2700 /* handle cleanup of dead nodes */
2701 if (node->children == NULL && info->uifiles == NULL)
2704 gtk_widget_destroy (info->proxy);
2706 gtk_widget_destroy (info->extra);
2707 if (info->type == NODE_TYPE_ACCELERATOR)
2708 gtk_action_disconnect_accelerator (info->action);
2710 g_node_destroy (node);
2715 do_updates (GtkUIManager *self)
2717 /* this function needs to check through the tree for dirty nodes.
2718 * For such nodes, it needs to do the following:
2720 * 1) check if they are referenced by any loaded UI files anymore.
2721 * In which case, the proxy widget should be destroyed, unless
2722 * there are any subnodes.
2724 * 2) lookup the action for this node again. If it is different to
2725 * the current one (or if no previous action has been looked up),
2726 * the proxy is reconnected to the new action (or a new proxy widget
2727 * is created and added to the parent container).
2729 update_node (self, self->private_data->root_node, FALSE);
2731 self->private_data->update_tag = 0;
2737 do_updates_idle (GtkUIManager *self)
2745 queue_update (GtkUIManager *self)
2747 if (self->private_data->update_tag != 0)
2750 self->private_data->update_tag = gdk_threads_add_idle (
2751 (GSourceFunc)do_updates_idle,
2757 * gtk_ui_manager_ensure_update:
2758 * @self: a #GtkUIManager
2760 * Makes sure that all pending updates to the UI have been completed.
2762 * This may occasionally be necessary, since #GtkUIManager updates the
2763 * UI in an idle function. A typical example where this function is
2764 * useful is to enforce that the menubar and toolbar have been added to
2765 * the main window before showing it:
2768 * gtk_container_add (GTK_CONTAINER (window), vbox);
2769 * g_signal_connect (merge, "add_widget",
2770 * G_CALLBACK (add_widget), vbox);
2771 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2772 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2773 * gtk_ui_manager_ensure_update (merge);
2774 * gtk_widget_show (window);
2776 * </informalexample>
2781 gtk_ui_manager_ensure_update (GtkUIManager *self)
2783 if (self->private_data->update_tag != 0)
2785 g_source_remove (self->private_data->update_tag);
2791 dirty_traverse_func (GNode *node,
2794 NODE_INFO (node)->dirty = TRUE;
2799 dirty_all_nodes (GtkUIManager *self)
2801 g_node_traverse (self->private_data->root_node,
2802 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2803 dirty_traverse_func, NULL);
2804 queue_update (self);
2808 mark_node_dirty (GNode *node)
2812 /* FIXME could optimize this */
2813 for (p = node; p; p = p->parent)
2814 NODE_INFO (p)->dirty = TRUE;
2817 static const gchar *
2818 open_tag_format (NodeType type)
2822 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2823 case NODE_TYPE_ROOT: return "%*s<ui";
2824 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2825 case NODE_TYPE_MENU: return "%*s<menu";
2826 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2827 case NODE_TYPE_MENU_PLACEHOLDER:
2828 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2829 case NODE_TYPE_POPUP: return "%*s<popup";
2830 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2831 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2832 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2833 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2834 default: return NULL;
2838 static const gchar *
2839 close_tag_format (NodeType type)
2843 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2844 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2845 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2846 case NODE_TYPE_MENU: return "%*s</menu>\n";
2847 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2848 case NODE_TYPE_MENU_PLACEHOLDER:
2849 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2850 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2851 default: return NULL;
2856 print_node (GtkUIManager *self,
2863 const gchar *open_fmt;
2864 const gchar *close_fmt;
2868 open_fmt = open_tag_format (mnode->type);
2869 close_fmt = close_tag_format (mnode->type);
2871 g_string_append_printf (buffer, open_fmt, indent_level, "");
2873 if (mnode->type != NODE_TYPE_ROOT)
2876 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2878 if (mnode->action_name)
2879 g_string_append_printf (buffer, " action=\"%s\"",
2880 g_quark_to_string (mnode->action_name));
2883 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2885 for (child = node->children; child != NULL; child = child->next)
2886 print_node (self, child, indent_level + 2, buffer);
2889 g_string_append_printf (buffer, close_fmt, indent_level, "");
2893 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2894 GtkBuilder *builder,
2896 const gchar *tagname,
2897 GMarkupParser *parser,
2903 if (strcmp (tagname, "ui") == 0)
2907 ctx = g_new0 (ParseContext, 1);
2908 ctx->state = STATE_START;
2909 ctx->self = GTK_UI_MANAGER (buildable);
2910 ctx->current = NULL;
2911 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2914 *parser = ui_parser;
2924 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2925 GtkBuilder *builder,
2927 const gchar *tagname,
2930 queue_update (GTK_UI_MANAGER (buildable));
2931 g_object_notify (G_OBJECT (buildable), "ui");
2936 * gtk_ui_manager_get_ui:
2937 * @self: a #GtkUIManager
2939 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2941 * Return value: A newly allocated string containing an XML representation of
2947 gtk_ui_manager_get_ui (GtkUIManager *self)
2951 buffer = g_string_new (NULL);
2953 gtk_ui_manager_ensure_update (self);
2955 print_node (self, self->private_data->root_node, 0, buffer);
2957 return g_string_free (buffer, FALSE);
2962 #undef gtk_ui_manager_add_ui_from_file
2965 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2966 const gchar *filename,
2969 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2972 if (utf8_filename == NULL)
2975 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2977 g_free (utf8_filename);
2984 #define __GTK_UI_MANAGER_C__
2985 #include "gtkaliasdef.c"