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 g_return_if_fail (GTK_IS_UI_MANAGER (self));
700 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
701 g_return_if_fail (g_list_find (self->private_data->action_groups,
702 action_group) == NULL);
704 g_object_ref (action_group);
705 self->private_data->action_groups =
706 g_list_insert (self->private_data->action_groups, action_group, pos);
707 g_object_connect (action_group,
708 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
709 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
710 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
711 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
714 /* dirty all nodes, as action bindings may change */
715 dirty_all_nodes (self);
717 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
721 * gtk_ui_manager_remove_action_group:
722 * @self: a #GtkUIManager object
723 * @action_group: the action group to be removed
725 * Removes an action group from the list of action groups associated
731 gtk_ui_manager_remove_action_group (GtkUIManager *self,
732 GtkActionGroup *action_group)
734 g_return_if_fail (GTK_IS_UI_MANAGER (self));
735 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
736 g_return_if_fail (g_list_find (self->private_data->action_groups,
737 action_group) != NULL);
739 self->private_data->action_groups =
740 g_list_remove (self->private_data->action_groups, action_group);
742 g_object_disconnect (action_group,
743 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
744 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
745 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
746 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
748 g_object_unref (action_group);
750 /* dirty all nodes, as action bindings may change */
751 dirty_all_nodes (self);
753 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
757 * gtk_ui_manager_get_action_groups:
758 * @self: a #GtkUIManager object
760 * Returns the list of action groups associated with @self.
762 * Return value: a #GList of action groups. The list is owned by GTK+
763 * and should not be modified.
768 gtk_ui_manager_get_action_groups (GtkUIManager *self)
770 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
772 return self->private_data->action_groups;
776 * gtk_ui_manager_get_accel_group:
777 * @self: a #GtkUIManager object
779 * Returns the #GtkAccelGroup associated with @self.
781 * Return value: the #GtkAccelGroup.
786 gtk_ui_manager_get_accel_group (GtkUIManager *self)
788 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
790 return self->private_data->accel_group;
794 * gtk_ui_manager_get_widget:
795 * @self: a #GtkUIManager
798 * Looks up a widget by following a path.
799 * The path consists of the names specified in the XML description of the UI.
800 * separated by '/'. Elements which don't have a name or action attribute in
801 * the XML (e.g. <popup>) can be addressed by their XML element name
802 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
804 * Note that the widget found by following a path that ends in a <menu>
805 * element is the menuitem to which the menu is attached, not the menu itself.
807 * Also note that the widgets constructed by a ui manager are not tied to
808 * the lifecycle of the ui manager. If you add the widgets returned by this
809 * function to some container or explicitly ref them, they will survive the
810 * destruction of the ui manager.
812 * Return value: the widget found by following the path, or %NULL if no widget
818 gtk_ui_manager_get_widget (GtkUIManager *self,
821 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
822 g_return_val_if_fail (path != NULL, NULL);
824 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
828 GtkUIManagerItemType types;
833 collect_toplevels (GNode *node,
836 ToplevelData *data = user_data;
838 if (NODE_INFO (node)->proxy)
840 switch (NODE_INFO (node)->type)
842 case NODE_TYPE_MENUBAR:
843 if (data->types & GTK_UI_MANAGER_MENUBAR)
844 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
846 case NODE_TYPE_TOOLBAR:
847 if (data->types & GTK_UI_MANAGER_TOOLBAR)
848 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
850 case NODE_TYPE_POPUP:
851 if (data->types & GTK_UI_MANAGER_POPUP)
852 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
860 * gtk_ui_manager_get_toplevels:
861 * @self: a #GtkUIManager
862 * @types: specifies the types of toplevel widgets to include. Allowed
863 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
864 * #GTK_UI_MANAGER_POPUP.
866 * Obtains a list of all toplevel widgets of the requested types.
868 * Return value: a newly-allocated of all toplevel widgets of the requested
874 gtk_ui_manager_get_toplevels (GtkUIManager *self,
875 GtkUIManagerItemType types)
879 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
880 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
881 GTK_UI_MANAGER_TOOLBAR |
882 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
888 g_node_children_foreach (self->private_data->root_node,
890 collect_toplevels, &data);
897 * gtk_ui_manager_get_action:
898 * @self: a #GtkUIManager
901 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
902 * for more information about paths.
904 * Return value: the action whose proxy widget is found by following the path,
905 * or %NULL if no widget was found.
910 gtk_ui_manager_get_action (GtkUIManager *self,
913 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
914 g_return_val_if_fail (path != NULL, NULL);
916 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
920 get_child_node (GtkUIManager *self,
923 const gchar *childname,
924 gint childname_length,
935 for (child = parent->children; child != NULL; child = child->next)
937 if (NODE_INFO (child)->name &&
938 strlen (NODE_INFO (child)->name) == childname_length &&
939 !strncmp (NODE_INFO (child)->name, childname, childname_length))
941 /* if undecided about node type, set it */
942 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
943 NODE_INFO (child)->type = node_type;
945 /* warn about type mismatch */
946 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
947 node_type != NODE_TYPE_UNDECIDED &&
948 NODE_INFO (child)->type != node_type)
949 g_warning ("node type doesn't match %d (%s is type %d)",
951 NODE_INFO (child)->name,
952 NODE_INFO (child)->type);
958 if (!child && create)
962 mnode = g_slice_new0 (Node);
963 mnode->type = node_type;
964 mnode->name = g_strndup (childname, childname_length);
969 child = g_node_insert_before (parent, sibling,
972 child = g_node_insert_after (parent, sibling,
978 child = g_node_prepend_data (parent, mnode);
980 child = g_node_append_data (parent, mnode);
983 mark_node_dirty (child);
988 /* handle root node */
989 if (self->private_data->root_node)
991 child = self->private_data->root_node;
992 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
993 g_warning ("root node name '%s' doesn't match '%s'",
994 childname, NODE_INFO (child)->name);
995 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
996 g_warning ("base element must be of type ROOT");
1002 mnode = g_slice_new0 (Node);
1003 mnode->type = node_type;
1004 mnode->name = g_strndup (childname, childname_length);
1005 mnode->dirty = TRUE;
1007 child = self->private_data->root_node = g_node_new (mnode);
1015 get_node (GtkUIManager *self,
1020 const gchar *pos, *end;
1021 GNode *parent, *node;
1023 if (strncmp ("/ui", path, 3) == 0)
1026 end = path + strlen (path);
1028 parent = node = NULL;
1034 slash = strchr (pos, '/');
1036 length = slash - pos;
1038 length = strlen (pos);
1040 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1045 pos += length + 1; /* move past the node name and the slash too */
1049 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1050 NODE_INFO (node)->type = node_type;
1056 node_ui_reference_free (gpointer data)
1058 g_slice_free (NodeUIReference, data);
1062 free_node (GNode *node)
1064 Node *info = NODE_INFO (node);
1066 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1067 g_list_free (info->uifiles);
1070 g_object_unref (info->action);
1072 g_object_unref (info->proxy);
1074 g_object_unref (info->extra);
1075 g_free (info->name);
1076 g_slice_free (Node, info);
1082 * gtk_ui_manager_new_merge_id:
1083 * @self: a #GtkUIManager
1085 * Returns an unused merge id, suitable for use with
1086 * gtk_ui_manager_add_ui().
1088 * Return value: an unused merge id.
1093 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1095 self->private_data->last_merge_id++;
1097 return self->private_data->last_merge_id;
1101 node_prepend_ui_reference (GNode *gnode,
1103 GQuark action_quark)
1105 Node *node = NODE_INFO (gnode);
1106 NodeUIReference *reference = NULL;
1108 if (node->uifiles &&
1109 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1110 reference = node->uifiles->data;
1113 reference = g_slice_new (NodeUIReference);
1114 node->uifiles = g_list_prepend (node->uifiles, reference);
1117 reference->merge_id = merge_id;
1118 reference->action_quark = action_quark;
1120 mark_node_dirty (gnode);
1124 node_remove_ui_reference (GNode *gnode,
1127 Node *node = NODE_INFO (gnode);
1130 for (p = node->uifiles; p != NULL; p = p->next)
1132 NodeUIReference *reference = p->data;
1134 if (reference->merge_id == merge_id)
1136 if (p == node->uifiles)
1137 mark_node_dirty (gnode);
1138 node->uifiles = g_list_delete_link (node->uifiles, p);
1139 g_slice_free (NodeUIReference, reference);
1146 /* -------------------- The UI file parser -------------------- */
1160 typedef struct _ParseContext ParseContext;
1161 struct _ParseContext
1164 ParseState prev_state;
1174 start_element_handler (GMarkupParseContext *context,
1175 const gchar *element_name,
1176 const gchar **attribute_names,
1177 const gchar **attribute_values,
1181 ParseContext *ctx = user_data;
1182 GtkUIManager *self = ctx->self;
1185 const gchar *node_name;
1186 const gchar *action;
1187 GQuark action_quark;
1189 gboolean expand = FALSE;
1191 gboolean raise_error = TRUE;
1198 for (i = 0; attribute_names[i] != NULL; i++)
1200 if (!strcmp (attribute_names[i], "name"))
1202 node_name = attribute_values[i];
1204 else if (!strcmp (attribute_names[i], "action"))
1206 action = attribute_values[i];
1207 action_quark = g_quark_from_string (attribute_values[i]);
1209 else if (!strcmp (attribute_names[i], "position"))
1211 top = !strcmp (attribute_values[i], "top");
1213 else if (!strcmp (attribute_names[i], "expand"))
1215 expand = !strcmp (attribute_values[i], "true");
1219 gint line_number, char_number;
1221 g_markup_parse_context_get_position (context,
1222 &line_number, &char_number);
1225 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1226 _("Unknown attribute '%s' on line %d char %d"),
1228 line_number, char_number);
1233 /* Work out a name for this node. Either the name attribute, or
1234 * the action, or the element name */
1235 if (node_name == NULL)
1240 node_name = element_name;
1243 switch (element_name[0])
1246 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1248 ctx->state = STATE_ACCELERATOR;
1249 ctx->current = get_child_node (self, ctx->current, NULL,
1250 node_name, strlen (node_name),
1251 NODE_TYPE_ACCELERATOR,
1253 if (NODE_INFO (ctx->current)->action_name == 0)
1254 NODE_INFO (ctx->current)->action_name = action_quark;
1256 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1258 raise_error = FALSE;
1262 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1264 ctx->state = STATE_ROOT;
1265 ctx->current = self->private_data->root_node;
1266 raise_error = FALSE;
1268 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1272 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1274 ctx->state = STATE_MENU;
1275 ctx->current = get_child_node (self, ctx->current, NULL,
1276 node_name, strlen (node_name),
1279 if (NODE_INFO (ctx->current)->action_name == 0)
1280 NODE_INFO (ctx->current)->action_name = action_quark;
1282 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1283 mark_node_dirty (ctx->current);
1285 raise_error = FALSE;
1287 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1289 ctx->current = get_child_node (self, ctx->current, NULL,
1290 node_name, strlen (node_name),
1293 if (NODE_INFO (ctx->current)->action_name == 0)
1294 NODE_INFO (ctx->current)->action_name = action_quark;
1296 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1298 raise_error = FALSE;
1300 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1302 ctx->state = STATE_MENU;
1304 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1305 node_name, strlen (node_name),
1308 if (NODE_INFO (ctx->current)->action_name == 0)
1309 NODE_INFO (ctx->current)->action_name = action_quark;
1311 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1313 raise_error = FALSE;
1315 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1319 ctx->state = STATE_MENUITEM;
1320 node = get_child_node (self, ctx->current, NULL,
1321 node_name, strlen (node_name),
1324 if (NODE_INFO (node)->action_name == 0)
1325 NODE_INFO (node)->action_name = action_quark;
1327 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1329 raise_error = FALSE;
1333 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1335 ctx->state = STATE_MENU;
1336 ctx->current = get_child_node (self, ctx->current, NULL,
1337 node_name, strlen (node_name),
1340 if (NODE_INFO (ctx->current)->action_name == 0)
1341 NODE_INFO (ctx->current)->action_name = action_quark;
1343 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1345 raise_error = FALSE;
1347 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1348 !strcmp (element_name, "placeholder"))
1350 if (ctx->state == STATE_TOOLBAR)
1351 ctx->current = get_child_node (self, ctx->current, NULL,
1352 node_name, strlen (node_name),
1353 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1356 ctx->current = get_child_node (self, ctx->current, NULL,
1357 node_name, strlen (node_name),
1358 NODE_TYPE_MENU_PLACEHOLDER,
1361 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1363 raise_error = FALSE;
1367 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1368 !strcmp (element_name, "separator"))
1373 if (ctx->state == STATE_TOOLBAR)
1374 ctx->state = STATE_TOOLITEM;
1376 ctx->state = STATE_MENUITEM;
1377 if (!strcmp (node_name, "separator"))
1383 length = strlen (node_name);
1384 node = get_child_node (self, ctx->current, NULL,
1386 NODE_TYPE_SEPARATOR,
1389 NODE_INFO (node)->expand = expand;
1391 if (NODE_INFO (node)->action_name == 0)
1392 NODE_INFO (node)->action_name = action_quark;
1394 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1396 raise_error = FALSE;
1400 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1402 ctx->state = STATE_TOOLBAR;
1403 ctx->current = get_child_node (self, ctx->current, NULL,
1404 node_name, strlen (node_name),
1407 if (NODE_INFO (ctx->current)->action_name == 0)
1408 NODE_INFO (ctx->current)->action_name = action_quark;
1410 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1412 raise_error = FALSE;
1414 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1418 ctx->state = STATE_TOOLITEM;
1419 node = get_child_node (self, ctx->current, NULL,
1420 node_name, strlen (node_name),
1423 if (NODE_INFO (node)->action_name == 0)
1424 NODE_INFO (node)->action_name = action_quark;
1426 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1428 raise_error = FALSE;
1436 gint line_number, char_number;
1438 g_markup_parse_context_get_position (context,
1439 &line_number, &char_number);
1442 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1443 _("Unexpected start tag '%s' on line %d char %d"),
1445 line_number, char_number);
1450 end_element_handler (GMarkupParseContext *context,
1451 const gchar *element_name,
1455 ParseContext *ctx = user_data;
1461 /* no need to GError here, GMarkup already catches this */
1464 ctx->current = NULL;
1465 ctx->state = STATE_END;
1469 case STATE_ACCELERATOR:
1470 ctx->current = ctx->current->parent;
1471 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1472 ctx->state = STATE_ROOT;
1473 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1475 ctx->current = ctx->current->parent;
1476 ctx->state = STATE_TOOLITEM;
1478 /* else, stay in same state */
1480 case STATE_MENUITEM:
1481 ctx->state = STATE_MENU;
1483 case STATE_TOOLITEM:
1484 ctx->state = STATE_TOOLBAR;
1490 cleanup (GMarkupParseContext *context,
1494 ParseContext *ctx = user_data;
1496 ctx->current = NULL;
1497 /* should also walk through the tree and get rid of nodes related to
1498 * this UI file's tag */
1500 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1504 xml_isspace (char c)
1506 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1510 text_handler (GMarkupParseContext *context,
1520 end = text + text_len;
1521 while (p != end && xml_isspace (*p))
1526 gint line_number, char_number;
1528 g_markup_parse_context_get_position (context,
1529 &line_number, &char_number);
1532 G_MARKUP_ERROR_INVALID_CONTENT,
1533 _("Unexpected character data on line %d char %d"),
1534 line_number, char_number);
1539 static const GMarkupParser ui_parser = {
1540 start_element_handler,
1541 end_element_handler,
1548 add_ui_from_string (GtkUIManager *self,
1549 const gchar *buffer,
1551 gboolean needs_root,
1554 ParseContext ctx = { 0 };
1555 GMarkupParseContext *context;
1557 ctx.state = STATE_START;
1560 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1562 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1565 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1568 if (!g_markup_parse_context_parse (context, buffer, length, error))
1572 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1575 if (!g_markup_parse_context_end_parse (context, error))
1578 g_markup_parse_context_free (context);
1580 queue_update (self);
1582 g_object_notify (G_OBJECT (self), "ui");
1584 return ctx.merge_id;
1588 g_markup_parse_context_free (context);
1594 * gtk_ui_manager_add_ui_from_string:
1595 * @self: a #GtkUIManager object
1596 * @buffer: the string to parse
1597 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1598 * @error: return location for an error
1600 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1601 * merges it with the current contents of @self. An enclosing <ui>
1602 * element is added if it is missing.
1604 * Return value: The merge id for the merged UI. The merge id can be used
1605 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1606 * the return value is 0.
1611 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1612 const gchar *buffer,
1616 gboolean needs_root = TRUE;
1620 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1621 g_return_val_if_fail (buffer != NULL, 0);
1624 length = strlen (buffer);
1627 end = buffer + length;
1628 while (p != end && xml_isspace (*p))
1631 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1634 return add_ui_from_string (self, buffer, length, needs_root, error);
1638 * gtk_ui_manager_add_ui_from_file:
1639 * @self: a #GtkUIManager object
1640 * @filename: the name of the file to parse
1641 * @error: return location for an error
1643 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1644 * merges it with the current contents of @self.
1646 * Return value: The merge id for the merged UI. The merge id can be used
1647 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1648 * the return value is 0.
1653 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1654 const gchar *filename,
1661 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1663 if (!g_file_get_contents (filename, &buffer, &length, error))
1666 res = add_ui_from_string (self, buffer, length, FALSE, error);
1673 * gtk_ui_manager_add_ui:
1674 * @self: a #GtkUIManager
1675 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1677 * @name: the name for the added UI element
1678 * @action: the name of the action to be proxied, or %NULL to add a separator
1679 * @type: the type of UI element to add.
1680 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1681 * is added after its siblings.
1683 * Adds a UI element to the current contents of @self.
1685 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1686 * separator if such an element can be inserted at the place determined by
1687 * @path. Otherwise @type must indicate an element that can be inserted at
1688 * the place determined by @path.
1690 * If @path points to a menuitem or toolitem, the new element will be inserted
1691 * before or after this item, depending on @top.
1696 gtk_ui_manager_add_ui (GtkUIManager *self,
1700 const gchar *action,
1701 GtkUIManagerItemType type,
1708 GQuark action_quark = 0;
1710 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1711 g_return_if_fail (merge_id > 0);
1712 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1714 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1720 node_type = NODE_TYPE_UNDECIDED;
1723 switch (NODE_INFO (node)->type)
1725 case NODE_TYPE_SEPARATOR:
1726 case NODE_TYPE_MENUITEM:
1727 case NODE_TYPE_TOOLITEM:
1729 node = node->parent;
1731 case NODE_TYPE_MENUBAR:
1732 case NODE_TYPE_MENU:
1733 case NODE_TYPE_POPUP:
1734 case NODE_TYPE_MENU_PLACEHOLDER:
1737 case GTK_UI_MANAGER_AUTO:
1739 node_type = NODE_TYPE_MENUITEM;
1741 node_type = NODE_TYPE_SEPARATOR;
1743 case GTK_UI_MANAGER_MENU:
1744 node_type = NODE_TYPE_MENU;
1746 case GTK_UI_MANAGER_MENUITEM:
1747 node_type = NODE_TYPE_MENUITEM;
1749 case GTK_UI_MANAGER_SEPARATOR:
1750 node_type = NODE_TYPE_SEPARATOR;
1752 case GTK_UI_MANAGER_PLACEHOLDER:
1753 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1759 case NODE_TYPE_TOOLBAR:
1760 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1763 case GTK_UI_MANAGER_AUTO:
1765 node_type = NODE_TYPE_TOOLITEM;
1767 node_type = NODE_TYPE_SEPARATOR;
1769 case GTK_UI_MANAGER_TOOLITEM:
1770 node_type = NODE_TYPE_TOOLITEM;
1772 case GTK_UI_MANAGER_SEPARATOR:
1773 node_type = NODE_TYPE_SEPARATOR;
1775 case GTK_UI_MANAGER_PLACEHOLDER:
1776 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1782 case NODE_TYPE_ROOT:
1785 case GTK_UI_MANAGER_MENUBAR:
1786 node_type = NODE_TYPE_MENUBAR;
1788 case GTK_UI_MANAGER_TOOLBAR:
1789 node_type = NODE_TYPE_TOOLBAR;
1791 case GTK_UI_MANAGER_POPUP:
1792 node_type = NODE_TYPE_POPUP;
1794 case GTK_UI_MANAGER_ACCELERATOR:
1795 node_type = NODE_TYPE_ACCELERATOR;
1805 if (node_type == NODE_TYPE_UNDECIDED)
1807 g_warning ("item type %d not suitable for adding at '%s'",
1812 child = get_child_node (self, node, sibling,
1813 name, name ? strlen (name) : 0,
1814 node_type, TRUE, top);
1817 action_quark = g_quark_from_string (action);
1819 node_prepend_ui_reference (child, merge_id, action_quark);
1821 if (NODE_INFO (child)->action_name == 0)
1822 NODE_INFO (child)->action_name = action_quark;
1824 queue_update (self);
1826 g_object_notify (G_OBJECT (self), "ui");
1830 remove_ui (GNode *node,
1833 guint merge_id = GPOINTER_TO_UINT (user_data);
1835 node_remove_ui_reference (node, merge_id);
1837 return FALSE; /* continue */
1841 * gtk_ui_manager_remove_ui:
1842 * @self: a #GtkUIManager object
1843 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1845 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1850 gtk_ui_manager_remove_ui (GtkUIManager *self,
1853 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1855 g_node_traverse (self->private_data->root_node,
1856 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1857 remove_ui, GUINT_TO_POINTER (merge_id));
1859 queue_update (self);
1861 g_object_notify (G_OBJECT (self), "ui");
1864 /* -------------------- Updates -------------------- */
1868 get_action_by_name (GtkUIManager *merge,
1869 const gchar *action_name)
1877 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1879 GtkActionGroup *action_group = tmp->data;
1882 action = gtk_action_group_get_action (action_group, action_name);
1892 find_menu_position (GNode *node,
1893 GtkWidget **menushell_p,
1896 GtkWidget *menushell;
1899 g_return_val_if_fail (node != NULL, FALSE);
1900 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1901 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1902 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1903 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1904 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1907 /* first sibling -- look at parent */
1908 if (node->prev == NULL)
1913 parent = node->parent;
1914 switch (NODE_INFO (parent)->type)
1916 case NODE_TYPE_MENUBAR:
1917 case NODE_TYPE_POPUP:
1918 menushell = NODE_INFO (parent)->proxy;
1921 case NODE_TYPE_MENU:
1922 menushell = NODE_INFO (parent)->proxy;
1923 if (GTK_IS_MENU_ITEM (menushell))
1924 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1925 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1926 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1930 g_list_free (siblings);
1932 case NODE_TYPE_MENU_PLACEHOLDER:
1933 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1934 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1935 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1936 NODE_INFO (parent)->proxy) + 1;
1939 g_warning ("%s: bad parent node type %d", G_STRLOC,
1940 NODE_INFO (parent)->type);
1946 GtkWidget *prev_child;
1949 sibling = node->prev;
1950 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1951 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1953 prev_child = NODE_INFO (sibling)->proxy;
1955 if (!GTK_IS_WIDGET (prev_child))
1958 menushell = gtk_widget_get_parent (prev_child);
1959 if (!GTK_IS_MENU_SHELL (menushell))
1962 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1966 *menushell_p = menushell;
1974 find_toolbar_position (GNode *node,
1975 GtkWidget **toolbar_p,
1981 g_return_val_if_fail (node != NULL, FALSE);
1982 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1983 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1984 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1985 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1988 /* first sibling -- look at parent */
1989 if (node->prev == NULL)
1993 parent = node->parent;
1994 switch (NODE_INFO (parent)->type)
1996 case NODE_TYPE_TOOLBAR:
1997 toolbar = NODE_INFO (parent)->proxy;
2000 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2001 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2002 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2003 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2004 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2007 g_warning ("%s: bad parent node type %d", G_STRLOC,
2008 NODE_INFO (parent)->type);
2014 GtkWidget *prev_child;
2017 sibling = node->prev;
2018 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2019 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2021 prev_child = NODE_INFO (sibling)->proxy;
2023 if (!GTK_IS_WIDGET (prev_child))
2026 toolbar = gtk_widget_get_parent (prev_child);
2027 if (!GTK_IS_TOOLBAR (toolbar))
2030 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2031 GTK_TOOL_ITEM (prev_child)) + 1;
2035 *toolbar_p = toolbar;
2043 * _gtk_menu_is_empty:
2044 * @menu: a #GtkMenu or %NULL
2046 * Determines whether @menu is empty. A menu is considered empty if it
2047 * the only visible children are tearoff menu items or "filler" menu
2048 * items which were inserted to mark the menu as empty.
2050 * This function is used by #GtkAction.
2052 * Return value: whether @menu is empty.
2055 _gtk_menu_is_empty (GtkWidget *menu)
2057 GList *children, *cur;
2058 gboolean result = TRUE;
2060 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2065 children = gtk_container_get_children (GTK_CONTAINER (menu));
2070 if (GTK_WIDGET_VISIBLE (cur->data))
2072 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2073 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2081 g_list_free (children);
2087 SEPARATOR_MODE_SMART,
2088 SEPARATOR_MODE_VISIBLE,
2089 SEPARATOR_MODE_HIDDEN
2093 update_smart_separators (GtkWidget *proxy)
2095 GtkWidget *parent = NULL;
2097 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2099 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2100 parent = gtk_widget_get_parent (proxy);
2106 GList *children, *cur, *last;
2109 children = gtk_container_get_children (GTK_CONTAINER (parent));
2119 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2123 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2124 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2127 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2128 "gtk-separator-mode"));
2131 case SEPARATOR_MODE_VISIBLE:
2132 gtk_widget_show (GTK_WIDGET (cur->data));
2136 case SEPARATOR_MODE_HIDDEN:
2137 gtk_widget_hide (GTK_WIDGET (cur->data));
2139 case SEPARATOR_MODE_SMART:
2142 gtk_widget_show (GTK_WIDGET (cur->data));
2147 gtk_widget_hide (GTK_WIDGET (cur->data));
2151 else if (GTK_WIDGET_VISIBLE (cur->data))
2154 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2167 gtk_widget_hide (GTK_WIDGET (last->data));
2169 if (GTK_IS_MENU (parent))
2173 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2174 if (GTK_IS_MENU_ITEM (item))
2175 _gtk_action_sync_menu_visible (NULL, item, empty);
2176 if (GTK_IS_WIDGET (filler))
2179 gtk_widget_show (filler);
2181 gtk_widget_hide (filler);
2185 g_list_free (children);
2190 update_node (GtkUIManager *self,
2197 const gchar *action_name;
2198 NodeUIReference *ref;
2200 #ifdef DEBUG_UI_MANAGER
2204 g_return_if_fail (node != NULL);
2205 g_return_if_fail (NODE_INFO (node) != NULL);
2207 info = NODE_INFO (node);
2212 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2214 #ifdef DEBUG_UI_MANAGER
2215 g_print ("update_node name=%s dirty=%d popup %d (",
2216 info->name, info->dirty, in_popup);
2217 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2219 NodeUIReference *ref = tmp->data;
2220 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2227 if (info->uifiles == NULL) {
2228 /* We may need to remove this node.
2229 * This must be done in post order
2231 goto recurse_children;
2234 ref = info->uifiles->data;
2235 action_name = g_quark_to_string (ref->action_quark);
2236 action = get_action_by_name (self, action_name);
2238 info->dirty = FALSE;
2240 /* Check if the node doesn't have an action and must have an action */
2241 if (action == NULL &&
2242 info->type != NODE_TYPE_ROOT &&
2243 info->type != NODE_TYPE_MENUBAR &&
2244 info->type != NODE_TYPE_TOOLBAR &&
2245 info->type != NODE_TYPE_POPUP &&
2246 info->type != NODE_TYPE_SEPARATOR &&
2247 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2248 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2250 g_warning ("%s: missing action %s", info->name, action_name);
2256 gtk_action_set_accel_group (action, self->private_data->accel_group);
2258 /* If the widget already has a proxy and the action hasn't changed, then
2259 * we only have to update the tearoff menu items.
2261 if (info->proxy != NULL && action == info->action)
2263 if (info->type == NODE_TYPE_MENU)
2268 if (GTK_IS_MENU (info->proxy))
2271 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2272 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2273 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2275 if (self->private_data->add_tearoffs && !in_popup)
2276 gtk_widget_show (GTK_WIDGET (siblings->data));
2278 gtk_widget_hide (GTK_WIDGET (siblings->data));
2280 g_list_free (siblings);
2283 goto recurse_children;
2288 case NODE_TYPE_MENUBAR:
2289 if (info->proxy == NULL)
2291 info->proxy = gtk_menu_bar_new ();
2292 g_object_ref_sink (info->proxy);
2293 gtk_widget_set_name (info->proxy, info->name);
2294 gtk_widget_show (info->proxy);
2295 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2298 case NODE_TYPE_POPUP:
2299 if (info->proxy == NULL)
2301 info->proxy = gtk_menu_new ();
2302 g_object_ref_sink (info->proxy);
2304 gtk_widget_set_name (info->proxy, info->name);
2306 case NODE_TYPE_MENU:
2308 GtkWidget *prev_submenu = NULL;
2309 GtkWidget *menu = NULL;
2312 /* remove the proxy if it is of the wrong type ... */
2314 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2316 if (GTK_IS_MENU_ITEM (info->proxy))
2318 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2321 g_object_ref (prev_submenu);
2322 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2326 gtk_action_disconnect_proxy (info->action, info->proxy);
2327 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2329 g_object_unref (info->proxy);
2333 /* create proxy if needed ... */
2334 if (info->proxy == NULL)
2336 /* ... if the action already provides a menu, then use
2337 * that menu instead of creating an empty one
2339 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2340 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2341 GTK_ACTION_GET_CLASS (action)->create_menu)
2343 menu = gtk_action_create_menu (action);
2351 menu = gtk_menu_new ();
2352 gtk_widget_set_name (menu, info->name);
2353 tearoff = gtk_tearoff_menu_item_new ();
2354 gtk_widget_set_no_show_all (tearoff, TRUE);
2355 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2356 filler = gtk_menu_item_new_with_label (_("Empty"));
2357 g_object_set_data (G_OBJECT (filler),
2358 I_("gtk-empty-menu-item"),
2359 GINT_TO_POINTER (TRUE));
2360 gtk_widget_set_sensitive (filler, FALSE);
2361 gtk_widget_set_no_show_all (filler, TRUE);
2362 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2365 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2368 g_object_ref_sink (info->proxy);
2369 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2374 GtkWidget *menushell;
2377 if (find_menu_position (node, &menushell, &pos))
2379 info->proxy = gtk_action_create_menu_item (action);
2380 g_object_ref_sink (info->proxy);
2381 g_signal_connect (info->proxy, "notify::visible",
2382 G_CALLBACK (update_smart_separators), NULL);
2383 gtk_widget_set_name (info->proxy, info->name);
2385 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2386 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2391 gtk_action_connect_proxy (action, info->proxy);
2395 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2397 g_object_unref (prev_submenu);
2400 if (GTK_IS_MENU (info->proxy))
2403 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2405 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2406 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2408 if (self->private_data->add_tearoffs && !in_popup)
2409 gtk_widget_show (GTK_WIDGET (siblings->data));
2411 gtk_widget_hide (GTK_WIDGET (siblings->data));
2413 g_list_free (siblings);
2416 case NODE_TYPE_UNDECIDED:
2417 g_warning ("found undecided node!");
2419 case NODE_TYPE_ROOT:
2421 case NODE_TYPE_TOOLBAR:
2422 if (info->proxy == NULL)
2424 info->proxy = gtk_toolbar_new ();
2425 g_object_ref_sink (info->proxy);
2426 gtk_widget_set_name (info->proxy, info->name);
2427 gtk_widget_show (info->proxy);
2428 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2431 case NODE_TYPE_MENU_PLACEHOLDER:
2432 /* create menu items for placeholders if necessary ... */
2433 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2434 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2438 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2440 g_object_unref (info->proxy);
2445 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2447 g_object_unref (info->extra);
2451 if (info->proxy == NULL)
2453 GtkWidget *menushell;
2456 if (find_menu_position (node, &menushell, &pos))
2458 info->proxy = gtk_separator_menu_item_new ();
2459 g_object_ref_sink (info->proxy);
2460 g_object_set_data (G_OBJECT (info->proxy),
2461 I_("gtk-separator-mode"),
2462 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2463 gtk_widget_set_no_show_all (info->proxy, TRUE);
2464 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2465 NODE_INFO (node)->proxy, pos);
2467 info->extra = gtk_separator_menu_item_new ();
2468 g_object_ref_sink (info->extra);
2469 g_object_set_data (G_OBJECT (info->extra),
2470 I_("gtk-separator-mode"),
2471 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2472 gtk_widget_set_no_show_all (info->extra, TRUE);
2473 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2474 NODE_INFO (node)->extra, pos + 1);
2478 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2479 /* create toolbar items for placeholders if necessary ... */
2480 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2481 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2485 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2487 g_object_unref (info->proxy);
2492 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2494 g_object_unref (info->extra);
2498 if (info->proxy == NULL)
2504 if (find_toolbar_position (node, &toolbar, &pos))
2506 item = gtk_separator_tool_item_new ();
2507 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2508 info->proxy = GTK_WIDGET (item);
2509 g_object_ref_sink (info->proxy);
2510 g_object_set_data (G_OBJECT (info->proxy),
2511 I_("gtk-separator-mode"),
2512 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2513 gtk_widget_set_no_show_all (info->proxy, TRUE);
2515 item = gtk_separator_tool_item_new ();
2516 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2517 info->extra = GTK_WIDGET (item);
2518 g_object_ref_sink (info->extra);
2519 g_object_set_data (G_OBJECT (info->extra),
2520 I_("gtk-separator-mode"),
2521 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2522 gtk_widget_set_no_show_all (info->extra, TRUE);
2526 case NODE_TYPE_MENUITEM:
2527 /* remove the proxy if it is of the wrong type ... */
2529 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2531 g_signal_handlers_disconnect_by_func (info->proxy,
2532 G_CALLBACK (update_smart_separators),
2534 gtk_action_disconnect_proxy (info->action, info->proxy);
2535 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2537 g_object_unref (info->proxy);
2540 /* create proxy if needed ... */
2541 if (info->proxy == NULL)
2543 GtkWidget *menushell;
2546 if (find_menu_position (node, &menushell, &pos))
2548 info->proxy = gtk_action_create_menu_item (action);
2549 g_object_ref_sink (info->proxy);
2550 gtk_widget_set_name (info->proxy, info->name);
2552 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2558 g_signal_handlers_disconnect_by_func (info->proxy,
2559 G_CALLBACK (update_smart_separators),
2561 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2562 gtk_action_connect_proxy (action, info->proxy);
2567 g_signal_connect (info->proxy, "notify::visible",
2568 G_CALLBACK (update_smart_separators), NULL);
2571 /* don't show accels in popups */
2572 GtkWidget *label = GTK_BIN (info->proxy)->child;
2573 g_object_set (label, "accel-closure", NULL, NULL);
2578 case NODE_TYPE_TOOLITEM:
2579 /* remove the proxy if it is of the wrong type ... */
2581 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2583 g_signal_handlers_disconnect_by_func (info->proxy,
2584 G_CALLBACK (update_smart_separators),
2586 gtk_action_disconnect_proxy (info->action, info->proxy);
2587 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2589 g_object_unref (info->proxy);
2592 /* create proxy if needed ... */
2593 if (info->proxy == NULL)
2598 if (find_toolbar_position (node, &toolbar, &pos))
2600 info->proxy = gtk_action_create_tool_item (action);
2601 g_object_ref_sink (info->proxy);
2602 gtk_widget_set_name (info->proxy, info->name);
2604 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2605 GTK_TOOL_ITEM (info->proxy), pos);
2610 g_signal_handlers_disconnect_by_func (info->proxy,
2611 G_CALLBACK (update_smart_separators),
2613 gtk_action_connect_proxy (action, info->proxy);
2618 g_signal_connect (info->proxy, "notify::visible",
2619 G_CALLBACK (update_smart_separators), NULL);
2622 case NODE_TYPE_SEPARATOR:
2623 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2624 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2628 gint separator_mode;
2631 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2633 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2635 g_object_unref (info->proxy);
2639 if (find_toolbar_position (node, &toolbar, &pos))
2641 item = gtk_separator_tool_item_new ();
2642 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2643 info->proxy = GTK_WIDGET (item);
2644 g_object_ref_sink (info->proxy);
2645 gtk_widget_set_no_show_all (info->proxy, TRUE);
2648 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2649 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2650 separator_mode = SEPARATOR_MODE_VISIBLE;
2653 separator_mode = SEPARATOR_MODE_SMART;
2655 g_object_set_data (G_OBJECT (info->proxy),
2656 I_("gtk-separator-mode"),
2657 GINT_TO_POINTER (separator_mode));
2658 gtk_widget_show (info->proxy);
2663 GtkWidget *menushell;
2666 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2668 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2670 g_object_unref (info->proxy);
2674 if (find_menu_position (node, &menushell, &pos))
2676 info->proxy = gtk_separator_menu_item_new ();
2677 g_object_ref_sink (info->proxy);
2678 gtk_widget_set_no_show_all (info->proxy, TRUE);
2679 g_object_set_data (G_OBJECT (info->proxy),
2680 I_("gtk-separator-mode"),
2681 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2682 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2684 gtk_widget_show (info->proxy);
2688 case NODE_TYPE_ACCELERATOR:
2689 gtk_action_connect_accelerator (action);
2694 g_object_ref (action);
2696 g_object_unref (info->action);
2697 info->action = action;
2700 /* process children */
2701 child = node->children;
2707 child = current->next;
2708 update_node (self, current, in_popup);
2713 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2714 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2715 else if (info->type == NODE_TYPE_MENU ||
2716 info->type == NODE_TYPE_TOOLBAR ||
2717 info->type == NODE_TYPE_POPUP)
2718 update_smart_separators (info->proxy);
2721 /* handle cleanup of dead nodes */
2722 if (node->children == NULL && info->uifiles == NULL)
2725 gtk_widget_destroy (info->proxy);
2727 gtk_widget_destroy (info->extra);
2728 if (info->type == NODE_TYPE_ACCELERATOR)
2729 gtk_action_disconnect_accelerator (info->action);
2731 g_node_destroy (node);
2736 do_updates (GtkUIManager *self)
2738 /* this function needs to check through the tree for dirty nodes.
2739 * For such nodes, it needs to do the following:
2741 * 1) check if they are referenced by any loaded UI files anymore.
2742 * In which case, the proxy widget should be destroyed, unless
2743 * there are any subnodes.
2745 * 2) lookup the action for this node again. If it is different to
2746 * the current one (or if no previous action has been looked up),
2747 * the proxy is reconnected to the new action (or a new proxy widget
2748 * is created and added to the parent container).
2750 update_node (self, self->private_data->root_node, FALSE);
2752 self->private_data->update_tag = 0;
2758 do_updates_idle (GtkUIManager *self)
2766 queue_update (GtkUIManager *self)
2768 if (self->private_data->update_tag != 0)
2771 self->private_data->update_tag = gdk_threads_add_idle (
2772 (GSourceFunc)do_updates_idle,
2778 * gtk_ui_manager_ensure_update:
2779 * @self: a #GtkUIManager
2781 * Makes sure that all pending updates to the UI have been completed.
2783 * This may occasionally be necessary, since #GtkUIManager updates the
2784 * UI in an idle function. A typical example where this function is
2785 * useful is to enforce that the menubar and toolbar have been added to
2786 * the main window before showing it:
2788 * gtk_container_add (GTK_CONTAINER (window), vbox);
2789 * g_signal_connect (merge, "add_widget",
2790 * G_CALLBACK (add_widget), vbox);
2791 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2792 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2793 * gtk_ui_manager_ensure_update (merge);
2794 * gtk_widget_show (window);
2800 gtk_ui_manager_ensure_update (GtkUIManager *self)
2802 if (self->private_data->update_tag != 0)
2804 g_source_remove (self->private_data->update_tag);
2810 dirty_traverse_func (GNode *node,
2813 NODE_INFO (node)->dirty = TRUE;
2818 dirty_all_nodes (GtkUIManager *self)
2820 g_node_traverse (self->private_data->root_node,
2821 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2822 dirty_traverse_func, NULL);
2823 queue_update (self);
2827 mark_node_dirty (GNode *node)
2831 /* FIXME could optimize this */
2832 for (p = node; p; p = p->parent)
2833 NODE_INFO (p)->dirty = TRUE;
2836 static const gchar *
2837 open_tag_format (NodeType type)
2841 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2842 case NODE_TYPE_ROOT: return "%*s<ui";
2843 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2844 case NODE_TYPE_MENU: return "%*s<menu";
2845 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2846 case NODE_TYPE_MENU_PLACEHOLDER:
2847 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2848 case NODE_TYPE_POPUP: return "%*s<popup";
2849 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2850 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2851 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2852 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2853 default: return NULL;
2857 static const gchar *
2858 close_tag_format (NodeType type)
2862 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2863 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2864 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2865 case NODE_TYPE_MENU: return "%*s</menu>\n";
2866 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2867 case NODE_TYPE_MENU_PLACEHOLDER:
2868 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2869 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2870 default: return NULL;
2875 print_node (GtkUIManager *self,
2882 const gchar *open_fmt;
2883 const gchar *close_fmt;
2887 open_fmt = open_tag_format (mnode->type);
2888 close_fmt = close_tag_format (mnode->type);
2890 g_string_append_printf (buffer, open_fmt, indent_level, "");
2892 if (mnode->type != NODE_TYPE_ROOT)
2895 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2897 if (mnode->action_name)
2898 g_string_append_printf (buffer, " action=\"%s\"",
2899 g_quark_to_string (mnode->action_name));
2902 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2904 for (child = node->children; child != NULL; child = child->next)
2905 print_node (self, child, indent_level + 2, buffer);
2908 g_string_append_printf (buffer, close_fmt, indent_level, "");
2912 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2913 GtkBuilder *builder,
2915 const gchar *tagname,
2916 GMarkupParser *parser,
2922 if (strcmp (tagname, "ui") == 0)
2926 ctx = g_new0 (ParseContext, 1);
2927 ctx->state = STATE_START;
2928 ctx->self = GTK_UI_MANAGER (buildable);
2929 ctx->current = NULL;
2930 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2933 *parser = ui_parser;
2943 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2944 GtkBuilder *builder,
2946 const gchar *tagname,
2949 queue_update (GTK_UI_MANAGER (buildable));
2950 g_object_notify (G_OBJECT (buildable), "ui");
2955 * gtk_ui_manager_get_ui:
2956 * @self: a #GtkUIManager
2958 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2960 * Return value: A newly allocated string containing an XML representation of
2966 gtk_ui_manager_get_ui (GtkUIManager *self)
2970 buffer = g_string_new (NULL);
2972 gtk_ui_manager_ensure_update (self);
2974 print_node (self, self->private_data->root_node, 0, buffer);
2976 return g_string_free (buffer, FALSE);
2981 #undef gtk_ui_manager_add_ui_from_file
2984 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2985 const gchar *filename,
2988 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2991 if (utf8_filename == NULL)
2994 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2996 g_free (utf8_filename);
3003 #define __GTK_UI_MANAGER_C__
3004 #include "gtkaliasdef.c"