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_child (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, G_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_child = gtk_ui_manager_buildable_add_child;
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_child (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 child_hierarchy_changed_cb (GtkWidget *widget,
462 GtkAccelGroup *group;
465 toplevel = gtk_widget_get_toplevel (widget);
466 if (!toplevel || !GTK_IS_WINDOW (toplevel))
469 group = gtk_ui_manager_get_accel_group (uimgr);
470 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
471 if (g_slist_find (groups, group) == NULL)
472 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
474 g_signal_handlers_disconnect_by_func (widget,
475 child_hierarchy_changed_cb,
480 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
487 name = g_strdup_printf ("ui/%s", id);
488 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
491 g_error ("Unknown ui manager child: %s\n", name);
497 g_signal_connect (widget, "hierarchy-changed",
498 G_CALLBACK (child_hierarchy_changed_cb),
499 GTK_UI_MANAGER (buildable));
500 return g_object_ref (widget);
504 gtk_ui_manager_set_property (GObject *object,
509 GtkUIManager *self = GTK_UI_MANAGER (object);
513 case PROP_ADD_TEAROFFS:
514 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 gtk_ui_manager_get_property (GObject *object,
528 GtkUIManager *self = GTK_UI_MANAGER (object);
532 case PROP_ADD_TEAROFFS:
533 g_value_set_boolean (value, self->private_data->add_tearoffs);
536 g_value_take_string (value, gtk_ui_manager_get_ui (self));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 gtk_ui_manager_real_get_widget (GtkUIManager *self,
550 /* ensure that there are no pending updates before we get the
552 gtk_ui_manager_ensure_update (self);
554 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
559 return NODE_INFO (node)->proxy;
563 gtk_ui_manager_real_get_action (GtkUIManager *self,
568 /* ensure that there are no pending updates before we get
570 gtk_ui_manager_ensure_update (self);
572 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
577 return NODE_INFO (node)->action;
582 * gtk_ui_manager_new:
584 * Creates a new ui manager object.
586 * Return value: a new ui manager object.
591 gtk_ui_manager_new (void)
593 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
598 * gtk_ui_manager_get_add_tearoffs:
599 * @self: a #GtkUIManager
601 * Returns whether menus generated by this #GtkUIManager
602 * will have tearoff menu items.
604 * Return value: whether tearoff menu items are added
609 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
611 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
613 return self->private_data->add_tearoffs;
618 * gtk_ui_manager_set_add_tearoffs:
619 * @self: a #GtkUIManager
620 * @add_tearoffs: whether tearoff menu items are added
622 * Sets the "add_tearoffs" property, which controls whether menus
623 * generated by this #GtkUIManager will have tearoff menu items.
625 * Note that this only affects regular menus. Generated popup
626 * menus never have tearoff menu items.
631 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
632 gboolean add_tearoffs)
634 g_return_if_fail (GTK_IS_UI_MANAGER (self));
636 add_tearoffs = add_tearoffs != FALSE;
638 if (add_tearoffs != self->private_data->add_tearoffs)
640 self->private_data->add_tearoffs = add_tearoffs;
642 dirty_all_nodes (self);
644 g_object_notify (G_OBJECT (self), "add-tearoffs");
649 cb_proxy_connect_proxy (GtkActionGroup *group,
654 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
658 cb_proxy_disconnect_proxy (GtkActionGroup *group,
663 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
667 cb_proxy_pre_activate (GtkActionGroup *group,
671 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
675 cb_proxy_post_activate (GtkActionGroup *group,
679 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
683 * gtk_ui_manager_insert_action_group:
684 * @self: a #GtkUIManager object
685 * @action_group: the action group to be inserted
686 * @pos: the position at which the group will be inserted.
688 * Inserts an action group into the list of action groups associated
689 * with @self. Actions in earlier groups hide actions with the same
690 * name in later groups.
695 gtk_ui_manager_insert_action_group (GtkUIManager *self,
696 GtkActionGroup *action_group,
699 #ifdef G_ENABLE_DEBUG
701 const char *group_name;
704 g_return_if_fail (GTK_IS_UI_MANAGER (self));
705 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
706 g_return_if_fail (g_list_find (self->private_data->action_groups,
707 action_group) == NULL);
709 #ifdef G_ENABLE_DEBUG
710 group_name = gtk_action_group_get_name (action_group);
712 for (l = self->private_data->action_groups; l; l = l->next)
714 GtkActionGroup *group = l->data;
716 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
718 g_warning ("Inserting action group '%s' into UI manager which "
719 "already has a group with this name\n", group_name);
723 #endif /* G_ENABLE_DEBUG */
725 g_object_ref (action_group);
726 self->private_data->action_groups =
727 g_list_insert (self->private_data->action_groups, action_group, pos);
728 g_object_connect (action_group,
729 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
730 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
731 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
732 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
735 /* dirty all nodes, as action bindings may change */
736 dirty_all_nodes (self);
738 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
742 * gtk_ui_manager_remove_action_group:
743 * @self: a #GtkUIManager object
744 * @action_group: the action group to be removed
746 * Removes an action group from the list of action groups associated
752 gtk_ui_manager_remove_action_group (GtkUIManager *self,
753 GtkActionGroup *action_group)
755 g_return_if_fail (GTK_IS_UI_MANAGER (self));
756 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
757 g_return_if_fail (g_list_find (self->private_data->action_groups,
758 action_group) != NULL);
760 self->private_data->action_groups =
761 g_list_remove (self->private_data->action_groups, action_group);
763 g_object_disconnect (action_group,
764 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
765 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
766 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
767 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
769 g_object_unref (action_group);
771 /* dirty all nodes, as action bindings may change */
772 dirty_all_nodes (self);
774 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
778 * gtk_ui_manager_get_action_groups:
779 * @self: a #GtkUIManager object
781 * Returns the list of action groups associated with @self.
783 * Return value: a #GList of action groups. The list is owned by GTK+
784 * and should not be modified.
789 gtk_ui_manager_get_action_groups (GtkUIManager *self)
791 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
793 return self->private_data->action_groups;
797 * gtk_ui_manager_get_accel_group:
798 * @self: a #GtkUIManager object
800 * Returns the #GtkAccelGroup associated with @self.
802 * Return value: the #GtkAccelGroup.
807 gtk_ui_manager_get_accel_group (GtkUIManager *self)
809 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
811 return self->private_data->accel_group;
815 * gtk_ui_manager_get_widget:
816 * @self: a #GtkUIManager
819 * Looks up a widget by following a path.
820 * The path consists of the names specified in the XML description of the UI.
821 * separated by '/'. Elements which don't have a name or action attribute in
822 * the XML (e.g. <popup>) can be addressed by their XML element name
823 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
825 * Note that the widget found by following a path that ends in a <menu>
826 * element is the menuitem to which the menu is attached, not the menu itself.
828 * Also note that the widgets constructed by a ui manager are not tied to
829 * the lifecycle of the ui manager. If you add the widgets returned by this
830 * function to some container or explicitly ref them, they will survive the
831 * destruction of the ui manager.
833 * Return value: the widget found by following the path, or %NULL if no widget
839 gtk_ui_manager_get_widget (GtkUIManager *self,
842 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
843 g_return_val_if_fail (path != NULL, NULL);
845 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
849 GtkUIManagerItemType types;
854 collect_toplevels (GNode *node,
857 ToplevelData *data = user_data;
859 if (NODE_INFO (node)->proxy)
861 switch (NODE_INFO (node)->type)
863 case NODE_TYPE_MENUBAR:
864 if (data->types & GTK_UI_MANAGER_MENUBAR)
865 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
867 case NODE_TYPE_TOOLBAR:
868 if (data->types & GTK_UI_MANAGER_TOOLBAR)
869 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
871 case NODE_TYPE_POPUP:
872 if (data->types & GTK_UI_MANAGER_POPUP)
873 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
881 * gtk_ui_manager_get_toplevels:
882 * @self: a #GtkUIManager
883 * @types: specifies the types of toplevel widgets to include. Allowed
884 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
885 * #GTK_UI_MANAGER_POPUP.
887 * Obtains a list of all toplevel widgets of the requested types.
889 * Return value: a newly-allocated of all toplevel widgets of the requested
895 gtk_ui_manager_get_toplevels (GtkUIManager *self,
896 GtkUIManagerItemType types)
900 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
901 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
902 GTK_UI_MANAGER_TOOLBAR |
903 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
909 g_node_children_foreach (self->private_data->root_node,
911 collect_toplevels, &data);
918 * gtk_ui_manager_get_action:
919 * @self: a #GtkUIManager
922 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
923 * for more information about paths.
925 * Return value: the action whose proxy widget is found by following the path,
926 * or %NULL if no widget was found.
931 gtk_ui_manager_get_action (GtkUIManager *self,
934 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
935 g_return_val_if_fail (path != NULL, NULL);
937 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
941 get_child_node (GtkUIManager *self,
944 const gchar *childname,
945 gint childname_length,
956 for (child = parent->children; child != NULL; child = child->next)
958 if (NODE_INFO (child)->name &&
959 strlen (NODE_INFO (child)->name) == childname_length &&
960 !strncmp (NODE_INFO (child)->name, childname, childname_length))
962 /* if undecided about node type, set it */
963 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
964 NODE_INFO (child)->type = node_type;
966 /* warn about type mismatch */
967 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
968 node_type != NODE_TYPE_UNDECIDED &&
969 NODE_INFO (child)->type != node_type)
970 g_warning ("node type doesn't match %d (%s is type %d)",
972 NODE_INFO (child)->name,
973 NODE_INFO (child)->type);
979 if (!child && create)
983 mnode = g_slice_new0 (Node);
984 mnode->type = node_type;
985 mnode->name = g_strndup (childname, childname_length);
990 child = g_node_insert_before (parent, sibling,
993 child = g_node_insert_after (parent, sibling,
999 child = g_node_prepend_data (parent, mnode);
1001 child = g_node_append_data (parent, mnode);
1004 mark_node_dirty (child);
1009 /* handle root node */
1010 if (self->private_data->root_node)
1012 child = self->private_data->root_node;
1013 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1014 g_warning ("root node name '%s' doesn't match '%s'",
1015 childname, NODE_INFO (child)->name);
1016 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1017 g_warning ("base element must be of type ROOT");
1023 mnode = g_slice_new0 (Node);
1024 mnode->type = node_type;
1025 mnode->name = g_strndup (childname, childname_length);
1026 mnode->dirty = TRUE;
1028 child = self->private_data->root_node = g_node_new (mnode);
1036 get_node (GtkUIManager *self,
1041 const gchar *pos, *end;
1042 GNode *parent, *node;
1044 if (strncmp ("/ui", path, 3) == 0)
1047 end = path + strlen (path);
1049 parent = node = NULL;
1055 slash = strchr (pos, '/');
1057 length = slash - pos;
1059 length = strlen (pos);
1061 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1066 pos += length + 1; /* move past the node name and the slash too */
1070 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1071 NODE_INFO (node)->type = node_type;
1077 node_ui_reference_free (gpointer data)
1079 g_slice_free (NodeUIReference, data);
1083 free_node (GNode *node)
1085 Node *info = NODE_INFO (node);
1087 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1088 g_list_free (info->uifiles);
1091 g_object_unref (info->action);
1093 g_object_unref (info->proxy);
1095 g_object_unref (info->extra);
1096 g_free (info->name);
1097 g_slice_free (Node, info);
1103 * gtk_ui_manager_new_merge_id:
1104 * @self: a #GtkUIManager
1106 * Returns an unused merge id, suitable for use with
1107 * gtk_ui_manager_add_ui().
1109 * Return value: an unused merge id.
1114 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1116 self->private_data->last_merge_id++;
1118 return self->private_data->last_merge_id;
1122 node_prepend_ui_reference (GNode *gnode,
1124 GQuark action_quark)
1126 Node *node = NODE_INFO (gnode);
1127 NodeUIReference *reference = NULL;
1129 if (node->uifiles &&
1130 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1131 reference = node->uifiles->data;
1134 reference = g_slice_new (NodeUIReference);
1135 node->uifiles = g_list_prepend (node->uifiles, reference);
1138 reference->merge_id = merge_id;
1139 reference->action_quark = action_quark;
1141 mark_node_dirty (gnode);
1145 node_remove_ui_reference (GNode *gnode,
1148 Node *node = NODE_INFO (gnode);
1151 for (p = node->uifiles; p != NULL; p = p->next)
1153 NodeUIReference *reference = p->data;
1155 if (reference->merge_id == merge_id)
1157 if (p == node->uifiles)
1158 mark_node_dirty (gnode);
1159 node->uifiles = g_list_delete_link (node->uifiles, p);
1160 g_slice_free (NodeUIReference, reference);
1167 /* -------------------- The UI file parser -------------------- */
1181 typedef struct _ParseContext ParseContext;
1182 struct _ParseContext
1185 ParseState prev_state;
1195 start_element_handler (GMarkupParseContext *context,
1196 const gchar *element_name,
1197 const gchar **attribute_names,
1198 const gchar **attribute_values,
1202 ParseContext *ctx = user_data;
1203 GtkUIManager *self = ctx->self;
1206 const gchar *node_name;
1207 const gchar *action;
1208 GQuark action_quark;
1210 gboolean expand = FALSE;
1212 gboolean raise_error = TRUE;
1219 for (i = 0; attribute_names[i] != NULL; i++)
1221 if (!strcmp (attribute_names[i], "name"))
1223 node_name = attribute_values[i];
1225 else if (!strcmp (attribute_names[i], "action"))
1227 action = attribute_values[i];
1228 action_quark = g_quark_from_string (attribute_values[i]);
1230 else if (!strcmp (attribute_names[i], "position"))
1232 top = !strcmp (attribute_values[i], "top");
1234 else if (!strcmp (attribute_names[i], "expand"))
1236 expand = !strcmp (attribute_values[i], "true");
1240 gint line_number, char_number;
1242 g_markup_parse_context_get_position (context,
1243 &line_number, &char_number);
1246 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1247 _("Unknown attribute '%s' on line %d char %d"),
1249 line_number, char_number);
1254 /* Work out a name for this node. Either the name attribute, or
1255 * the action, or the element name */
1256 if (node_name == NULL)
1261 node_name = element_name;
1264 switch (element_name[0])
1267 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1269 ctx->state = STATE_ACCELERATOR;
1270 ctx->current = get_child_node (self, ctx->current, NULL,
1271 node_name, strlen (node_name),
1272 NODE_TYPE_ACCELERATOR,
1274 if (NODE_INFO (ctx->current)->action_name == 0)
1275 NODE_INFO (ctx->current)->action_name = action_quark;
1277 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1279 raise_error = FALSE;
1283 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1285 ctx->state = STATE_ROOT;
1286 ctx->current = self->private_data->root_node;
1287 raise_error = FALSE;
1289 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1293 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1295 ctx->state = STATE_MENU;
1296 ctx->current = get_child_node (self, ctx->current, NULL,
1297 node_name, strlen (node_name),
1300 if (NODE_INFO (ctx->current)->action_name == 0)
1301 NODE_INFO (ctx->current)->action_name = action_quark;
1303 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1304 mark_node_dirty (ctx->current);
1306 raise_error = FALSE;
1308 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1310 ctx->current = get_child_node (self, ctx->current, NULL,
1311 node_name, strlen (node_name),
1314 if (NODE_INFO (ctx->current)->action_name == 0)
1315 NODE_INFO (ctx->current)->action_name = action_quark;
1317 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1319 raise_error = FALSE;
1321 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1323 ctx->state = STATE_MENU;
1325 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1326 node_name, strlen (node_name),
1329 if (NODE_INFO (ctx->current)->action_name == 0)
1330 NODE_INFO (ctx->current)->action_name = action_quark;
1332 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1334 raise_error = FALSE;
1336 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1340 ctx->state = STATE_MENUITEM;
1341 node = get_child_node (self, ctx->current, NULL,
1342 node_name, strlen (node_name),
1345 if (NODE_INFO (node)->action_name == 0)
1346 NODE_INFO (node)->action_name = action_quark;
1348 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1350 raise_error = FALSE;
1354 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1356 ctx->state = STATE_MENU;
1357 ctx->current = get_child_node (self, ctx->current, NULL,
1358 node_name, strlen (node_name),
1361 if (NODE_INFO (ctx->current)->action_name == 0)
1362 NODE_INFO (ctx->current)->action_name = action_quark;
1364 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1366 raise_error = FALSE;
1368 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1369 !strcmp (element_name, "placeholder"))
1371 if (ctx->state == STATE_TOOLBAR)
1372 ctx->current = get_child_node (self, ctx->current, NULL,
1373 node_name, strlen (node_name),
1374 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1377 ctx->current = get_child_node (self, ctx->current, NULL,
1378 node_name, strlen (node_name),
1379 NODE_TYPE_MENU_PLACEHOLDER,
1382 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1384 raise_error = FALSE;
1388 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1389 !strcmp (element_name, "separator"))
1394 if (ctx->state == STATE_TOOLBAR)
1395 ctx->state = STATE_TOOLITEM;
1397 ctx->state = STATE_MENUITEM;
1398 if (!strcmp (node_name, "separator"))
1404 length = strlen (node_name);
1405 node = get_child_node (self, ctx->current, NULL,
1407 NODE_TYPE_SEPARATOR,
1410 NODE_INFO (node)->expand = expand;
1412 if (NODE_INFO (node)->action_name == 0)
1413 NODE_INFO (node)->action_name = action_quark;
1415 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1417 raise_error = FALSE;
1421 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1423 ctx->state = STATE_TOOLBAR;
1424 ctx->current = get_child_node (self, ctx->current, NULL,
1425 node_name, strlen (node_name),
1428 if (NODE_INFO (ctx->current)->action_name == 0)
1429 NODE_INFO (ctx->current)->action_name = action_quark;
1431 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1433 raise_error = FALSE;
1435 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1439 ctx->state = STATE_TOOLITEM;
1440 node = get_child_node (self, ctx->current, NULL,
1441 node_name, strlen (node_name),
1444 if (NODE_INFO (node)->action_name == 0)
1445 NODE_INFO (node)->action_name = action_quark;
1447 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1449 raise_error = FALSE;
1457 gint line_number, char_number;
1459 g_markup_parse_context_get_position (context,
1460 &line_number, &char_number);
1463 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1464 _("Unexpected start tag '%s' on line %d char %d"),
1466 line_number, char_number);
1471 end_element_handler (GMarkupParseContext *context,
1472 const gchar *element_name,
1476 ParseContext *ctx = user_data;
1482 /* no need to GError here, GMarkup already catches this */
1485 ctx->current = NULL;
1486 ctx->state = STATE_END;
1490 case STATE_ACCELERATOR:
1491 ctx->current = ctx->current->parent;
1492 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1493 ctx->state = STATE_ROOT;
1494 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1496 ctx->current = ctx->current->parent;
1497 ctx->state = STATE_TOOLITEM;
1499 /* else, stay in same state */
1501 case STATE_MENUITEM:
1502 ctx->state = STATE_MENU;
1504 case STATE_TOOLITEM:
1505 ctx->state = STATE_TOOLBAR;
1511 cleanup (GMarkupParseContext *context,
1515 ParseContext *ctx = user_data;
1517 ctx->current = NULL;
1518 /* should also walk through the tree and get rid of nodes related to
1519 * this UI file's tag */
1521 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1525 xml_isspace (char c)
1527 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1531 text_handler (GMarkupParseContext *context,
1541 end = text + text_len;
1542 while (p != end && xml_isspace (*p))
1547 gint line_number, char_number;
1549 g_markup_parse_context_get_position (context,
1550 &line_number, &char_number);
1553 G_MARKUP_ERROR_INVALID_CONTENT,
1554 _("Unexpected character data on line %d char %d"),
1555 line_number, char_number);
1560 static const GMarkupParser ui_parser = {
1561 start_element_handler,
1562 end_element_handler,
1569 add_ui_from_string (GtkUIManager *self,
1570 const gchar *buffer,
1572 gboolean needs_root,
1575 ParseContext ctx = { 0 };
1576 GMarkupParseContext *context;
1578 ctx.state = STATE_START;
1581 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1583 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1586 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1589 if (!g_markup_parse_context_parse (context, buffer, length, error))
1593 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1596 if (!g_markup_parse_context_end_parse (context, error))
1599 g_markup_parse_context_free (context);
1601 queue_update (self);
1603 g_object_notify (G_OBJECT (self), "ui");
1605 return ctx.merge_id;
1609 g_markup_parse_context_free (context);
1615 * gtk_ui_manager_add_ui_from_string:
1616 * @self: a #GtkUIManager object
1617 * @buffer: the string to parse
1618 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1619 * @error: return location for an error
1621 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1622 * merges it with the current contents of @self. An enclosing <ui>
1623 * element is added if it is missing.
1625 * Return value: The merge id for the merged UI. The merge id can be used
1626 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1627 * the return value is 0.
1632 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1633 const gchar *buffer,
1637 gboolean needs_root = TRUE;
1641 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1642 g_return_val_if_fail (buffer != NULL, 0);
1645 length = strlen (buffer);
1648 end = buffer + length;
1649 while (p != end && xml_isspace (*p))
1652 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1655 return add_ui_from_string (self, buffer, length, needs_root, error);
1659 * gtk_ui_manager_add_ui_from_file:
1660 * @self: a #GtkUIManager object
1661 * @filename: the name of the file to parse
1662 * @error: return location for an error
1664 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1665 * merges it with the current contents of @self.
1667 * Return value: The merge id for the merged UI. The merge id can be used
1668 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1669 * the return value is 0.
1674 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1675 const gchar *filename,
1682 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1684 if (!g_file_get_contents (filename, &buffer, &length, error))
1687 res = add_ui_from_string (self, buffer, length, FALSE, error);
1694 * gtk_ui_manager_add_ui:
1695 * @self: a #GtkUIManager
1696 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1698 * @name: the name for the added UI element
1699 * @action: the name of the action to be proxied, or %NULL to add a separator
1700 * @type: the type of UI element to add.
1701 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1702 * is added after its siblings.
1704 * Adds a UI element to the current contents of @self.
1706 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1707 * separator if such an element can be inserted at the place determined by
1708 * @path. Otherwise @type must indicate an element that can be inserted at
1709 * the place determined by @path.
1711 * If @path points to a menuitem or toolitem, the new element will be inserted
1712 * before or after this item, depending on @top.
1717 gtk_ui_manager_add_ui (GtkUIManager *self,
1721 const gchar *action,
1722 GtkUIManagerItemType type,
1729 GQuark action_quark = 0;
1731 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1732 g_return_if_fail (merge_id > 0);
1733 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1735 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1741 node_type = NODE_TYPE_UNDECIDED;
1744 switch (NODE_INFO (node)->type)
1746 case NODE_TYPE_SEPARATOR:
1747 case NODE_TYPE_MENUITEM:
1748 case NODE_TYPE_TOOLITEM:
1750 node = node->parent;
1752 case NODE_TYPE_MENUBAR:
1753 case NODE_TYPE_MENU:
1754 case NODE_TYPE_POPUP:
1755 case NODE_TYPE_MENU_PLACEHOLDER:
1758 case GTK_UI_MANAGER_AUTO:
1760 node_type = NODE_TYPE_MENUITEM;
1762 node_type = NODE_TYPE_SEPARATOR;
1764 case GTK_UI_MANAGER_MENU:
1765 node_type = NODE_TYPE_MENU;
1767 case GTK_UI_MANAGER_MENUITEM:
1768 node_type = NODE_TYPE_MENUITEM;
1770 case GTK_UI_MANAGER_SEPARATOR:
1771 node_type = NODE_TYPE_SEPARATOR;
1773 case GTK_UI_MANAGER_PLACEHOLDER:
1774 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1780 case NODE_TYPE_TOOLBAR:
1781 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1784 case GTK_UI_MANAGER_AUTO:
1786 node_type = NODE_TYPE_TOOLITEM;
1788 node_type = NODE_TYPE_SEPARATOR;
1790 case GTK_UI_MANAGER_TOOLITEM:
1791 node_type = NODE_TYPE_TOOLITEM;
1793 case GTK_UI_MANAGER_SEPARATOR:
1794 node_type = NODE_TYPE_SEPARATOR;
1796 case GTK_UI_MANAGER_PLACEHOLDER:
1797 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1803 case NODE_TYPE_ROOT:
1806 case GTK_UI_MANAGER_MENUBAR:
1807 node_type = NODE_TYPE_MENUBAR;
1809 case GTK_UI_MANAGER_TOOLBAR:
1810 node_type = NODE_TYPE_TOOLBAR;
1812 case GTK_UI_MANAGER_POPUP:
1813 node_type = NODE_TYPE_POPUP;
1815 case GTK_UI_MANAGER_ACCELERATOR:
1816 node_type = NODE_TYPE_ACCELERATOR;
1826 if (node_type == NODE_TYPE_UNDECIDED)
1828 g_warning ("item type %d not suitable for adding at '%s'",
1833 child = get_child_node (self, node, sibling,
1834 name, name ? strlen (name) : 0,
1835 node_type, TRUE, top);
1838 action_quark = g_quark_from_string (action);
1840 node_prepend_ui_reference (child, merge_id, action_quark);
1842 if (NODE_INFO (child)->action_name == 0)
1843 NODE_INFO (child)->action_name = action_quark;
1845 queue_update (self);
1847 g_object_notify (G_OBJECT (self), "ui");
1851 remove_ui (GNode *node,
1854 guint merge_id = GPOINTER_TO_UINT (user_data);
1856 node_remove_ui_reference (node, merge_id);
1858 return FALSE; /* continue */
1862 * gtk_ui_manager_remove_ui:
1863 * @self: a #GtkUIManager object
1864 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1866 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1871 gtk_ui_manager_remove_ui (GtkUIManager *self,
1874 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1876 g_node_traverse (self->private_data->root_node,
1877 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1878 remove_ui, GUINT_TO_POINTER (merge_id));
1880 queue_update (self);
1882 g_object_notify (G_OBJECT (self), "ui");
1885 /* -------------------- Updates -------------------- */
1889 get_action_by_name (GtkUIManager *merge,
1890 const gchar *action_name)
1898 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1900 GtkActionGroup *action_group = tmp->data;
1903 action = gtk_action_group_get_action (action_group, action_name);
1913 find_menu_position (GNode *node,
1914 GtkWidget **menushell_p,
1917 GtkWidget *menushell;
1920 g_return_val_if_fail (node != NULL, FALSE);
1921 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1922 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1923 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1924 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1925 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1928 /* first sibling -- look at parent */
1929 if (node->prev == NULL)
1934 parent = node->parent;
1935 switch (NODE_INFO (parent)->type)
1937 case NODE_TYPE_MENUBAR:
1938 case NODE_TYPE_POPUP:
1939 menushell = NODE_INFO (parent)->proxy;
1942 case NODE_TYPE_MENU:
1943 menushell = NODE_INFO (parent)->proxy;
1944 if (GTK_IS_MENU_ITEM (menushell))
1945 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1946 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1947 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1951 g_list_free (siblings);
1953 case NODE_TYPE_MENU_PLACEHOLDER:
1954 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1955 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1956 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1957 NODE_INFO (parent)->proxy) + 1;
1960 g_warning ("%s: bad parent node type %d", G_STRLOC,
1961 NODE_INFO (parent)->type);
1967 GtkWidget *prev_child;
1970 sibling = node->prev;
1971 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1972 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1974 prev_child = NODE_INFO (sibling)->proxy;
1976 if (!GTK_IS_WIDGET (prev_child))
1979 menushell = gtk_widget_get_parent (prev_child);
1980 if (!GTK_IS_MENU_SHELL (menushell))
1983 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1987 *menushell_p = menushell;
1995 find_toolbar_position (GNode *node,
1996 GtkWidget **toolbar_p,
2002 g_return_val_if_fail (node != NULL, FALSE);
2003 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2004 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2005 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2006 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2009 /* first sibling -- look at parent */
2010 if (node->prev == NULL)
2014 parent = node->parent;
2015 switch (NODE_INFO (parent)->type)
2017 case NODE_TYPE_TOOLBAR:
2018 toolbar = NODE_INFO (parent)->proxy;
2021 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2022 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2023 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2024 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2025 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2028 g_warning ("%s: bad parent node type %d", G_STRLOC,
2029 NODE_INFO (parent)->type);
2035 GtkWidget *prev_child;
2038 sibling = node->prev;
2039 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2040 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2042 prev_child = NODE_INFO (sibling)->proxy;
2044 if (!GTK_IS_WIDGET (prev_child))
2047 toolbar = gtk_widget_get_parent (prev_child);
2048 if (!GTK_IS_TOOLBAR (toolbar))
2051 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2052 GTK_TOOL_ITEM (prev_child)) + 1;
2056 *toolbar_p = toolbar;
2064 * _gtk_menu_is_empty:
2065 * @menu: a #GtkMenu or %NULL
2067 * Determines whether @menu is empty. A menu is considered empty if it
2068 * the only visible children are tearoff menu items or "filler" menu
2069 * items which were inserted to mark the menu as empty.
2071 * This function is used by #GtkAction.
2073 * Return value: whether @menu is empty.
2076 _gtk_menu_is_empty (GtkWidget *menu)
2078 GList *children, *cur;
2079 gboolean result = TRUE;
2081 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2086 children = gtk_container_get_children (GTK_CONTAINER (menu));
2091 if (GTK_WIDGET_VISIBLE (cur->data))
2093 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2094 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2102 g_list_free (children);
2108 SEPARATOR_MODE_SMART,
2109 SEPARATOR_MODE_VISIBLE,
2110 SEPARATOR_MODE_HIDDEN
2114 update_smart_separators (GtkWidget *proxy)
2116 GtkWidget *parent = NULL;
2118 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2120 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2121 parent = gtk_widget_get_parent (proxy);
2127 GList *children, *cur, *last;
2130 children = gtk_container_get_children (GTK_CONTAINER (parent));
2140 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2144 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2145 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2148 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2149 "gtk-separator-mode"));
2152 case SEPARATOR_MODE_VISIBLE:
2153 gtk_widget_show (GTK_WIDGET (cur->data));
2157 case SEPARATOR_MODE_HIDDEN:
2158 gtk_widget_hide (GTK_WIDGET (cur->data));
2160 case SEPARATOR_MODE_SMART:
2163 gtk_widget_show (GTK_WIDGET (cur->data));
2168 gtk_widget_hide (GTK_WIDGET (cur->data));
2172 else if (GTK_WIDGET_VISIBLE (cur->data))
2175 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2188 gtk_widget_hide (GTK_WIDGET (last->data));
2190 if (GTK_IS_MENU (parent))
2194 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2195 if (GTK_IS_MENU_ITEM (item))
2196 _gtk_action_sync_menu_visible (NULL, item, empty);
2197 if (GTK_IS_WIDGET (filler))
2200 gtk_widget_show (filler);
2202 gtk_widget_hide (filler);
2206 g_list_free (children);
2211 update_node (GtkUIManager *self,
2218 const gchar *action_name;
2219 NodeUIReference *ref;
2221 #ifdef DEBUG_UI_MANAGER
2225 g_return_if_fail (node != NULL);
2226 g_return_if_fail (NODE_INFO (node) != NULL);
2228 info = NODE_INFO (node);
2233 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2235 #ifdef DEBUG_UI_MANAGER
2236 g_print ("update_node name=%s dirty=%d popup %d (",
2237 info->name, info->dirty, in_popup);
2238 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2240 NodeUIReference *ref = tmp->data;
2241 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2248 if (info->uifiles == NULL) {
2249 /* We may need to remove this node.
2250 * This must be done in post order
2252 goto recurse_children;
2255 ref = info->uifiles->data;
2256 action_name = g_quark_to_string (ref->action_quark);
2257 action = get_action_by_name (self, action_name);
2259 info->dirty = FALSE;
2261 /* Check if the node doesn't have an action and must have an action */
2262 if (action == NULL &&
2263 info->type != NODE_TYPE_ROOT &&
2264 info->type != NODE_TYPE_MENUBAR &&
2265 info->type != NODE_TYPE_TOOLBAR &&
2266 info->type != NODE_TYPE_POPUP &&
2267 info->type != NODE_TYPE_SEPARATOR &&
2268 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2269 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2271 g_warning ("%s: missing action %s", info->name, action_name);
2277 gtk_action_set_accel_group (action, self->private_data->accel_group);
2279 /* If the widget already has a proxy and the action hasn't changed, then
2280 * we only have to update the tearoff menu items.
2282 if (info->proxy != NULL && action == info->action)
2284 if (info->type == NODE_TYPE_MENU)
2289 if (GTK_IS_MENU (info->proxy))
2292 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2293 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2294 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2296 if (self->private_data->add_tearoffs && !in_popup)
2297 gtk_widget_show (GTK_WIDGET (siblings->data));
2299 gtk_widget_hide (GTK_WIDGET (siblings->data));
2301 g_list_free (siblings);
2304 goto recurse_children;
2309 case NODE_TYPE_MENUBAR:
2310 if (info->proxy == NULL)
2312 info->proxy = gtk_menu_bar_new ();
2313 g_object_ref_sink (info->proxy);
2314 gtk_widget_set_name (info->proxy, info->name);
2315 gtk_widget_show (info->proxy);
2316 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2319 case NODE_TYPE_POPUP:
2320 if (info->proxy == NULL)
2322 info->proxy = gtk_menu_new ();
2323 g_object_ref_sink (info->proxy);
2325 gtk_widget_set_name (info->proxy, info->name);
2327 case NODE_TYPE_MENU:
2329 GtkWidget *prev_submenu = NULL;
2330 GtkWidget *menu = NULL;
2333 /* remove the proxy if it is of the wrong type ... */
2335 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2337 if (GTK_IS_MENU_ITEM (info->proxy))
2339 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2342 g_object_ref (prev_submenu);
2343 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2347 gtk_action_disconnect_proxy (info->action, info->proxy);
2348 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2350 g_object_unref (info->proxy);
2354 /* create proxy if needed ... */
2355 if (info->proxy == NULL)
2357 /* ... if the action already provides a menu, then use
2358 * that menu instead of creating an empty one
2360 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2361 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2362 GTK_ACTION_GET_CLASS (action)->create_menu)
2364 menu = gtk_action_create_menu (action);
2372 menu = gtk_menu_new ();
2373 gtk_widget_set_name (menu, info->name);
2374 tearoff = gtk_tearoff_menu_item_new ();
2375 gtk_widget_set_no_show_all (tearoff, TRUE);
2376 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2377 filler = gtk_menu_item_new_with_label (_("Empty"));
2378 g_object_set_data (G_OBJECT (filler),
2379 I_("gtk-empty-menu-item"),
2380 GINT_TO_POINTER (TRUE));
2381 gtk_widget_set_sensitive (filler, FALSE);
2382 gtk_widget_set_no_show_all (filler, TRUE);
2383 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2386 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2389 g_object_ref_sink (info->proxy);
2390 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2395 GtkWidget *menushell;
2398 if (find_menu_position (node, &menushell, &pos))
2400 info->proxy = gtk_action_create_menu_item (action);
2401 g_object_ref_sink (info->proxy);
2402 g_signal_connect (info->proxy, "notify::visible",
2403 G_CALLBACK (update_smart_separators), NULL);
2404 gtk_widget_set_name (info->proxy, info->name);
2406 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2407 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2412 gtk_action_connect_proxy (action, info->proxy);
2416 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2418 g_object_unref (prev_submenu);
2421 if (GTK_IS_MENU (info->proxy))
2424 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2426 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2427 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2429 if (self->private_data->add_tearoffs && !in_popup)
2430 gtk_widget_show (GTK_WIDGET (siblings->data));
2432 gtk_widget_hide (GTK_WIDGET (siblings->data));
2434 g_list_free (siblings);
2437 case NODE_TYPE_UNDECIDED:
2438 g_warning ("found undecided node!");
2440 case NODE_TYPE_ROOT:
2442 case NODE_TYPE_TOOLBAR:
2443 if (info->proxy == NULL)
2445 info->proxy = gtk_toolbar_new ();
2446 g_object_ref_sink (info->proxy);
2447 gtk_widget_set_name (info->proxy, info->name);
2448 gtk_widget_show (info->proxy);
2449 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2452 case NODE_TYPE_MENU_PLACEHOLDER:
2453 /* create menu items for placeholders if necessary ... */
2454 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2455 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2459 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2461 g_object_unref (info->proxy);
2466 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2468 g_object_unref (info->extra);
2472 if (info->proxy == NULL)
2474 GtkWidget *menushell;
2477 if (find_menu_position (node, &menushell, &pos))
2479 info->proxy = gtk_separator_menu_item_new ();
2480 g_object_ref_sink (info->proxy);
2481 g_object_set_data (G_OBJECT (info->proxy),
2482 I_("gtk-separator-mode"),
2483 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2484 gtk_widget_set_no_show_all (info->proxy, TRUE);
2485 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2486 NODE_INFO (node)->proxy, pos);
2488 info->extra = gtk_separator_menu_item_new ();
2489 g_object_ref_sink (info->extra);
2490 g_object_set_data (G_OBJECT (info->extra),
2491 I_("gtk-separator-mode"),
2492 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2493 gtk_widget_set_no_show_all (info->extra, TRUE);
2494 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2495 NODE_INFO (node)->extra, pos + 1);
2499 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2500 /* create toolbar items for placeholders if necessary ... */
2501 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2502 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2506 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2508 g_object_unref (info->proxy);
2513 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2515 g_object_unref (info->extra);
2519 if (info->proxy == NULL)
2525 if (find_toolbar_position (node, &toolbar, &pos))
2527 item = gtk_separator_tool_item_new ();
2528 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2529 info->proxy = GTK_WIDGET (item);
2530 g_object_ref_sink (info->proxy);
2531 g_object_set_data (G_OBJECT (info->proxy),
2532 I_("gtk-separator-mode"),
2533 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2534 gtk_widget_set_no_show_all (info->proxy, TRUE);
2536 item = gtk_separator_tool_item_new ();
2537 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2538 info->extra = GTK_WIDGET (item);
2539 g_object_ref_sink (info->extra);
2540 g_object_set_data (G_OBJECT (info->extra),
2541 I_("gtk-separator-mode"),
2542 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2543 gtk_widget_set_no_show_all (info->extra, TRUE);
2547 case NODE_TYPE_MENUITEM:
2548 /* remove the proxy if it is of the wrong type ... */
2550 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_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)
2564 GtkWidget *menushell;
2567 if (find_menu_position (node, &menushell, &pos))
2569 info->proxy = gtk_action_create_menu_item (action);
2570 g_object_ref_sink (info->proxy);
2571 gtk_widget_set_name (info->proxy, info->name);
2573 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2579 g_signal_handlers_disconnect_by_func (info->proxy,
2580 G_CALLBACK (update_smart_separators),
2582 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2583 gtk_action_connect_proxy (action, info->proxy);
2588 g_signal_connect (info->proxy, "notify::visible",
2589 G_CALLBACK (update_smart_separators), NULL);
2592 /* don't show accels in popups */
2593 GtkWidget *label = GTK_BIN (info->proxy)->child;
2594 g_object_set (label, "accel-closure", NULL, NULL);
2599 case NODE_TYPE_TOOLITEM:
2600 /* remove the proxy if it is of the wrong type ... */
2602 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2604 g_signal_handlers_disconnect_by_func (info->proxy,
2605 G_CALLBACK (update_smart_separators),
2607 gtk_action_disconnect_proxy (info->action, info->proxy);
2608 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2610 g_object_unref (info->proxy);
2613 /* create proxy if needed ... */
2614 if (info->proxy == NULL)
2619 if (find_toolbar_position (node, &toolbar, &pos))
2621 info->proxy = gtk_action_create_tool_item (action);
2622 g_object_ref_sink (info->proxy);
2623 gtk_widget_set_name (info->proxy, info->name);
2625 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2626 GTK_TOOL_ITEM (info->proxy), pos);
2631 g_signal_handlers_disconnect_by_func (info->proxy,
2632 G_CALLBACK (update_smart_separators),
2634 gtk_action_connect_proxy (action, info->proxy);
2639 g_signal_connect (info->proxy, "notify::visible",
2640 G_CALLBACK (update_smart_separators), NULL);
2643 case NODE_TYPE_SEPARATOR:
2644 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2645 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2649 gint separator_mode;
2652 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2654 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2656 g_object_unref (info->proxy);
2660 if (find_toolbar_position (node, &toolbar, &pos))
2662 item = gtk_separator_tool_item_new ();
2663 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2664 info->proxy = GTK_WIDGET (item);
2665 g_object_ref_sink (info->proxy);
2666 gtk_widget_set_no_show_all (info->proxy, TRUE);
2669 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2670 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2671 separator_mode = SEPARATOR_MODE_VISIBLE;
2674 separator_mode = SEPARATOR_MODE_SMART;
2676 g_object_set_data (G_OBJECT (info->proxy),
2677 I_("gtk-separator-mode"),
2678 GINT_TO_POINTER (separator_mode));
2679 gtk_widget_show (info->proxy);
2684 GtkWidget *menushell;
2687 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2689 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2691 g_object_unref (info->proxy);
2695 if (find_menu_position (node, &menushell, &pos))
2697 info->proxy = gtk_separator_menu_item_new ();
2698 g_object_ref_sink (info->proxy);
2699 gtk_widget_set_no_show_all (info->proxy, TRUE);
2700 g_object_set_data (G_OBJECT (info->proxy),
2701 I_("gtk-separator-mode"),
2702 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2703 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2705 gtk_widget_show (info->proxy);
2709 case NODE_TYPE_ACCELERATOR:
2710 gtk_action_connect_accelerator (action);
2715 g_object_ref (action);
2717 g_object_unref (info->action);
2718 info->action = action;
2721 /* process children */
2722 child = node->children;
2728 child = current->next;
2729 update_node (self, current, in_popup);
2734 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2735 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2736 else if (info->type == NODE_TYPE_MENU ||
2737 info->type == NODE_TYPE_TOOLBAR ||
2738 info->type == NODE_TYPE_POPUP)
2739 update_smart_separators (info->proxy);
2742 /* handle cleanup of dead nodes */
2743 if (node->children == NULL && info->uifiles == NULL)
2746 gtk_widget_destroy (info->proxy);
2748 gtk_widget_destroy (info->extra);
2749 if (info->type == NODE_TYPE_ACCELERATOR)
2750 gtk_action_disconnect_accelerator (info->action);
2752 g_node_destroy (node);
2757 do_updates (GtkUIManager *self)
2759 /* this function needs to check through the tree for dirty nodes.
2760 * For such nodes, it needs to do the following:
2762 * 1) check if they are referenced by any loaded UI files anymore.
2763 * In which case, the proxy widget should be destroyed, unless
2764 * there are any subnodes.
2766 * 2) lookup the action for this node again. If it is different to
2767 * the current one (or if no previous action has been looked up),
2768 * the proxy is reconnected to the new action (or a new proxy widget
2769 * is created and added to the parent container).
2771 update_node (self, self->private_data->root_node, FALSE);
2773 self->private_data->update_tag = 0;
2779 do_updates_idle (GtkUIManager *self)
2787 queue_update (GtkUIManager *self)
2789 if (self->private_data->update_tag != 0)
2792 self->private_data->update_tag = gdk_threads_add_idle (
2793 (GSourceFunc)do_updates_idle,
2799 * gtk_ui_manager_ensure_update:
2800 * @self: a #GtkUIManager
2802 * Makes sure that all pending updates to the UI have been completed.
2804 * This may occasionally be necessary, since #GtkUIManager updates the
2805 * UI in an idle function. A typical example where this function is
2806 * useful is to enforce that the menubar and toolbar have been added to
2807 * the main window before showing it:
2809 * gtk_container_add (GTK_CONTAINER (window), vbox);
2810 * g_signal_connect (merge, "add_widget",
2811 * G_CALLBACK (add_widget), vbox);
2812 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2813 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2814 * gtk_ui_manager_ensure_update (merge);
2815 * gtk_widget_show (window);
2821 gtk_ui_manager_ensure_update (GtkUIManager *self)
2823 if (self->private_data->update_tag != 0)
2825 g_source_remove (self->private_data->update_tag);
2831 dirty_traverse_func (GNode *node,
2834 NODE_INFO (node)->dirty = TRUE;
2839 dirty_all_nodes (GtkUIManager *self)
2841 g_node_traverse (self->private_data->root_node,
2842 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2843 dirty_traverse_func, NULL);
2844 queue_update (self);
2848 mark_node_dirty (GNode *node)
2852 /* FIXME could optimize this */
2853 for (p = node; p; p = p->parent)
2854 NODE_INFO (p)->dirty = TRUE;
2857 static const gchar *
2858 open_tag_format (NodeType type)
2862 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2863 case NODE_TYPE_ROOT: return "%*s<ui";
2864 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2865 case NODE_TYPE_MENU: return "%*s<menu";
2866 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2867 case NODE_TYPE_MENU_PLACEHOLDER:
2868 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2869 case NODE_TYPE_POPUP: return "%*s<popup";
2870 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2871 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2872 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2873 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2874 default: return NULL;
2878 static const gchar *
2879 close_tag_format (NodeType type)
2883 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2884 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2885 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2886 case NODE_TYPE_MENU: return "%*s</menu>\n";
2887 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2888 case NODE_TYPE_MENU_PLACEHOLDER:
2889 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2890 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2891 default: return NULL;
2896 print_node (GtkUIManager *self,
2903 const gchar *open_fmt;
2904 const gchar *close_fmt;
2908 open_fmt = open_tag_format (mnode->type);
2909 close_fmt = close_tag_format (mnode->type);
2911 g_string_append_printf (buffer, open_fmt, indent_level, "");
2913 if (mnode->type != NODE_TYPE_ROOT)
2916 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2918 if (mnode->action_name)
2919 g_string_append_printf (buffer, " action=\"%s\"",
2920 g_quark_to_string (mnode->action_name));
2923 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2925 for (child = node->children; child != NULL; child = child->next)
2926 print_node (self, child, indent_level + 2, buffer);
2929 g_string_append_printf (buffer, close_fmt, indent_level, "");
2933 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2934 GtkBuilder *builder,
2936 const gchar *tagname,
2937 GMarkupParser *parser,
2943 if (strcmp (tagname, "ui") == 0)
2947 ctx = g_new0 (ParseContext, 1);
2948 ctx->state = STATE_START;
2949 ctx->self = GTK_UI_MANAGER (buildable);
2950 ctx->current = NULL;
2951 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2954 *parser = ui_parser;
2964 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2965 GtkBuilder *builder,
2967 const gchar *tagname,
2970 queue_update (GTK_UI_MANAGER (buildable));
2971 g_object_notify (G_OBJECT (buildable), "ui");
2976 * gtk_ui_manager_get_ui:
2977 * @self: a #GtkUIManager
2979 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2981 * Return value: A newly allocated string containing an XML representation of
2987 gtk_ui_manager_get_ui (GtkUIManager *self)
2991 buffer = g_string_new (NULL);
2993 gtk_ui_manager_ensure_update (self);
2995 print_node (self, self->private_data->root_node, 0, buffer);
2997 return g_string_free (buffer, FALSE);
3002 #undef gtk_ui_manager_add_ui_from_file
3005 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
3006 const gchar *filename,
3009 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
3012 if (utf8_filename == NULL)
3015 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
3017 g_free (utf8_filename);
3024 #define __GTK_UI_MANAGER_C__
3025 #include "gtkaliasdef.c"