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 "gtkmenutoolbutton.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtkseparatortoolitem.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtktoolbar.h"
44 #include "gtkuimanager.h"
45 #include "gtkwindow.h"
46 #include "gtkprivate.h"
49 #undef DEBUG_UI_MANAGER
58 NODE_TYPE_MENU_PLACEHOLDER,
59 NODE_TYPE_TOOLBAR_PLACEHOLDER,
67 typedef struct _Node Node;
77 GtkWidget *extra; /* second separator for placeholders */
82 guint expand : 1; /* used for separators */
85 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
87 struct _GtkUIManagerPrivate
89 GtkAccelGroup *accel_group;
98 gboolean add_tearoffs;
101 #define NODE_INFO(node) ((Node *)node->data)
103 typedef struct _NodeUIReference NodeUIReference;
105 struct _NodeUIReference
111 static void gtk_ui_manager_finalize (GObject *object);
112 static void gtk_ui_manager_set_property (GObject *object,
116 static void gtk_ui_manager_get_property (GObject *object,
120 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
122 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
124 static void queue_update (GtkUIManager *self);
125 static void dirty_all_nodes (GtkUIManager *self);
126 static void mark_node_dirty (GNode *node);
127 static GNode * get_child_node (GtkUIManager *self,
130 const gchar *childname,
131 gint childname_length,
135 static GNode * get_node (GtkUIManager *self,
139 static gboolean free_node (GNode *node);
140 static void node_prepend_ui_reference (GNode *node,
142 GQuark action_quark);
143 static void node_remove_ui_reference (GNode *node,
147 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
148 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
152 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
155 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
158 const gchar *tagname,
159 GMarkupParser *parser,
161 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
164 const gchar *tagname,
187 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
189 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
190 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
191 gtk_ui_manager_buildable_init))
194 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
196 GObjectClass *gobject_class;
198 gobject_class = G_OBJECT_CLASS (klass);
200 gobject_class->finalize = gtk_ui_manager_finalize;
201 gobject_class->set_property = gtk_ui_manager_set_property;
202 gobject_class->get_property = gtk_ui_manager_get_property;
203 klass->get_widget = gtk_ui_manager_real_get_widget;
204 klass->get_action = gtk_ui_manager_real_get_action;
207 * GtkUIManager:add-tearoffs:
209 * The "add-tearoffs" property controls whether generated menus
210 * have tearoff menu items.
212 * Note that this only affects regular menus. Generated popup
213 * menus never have tearoff menu items.
217 g_object_class_install_property (gobject_class,
219 g_param_spec_boolean ("add-tearoffs",
220 P_("Add tearoffs to menus"),
221 P_("Whether tearoff menu items should be added to menus"),
223 GTK_PARAM_READWRITE));
225 g_object_class_install_property (gobject_class,
227 g_param_spec_string ("ui",
228 P_("Merged UI definition"),
229 P_("An XML string describing the merged UI"),
231 GTK_PARAM_READABLE));
235 * GtkUIManager::add-widget:
236 * @merge: a #GtkUIManager
237 * @widget: the added widget
239 * The add_widget signal is emitted for each generated menubar and toolbar.
240 * It is not emitted for generated popup menus, which can be obtained by
241 * gtk_ui_manager_get_widget().
245 ui_manager_signals[ADD_WIDGET] =
246 g_signal_new (I_("add_widget"),
247 G_OBJECT_CLASS_TYPE (klass),
248 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
249 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
251 g_cclosure_marshal_VOID__OBJECT,
256 * GtkUIManager::actions-changed:
257 * @merge: a #GtkUIManager
259 * The "actions-changed" signal is emitted whenever the set of actions
264 ui_manager_signals[ACTIONS_CHANGED] =
265 g_signal_new (I_("actions_changed"),
266 G_OBJECT_CLASS_TYPE (klass),
267 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
268 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
270 g_cclosure_marshal_VOID__VOID,
274 * GtkUIManager::connect-proxy:
275 * @uimanager: the ui manager
276 * @action: the action
279 * The connect_proxy signal is emitted after connecting a proxy to
280 * an action in the group.
282 * This is intended for simple customizations for which a custom action
283 * class would be too clumsy, e.g. showing tooltips for menuitems in the
288 ui_manager_signals[CONNECT_PROXY] =
289 g_signal_new (I_("connect_proxy"),
290 G_OBJECT_CLASS_TYPE (klass),
291 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
292 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
294 _gtk_marshal_VOID__OBJECT_OBJECT,
300 * GtkUIManager::disconnect-proxy:
301 * @uimanager: the ui manager
302 * @action: the action
305 * The disconnect_proxy signal is emitted after disconnecting a proxy
306 * from an action in the group.
310 ui_manager_signals[DISCONNECT_PROXY] =
311 g_signal_new (I_("disconnect_proxy"),
312 G_OBJECT_CLASS_TYPE (klass),
313 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
314 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
316 _gtk_marshal_VOID__OBJECT_OBJECT,
322 * GtkUIManager::pre-activate:
323 * @uimanager: the ui manager
324 * @action: the action
326 * The pre_activate signal is emitted just before the @action
329 * This is intended for applications to get notification
330 * just before any action is activated.
334 ui_manager_signals[PRE_ACTIVATE] =
335 g_signal_new (I_("pre_activate"),
336 G_OBJECT_CLASS_TYPE (klass),
337 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
338 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
340 _gtk_marshal_VOID__OBJECT,
345 * GtkUIManager::post-activate:
346 * @uimanager: the ui manager
347 * @action: the action
349 * The post_activate signal is emitted just after the @action
352 * This is intended for applications to get notification
353 * just after any action is activated.
357 ui_manager_signals[POST_ACTIVATE] =
358 g_signal_new (I_("post_activate"),
359 G_OBJECT_CLASS_TYPE (klass),
360 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
361 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
363 _gtk_marshal_VOID__OBJECT,
367 klass->add_widget = NULL;
368 klass->actions_changed = NULL;
369 klass->connect_proxy = NULL;
370 klass->disconnect_proxy = NULL;
371 klass->pre_activate = NULL;
372 klass->post_activate = NULL;
374 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
379 gtk_ui_manager_init (GtkUIManager *self)
384 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
386 self->private_data->accel_group = gtk_accel_group_new ();
388 self->private_data->root_node = NULL;
389 self->private_data->action_groups = NULL;
391 self->private_data->last_merge_id = 0;
392 self->private_data->add_tearoffs = FALSE;
394 merge_id = gtk_ui_manager_new_merge_id (self);
395 node = get_child_node (self, NULL, NULL, "ui", 2,
396 NODE_TYPE_ROOT, TRUE, FALSE);
397 node_prepend_ui_reference (node, merge_id, 0);
401 gtk_ui_manager_finalize (GObject *object)
403 GtkUIManager *self = GTK_UI_MANAGER (object);
405 if (self->private_data->update_tag != 0)
407 g_source_remove (self->private_data->update_tag);
408 self->private_data->update_tag = 0;
411 g_node_traverse (self->private_data->root_node,
412 G_POST_ORDER, G_TRAVERSE_ALL, -1,
413 (GNodeTraverseFunc)free_node, NULL);
414 g_node_destroy (self->private_data->root_node);
415 self->private_data->root_node = NULL;
417 g_list_foreach (self->private_data->action_groups,
418 (GFunc) g_object_unref, NULL);
419 g_list_free (self->private_data->action_groups);
420 self->private_data->action_groups = NULL;
422 g_object_unref (self->private_data->accel_group);
423 self->private_data->accel_group = NULL;
425 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
429 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
431 iface->add_child = gtk_ui_manager_buildable_add_child;
432 iface->construct_child = gtk_ui_manager_buildable_construct_child;
433 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
434 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
438 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
443 GtkUIManager *self = GTK_UI_MANAGER (buildable);
446 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
448 pos = g_list_length (self->private_data->action_groups);
450 g_object_ref (child);
451 gtk_ui_manager_insert_action_group (self,
452 GTK_ACTION_GROUP (child),
457 child_hierarchy_changed_cb (GtkWidget *widget,
462 GtkAccelGroup *group;
465 toplevel = gtk_widget_get_toplevel (widget);
466 if (!toplevel || !GTK_IS_WINDOW (toplevel))
469 group = gtk_ui_manager_get_accel_group (uimgr);
470 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
471 if (g_slist_find (groups, group) == NULL)
472 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
474 g_signal_handlers_disconnect_by_func (widget,
475 child_hierarchy_changed_cb,
480 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
487 name = g_strdup_printf ("ui/%s", id);
488 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
491 g_error ("Unknown ui manager child: %s\n", name);
497 g_signal_connect (widget, "hierarchy-changed",
498 G_CALLBACK (child_hierarchy_changed_cb),
499 GTK_UI_MANAGER (buildable));
500 return g_object_ref (widget);
504 gtk_ui_manager_set_property (GObject *object,
509 GtkUIManager *self = GTK_UI_MANAGER (object);
513 case PROP_ADD_TEAROFFS:
514 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 gtk_ui_manager_get_property (GObject *object,
528 GtkUIManager *self = GTK_UI_MANAGER (object);
532 case PROP_ADD_TEAROFFS:
533 g_value_set_boolean (value, self->private_data->add_tearoffs);
536 g_value_take_string (value, gtk_ui_manager_get_ui (self));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 gtk_ui_manager_real_get_widget (GtkUIManager *self,
550 /* ensure that there are no pending updates before we get the
552 gtk_ui_manager_ensure_update (self);
554 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
559 return NODE_INFO (node)->proxy;
563 gtk_ui_manager_real_get_action (GtkUIManager *self,
568 /* ensure that there are no pending updates before we get
570 gtk_ui_manager_ensure_update (self);
572 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
577 return NODE_INFO (node)->action;
582 * gtk_ui_manager_new:
584 * Creates a new ui manager object.
586 * Return value: a new ui manager object.
591 gtk_ui_manager_new (void)
593 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
598 * gtk_ui_manager_get_add_tearoffs:
599 * @self: a #GtkUIManager
601 * Returns whether menus generated by this #GtkUIManager
602 * will have tearoff menu items.
604 * Return value: whether tearoff menu items are added
609 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
611 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
613 return self->private_data->add_tearoffs;
618 * gtk_ui_manager_set_add_tearoffs:
619 * @self: a #GtkUIManager
620 * @add_tearoffs: whether tearoff menu items are added
622 * Sets the "add_tearoffs" property, which controls whether menus
623 * generated by this #GtkUIManager will have tearoff menu items.
625 * Note that this only affects regular menus. Generated popup
626 * menus never have tearoff menu items.
631 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
632 gboolean add_tearoffs)
634 g_return_if_fail (GTK_IS_UI_MANAGER (self));
636 add_tearoffs = add_tearoffs != FALSE;
638 if (add_tearoffs != self->private_data->add_tearoffs)
640 self->private_data->add_tearoffs = add_tearoffs;
642 dirty_all_nodes (self);
644 g_object_notify (G_OBJECT (self), "add-tearoffs");
649 cb_proxy_connect_proxy (GtkActionGroup *group,
654 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
658 cb_proxy_disconnect_proxy (GtkActionGroup *group,
663 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
667 cb_proxy_pre_activate (GtkActionGroup *group,
671 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
675 cb_proxy_post_activate (GtkActionGroup *group,
679 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
683 * gtk_ui_manager_insert_action_group:
684 * @self: a #GtkUIManager object
685 * @action_group: the action group to be inserted
686 * @pos: the position at which the group will be inserted.
688 * Inserts an action group into the list of action groups associated
689 * with @self. Actions in earlier groups hide actions with the same
690 * name in later groups.
695 gtk_ui_manager_insert_action_group (GtkUIManager *self,
696 GtkActionGroup *action_group,
699 #ifdef G_ENABLE_DEBUG
701 const char *group_name;
704 g_return_if_fail (GTK_IS_UI_MANAGER (self));
705 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
706 g_return_if_fail (g_list_find (self->private_data->action_groups,
707 action_group) == NULL);
709 #ifdef G_ENABLE_DEBUG
710 group_name = gtk_action_group_get_name (action_group);
712 for (l = self->private_data->action_groups; l; l = l->next)
714 GtkActionGroup *group = l->data;
716 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
718 g_warning ("Inserting action group '%s' into UI manager which "
719 "already has a group with this name\n", group_name);
723 #endif /* G_ENABLE_DEBUG */
725 g_object_ref (action_group);
726 self->private_data->action_groups =
727 g_list_insert (self->private_data->action_groups, action_group, pos);
728 g_object_connect (action_group,
729 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
730 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
731 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
732 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
735 /* dirty all nodes, as action bindings may change */
736 dirty_all_nodes (self);
738 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
742 * gtk_ui_manager_remove_action_group:
743 * @self: a #GtkUIManager object
744 * @action_group: the action group to be removed
746 * Removes an action group from the list of action groups associated
752 gtk_ui_manager_remove_action_group (GtkUIManager *self,
753 GtkActionGroup *action_group)
755 g_return_if_fail (GTK_IS_UI_MANAGER (self));
756 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
757 g_return_if_fail (g_list_find (self->private_data->action_groups,
758 action_group) != NULL);
760 self->private_data->action_groups =
761 g_list_remove (self->private_data->action_groups, action_group);
763 g_object_disconnect (action_group,
764 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
765 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
766 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
767 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
769 g_object_unref (action_group);
771 /* dirty all nodes, as action bindings may change */
772 dirty_all_nodes (self);
774 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
778 * gtk_ui_manager_get_action_groups:
779 * @self: a #GtkUIManager object
781 * Returns the list of action groups associated with @self.
783 * Return value: a #GList of action groups. The list is owned by GTK+
784 * and should not be modified.
789 gtk_ui_manager_get_action_groups (GtkUIManager *self)
791 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
793 return self->private_data->action_groups;
797 * gtk_ui_manager_get_accel_group:
798 * @self: a #GtkUIManager object
800 * Returns the #GtkAccelGroup associated with @self.
802 * Return value: the #GtkAccelGroup.
807 gtk_ui_manager_get_accel_group (GtkUIManager *self)
809 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
811 return self->private_data->accel_group;
815 * gtk_ui_manager_get_widget:
816 * @self: a #GtkUIManager
819 * Looks up a widget by following a path.
820 * The path consists of the names specified in the XML description of the UI.
821 * separated by '/'. Elements which don't have a name or action attribute in
822 * the XML (e.g. <popup>) can be addressed by their XML element name
823 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
825 * Note that the widget found by following a path that ends in a <menu>
826 * element is the menuitem to which the menu is attached, not the menu itself.
828 * Also note that the widgets constructed by a ui manager are not tied to
829 * the lifecycle of the ui manager. If you add the widgets returned by this
830 * function to some container or explicitly ref them, they will survive the
831 * destruction of the ui manager.
833 * Return value: the widget found by following the path, or %NULL if no widget
839 gtk_ui_manager_get_widget (GtkUIManager *self,
842 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
843 g_return_val_if_fail (path != NULL, NULL);
845 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
849 GtkUIManagerItemType types;
854 collect_toplevels (GNode *node,
857 ToplevelData *data = user_data;
859 if (NODE_INFO (node)->proxy)
861 switch (NODE_INFO (node)->type)
863 case NODE_TYPE_MENUBAR:
864 if (data->types & GTK_UI_MANAGER_MENUBAR)
865 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
867 case NODE_TYPE_TOOLBAR:
868 if (data->types & GTK_UI_MANAGER_TOOLBAR)
869 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
871 case NODE_TYPE_POPUP:
872 if (data->types & GTK_UI_MANAGER_POPUP)
873 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
881 * gtk_ui_manager_get_toplevels:
882 * @self: a #GtkUIManager
883 * @types: specifies the types of toplevel widgets to include. Allowed
884 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
885 * #GTK_UI_MANAGER_POPUP.
887 * Obtains a list of all toplevel widgets of the requested types.
889 * Return value: a newly-allocated #GSList of all toplevel widgets of the
890 * requested types. Free the returned list with g_slist_free().
895 gtk_ui_manager_get_toplevels (GtkUIManager *self,
896 GtkUIManagerItemType types)
900 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
901 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
902 GTK_UI_MANAGER_TOOLBAR |
903 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
909 g_node_children_foreach (self->private_data->root_node,
911 collect_toplevels, &data);
918 * gtk_ui_manager_get_action:
919 * @self: a #GtkUIManager
922 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
923 * for more information about paths.
925 * Return value: the action whose proxy widget is found by following the path,
926 * or %NULL if no widget was found.
931 gtk_ui_manager_get_action (GtkUIManager *self,
934 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
935 g_return_val_if_fail (path != NULL, NULL);
937 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
941 get_child_node (GtkUIManager *self,
944 const gchar *childname,
945 gint childname_length,
956 for (child = parent->children; child != NULL; child = child->next)
958 if (NODE_INFO (child)->name &&
959 strlen (NODE_INFO (child)->name) == childname_length &&
960 !strncmp (NODE_INFO (child)->name, childname, childname_length))
962 /* if undecided about node type, set it */
963 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
964 NODE_INFO (child)->type = node_type;
966 /* warn about type mismatch */
967 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
968 node_type != NODE_TYPE_UNDECIDED &&
969 NODE_INFO (child)->type != node_type)
970 g_warning ("node type doesn't match %d (%s is type %d)",
972 NODE_INFO (child)->name,
973 NODE_INFO (child)->type);
979 if (!child && create)
983 mnode = g_slice_new0 (Node);
984 mnode->type = node_type;
985 mnode->name = g_strndup (childname, childname_length);
990 child = g_node_insert_before (parent, sibling,
993 child = g_node_insert_after (parent, sibling,
999 child = g_node_prepend_data (parent, mnode);
1001 child = g_node_append_data (parent, mnode);
1004 mark_node_dirty (child);
1009 /* handle root node */
1010 if (self->private_data->root_node)
1012 child = self->private_data->root_node;
1013 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1014 g_warning ("root node name '%s' doesn't match '%s'",
1015 childname, NODE_INFO (child)->name);
1016 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1017 g_warning ("base element must be of type ROOT");
1023 mnode = g_slice_new0 (Node);
1024 mnode->type = node_type;
1025 mnode->name = g_strndup (childname, childname_length);
1026 mnode->dirty = TRUE;
1028 child = self->private_data->root_node = g_node_new (mnode);
1036 get_node (GtkUIManager *self,
1041 const gchar *pos, *end;
1042 GNode *parent, *node;
1044 if (strncmp ("/ui", path, 3) == 0)
1047 end = path + strlen (path);
1049 parent = node = NULL;
1055 slash = strchr (pos, '/');
1057 length = slash - pos;
1059 length = strlen (pos);
1061 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1066 pos += length + 1; /* move past the node name and the slash too */
1070 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1071 NODE_INFO (node)->type = node_type;
1077 node_ui_reference_free (gpointer data)
1079 g_slice_free (NodeUIReference, data);
1083 free_node (GNode *node)
1085 Node *info = NODE_INFO (node);
1087 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1088 g_list_free (info->uifiles);
1091 g_object_unref (info->action);
1093 g_object_unref (info->proxy);
1095 g_object_unref (info->extra);
1096 g_free (info->name);
1097 g_slice_free (Node, info);
1103 * gtk_ui_manager_new_merge_id:
1104 * @self: a #GtkUIManager
1106 * Returns an unused merge id, suitable for use with
1107 * gtk_ui_manager_add_ui().
1109 * Return value: an unused merge id.
1114 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1116 self->private_data->last_merge_id++;
1118 return self->private_data->last_merge_id;
1122 node_prepend_ui_reference (GNode *gnode,
1124 GQuark action_quark)
1126 Node *node = NODE_INFO (gnode);
1127 NodeUIReference *reference = NULL;
1129 if (node->uifiles &&
1130 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1131 reference = node->uifiles->data;
1134 reference = g_slice_new (NodeUIReference);
1135 node->uifiles = g_list_prepend (node->uifiles, reference);
1138 reference->merge_id = merge_id;
1139 reference->action_quark = action_quark;
1141 mark_node_dirty (gnode);
1145 node_remove_ui_reference (GNode *gnode,
1148 Node *node = NODE_INFO (gnode);
1151 for (p = node->uifiles; p != NULL; p = p->next)
1153 NodeUIReference *reference = p->data;
1155 if (reference->merge_id == merge_id)
1157 if (p == node->uifiles)
1158 mark_node_dirty (gnode);
1159 node->uifiles = g_list_delete_link (node->uifiles, p);
1160 g_slice_free (NodeUIReference, reference);
1167 /* -------------------- The UI file parser -------------------- */
1181 typedef struct _ParseContext ParseContext;
1182 struct _ParseContext
1185 ParseState prev_state;
1195 start_element_handler (GMarkupParseContext *context,
1196 const gchar *element_name,
1197 const gchar **attribute_names,
1198 const gchar **attribute_values,
1202 ParseContext *ctx = user_data;
1203 GtkUIManager *self = ctx->self;
1206 const gchar *node_name;
1207 const gchar *action;
1208 GQuark action_quark;
1210 gboolean expand = FALSE;
1212 gboolean raise_error = TRUE;
1219 for (i = 0; attribute_names[i] != NULL; i++)
1221 if (!strcmp (attribute_names[i], "name"))
1223 node_name = attribute_values[i];
1225 else if (!strcmp (attribute_names[i], "action"))
1227 action = attribute_values[i];
1228 action_quark = g_quark_from_string (attribute_values[i]);
1230 else if (!strcmp (attribute_names[i], "position"))
1232 top = !strcmp (attribute_values[i], "top");
1234 else if (!strcmp (attribute_names[i], "expand"))
1236 expand = !strcmp (attribute_values[i], "true");
1238 /* else silently skip unknown attributes to be compatible with
1239 * future additional attributes.
1243 /* Work out a name for this node. Either the name attribute, or
1244 * the action, or the element name */
1245 if (node_name == NULL)
1250 node_name = element_name;
1253 switch (element_name[0])
1256 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1258 ctx->state = STATE_ACCELERATOR;
1259 ctx->current = get_child_node (self, ctx->current, NULL,
1260 node_name, strlen (node_name),
1261 NODE_TYPE_ACCELERATOR,
1263 if (NODE_INFO (ctx->current)->action_name == 0)
1264 NODE_INFO (ctx->current)->action_name = action_quark;
1266 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1268 raise_error = FALSE;
1272 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1274 ctx->state = STATE_ROOT;
1275 ctx->current = self->private_data->root_node;
1276 raise_error = FALSE;
1278 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1282 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1284 ctx->state = STATE_MENU;
1285 ctx->current = get_child_node (self, ctx->current, NULL,
1286 node_name, strlen (node_name),
1289 if (NODE_INFO (ctx->current)->action_name == 0)
1290 NODE_INFO (ctx->current)->action_name = action_quark;
1292 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1293 mark_node_dirty (ctx->current);
1295 raise_error = FALSE;
1297 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1299 ctx->current = get_child_node (self, ctx->current, NULL,
1300 node_name, strlen (node_name),
1303 if (NODE_INFO (ctx->current)->action_name == 0)
1304 NODE_INFO (ctx->current)->action_name = action_quark;
1306 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1308 raise_error = FALSE;
1310 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1312 ctx->state = STATE_MENU;
1314 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1315 node_name, strlen (node_name),
1318 if (NODE_INFO (ctx->current)->action_name == 0)
1319 NODE_INFO (ctx->current)->action_name = action_quark;
1321 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1323 raise_error = FALSE;
1325 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1329 ctx->state = STATE_MENUITEM;
1330 node = get_child_node (self, ctx->current, NULL,
1331 node_name, strlen (node_name),
1334 if (NODE_INFO (node)->action_name == 0)
1335 NODE_INFO (node)->action_name = action_quark;
1337 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1339 raise_error = FALSE;
1343 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1345 ctx->state = STATE_MENU;
1346 ctx->current = get_child_node (self, ctx->current, NULL,
1347 node_name, strlen (node_name),
1350 if (NODE_INFO (ctx->current)->action_name == 0)
1351 NODE_INFO (ctx->current)->action_name = action_quark;
1353 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1355 raise_error = FALSE;
1357 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1358 !strcmp (element_name, "placeholder"))
1360 if (ctx->state == STATE_TOOLBAR)
1361 ctx->current = get_child_node (self, ctx->current, NULL,
1362 node_name, strlen (node_name),
1363 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1366 ctx->current = get_child_node (self, ctx->current, NULL,
1367 node_name, strlen (node_name),
1368 NODE_TYPE_MENU_PLACEHOLDER,
1371 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1373 raise_error = FALSE;
1377 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1378 !strcmp (element_name, "separator"))
1383 if (ctx->state == STATE_TOOLBAR)
1384 ctx->state = STATE_TOOLITEM;
1386 ctx->state = STATE_MENUITEM;
1387 if (!strcmp (node_name, "separator"))
1393 length = strlen (node_name);
1394 node = get_child_node (self, ctx->current, NULL,
1396 NODE_TYPE_SEPARATOR,
1399 NODE_INFO (node)->expand = expand;
1401 if (NODE_INFO (node)->action_name == 0)
1402 NODE_INFO (node)->action_name = action_quark;
1404 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1406 raise_error = FALSE;
1410 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1412 ctx->state = STATE_TOOLBAR;
1413 ctx->current = get_child_node (self, ctx->current, NULL,
1414 node_name, strlen (node_name),
1417 if (NODE_INFO (ctx->current)->action_name == 0)
1418 NODE_INFO (ctx->current)->action_name = action_quark;
1420 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1422 raise_error = FALSE;
1424 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1428 ctx->state = STATE_TOOLITEM;
1429 node = get_child_node (self, ctx->current, NULL,
1430 node_name, strlen (node_name),
1433 if (NODE_INFO (node)->action_name == 0)
1434 NODE_INFO (node)->action_name = action_quark;
1436 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1438 raise_error = FALSE;
1446 gint line_number, char_number;
1448 g_markup_parse_context_get_position (context,
1449 &line_number, &char_number);
1452 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1453 _("Unexpected start tag '%s' on line %d char %d"),
1455 line_number, char_number);
1460 end_element_handler (GMarkupParseContext *context,
1461 const gchar *element_name,
1465 ParseContext *ctx = user_data;
1471 /* no need to GError here, GMarkup already catches this */
1474 ctx->current = NULL;
1475 ctx->state = STATE_END;
1479 case STATE_ACCELERATOR:
1480 ctx->current = ctx->current->parent;
1481 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1482 ctx->state = STATE_ROOT;
1483 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1485 ctx->current = ctx->current->parent;
1486 ctx->state = STATE_TOOLITEM;
1488 /* else, stay in same state */
1490 case STATE_MENUITEM:
1491 ctx->state = STATE_MENU;
1493 case STATE_TOOLITEM:
1494 ctx->state = STATE_TOOLBAR;
1500 cleanup (GMarkupParseContext *context,
1504 ParseContext *ctx = user_data;
1506 ctx->current = NULL;
1507 /* should also walk through the tree and get rid of nodes related to
1508 * this UI file's tag */
1510 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1514 xml_isspace (char c)
1516 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1520 text_handler (GMarkupParseContext *context,
1530 end = text + text_len;
1531 while (p != end && xml_isspace (*p))
1536 gint line_number, char_number;
1538 g_markup_parse_context_get_position (context,
1539 &line_number, &char_number);
1542 G_MARKUP_ERROR_INVALID_CONTENT,
1543 _("Unexpected character data on line %d char %d"),
1544 line_number, char_number);
1549 static const GMarkupParser ui_parser = {
1550 start_element_handler,
1551 end_element_handler,
1558 add_ui_from_string (GtkUIManager *self,
1559 const gchar *buffer,
1561 gboolean needs_root,
1564 ParseContext ctx = { 0 };
1565 GMarkupParseContext *context;
1567 ctx.state = STATE_START;
1570 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1572 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1575 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1578 if (!g_markup_parse_context_parse (context, buffer, length, error))
1582 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1585 if (!g_markup_parse_context_end_parse (context, error))
1588 g_markup_parse_context_free (context);
1590 queue_update (self);
1592 g_object_notify (G_OBJECT (self), "ui");
1594 return ctx.merge_id;
1598 g_markup_parse_context_free (context);
1604 * gtk_ui_manager_add_ui_from_string:
1605 * @self: a #GtkUIManager object
1606 * @buffer: the string to parse
1607 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1608 * @error: return location for an error
1610 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1611 * merges it with the current contents of @self. An enclosing <ui>
1612 * element is added if it is missing.
1614 * Return value: The merge id for the merged UI. The merge id can be used
1615 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1616 * the return value is 0.
1621 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1622 const gchar *buffer,
1626 gboolean needs_root = TRUE;
1630 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1631 g_return_val_if_fail (buffer != NULL, 0);
1634 length = strlen (buffer);
1637 end = buffer + length;
1638 while (p != end && xml_isspace (*p))
1641 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1644 return add_ui_from_string (self, buffer, length, needs_root, error);
1648 * gtk_ui_manager_add_ui_from_file:
1649 * @self: a #GtkUIManager object
1650 * @filename: the name of the file to parse
1651 * @error: return location for an error
1653 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1654 * merges it with the current contents of @self.
1656 * Return value: The merge id for the merged UI. The merge id can be used
1657 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1658 * the return value is 0.
1663 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1664 const gchar *filename,
1671 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1673 if (!g_file_get_contents (filename, &buffer, &length, error))
1676 res = add_ui_from_string (self, buffer, length, FALSE, error);
1683 * gtk_ui_manager_add_ui:
1684 * @self: a #GtkUIManager
1685 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1687 * @name: the name for the added UI element
1688 * @action: the name of the action to be proxied, or %NULL to add a separator
1689 * @type: the type of UI element to add.
1690 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1691 * is added after its siblings.
1693 * Adds a UI element to the current contents of @self.
1695 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1696 * separator if such an element can be inserted at the place determined by
1697 * @path. Otherwise @type must indicate an element that can be inserted at
1698 * the place determined by @path.
1700 * If @path points to a menuitem or toolitem, the new element will be inserted
1701 * before or after this item, depending on @top.
1706 gtk_ui_manager_add_ui (GtkUIManager *self,
1710 const gchar *action,
1711 GtkUIManagerItemType type,
1718 GQuark action_quark = 0;
1720 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1721 g_return_if_fail (merge_id > 0);
1722 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1724 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1730 node_type = NODE_TYPE_UNDECIDED;
1733 switch (NODE_INFO (node)->type)
1735 case NODE_TYPE_SEPARATOR:
1736 case NODE_TYPE_MENUITEM:
1737 case NODE_TYPE_TOOLITEM:
1739 node = node->parent;
1741 case NODE_TYPE_MENUBAR:
1742 case NODE_TYPE_MENU:
1743 case NODE_TYPE_POPUP:
1744 case NODE_TYPE_MENU_PLACEHOLDER:
1747 case GTK_UI_MANAGER_AUTO:
1749 node_type = NODE_TYPE_MENUITEM;
1751 node_type = NODE_TYPE_SEPARATOR;
1753 case GTK_UI_MANAGER_MENU:
1754 node_type = NODE_TYPE_MENU;
1756 case GTK_UI_MANAGER_MENUITEM:
1757 node_type = NODE_TYPE_MENUITEM;
1759 case GTK_UI_MANAGER_SEPARATOR:
1760 node_type = NODE_TYPE_SEPARATOR;
1762 case GTK_UI_MANAGER_PLACEHOLDER:
1763 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1769 case NODE_TYPE_TOOLBAR:
1770 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1773 case GTK_UI_MANAGER_AUTO:
1775 node_type = NODE_TYPE_TOOLITEM;
1777 node_type = NODE_TYPE_SEPARATOR;
1779 case GTK_UI_MANAGER_TOOLITEM:
1780 node_type = NODE_TYPE_TOOLITEM;
1782 case GTK_UI_MANAGER_SEPARATOR:
1783 node_type = NODE_TYPE_SEPARATOR;
1785 case GTK_UI_MANAGER_PLACEHOLDER:
1786 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1792 case NODE_TYPE_ROOT:
1795 case GTK_UI_MANAGER_MENUBAR:
1796 node_type = NODE_TYPE_MENUBAR;
1798 case GTK_UI_MANAGER_TOOLBAR:
1799 node_type = NODE_TYPE_TOOLBAR;
1801 case GTK_UI_MANAGER_POPUP:
1802 node_type = NODE_TYPE_POPUP;
1804 case GTK_UI_MANAGER_ACCELERATOR:
1805 node_type = NODE_TYPE_ACCELERATOR;
1815 if (node_type == NODE_TYPE_UNDECIDED)
1817 g_warning ("item type %d not suitable for adding at '%s'",
1822 child = get_child_node (self, node, sibling,
1823 name, name ? strlen (name) : 0,
1824 node_type, TRUE, top);
1827 action_quark = g_quark_from_string (action);
1829 node_prepend_ui_reference (child, merge_id, action_quark);
1831 if (NODE_INFO (child)->action_name == 0)
1832 NODE_INFO (child)->action_name = action_quark;
1834 queue_update (self);
1836 g_object_notify (G_OBJECT (self), "ui");
1840 remove_ui (GNode *node,
1843 guint merge_id = GPOINTER_TO_UINT (user_data);
1845 node_remove_ui_reference (node, merge_id);
1847 return FALSE; /* continue */
1851 * gtk_ui_manager_remove_ui:
1852 * @self: a #GtkUIManager object
1853 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1855 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1860 gtk_ui_manager_remove_ui (GtkUIManager *self,
1863 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1865 g_node_traverse (self->private_data->root_node,
1866 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1867 remove_ui, GUINT_TO_POINTER (merge_id));
1869 queue_update (self);
1871 g_object_notify (G_OBJECT (self), "ui");
1874 /* -------------------- Updates -------------------- */
1878 get_action_by_name (GtkUIManager *merge,
1879 const gchar *action_name)
1887 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1889 GtkActionGroup *action_group = tmp->data;
1892 action = gtk_action_group_get_action (action_group, action_name);
1902 find_menu_position (GNode *node,
1903 GtkWidget **menushell_p,
1906 GtkWidget *menushell;
1909 g_return_val_if_fail (node != NULL, FALSE);
1910 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1911 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1912 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1913 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1914 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1917 /* first sibling -- look at parent */
1918 if (node->prev == NULL)
1923 parent = node->parent;
1924 switch (NODE_INFO (parent)->type)
1926 case NODE_TYPE_MENUBAR:
1927 case NODE_TYPE_POPUP:
1928 menushell = NODE_INFO (parent)->proxy;
1931 case NODE_TYPE_MENU:
1932 menushell = NODE_INFO (parent)->proxy;
1933 if (GTK_IS_MENU_ITEM (menushell))
1934 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1935 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1936 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1940 g_list_free (siblings);
1942 case NODE_TYPE_MENU_PLACEHOLDER:
1943 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1944 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1945 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1946 NODE_INFO (parent)->proxy) + 1;
1949 g_warning ("%s: bad parent node type %d", G_STRLOC,
1950 NODE_INFO (parent)->type);
1956 GtkWidget *prev_child;
1959 sibling = node->prev;
1960 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1961 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1963 prev_child = NODE_INFO (sibling)->proxy;
1965 if (!GTK_IS_WIDGET (prev_child))
1968 menushell = gtk_widget_get_parent (prev_child);
1969 if (!GTK_IS_MENU_SHELL (menushell))
1972 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1976 *menushell_p = menushell;
1984 find_toolbar_position (GNode *node,
1985 GtkWidget **toolbar_p,
1991 g_return_val_if_fail (node != NULL, FALSE);
1992 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1993 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1994 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1995 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1998 /* first sibling -- look at parent */
1999 if (node->prev == NULL)
2003 parent = node->parent;
2004 switch (NODE_INFO (parent)->type)
2006 case NODE_TYPE_TOOLBAR:
2007 toolbar = NODE_INFO (parent)->proxy;
2010 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2011 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2012 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2013 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2014 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2017 g_warning ("%s: bad parent node type %d", G_STRLOC,
2018 NODE_INFO (parent)->type);
2024 GtkWidget *prev_child;
2027 sibling = node->prev;
2028 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2029 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2031 prev_child = NODE_INFO (sibling)->proxy;
2033 if (!GTK_IS_WIDGET (prev_child))
2036 toolbar = gtk_widget_get_parent (prev_child);
2037 if (!GTK_IS_TOOLBAR (toolbar))
2040 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2041 GTK_TOOL_ITEM (prev_child)) + 1;
2045 *toolbar_p = toolbar;
2053 * _gtk_menu_is_empty:
2054 * @menu: a #GtkMenu or %NULL
2056 * Determines whether @menu is empty. A menu is considered empty if it
2057 * the only visible children are tearoff menu items or "filler" menu
2058 * items which were inserted to mark the menu as empty.
2060 * This function is used by #GtkAction.
2062 * Return value: whether @menu is empty.
2065 _gtk_menu_is_empty (GtkWidget *menu)
2067 GList *children, *cur;
2068 gboolean result = TRUE;
2070 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2075 children = gtk_container_get_children (GTK_CONTAINER (menu));
2080 if (GTK_WIDGET_VISIBLE (cur->data))
2082 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2083 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2091 g_list_free (children);
2097 SEPARATOR_MODE_SMART,
2098 SEPARATOR_MODE_VISIBLE,
2099 SEPARATOR_MODE_HIDDEN
2103 update_smart_separators (GtkWidget *proxy)
2105 GtkWidget *parent = NULL;
2107 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2109 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2110 parent = gtk_widget_get_parent (proxy);
2116 GList *children, *cur, *last;
2119 children = gtk_container_get_children (GTK_CONTAINER (parent));
2129 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2133 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2134 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2137 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2138 "gtk-separator-mode"));
2141 case SEPARATOR_MODE_VISIBLE:
2142 gtk_widget_show (GTK_WIDGET (cur->data));
2146 case SEPARATOR_MODE_HIDDEN:
2147 gtk_widget_hide (GTK_WIDGET (cur->data));
2149 case SEPARATOR_MODE_SMART:
2152 gtk_widget_show (GTK_WIDGET (cur->data));
2157 gtk_widget_hide (GTK_WIDGET (cur->data));
2161 else if (GTK_WIDGET_VISIBLE (cur->data))
2164 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2177 gtk_widget_hide (GTK_WIDGET (last->data));
2179 if (GTK_IS_MENU (parent))
2183 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2184 if (GTK_IS_MENU_ITEM (item))
2185 _gtk_action_sync_menu_visible (NULL, item, empty);
2186 if (GTK_IS_WIDGET (filler))
2189 gtk_widget_show (filler);
2191 gtk_widget_hide (filler);
2195 g_list_free (children);
2200 update_node (GtkUIManager *self,
2207 const gchar *action_name;
2208 NodeUIReference *ref;
2210 #ifdef DEBUG_UI_MANAGER
2214 g_return_if_fail (node != NULL);
2215 g_return_if_fail (NODE_INFO (node) != NULL);
2217 info = NODE_INFO (node);
2222 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2224 #ifdef DEBUG_UI_MANAGER
2225 g_print ("update_node name=%s dirty=%d popup %d (",
2226 info->name, info->dirty, in_popup);
2227 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2229 NodeUIReference *ref = tmp->data;
2230 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2237 if (info->uifiles == NULL) {
2238 /* We may need to remove this node.
2239 * This must be done in post order
2241 goto recurse_children;
2244 ref = info->uifiles->data;
2245 action_name = g_quark_to_string (ref->action_quark);
2246 action = get_action_by_name (self, action_name);
2248 info->dirty = FALSE;
2250 /* Check if the node doesn't have an action and must have an action */
2251 if (action == NULL &&
2252 info->type != NODE_TYPE_ROOT &&
2253 info->type != NODE_TYPE_MENUBAR &&
2254 info->type != NODE_TYPE_TOOLBAR &&
2255 info->type != NODE_TYPE_POPUP &&
2256 info->type != NODE_TYPE_SEPARATOR &&
2257 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2258 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2260 g_warning ("%s: missing action %s", info->name, action_name);
2266 gtk_action_set_accel_group (action, self->private_data->accel_group);
2268 /* If the widget already has a proxy and the action hasn't changed, then
2269 * we only have to update the tearoff menu items.
2271 if (info->proxy != NULL && action == info->action)
2273 if (info->type == NODE_TYPE_MENU)
2278 if (GTK_IS_MENU (info->proxy))
2281 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2282 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2283 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2285 if (self->private_data->add_tearoffs && !in_popup)
2286 gtk_widget_show (GTK_WIDGET (siblings->data));
2288 gtk_widget_hide (GTK_WIDGET (siblings->data));
2290 g_list_free (siblings);
2293 goto recurse_children;
2298 case NODE_TYPE_MENUBAR:
2299 if (info->proxy == NULL)
2301 info->proxy = gtk_menu_bar_new ();
2302 g_object_ref_sink (info->proxy);
2303 gtk_widget_set_name (info->proxy, info->name);
2304 gtk_widget_show (info->proxy);
2305 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2308 case NODE_TYPE_POPUP:
2309 if (info->proxy == NULL)
2311 info->proxy = gtk_menu_new ();
2312 g_object_ref_sink (info->proxy);
2314 gtk_widget_set_name (info->proxy, info->name);
2316 case NODE_TYPE_MENU:
2318 GtkWidget *prev_submenu = NULL;
2319 GtkWidget *menu = NULL;
2322 /* remove the proxy if it is of the wrong type ... */
2324 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2326 if (GTK_IS_MENU_ITEM (info->proxy))
2328 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2331 g_object_ref (prev_submenu);
2332 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2336 gtk_action_disconnect_proxy (info->action, info->proxy);
2337 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2339 g_object_unref (info->proxy);
2343 /* create proxy if needed ... */
2344 if (info->proxy == NULL)
2346 /* ... if the action already provides a menu, then use
2347 * that menu instead of creating an empty one
2349 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2350 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2351 GTK_ACTION_GET_CLASS (action)->create_menu)
2353 menu = gtk_action_create_menu (action);
2361 menu = gtk_menu_new ();
2362 gtk_widget_set_name (menu, info->name);
2363 tearoff = gtk_tearoff_menu_item_new ();
2364 gtk_widget_set_no_show_all (tearoff, TRUE);
2365 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2366 filler = gtk_menu_item_new_with_label (_("Empty"));
2367 g_object_set_data (G_OBJECT (filler),
2368 I_("gtk-empty-menu-item"),
2369 GINT_TO_POINTER (TRUE));
2370 gtk_widget_set_sensitive (filler, FALSE);
2371 gtk_widget_set_no_show_all (filler, TRUE);
2372 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2375 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2378 g_object_ref_sink (info->proxy);
2379 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2384 GtkWidget *menushell;
2387 if (find_menu_position (node, &menushell, &pos))
2389 info->proxy = gtk_action_create_menu_item (action);
2390 g_object_ref_sink (info->proxy);
2391 g_signal_connect (info->proxy, "notify::visible",
2392 G_CALLBACK (update_smart_separators), NULL);
2393 gtk_widget_set_name (info->proxy, info->name);
2395 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2396 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2401 gtk_action_connect_proxy (action, info->proxy);
2405 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2407 g_object_unref (prev_submenu);
2410 if (GTK_IS_MENU (info->proxy))
2413 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2415 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2416 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2418 if (self->private_data->add_tearoffs && !in_popup)
2419 gtk_widget_show (GTK_WIDGET (siblings->data));
2421 gtk_widget_hide (GTK_WIDGET (siblings->data));
2423 g_list_free (siblings);
2426 case NODE_TYPE_UNDECIDED:
2427 g_warning ("found undecided node!");
2429 case NODE_TYPE_ROOT:
2431 case NODE_TYPE_TOOLBAR:
2432 if (info->proxy == NULL)
2434 info->proxy = gtk_toolbar_new ();
2435 g_object_ref_sink (info->proxy);
2436 gtk_widget_set_name (info->proxy, info->name);
2437 gtk_widget_show (info->proxy);
2438 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2441 case NODE_TYPE_MENU_PLACEHOLDER:
2442 /* create menu items for placeholders if necessary ... */
2443 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2444 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2448 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2450 g_object_unref (info->proxy);
2455 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2457 g_object_unref (info->extra);
2461 if (info->proxy == NULL)
2463 GtkWidget *menushell;
2466 if (find_menu_position (node, &menushell, &pos))
2468 info->proxy = gtk_separator_menu_item_new ();
2469 g_object_ref_sink (info->proxy);
2470 g_object_set_data (G_OBJECT (info->proxy),
2471 I_("gtk-separator-mode"),
2472 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2473 gtk_widget_set_no_show_all (info->proxy, TRUE);
2474 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2475 NODE_INFO (node)->proxy, pos);
2477 info->extra = gtk_separator_menu_item_new ();
2478 g_object_ref_sink (info->extra);
2479 g_object_set_data (G_OBJECT (info->extra),
2480 I_("gtk-separator-mode"),
2481 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2482 gtk_widget_set_no_show_all (info->extra, TRUE);
2483 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2484 NODE_INFO (node)->extra, pos + 1);
2488 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2489 /* create toolbar items for placeholders if necessary ... */
2490 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2491 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2495 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2497 g_object_unref (info->proxy);
2502 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2504 g_object_unref (info->extra);
2508 if (info->proxy == NULL)
2514 if (find_toolbar_position (node, &toolbar, &pos))
2516 item = gtk_separator_tool_item_new ();
2517 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2518 info->proxy = GTK_WIDGET (item);
2519 g_object_ref_sink (info->proxy);
2520 g_object_set_data (G_OBJECT (info->proxy),
2521 I_("gtk-separator-mode"),
2522 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2523 gtk_widget_set_no_show_all (info->proxy, TRUE);
2525 item = gtk_separator_tool_item_new ();
2526 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2527 info->extra = GTK_WIDGET (item);
2528 g_object_ref_sink (info->extra);
2529 g_object_set_data (G_OBJECT (info->extra),
2530 I_("gtk-separator-mode"),
2531 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2532 gtk_widget_set_no_show_all (info->extra, TRUE);
2536 case NODE_TYPE_MENUITEM:
2537 /* remove the proxy if it is of the wrong type ... */
2539 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2541 g_signal_handlers_disconnect_by_func (info->proxy,
2542 G_CALLBACK (update_smart_separators),
2544 gtk_action_disconnect_proxy (info->action, info->proxy);
2545 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2547 g_object_unref (info->proxy);
2550 /* create proxy if needed ... */
2551 if (info->proxy == NULL)
2553 GtkWidget *menushell;
2556 if (find_menu_position (node, &menushell, &pos))
2558 info->proxy = gtk_action_create_menu_item (action);
2559 g_object_ref_sink (info->proxy);
2560 gtk_widget_set_name (info->proxy, info->name);
2562 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2568 g_signal_handlers_disconnect_by_func (info->proxy,
2569 G_CALLBACK (update_smart_separators),
2571 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2572 gtk_action_connect_proxy (action, info->proxy);
2577 g_signal_connect (info->proxy, "notify::visible",
2578 G_CALLBACK (update_smart_separators), NULL);
2581 /* don't show accels in popups */
2582 GtkWidget *label = GTK_BIN (info->proxy)->child;
2583 g_object_set (label, "accel-closure", NULL, NULL);
2588 case NODE_TYPE_TOOLITEM:
2589 /* remove the proxy if it is of the wrong type ... */
2591 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2593 g_signal_handlers_disconnect_by_func (info->proxy,
2594 G_CALLBACK (update_smart_separators),
2596 gtk_action_disconnect_proxy (info->action, info->proxy);
2597 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2599 g_object_unref (info->proxy);
2602 /* create proxy if needed ... */
2603 if (info->proxy == NULL)
2608 if (find_toolbar_position (node, &toolbar, &pos))
2610 info->proxy = gtk_action_create_tool_item (action);
2611 g_object_ref_sink (info->proxy);
2612 gtk_widget_set_name (info->proxy, info->name);
2614 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2615 GTK_TOOL_ITEM (info->proxy), pos);
2620 g_signal_handlers_disconnect_by_func (info->proxy,
2621 G_CALLBACK (update_smart_separators),
2623 gtk_action_connect_proxy (action, info->proxy);
2628 g_signal_connect (info->proxy, "notify::visible",
2629 G_CALLBACK (update_smart_separators), NULL);
2632 case NODE_TYPE_SEPARATOR:
2633 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2634 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2638 gint separator_mode;
2641 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2643 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2645 g_object_unref (info->proxy);
2649 if (find_toolbar_position (node, &toolbar, &pos))
2651 item = gtk_separator_tool_item_new ();
2652 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2653 info->proxy = GTK_WIDGET (item);
2654 g_object_ref_sink (info->proxy);
2655 gtk_widget_set_no_show_all (info->proxy, TRUE);
2658 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2659 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2660 separator_mode = SEPARATOR_MODE_VISIBLE;
2663 separator_mode = SEPARATOR_MODE_SMART;
2665 g_object_set_data (G_OBJECT (info->proxy),
2666 I_("gtk-separator-mode"),
2667 GINT_TO_POINTER (separator_mode));
2668 gtk_widget_show (info->proxy);
2673 GtkWidget *menushell;
2676 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2678 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2680 g_object_unref (info->proxy);
2684 if (find_menu_position (node, &menushell, &pos))
2686 info->proxy = gtk_separator_menu_item_new ();
2687 g_object_ref_sink (info->proxy);
2688 gtk_widget_set_no_show_all (info->proxy, TRUE);
2689 g_object_set_data (G_OBJECT (info->proxy),
2690 I_("gtk-separator-mode"),
2691 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2692 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2694 gtk_widget_show (info->proxy);
2698 case NODE_TYPE_ACCELERATOR:
2699 gtk_action_connect_accelerator (action);
2704 g_object_ref (action);
2706 g_object_unref (info->action);
2707 info->action = action;
2710 /* process children */
2711 child = node->children;
2717 child = current->next;
2718 update_node (self, current, in_popup);
2723 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2724 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2725 else if (info->type == NODE_TYPE_MENU ||
2726 info->type == NODE_TYPE_TOOLBAR ||
2727 info->type == NODE_TYPE_POPUP)
2728 update_smart_separators (info->proxy);
2731 /* handle cleanup of dead nodes */
2732 if (node->children == NULL && info->uifiles == NULL)
2735 gtk_widget_destroy (info->proxy);
2737 gtk_widget_destroy (info->extra);
2738 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
2739 gtk_action_disconnect_accelerator (info->action);
2741 g_node_destroy (node);
2746 do_updates (GtkUIManager *self)
2748 /* this function needs to check through the tree for dirty nodes.
2749 * For such nodes, it needs to do the following:
2751 * 1) check if they are referenced by any loaded UI files anymore.
2752 * In which case, the proxy widget should be destroyed, unless
2753 * there are any subnodes.
2755 * 2) lookup the action for this node again. If it is different to
2756 * the current one (or if no previous action has been looked up),
2757 * the proxy is reconnected to the new action (or a new proxy widget
2758 * is created and added to the parent container).
2760 update_node (self, self->private_data->root_node, FALSE);
2762 self->private_data->update_tag = 0;
2768 do_updates_idle (GtkUIManager *self)
2776 queue_update (GtkUIManager *self)
2778 if (self->private_data->update_tag != 0)
2781 self->private_data->update_tag = gdk_threads_add_idle (
2782 (GSourceFunc)do_updates_idle,
2788 * gtk_ui_manager_ensure_update:
2789 * @self: a #GtkUIManager
2791 * Makes sure that all pending updates to the UI have been completed.
2793 * This may occasionally be necessary, since #GtkUIManager updates the
2794 * UI in an idle function. A typical example where this function is
2795 * useful is to enforce that the menubar and toolbar have been added to
2796 * the main window before showing it:
2798 * gtk_container_add (GTK_CONTAINER (window), vbox);
2799 * g_signal_connect (merge, "add_widget",
2800 * G_CALLBACK (add_widget), vbox);
2801 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2802 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2803 * gtk_ui_manager_ensure_update (merge);
2804 * gtk_widget_show (window);
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);
2989 #if defined (G_OS_WIN32) && !defined (_WIN64)
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"