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 "gtkactivatable.h"
35 #include "gtkbuildable.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenubar.h"
40 #include "gtkmenutoolbutton.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtkseparatortoolitem.h"
43 #include "gtktearoffmenuitem.h"
44 #include "gtktoolbar.h"
45 #include "gtkuimanager.h"
46 #include "gtkwindow.h"
47 #include "gtkprivate.h"
50 #undef DEBUG_UI_MANAGER
59 NODE_TYPE_MENU_PLACEHOLDER,
60 NODE_TYPE_TOOLBAR_PLACEHOLDER,
68 typedef struct _Node Node;
78 GtkWidget *extra; /* second separator for placeholders */
83 guint expand : 1; /* used for separators */
84 guint popup_accels : 1;
87 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
89 struct _GtkUIManagerPrivate
91 GtkAccelGroup *accel_group;
100 gboolean add_tearoffs;
103 #define NODE_INFO(node) ((Node *)node->data)
105 typedef struct _NodeUIReference NodeUIReference;
107 struct _NodeUIReference
113 static void gtk_ui_manager_finalize (GObject *object);
114 static void gtk_ui_manager_set_property (GObject *object,
118 static void gtk_ui_manager_get_property (GObject *object,
122 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
124 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
126 static void queue_update (GtkUIManager *self);
127 static void dirty_all_nodes (GtkUIManager *self);
128 static void mark_node_dirty (GNode *node);
129 static GNode * get_child_node (GtkUIManager *self,
132 const gchar *childname,
133 gint childname_length,
137 static GNode * get_node (GtkUIManager *self,
141 static gboolean free_node (GNode *node);
142 static void node_prepend_ui_reference (GNode *node,
144 GQuark action_quark);
145 static void node_remove_ui_reference (GNode *node,
149 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
150 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
154 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
157 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
160 const gchar *tagname,
161 GMarkupParser *parser,
163 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
166 const gchar *tagname,
189 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
191 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
193 gtk_ui_manager_buildable_init))
196 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
198 GObjectClass *gobject_class;
200 gobject_class = G_OBJECT_CLASS (klass);
202 gobject_class->finalize = gtk_ui_manager_finalize;
203 gobject_class->set_property = gtk_ui_manager_set_property;
204 gobject_class->get_property = gtk_ui_manager_get_property;
205 klass->get_widget = gtk_ui_manager_real_get_widget;
206 klass->get_action = gtk_ui_manager_real_get_action;
209 * GtkUIManager:add-tearoffs:
211 * The "add-tearoffs" property controls whether generated menus
212 * have tearoff menu items.
214 * Note that this only affects regular menus. Generated popup
215 * menus never have tearoff menu items.
219 g_object_class_install_property (gobject_class,
221 g_param_spec_boolean ("add-tearoffs",
222 P_("Add tearoffs to menus"),
223 P_("Whether tearoff menu items should be added to menus"),
225 GTK_PARAM_READWRITE));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_string ("ui",
230 P_("Merged UI definition"),
231 P_("An XML string describing the merged UI"),
233 GTK_PARAM_READABLE));
237 * GtkUIManager::add-widget:
238 * @merge: a #GtkUIManager
239 * @widget: the added widget
241 * The add_widget signal is emitted for each generated menubar and toolbar.
242 * It is not emitted for generated popup menus, which can be obtained by
243 * gtk_ui_manager_get_widget().
247 ui_manager_signals[ADD_WIDGET] =
248 g_signal_new (I_("add-widget"),
249 G_OBJECT_CLASS_TYPE (klass),
250 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
251 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
253 g_cclosure_marshal_VOID__OBJECT,
258 * GtkUIManager::actions-changed:
259 * @merge: a #GtkUIManager
261 * The "actions-changed" signal is emitted whenever the set of actions
266 ui_manager_signals[ACTIONS_CHANGED] =
267 g_signal_new (I_("actions-changed"),
268 G_OBJECT_CLASS_TYPE (klass),
269 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
270 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
272 g_cclosure_marshal_VOID__VOID,
276 * GtkUIManager::connect-proxy:
277 * @uimanager: the ui manager
278 * @action: the action
281 * The connect_proxy signal is emitted after connecting a proxy to
282 * an action in the group.
284 * This is intended for simple customizations for which a custom action
285 * class would be too clumsy, e.g. showing tooltips for menuitems in the
290 ui_manager_signals[CONNECT_PROXY] =
291 g_signal_new (I_("connect-proxy"),
292 G_OBJECT_CLASS_TYPE (klass),
293 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
294 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
296 _gtk_marshal_VOID__OBJECT_OBJECT,
302 * GtkUIManager::disconnect-proxy:
303 * @uimanager: the ui manager
304 * @action: the action
307 * The disconnect_proxy signal is emitted after disconnecting a proxy
308 * from an action in the group.
312 ui_manager_signals[DISCONNECT_PROXY] =
313 g_signal_new (I_("disconnect-proxy"),
314 G_OBJECT_CLASS_TYPE (klass),
315 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
316 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
318 _gtk_marshal_VOID__OBJECT_OBJECT,
324 * GtkUIManager::pre-activate:
325 * @uimanager: the ui manager
326 * @action: the action
328 * The pre_activate signal is emitted just before the @action
331 * This is intended for applications to get notification
332 * just before any action is activated.
336 ui_manager_signals[PRE_ACTIVATE] =
337 g_signal_new (I_("pre-activate"),
338 G_OBJECT_CLASS_TYPE (klass),
339 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
340 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
342 _gtk_marshal_VOID__OBJECT,
347 * GtkUIManager::post-activate:
348 * @uimanager: the ui manager
349 * @action: the action
351 * The post_activate signal is emitted just after the @action
354 * This is intended for applications to get notification
355 * just after any action is activated.
359 ui_manager_signals[POST_ACTIVATE] =
360 g_signal_new (I_("post-activate"),
361 G_OBJECT_CLASS_TYPE (klass),
362 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
363 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
365 _gtk_marshal_VOID__OBJECT,
369 klass->add_widget = NULL;
370 klass->actions_changed = NULL;
371 klass->connect_proxy = NULL;
372 klass->disconnect_proxy = NULL;
373 klass->pre_activate = NULL;
374 klass->post_activate = NULL;
376 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
381 gtk_ui_manager_init (GtkUIManager *self)
386 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
388 self->private_data->accel_group = gtk_accel_group_new ();
390 self->private_data->root_node = NULL;
391 self->private_data->action_groups = NULL;
393 self->private_data->last_merge_id = 0;
394 self->private_data->add_tearoffs = FALSE;
396 merge_id = gtk_ui_manager_new_merge_id (self);
397 node = get_child_node (self, NULL, NULL, "ui", 2,
398 NODE_TYPE_ROOT, TRUE, FALSE);
399 node_prepend_ui_reference (node, merge_id, 0);
403 gtk_ui_manager_finalize (GObject *object)
405 GtkUIManager *self = GTK_UI_MANAGER (object);
407 if (self->private_data->update_tag != 0)
409 g_source_remove (self->private_data->update_tag);
410 self->private_data->update_tag = 0;
413 g_node_traverse (self->private_data->root_node,
414 G_POST_ORDER, G_TRAVERSE_ALL, -1,
415 (GNodeTraverseFunc)free_node, NULL);
416 g_node_destroy (self->private_data->root_node);
417 self->private_data->root_node = NULL;
419 g_list_foreach (self->private_data->action_groups,
420 (GFunc) g_object_unref, NULL);
421 g_list_free (self->private_data->action_groups);
422 self->private_data->action_groups = NULL;
424 g_object_unref (self->private_data->accel_group);
425 self->private_data->accel_group = NULL;
427 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
431 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
433 iface->add_child = gtk_ui_manager_buildable_add_child;
434 iface->construct_child = gtk_ui_manager_buildable_construct_child;
435 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
436 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
440 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
445 GtkUIManager *self = GTK_UI_MANAGER (buildable);
448 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
450 pos = g_list_length (self->private_data->action_groups);
452 g_object_ref (child);
453 gtk_ui_manager_insert_action_group (self,
454 GTK_ACTION_GROUP (child),
459 child_hierarchy_changed_cb (GtkWidget *widget,
464 GtkAccelGroup *group;
467 toplevel = gtk_widget_get_toplevel (widget);
468 if (!toplevel || !GTK_IS_WINDOW (toplevel))
471 group = gtk_ui_manager_get_accel_group (uimgr);
472 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
473 if (g_slist_find (groups, group) == NULL)
474 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
476 g_signal_handlers_disconnect_by_func (widget,
477 child_hierarchy_changed_cb,
482 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
489 name = g_strdup_printf ("ui/%s", id);
490 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
493 g_error ("Unknown ui manager child: %s\n", name);
499 g_signal_connect (widget, "hierarchy-changed",
500 G_CALLBACK (child_hierarchy_changed_cb),
501 GTK_UI_MANAGER (buildable));
502 return g_object_ref (widget);
506 gtk_ui_manager_set_property (GObject *object,
511 GtkUIManager *self = GTK_UI_MANAGER (object);
515 case PROP_ADD_TEAROFFS:
516 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525 gtk_ui_manager_get_property (GObject *object,
530 GtkUIManager *self = GTK_UI_MANAGER (object);
534 case PROP_ADD_TEAROFFS:
535 g_value_set_boolean (value, self->private_data->add_tearoffs);
538 g_value_take_string (value, gtk_ui_manager_get_ui (self));
541 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547 gtk_ui_manager_real_get_widget (GtkUIManager *self,
552 /* ensure that there are no pending updates before we get the
554 gtk_ui_manager_ensure_update (self);
556 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
561 return NODE_INFO (node)->proxy;
565 gtk_ui_manager_real_get_action (GtkUIManager *self,
570 /* ensure that there are no pending updates before we get
572 gtk_ui_manager_ensure_update (self);
574 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
579 return NODE_INFO (node)->action;
584 * gtk_ui_manager_new:
586 * Creates a new ui manager object.
588 * Return value: a new ui manager object.
593 gtk_ui_manager_new (void)
595 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
600 * gtk_ui_manager_get_add_tearoffs:
601 * @self: a #GtkUIManager
603 * Returns whether menus generated by this #GtkUIManager
604 * will have tearoff menu items.
606 * Return value: whether tearoff menu items are added
611 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
613 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
615 return self->private_data->add_tearoffs;
620 * gtk_ui_manager_set_add_tearoffs:
621 * @self: a #GtkUIManager
622 * @add_tearoffs: whether tearoff menu items are added
624 * Sets the "add_tearoffs" property, which controls whether menus
625 * generated by this #GtkUIManager will have tearoff menu items.
627 * Note that this only affects regular menus. Generated popup
628 * menus never have tearoff menu items.
633 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
634 gboolean add_tearoffs)
636 g_return_if_fail (GTK_IS_UI_MANAGER (self));
638 add_tearoffs = add_tearoffs != FALSE;
640 if (add_tearoffs != self->private_data->add_tearoffs)
642 self->private_data->add_tearoffs = add_tearoffs;
644 dirty_all_nodes (self);
646 g_object_notify (G_OBJECT (self), "add-tearoffs");
651 cb_proxy_connect_proxy (GtkActionGroup *group,
656 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
660 cb_proxy_disconnect_proxy (GtkActionGroup *group,
665 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
669 cb_proxy_pre_activate (GtkActionGroup *group,
673 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
677 cb_proxy_post_activate (GtkActionGroup *group,
681 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
685 * gtk_ui_manager_insert_action_group:
686 * @self: a #GtkUIManager object
687 * @action_group: the action group to be inserted
688 * @pos: the position at which the group will be inserted.
690 * Inserts an action group into the list of action groups associated
691 * with @self. Actions in earlier groups hide actions with the same
692 * name in later groups.
697 gtk_ui_manager_insert_action_group (GtkUIManager *self,
698 GtkActionGroup *action_group,
701 #ifdef G_ENABLE_DEBUG
703 const char *group_name;
706 g_return_if_fail (GTK_IS_UI_MANAGER (self));
707 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
708 g_return_if_fail (g_list_find (self->private_data->action_groups,
709 action_group) == NULL);
711 #ifdef G_ENABLE_DEBUG
712 group_name = gtk_action_group_get_name (action_group);
714 for (l = self->private_data->action_groups; l; l = l->next)
716 GtkActionGroup *group = l->data;
718 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
720 g_warning ("Inserting action group '%s' into UI manager which "
721 "already has a group with this name\n", group_name);
725 #endif /* G_ENABLE_DEBUG */
727 g_object_ref (action_group);
728 self->private_data->action_groups =
729 g_list_insert (self->private_data->action_groups, action_group, pos);
730 g_object_connect (action_group,
731 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
732 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
733 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), self,
734 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), self,
737 /* dirty all nodes, as action bindings may change */
738 dirty_all_nodes (self);
740 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
744 * gtk_ui_manager_remove_action_group:
745 * @self: a #GtkUIManager object
746 * @action_group: the action group to be removed
748 * Removes an action group from the list of action groups associated
754 gtk_ui_manager_remove_action_group (GtkUIManager *self,
755 GtkActionGroup *action_group)
757 g_return_if_fail (GTK_IS_UI_MANAGER (self));
758 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
759 g_return_if_fail (g_list_find (self->private_data->action_groups,
760 action_group) != NULL);
762 self->private_data->action_groups =
763 g_list_remove (self->private_data->action_groups, action_group);
765 g_object_disconnect (action_group,
766 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
767 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
768 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), self,
769 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), self,
771 g_object_unref (action_group);
773 /* dirty all nodes, as action bindings may change */
774 dirty_all_nodes (self);
776 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
780 * gtk_ui_manager_get_action_groups:
781 * @self: a #GtkUIManager object
783 * Returns the list of action groups associated with @self.
785 * Return value: a #GList of action groups. The list is owned by GTK+
786 * and should not be modified.
791 gtk_ui_manager_get_action_groups (GtkUIManager *self)
793 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
795 return self->private_data->action_groups;
799 * gtk_ui_manager_get_accel_group:
800 * @self: a #GtkUIManager object
802 * Returns the #GtkAccelGroup associated with @self.
804 * Return value: the #GtkAccelGroup.
809 gtk_ui_manager_get_accel_group (GtkUIManager *self)
811 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
813 return self->private_data->accel_group;
817 * gtk_ui_manager_get_widget:
818 * @self: a #GtkUIManager
821 * Looks up a widget by following a path.
822 * The path consists of the names specified in the XML description of the UI.
823 * separated by '/'. Elements which don't have a name or action attribute in
824 * the XML (e.g. <popup>) can be addressed by their XML element name
825 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
827 * Note that the widget found by following a path that ends in a <menu>
828 * element is the menuitem to which the menu is attached, not the menu itself.
830 * Also note that the widgets constructed by a ui manager are not tied to
831 * the lifecycle of the ui manager. If you add the widgets returned by this
832 * function to some container or explicitly ref them, they will survive the
833 * destruction of the ui manager.
835 * Return value: the widget found by following the path, or %NULL if no widget
841 gtk_ui_manager_get_widget (GtkUIManager *self,
844 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
845 g_return_val_if_fail (path != NULL, NULL);
847 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
851 GtkUIManagerItemType types;
856 collect_toplevels (GNode *node,
859 ToplevelData *data = user_data;
861 if (NODE_INFO (node)->proxy)
863 switch (NODE_INFO (node)->type)
865 case NODE_TYPE_MENUBAR:
866 if (data->types & GTK_UI_MANAGER_MENUBAR)
867 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
869 case NODE_TYPE_TOOLBAR:
870 if (data->types & GTK_UI_MANAGER_TOOLBAR)
871 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
873 case NODE_TYPE_POPUP:
874 if (data->types & GTK_UI_MANAGER_POPUP)
875 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
883 * gtk_ui_manager_get_toplevels:
884 * @self: a #GtkUIManager
885 * @types: specifies the types of toplevel widgets to include. Allowed
886 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
887 * #GTK_UI_MANAGER_POPUP.
889 * Obtains a list of all toplevel widgets of the requested types.
891 * Return value: a newly-allocated #GSList of all toplevel widgets of the
892 * requested types. Free the returned list with g_slist_free().
897 gtk_ui_manager_get_toplevels (GtkUIManager *self,
898 GtkUIManagerItemType types)
902 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
903 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
904 GTK_UI_MANAGER_TOOLBAR |
905 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
911 g_node_children_foreach (self->private_data->root_node,
913 collect_toplevels, &data);
920 * gtk_ui_manager_get_action:
921 * @self: a #GtkUIManager
924 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
925 * for more information about paths.
927 * Return value: the action whose proxy widget is found by following the path,
928 * or %NULL if no widget was found.
933 gtk_ui_manager_get_action (GtkUIManager *self,
936 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
937 g_return_val_if_fail (path != NULL, NULL);
939 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
943 get_child_node (GtkUIManager *self,
946 const gchar *childname,
947 gint childname_length,
958 for (child = parent->children; child != NULL; child = child->next)
960 if (NODE_INFO (child)->name &&
961 strlen (NODE_INFO (child)->name) == childname_length &&
962 !strncmp (NODE_INFO (child)->name, childname, childname_length))
964 /* if undecided about node type, set it */
965 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
966 NODE_INFO (child)->type = node_type;
968 /* warn about type mismatch */
969 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
970 node_type != NODE_TYPE_UNDECIDED &&
971 NODE_INFO (child)->type != node_type)
972 g_warning ("node type doesn't match %d (%s is type %d)",
974 NODE_INFO (child)->name,
975 NODE_INFO (child)->type);
981 if (!child && create)
985 mnode = g_slice_new0 (Node);
986 mnode->type = node_type;
987 mnode->name = g_strndup (childname, childname_length);
992 child = g_node_insert_before (parent, sibling,
995 child = g_node_insert_after (parent, sibling,
1001 child = g_node_prepend_data (parent, mnode);
1003 child = g_node_append_data (parent, mnode);
1006 mark_node_dirty (child);
1011 /* handle root node */
1012 if (self->private_data->root_node)
1014 child = self->private_data->root_node;
1015 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1016 g_warning ("root node name '%s' doesn't match '%s'",
1017 childname, NODE_INFO (child)->name);
1018 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1019 g_warning ("base element must be of type ROOT");
1025 mnode = g_slice_new0 (Node);
1026 mnode->type = node_type;
1027 mnode->name = g_strndup (childname, childname_length);
1028 mnode->dirty = TRUE;
1030 child = self->private_data->root_node = g_node_new (mnode);
1038 get_node (GtkUIManager *self,
1043 const gchar *pos, *end;
1044 GNode *parent, *node;
1046 if (strncmp ("/ui", path, 3) == 0)
1049 end = path + strlen (path);
1051 parent = node = NULL;
1057 slash = strchr (pos, '/');
1059 length = slash - pos;
1061 length = strlen (pos);
1063 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1068 pos += length + 1; /* move past the node name and the slash too */
1072 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1073 NODE_INFO (node)->type = node_type;
1079 node_ui_reference_free (gpointer data)
1081 g_slice_free (NodeUIReference, data);
1085 free_node (GNode *node)
1087 Node *info = NODE_INFO (node);
1089 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1090 g_list_free (info->uifiles);
1093 g_object_unref (info->action);
1095 g_object_unref (info->proxy);
1097 g_object_unref (info->extra);
1098 g_free (info->name);
1099 g_slice_free (Node, info);
1105 * gtk_ui_manager_new_merge_id:
1106 * @self: a #GtkUIManager
1108 * Returns an unused merge id, suitable for use with
1109 * gtk_ui_manager_add_ui().
1111 * Return value: an unused merge id.
1116 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1118 self->private_data->last_merge_id++;
1120 return self->private_data->last_merge_id;
1124 node_prepend_ui_reference (GNode *gnode,
1126 GQuark action_quark)
1128 Node *node = NODE_INFO (gnode);
1129 NodeUIReference *reference = NULL;
1131 if (node->uifiles &&
1132 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1133 reference = node->uifiles->data;
1136 reference = g_slice_new (NodeUIReference);
1137 node->uifiles = g_list_prepend (node->uifiles, reference);
1140 reference->merge_id = merge_id;
1141 reference->action_quark = action_quark;
1143 mark_node_dirty (gnode);
1147 node_remove_ui_reference (GNode *gnode,
1150 Node *node = NODE_INFO (gnode);
1153 for (p = node->uifiles; p != NULL; p = p->next)
1155 NodeUIReference *reference = p->data;
1157 if (reference->merge_id == merge_id)
1159 if (p == node->uifiles)
1160 mark_node_dirty (gnode);
1161 node->uifiles = g_list_delete_link (node->uifiles, p);
1162 g_slice_free (NodeUIReference, reference);
1169 /* -------------------- The UI file parser -------------------- */
1183 typedef struct _ParseContext ParseContext;
1184 struct _ParseContext
1187 ParseState prev_state;
1197 start_element_handler (GMarkupParseContext *context,
1198 const gchar *element_name,
1199 const gchar **attribute_names,
1200 const gchar **attribute_values,
1204 ParseContext *ctx = user_data;
1205 GtkUIManager *self = ctx->self;
1208 const gchar *node_name;
1209 const gchar *action;
1210 GQuark action_quark;
1212 gboolean expand = FALSE;
1213 gboolean accelerators = FALSE;
1215 gboolean raise_error = TRUE;
1222 for (i = 0; attribute_names[i] != NULL; i++)
1224 if (!strcmp (attribute_names[i], "name"))
1226 node_name = attribute_values[i];
1228 else if (!strcmp (attribute_names[i], "action"))
1230 action = attribute_values[i];
1231 action_quark = g_quark_from_string (attribute_values[i]);
1233 else if (!strcmp (attribute_names[i], "position"))
1235 top = !strcmp (attribute_values[i], "top");
1237 else if (!strcmp (attribute_names[i], "expand"))
1239 expand = !strcmp (attribute_values[i], "true");
1241 else if (!strcmp (attribute_names[i], "accelerators"))
1243 accelerators = !strcmp (attribute_values[i], "true");
1245 /* else silently skip unknown attributes to be compatible with
1246 * future additional attributes.
1250 /* Work out a name for this node. Either the name attribute, or
1251 * the action, or the element name */
1252 if (node_name == NULL)
1257 node_name = element_name;
1260 switch (element_name[0])
1263 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1265 ctx->state = STATE_ACCELERATOR;
1266 ctx->current = get_child_node (self, ctx->current, NULL,
1267 node_name, strlen (node_name),
1268 NODE_TYPE_ACCELERATOR,
1270 if (NODE_INFO (ctx->current)->action_name == 0)
1271 NODE_INFO (ctx->current)->action_name = action_quark;
1273 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1275 raise_error = FALSE;
1279 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1281 ctx->state = STATE_ROOT;
1282 ctx->current = self->private_data->root_node;
1283 raise_error = FALSE;
1285 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1289 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1291 ctx->state = STATE_MENU;
1292 ctx->current = get_child_node (self, ctx->current, NULL,
1293 node_name, strlen (node_name),
1296 if (NODE_INFO (ctx->current)->action_name == 0)
1297 NODE_INFO (ctx->current)->action_name = action_quark;
1299 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1300 mark_node_dirty (ctx->current);
1302 raise_error = FALSE;
1304 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1306 ctx->current = get_child_node (self, ctx->current, NULL,
1307 node_name, strlen (node_name),
1310 if (NODE_INFO (ctx->current)->action_name == 0)
1311 NODE_INFO (ctx->current)->action_name = action_quark;
1313 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1315 raise_error = FALSE;
1317 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1319 ctx->state = STATE_MENU;
1321 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1322 node_name, strlen (node_name),
1325 if (NODE_INFO (ctx->current)->action_name == 0)
1326 NODE_INFO (ctx->current)->action_name = action_quark;
1328 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1330 raise_error = FALSE;
1332 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1336 ctx->state = STATE_MENUITEM;
1337 node = get_child_node (self, ctx->current, NULL,
1338 node_name, strlen (node_name),
1341 if (NODE_INFO (node)->action_name == 0)
1342 NODE_INFO (node)->action_name = action_quark;
1344 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1346 raise_error = FALSE;
1350 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1352 ctx->state = STATE_MENU;
1353 ctx->current = get_child_node (self, ctx->current, NULL,
1354 node_name, strlen (node_name),
1358 NODE_INFO (ctx->current)->popup_accels = accelerators;
1360 if (NODE_INFO (ctx->current)->action_name == 0)
1361 NODE_INFO (ctx->current)->action_name = action_quark;
1363 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1365 raise_error = FALSE;
1367 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1368 !strcmp (element_name, "placeholder"))
1370 if (ctx->state == STATE_TOOLBAR)
1371 ctx->current = get_child_node (self, ctx->current, NULL,
1372 node_name, strlen (node_name),
1373 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1376 ctx->current = get_child_node (self, ctx->current, NULL,
1377 node_name, strlen (node_name),
1378 NODE_TYPE_MENU_PLACEHOLDER,
1381 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1383 raise_error = FALSE;
1387 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1388 !strcmp (element_name, "separator"))
1393 if (ctx->state == STATE_TOOLBAR)
1394 ctx->state = STATE_TOOLITEM;
1396 ctx->state = STATE_MENUITEM;
1397 if (!strcmp (node_name, "separator"))
1403 length = strlen (node_name);
1404 node = get_child_node (self, ctx->current, NULL,
1406 NODE_TYPE_SEPARATOR,
1409 NODE_INFO (node)->expand = expand;
1411 if (NODE_INFO (node)->action_name == 0)
1412 NODE_INFO (node)->action_name = action_quark;
1414 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1416 raise_error = FALSE;
1420 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1422 ctx->state = STATE_TOOLBAR;
1423 ctx->current = get_child_node (self, ctx->current, NULL,
1424 node_name, strlen (node_name),
1427 if (NODE_INFO (ctx->current)->action_name == 0)
1428 NODE_INFO (ctx->current)->action_name = action_quark;
1430 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1432 raise_error = FALSE;
1434 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1438 ctx->state = STATE_TOOLITEM;
1439 node = get_child_node (self, ctx->current, NULL,
1440 node_name, strlen (node_name),
1443 if (NODE_INFO (node)->action_name == 0)
1444 NODE_INFO (node)->action_name = action_quark;
1446 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1448 raise_error = FALSE;
1456 gint line_number, char_number;
1458 g_markup_parse_context_get_position (context,
1459 &line_number, &char_number);
1462 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1463 _("Unexpected start tag '%s' on line %d char %d"),
1465 line_number, char_number);
1470 end_element_handler (GMarkupParseContext *context,
1471 const gchar *element_name,
1475 ParseContext *ctx = user_data;
1481 /* no need to GError here, GMarkup already catches this */
1484 ctx->current = NULL;
1485 ctx->state = STATE_END;
1489 case STATE_ACCELERATOR:
1490 ctx->current = ctx->current->parent;
1491 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1492 ctx->state = STATE_ROOT;
1493 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1495 ctx->current = ctx->current->parent;
1496 ctx->state = STATE_TOOLITEM;
1498 /* else, stay in same state */
1500 case STATE_MENUITEM:
1501 ctx->state = STATE_MENU;
1503 case STATE_TOOLITEM:
1504 ctx->state = STATE_TOOLBAR;
1510 cleanup (GMarkupParseContext *context,
1514 ParseContext *ctx = user_data;
1516 ctx->current = NULL;
1517 /* should also walk through the tree and get rid of nodes related to
1518 * this UI file's tag */
1520 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1524 xml_isspace (char c)
1526 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1530 text_handler (GMarkupParseContext *context,
1540 end = text + text_len;
1541 while (p != end && xml_isspace (*p))
1546 gint line_number, char_number;
1548 g_markup_parse_context_get_position (context,
1549 &line_number, &char_number);
1552 G_MARKUP_ERROR_INVALID_CONTENT,
1553 _("Unexpected character data on line %d char %d"),
1554 line_number, char_number);
1559 static const GMarkupParser ui_parser = {
1560 start_element_handler,
1561 end_element_handler,
1568 add_ui_from_string (GtkUIManager *self,
1569 const gchar *buffer,
1571 gboolean needs_root,
1574 ParseContext ctx = { 0 };
1575 GMarkupParseContext *context;
1577 ctx.state = STATE_START;
1580 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1582 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1585 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1588 if (!g_markup_parse_context_parse (context, buffer, length, error))
1592 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1595 if (!g_markup_parse_context_end_parse (context, error))
1598 g_markup_parse_context_free (context);
1600 queue_update (self);
1602 g_object_notify (G_OBJECT (self), "ui");
1604 return ctx.merge_id;
1608 g_markup_parse_context_free (context);
1614 * gtk_ui_manager_add_ui_from_string:
1615 * @self: a #GtkUIManager object
1616 * @buffer: the string to parse
1617 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1618 * @error: return location for an error
1620 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1621 * merges it with the current contents of @self. An enclosing <ui>
1622 * element is added if it is missing.
1624 * Return value: The merge id for the merged UI. The merge id can be used
1625 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1626 * the return value is 0.
1631 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1632 const gchar *buffer,
1636 gboolean needs_root = TRUE;
1640 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1641 g_return_val_if_fail (buffer != NULL, 0);
1644 length = strlen (buffer);
1647 end = buffer + length;
1648 while (p != end && xml_isspace (*p))
1651 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1654 return add_ui_from_string (self, buffer, length, needs_root, error);
1658 * gtk_ui_manager_add_ui_from_file:
1659 * @self: a #GtkUIManager object
1660 * @filename: the name of the file to parse
1661 * @error: return location for an error
1663 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1664 * merges it with the current contents of @self.
1666 * Return value: The merge id for the merged UI. The merge id can be used
1667 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1668 * the return value is 0.
1673 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1674 const gchar *filename,
1681 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1683 if (!g_file_get_contents (filename, &buffer, &length, error))
1686 res = add_ui_from_string (self, buffer, length, FALSE, error);
1693 * gtk_ui_manager_add_ui:
1694 * @self: a #GtkUIManager
1695 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1697 * @name: the name for the added UI element
1698 * @action: the name of the action to be proxied, or %NULL to add a separator
1699 * @type: the type of UI element to add.
1700 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1701 * is added after its siblings.
1703 * Adds a UI element to the current contents of @self.
1705 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1706 * separator if such an element can be inserted at the place determined by
1707 * @path. Otherwise @type must indicate an element that can be inserted at
1708 * the place determined by @path.
1710 * If @path points to a menuitem or toolitem, the new element will be inserted
1711 * before or after this item, depending on @top.
1716 gtk_ui_manager_add_ui (GtkUIManager *self,
1720 const gchar *action,
1721 GtkUIManagerItemType type,
1728 GQuark action_quark = 0;
1730 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1731 g_return_if_fail (merge_id > 0);
1732 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1734 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1740 node_type = NODE_TYPE_UNDECIDED;
1743 switch (NODE_INFO (node)->type)
1745 case NODE_TYPE_SEPARATOR:
1746 case NODE_TYPE_MENUITEM:
1747 case NODE_TYPE_TOOLITEM:
1749 node = node->parent;
1751 case NODE_TYPE_MENUBAR:
1752 case NODE_TYPE_MENU:
1753 case NODE_TYPE_POPUP:
1754 case NODE_TYPE_MENU_PLACEHOLDER:
1757 case GTK_UI_MANAGER_AUTO:
1759 node_type = NODE_TYPE_MENUITEM;
1761 node_type = NODE_TYPE_SEPARATOR;
1763 case GTK_UI_MANAGER_MENU:
1764 node_type = NODE_TYPE_MENU;
1766 case GTK_UI_MANAGER_MENUITEM:
1767 node_type = NODE_TYPE_MENUITEM;
1769 case GTK_UI_MANAGER_SEPARATOR:
1770 node_type = NODE_TYPE_SEPARATOR;
1772 case GTK_UI_MANAGER_PLACEHOLDER:
1773 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1779 case NODE_TYPE_TOOLBAR:
1780 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1783 case GTK_UI_MANAGER_AUTO:
1785 node_type = NODE_TYPE_TOOLITEM;
1787 node_type = NODE_TYPE_SEPARATOR;
1789 case GTK_UI_MANAGER_TOOLITEM:
1790 node_type = NODE_TYPE_TOOLITEM;
1792 case GTK_UI_MANAGER_SEPARATOR:
1793 node_type = NODE_TYPE_SEPARATOR;
1795 case GTK_UI_MANAGER_PLACEHOLDER:
1796 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1802 case NODE_TYPE_ROOT:
1805 case GTK_UI_MANAGER_MENUBAR:
1806 node_type = NODE_TYPE_MENUBAR;
1808 case GTK_UI_MANAGER_TOOLBAR:
1809 node_type = NODE_TYPE_TOOLBAR;
1811 case GTK_UI_MANAGER_POPUP:
1812 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
1813 node_type = NODE_TYPE_POPUP;
1815 case GTK_UI_MANAGER_ACCELERATOR:
1816 node_type = NODE_TYPE_ACCELERATOR;
1826 if (node_type == NODE_TYPE_UNDECIDED)
1828 g_warning ("item type %d not suitable for adding at '%s'",
1833 child = get_child_node (self, node, sibling,
1834 name, name ? strlen (name) : 0,
1835 node_type, TRUE, top);
1837 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
1838 NODE_INFO (child)->popup_accels = TRUE;
1841 action_quark = g_quark_from_string (action);
1843 node_prepend_ui_reference (child, merge_id, action_quark);
1845 if (NODE_INFO (child)->action_name == 0)
1846 NODE_INFO (child)->action_name = action_quark;
1848 queue_update (self);
1850 g_object_notify (G_OBJECT (self), "ui");
1854 remove_ui (GNode *node,
1857 guint merge_id = GPOINTER_TO_UINT (user_data);
1859 node_remove_ui_reference (node, merge_id);
1861 return FALSE; /* continue */
1865 * gtk_ui_manager_remove_ui:
1866 * @self: a #GtkUIManager object
1867 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1869 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1874 gtk_ui_manager_remove_ui (GtkUIManager *self,
1877 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1879 g_node_traverse (self->private_data->root_node,
1880 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1881 remove_ui, GUINT_TO_POINTER (merge_id));
1883 queue_update (self);
1885 g_object_notify (G_OBJECT (self), "ui");
1888 /* -------------------- Updates -------------------- */
1892 get_action_by_name (GtkUIManager *merge,
1893 const gchar *action_name)
1901 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1903 GtkActionGroup *action_group = tmp->data;
1906 action = gtk_action_group_get_action (action_group, action_name);
1916 find_menu_position (GNode *node,
1917 GtkWidget **menushell_p,
1920 GtkWidget *menushell;
1923 g_return_val_if_fail (node != NULL, FALSE);
1924 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1925 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1926 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1927 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1928 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1931 /* first sibling -- look at parent */
1932 if (node->prev == NULL)
1937 parent = node->parent;
1938 switch (NODE_INFO (parent)->type)
1940 case NODE_TYPE_MENUBAR:
1941 case NODE_TYPE_POPUP:
1942 menushell = NODE_INFO (parent)->proxy;
1945 case NODE_TYPE_MENU:
1946 menushell = NODE_INFO (parent)->proxy;
1947 if (GTK_IS_MENU_ITEM (menushell))
1948 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1949 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1950 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1954 g_list_free (siblings);
1956 case NODE_TYPE_MENU_PLACEHOLDER:
1957 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1958 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1959 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1960 NODE_INFO (parent)->proxy) + 1;
1963 g_warning ("%s: bad parent node type %d", G_STRLOC,
1964 NODE_INFO (parent)->type);
1970 GtkWidget *prev_child;
1973 sibling = node->prev;
1974 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1975 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1977 prev_child = NODE_INFO (sibling)->proxy;
1979 if (!GTK_IS_WIDGET (prev_child))
1982 menushell = gtk_widget_get_parent (prev_child);
1983 if (!GTK_IS_MENU_SHELL (menushell))
1986 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1990 *menushell_p = menushell;
1998 find_toolbar_position (GNode *node,
1999 GtkWidget **toolbar_p,
2005 g_return_val_if_fail (node != NULL, FALSE);
2006 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2007 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2008 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2009 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2012 /* first sibling -- look at parent */
2013 if (node->prev == NULL)
2017 parent = node->parent;
2018 switch (NODE_INFO (parent)->type)
2020 case NODE_TYPE_TOOLBAR:
2021 toolbar = NODE_INFO (parent)->proxy;
2024 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2025 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2026 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2027 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2028 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2031 g_warning ("%s: bad parent node type %d", G_STRLOC,
2032 NODE_INFO (parent)->type);
2038 GtkWidget *prev_child;
2041 sibling = node->prev;
2042 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2043 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2045 prev_child = NODE_INFO (sibling)->proxy;
2047 if (!GTK_IS_WIDGET (prev_child))
2050 toolbar = gtk_widget_get_parent (prev_child);
2051 if (!GTK_IS_TOOLBAR (toolbar))
2054 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2055 GTK_TOOL_ITEM (prev_child)) + 1;
2059 *toolbar_p = toolbar;
2067 * _gtk_menu_is_empty:
2068 * @menu: a #GtkMenu or %NULL
2070 * Determines whether @menu is empty. A menu is considered empty if it
2071 * the only visible children are tearoff menu items or "filler" menu
2072 * items which were inserted to mark the menu as empty.
2074 * This function is used by #GtkAction.
2076 * Return value: whether @menu is empty.
2079 _gtk_menu_is_empty (GtkWidget *menu)
2081 GList *children, *cur;
2082 gboolean result = TRUE;
2084 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2089 children = gtk_container_get_children (GTK_CONTAINER (menu));
2094 if (GTK_WIDGET_VISIBLE (cur->data))
2096 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2097 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2105 g_list_free (children);
2111 SEPARATOR_MODE_SMART,
2112 SEPARATOR_MODE_VISIBLE,
2113 SEPARATOR_MODE_HIDDEN
2117 update_smart_separators (GtkWidget *proxy)
2119 GtkWidget *parent = NULL;
2121 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2123 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2124 parent = gtk_widget_get_parent (proxy);
2130 GList *children, *cur, *last;
2133 children = gtk_container_get_children (GTK_CONTAINER (parent));
2143 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2147 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2148 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2151 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2152 "gtk-separator-mode"));
2155 case SEPARATOR_MODE_VISIBLE:
2156 gtk_widget_show (GTK_WIDGET (cur->data));
2160 case SEPARATOR_MODE_HIDDEN:
2161 gtk_widget_hide (GTK_WIDGET (cur->data));
2163 case SEPARATOR_MODE_SMART:
2166 gtk_widget_show (GTK_WIDGET (cur->data));
2171 gtk_widget_hide (GTK_WIDGET (cur->data));
2175 else if (GTK_WIDGET_VISIBLE (cur->data))
2178 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2191 gtk_widget_hide (GTK_WIDGET (last->data));
2193 if (GTK_IS_MENU (parent))
2197 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2198 if (GTK_IS_MENU_ITEM (item))
2199 _gtk_action_sync_menu_visible (NULL, item, empty);
2200 if (GTK_IS_WIDGET (filler))
2203 gtk_widget_show (filler);
2205 gtk_widget_hide (filler);
2209 g_list_free (children);
2214 update_node (GtkUIManager *self,
2217 gboolean popup_accels)
2222 const gchar *action_name;
2223 NodeUIReference *ref;
2225 #ifdef DEBUG_UI_MANAGER
2229 g_return_if_fail (node != NULL);
2230 g_return_if_fail (NODE_INFO (node) != NULL);
2232 info = NODE_INFO (node);
2237 if (info->type == NODE_TYPE_POPUP)
2240 popup_accels = info->popup_accels;
2243 #ifdef DEBUG_UI_MANAGER
2244 g_print ("update_node name=%s dirty=%d popup %d (",
2245 info->name, info->dirty, in_popup);
2246 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2248 NodeUIReference *ref = tmp->data;
2249 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2256 if (info->uifiles == NULL) {
2257 /* We may need to remove this node.
2258 * This must be done in post order
2260 goto recurse_children;
2263 ref = info->uifiles->data;
2264 action_name = g_quark_to_string (ref->action_quark);
2265 action = get_action_by_name (self, action_name);
2267 info->dirty = FALSE;
2269 /* Check if the node doesn't have an action and must have an action */
2270 if (action == NULL &&
2271 info->type != NODE_TYPE_ROOT &&
2272 info->type != NODE_TYPE_MENUBAR &&
2273 info->type != NODE_TYPE_TOOLBAR &&
2274 info->type != NODE_TYPE_POPUP &&
2275 info->type != NODE_TYPE_SEPARATOR &&
2276 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2277 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2279 g_warning ("%s: missing action %s", info->name, action_name);
2285 gtk_action_set_accel_group (action, self->private_data->accel_group);
2287 /* If the widget already has a proxy and the action hasn't changed, then
2288 * we only have to update the tearoff menu items.
2290 if (info->proxy != NULL && action == info->action)
2292 if (info->type == NODE_TYPE_MENU)
2297 if (GTK_IS_MENU (info->proxy))
2300 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2301 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2302 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2304 if (self->private_data->add_tearoffs && !in_popup)
2305 gtk_widget_show (GTK_WIDGET (siblings->data));
2307 gtk_widget_hide (GTK_WIDGET (siblings->data));
2309 g_list_free (siblings);
2312 goto recurse_children;
2317 case NODE_TYPE_MENUBAR:
2318 if (info->proxy == NULL)
2320 info->proxy = gtk_menu_bar_new ();
2321 g_object_ref_sink (info->proxy);
2322 gtk_widget_set_name (info->proxy, info->name);
2323 gtk_widget_show (info->proxy);
2324 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2327 case NODE_TYPE_POPUP:
2328 if (info->proxy == NULL)
2330 info->proxy = gtk_menu_new ();
2331 g_object_ref_sink (info->proxy);
2333 gtk_widget_set_name (info->proxy, info->name);
2335 case NODE_TYPE_MENU:
2337 GtkWidget *prev_submenu = NULL;
2338 GtkWidget *menu = NULL;
2341 /* remove the proxy if it is of the wrong type ... */
2343 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2345 if (GTK_IS_MENU_ITEM (info->proxy))
2347 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2350 g_object_ref (prev_submenu);
2351 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2355 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2356 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2358 g_object_unref (info->proxy);
2362 /* create proxy if needed ... */
2363 if (info->proxy == NULL)
2365 /* ... if the action already provides a menu, then use
2366 * that menu instead of creating an empty one
2368 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2369 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2370 GTK_ACTION_GET_CLASS (action)->create_menu)
2372 menu = gtk_action_create_menu (action);
2380 menu = gtk_menu_new ();
2381 gtk_widget_set_name (menu, info->name);
2382 tearoff = gtk_tearoff_menu_item_new ();
2383 gtk_widget_set_no_show_all (tearoff, TRUE);
2384 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2385 filler = gtk_menu_item_new_with_label (_("Empty"));
2386 g_object_set_data (G_OBJECT (filler),
2387 I_("gtk-empty-menu-item"),
2388 GINT_TO_POINTER (TRUE));
2389 gtk_widget_set_sensitive (filler, FALSE);
2390 gtk_widget_set_no_show_all (filler, TRUE);
2391 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2394 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2397 g_object_ref_sink (info->proxy);
2398 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2403 GtkWidget *menushell;
2406 if (find_menu_position (node, &menushell, &pos))
2408 info->proxy = gtk_action_create_menu_item (action);
2409 g_object_ref_sink (info->proxy);
2410 g_signal_connect (info->proxy, "notify::visible",
2411 G_CALLBACK (update_smart_separators), NULL);
2412 gtk_widget_set_name (info->proxy, info->name);
2414 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2415 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2420 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2424 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2426 g_object_unref (prev_submenu);
2429 if (GTK_IS_MENU (info->proxy))
2432 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2434 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2435 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2437 if (self->private_data->add_tearoffs && !in_popup)
2438 gtk_widget_show (GTK_WIDGET (siblings->data));
2440 gtk_widget_hide (GTK_WIDGET (siblings->data));
2442 g_list_free (siblings);
2445 case NODE_TYPE_UNDECIDED:
2446 g_warning ("found undecided node!");
2448 case NODE_TYPE_ROOT:
2450 case NODE_TYPE_TOOLBAR:
2451 if (info->proxy == NULL)
2453 info->proxy = gtk_toolbar_new ();
2454 g_object_ref_sink (info->proxy);
2455 gtk_widget_set_name (info->proxy, info->name);
2456 gtk_widget_show (info->proxy);
2457 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2460 case NODE_TYPE_MENU_PLACEHOLDER:
2461 /* create menu items for placeholders if necessary ... */
2462 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2463 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2467 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2469 g_object_unref (info->proxy);
2474 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2476 g_object_unref (info->extra);
2480 if (info->proxy == NULL)
2482 GtkWidget *menushell;
2485 if (find_menu_position (node, &menushell, &pos))
2487 info->proxy = gtk_separator_menu_item_new ();
2488 g_object_ref_sink (info->proxy);
2489 g_object_set_data (G_OBJECT (info->proxy),
2490 I_("gtk-separator-mode"),
2491 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2492 gtk_widget_set_no_show_all (info->proxy, TRUE);
2493 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2494 NODE_INFO (node)->proxy, pos);
2496 info->extra = gtk_separator_menu_item_new ();
2497 g_object_ref_sink (info->extra);
2498 g_object_set_data (G_OBJECT (info->extra),
2499 I_("gtk-separator-mode"),
2500 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2501 gtk_widget_set_no_show_all (info->extra, TRUE);
2502 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2503 NODE_INFO (node)->extra, pos + 1);
2507 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2508 /* create toolbar items for placeholders if necessary ... */
2509 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2510 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2514 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2516 g_object_unref (info->proxy);
2521 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2523 g_object_unref (info->extra);
2527 if (info->proxy == NULL)
2533 if (find_toolbar_position (node, &toolbar, &pos))
2535 item = gtk_separator_tool_item_new ();
2536 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2537 info->proxy = GTK_WIDGET (item);
2538 g_object_ref_sink (info->proxy);
2539 g_object_set_data (G_OBJECT (info->proxy),
2540 I_("gtk-separator-mode"),
2541 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2542 gtk_widget_set_no_show_all (info->proxy, TRUE);
2544 item = gtk_separator_tool_item_new ();
2545 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2546 info->extra = GTK_WIDGET (item);
2547 g_object_ref_sink (info->extra);
2548 g_object_set_data (G_OBJECT (info->extra),
2549 I_("gtk-separator-mode"),
2550 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2551 gtk_widget_set_no_show_all (info->extra, TRUE);
2555 case NODE_TYPE_MENUITEM:
2556 /* remove the proxy if it is of the wrong type ... */
2558 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2560 g_signal_handlers_disconnect_by_func (info->proxy,
2561 G_CALLBACK (update_smart_separators),
2563 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2564 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2566 g_object_unref (info->proxy);
2569 /* create proxy if needed ... */
2570 if (info->proxy == NULL)
2572 GtkWidget *menushell;
2575 if (find_menu_position (node, &menushell, &pos))
2577 info->proxy = gtk_action_create_menu_item (action);
2578 g_object_ref_sink (info->proxy);
2579 gtk_widget_set_name (info->proxy, info->name);
2581 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2587 g_signal_handlers_disconnect_by_func (info->proxy,
2588 G_CALLBACK (update_smart_separators),
2590 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2591 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2596 g_signal_connect (info->proxy, "notify::visible",
2597 G_CALLBACK (update_smart_separators), NULL);
2598 if (in_popup && !popup_accels)
2600 /* don't show accels in popups */
2601 GtkWidget *label = GTK_BIN (info->proxy)->child;
2602 g_object_set (label, "accel-closure", NULL, NULL);
2607 case NODE_TYPE_TOOLITEM:
2608 /* remove the proxy if it is of the wrong type ... */
2610 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2612 g_signal_handlers_disconnect_by_func (info->proxy,
2613 G_CALLBACK (update_smart_separators),
2615 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2616 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2618 g_object_unref (info->proxy);
2621 /* create proxy if needed ... */
2622 if (info->proxy == NULL)
2627 if (find_toolbar_position (node, &toolbar, &pos))
2629 info->proxy = gtk_action_create_tool_item (action);
2630 g_object_ref_sink (info->proxy);
2631 gtk_widget_set_name (info->proxy, info->name);
2633 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2634 GTK_TOOL_ITEM (info->proxy), pos);
2639 g_signal_handlers_disconnect_by_func (info->proxy,
2640 G_CALLBACK (update_smart_separators),
2642 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2647 g_signal_connect (info->proxy, "notify::visible",
2648 G_CALLBACK (update_smart_separators), NULL);
2651 case NODE_TYPE_SEPARATOR:
2652 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2653 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2657 gint separator_mode;
2660 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2662 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2664 g_object_unref (info->proxy);
2668 if (find_toolbar_position (node, &toolbar, &pos))
2670 item = gtk_separator_tool_item_new ();
2671 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2672 info->proxy = GTK_WIDGET (item);
2673 g_object_ref_sink (info->proxy);
2674 gtk_widget_set_no_show_all (info->proxy, TRUE);
2677 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2678 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2679 separator_mode = SEPARATOR_MODE_VISIBLE;
2682 separator_mode = SEPARATOR_MODE_SMART;
2684 g_object_set_data (G_OBJECT (info->proxy),
2685 I_("gtk-separator-mode"),
2686 GINT_TO_POINTER (separator_mode));
2687 gtk_widget_show (info->proxy);
2692 GtkWidget *menushell;
2695 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2697 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2699 g_object_unref (info->proxy);
2703 if (find_menu_position (node, &menushell, &pos))
2705 info->proxy = gtk_separator_menu_item_new ();
2706 g_object_ref_sink (info->proxy);
2707 gtk_widget_set_no_show_all (info->proxy, TRUE);
2708 g_object_set_data (G_OBJECT (info->proxy),
2709 I_("gtk-separator-mode"),
2710 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2711 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2713 gtk_widget_show (info->proxy);
2717 case NODE_TYPE_ACCELERATOR:
2718 gtk_action_connect_accelerator (action);
2723 g_object_ref (action);
2725 g_object_unref (info->action);
2726 info->action = action;
2729 /* process children */
2730 child = node->children;
2736 child = current->next;
2737 update_node (self, current, in_popup, popup_accels);
2742 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2743 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2744 else if (info->type == NODE_TYPE_MENU ||
2745 info->type == NODE_TYPE_TOOLBAR ||
2746 info->type == NODE_TYPE_POPUP)
2747 update_smart_separators (info->proxy);
2750 /* handle cleanup of dead nodes */
2751 if (node->children == NULL && info->uifiles == NULL)
2754 gtk_widget_destroy (info->proxy);
2756 gtk_widget_destroy (info->extra);
2757 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
2758 gtk_action_disconnect_accelerator (info->action);
2760 g_node_destroy (node);
2765 do_updates (GtkUIManager *self)
2767 /* this function needs to check through the tree for dirty nodes.
2768 * For such nodes, it needs to do the following:
2770 * 1) check if they are referenced by any loaded UI files anymore.
2771 * In which case, the proxy widget should be destroyed, unless
2772 * there are any subnodes.
2774 * 2) lookup the action for this node again. If it is different to
2775 * the current one (or if no previous action has been looked up),
2776 * the proxy is reconnected to the new action (or a new proxy widget
2777 * is created and added to the parent container).
2779 update_node (self, self->private_data->root_node, FALSE, FALSE);
2781 self->private_data->update_tag = 0;
2787 do_updates_idle (GtkUIManager *self)
2795 queue_update (GtkUIManager *self)
2797 if (self->private_data->update_tag != 0)
2800 self->private_data->update_tag = gdk_threads_add_idle (
2801 (GSourceFunc)do_updates_idle,
2807 * gtk_ui_manager_ensure_update:
2808 * @self: a #GtkUIManager
2810 * Makes sure that all pending updates to the UI have been completed.
2812 * This may occasionally be necessary, since #GtkUIManager updates the
2813 * UI in an idle function. A typical example where this function is
2814 * useful is to enforce that the menubar and toolbar have been added to
2815 * the main window before showing it:
2817 * gtk_container_add (GTK_CONTAINER (window), vbox);
2818 * g_signal_connect (merge, "add-widget",
2819 * G_CALLBACK (add_widget), vbox);
2820 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2821 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2822 * gtk_ui_manager_ensure_update (merge);
2823 * gtk_widget_show (window);
2829 gtk_ui_manager_ensure_update (GtkUIManager *self)
2831 if (self->private_data->update_tag != 0)
2833 g_source_remove (self->private_data->update_tag);
2839 dirty_traverse_func (GNode *node,
2842 NODE_INFO (node)->dirty = TRUE;
2847 dirty_all_nodes (GtkUIManager *self)
2849 g_node_traverse (self->private_data->root_node,
2850 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2851 dirty_traverse_func, NULL);
2852 queue_update (self);
2856 mark_node_dirty (GNode *node)
2860 /* FIXME could optimize this */
2861 for (p = node; p; p = p->parent)
2862 NODE_INFO (p)->dirty = TRUE;
2865 static const gchar *
2866 open_tag_format (NodeType type)
2870 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2871 case NODE_TYPE_ROOT: return "%*s<ui";
2872 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2873 case NODE_TYPE_MENU: return "%*s<menu";
2874 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2875 case NODE_TYPE_MENU_PLACEHOLDER:
2876 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2877 case NODE_TYPE_POPUP: return "%*s<popup";
2878 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2879 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2880 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2881 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2882 default: return NULL;
2886 static const gchar *
2887 close_tag_format (NodeType type)
2891 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2892 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2893 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2894 case NODE_TYPE_MENU: return "%*s</menu>\n";
2895 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2896 case NODE_TYPE_MENU_PLACEHOLDER:
2897 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2898 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2899 default: return NULL;
2904 print_node (GtkUIManager *self,
2911 const gchar *open_fmt;
2912 const gchar *close_fmt;
2916 open_fmt = open_tag_format (mnode->type);
2917 close_fmt = close_tag_format (mnode->type);
2919 g_string_append_printf (buffer, open_fmt, indent_level, "");
2921 if (mnode->type != NODE_TYPE_ROOT)
2924 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2926 if (mnode->action_name)
2927 g_string_append_printf (buffer, " action=\"%s\"",
2928 g_quark_to_string (mnode->action_name));
2931 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2933 for (child = node->children; child != NULL; child = child->next)
2934 print_node (self, child, indent_level + 2, buffer);
2937 g_string_append_printf (buffer, close_fmt, indent_level, "");
2941 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2942 GtkBuilder *builder,
2944 const gchar *tagname,
2945 GMarkupParser *parser,
2951 if (strcmp (tagname, "ui") == 0)
2955 ctx = g_new0 (ParseContext, 1);
2956 ctx->state = STATE_START;
2957 ctx->self = GTK_UI_MANAGER (buildable);
2958 ctx->current = NULL;
2959 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
2962 *parser = ui_parser;
2972 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
2973 GtkBuilder *builder,
2975 const gchar *tagname,
2978 queue_update (GTK_UI_MANAGER (buildable));
2979 g_object_notify (G_OBJECT (buildable), "ui");
2984 * gtk_ui_manager_get_ui:
2985 * @self: a #GtkUIManager
2987 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2989 * Return value: A newly allocated string containing an XML representation of
2995 gtk_ui_manager_get_ui (GtkUIManager *self)
2999 buffer = g_string_new (NULL);
3001 gtk_ui_manager_ensure_update (self);
3003 print_node (self, self->private_data->root_node, 0, buffer);
3005 return g_string_free (buffer, FALSE);
3008 #if defined (G_OS_WIN32) && !defined (_WIN64)
3010 #undef gtk_ui_manager_add_ui_from_file
3013 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
3014 const gchar *filename,
3017 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
3020 if (utf8_filename == NULL)
3023 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
3025 g_free (utf8_filename);
3032 #define __GTK_UI_MANAGER_C__
3033 #include "gtkaliasdef.c"