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 (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 (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_node_traverse (self->private_data->root_node,
1854 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1855 remove_ui, GUINT_TO_POINTER (merge_id));
1857 queue_update (self);
1859 g_object_notify (G_OBJECT (self), "ui");
1862 /* -------------------- Updates -------------------- */
1866 get_action_by_name (GtkUIManager *merge,
1867 const gchar *action_name)
1875 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1877 GtkActionGroup *action_group = tmp->data;
1880 action = gtk_action_group_get_action (action_group, action_name);
1890 find_menu_position (GNode *node,
1891 GtkWidget **menushell_p,
1894 GtkWidget *menushell;
1897 g_return_val_if_fail (node != NULL, FALSE);
1898 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1899 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1900 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1901 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1902 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1905 /* first sibling -- look at parent */
1906 if (node->prev == NULL)
1911 parent = node->parent;
1912 switch (NODE_INFO (parent)->type)
1914 case NODE_TYPE_MENUBAR:
1915 case NODE_TYPE_POPUP:
1916 menushell = NODE_INFO (parent)->proxy;
1919 case NODE_TYPE_MENU:
1920 menushell = NODE_INFO (parent)->proxy;
1921 if (GTK_IS_MENU_ITEM (menushell))
1922 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1923 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1924 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1928 g_list_free (siblings);
1930 case NODE_TYPE_MENU_PLACEHOLDER:
1931 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1932 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1933 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1934 NODE_INFO (parent)->proxy) + 1;
1937 g_warning ("%s: bad parent node type %d", G_STRLOC,
1938 NODE_INFO (parent)->type);
1944 GtkWidget *prev_child;
1947 sibling = node->prev;
1948 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1949 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1951 prev_child = NODE_INFO (sibling)->proxy;
1953 if (!GTK_IS_WIDGET (prev_child))
1956 menushell = gtk_widget_get_parent (prev_child);
1957 if (!GTK_IS_MENU_SHELL (menushell))
1960 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1964 *menushell_p = menushell;
1972 find_toolbar_position (GNode *node,
1973 GtkWidget **toolbar_p,
1979 g_return_val_if_fail (node != NULL, FALSE);
1980 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1981 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1982 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1983 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1986 /* first sibling -- look at parent */
1987 if (node->prev == NULL)
1991 parent = node->parent;
1992 switch (NODE_INFO (parent)->type)
1994 case NODE_TYPE_TOOLBAR:
1995 toolbar = NODE_INFO (parent)->proxy;
1998 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1999 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2000 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2001 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2002 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2005 g_warning ("%s: bad parent node type %d", G_STRLOC,
2006 NODE_INFO (parent)->type);
2012 GtkWidget *prev_child;
2015 sibling = node->prev;
2016 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2017 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2019 prev_child = NODE_INFO (sibling)->proxy;
2021 if (!GTK_IS_WIDGET (prev_child))
2024 toolbar = gtk_widget_get_parent (prev_child);
2025 if (!GTK_IS_TOOLBAR (toolbar))
2028 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2029 GTK_TOOL_ITEM (prev_child)) + 1;
2033 *toolbar_p = toolbar;
2041 * _gtk_menu_is_empty:
2042 * @menu: a #GtkMenu or %NULL
2044 * Determines whether @menu is empty. A menu is considered empty if it
2045 * the only visible children are tearoff menu items or "filler" menu
2046 * items which were inserted to mark the menu as empty.
2048 * This function is used by #GtkAction.
2050 * Return value: whether @menu is empty.
2053 _gtk_menu_is_empty (GtkWidget *menu)
2055 GList *children, *cur;
2056 gboolean result = TRUE;
2058 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2063 children = gtk_container_get_children (GTK_CONTAINER (menu));
2068 if (GTK_WIDGET_VISIBLE (cur->data))
2070 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2071 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2079 g_list_free (children);
2085 SEPARATOR_MODE_SMART,
2086 SEPARATOR_MODE_VISIBLE,
2087 SEPARATOR_MODE_HIDDEN
2091 update_smart_separators (GtkWidget *proxy)
2093 GtkWidget *parent = NULL;
2095 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2097 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2098 parent = gtk_widget_get_parent (proxy);
2104 GList *children, *cur, *last;
2107 children = gtk_container_get_children (GTK_CONTAINER (parent));
2117 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2121 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2122 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2125 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2126 "gtk-separator-mode"));
2129 case SEPARATOR_MODE_VISIBLE:
2130 gtk_widget_show (GTK_WIDGET (cur->data));
2134 case SEPARATOR_MODE_HIDDEN:
2135 gtk_widget_hide (GTK_WIDGET (cur->data));
2137 case SEPARATOR_MODE_SMART:
2140 gtk_widget_show (GTK_WIDGET (cur->data));
2145 gtk_widget_hide (GTK_WIDGET (cur->data));
2149 else if (GTK_WIDGET_VISIBLE (cur->data))
2152 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2165 gtk_widget_hide (GTK_WIDGET (last->data));
2167 if (GTK_IS_MENU (parent))
2171 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2172 if (GTK_IS_MENU_ITEM (item))
2173 _gtk_action_sync_menu_visible (NULL, item, empty);
2174 if (GTK_IS_WIDGET (filler))
2177 gtk_widget_show (filler);
2179 gtk_widget_hide (filler);
2183 g_list_free (children);
2188 update_node (GtkUIManager *self,
2195 const gchar *action_name;
2196 NodeUIReference *ref;
2198 #ifdef DEBUG_UI_MANAGER
2202 g_return_if_fail (node != NULL);
2203 g_return_if_fail (NODE_INFO (node) != NULL);
2205 info = NODE_INFO (node);
2210 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2212 #ifdef DEBUG_UI_MANAGER
2213 g_print ("update_node name=%s dirty=%d popup %d (",
2214 info->name, info->dirty, in_popup);
2215 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2217 NodeUIReference *ref = tmp->data;
2218 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2225 if (info->uifiles == NULL) {
2226 /* We may need to remove this node.
2227 * This must be done in post order
2229 goto recurse_children;
2232 ref = info->uifiles->data;
2233 action_name = g_quark_to_string (ref->action_quark);
2234 action = get_action_by_name (self, action_name);
2236 info->dirty = FALSE;
2238 /* Check if the node doesn't have an action and must have an action */
2239 if (action == NULL &&
2240 info->type != NODE_TYPE_ROOT &&
2241 info->type != NODE_TYPE_MENUBAR &&
2242 info->type != NODE_TYPE_TOOLBAR &&
2243 info->type != NODE_TYPE_POPUP &&
2244 info->type != NODE_TYPE_SEPARATOR &&
2245 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2246 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2248 g_warning ("%s: missing action %s", info->name, action_name);
2254 gtk_action_set_accel_group (action, self->private_data->accel_group);
2256 /* If the widget already has a proxy and the action hasn't changed, then
2257 * we only have to update the tearoff menu items.
2259 if (info->proxy != NULL && action == info->action)
2261 if (info->type == NODE_TYPE_MENU)
2266 if (GTK_IS_MENU (info->proxy))
2269 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2270 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2271 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2273 if (self->private_data->add_tearoffs && !in_popup)
2274 gtk_widget_show (GTK_WIDGET (siblings->data));
2276 gtk_widget_hide (GTK_WIDGET (siblings->data));
2278 g_list_free (siblings);
2281 goto recurse_children;
2286 case NODE_TYPE_MENUBAR:
2287 if (info->proxy == NULL)
2289 info->proxy = gtk_menu_bar_new ();
2290 g_object_ref_sink (info->proxy);
2291 gtk_widget_set_name (info->proxy, info->name);
2292 gtk_widget_show (info->proxy);
2293 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2296 case NODE_TYPE_POPUP:
2297 if (info->proxy == NULL)
2299 info->proxy = gtk_menu_new ();
2300 g_object_ref_sink (info->proxy);
2302 gtk_widget_set_name (info->proxy, info->name);
2304 case NODE_TYPE_MENU:
2306 GtkWidget *prev_submenu = NULL;
2307 GtkWidget *menu = NULL;
2310 /* remove the proxy if it is of the wrong type ... */
2312 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2314 if (GTK_IS_MENU_ITEM (info->proxy))
2316 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2319 g_object_ref (prev_submenu);
2320 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2324 gtk_action_disconnect_proxy (info->action, info->proxy);
2325 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2327 g_object_unref (info->proxy);
2331 /* create proxy if needed ... */
2332 if (info->proxy == NULL)
2334 /* ... if the action already provides a menu, then use
2335 * that menu instead of creating an empty one
2337 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2338 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2339 GTK_ACTION_GET_CLASS (action)->create_menu)
2341 menu = gtk_action_create_menu (action);
2349 menu = gtk_menu_new ();
2350 gtk_widget_set_name (menu, info->name);
2351 tearoff = gtk_tearoff_menu_item_new ();
2352 gtk_widget_set_no_show_all (tearoff, TRUE);
2353 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2354 filler = gtk_menu_item_new_with_label (_("Empty"));
2355 g_object_set_data (G_OBJECT (filler),
2356 I_("gtk-empty-menu-item"),
2357 GINT_TO_POINTER (TRUE));
2358 gtk_widget_set_sensitive (filler, FALSE);
2359 gtk_widget_set_no_show_all (filler, TRUE);
2360 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2363 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2366 g_object_ref_sink (info->proxy);
2367 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2372 GtkWidget *menushell;
2375 if (find_menu_position (node, &menushell, &pos))
2377 info->proxy = gtk_action_create_menu_item (action);
2378 g_object_ref_sink (info->proxy);
2379 g_signal_connect (info->proxy, "notify::visible",
2380 G_CALLBACK (update_smart_separators), NULL);
2381 gtk_widget_set_name (info->proxy, info->name);
2383 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2384 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2389 gtk_action_connect_proxy (action, info->proxy);
2393 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2395 g_object_unref (prev_submenu);
2398 if (GTK_IS_MENU (info->proxy))
2401 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2403 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2404 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2406 if (self->private_data->add_tearoffs && !in_popup)
2407 gtk_widget_show (GTK_WIDGET (siblings->data));
2409 gtk_widget_hide (GTK_WIDGET (siblings->data));
2411 g_list_free (siblings);
2414 case NODE_TYPE_UNDECIDED:
2415 g_warning ("found undecided node!");
2417 case NODE_TYPE_ROOT:
2419 case NODE_TYPE_TOOLBAR:
2420 if (info->proxy == NULL)
2422 info->proxy = gtk_toolbar_new ();
2423 g_object_ref_sink (info->proxy);
2424 gtk_widget_set_name (info->proxy, info->name);
2425 gtk_widget_show (info->proxy);
2426 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2429 case NODE_TYPE_MENU_PLACEHOLDER:
2430 /* create menu items for placeholders if necessary ... */
2431 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2432 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2436 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2438 g_object_unref (info->proxy);
2443 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2445 g_object_unref (info->extra);
2449 if (info->proxy == NULL)
2451 GtkWidget *menushell;
2454 if (find_menu_position (node, &menushell, &pos))
2456 info->proxy = gtk_separator_menu_item_new ();
2457 g_object_ref_sink (info->proxy);
2458 g_object_set_data (G_OBJECT (info->proxy),
2459 I_("gtk-separator-mode"),
2460 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2461 gtk_widget_set_no_show_all (info->proxy, TRUE);
2462 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2463 NODE_INFO (node)->proxy, pos);
2465 info->extra = gtk_separator_menu_item_new ();
2466 g_object_ref_sink (info->extra);
2467 g_object_set_data (G_OBJECT (info->extra),
2468 I_("gtk-separator-mode"),
2469 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2470 gtk_widget_set_no_show_all (info->extra, TRUE);
2471 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2472 NODE_INFO (node)->extra, pos + 1);
2476 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2477 /* create toolbar items for placeholders if necessary ... */
2478 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2479 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2483 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2485 g_object_unref (info->proxy);
2490 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2492 g_object_unref (info->extra);
2496 if (info->proxy == NULL)
2502 if (find_toolbar_position (node, &toolbar, &pos))
2504 item = gtk_separator_tool_item_new ();
2505 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2506 info->proxy = GTK_WIDGET (item);
2507 g_object_ref_sink (info->proxy);
2508 g_object_set_data (G_OBJECT (info->proxy),
2509 I_("gtk-separator-mode"),
2510 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2511 gtk_widget_set_no_show_all (info->proxy, TRUE);
2513 item = gtk_separator_tool_item_new ();
2514 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2515 info->extra = GTK_WIDGET (item);
2516 g_object_ref_sink (info->extra);
2517 g_object_set_data (G_OBJECT (info->extra),
2518 I_("gtk-separator-mode"),
2519 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2520 gtk_widget_set_no_show_all (info->extra, TRUE);
2524 case NODE_TYPE_MENUITEM:
2525 /* remove the proxy if it is of the wrong type ... */
2527 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2529 g_signal_handlers_disconnect_by_func (info->proxy,
2530 G_CALLBACK (update_smart_separators),
2532 gtk_action_disconnect_proxy (info->action, info->proxy);
2533 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2535 g_object_unref (info->proxy);
2538 /* create proxy if needed ... */
2539 if (info->proxy == NULL)
2541 GtkWidget *menushell;
2544 if (find_menu_position (node, &menushell, &pos))
2546 info->proxy = gtk_action_create_menu_item (action);
2547 g_object_ref_sink (info->proxy);
2548 gtk_widget_set_name (info->proxy, info->name);
2550 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2556 g_signal_handlers_disconnect_by_func (info->proxy,
2557 G_CALLBACK (update_smart_separators),
2559 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2560 gtk_action_connect_proxy (action, info->proxy);
2565 g_signal_connect (info->proxy, "notify::visible",
2566 G_CALLBACK (update_smart_separators), NULL);
2569 /* don't show accels in popups */
2570 GtkWidget *label = GTK_BIN (info->proxy)->child;
2571 g_object_set (label, "accel-closure", NULL, NULL);
2576 case NODE_TYPE_TOOLITEM:
2577 /* remove the proxy if it is of the wrong type ... */
2579 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2581 g_signal_handlers_disconnect_by_func (info->proxy,
2582 G_CALLBACK (update_smart_separators),
2584 gtk_action_disconnect_proxy (info->action, info->proxy);
2585 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2587 g_object_unref (info->proxy);
2590 /* create proxy if needed ... */
2591 if (info->proxy == NULL)
2596 if (find_toolbar_position (node, &toolbar, &pos))
2598 info->proxy = gtk_action_create_tool_item (action);
2599 g_object_ref_sink (info->proxy);
2600 gtk_widget_set_name (info->proxy, info->name);
2602 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2603 GTK_TOOL_ITEM (info->proxy), pos);
2608 g_signal_handlers_disconnect_by_func (info->proxy,
2609 G_CALLBACK (update_smart_separators),
2611 gtk_action_connect_proxy (action, info->proxy);
2616 /* FIXME: we must re-set the tooltip, since tooltips on
2617 * toolitems can't be set before the toolitem is added
2622 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2623 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2626 g_signal_connect (info->proxy, "notify::visible",
2627 G_CALLBACK (update_smart_separators), NULL);
2630 case NODE_TYPE_SEPARATOR:
2631 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2632 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2636 gint separator_mode;
2639 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2641 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2643 g_object_unref (info->proxy);
2647 if (find_toolbar_position (node, &toolbar, &pos))
2649 item = gtk_separator_tool_item_new ();
2650 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2651 info->proxy = GTK_WIDGET (item);
2652 g_object_ref_sink (info->proxy);
2653 gtk_widget_set_no_show_all (info->proxy, TRUE);
2656 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2657 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2658 separator_mode = SEPARATOR_MODE_VISIBLE;
2661 separator_mode = SEPARATOR_MODE_SMART;
2663 g_object_set_data (G_OBJECT (info->proxy),
2664 I_("gtk-separator-mode"),
2665 GINT_TO_POINTER (separator_mode));
2666 gtk_widget_show (info->proxy);
2671 GtkWidget *menushell;
2674 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2676 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2678 g_object_unref (info->proxy);
2682 if (find_menu_position (node, &menushell, &pos))
2684 info->proxy = gtk_separator_menu_item_new ();
2685 g_object_ref_sink (info->proxy);
2686 gtk_widget_set_no_show_all (info->proxy, TRUE);
2687 g_object_set_data (G_OBJECT (info->proxy),
2688 I_("gtk-separator-mode"),
2689 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2690 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2692 gtk_widget_show (info->proxy);
2696 case NODE_TYPE_ACCELERATOR:
2697 gtk_action_connect_accelerator (action);
2702 g_object_ref (action);
2704 g_object_unref (info->action);
2705 info->action = action;
2708 /* process children */
2709 child = node->children;
2715 child = current->next;
2716 update_node (self, current, in_popup);
2721 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2722 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2723 else if (info->type == NODE_TYPE_MENU ||
2724 info->type == NODE_TYPE_TOOLBAR ||
2725 info->type == NODE_TYPE_POPUP)
2726 update_smart_separators (info->proxy);
2729 /* handle cleanup of dead nodes */
2730 if (node->children == NULL && info->uifiles == NULL)
2733 gtk_widget_destroy (info->proxy);
2735 gtk_widget_destroy (info->extra);
2736 if (info->type == NODE_TYPE_ACCELERATOR)
2737 gtk_action_disconnect_accelerator (info->action);
2739 g_node_destroy (node);
2744 do_updates (GtkUIManager *self)
2746 /* this function needs to check through the tree for dirty nodes.
2747 * For such nodes, it needs to do the following:
2749 * 1) check if they are referenced by any loaded UI files anymore.
2750 * In which case, the proxy widget should be destroyed, unless
2751 * there are any subnodes.
2753 * 2) lookup the action for this node again. If it is different to
2754 * the current one (or if no previous action has been looked up),
2755 * the proxy is reconnected to the new action (or a new proxy widget
2756 * is created and added to the parent container).
2758 update_node (self, self->private_data->root_node, FALSE);
2760 self->private_data->update_tag = 0;
2766 do_updates_idle (GtkUIManager *self)
2774 queue_update (GtkUIManager *self)
2776 if (self->private_data->update_tag != 0)
2779 self->private_data->update_tag = gdk_threads_add_idle (
2780 (GSourceFunc)do_updates_idle,
2786 * gtk_ui_manager_ensure_update:
2787 * @self: a #GtkUIManager
2789 * Makes sure that all pending updates to the UI have been completed.
2791 * This may occasionally be necessary, since #GtkUIManager updates the
2792 * UI in an idle function. A typical example where this function is
2793 * useful is to enforce that the menubar and toolbar have been added to
2794 * the main window before showing it:
2797 * gtk_container_add (GTK_CONTAINER (window), vbox);
2798 * g_signal_connect (merge, "add_widget",
2799 * G_CALLBACK (add_widget), vbox);
2800 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2801 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2802 * gtk_ui_manager_ensure_update (merge);
2803 * gtk_widget_show (window);
2805 * </informalexample>
2810 gtk_ui_manager_ensure_update (GtkUIManager *self)
2812 if (self->private_data->update_tag != 0)
2814 g_source_remove (self->private_data->update_tag);
2820 dirty_traverse_func (GNode *node,
2823 NODE_INFO (node)->dirty = TRUE;
2828 dirty_all_nodes (GtkUIManager *self)
2830 g_node_traverse (self->private_data->root_node,
2831 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2832 dirty_traverse_func, NULL);
2833 queue_update (self);
2837 mark_node_dirty (GNode *node)
2841 /* FIXME could optimize this */
2842 for (p = node; p; p = p->parent)
2843 NODE_INFO (p)->dirty = TRUE;
2846 static const gchar *
2847 open_tag_format (NodeType type)
2851 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2852 case NODE_TYPE_ROOT: return "%*s<ui";
2853 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2854 case NODE_TYPE_MENU: return "%*s<menu";
2855 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2856 case NODE_TYPE_MENU_PLACEHOLDER:
2857 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2858 case NODE_TYPE_POPUP: return "%*s<popup";
2859 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2860 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2861 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2862 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2863 default: return NULL;
2867 static const gchar *
2868 close_tag_format (NodeType type)
2872 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2873 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2874 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2875 case NODE_TYPE_MENU: return "%*s</menu>\n";
2876 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2877 case NODE_TYPE_MENU_PLACEHOLDER:
2878 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2879 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2880 default: return NULL;
2885 print_node (GtkUIManager *self,
2892 const gchar *open_fmt;
2893 const gchar *close_fmt;
2897 open_fmt = open_tag_format (mnode->type);
2898 close_fmt = close_tag_format (mnode->type);
2900 g_string_append_printf (buffer, open_fmt, indent_level, "");
2902 if (mnode->type != NODE_TYPE_ROOT)
2905 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2907 if (mnode->action_name)
2908 g_string_append_printf (buffer, " action=\"%s\"",
2909 g_quark_to_string (mnode->action_name));
2912 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2914 for (child = node->children; child != NULL; child = child->next)
2915 print_node (self, child, indent_level + 2, buffer);
2918 g_string_append_printf (buffer, close_fmt, indent_level, "");
2922 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2923 GtkBuilder *builder,
2925 const gchar *tagname,
2926 GMarkupParser *parser,
2932 if (strcmp (tagname, "ui") == 0)
2936 ctx = g_new0 (ParseContext, 1);
2937 ctx->state = STATE_START;
2938 ctx->self = GTK_UI_MANAGER (buildable);
2939 ctx->current = NULL;
2940 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2943 *parser = ui_parser;
2953 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2954 GtkBuilder *builder,
2956 const gchar *tagname,
2959 queue_update (GTK_UI_MANAGER (buildable));
2960 g_object_notify (G_OBJECT (buildable), "ui");
2965 * gtk_ui_manager_get_ui:
2966 * @self: a #GtkUIManager
2968 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2970 * Return value: A newly allocated string containing an XML representation of
2976 gtk_ui_manager_get_ui (GtkUIManager *self)
2980 buffer = g_string_new (NULL);
2982 gtk_ui_manager_ensure_update (self);
2984 print_node (self, self->private_data->root_node, 0, buffer);
2986 return g_string_free (buffer, FALSE);
2991 #undef gtk_ui_manager_add_ui_from_file
2994 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2995 const gchar *filename,
2998 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
3001 if (utf8_filename == NULL)
3004 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
3006 g_free (utf8_filename);
3013 #define __GTK_UI_MANAGER_C__
3014 #include "gtkaliasdef.c"