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 "gtkwindow.h"
47 #include "gtkprivate.h"
50 #undef DEBUG_UI_MANAGER
59 NODE_TYPE_MENU_PLACEHOLDER,
60 NODE_TYPE_TOOLBAR_PLACEHOLDER,
68 typedef struct _Node Node;
78 GtkWidget *extra; /* second separator for placeholders */
83 guint expand : 1; /* used for separators */
86 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
88 struct _GtkUIManagerPrivate
90 GtkAccelGroup *accel_group;
99 gboolean add_tearoffs;
102 #define NODE_INFO(node) ((Node *)node->data)
104 typedef struct _NodeUIReference NodeUIReference;
106 struct _NodeUIReference
112 static void gtk_ui_manager_finalize (GObject *object);
113 static void gtk_ui_manager_set_property (GObject *object,
117 static void gtk_ui_manager_get_property (GObject *object,
121 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
123 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
125 static void queue_update (GtkUIManager *self);
126 static void dirty_all_nodes (GtkUIManager *self);
127 static void mark_node_dirty (GNode *node);
128 static GNode * get_child_node (GtkUIManager *self,
131 const gchar *childname,
132 gint childname_length,
136 static GNode * get_node (GtkUIManager *self,
140 static gboolean free_node (GNode *node);
141 static void node_prepend_ui_reference (GNode *node,
143 GQuark action_quark);
144 static void node_remove_ui_reference (GNode *node,
148 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
149 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
153 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
156 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
159 const gchar *tagname,
160 GMarkupParser *parser,
162 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
165 const gchar *tagname,
188 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
190 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
191 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
192 gtk_ui_manager_buildable_init))
195 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
197 GObjectClass *gobject_class;
199 gobject_class = G_OBJECT_CLASS (klass);
201 gobject_class->finalize = gtk_ui_manager_finalize;
202 gobject_class->set_property = gtk_ui_manager_set_property;
203 gobject_class->get_property = gtk_ui_manager_get_property;
204 klass->get_widget = gtk_ui_manager_real_get_widget;
205 klass->get_action = gtk_ui_manager_real_get_action;
208 * GtkUIManager:add-tearoffs:
210 * The "add-tearoffs" property controls whether generated menus
211 * have tearoff menu items.
213 * Note that this only affects regular menus. Generated popup
214 * menus never have tearoff menu items.
218 g_object_class_install_property (gobject_class,
220 g_param_spec_boolean ("add-tearoffs",
221 P_("Add tearoffs to menus"),
222 P_("Whether tearoff menu items should be added to menus"),
224 GTK_PARAM_READWRITE));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("ui",
229 P_("Merged UI definition"),
230 P_("An XML string describing the merged UI"),
232 GTK_PARAM_READABLE));
236 * GtkUIManager::add-widget:
237 * @merge: a #GtkUIManager
238 * @widget: the added widget
240 * The add_widget signal is emitted for each generated menubar and toolbar.
241 * It is not emitted for generated popup menus, which can be obtained by
242 * gtk_ui_manager_get_widget().
246 ui_manager_signals[ADD_WIDGET] =
247 g_signal_new (I_("add_widget"),
248 G_OBJECT_CLASS_TYPE (klass),
249 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
250 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
252 g_cclosure_marshal_VOID__OBJECT,
257 * GtkUIManager::actions-changed:
258 * @merge: a #GtkUIManager
260 * The "actions-changed" signal is emitted whenever the set of actions
265 ui_manager_signals[ACTIONS_CHANGED] =
266 g_signal_new (I_("actions_changed"),
267 G_OBJECT_CLASS_TYPE (klass),
268 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
269 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
271 g_cclosure_marshal_VOID__VOID,
275 * GtkUIManager::connect-proxy:
276 * @uimanager: the ui manager
277 * @action: the action
280 * The connect_proxy signal is emitted after connecting a proxy to
281 * an action in the group.
283 * This is intended for simple customizations for which a custom action
284 * class would be too clumsy, e.g. showing tooltips for menuitems in the
289 ui_manager_signals[CONNECT_PROXY] =
290 g_signal_new (I_("connect_proxy"),
291 G_OBJECT_CLASS_TYPE (klass),
292 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
293 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
295 _gtk_marshal_VOID__OBJECT_OBJECT,
301 * GtkUIManager::disconnect-proxy:
302 * @uimanager: the ui manager
303 * @action: the action
306 * The disconnect_proxy signal is emitted after disconnecting a proxy
307 * from an action in the group.
311 ui_manager_signals[DISCONNECT_PROXY] =
312 g_signal_new (I_("disconnect_proxy"),
313 G_OBJECT_CLASS_TYPE (klass),
314 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
315 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
317 _gtk_marshal_VOID__OBJECT_OBJECT,
323 * GtkUIManager::pre-activate:
324 * @uimanager: the ui manager
325 * @action: the action
327 * The pre_activate signal is emitted just before the @action
330 * This is intended for applications to get notification
331 * just before any action is activated.
335 ui_manager_signals[PRE_ACTIVATE] =
336 g_signal_new (I_("pre_activate"),
337 G_OBJECT_CLASS_TYPE (klass),
338 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
339 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
341 _gtk_marshal_VOID__OBJECT,
346 * GtkUIManager::post-activate:
347 * @uimanager: the ui manager
348 * @action: the action
350 * The post_activate signal is emitted just after the @action
353 * This is intended for applications to get notification
354 * just after any action is activated.
358 ui_manager_signals[POST_ACTIVATE] =
359 g_signal_new (I_("post_activate"),
360 G_OBJECT_CLASS_TYPE (klass),
361 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
362 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
364 _gtk_marshal_VOID__OBJECT,
368 klass->add_widget = NULL;
369 klass->actions_changed = NULL;
370 klass->connect_proxy = NULL;
371 klass->disconnect_proxy = NULL;
372 klass->pre_activate = NULL;
373 klass->post_activate = NULL;
375 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
380 gtk_ui_manager_init (GtkUIManager *self)
385 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
387 self->private_data->accel_group = gtk_accel_group_new ();
389 self->private_data->root_node = NULL;
390 self->private_data->action_groups = NULL;
392 self->private_data->last_merge_id = 0;
393 self->private_data->add_tearoffs = FALSE;
395 merge_id = gtk_ui_manager_new_merge_id (self);
396 node = get_child_node (self, NULL, NULL, "ui", 2,
397 NODE_TYPE_ROOT, TRUE, FALSE);
398 node_prepend_ui_reference (node, merge_id, 0);
402 gtk_ui_manager_finalize (GObject *object)
404 GtkUIManager *self = GTK_UI_MANAGER (object);
406 if (self->private_data->update_tag != 0)
408 g_source_remove (self->private_data->update_tag);
409 self->private_data->update_tag = 0;
412 g_node_traverse (self->private_data->root_node,
413 G_POST_ORDER, G_TRAVERSE_ALL, -1,
414 (GNodeTraverseFunc)free_node, NULL);
415 g_node_destroy (self->private_data->root_node);
416 self->private_data->root_node = NULL;
418 g_list_foreach (self->private_data->action_groups,
419 (GFunc) g_object_unref, NULL);
420 g_list_free (self->private_data->action_groups);
421 self->private_data->action_groups = NULL;
423 g_object_unref (self->private_data->accel_group);
424 self->private_data->accel_group = NULL;
426 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
430 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
432 iface->add_child = gtk_ui_manager_buildable_add_child;
433 iface->construct_child = gtk_ui_manager_buildable_construct_child;
434 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
435 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
439 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
444 GtkUIManager *self = GTK_UI_MANAGER (buildable);
447 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
449 pos = g_list_length (self->private_data->action_groups);
451 g_object_ref (child);
452 gtk_ui_manager_insert_action_group (self,
453 GTK_ACTION_GROUP (child),
458 child_hierarchy_changed_cb (GtkWidget *widget,
463 GtkAccelGroup *group;
466 toplevel = gtk_widget_get_toplevel (widget);
467 if (!toplevel || !GTK_IS_WINDOW (toplevel))
470 group = gtk_ui_manager_get_accel_group (uimgr);
471 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
472 if (g_slist_find (groups, group) == NULL)
473 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
475 g_signal_handlers_disconnect_by_func (widget,
476 child_hierarchy_changed_cb,
481 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
488 name = g_strdup_printf ("ui/%s", id);
489 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
492 g_error ("Unknown ui manager child: %s\n", name);
498 g_signal_connect (widget, "hierarchy-changed",
499 G_CALLBACK (child_hierarchy_changed_cb),
500 GTK_UI_MANAGER (buildable));
501 return g_object_ref (widget);
505 gtk_ui_manager_set_property (GObject *object,
510 GtkUIManager *self = GTK_UI_MANAGER (object);
514 case PROP_ADD_TEAROFFS:
515 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
518 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524 gtk_ui_manager_get_property (GObject *object,
529 GtkUIManager *self = GTK_UI_MANAGER (object);
533 case PROP_ADD_TEAROFFS:
534 g_value_set_boolean (value, self->private_data->add_tearoffs);
537 g_value_take_string (value, gtk_ui_manager_get_ui (self));
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546 gtk_ui_manager_real_get_widget (GtkUIManager *self,
551 /* ensure that there are no pending updates before we get the
553 gtk_ui_manager_ensure_update (self);
555 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
560 return NODE_INFO (node)->proxy;
564 gtk_ui_manager_real_get_action (GtkUIManager *self,
569 /* ensure that there are no pending updates before we get
571 gtk_ui_manager_ensure_update (self);
573 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
578 return NODE_INFO (node)->action;
583 * gtk_ui_manager_new:
585 * Creates a new ui manager object.
587 * Return value: a new ui manager object.
592 gtk_ui_manager_new (void)
594 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
599 * gtk_ui_manager_get_add_tearoffs:
600 * @self: a #GtkUIManager
602 * Returns whether menus generated by this #GtkUIManager
603 * will have tearoff menu items.
605 * Return value: whether tearoff menu items are added
610 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
612 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
614 return self->private_data->add_tearoffs;
619 * gtk_ui_manager_set_add_tearoffs:
620 * @self: a #GtkUIManager
621 * @add_tearoffs: whether tearoff menu items are added
623 * Sets the "add_tearoffs" property, which controls whether menus
624 * generated by this #GtkUIManager will have tearoff menu items.
626 * Note that this only affects regular menus. Generated popup
627 * menus never have tearoff menu items.
632 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
633 gboolean add_tearoffs)
635 g_return_if_fail (GTK_IS_UI_MANAGER (self));
637 add_tearoffs = add_tearoffs != FALSE;
639 if (add_tearoffs != self->private_data->add_tearoffs)
641 self->private_data->add_tearoffs = add_tearoffs;
643 dirty_all_nodes (self);
645 g_object_notify (G_OBJECT (self), "add-tearoffs");
650 cb_proxy_connect_proxy (GtkActionGroup *group,
655 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
659 cb_proxy_disconnect_proxy (GtkActionGroup *group,
664 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
668 cb_proxy_pre_activate (GtkActionGroup *group,
672 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
676 cb_proxy_post_activate (GtkActionGroup *group,
680 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
684 * gtk_ui_manager_insert_action_group:
685 * @self: a #GtkUIManager object
686 * @action_group: the action group to be inserted
687 * @pos: the position at which the group will be inserted.
689 * Inserts an action group into the list of action groups associated
690 * with @self. Actions in earlier groups hide actions with the same
691 * name in later groups.
696 gtk_ui_manager_insert_action_group (GtkUIManager *self,
697 GtkActionGroup *action_group,
700 #ifdef G_ENABLE_DEBUG
702 const char *group_name;
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 #ifdef G_ENABLE_DEBUG
711 group_name = gtk_action_group_get_name (action_group);
713 for (l = self->private_data->action_groups; l; l = l->next)
715 GtkActionGroup *group = l->data;
717 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
719 g_warning ("Inserting action group '%s' into UI manager which "
720 "already has a group with this name\n", group_name);
724 #endif /* G_ENABLE_DEBUG */
726 g_object_ref (action_group);
727 self->private_data->action_groups =
728 g_list_insert (self->private_data->action_groups, action_group, pos);
729 g_object_connect (action_group,
730 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
731 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
732 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
733 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
736 /* dirty all nodes, as action bindings may change */
737 dirty_all_nodes (self);
739 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
743 * gtk_ui_manager_remove_action_group:
744 * @self: a #GtkUIManager object
745 * @action_group: the action group to be removed
747 * Removes an action group from the list of action groups associated
753 gtk_ui_manager_remove_action_group (GtkUIManager *self,
754 GtkActionGroup *action_group)
756 g_return_if_fail (GTK_IS_UI_MANAGER (self));
757 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
758 g_return_if_fail (g_list_find (self->private_data->action_groups,
759 action_group) != NULL);
761 self->private_data->action_groups =
762 g_list_remove (self->private_data->action_groups, action_group);
764 g_object_disconnect (action_group,
765 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
766 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
767 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
768 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
770 g_object_unref (action_group);
772 /* dirty all nodes, as action bindings may change */
773 dirty_all_nodes (self);
775 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
779 * gtk_ui_manager_get_action_groups:
780 * @self: a #GtkUIManager object
782 * Returns the list of action groups associated with @self.
784 * Return value: a #GList of action groups. The list is owned by GTK+
785 * and should not be modified.
790 gtk_ui_manager_get_action_groups (GtkUIManager *self)
792 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
794 return self->private_data->action_groups;
798 * gtk_ui_manager_get_accel_group:
799 * @self: a #GtkUIManager object
801 * Returns the #GtkAccelGroup associated with @self.
803 * Return value: the #GtkAccelGroup.
808 gtk_ui_manager_get_accel_group (GtkUIManager *self)
810 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
812 return self->private_data->accel_group;
816 * gtk_ui_manager_get_widget:
817 * @self: a #GtkUIManager
820 * Looks up a widget by following a path.
821 * The path consists of the names specified in the XML description of the UI.
822 * separated by '/'. Elements which don't have a name or action attribute in
823 * the XML (e.g. <popup>) can be addressed by their XML element name
824 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
826 * Note that the widget found by following a path that ends in a <menu>
827 * element is the menuitem to which the menu is attached, not the menu itself.
829 * Also note that the widgets constructed by a ui manager are not tied to
830 * the lifecycle of the ui manager. If you add the widgets returned by this
831 * function to some container or explicitly ref them, they will survive the
832 * destruction of the ui manager.
834 * Return value: the widget found by following the path, or %NULL if no widget
840 gtk_ui_manager_get_widget (GtkUIManager *self,
843 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
844 g_return_val_if_fail (path != NULL, NULL);
846 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
850 GtkUIManagerItemType types;
855 collect_toplevels (GNode *node,
858 ToplevelData *data = user_data;
860 if (NODE_INFO (node)->proxy)
862 switch (NODE_INFO (node)->type)
864 case NODE_TYPE_MENUBAR:
865 if (data->types & GTK_UI_MANAGER_MENUBAR)
866 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
868 case NODE_TYPE_TOOLBAR:
869 if (data->types & GTK_UI_MANAGER_TOOLBAR)
870 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
872 case NODE_TYPE_POPUP:
873 if (data->types & GTK_UI_MANAGER_POPUP)
874 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
882 * gtk_ui_manager_get_toplevels:
883 * @self: a #GtkUIManager
884 * @types: specifies the types of toplevel widgets to include. Allowed
885 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
886 * #GTK_UI_MANAGER_POPUP.
888 * Obtains a list of all toplevel widgets of the requested types.
890 * Return value: a newly-allocated of all toplevel widgets of the requested
896 gtk_ui_manager_get_toplevels (GtkUIManager *self,
897 GtkUIManagerItemType types)
901 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
902 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
903 GTK_UI_MANAGER_TOOLBAR |
904 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
910 g_node_children_foreach (self->private_data->root_node,
912 collect_toplevels, &data);
919 * gtk_ui_manager_get_action:
920 * @self: a #GtkUIManager
923 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
924 * for more information about paths.
926 * Return value: the action whose proxy widget is found by following the path,
927 * or %NULL if no widget was found.
932 gtk_ui_manager_get_action (GtkUIManager *self,
935 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
936 g_return_val_if_fail (path != NULL, NULL);
938 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
942 get_child_node (GtkUIManager *self,
945 const gchar *childname,
946 gint childname_length,
957 for (child = parent->children; child != NULL; child = child->next)
959 if (NODE_INFO (child)->name &&
960 strlen (NODE_INFO (child)->name) == childname_length &&
961 !strncmp (NODE_INFO (child)->name, childname, childname_length))
963 /* if undecided about node type, set it */
964 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
965 NODE_INFO (child)->type = node_type;
967 /* warn about type mismatch */
968 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
969 node_type != NODE_TYPE_UNDECIDED &&
970 NODE_INFO (child)->type != node_type)
971 g_warning ("node type doesn't match %d (%s is type %d)",
973 NODE_INFO (child)->name,
974 NODE_INFO (child)->type);
980 if (!child && create)
984 mnode = g_slice_new0 (Node);
985 mnode->type = node_type;
986 mnode->name = g_strndup (childname, childname_length);
991 child = g_node_insert_before (parent, sibling,
994 child = g_node_insert_after (parent, sibling,
1000 child = g_node_prepend_data (parent, mnode);
1002 child = g_node_append_data (parent, mnode);
1005 mark_node_dirty (child);
1010 /* handle root node */
1011 if (self->private_data->root_node)
1013 child = self->private_data->root_node;
1014 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1015 g_warning ("root node name '%s' doesn't match '%s'",
1016 childname, NODE_INFO (child)->name);
1017 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1018 g_warning ("base element must be of type ROOT");
1024 mnode = g_slice_new0 (Node);
1025 mnode->type = node_type;
1026 mnode->name = g_strndup (childname, childname_length);
1027 mnode->dirty = TRUE;
1029 child = self->private_data->root_node = g_node_new (mnode);
1037 get_node (GtkUIManager *self,
1042 const gchar *pos, *end;
1043 GNode *parent, *node;
1045 if (strncmp ("/ui", path, 3) == 0)
1048 end = path + strlen (path);
1050 parent = node = NULL;
1056 slash = strchr (pos, '/');
1058 length = slash - pos;
1060 length = strlen (pos);
1062 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1067 pos += length + 1; /* move past the node name and the slash too */
1071 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1072 NODE_INFO (node)->type = node_type;
1078 node_ui_reference_free (gpointer data)
1080 g_slice_free (NodeUIReference, data);
1084 free_node (GNode *node)
1086 Node *info = NODE_INFO (node);
1088 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1089 g_list_free (info->uifiles);
1092 g_object_unref (info->action);
1094 g_object_unref (info->proxy);
1096 g_object_unref (info->extra);
1097 g_free (info->name);
1098 g_slice_free (Node, info);
1104 * gtk_ui_manager_new_merge_id:
1105 * @self: a #GtkUIManager
1107 * Returns an unused merge id, suitable for use with
1108 * gtk_ui_manager_add_ui().
1110 * Return value: an unused merge id.
1115 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1117 self->private_data->last_merge_id++;
1119 return self->private_data->last_merge_id;
1123 node_prepend_ui_reference (GNode *gnode,
1125 GQuark action_quark)
1127 Node *node = NODE_INFO (gnode);
1128 NodeUIReference *reference = NULL;
1130 if (node->uifiles &&
1131 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1132 reference = node->uifiles->data;
1135 reference = g_slice_new (NodeUIReference);
1136 node->uifiles = g_list_prepend (node->uifiles, reference);
1139 reference->merge_id = merge_id;
1140 reference->action_quark = action_quark;
1142 mark_node_dirty (gnode);
1146 node_remove_ui_reference (GNode *gnode,
1149 Node *node = NODE_INFO (gnode);
1152 for (p = node->uifiles; p != NULL; p = p->next)
1154 NodeUIReference *reference = p->data;
1156 if (reference->merge_id == merge_id)
1158 if (p == node->uifiles)
1159 mark_node_dirty (gnode);
1160 node->uifiles = g_list_delete_link (node->uifiles, p);
1161 g_slice_free (NodeUIReference, reference);
1168 /* -------------------- The UI file parser -------------------- */
1182 typedef struct _ParseContext ParseContext;
1183 struct _ParseContext
1186 ParseState prev_state;
1196 start_element_handler (GMarkupParseContext *context,
1197 const gchar *element_name,
1198 const gchar **attribute_names,
1199 const gchar **attribute_values,
1203 ParseContext *ctx = user_data;
1204 GtkUIManager *self = ctx->self;
1207 const gchar *node_name;
1208 const gchar *action;
1209 GQuark action_quark;
1211 gboolean expand = FALSE;
1213 gboolean raise_error = TRUE;
1220 for (i = 0; attribute_names[i] != NULL; i++)
1222 if (!strcmp (attribute_names[i], "name"))
1224 node_name = attribute_values[i];
1226 else if (!strcmp (attribute_names[i], "action"))
1228 action = attribute_values[i];
1229 action_quark = g_quark_from_string (attribute_values[i]);
1231 else if (!strcmp (attribute_names[i], "position"))
1233 top = !strcmp (attribute_values[i], "top");
1235 else if (!strcmp (attribute_names[i], "expand"))
1237 expand = !strcmp (attribute_values[i], "true");
1239 /* else silently skip unknown attributes to be compatible with
1240 * future additional attributes.
1244 /* Work out a name for this node. Either the name attribute, or
1245 * the action, or the element name */
1246 if (node_name == NULL)
1251 node_name = element_name;
1254 switch (element_name[0])
1257 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1259 ctx->state = STATE_ACCELERATOR;
1260 ctx->current = get_child_node (self, ctx->current, NULL,
1261 node_name, strlen (node_name),
1262 NODE_TYPE_ACCELERATOR,
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;
1273 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1275 ctx->state = STATE_ROOT;
1276 ctx->current = self->private_data->root_node;
1277 raise_error = FALSE;
1279 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1283 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1285 ctx->state = STATE_MENU;
1286 ctx->current = get_child_node (self, ctx->current, NULL,
1287 node_name, strlen (node_name),
1290 if (NODE_INFO (ctx->current)->action_name == 0)
1291 NODE_INFO (ctx->current)->action_name = action_quark;
1293 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1294 mark_node_dirty (ctx->current);
1296 raise_error = FALSE;
1298 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1300 ctx->current = get_child_node (self, ctx->current, NULL,
1301 node_name, strlen (node_name),
1304 if (NODE_INFO (ctx->current)->action_name == 0)
1305 NODE_INFO (ctx->current)->action_name = action_quark;
1307 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1309 raise_error = FALSE;
1311 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1313 ctx->state = STATE_MENU;
1315 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1316 node_name, strlen (node_name),
1319 if (NODE_INFO (ctx->current)->action_name == 0)
1320 NODE_INFO (ctx->current)->action_name = action_quark;
1322 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1324 raise_error = FALSE;
1326 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1330 ctx->state = STATE_MENUITEM;
1331 node = get_child_node (self, ctx->current, NULL,
1332 node_name, strlen (node_name),
1335 if (NODE_INFO (node)->action_name == 0)
1336 NODE_INFO (node)->action_name = action_quark;
1338 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1340 raise_error = FALSE;
1344 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1346 ctx->state = STATE_MENU;
1347 ctx->current = get_child_node (self, ctx->current, NULL,
1348 node_name, strlen (node_name),
1351 if (NODE_INFO (ctx->current)->action_name == 0)
1352 NODE_INFO (ctx->current)->action_name = action_quark;
1354 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1356 raise_error = FALSE;
1358 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1359 !strcmp (element_name, "placeholder"))
1361 if (ctx->state == STATE_TOOLBAR)
1362 ctx->current = get_child_node (self, ctx->current, NULL,
1363 node_name, strlen (node_name),
1364 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1367 ctx->current = get_child_node (self, ctx->current, NULL,
1368 node_name, strlen (node_name),
1369 NODE_TYPE_MENU_PLACEHOLDER,
1372 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1374 raise_error = FALSE;
1378 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1379 !strcmp (element_name, "separator"))
1384 if (ctx->state == STATE_TOOLBAR)
1385 ctx->state = STATE_TOOLITEM;
1387 ctx->state = STATE_MENUITEM;
1388 if (!strcmp (node_name, "separator"))
1394 length = strlen (node_name);
1395 node = get_child_node (self, ctx->current, NULL,
1397 NODE_TYPE_SEPARATOR,
1400 NODE_INFO (node)->expand = expand;
1402 if (NODE_INFO (node)->action_name == 0)
1403 NODE_INFO (node)->action_name = action_quark;
1405 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1407 raise_error = FALSE;
1411 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1413 ctx->state = STATE_TOOLBAR;
1414 ctx->current = get_child_node (self, ctx->current, NULL,
1415 node_name, strlen (node_name),
1418 if (NODE_INFO (ctx->current)->action_name == 0)
1419 NODE_INFO (ctx->current)->action_name = action_quark;
1421 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1423 raise_error = FALSE;
1425 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1429 ctx->state = STATE_TOOLITEM;
1430 node = get_child_node (self, ctx->current, NULL,
1431 node_name, strlen (node_name),
1434 if (NODE_INFO (node)->action_name == 0)
1435 NODE_INFO (node)->action_name = action_quark;
1437 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1439 raise_error = FALSE;
1447 gint line_number, char_number;
1449 g_markup_parse_context_get_position (context,
1450 &line_number, &char_number);
1453 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1454 _("Unexpected start tag '%s' on line %d char %d"),
1456 line_number, char_number);
1461 end_element_handler (GMarkupParseContext *context,
1462 const gchar *element_name,
1466 ParseContext *ctx = user_data;
1472 /* no need to GError here, GMarkup already catches this */
1475 ctx->current = NULL;
1476 ctx->state = STATE_END;
1480 case STATE_ACCELERATOR:
1481 ctx->current = ctx->current->parent;
1482 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1483 ctx->state = STATE_ROOT;
1484 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1486 ctx->current = ctx->current->parent;
1487 ctx->state = STATE_TOOLITEM;
1489 /* else, stay in same state */
1491 case STATE_MENUITEM:
1492 ctx->state = STATE_MENU;
1494 case STATE_TOOLITEM:
1495 ctx->state = STATE_TOOLBAR;
1501 cleanup (GMarkupParseContext *context,
1505 ParseContext *ctx = user_data;
1507 ctx->current = NULL;
1508 /* should also walk through the tree and get rid of nodes related to
1509 * this UI file's tag */
1511 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1515 xml_isspace (char c)
1517 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1521 text_handler (GMarkupParseContext *context,
1531 end = text + text_len;
1532 while (p != end && xml_isspace (*p))
1537 gint line_number, char_number;
1539 g_markup_parse_context_get_position (context,
1540 &line_number, &char_number);
1543 G_MARKUP_ERROR_INVALID_CONTENT,
1544 _("Unexpected character data on line %d char %d"),
1545 line_number, char_number);
1550 static const GMarkupParser ui_parser = {
1551 start_element_handler,
1552 end_element_handler,
1559 add_ui_from_string (GtkUIManager *self,
1560 const gchar *buffer,
1562 gboolean needs_root,
1565 ParseContext ctx = { 0 };
1566 GMarkupParseContext *context;
1568 ctx.state = STATE_START;
1571 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1573 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1576 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1579 if (!g_markup_parse_context_parse (context, buffer, length, error))
1583 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1586 if (!g_markup_parse_context_end_parse (context, error))
1589 g_markup_parse_context_free (context);
1591 queue_update (self);
1593 g_object_notify (G_OBJECT (self), "ui");
1595 return ctx.merge_id;
1599 g_markup_parse_context_free (context);
1605 * gtk_ui_manager_add_ui_from_string:
1606 * @self: a #GtkUIManager object
1607 * @buffer: the string to parse
1608 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1609 * @error: return location for an error
1611 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1612 * merges it with the current contents of @self. An enclosing <ui>
1613 * element is added if it is missing.
1615 * Return value: The merge id for the merged UI. The merge id can be used
1616 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1617 * the return value is 0.
1622 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1623 const gchar *buffer,
1627 gboolean needs_root = TRUE;
1631 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1632 g_return_val_if_fail (buffer != NULL, 0);
1635 length = strlen (buffer);
1638 end = buffer + length;
1639 while (p != end && xml_isspace (*p))
1642 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1645 return add_ui_from_string (self, buffer, length, needs_root, error);
1649 * gtk_ui_manager_add_ui_from_file:
1650 * @self: a #GtkUIManager object
1651 * @filename: the name of the file to parse
1652 * @error: return location for an error
1654 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1655 * merges it with the current contents of @self.
1657 * Return value: The merge id for the merged UI. The merge id can be used
1658 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1659 * the return value is 0.
1664 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1665 const gchar *filename,
1672 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1674 if (!g_file_get_contents (filename, &buffer, &length, error))
1677 res = add_ui_from_string (self, buffer, length, FALSE, error);
1684 * gtk_ui_manager_add_ui:
1685 * @self: a #GtkUIManager
1686 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1688 * @name: the name for the added UI element
1689 * @action: the name of the action to be proxied, or %NULL to add a separator
1690 * @type: the type of UI element to add.
1691 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1692 * is added after its siblings.
1694 * Adds a UI element to the current contents of @self.
1696 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1697 * separator if such an element can be inserted at the place determined by
1698 * @path. Otherwise @type must indicate an element that can be inserted at
1699 * the place determined by @path.
1701 * If @path points to a menuitem or toolitem, the new element will be inserted
1702 * before or after this item, depending on @top.
1707 gtk_ui_manager_add_ui (GtkUIManager *self,
1711 const gchar *action,
1712 GtkUIManagerItemType type,
1719 GQuark action_quark = 0;
1721 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1722 g_return_if_fail (merge_id > 0);
1723 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1725 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1731 node_type = NODE_TYPE_UNDECIDED;
1734 switch (NODE_INFO (node)->type)
1736 case NODE_TYPE_SEPARATOR:
1737 case NODE_TYPE_MENUITEM:
1738 case NODE_TYPE_TOOLITEM:
1740 node = node->parent;
1742 case NODE_TYPE_MENUBAR:
1743 case NODE_TYPE_MENU:
1744 case NODE_TYPE_POPUP:
1745 case NODE_TYPE_MENU_PLACEHOLDER:
1748 case GTK_UI_MANAGER_AUTO:
1750 node_type = NODE_TYPE_MENUITEM;
1752 node_type = NODE_TYPE_SEPARATOR;
1754 case GTK_UI_MANAGER_MENU:
1755 node_type = NODE_TYPE_MENU;
1757 case GTK_UI_MANAGER_MENUITEM:
1758 node_type = NODE_TYPE_MENUITEM;
1760 case GTK_UI_MANAGER_SEPARATOR:
1761 node_type = NODE_TYPE_SEPARATOR;
1763 case GTK_UI_MANAGER_PLACEHOLDER:
1764 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1770 case NODE_TYPE_TOOLBAR:
1771 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1774 case GTK_UI_MANAGER_AUTO:
1776 node_type = NODE_TYPE_TOOLITEM;
1778 node_type = NODE_TYPE_SEPARATOR;
1780 case GTK_UI_MANAGER_TOOLITEM:
1781 node_type = NODE_TYPE_TOOLITEM;
1783 case GTK_UI_MANAGER_SEPARATOR:
1784 node_type = NODE_TYPE_SEPARATOR;
1786 case GTK_UI_MANAGER_PLACEHOLDER:
1787 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1793 case NODE_TYPE_ROOT:
1796 case GTK_UI_MANAGER_MENUBAR:
1797 node_type = NODE_TYPE_MENUBAR;
1799 case GTK_UI_MANAGER_TOOLBAR:
1800 node_type = NODE_TYPE_TOOLBAR;
1802 case GTK_UI_MANAGER_POPUP:
1803 node_type = NODE_TYPE_POPUP;
1805 case GTK_UI_MANAGER_ACCELERATOR:
1806 node_type = NODE_TYPE_ACCELERATOR;
1816 if (node_type == NODE_TYPE_UNDECIDED)
1818 g_warning ("item type %d not suitable for adding at '%s'",
1823 child = get_child_node (self, node, sibling,
1824 name, name ? strlen (name) : 0,
1825 node_type, TRUE, top);
1828 action_quark = g_quark_from_string (action);
1830 node_prepend_ui_reference (child, merge_id, action_quark);
1832 if (NODE_INFO (child)->action_name == 0)
1833 NODE_INFO (child)->action_name = action_quark;
1835 queue_update (self);
1837 g_object_notify (G_OBJECT (self), "ui");
1841 remove_ui (GNode *node,
1844 guint merge_id = GPOINTER_TO_UINT (user_data);
1846 node_remove_ui_reference (node, merge_id);
1848 return FALSE; /* continue */
1852 * gtk_ui_manager_remove_ui:
1853 * @self: a #GtkUIManager object
1854 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1856 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1861 gtk_ui_manager_remove_ui (GtkUIManager *self,
1864 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1866 g_node_traverse (self->private_data->root_node,
1867 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1868 remove_ui, GUINT_TO_POINTER (merge_id));
1870 queue_update (self);
1872 g_object_notify (G_OBJECT (self), "ui");
1875 /* -------------------- Updates -------------------- */
1879 get_action_by_name (GtkUIManager *merge,
1880 const gchar *action_name)
1888 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1890 GtkActionGroup *action_group = tmp->data;
1893 action = gtk_action_group_get_action (action_group, action_name);
1903 find_menu_position (GNode *node,
1904 GtkWidget **menushell_p,
1907 GtkWidget *menushell;
1910 g_return_val_if_fail (node != NULL, FALSE);
1911 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1912 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1913 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1914 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1915 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1918 /* first sibling -- look at parent */
1919 if (node->prev == NULL)
1924 parent = node->parent;
1925 switch (NODE_INFO (parent)->type)
1927 case NODE_TYPE_MENUBAR:
1928 case NODE_TYPE_POPUP:
1929 menushell = NODE_INFO (parent)->proxy;
1932 case NODE_TYPE_MENU:
1933 menushell = NODE_INFO (parent)->proxy;
1934 if (GTK_IS_MENU_ITEM (menushell))
1935 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1936 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1937 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1941 g_list_free (siblings);
1943 case NODE_TYPE_MENU_PLACEHOLDER:
1944 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1945 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1946 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1947 NODE_INFO (parent)->proxy) + 1;
1950 g_warning ("%s: bad parent node type %d", G_STRLOC,
1951 NODE_INFO (parent)->type);
1957 GtkWidget *prev_child;
1960 sibling = node->prev;
1961 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1962 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1964 prev_child = NODE_INFO (sibling)->proxy;
1966 if (!GTK_IS_WIDGET (prev_child))
1969 menushell = gtk_widget_get_parent (prev_child);
1970 if (!GTK_IS_MENU_SHELL (menushell))
1973 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1977 *menushell_p = menushell;
1985 find_toolbar_position (GNode *node,
1986 GtkWidget **toolbar_p,
1992 g_return_val_if_fail (node != NULL, FALSE);
1993 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1994 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1995 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1996 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1999 /* first sibling -- look at parent */
2000 if (node->prev == NULL)
2004 parent = node->parent;
2005 switch (NODE_INFO (parent)->type)
2007 case NODE_TYPE_TOOLBAR:
2008 toolbar = NODE_INFO (parent)->proxy;
2011 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2012 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2013 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2014 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2015 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2018 g_warning ("%s: bad parent node type %d", G_STRLOC,
2019 NODE_INFO (parent)->type);
2025 GtkWidget *prev_child;
2028 sibling = node->prev;
2029 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2030 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2032 prev_child = NODE_INFO (sibling)->proxy;
2034 if (!GTK_IS_WIDGET (prev_child))
2037 toolbar = gtk_widget_get_parent (prev_child);
2038 if (!GTK_IS_TOOLBAR (toolbar))
2041 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2042 GTK_TOOL_ITEM (prev_child)) + 1;
2046 *toolbar_p = toolbar;
2054 * _gtk_menu_is_empty:
2055 * @menu: a #GtkMenu or %NULL
2057 * Determines whether @menu is empty. A menu is considered empty if it
2058 * the only visible children are tearoff menu items or "filler" menu
2059 * items which were inserted to mark the menu as empty.
2061 * This function is used by #GtkAction.
2063 * Return value: whether @menu is empty.
2066 _gtk_menu_is_empty (GtkWidget *menu)
2068 GList *children, *cur;
2069 gboolean result = TRUE;
2071 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2076 children = gtk_container_get_children (GTK_CONTAINER (menu));
2081 if (GTK_WIDGET_VISIBLE (cur->data))
2083 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2084 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2092 g_list_free (children);
2098 SEPARATOR_MODE_SMART,
2099 SEPARATOR_MODE_VISIBLE,
2100 SEPARATOR_MODE_HIDDEN
2104 update_smart_separators (GtkWidget *proxy)
2106 GtkWidget *parent = NULL;
2108 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2110 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2111 parent = gtk_widget_get_parent (proxy);
2117 GList *children, *cur, *last;
2120 children = gtk_container_get_children (GTK_CONTAINER (parent));
2130 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2134 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2135 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2138 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2139 "gtk-separator-mode"));
2142 case SEPARATOR_MODE_VISIBLE:
2143 gtk_widget_show (GTK_WIDGET (cur->data));
2147 case SEPARATOR_MODE_HIDDEN:
2148 gtk_widget_hide (GTK_WIDGET (cur->data));
2150 case SEPARATOR_MODE_SMART:
2153 gtk_widget_show (GTK_WIDGET (cur->data));
2158 gtk_widget_hide (GTK_WIDGET (cur->data));
2162 else if (GTK_WIDGET_VISIBLE (cur->data))
2165 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2178 gtk_widget_hide (GTK_WIDGET (last->data));
2180 if (GTK_IS_MENU (parent))
2184 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2185 if (GTK_IS_MENU_ITEM (item))
2186 _gtk_action_sync_menu_visible (NULL, item, empty);
2187 if (GTK_IS_WIDGET (filler))
2190 gtk_widget_show (filler);
2192 gtk_widget_hide (filler);
2196 g_list_free (children);
2201 update_node (GtkUIManager *self,
2208 const gchar *action_name;
2209 NodeUIReference *ref;
2211 #ifdef DEBUG_UI_MANAGER
2215 g_return_if_fail (node != NULL);
2216 g_return_if_fail (NODE_INFO (node) != NULL);
2218 info = NODE_INFO (node);
2223 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2225 #ifdef DEBUG_UI_MANAGER
2226 g_print ("update_node name=%s dirty=%d popup %d (",
2227 info->name, info->dirty, in_popup);
2228 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2230 NodeUIReference *ref = tmp->data;
2231 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2238 if (info->uifiles == NULL) {
2239 /* We may need to remove this node.
2240 * This must be done in post order
2242 goto recurse_children;
2245 ref = info->uifiles->data;
2246 action_name = g_quark_to_string (ref->action_quark);
2247 action = get_action_by_name (self, action_name);
2249 info->dirty = FALSE;
2251 /* Check if the node doesn't have an action and must have an action */
2252 if (action == NULL &&
2253 info->type != NODE_TYPE_ROOT &&
2254 info->type != NODE_TYPE_MENUBAR &&
2255 info->type != NODE_TYPE_TOOLBAR &&
2256 info->type != NODE_TYPE_POPUP &&
2257 info->type != NODE_TYPE_SEPARATOR &&
2258 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2259 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2261 g_warning ("%s: missing action %s", info->name, action_name);
2267 gtk_action_set_accel_group (action, self->private_data->accel_group);
2269 /* If the widget already has a proxy and the action hasn't changed, then
2270 * we only have to update the tearoff menu items.
2272 if (info->proxy != NULL && action == info->action)
2274 if (info->type == NODE_TYPE_MENU)
2279 if (GTK_IS_MENU (info->proxy))
2282 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2283 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2284 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2286 if (self->private_data->add_tearoffs && !in_popup)
2287 gtk_widget_show (GTK_WIDGET (siblings->data));
2289 gtk_widget_hide (GTK_WIDGET (siblings->data));
2291 g_list_free (siblings);
2294 goto recurse_children;
2299 case NODE_TYPE_MENUBAR:
2300 if (info->proxy == NULL)
2302 info->proxy = gtk_menu_bar_new ();
2303 g_object_ref_sink (info->proxy);
2304 gtk_widget_set_name (info->proxy, info->name);
2305 gtk_widget_show (info->proxy);
2306 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2309 case NODE_TYPE_POPUP:
2310 if (info->proxy == NULL)
2312 info->proxy = gtk_menu_new ();
2313 g_object_ref_sink (info->proxy);
2315 gtk_widget_set_name (info->proxy, info->name);
2317 case NODE_TYPE_MENU:
2319 GtkWidget *prev_submenu = NULL;
2320 GtkWidget *menu = NULL;
2323 /* remove the proxy if it is of the wrong type ... */
2325 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2327 if (GTK_IS_MENU_ITEM (info->proxy))
2329 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2332 g_object_ref (prev_submenu);
2333 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2337 gtk_action_disconnect_proxy (info->action, info->proxy);
2338 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2340 g_object_unref (info->proxy);
2344 /* create proxy if needed ... */
2345 if (info->proxy == NULL)
2347 /* ... if the action already provides a menu, then use
2348 * that menu instead of creating an empty one
2350 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2351 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2352 GTK_ACTION_GET_CLASS (action)->create_menu)
2354 menu = gtk_action_create_menu (action);
2362 menu = gtk_menu_new ();
2363 gtk_widget_set_name (menu, info->name);
2364 tearoff = gtk_tearoff_menu_item_new ();
2365 gtk_widget_set_no_show_all (tearoff, TRUE);
2366 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2367 filler = gtk_menu_item_new_with_label (_("Empty"));
2368 g_object_set_data (G_OBJECT (filler),
2369 I_("gtk-empty-menu-item"),
2370 GINT_TO_POINTER (TRUE));
2371 gtk_widget_set_sensitive (filler, FALSE);
2372 gtk_widget_set_no_show_all (filler, TRUE);
2373 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2376 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2379 g_object_ref_sink (info->proxy);
2380 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2385 GtkWidget *menushell;
2388 if (find_menu_position (node, &menushell, &pos))
2390 info->proxy = gtk_action_create_menu_item (action);
2391 g_object_ref_sink (info->proxy);
2392 g_signal_connect (info->proxy, "notify::visible",
2393 G_CALLBACK (update_smart_separators), NULL);
2394 gtk_widget_set_name (info->proxy, info->name);
2396 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2397 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2402 gtk_action_connect_proxy (action, info->proxy);
2406 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2408 g_object_unref (prev_submenu);
2411 if (GTK_IS_MENU (info->proxy))
2414 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2416 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2417 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2419 if (self->private_data->add_tearoffs && !in_popup)
2420 gtk_widget_show (GTK_WIDGET (siblings->data));
2422 gtk_widget_hide (GTK_WIDGET (siblings->data));
2424 g_list_free (siblings);
2427 case NODE_TYPE_UNDECIDED:
2428 g_warning ("found undecided node!");
2430 case NODE_TYPE_ROOT:
2432 case NODE_TYPE_TOOLBAR:
2433 if (info->proxy == NULL)
2435 info->proxy = gtk_toolbar_new ();
2436 g_object_ref_sink (info->proxy);
2437 gtk_widget_set_name (info->proxy, info->name);
2438 gtk_widget_show (info->proxy);
2439 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2442 case NODE_TYPE_MENU_PLACEHOLDER:
2443 /* create menu items for placeholders if necessary ... */
2444 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2445 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2449 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2451 g_object_unref (info->proxy);
2456 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2458 g_object_unref (info->extra);
2462 if (info->proxy == NULL)
2464 GtkWidget *menushell;
2467 if (find_menu_position (node, &menushell, &pos))
2469 info->proxy = gtk_separator_menu_item_new ();
2470 g_object_ref_sink (info->proxy);
2471 g_object_set_data (G_OBJECT (info->proxy),
2472 I_("gtk-separator-mode"),
2473 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2474 gtk_widget_set_no_show_all (info->proxy, TRUE);
2475 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2476 NODE_INFO (node)->proxy, pos);
2478 info->extra = gtk_separator_menu_item_new ();
2479 g_object_ref_sink (info->extra);
2480 g_object_set_data (G_OBJECT (info->extra),
2481 I_("gtk-separator-mode"),
2482 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2483 gtk_widget_set_no_show_all (info->extra, TRUE);
2484 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2485 NODE_INFO (node)->extra, pos + 1);
2489 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2490 /* create toolbar items for placeholders if necessary ... */
2491 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2492 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2496 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2498 g_object_unref (info->proxy);
2503 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2505 g_object_unref (info->extra);
2509 if (info->proxy == NULL)
2515 if (find_toolbar_position (node, &toolbar, &pos))
2517 item = gtk_separator_tool_item_new ();
2518 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2519 info->proxy = GTK_WIDGET (item);
2520 g_object_ref_sink (info->proxy);
2521 g_object_set_data (G_OBJECT (info->proxy),
2522 I_("gtk-separator-mode"),
2523 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2524 gtk_widget_set_no_show_all (info->proxy, TRUE);
2526 item = gtk_separator_tool_item_new ();
2527 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2528 info->extra = GTK_WIDGET (item);
2529 g_object_ref_sink (info->extra);
2530 g_object_set_data (G_OBJECT (info->extra),
2531 I_("gtk-separator-mode"),
2532 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2533 gtk_widget_set_no_show_all (info->extra, TRUE);
2537 case NODE_TYPE_MENUITEM:
2538 /* remove the proxy if it is of the wrong type ... */
2540 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2542 g_signal_handlers_disconnect_by_func (info->proxy,
2543 G_CALLBACK (update_smart_separators),
2545 gtk_action_disconnect_proxy (info->action, info->proxy);
2546 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2548 g_object_unref (info->proxy);
2551 /* create proxy if needed ... */
2552 if (info->proxy == NULL)
2554 GtkWidget *menushell;
2557 if (find_menu_position (node, &menushell, &pos))
2559 info->proxy = gtk_action_create_menu_item (action);
2560 g_object_ref_sink (info->proxy);
2561 gtk_widget_set_name (info->proxy, info->name);
2563 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2569 g_signal_handlers_disconnect_by_func (info->proxy,
2570 G_CALLBACK (update_smart_separators),
2572 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2573 gtk_action_connect_proxy (action, info->proxy);
2578 g_signal_connect (info->proxy, "notify::visible",
2579 G_CALLBACK (update_smart_separators), NULL);
2582 /* don't show accels in popups */
2583 GtkWidget *label = GTK_BIN (info->proxy)->child;
2584 g_object_set (label, "accel-closure", NULL, NULL);
2589 case NODE_TYPE_TOOLITEM:
2590 /* remove the proxy if it is of the wrong type ... */
2592 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2594 g_signal_handlers_disconnect_by_func (info->proxy,
2595 G_CALLBACK (update_smart_separators),
2597 gtk_action_disconnect_proxy (info->action, info->proxy);
2598 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2600 g_object_unref (info->proxy);
2603 /* create proxy if needed ... */
2604 if (info->proxy == NULL)
2609 if (find_toolbar_position (node, &toolbar, &pos))
2611 info->proxy = gtk_action_create_tool_item (action);
2612 g_object_ref_sink (info->proxy);
2613 gtk_widget_set_name (info->proxy, info->name);
2615 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2616 GTK_TOOL_ITEM (info->proxy), pos);
2621 g_signal_handlers_disconnect_by_func (info->proxy,
2622 G_CALLBACK (update_smart_separators),
2624 gtk_action_connect_proxy (action, info->proxy);
2629 g_signal_connect (info->proxy, "notify::visible",
2630 G_CALLBACK (update_smart_separators), NULL);
2633 case NODE_TYPE_SEPARATOR:
2634 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2635 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2639 gint separator_mode;
2642 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2644 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2646 g_object_unref (info->proxy);
2650 if (find_toolbar_position (node, &toolbar, &pos))
2652 item = gtk_separator_tool_item_new ();
2653 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2654 info->proxy = GTK_WIDGET (item);
2655 g_object_ref_sink (info->proxy);
2656 gtk_widget_set_no_show_all (info->proxy, TRUE);
2659 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2660 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2661 separator_mode = SEPARATOR_MODE_VISIBLE;
2664 separator_mode = SEPARATOR_MODE_SMART;
2666 g_object_set_data (G_OBJECT (info->proxy),
2667 I_("gtk-separator-mode"),
2668 GINT_TO_POINTER (separator_mode));
2669 gtk_widget_show (info->proxy);
2674 GtkWidget *menushell;
2677 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2679 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2681 g_object_unref (info->proxy);
2685 if (find_menu_position (node, &menushell, &pos))
2687 info->proxy = gtk_separator_menu_item_new ();
2688 g_object_ref_sink (info->proxy);
2689 gtk_widget_set_no_show_all (info->proxy, TRUE);
2690 g_object_set_data (G_OBJECT (info->proxy),
2691 I_("gtk-separator-mode"),
2692 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2693 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2695 gtk_widget_show (info->proxy);
2699 case NODE_TYPE_ACCELERATOR:
2700 gtk_action_connect_accelerator (action);
2705 g_object_ref (action);
2707 g_object_unref (info->action);
2708 info->action = action;
2711 /* process children */
2712 child = node->children;
2718 child = current->next;
2719 update_node (self, current, in_popup);
2724 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2725 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2726 else if (info->type == NODE_TYPE_MENU ||
2727 info->type == NODE_TYPE_TOOLBAR ||
2728 info->type == NODE_TYPE_POPUP)
2729 update_smart_separators (info->proxy);
2732 /* handle cleanup of dead nodes */
2733 if (node->children == NULL && info->uifiles == NULL)
2736 gtk_widget_destroy (info->proxy);
2738 gtk_widget_destroy (info->extra);
2739 if (info->type == NODE_TYPE_ACCELERATOR)
2740 gtk_action_disconnect_accelerator (info->action);
2742 g_node_destroy (node);
2747 do_updates (GtkUIManager *self)
2749 /* this function needs to check through the tree for dirty nodes.
2750 * For such nodes, it needs to do the following:
2752 * 1) check if they are referenced by any loaded UI files anymore.
2753 * In which case, the proxy widget should be destroyed, unless
2754 * there are any subnodes.
2756 * 2) lookup the action for this node again. If it is different to
2757 * the current one (or if no previous action has been looked up),
2758 * the proxy is reconnected to the new action (or a new proxy widget
2759 * is created and added to the parent container).
2761 update_node (self, self->private_data->root_node, FALSE);
2763 self->private_data->update_tag = 0;
2769 do_updates_idle (GtkUIManager *self)
2777 queue_update (GtkUIManager *self)
2779 if (self->private_data->update_tag != 0)
2782 self->private_data->update_tag = gdk_threads_add_idle (
2783 (GSourceFunc)do_updates_idle,
2789 * gtk_ui_manager_ensure_update:
2790 * @self: a #GtkUIManager
2792 * Makes sure that all pending updates to the UI have been completed.
2794 * This may occasionally be necessary, since #GtkUIManager updates the
2795 * UI in an idle function. A typical example where this function is
2796 * useful is to enforce that the menubar and toolbar have been added to
2797 * the main window before showing it:
2799 * gtk_container_add (GTK_CONTAINER (window), vbox);
2800 * g_signal_connect (merge, "add_widget",
2801 * G_CALLBACK (add_widget), vbox);
2802 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2803 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2804 * gtk_ui_manager_ensure_update (merge);
2805 * gtk_widget_show (window);
2811 gtk_ui_manager_ensure_update (GtkUIManager *self)
2813 if (self->private_data->update_tag != 0)
2815 g_source_remove (self->private_data->update_tag);
2821 dirty_traverse_func (GNode *node,
2824 NODE_INFO (node)->dirty = TRUE;
2829 dirty_all_nodes (GtkUIManager *self)
2831 g_node_traverse (self->private_data->root_node,
2832 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2833 dirty_traverse_func, NULL);
2834 queue_update (self);
2838 mark_node_dirty (GNode *node)
2842 /* FIXME could optimize this */
2843 for (p = node; p; p = p->parent)
2844 NODE_INFO (p)->dirty = TRUE;
2847 static const gchar *
2848 open_tag_format (NodeType type)
2852 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2853 case NODE_TYPE_ROOT: return "%*s<ui";
2854 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2855 case NODE_TYPE_MENU: return "%*s<menu";
2856 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2857 case NODE_TYPE_MENU_PLACEHOLDER:
2858 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2859 case NODE_TYPE_POPUP: return "%*s<popup";
2860 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2861 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2862 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2863 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2864 default: return NULL;
2868 static const gchar *
2869 close_tag_format (NodeType type)
2873 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2874 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2875 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2876 case NODE_TYPE_MENU: return "%*s</menu>\n";
2877 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2878 case NODE_TYPE_MENU_PLACEHOLDER:
2879 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2880 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2881 default: return NULL;
2886 print_node (GtkUIManager *self,
2893 const gchar *open_fmt;
2894 const gchar *close_fmt;
2898 open_fmt = open_tag_format (mnode->type);
2899 close_fmt = close_tag_format (mnode->type);
2901 g_string_append_printf (buffer, open_fmt, indent_level, "");
2903 if (mnode->type != NODE_TYPE_ROOT)
2906 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2908 if (mnode->action_name)
2909 g_string_append_printf (buffer, " action=\"%s\"",
2910 g_quark_to_string (mnode->action_name));
2913 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2915 for (child = node->children; child != NULL; child = child->next)
2916 print_node (self, child, indent_level + 2, buffer);
2919 g_string_append_printf (buffer, close_fmt, indent_level, "");
2923 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2924 GtkBuilder *builder,
2926 const gchar *tagname,
2927 GMarkupParser *parser,
2933 if (strcmp (tagname, "ui") == 0)
2937 ctx = g_new0 (ParseContext, 1);
2938 ctx->state = STATE_START;
2939 ctx->self = GTK_UI_MANAGER (buildable);
2940 ctx->current = NULL;
2941 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2944 *parser = ui_parser;
2954 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2955 GtkBuilder *builder,
2957 const gchar *tagname,
2960 queue_update (GTK_UI_MANAGER (buildable));
2961 g_object_notify (G_OBJECT (buildable), "ui");
2966 * gtk_ui_manager_get_ui:
2967 * @self: a #GtkUIManager
2969 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2971 * Return value: A newly allocated string containing an XML representation of
2977 gtk_ui_manager_get_ui (GtkUIManager *self)
2981 buffer = g_string_new (NULL);
2983 gtk_ui_manager_ensure_update (self);
2985 print_node (self, self->private_data->root_node, 0, buffer);
2987 return g_string_free (buffer, FALSE);
2992 #undef gtk_ui_manager_add_ui_from_file
2995 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2996 const gchar *filename,
2999 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
3002 if (utf8_filename == NULL)
3005 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
3007 g_free (utf8_filename);
3014 #define __GTK_UI_MANAGER_C__
3015 #include "gtkaliasdef.c"