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 "gtkaccellabel.h"
35 #include "gtkactivatable.h"
36 #include "gtkbuildable.h"
37 #include "gtkimagemenuitem.h"
39 #include "gtkmarshalers.h"
41 #include "gtkmenubar.h"
42 #include "gtkmenutoolbutton.h"
43 #include "gtkseparatormenuitem.h"
44 #include "gtkseparatortoolitem.h"
45 #include "gtktearoffmenuitem.h"
46 #include "gtktoolbar.h"
47 #include "gtkuimanager.h"
48 #include "gtkwindow.h"
49 #include "gtkprivate.h"
51 #undef DEBUG_UI_MANAGER
60 NODE_TYPE_MENU_PLACEHOLDER,
61 NODE_TYPE_TOOLBAR_PLACEHOLDER,
69 typedef struct _Node Node;
79 GtkWidget *extra; /* second separator for placeholders */
84 guint expand : 1; /* used for separators */
85 guint popup_accels : 1;
86 guint always_show_image_set : 1; /* used for menu items */
87 guint always_show_image : 1; /* used for menu items */
90 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
92 struct _GtkUIManagerPrivate
94 GtkAccelGroup *accel_group;
103 gboolean add_tearoffs;
106 #define NODE_INFO(node) ((Node *)node->data)
108 typedef struct _NodeUIReference NodeUIReference;
110 struct _NodeUIReference
116 static void gtk_ui_manager_finalize (GObject *object);
117 static void gtk_ui_manager_set_property (GObject *object,
121 static void gtk_ui_manager_get_property (GObject *object,
125 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
127 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
129 static void queue_update (GtkUIManager *self);
130 static void dirty_all_nodes (GtkUIManager *self);
131 static void mark_node_dirty (GNode *node);
132 static GNode * get_child_node (GtkUIManager *self,
135 const gchar *childname,
136 gint childname_length,
140 static GNode * get_node (GtkUIManager *self,
144 static gboolean free_node (GNode *node);
145 static void node_prepend_ui_reference (GNode *node,
147 GQuark action_quark);
148 static void node_remove_ui_reference (GNode *node,
152 static void gtk_ui_manager_buildable_init (GtkBuildableIface *iface);
153 static void gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
157 static GObject* gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
160 static gboolean gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
163 const gchar *tagname,
164 GMarkupParser *parser,
166 static void gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
169 const gchar *tagname,
192 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
194 G_DEFINE_TYPE_WITH_CODE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT,
195 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
196 gtk_ui_manager_buildable_init))
199 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
201 GObjectClass *gobject_class;
203 gobject_class = G_OBJECT_CLASS (klass);
205 gobject_class->finalize = gtk_ui_manager_finalize;
206 gobject_class->set_property = gtk_ui_manager_set_property;
207 gobject_class->get_property = gtk_ui_manager_get_property;
208 klass->get_widget = gtk_ui_manager_real_get_widget;
209 klass->get_action = gtk_ui_manager_real_get_action;
212 * GtkUIManager:add-tearoffs:
214 * The "add-tearoffs" property controls whether generated menus
215 * have tearoff menu items.
217 * Note that this only affects regular menus. Generated popup
218 * menus never have tearoff menu items.
222 g_object_class_install_property (gobject_class,
224 g_param_spec_boolean ("add-tearoffs",
225 P_("Add tearoffs to menus"),
226 P_("Whether tearoff menu items should be added to menus"),
228 GTK_PARAM_READWRITE));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_string ("ui",
233 P_("Merged UI definition"),
234 P_("An XML string describing the merged UI"),
236 GTK_PARAM_READABLE));
240 * GtkUIManager::add-widget:
241 * @merge: a #GtkUIManager
242 * @widget: the added widget
244 * The ::add-widget signal is emitted for each generated menubar and toolbar.
245 * It is not emitted for generated popup menus, which can be obtained by
246 * gtk_ui_manager_get_widget().
250 ui_manager_signals[ADD_WIDGET] =
251 g_signal_new (I_("add-widget"),
252 G_OBJECT_CLASS_TYPE (klass),
253 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
254 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
256 g_cclosure_marshal_VOID__OBJECT,
261 * GtkUIManager::actions-changed:
262 * @merge: a #GtkUIManager
264 * The ::actions-changed signal is emitted whenever the set of actions
269 ui_manager_signals[ACTIONS_CHANGED] =
270 g_signal_new (I_("actions-changed"),
271 G_OBJECT_CLASS_TYPE (klass),
272 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
273 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
275 g_cclosure_marshal_VOID__VOID,
279 * GtkUIManager::connect-proxy:
280 * @uimanager: the ui manager
281 * @action: the action
284 * The ::connect-proxy signal is emitted after connecting a proxy to
285 * an action in the group.
287 * This is intended for simple customizations for which a custom action
288 * class would be too clumsy, e.g. showing tooltips for menuitems in the
293 ui_manager_signals[CONNECT_PROXY] =
294 g_signal_new (I_("connect-proxy"),
295 G_OBJECT_CLASS_TYPE (klass),
296 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
297 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
299 _gtk_marshal_VOID__OBJECT_OBJECT,
305 * GtkUIManager::disconnect-proxy:
306 * @uimanager: the ui manager
307 * @action: the action
310 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
311 * from an action in the group.
315 ui_manager_signals[DISCONNECT_PROXY] =
316 g_signal_new (I_("disconnect-proxy"),
317 G_OBJECT_CLASS_TYPE (klass),
318 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
319 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
321 _gtk_marshal_VOID__OBJECT_OBJECT,
327 * GtkUIManager::pre-activate:
328 * @uimanager: the ui manager
329 * @action: the action
331 * The ::pre-activate signal is emitted just before the @action
334 * This is intended for applications to get notification
335 * just before any action is activated.
339 ui_manager_signals[PRE_ACTIVATE] =
340 g_signal_new (I_("pre-activate"),
341 G_OBJECT_CLASS_TYPE (klass),
342 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
343 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
345 _gtk_marshal_VOID__OBJECT,
350 * GtkUIManager::post-activate:
351 * @uimanager: the ui manager
352 * @action: the action
354 * The ::post-activate signal is emitted just after the @action
357 * This is intended for applications to get notification
358 * just after any action is activated.
362 ui_manager_signals[POST_ACTIVATE] =
363 g_signal_new (I_("post-activate"),
364 G_OBJECT_CLASS_TYPE (klass),
365 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
366 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
368 _gtk_marshal_VOID__OBJECT,
372 klass->add_widget = NULL;
373 klass->actions_changed = NULL;
374 klass->connect_proxy = NULL;
375 klass->disconnect_proxy = NULL;
376 klass->pre_activate = NULL;
377 klass->post_activate = NULL;
379 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
384 gtk_ui_manager_init (GtkUIManager *self)
389 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
391 self->private_data->accel_group = gtk_accel_group_new ();
393 self->private_data->root_node = NULL;
394 self->private_data->action_groups = NULL;
396 self->private_data->last_merge_id = 0;
397 self->private_data->add_tearoffs = FALSE;
399 merge_id = gtk_ui_manager_new_merge_id (self);
400 node = get_child_node (self, NULL, NULL, "ui", 2,
401 NODE_TYPE_ROOT, TRUE, FALSE);
402 node_prepend_ui_reference (node, merge_id, 0);
406 gtk_ui_manager_finalize (GObject *object)
408 GtkUIManager *self = GTK_UI_MANAGER (object);
410 if (self->private_data->update_tag != 0)
412 g_source_remove (self->private_data->update_tag);
413 self->private_data->update_tag = 0;
416 g_node_traverse (self->private_data->root_node,
417 G_POST_ORDER, G_TRAVERSE_ALL, -1,
418 (GNodeTraverseFunc)free_node, NULL);
419 g_node_destroy (self->private_data->root_node);
420 self->private_data->root_node = NULL;
422 g_list_foreach (self->private_data->action_groups,
423 (GFunc) g_object_unref, NULL);
424 g_list_free (self->private_data->action_groups);
425 self->private_data->action_groups = NULL;
427 g_object_unref (self->private_data->accel_group);
428 self->private_data->accel_group = NULL;
430 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
434 gtk_ui_manager_buildable_init (GtkBuildableIface *iface)
436 iface->add_child = gtk_ui_manager_buildable_add_child;
437 iface->construct_child = gtk_ui_manager_buildable_construct_child;
438 iface->custom_tag_start = gtk_ui_manager_buildable_custom_tag_start;
439 iface->custom_tag_end = gtk_ui_manager_buildable_custom_tag_end;
443 gtk_ui_manager_buildable_add_child (GtkBuildable *buildable,
448 GtkUIManager *self = GTK_UI_MANAGER (buildable);
451 g_return_if_fail (GTK_IS_ACTION_GROUP (child));
453 pos = g_list_length (self->private_data->action_groups);
455 g_object_ref (child);
456 gtk_ui_manager_insert_action_group (self,
457 GTK_ACTION_GROUP (child),
462 child_hierarchy_changed_cb (GtkWidget *widget,
467 GtkAccelGroup *group;
470 toplevel = gtk_widget_get_toplevel (widget);
471 if (!toplevel || !GTK_IS_WINDOW (toplevel))
474 group = gtk_ui_manager_get_accel_group (uimgr);
475 groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
476 if (g_slist_find (groups, group) == NULL)
477 gtk_window_add_accel_group (GTK_WINDOW (toplevel), group);
479 g_signal_handlers_disconnect_by_func (widget,
480 child_hierarchy_changed_cb,
485 gtk_ui_manager_buildable_construct_child (GtkBuildable *buildable,
492 name = g_strdup_printf ("ui/%s", id);
493 widget = gtk_ui_manager_get_widget (GTK_UI_MANAGER (buildable), name);
496 g_error ("Unknown ui manager child: %s\n", name);
502 g_signal_connect (widget, "hierarchy-changed",
503 G_CALLBACK (child_hierarchy_changed_cb),
504 GTK_UI_MANAGER (buildable));
505 return g_object_ref (widget);
509 gtk_ui_manager_set_property (GObject *object,
514 GtkUIManager *self = GTK_UI_MANAGER (object);
518 case PROP_ADD_TEAROFFS:
519 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gtk_ui_manager_get_property (GObject *object,
533 GtkUIManager *self = GTK_UI_MANAGER (object);
537 case PROP_ADD_TEAROFFS:
538 g_value_set_boolean (value, self->private_data->add_tearoffs);
541 g_value_take_string (value, gtk_ui_manager_get_ui (self));
544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
550 gtk_ui_manager_real_get_widget (GtkUIManager *self,
555 /* ensure that there are no pending updates before we get the
557 gtk_ui_manager_ensure_update (self);
559 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
564 return NODE_INFO (node)->proxy;
568 gtk_ui_manager_real_get_action (GtkUIManager *self,
573 /* ensure that there are no pending updates before we get
575 gtk_ui_manager_ensure_update (self);
577 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
582 return NODE_INFO (node)->action;
587 * gtk_ui_manager_new:
589 * Creates a new ui manager object.
591 * Return value: a new ui manager object.
596 gtk_ui_manager_new (void)
598 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
603 * gtk_ui_manager_get_add_tearoffs:
604 * @self: a #GtkUIManager
606 * Returns whether menus generated by this #GtkUIManager
607 * will have tearoff menu items.
609 * Return value: whether tearoff menu items are added
614 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
616 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
618 return self->private_data->add_tearoffs;
623 * gtk_ui_manager_set_add_tearoffs:
624 * @self: a #GtkUIManager
625 * @add_tearoffs: whether tearoff menu items are added
627 * Sets the "add_tearoffs" property, which controls whether menus
628 * generated by this #GtkUIManager will have tearoff menu items.
630 * Note that this only affects regular menus. Generated popup
631 * menus never have tearoff menu items.
636 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
637 gboolean add_tearoffs)
639 g_return_if_fail (GTK_IS_UI_MANAGER (self));
641 add_tearoffs = add_tearoffs != FALSE;
643 if (add_tearoffs != self->private_data->add_tearoffs)
645 self->private_data->add_tearoffs = add_tearoffs;
647 dirty_all_nodes (self);
649 g_object_notify (G_OBJECT (self), "add-tearoffs");
654 cb_proxy_connect_proxy (GtkActionGroup *group,
659 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
663 cb_proxy_disconnect_proxy (GtkActionGroup *group,
668 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
672 cb_proxy_pre_activate (GtkActionGroup *group,
676 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
680 cb_proxy_post_activate (GtkActionGroup *group,
684 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
688 * gtk_ui_manager_insert_action_group:
689 * @self: a #GtkUIManager object
690 * @action_group: the action group to be inserted
691 * @pos: the position at which the group will be inserted.
693 * Inserts an action group into the list of action groups associated
694 * with @self. Actions in earlier groups hide actions with the same
695 * name in later groups.
700 gtk_ui_manager_insert_action_group (GtkUIManager *self,
701 GtkActionGroup *action_group,
704 #ifdef G_ENABLE_DEBUG
706 const char *group_name;
709 g_return_if_fail (GTK_IS_UI_MANAGER (self));
710 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
711 g_return_if_fail (g_list_find (self->private_data->action_groups,
712 action_group) == NULL);
714 #ifdef G_ENABLE_DEBUG
715 group_name = gtk_action_group_get_name (action_group);
717 for (l = self->private_data->action_groups; l; l = l->next)
719 GtkActionGroup *group = l->data;
721 if (strcmp (gtk_action_group_get_name (group), group_name) == 0)
723 g_warning ("Inserting action group '%s' into UI manager which "
724 "already has a group with this name\n", group_name);
728 #endif /* G_ENABLE_DEBUG */
730 g_object_ref (action_group);
731 self->private_data->action_groups =
732 g_list_insert (self->private_data->action_groups, action_group, pos);
733 g_object_connect (action_group,
734 "object-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
735 "object-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
736 "object-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), self,
737 "object-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), self,
740 /* dirty all nodes, as action bindings may change */
741 dirty_all_nodes (self);
743 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
747 * gtk_ui_manager_remove_action_group:
748 * @self: a #GtkUIManager object
749 * @action_group: the action group to be removed
751 * Removes an action group from the list of action groups associated
757 gtk_ui_manager_remove_action_group (GtkUIManager *self,
758 GtkActionGroup *action_group)
760 g_return_if_fail (GTK_IS_UI_MANAGER (self));
761 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
762 g_return_if_fail (g_list_find (self->private_data->action_groups,
763 action_group) != NULL);
765 self->private_data->action_groups =
766 g_list_remove (self->private_data->action_groups, action_group);
768 g_object_disconnect (action_group,
769 "any-signal::connect-proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
770 "any-signal::disconnect-proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
771 "any-signal::pre-activate", G_CALLBACK (cb_proxy_pre_activate), self,
772 "any-signal::post-activate", G_CALLBACK (cb_proxy_post_activate), self,
774 g_object_unref (action_group);
776 /* dirty all nodes, as action bindings may change */
777 dirty_all_nodes (self);
779 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
783 * gtk_ui_manager_get_action_groups:
784 * @self: a #GtkUIManager object
786 * Returns the list of action groups associated with @self.
788 * Return value: (element-type GtkActionGroup) (transfer none): a #GList of
789 * action groups. The list is owned by GTK+
790 * and should not be modified.
795 gtk_ui_manager_get_action_groups (GtkUIManager *self)
797 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
799 return self->private_data->action_groups;
803 * gtk_ui_manager_get_accel_group:
804 * @self: a #GtkUIManager object
806 * Returns the #GtkAccelGroup associated with @self.
808 * Return value: (transfer none): the #GtkAccelGroup.
813 gtk_ui_manager_get_accel_group (GtkUIManager *self)
815 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
817 return self->private_data->accel_group;
821 * gtk_ui_manager_get_widget:
822 * @self: a #GtkUIManager
825 * Looks up a widget by following a path.
826 * The path consists of the names specified in the XML description of the UI.
827 * separated by '/'. Elements which don't have a name or action attribute in
828 * the XML (e.g. <popup>) can be addressed by their XML element name
829 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
831 * Note that the widget found by following a path that ends in a <menu>
832 * element is the menuitem to which the menu is attached, not the menu itself.
834 * Also note that the widgets constructed by a ui manager are not tied to
835 * the lifecycle of the ui manager. If you add the widgets returned by this
836 * function to some container or explicitly ref them, they will survive the
837 * destruction of the ui manager.
839 * Return value: (transfer none): the widget found by following the path, or %NULL if no widget
845 gtk_ui_manager_get_widget (GtkUIManager *self,
848 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
849 g_return_val_if_fail (path != NULL, NULL);
851 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
855 GtkUIManagerItemType types;
860 collect_toplevels (GNode *node,
863 ToplevelData *data = user_data;
865 if (NODE_INFO (node)->proxy)
867 switch (NODE_INFO (node)->type)
869 case NODE_TYPE_MENUBAR:
870 if (data->types & GTK_UI_MANAGER_MENUBAR)
871 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
873 case NODE_TYPE_TOOLBAR:
874 if (data->types & GTK_UI_MANAGER_TOOLBAR)
875 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
877 case NODE_TYPE_POPUP:
878 if (data->types & GTK_UI_MANAGER_POPUP)
879 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
887 * gtk_ui_manager_get_toplevels:
888 * @self: a #GtkUIManager
889 * @types: specifies the types of toplevel widgets to include. Allowed
890 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
891 * #GTK_UI_MANAGER_POPUP.
893 * Obtains a list of all toplevel widgets of the requested types.
895 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated #GSList of
896 * all toplevel widgets of the requested types. Free the returned list with g_slist_free().
901 gtk_ui_manager_get_toplevels (GtkUIManager *self,
902 GtkUIManagerItemType types)
906 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
907 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
908 GTK_UI_MANAGER_TOOLBAR |
909 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
915 g_node_children_foreach (self->private_data->root_node,
917 collect_toplevels, &data);
924 * gtk_ui_manager_get_action:
925 * @self: a #GtkUIManager
928 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
929 * for more information about paths.
931 * Return value: the action whose proxy widget is found by following the path,
932 * or %NULL if no widget was found.
937 gtk_ui_manager_get_action (GtkUIManager *self,
940 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
941 g_return_val_if_fail (path != NULL, NULL);
943 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
947 node_is_dead (GNode *node)
951 if (NODE_INFO (node)->uifiles != NULL)
954 for (child = node->children; child != NULL; child = child->next)
956 if (!node_is_dead (child))
964 get_child_node (GtkUIManager *self,
967 const gchar *childname,
968 gint childname_length,
979 for (child = parent->children; child != NULL; child = child->next)
981 if (NODE_INFO (child)->name &&
982 strlen (NODE_INFO (child)->name) == childname_length &&
983 !strncmp (NODE_INFO (child)->name, childname, childname_length))
985 /* if undecided about node type, set it */
986 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
987 NODE_INFO (child)->type = node_type;
989 /* warn about type mismatch */
990 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
991 node_type != NODE_TYPE_UNDECIDED &&
992 NODE_INFO (child)->type != node_type)
993 g_warning ("node type doesn't match %d (%s is type %d)",
995 NODE_INFO (child)->name,
996 NODE_INFO (child)->type);
998 if (node_is_dead (child))
1000 /* This node was removed but is still dirty so
1001 * it is still in the tree. We want to treat this
1002 * as if it didn't exist, which means we move it
1003 * to the position it would have been created at.
1005 g_node_unlink (child);
1013 if (!child && create)
1017 mnode = g_slice_new0 (Node);
1018 mnode->type = node_type;
1019 mnode->name = g_strndup (childname, childname_length);
1021 child = g_node_new (mnode);
1026 g_node_insert_before (parent, sibling, child);
1028 g_node_insert_after (parent, sibling, child);
1033 g_node_prepend (parent, child);
1035 g_node_append (parent, child);
1038 mark_node_dirty (child);
1043 /* handle root node */
1044 if (self->private_data->root_node)
1046 child = self->private_data->root_node;
1047 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
1048 g_warning ("root node name '%s' doesn't match '%s'",
1049 childname, NODE_INFO (child)->name);
1050 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
1051 g_warning ("base element must be of type ROOT");
1057 mnode = g_slice_new0 (Node);
1058 mnode->type = node_type;
1059 mnode->name = g_strndup (childname, childname_length);
1060 mnode->dirty = TRUE;
1062 child = self->private_data->root_node = g_node_new (mnode);
1070 get_node (GtkUIManager *self,
1075 const gchar *pos, *end;
1076 GNode *parent, *node;
1078 if (strncmp ("/ui", path, 3) == 0)
1081 end = path + strlen (path);
1083 parent = node = NULL;
1089 slash = strchr (pos, '/');
1091 length = slash - pos;
1093 length = strlen (pos);
1095 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
1100 pos += length + 1; /* move past the node name and the slash too */
1104 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
1105 NODE_INFO (node)->type = node_type;
1111 node_ui_reference_free (gpointer data)
1113 g_slice_free (NodeUIReference, data);
1117 free_node (GNode *node)
1119 Node *info = NODE_INFO (node);
1121 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
1122 g_list_free (info->uifiles);
1125 g_object_unref (info->action);
1127 g_object_unref (info->proxy);
1129 g_object_unref (info->extra);
1130 g_free (info->name);
1131 g_slice_free (Node, info);
1137 * gtk_ui_manager_new_merge_id:
1138 * @self: a #GtkUIManager
1140 * Returns an unused merge id, suitable for use with
1141 * gtk_ui_manager_add_ui().
1143 * Return value: an unused merge id.
1148 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1150 self->private_data->last_merge_id++;
1152 return self->private_data->last_merge_id;
1156 node_prepend_ui_reference (GNode *gnode,
1158 GQuark action_quark)
1160 Node *node = NODE_INFO (gnode);
1161 NodeUIReference *reference = NULL;
1163 if (node->uifiles &&
1164 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1165 reference = node->uifiles->data;
1168 reference = g_slice_new (NodeUIReference);
1169 node->uifiles = g_list_prepend (node->uifiles, reference);
1172 reference->merge_id = merge_id;
1173 reference->action_quark = action_quark;
1175 mark_node_dirty (gnode);
1179 node_remove_ui_reference (GNode *gnode,
1182 Node *node = NODE_INFO (gnode);
1185 for (p = node->uifiles; p != NULL; p = p->next)
1187 NodeUIReference *reference = p->data;
1189 if (reference->merge_id == merge_id)
1191 if (p == node->uifiles)
1192 mark_node_dirty (gnode);
1193 node->uifiles = g_list_delete_link (node->uifiles, p);
1194 g_slice_free (NodeUIReference, reference);
1201 /* -------------------- The UI file parser -------------------- */
1215 typedef struct _ParseContext ParseContext;
1216 struct _ParseContext
1219 ParseState prev_state;
1229 start_element_handler (GMarkupParseContext *context,
1230 const gchar *element_name,
1231 const gchar **attribute_names,
1232 const gchar **attribute_values,
1236 ParseContext *ctx = user_data;
1237 GtkUIManager *self = ctx->self;
1240 const gchar *node_name;
1241 const gchar *action;
1242 GQuark action_quark;
1244 gboolean expand = FALSE;
1245 gboolean accelerators = FALSE;
1246 gboolean always_show_image_set = FALSE, always_show_image = FALSE;
1248 gboolean raise_error = TRUE;
1255 for (i = 0; attribute_names[i] != NULL; i++)
1257 if (!strcmp (attribute_names[i], "name"))
1259 node_name = attribute_values[i];
1261 else if (!strcmp (attribute_names[i], "action"))
1263 action = attribute_values[i];
1264 action_quark = g_quark_from_string (attribute_values[i]);
1266 else if (!strcmp (attribute_names[i], "position"))
1268 top = !strcmp (attribute_values[i], "top");
1270 else if (!strcmp (attribute_names[i], "expand"))
1272 expand = !strcmp (attribute_values[i], "true");
1274 else if (!strcmp (attribute_names[i], "accelerators"))
1276 accelerators = !strcmp (attribute_values[i], "true");
1278 else if (!strcmp (attribute_names[i], "always-show-image"))
1280 always_show_image_set = TRUE;
1281 always_show_image = !strcmp (attribute_values[i], "true");
1283 /* else silently skip unknown attributes to be compatible with
1284 * future additional attributes.
1288 /* Work out a name for this node. Either the name attribute, or
1289 * the action, or the element name */
1290 if (node_name == NULL)
1295 node_name = element_name;
1298 switch (element_name[0])
1301 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1303 ctx->state = STATE_ACCELERATOR;
1304 ctx->current = get_child_node (self, ctx->current, NULL,
1305 node_name, strlen (node_name),
1306 NODE_TYPE_ACCELERATOR,
1308 if (NODE_INFO (ctx->current)->action_name == 0)
1309 NODE_INFO (ctx->current)->action_name = action_quark;
1311 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1313 raise_error = FALSE;
1317 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1319 ctx->state = STATE_ROOT;
1320 ctx->current = self->private_data->root_node;
1321 raise_error = FALSE;
1323 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1327 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1329 ctx->state = STATE_MENU;
1330 ctx->current = get_child_node (self, ctx->current, NULL,
1331 node_name, strlen (node_name),
1334 if (NODE_INFO (ctx->current)->action_name == 0)
1335 NODE_INFO (ctx->current)->action_name = action_quark;
1337 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1338 mark_node_dirty (ctx->current);
1340 raise_error = FALSE;
1342 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1344 ctx->current = get_child_node (self, ctx->current, NULL,
1345 node_name, strlen (node_name),
1348 if (NODE_INFO (ctx->current)->action_name == 0)
1349 NODE_INFO (ctx->current)->action_name = action_quark;
1351 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1353 raise_error = FALSE;
1355 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1357 ctx->state = STATE_MENU;
1359 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
1360 node_name, strlen (node_name),
1363 if (NODE_INFO (ctx->current)->action_name == 0)
1364 NODE_INFO (ctx->current)->action_name = action_quark;
1366 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1368 raise_error = FALSE;
1370 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1374 ctx->state = STATE_MENUITEM;
1375 node = get_child_node (self, ctx->current, NULL,
1376 node_name, strlen (node_name),
1379 if (NODE_INFO (node)->action_name == 0)
1380 NODE_INFO (node)->action_name = action_quark;
1382 NODE_INFO (node)->always_show_image_set = always_show_image_set;
1383 NODE_INFO (node)->always_show_image = always_show_image;
1385 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1387 raise_error = FALSE;
1391 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1393 ctx->state = STATE_MENU;
1394 ctx->current = get_child_node (self, ctx->current, NULL,
1395 node_name, strlen (node_name),
1399 NODE_INFO (ctx->current)->popup_accels = accelerators;
1401 if (NODE_INFO (ctx->current)->action_name == 0)
1402 NODE_INFO (ctx->current)->action_name = action_quark;
1404 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1406 raise_error = FALSE;
1408 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1409 !strcmp (element_name, "placeholder"))
1411 if (ctx->state == STATE_TOOLBAR)
1412 ctx->current = get_child_node (self, ctx->current, NULL,
1413 node_name, strlen (node_name),
1414 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1417 ctx->current = get_child_node (self, ctx->current, NULL,
1418 node_name, strlen (node_name),
1419 NODE_TYPE_MENU_PLACEHOLDER,
1422 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1424 raise_error = FALSE;
1428 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1429 !strcmp (element_name, "separator"))
1434 if (ctx->state == STATE_TOOLBAR)
1435 ctx->state = STATE_TOOLITEM;
1437 ctx->state = STATE_MENUITEM;
1438 if (!strcmp (node_name, "separator"))
1444 length = strlen (node_name);
1445 node = get_child_node (self, ctx->current, NULL,
1447 NODE_TYPE_SEPARATOR,
1450 NODE_INFO (node)->expand = expand;
1452 if (NODE_INFO (node)->action_name == 0)
1453 NODE_INFO (node)->action_name = action_quark;
1455 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1457 raise_error = FALSE;
1461 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1463 ctx->state = STATE_TOOLBAR;
1464 ctx->current = get_child_node (self, ctx->current, NULL,
1465 node_name, strlen (node_name),
1468 if (NODE_INFO (ctx->current)->action_name == 0)
1469 NODE_INFO (ctx->current)->action_name = action_quark;
1471 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1473 raise_error = FALSE;
1475 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1479 ctx->state = STATE_TOOLITEM;
1480 node = get_child_node (self, ctx->current, NULL,
1481 node_name, strlen (node_name),
1484 if (NODE_INFO (node)->action_name == 0)
1485 NODE_INFO (node)->action_name = action_quark;
1487 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1489 raise_error = FALSE;
1497 gint line_number, char_number;
1499 g_markup_parse_context_get_position (context,
1500 &line_number, &char_number);
1503 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1504 _("Unexpected start tag '%s' on line %d char %d"),
1506 line_number, char_number);
1511 end_element_handler (GMarkupParseContext *context,
1512 const gchar *element_name,
1516 ParseContext *ctx = user_data;
1522 /* no need to GError here, GMarkup already catches this */
1525 ctx->current = NULL;
1526 ctx->state = STATE_END;
1530 case STATE_ACCELERATOR:
1531 ctx->current = ctx->current->parent;
1532 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1533 ctx->state = STATE_ROOT;
1534 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1536 ctx->current = ctx->current->parent;
1537 ctx->state = STATE_TOOLITEM;
1539 /* else, stay in same state */
1541 case STATE_MENUITEM:
1542 ctx->state = STATE_MENU;
1544 case STATE_TOOLITEM:
1545 ctx->state = STATE_TOOLBAR;
1551 cleanup (GMarkupParseContext *context,
1555 ParseContext *ctx = user_data;
1557 ctx->current = NULL;
1558 /* should also walk through the tree and get rid of nodes related to
1559 * this UI file's tag */
1561 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1565 xml_isspace (char c)
1567 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1571 text_handler (GMarkupParseContext *context,
1581 end = text + text_len;
1582 while (p != end && xml_isspace (*p))
1587 gint line_number, char_number;
1589 g_markup_parse_context_get_position (context,
1590 &line_number, &char_number);
1593 G_MARKUP_ERROR_INVALID_CONTENT,
1594 _("Unexpected character data on line %d char %d"),
1595 line_number, char_number);
1600 static const GMarkupParser ui_parser = {
1601 start_element_handler,
1602 end_element_handler,
1609 add_ui_from_string (GtkUIManager *self,
1610 const gchar *buffer,
1612 gboolean needs_root,
1615 ParseContext ctx = { 0 };
1616 GMarkupParseContext *context;
1618 ctx.state = STATE_START;
1621 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1623 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1626 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1629 if (!g_markup_parse_context_parse (context, buffer, length, error))
1633 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1636 if (!g_markup_parse_context_end_parse (context, error))
1639 g_markup_parse_context_free (context);
1641 queue_update (self);
1643 g_object_notify (G_OBJECT (self), "ui");
1645 return ctx.merge_id;
1649 g_markup_parse_context_free (context);
1655 * gtk_ui_manager_add_ui_from_string:
1656 * @self: a #GtkUIManager object
1657 * @buffer: the string to parse
1658 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1659 * @error: return location for an error
1661 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1662 * merges it with the current contents of @self. An enclosing <ui>
1663 * element is added if it is missing.
1665 * Return value: The merge id for the merged UI. The merge id can be used
1666 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1667 * the return value is 0.
1672 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1673 const gchar *buffer,
1677 gboolean needs_root = TRUE;
1681 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1682 g_return_val_if_fail (buffer != NULL, 0);
1685 length = strlen (buffer);
1688 end = buffer + length;
1689 while (p != end && xml_isspace (*p))
1692 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1695 return add_ui_from_string (self, buffer, length, needs_root, error);
1699 * gtk_ui_manager_add_ui_from_file:
1700 * @self: a #GtkUIManager object
1701 * @filename: the name of the file to parse
1702 * @error: return location for an error
1704 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1705 * merges it with the current contents of @self.
1707 * Return value: The merge id for the merged UI. The merge id can be used
1708 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1709 * the return value is 0.
1714 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1715 const gchar *filename,
1722 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1724 if (!g_file_get_contents (filename, &buffer, &length, error))
1727 res = add_ui_from_string (self, buffer, length, FALSE, error);
1734 * gtk_ui_manager_add_ui:
1735 * @self: a #GtkUIManager
1736 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1738 * @name: the name for the added UI element
1739 * @action: (allow-none): the name of the action to be proxied, or %NULL to add a separator
1740 * @type: the type of UI element to add.
1741 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1742 * is added after its siblings.
1744 * Adds a UI element to the current contents of @self.
1746 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1747 * separator if such an element can be inserted at the place determined by
1748 * @path. Otherwise @type must indicate an element that can be inserted at
1749 * the place determined by @path.
1751 * If @path points to a menuitem or toolitem, the new element will be inserted
1752 * before or after this item, depending on @top.
1757 gtk_ui_manager_add_ui (GtkUIManager *self,
1761 const gchar *action,
1762 GtkUIManagerItemType type,
1769 GQuark action_quark = 0;
1771 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1772 g_return_if_fail (merge_id > 0);
1773 g_return_if_fail (name != NULL || type == GTK_UI_MANAGER_SEPARATOR);
1775 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1781 node_type = NODE_TYPE_UNDECIDED;
1784 switch (NODE_INFO (node)->type)
1786 case NODE_TYPE_SEPARATOR:
1787 case NODE_TYPE_MENUITEM:
1788 case NODE_TYPE_TOOLITEM:
1790 node = node->parent;
1792 case NODE_TYPE_MENUBAR:
1793 case NODE_TYPE_MENU:
1794 case NODE_TYPE_POPUP:
1795 case NODE_TYPE_MENU_PLACEHOLDER:
1798 case GTK_UI_MANAGER_AUTO:
1800 node_type = NODE_TYPE_MENUITEM;
1802 node_type = NODE_TYPE_SEPARATOR;
1804 case GTK_UI_MANAGER_MENU:
1805 node_type = NODE_TYPE_MENU;
1807 case GTK_UI_MANAGER_MENUITEM:
1808 node_type = NODE_TYPE_MENUITEM;
1810 case GTK_UI_MANAGER_SEPARATOR:
1811 node_type = NODE_TYPE_SEPARATOR;
1813 case GTK_UI_MANAGER_PLACEHOLDER:
1814 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1820 case NODE_TYPE_TOOLBAR:
1821 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1824 case GTK_UI_MANAGER_AUTO:
1826 node_type = NODE_TYPE_TOOLITEM;
1828 node_type = NODE_TYPE_SEPARATOR;
1830 case GTK_UI_MANAGER_TOOLITEM:
1831 node_type = NODE_TYPE_TOOLITEM;
1833 case GTK_UI_MANAGER_SEPARATOR:
1834 node_type = NODE_TYPE_SEPARATOR;
1836 case GTK_UI_MANAGER_PLACEHOLDER:
1837 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1843 case NODE_TYPE_ROOT:
1846 case GTK_UI_MANAGER_MENUBAR:
1847 node_type = NODE_TYPE_MENUBAR;
1849 case GTK_UI_MANAGER_TOOLBAR:
1850 node_type = NODE_TYPE_TOOLBAR;
1852 case GTK_UI_MANAGER_POPUP:
1853 case GTK_UI_MANAGER_POPUP_WITH_ACCELS:
1854 node_type = NODE_TYPE_POPUP;
1856 case GTK_UI_MANAGER_ACCELERATOR:
1857 node_type = NODE_TYPE_ACCELERATOR;
1867 if (node_type == NODE_TYPE_UNDECIDED)
1869 g_warning ("item type %d not suitable for adding at '%s'",
1874 child = get_child_node (self, node, sibling,
1875 name, name ? strlen (name) : 0,
1876 node_type, TRUE, top);
1878 if (type == GTK_UI_MANAGER_POPUP_WITH_ACCELS)
1879 NODE_INFO (child)->popup_accels = TRUE;
1882 action_quark = g_quark_from_string (action);
1884 node_prepend_ui_reference (child, merge_id, action_quark);
1886 if (NODE_INFO (child)->action_name == 0)
1887 NODE_INFO (child)->action_name = action_quark;
1889 queue_update (self);
1891 g_object_notify (G_OBJECT (self), "ui");
1895 remove_ui (GNode *node,
1898 guint merge_id = GPOINTER_TO_UINT (user_data);
1900 node_remove_ui_reference (node, merge_id);
1902 return FALSE; /* continue */
1906 * gtk_ui_manager_remove_ui:
1907 * @self: a #GtkUIManager object
1908 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1910 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1915 gtk_ui_manager_remove_ui (GtkUIManager *self,
1918 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1920 g_node_traverse (self->private_data->root_node,
1921 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1922 remove_ui, GUINT_TO_POINTER (merge_id));
1924 queue_update (self);
1926 g_object_notify (G_OBJECT (self), "ui");
1929 /* -------------------- Updates -------------------- */
1933 get_action_by_name (GtkUIManager *merge,
1934 const gchar *action_name)
1942 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1944 GtkActionGroup *action_group = tmp->data;
1947 action = gtk_action_group_get_action (action_group, action_name);
1957 find_menu_position (GNode *node,
1958 GtkWidget **menushell_p,
1961 GtkWidget *menushell;
1964 g_return_val_if_fail (node != NULL, FALSE);
1965 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1966 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1967 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1968 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1969 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1972 /* first sibling -- look at parent */
1973 if (node->prev == NULL)
1978 parent = node->parent;
1979 switch (NODE_INFO (parent)->type)
1981 case NODE_TYPE_MENUBAR:
1982 case NODE_TYPE_POPUP:
1983 menushell = NODE_INFO (parent)->proxy;
1986 case NODE_TYPE_MENU:
1987 menushell = NODE_INFO (parent)->proxy;
1988 if (GTK_IS_MENU_ITEM (menushell))
1989 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1990 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1991 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1995 g_list_free (siblings);
1997 case NODE_TYPE_MENU_PLACEHOLDER:
1998 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1999 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
2000 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
2001 NODE_INFO (parent)->proxy) + 1;
2004 g_warning ("%s: bad parent node type %d", G_STRLOC,
2005 NODE_INFO (parent)->type);
2011 GtkWidget *prev_child;
2014 sibling = node->prev;
2015 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
2016 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2018 prev_child = NODE_INFO (sibling)->proxy;
2020 if (!GTK_IS_WIDGET (prev_child))
2023 menushell = gtk_widget_get_parent (prev_child);
2024 if (!GTK_IS_MENU_SHELL (menushell))
2027 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
2031 *menushell_p = menushell;
2039 find_toolbar_position (GNode *node,
2040 GtkWidget **toolbar_p,
2046 g_return_val_if_fail (node != NULL, FALSE);
2047 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
2048 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
2049 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
2050 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
2053 /* first sibling -- look at parent */
2054 if (node->prev == NULL)
2058 parent = node->parent;
2059 switch (NODE_INFO (parent)->type)
2061 case NODE_TYPE_TOOLBAR:
2062 toolbar = NODE_INFO (parent)->proxy;
2065 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2066 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
2067 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2068 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2069 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
2072 g_warning ("%s: bad parent node type %d", G_STRLOC,
2073 NODE_INFO (parent)->type);
2079 GtkWidget *prev_child;
2082 sibling = node->prev;
2083 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2084 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
2086 prev_child = NODE_INFO (sibling)->proxy;
2088 if (!GTK_IS_WIDGET (prev_child))
2091 toolbar = gtk_widget_get_parent (prev_child);
2092 if (!GTK_IS_TOOLBAR (toolbar))
2095 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
2096 GTK_TOOL_ITEM (prev_child)) + 1;
2100 *toolbar_p = toolbar;
2108 * _gtk_menu_is_empty:
2109 * @menu: (allow-none): a #GtkMenu or %NULL
2111 * Determines whether @menu is empty. A menu is considered empty if it
2112 * the only visible children are tearoff menu items or "filler" menu
2113 * items which were inserted to mark the menu as empty.
2115 * This function is used by #GtkAction.
2117 * Return value: whether @menu is empty.
2120 _gtk_menu_is_empty (GtkWidget *menu)
2122 GList *children, *cur;
2123 gboolean result = TRUE;
2125 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
2130 children = gtk_container_get_children (GTK_CONTAINER (menu));
2135 if (gtk_widget_get_visible (cur->data))
2137 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
2138 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
2146 g_list_free (children);
2152 SEPARATOR_MODE_SMART,
2153 SEPARATOR_MODE_VISIBLE,
2154 SEPARATOR_MODE_HIDDEN
2158 update_smart_separators (GtkWidget *proxy)
2160 GtkWidget *parent = NULL;
2162 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
2164 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
2165 parent = gtk_widget_get_parent (proxy);
2171 GList *children, *cur, *last;
2174 children = gtk_container_get_children (GTK_CONTAINER (parent));
2184 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2188 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2189 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2192 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2193 "gtk-separator-mode"));
2196 case SEPARATOR_MODE_VISIBLE:
2197 gtk_widget_show (GTK_WIDGET (cur->data));
2201 case SEPARATOR_MODE_HIDDEN:
2202 gtk_widget_hide (GTK_WIDGET (cur->data));
2204 case SEPARATOR_MODE_SMART:
2207 gtk_widget_show (GTK_WIDGET (cur->data));
2212 gtk_widget_hide (GTK_WIDGET (cur->data));
2216 else if (gtk_widget_get_visible (cur->data))
2219 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2232 gtk_widget_hide (GTK_WIDGET (last->data));
2234 if (GTK_IS_MENU (parent))
2238 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2239 if (GTK_IS_MENU_ITEM (item))
2240 _gtk_action_sync_menu_visible (NULL, item, empty);
2241 if (GTK_IS_WIDGET (filler))
2244 gtk_widget_show (filler);
2246 gtk_widget_hide (filler);
2250 g_list_free (children);
2255 update_node (GtkUIManager *self,
2258 gboolean popup_accels)
2263 const gchar *action_name;
2264 NodeUIReference *ref;
2266 #ifdef DEBUG_UI_MANAGER
2270 g_return_if_fail (node != NULL);
2271 g_return_if_fail (NODE_INFO (node) != NULL);
2273 info = NODE_INFO (node);
2278 if (info->type == NODE_TYPE_POPUP)
2281 popup_accels = info->popup_accels;
2284 #ifdef DEBUG_UI_MANAGER
2285 g_print ("update_node name=%s dirty=%d popup %d (",
2286 info->name, info->dirty, in_popup);
2287 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2289 NodeUIReference *ref = tmp->data;
2290 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2297 if (info->uifiles == NULL) {
2298 /* We may need to remove this node.
2299 * This must be done in post order
2301 goto recurse_children;
2304 ref = info->uifiles->data;
2305 action_name = g_quark_to_string (ref->action_quark);
2306 action = get_action_by_name (self, action_name);
2308 info->dirty = FALSE;
2310 /* Check if the node doesn't have an action and must have an action */
2311 if (action == NULL &&
2312 info->type != NODE_TYPE_ROOT &&
2313 info->type != NODE_TYPE_MENUBAR &&
2314 info->type != NODE_TYPE_TOOLBAR &&
2315 info->type != NODE_TYPE_POPUP &&
2316 info->type != NODE_TYPE_SEPARATOR &&
2317 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2318 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2320 g_warning ("%s: missing action %s", info->name, action_name);
2326 gtk_action_set_accel_group (action, self->private_data->accel_group);
2328 /* If the widget already has a proxy and the action hasn't changed, then
2329 * we only have to update the tearoff menu items.
2331 if (info->proxy != NULL && action == info->action)
2333 if (info->type == NODE_TYPE_MENU)
2338 if (GTK_IS_MENU (info->proxy))
2341 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2342 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2343 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2345 if (self->private_data->add_tearoffs && !in_popup)
2346 gtk_widget_show (GTK_WIDGET (siblings->data));
2348 gtk_widget_hide (GTK_WIDGET (siblings->data));
2350 g_list_free (siblings);
2353 goto recurse_children;
2358 case NODE_TYPE_MENUBAR:
2359 if (info->proxy == NULL)
2361 info->proxy = gtk_menu_bar_new ();
2362 g_object_ref_sink (info->proxy);
2363 gtk_widget_set_name (info->proxy, info->name);
2364 gtk_widget_show (info->proxy);
2365 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2368 case NODE_TYPE_POPUP:
2369 if (info->proxy == NULL)
2371 info->proxy = gtk_menu_new ();
2372 g_object_ref_sink (info->proxy);
2374 gtk_widget_set_name (info->proxy, info->name);
2376 case NODE_TYPE_MENU:
2378 GtkWidget *prev_submenu = NULL;
2379 GtkWidget *menu = NULL;
2382 /* remove the proxy if it is of the wrong type ... */
2384 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2386 if (GTK_IS_MENU_ITEM (info->proxy))
2388 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2391 g_object_ref (prev_submenu);
2392 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2396 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2397 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2399 g_object_unref (info->proxy);
2403 /* create proxy if needed ... */
2404 if (info->proxy == NULL)
2406 /* ... if the action already provides a menu, then use
2407 * that menu instead of creating an empty one
2409 if ((NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2410 NODE_INFO (node->parent)->type == NODE_TYPE_MENUITEM) &&
2411 GTK_ACTION_GET_CLASS (action)->create_menu)
2413 menu = gtk_action_create_menu (action);
2421 menu = gtk_menu_new ();
2422 gtk_widget_set_name (menu, info->name);
2423 tearoff = gtk_tearoff_menu_item_new ();
2424 gtk_widget_set_no_show_all (tearoff, TRUE);
2425 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2426 filler = gtk_menu_item_new_with_label (_("Empty"));
2427 g_object_set_data (G_OBJECT (filler),
2428 I_("gtk-empty-menu-item"),
2429 GINT_TO_POINTER (TRUE));
2430 gtk_widget_set_sensitive (filler, FALSE);
2431 gtk_widget_set_no_show_all (filler, TRUE);
2432 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2435 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2438 g_object_ref_sink (info->proxy);
2439 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2444 GtkWidget *menushell;
2447 if (find_menu_position (node, &menushell, &pos))
2449 info->proxy = gtk_action_create_menu_item (action);
2450 g_object_ref_sink (info->proxy);
2451 g_signal_connect (info->proxy, "notify::visible",
2452 G_CALLBACK (update_smart_separators), NULL);
2453 gtk_widget_set_name (info->proxy, info->name);
2455 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2456 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2461 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2465 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2467 g_object_unref (prev_submenu);
2470 if (GTK_IS_MENU (info->proxy))
2473 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2475 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2476 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2478 if (self->private_data->add_tearoffs && !in_popup)
2479 gtk_widget_show (GTK_WIDGET (siblings->data));
2481 gtk_widget_hide (GTK_WIDGET (siblings->data));
2483 g_list_free (siblings);
2486 case NODE_TYPE_UNDECIDED:
2487 g_warning ("found undecided node!");
2489 case NODE_TYPE_ROOT:
2491 case NODE_TYPE_TOOLBAR:
2492 if (info->proxy == NULL)
2494 info->proxy = gtk_toolbar_new ();
2495 g_object_ref_sink (info->proxy);
2496 gtk_widget_set_name (info->proxy, info->name);
2497 gtk_widget_show (info->proxy);
2498 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2501 case NODE_TYPE_MENU_PLACEHOLDER:
2502 /* create menu items for placeholders if necessary ... */
2503 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2504 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2508 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2510 g_object_unref (info->proxy);
2515 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2517 g_object_unref (info->extra);
2521 if (info->proxy == NULL)
2523 GtkWidget *menushell;
2526 if (find_menu_position (node, &menushell, &pos))
2528 info->proxy = gtk_separator_menu_item_new ();
2529 g_object_ref_sink (info->proxy);
2530 g_object_set_data (G_OBJECT (info->proxy),
2531 I_("gtk-separator-mode"),
2532 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2533 gtk_widget_set_no_show_all (info->proxy, TRUE);
2534 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2535 NODE_INFO (node)->proxy, pos);
2537 info->extra = gtk_separator_menu_item_new ();
2538 g_object_ref_sink (info->extra);
2539 g_object_set_data (G_OBJECT (info->extra),
2540 I_("gtk-separator-mode"),
2541 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2542 gtk_widget_set_no_show_all (info->extra, TRUE);
2543 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2544 NODE_INFO (node)->extra, pos + 1);
2548 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2549 /* create toolbar items for placeholders if necessary ... */
2550 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2551 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2555 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2557 g_object_unref (info->proxy);
2562 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2564 g_object_unref (info->extra);
2568 if (info->proxy == NULL)
2574 if (find_toolbar_position (node, &toolbar, &pos))
2576 item = gtk_separator_tool_item_new ();
2577 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2578 info->proxy = GTK_WIDGET (item);
2579 g_object_ref_sink (info->proxy);
2580 g_object_set_data (G_OBJECT (info->proxy),
2581 I_("gtk-separator-mode"),
2582 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2583 gtk_widget_set_no_show_all (info->proxy, TRUE);
2585 item = gtk_separator_tool_item_new ();
2586 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2587 info->extra = GTK_WIDGET (item);
2588 g_object_ref_sink (info->extra);
2589 g_object_set_data (G_OBJECT (info->extra),
2590 I_("gtk-separator-mode"),
2591 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2592 gtk_widget_set_no_show_all (info->extra, TRUE);
2596 case NODE_TYPE_MENUITEM:
2597 /* remove the proxy if it is of the wrong type ... */
2599 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2601 g_signal_handlers_disconnect_by_func (info->proxy,
2602 G_CALLBACK (update_smart_separators),
2604 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2605 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2607 g_object_unref (info->proxy);
2610 /* create proxy if needed ... */
2611 if (info->proxy == NULL)
2613 GtkWidget *menushell;
2616 if (find_menu_position (node, &menushell, &pos))
2618 info->proxy = gtk_action_create_menu_item (action);
2619 g_object_ref_sink (info->proxy);
2620 gtk_widget_set_name (info->proxy, info->name);
2622 if (info->always_show_image_set &&
2623 GTK_IS_IMAGE_MENU_ITEM (info->proxy))
2624 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (info->proxy),
2625 info->always_show_image);
2627 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2633 g_signal_handlers_disconnect_by_func (info->proxy,
2634 G_CALLBACK (update_smart_separators),
2636 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2637 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2642 g_signal_connect (info->proxy, "notify::visible",
2643 G_CALLBACK (update_smart_separators), NULL);
2644 if (in_popup && !popup_accels)
2646 /* don't show accels in popups */
2647 GtkWidget *child = gtk_bin_get_child (GTK_BIN (info->proxy));
2648 if (GTK_IS_ACCEL_LABEL (child))
2649 g_object_set (child, "accel-closure", NULL, NULL);
2654 case NODE_TYPE_TOOLITEM:
2655 /* remove the proxy if it is of the wrong type ... */
2657 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2659 g_signal_handlers_disconnect_by_func (info->proxy,
2660 G_CALLBACK (update_smart_separators),
2662 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), NULL);
2663 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2665 g_object_unref (info->proxy);
2668 /* create proxy if needed ... */
2669 if (info->proxy == NULL)
2674 if (find_toolbar_position (node, &toolbar, &pos))
2676 info->proxy = gtk_action_create_tool_item (action);
2677 g_object_ref_sink (info->proxy);
2678 gtk_widget_set_name (info->proxy, info->name);
2680 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2681 GTK_TOOL_ITEM (info->proxy), pos);
2686 g_signal_handlers_disconnect_by_func (info->proxy,
2687 G_CALLBACK (update_smart_separators),
2689 gtk_activatable_set_related_action (GTK_ACTIVATABLE (info->proxy), action);
2694 g_signal_connect (info->proxy, "notify::visible",
2695 G_CALLBACK (update_smart_separators), NULL);
2698 case NODE_TYPE_SEPARATOR:
2699 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2700 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2704 gint separator_mode;
2707 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2709 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2711 g_object_unref (info->proxy);
2715 if (find_toolbar_position (node, &toolbar, &pos))
2717 item = gtk_separator_tool_item_new ();
2718 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2719 info->proxy = GTK_WIDGET (item);
2720 g_object_ref_sink (info->proxy);
2721 gtk_widget_set_no_show_all (info->proxy, TRUE);
2724 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2725 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2726 separator_mode = SEPARATOR_MODE_VISIBLE;
2729 separator_mode = SEPARATOR_MODE_SMART;
2731 g_object_set_data (G_OBJECT (info->proxy),
2732 I_("gtk-separator-mode"),
2733 GINT_TO_POINTER (separator_mode));
2734 gtk_widget_show (info->proxy);
2739 GtkWidget *menushell;
2742 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2744 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2746 g_object_unref (info->proxy);
2750 if (find_menu_position (node, &menushell, &pos))
2752 info->proxy = gtk_separator_menu_item_new ();
2753 g_object_ref_sink (info->proxy);
2754 gtk_widget_set_no_show_all (info->proxy, TRUE);
2755 g_object_set_data (G_OBJECT (info->proxy),
2756 I_("gtk-separator-mode"),
2757 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2758 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2760 gtk_widget_show (info->proxy);
2764 case NODE_TYPE_ACCELERATOR:
2765 gtk_action_connect_accelerator (action);
2770 g_object_ref (action);
2772 g_object_unref (info->action);
2773 info->action = action;
2776 /* process children */
2777 child = node->children;
2783 child = current->next;
2784 update_node (self, current, in_popup, popup_accels);
2789 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2790 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2791 else if (info->type == NODE_TYPE_MENU ||
2792 info->type == NODE_TYPE_TOOLBAR ||
2793 info->type == NODE_TYPE_POPUP)
2794 update_smart_separators (info->proxy);
2797 /* handle cleanup of dead nodes */
2798 if (node->children == NULL && info->uifiles == NULL)
2801 gtk_widget_destroy (info->proxy);
2803 gtk_widget_destroy (info->extra);
2804 if (info->type == NODE_TYPE_ACCELERATOR && info->action != NULL)
2805 gtk_action_disconnect_accelerator (info->action);
2807 g_node_destroy (node);
2812 do_updates (GtkUIManager *self)
2814 /* this function needs to check through the tree for dirty nodes.
2815 * For such nodes, it needs to do the following:
2817 * 1) check if they are referenced by any loaded UI files anymore.
2818 * In which case, the proxy widget should be destroyed, unless
2819 * there are any subnodes.
2821 * 2) lookup the action for this node again. If it is different to
2822 * the current one (or if no previous action has been looked up),
2823 * the proxy is reconnected to the new action (or a new proxy widget
2824 * is created and added to the parent container).
2826 update_node (self, self->private_data->root_node, FALSE, FALSE);
2828 self->private_data->update_tag = 0;
2834 do_updates_idle (GtkUIManager *self)
2842 queue_update (GtkUIManager *self)
2844 if (self->private_data->update_tag != 0)
2847 self->private_data->update_tag = gdk_threads_add_idle (
2848 (GSourceFunc)do_updates_idle,
2854 * gtk_ui_manager_ensure_update:
2855 * @self: a #GtkUIManager
2857 * Makes sure that all pending updates to the UI have been completed.
2859 * This may occasionally be necessary, since #GtkUIManager updates the
2860 * UI in an idle function. A typical example where this function is
2861 * useful is to enforce that the menubar and toolbar have been added to
2862 * the main window before showing it:
2864 * gtk_container_add (GTK_CONTAINER (window), vbox);
2865 * g_signal_connect (merge, "add-widget",
2866 * G_CALLBACK (add_widget), vbox);
2867 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2868 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2869 * gtk_ui_manager_ensure_update (merge);
2870 * gtk_widget_show (window);
2876 gtk_ui_manager_ensure_update (GtkUIManager *self)
2878 if (self->private_data->update_tag != 0)
2880 g_source_remove (self->private_data->update_tag);
2886 dirty_traverse_func (GNode *node,
2889 NODE_INFO (node)->dirty = TRUE;
2894 dirty_all_nodes (GtkUIManager *self)
2896 g_node_traverse (self->private_data->root_node,
2897 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2898 dirty_traverse_func, NULL);
2899 queue_update (self);
2903 mark_node_dirty (GNode *node)
2907 /* FIXME could optimize this */
2908 for (p = node; p; p = p->parent)
2909 NODE_INFO (p)->dirty = TRUE;
2912 static const gchar *
2913 open_tag_format (NodeType type)
2917 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2918 case NODE_TYPE_ROOT: return "%*s<ui";
2919 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2920 case NODE_TYPE_MENU: return "%*s<menu";
2921 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2922 case NODE_TYPE_MENU_PLACEHOLDER:
2923 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2924 case NODE_TYPE_POPUP: return "%*s<popup";
2925 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2926 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2927 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2928 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2929 default: return NULL;
2933 static const gchar *
2934 close_tag_format (NodeType type)
2938 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2939 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2940 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2941 case NODE_TYPE_MENU: return "%*s</menu>\n";
2942 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2943 case NODE_TYPE_MENU_PLACEHOLDER:
2944 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2945 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2946 default: return NULL;
2951 print_node (GtkUIManager *self,
2958 const gchar *open_fmt;
2959 const gchar *close_fmt;
2963 open_fmt = open_tag_format (mnode->type);
2964 close_fmt = close_tag_format (mnode->type);
2966 g_string_append_printf (buffer, open_fmt, indent_level, "");
2968 if (mnode->type != NODE_TYPE_ROOT)
2971 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2973 if (mnode->action_name)
2974 g_string_append_printf (buffer, " action=\"%s\"",
2975 g_quark_to_string (mnode->action_name));
2978 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2980 for (child = node->children; child != NULL; child = child->next)
2981 print_node (self, child, indent_level + 2, buffer);
2984 g_string_append_printf (buffer, close_fmt, indent_level, "");
2988 gtk_ui_manager_buildable_custom_tag_start (GtkBuildable *buildable,
2989 GtkBuilder *builder,
2991 const gchar *tagname,
2992 GMarkupParser *parser,
2998 if (strcmp (tagname, "ui") == 0)
3002 ctx = g_new0 (ParseContext, 1);
3003 ctx->state = STATE_START;
3004 ctx->self = GTK_UI_MANAGER (buildable);
3005 ctx->current = NULL;
3006 ctx->merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (buildable));
3009 *parser = ui_parser;
3019 gtk_ui_manager_buildable_custom_tag_end (GtkBuildable *buildable,
3020 GtkBuilder *builder,
3022 const gchar *tagname,
3025 queue_update (GTK_UI_MANAGER (buildable));
3026 g_object_notify (G_OBJECT (buildable), "ui");
3031 * gtk_ui_manager_get_ui:
3032 * @self: a #GtkUIManager
3034 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
3036 * Return value: A newly allocated string containing an XML representation of
3042 gtk_ui_manager_get_ui (GtkUIManager *self)
3046 buffer = g_string_new (NULL);
3048 gtk_ui_manager_ensure_update (self);
3050 print_node (self, self->private_data->root_node, 0, buffer);
3052 return g_string_free (buffer, FALSE);
3055 #if defined (G_OS_WIN32) && !defined (_WIN64)
3057 #undef gtk_ui_manager_add_ui_from_file
3060 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
3061 const gchar *filename,
3064 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
3067 if (utf8_filename == NULL)
3070 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
3072 g_free (utf8_filename);