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/.
36 #include "gtkmarshalers.h"
38 #include "gtkmenubar.h"
39 #include "gtkmenushell.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtkseparatortoolitem.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtktoolbar.h"
44 #include "gtkuimanager.h"
46 #undef DEBUG_UI_MANAGER
55 NODE_TYPE_MENU_PLACEHOLDER,
56 NODE_TYPE_TOOLBAR_PLACEHOLDER,
64 typedef struct _Node Node;
74 GtkWidget *extra; /* second separator for placeholders */
79 guint expand : 1; /* used for separators */
82 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
84 struct _GtkUIManagerPrivate
86 GtkAccelGroup *accel_group;
95 gboolean add_tearoffs;
98 #define NODE_INFO(node) ((Node *)node->data)
100 typedef struct _NodeUIReference NodeUIReference;
102 struct _NodeUIReference
108 static void gtk_ui_manager_class_init (GtkUIManagerClass *class);
109 static void gtk_ui_manager_init (GtkUIManager *self);
110 static void gtk_ui_manager_finalize (GObject *object);
111 static void gtk_ui_manager_set_property (GObject *object,
115 static void gtk_ui_manager_get_property (GObject *object,
119 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
121 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
123 static void queue_update (GtkUIManager *self);
124 static void dirty_all_nodes (GtkUIManager *self);
125 static void mark_node_dirty (GNode *node);
126 static GNode * get_child_node (GtkUIManager *self,
128 const gchar *childname,
129 gint childname_length,
133 static GNode * get_node (GtkUIManager *self,
137 static gboolean free_node (GNode *node);
138 static void node_prepend_ui_reference (GNode *node,
140 GQuark action_quark);
141 static void node_remove_ui_reference (GNode *node,
163 static GObjectClass *parent_class = NULL;
164 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
166 static GMemChunk *merge_node_chunk = NULL;
169 gtk_ui_manager_get_type (void)
171 static GtkType type = 0;
175 static const GTypeInfo type_info =
177 sizeof (GtkUIManagerClass),
178 (GBaseInitFunc) NULL,
179 (GBaseFinalizeFunc) NULL,
180 (GClassInitFunc) gtk_ui_manager_class_init,
181 (GClassFinalizeFunc) NULL,
184 sizeof (GtkUIManager),
186 (GInstanceInitFunc) gtk_ui_manager_init,
189 type = g_type_register_static (G_TYPE_OBJECT,
197 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
199 GObjectClass *gobject_class;
201 parent_class = g_type_class_peek_parent (klass);
203 gobject_class = G_OBJECT_CLASS (klass);
205 if (!merge_node_chunk)
206 merge_node_chunk = g_mem_chunk_create (Node, 64,
209 gobject_class->finalize = gtk_ui_manager_finalize;
210 gobject_class->set_property = gtk_ui_manager_set_property;
211 gobject_class->get_property = gtk_ui_manager_get_property;
212 klass->get_widget = gtk_ui_manager_real_get_widget;
213 klass->get_action = gtk_ui_manager_real_get_action;
216 * GtkUIManager:add-tearoffs:
218 * The "add-tearoffs" property controls whether generated menus
219 * have tearoff menu items.
221 * Note that this only affects regular menus. Generated popup
222 * menus never have tearoff menu items.
226 g_object_class_install_property (gobject_class,
228 g_param_spec_boolean ("add_tearoffs",
229 P_("Add tearoffs to menus"),
230 P_("Whether tearoff menu items should be added to menus"),
234 g_object_class_install_property (gobject_class,
236 g_param_spec_string ("ui",
237 P_("Merged UI definition"),
238 P_("An XML string describing the merged UI"),
244 * GtkUIManager::add-widget:
245 * @merge: a #GtkUIManager
246 * @widget: the added widget
248 * The add_widget signal is emitted for each generated menubar and toolbar.
249 * It is not emitted for generated popup menus, which can be obtained by
250 * gtk_ui_manager_get_widget().
254 ui_manager_signals[ADD_WIDGET] =
255 g_signal_new ("add_widget",
256 G_OBJECT_CLASS_TYPE (klass),
257 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
258 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
260 g_cclosure_marshal_VOID__OBJECT,
265 * GtkUIManager::actions-changed:
266 * @merge: a #GtkUIManager
268 * The "actions-changed" signal is emitted whenever the set of actions
273 ui_manager_signals[ACTIONS_CHANGED] =
274 g_signal_new ("actions_changed",
275 G_OBJECT_CLASS_TYPE (klass),
276 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
277 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
279 g_cclosure_marshal_VOID__VOID,
283 * GtkUIManager::connect-proxy:
284 * @uimanager: the ui manager
285 * @action: the action
288 * The connect_proxy signal is emitted after connecting a proxy to
289 * an action in the group.
291 * This is intended for simple customizations for which a custom action
292 * class would be too clumsy, e.g. showing tooltips for menuitems in the
297 ui_manager_signals[CONNECT_PROXY] =
298 g_signal_new ("connect_proxy",
299 G_OBJECT_CLASS_TYPE (klass),
300 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
301 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
303 _gtk_marshal_VOID__OBJECT_OBJECT,
309 * GtkUIManager::disconnect-proxy:
310 * @uimanager: the ui manager
311 * @action: the action
314 * The disconnect_proxy signal is emitted after disconnecting a proxy
315 * from an action in the group.
319 ui_manager_signals[DISCONNECT_PROXY] =
320 g_signal_new ("disconnect_proxy",
321 G_OBJECT_CLASS_TYPE (klass),
322 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
323 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
325 _gtk_marshal_VOID__OBJECT_OBJECT,
331 * GtkUIManager::pre-activate:
332 * @uimanager: the ui manager
333 * @action: the action
335 * The pre_activate signal is emitted just before the @action
338 * This is intended for applications to get notification
339 * just before any action is activated.
343 ui_manager_signals[PRE_ACTIVATE] =
344 g_signal_new ("pre_activate",
345 G_OBJECT_CLASS_TYPE (klass),
346 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
347 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
349 _gtk_marshal_VOID__OBJECT,
354 * GtkUIManager::post-activate:
355 * @uimanager: the ui manager
356 * @action: the action
358 * The post_activate signal is emitted just after the @action
361 * This is intended for applications to get notification
362 * just after any action is activated.
366 ui_manager_signals[POST_ACTIVATE] =
367 g_signal_new ("post_activate",
368 G_OBJECT_CLASS_TYPE (klass),
369 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
370 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
372 _gtk_marshal_VOID__OBJECT,
376 klass->add_widget = NULL;
377 klass->actions_changed = NULL;
378 klass->connect_proxy = NULL;
379 klass->disconnect_proxy = NULL;
380 klass->pre_activate = NULL;
381 klass->post_activate = NULL;
383 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
388 gtk_ui_manager_init (GtkUIManager *self)
393 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
395 self->private_data->accel_group = gtk_accel_group_new ();
397 self->private_data->root_node = NULL;
398 self->private_data->action_groups = NULL;
400 self->private_data->last_merge_id = 0;
401 self->private_data->add_tearoffs = FALSE;
403 merge_id = gtk_ui_manager_new_merge_id (self);
404 node = get_child_node (self, NULL, "ui", 2,
405 NODE_TYPE_ROOT, TRUE, FALSE);
406 node_prepend_ui_reference (node, merge_id, 0);
410 gtk_ui_manager_finalize (GObject *object)
412 GtkUIManager *self = GTK_UI_MANAGER (object);
414 if (self->private_data->update_tag != 0)
416 g_source_remove (self->private_data->update_tag);
417 self->private_data->update_tag = 0;
420 g_node_traverse (self->private_data->root_node,
421 G_POST_ORDER, G_TRAVERSE_ALL, -1,
422 (GNodeTraverseFunc)free_node, NULL);
423 g_node_destroy (self->private_data->root_node);
424 self->private_data->root_node = NULL;
426 g_list_foreach (self->private_data->action_groups,
427 (GFunc) g_object_unref, NULL);
428 g_list_free (self->private_data->action_groups);
429 self->private_data->action_groups = NULL;
431 g_object_unref (self->private_data->accel_group);
432 self->private_data->accel_group = NULL;
434 G_OBJECT_CLASS (parent_class)->finalize (object);
438 gtk_ui_manager_set_property (GObject *object,
443 GtkUIManager *self = GTK_UI_MANAGER (object);
447 case PROP_ADD_TEAROFFS:
448 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gtk_ui_manager_get_property (GObject *object,
462 GtkUIManager *self = GTK_UI_MANAGER (object);
466 case PROP_ADD_TEAROFFS:
467 g_value_set_boolean (value, self->private_data->add_tearoffs);
470 g_value_set_string (value, gtk_ui_manager_get_ui (self));
473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
479 gtk_ui_manager_real_get_widget (GtkUIManager *self,
484 /* ensure that there are no pending updates before we get the
486 gtk_ui_manager_ensure_update (self);
488 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
493 return NODE_INFO (node)->proxy;
497 gtk_ui_manager_real_get_action (GtkUIManager *self,
502 /* ensure that there are no pending updates before we get
504 gtk_ui_manager_ensure_update (self);
506 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
511 return NODE_INFO (node)->action;
516 * gtk_ui_manager_new:
518 * Creates a new ui manager object.
520 * Return value: a new ui manager object.
525 gtk_ui_manager_new (void)
527 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
532 * gtk_ui_manager_get_add_tearoffs:
533 * @self: a #GtkUIManager
535 * Returns whether menus generated by this #GtkUIManager
536 * will have tearoff menu items.
538 * Return value: whether tearoff menu items are added
543 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
545 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
547 return self->private_data->add_tearoffs;
552 * gtk_ui_manager_set_add_tearoffs:
553 * @self: a #GtkUIManager
554 * @add_tearoffs: whether tearoff menu items are added
556 * Sets the "add_tearoffs" property, which controls whether menus
557 * generated by this #GtkUIManager will have tearoff menu items.
559 * Note that this only affects regular menus. Generated popup
560 * menus never have tearoff menu items.
565 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
566 gboolean add_tearoffs)
568 g_return_if_fail (GTK_IS_UI_MANAGER (self));
570 add_tearoffs = add_tearoffs != FALSE;
572 if (add_tearoffs != self->private_data->add_tearoffs)
574 self->private_data->add_tearoffs = add_tearoffs;
576 dirty_all_nodes (self);
578 g_object_notify (G_OBJECT (self), "add_tearoffs");
583 cb_proxy_connect_proxy (GtkActionGroup *group,
588 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
592 cb_proxy_disconnect_proxy (GtkActionGroup *group,
597 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
601 cb_proxy_pre_activate (GtkActionGroup *group,
605 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
609 cb_proxy_post_activate (GtkActionGroup *group,
613 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
617 * gtk_ui_manager_insert_action_group:
618 * @self: a #GtkUIManager object
619 * @action_group: the action group to be inserted
620 * @pos: the position at which the group will be inserted.
622 * Inserts an action group into the list of action groups associated
623 * with @self. Actions in earlier groups hide actions with the same
624 * name in later groups.
629 gtk_ui_manager_insert_action_group (GtkUIManager *self,
630 GtkActionGroup *action_group,
633 g_return_if_fail (GTK_IS_UI_MANAGER (self));
634 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
635 g_return_if_fail (g_list_find (self->private_data->action_groups,
636 action_group) == NULL);
638 g_object_ref (action_group);
639 self->private_data->action_groups =
640 g_list_insert (self->private_data->action_groups, action_group, pos);
641 g_object_connect (action_group,
642 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
643 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
644 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
645 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
648 /* dirty all nodes, as action bindings may change */
649 dirty_all_nodes (self);
651 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
655 * gtk_ui_manager_remove_action_group:
656 * @self: a #GtkUIManager object
657 * @action_group: the action group to be removed
659 * Removes an action group from the list of action groups associated
665 gtk_ui_manager_remove_action_group (GtkUIManager *self,
666 GtkActionGroup *action_group)
668 g_return_if_fail (GTK_IS_UI_MANAGER (self));
669 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
670 g_return_if_fail (g_list_find (self->private_data->action_groups,
671 action_group) != NULL);
673 self->private_data->action_groups =
674 g_list_remove (self->private_data->action_groups, action_group);
676 g_object_disconnect (action_group,
677 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
678 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
679 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
680 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
682 g_object_unref (action_group);
684 /* dirty all nodes, as action bindings may change */
685 dirty_all_nodes (self);
687 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
691 * gtk_ui_manager_get_action_groups:
692 * @self: a #GtkUIManager object
694 * Returns the list of action groups associated with @self.
696 * Return value: a #GList of action groups. The list is owned by GTK+
697 * and should not be modified.
702 gtk_ui_manager_get_action_groups (GtkUIManager *self)
704 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
706 return self->private_data->action_groups;
710 * gtk_ui_manager_get_accel_group:
711 * @self: a #GtkUIManager object
713 * Returns the #GtkAccelGroup associated with @self.
715 * Return value: the #GtkAccelGroup.
720 gtk_ui_manager_get_accel_group (GtkUIManager *self)
722 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
724 return self->private_data->accel_group;
728 * gtk_ui_manager_get_widget:
729 * @self: a #GtkUIManager
732 * Looks up a widget by following a path.
733 * The path consists of the names specified in the XML description of the UI.
734 * separated by '/'. Elements which don't have a name or action attribute in
735 * the XML (e.g. <popup>) can be addressed by their XML element name
736 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
738 * Note that the widget found by following a path that ends in a <menu>
739 * element is the menuitem to which the menu is attached, not the menu itself.
741 * Also note that the widgets constructed by a ui manager are not tied to
742 * the lifecycle of the ui manager. If you add the widgets returned by this
743 * function to some container or explicitly ref them, they will survive the
744 * destruction of the ui manager.
746 * Return value: the widget found by following the path, or %NULL if no widget
752 gtk_ui_manager_get_widget (GtkUIManager *self,
755 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
756 g_return_val_if_fail (path != NULL, NULL);
758 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
762 collect_toplevels (GNode *node,
766 GtkUIManagerItemType types;
770 switch (NODE_INFO (node)->type) {
771 case NODE_TYPE_MENUBAR:
772 if (data->types & GTK_UI_MANAGER_MENUBAR)
773 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
775 case NODE_TYPE_TOOLBAR:
776 if (data->types & GTK_UI_MANAGER_TOOLBAR)
777 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
779 case NODE_TYPE_POPUP:
780 if (data->types & GTK_UI_MANAGER_POPUP)
781 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
788 * gtk_ui_manager_get_toplevels:
789 * @self: a #GtkUIManager
790 * @types: specifies the types of toplevel widgets to include. Allowed
791 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
792 * #GTK_UI_MANAGER_POPUP.
794 * Obtains a list of all toplevel widgets of the requested types.
796 * Return value: a newly-allocated of all toplevel widgets of the requested
802 gtk_ui_manager_get_toplevels (GtkUIManager *self,
803 GtkUIManagerItemType types)
806 GtkUIManagerItemType types;
810 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
811 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
812 GTK_UI_MANAGER_TOOLBAR |
813 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
819 g_node_children_foreach (self->private_data->root_node,
821 collect_toplevels, &data);
828 * gtk_ui_manager_get_action:
829 * @self: a #GtkUIManager
832 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
833 * for more information about paths.
835 * Return value: the action whose proxy widget is found by following the path,
836 * or %NULL if no widget was found.
841 gtk_ui_manager_get_action (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_action (self, path);
851 get_child_node (GtkUIManager *self,
853 const gchar *childname,
854 gint childname_length,
861 g_return_val_if_fail (parent == NULL ||
862 (NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
863 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM),
870 for (child = parent->children; child != NULL; child = child->next)
872 if (NODE_INFO (child)->name &&
873 strlen (NODE_INFO (child)->name) == childname_length &&
874 !strncmp (NODE_INFO (child)->name, childname, childname_length))
876 /* if undecided about node type, set it */
877 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
878 NODE_INFO (child)->type = node_type;
880 /* warn about type mismatch */
881 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
882 node_type != NODE_TYPE_UNDECIDED &&
883 NODE_INFO (child)->type != node_type)
884 g_warning ("node type doesn't match %d (%s is type %d)",
886 NODE_INFO (child)->name,
887 NODE_INFO (child)->type);
893 if (!child && create)
897 mnode = g_chunk_new0 (Node, merge_node_chunk);
898 mnode->type = node_type;
899 mnode->name = g_strndup (childname, childname_length);
902 child = g_node_prepend_data (parent, mnode);
904 child = g_node_append_data (parent, mnode);
906 mark_node_dirty (child);
911 /* handle root node */
912 if (self->private_data->root_node)
914 child = self->private_data->root_node;
915 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
916 g_warning ("root node name '%s' doesn't match '%s'",
917 childname, NODE_INFO (child)->name);
918 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
919 g_warning ("base element must be of type ROOT");
925 mnode = g_chunk_new0 (Node, merge_node_chunk);
926 mnode->type = node_type;
927 mnode->name = g_strndup (childname, childname_length);
930 child = self->private_data->root_node = g_node_new (mnode);
938 get_node (GtkUIManager *self,
943 const gchar *pos, *end;
944 GNode *parent, *node;
946 if (strncmp ("/ui", path, 3) == 0)
949 end = path + strlen (path);
951 parent = node = NULL;
957 slash = strchr (pos, '/');
959 length = slash - pos;
961 length = strlen (pos);
963 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
968 pos += length + 1; /* move past the node name and the slash too */
972 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
973 NODE_INFO (node)->type = node_type;
979 free_node (GNode *node)
981 Node *info = NODE_INFO (node);
983 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
984 g_list_free (info->uifiles);
987 g_object_unref (info->action);
989 g_object_unref (info->proxy);
991 g_object_unref (info->extra);
993 g_chunk_free (info, merge_node_chunk);
999 * gtk_ui_manager_new_merge_id:
1000 * @self: a #GtkUIManager
1002 * Returns an unused merge id, suitable for use with
1003 * gtk_ui_manager_add_ui().
1005 * Return value: an unused merge id.
1010 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1012 self->private_data->last_merge_id++;
1014 return self->private_data->last_merge_id;
1018 node_prepend_ui_reference (GNode *gnode,
1020 GQuark action_quark)
1022 Node *node = NODE_INFO (gnode);
1023 NodeUIReference *reference = NULL;
1025 if (node->uifiles &&
1026 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1027 reference = node->uifiles->data;
1030 reference = g_new (NodeUIReference, 1);
1031 node->uifiles = g_list_prepend (node->uifiles, reference);
1034 reference->merge_id = merge_id;
1035 reference->action_quark = action_quark;
1037 mark_node_dirty (gnode);
1041 node_remove_ui_reference (GNode *gnode,
1044 Node *node = NODE_INFO (gnode);
1047 for (p = node->uifiles; p != NULL; p = p->next)
1049 NodeUIReference *reference = p->data;
1051 if (reference->merge_id == merge_id)
1053 if (p == node->uifiles)
1054 mark_node_dirty (gnode);
1055 node->uifiles = g_list_delete_link (node->uifiles, p);
1063 /* -------------------- The UI file parser -------------------- */
1077 typedef struct _ParseContext ParseContext;
1078 struct _ParseContext
1081 ParseState prev_state;
1091 start_element_handler (GMarkupParseContext *context,
1092 const gchar *element_name,
1093 const gchar **attribute_names,
1094 const gchar **attribute_values,
1098 ParseContext *ctx = user_data;
1099 GtkUIManager *self = ctx->self;
1102 const gchar *node_name;
1103 const gchar *action;
1104 GQuark action_quark;
1108 gboolean raise_error = TRUE;
1115 for (i = 0; attribute_names[i] != NULL; i++)
1117 if (!strcmp (attribute_names[i], "name"))
1119 node_name = attribute_values[i];
1121 else if (!strcmp (attribute_names[i], "action"))
1123 action = attribute_values[i];
1124 action_quark = g_quark_from_string (attribute_values[i]);
1126 else if (!strcmp (attribute_names[i], "position"))
1128 top = !strcmp (attribute_values[i], "top");
1130 else if (!strcmp (attribute_names[i], "expand"))
1132 expand = !strcmp (attribute_values[i], "true");
1136 gint line_number, char_number;
1138 g_markup_parse_context_get_position (context,
1139 &line_number, &char_number);
1142 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1143 _("Unknown attribute '%s' on line %d char %d"),
1145 line_number, char_number);
1150 /* Work out a name for this node. Either the name attribute, or
1151 * the action, or the element name */
1152 if (node_name == NULL)
1157 node_name = element_name;
1160 switch (element_name[0])
1163 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1165 ctx->state = STATE_ACCELERATOR;
1166 ctx->current = get_child_node (self, ctx->current,
1167 node_name, strlen (node_name),
1168 NODE_TYPE_ACCELERATOR,
1170 if (NODE_INFO (ctx->current)->action_name == 0)
1171 NODE_INFO (ctx->current)->action_name = action_quark;
1173 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1175 raise_error = FALSE;
1179 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1181 ctx->state = STATE_ROOT;
1182 ctx->current = self->private_data->root_node;
1183 raise_error = FALSE;
1185 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1189 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1191 ctx->state = STATE_MENU;
1192 ctx->current = get_child_node (self, ctx->current,
1193 node_name, strlen (node_name),
1196 if (NODE_INFO (ctx->current)->action_name == 0)
1197 NODE_INFO (ctx->current)->action_name = action_quark;
1199 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1200 mark_node_dirty (ctx->current);
1202 raise_error = FALSE;
1204 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1206 ctx->current = get_child_node (self, ctx->current,
1207 node_name, strlen (node_name),
1210 if (NODE_INFO (ctx->current)->action_name == 0)
1211 NODE_INFO (ctx->current)->action_name = action_quark;
1213 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1215 raise_error = FALSE;
1217 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1221 ctx->state = STATE_MENUITEM;
1222 node = get_child_node (self, ctx->current,
1223 node_name, strlen (node_name),
1226 if (NODE_INFO (node)->action_name == 0)
1227 NODE_INFO (node)->action_name = action_quark;
1229 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1231 raise_error = FALSE;
1235 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1237 ctx->state = STATE_MENU;
1238 ctx->current = get_child_node (self, ctx->current,
1239 node_name, strlen (node_name),
1242 if (NODE_INFO (ctx->current)->action_name == 0)
1243 NODE_INFO (ctx->current)->action_name = action_quark;
1245 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1247 raise_error = FALSE;
1249 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1250 !strcmp (element_name, "placeholder"))
1252 if (ctx->state == STATE_TOOLBAR)
1253 ctx->current = get_child_node (self, ctx->current,
1254 node_name, strlen (node_name),
1255 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1258 ctx->current = get_child_node (self, ctx->current,
1259 node_name, strlen (node_name),
1260 NODE_TYPE_MENU_PLACEHOLDER,
1263 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1265 raise_error = FALSE;
1269 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1270 !strcmp (element_name, "separator"))
1275 if (ctx->state == STATE_TOOLBAR)
1276 ctx->state = STATE_TOOLITEM;
1278 ctx->state = STATE_MENUITEM;
1279 if (!strcmp (node_name, "separator"))
1285 length = strlen (node_name);
1286 node = get_child_node (self, ctx->current,
1288 NODE_TYPE_SEPARATOR,
1291 NODE_INFO (node)->expand = expand;
1293 if (NODE_INFO (node)->action_name == 0)
1294 NODE_INFO (node)->action_name = action_quark;
1296 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1298 raise_error = FALSE;
1302 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1304 ctx->state = STATE_TOOLBAR;
1305 ctx->current = get_child_node (self, ctx->current,
1306 node_name, strlen (node_name),
1309 if (NODE_INFO (ctx->current)->action_name == 0)
1310 NODE_INFO (ctx->current)->action_name = action_quark;
1312 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1314 raise_error = FALSE;
1316 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1320 ctx->state = STATE_TOOLITEM;
1321 node = get_child_node (self, ctx->current,
1322 node_name, strlen (node_name),
1325 if (NODE_INFO (node)->action_name == 0)
1326 NODE_INFO (node)->action_name = action_quark;
1328 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1330 raise_error = FALSE;
1338 gint line_number, char_number;
1340 g_markup_parse_context_get_position (context,
1341 &line_number, &char_number);
1344 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1345 _("Unexpected start tag '%s' on line %d char %d"),
1347 line_number, char_number);
1352 end_element_handler (GMarkupParseContext *context,
1353 const gchar *element_name,
1357 ParseContext *ctx = user_data;
1363 /* no need to GError here, GMarkup already catches this */
1366 ctx->current = NULL;
1367 ctx->state = STATE_END;
1371 case STATE_ACCELERATOR:
1372 ctx->current = ctx->current->parent;
1373 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1374 ctx->state = STATE_ROOT;
1375 /* else, stay in same state */
1377 case STATE_MENUITEM:
1378 ctx->state = STATE_MENU;
1380 case STATE_TOOLITEM:
1381 ctx->state = STATE_TOOLBAR;
1387 cleanup (GMarkupParseContext *context,
1391 ParseContext *ctx = user_data;
1393 ctx->current = NULL;
1394 /* should also walk through the tree and get rid of nodes related to
1395 * this UI file's tag */
1397 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1401 xml_isspace (char c)
1403 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1407 text_handler (GMarkupParseContext *context,
1417 end = text + text_len;
1418 while (p != end && xml_isspace (*p))
1423 gint line_number, char_number;
1425 g_markup_parse_context_get_position (context,
1426 &line_number, &char_number);
1429 G_MARKUP_ERROR_INVALID_CONTENT,
1430 _("Unexpected character data on line %d char %d"),
1431 line_number, char_number);
1436 static GMarkupParser ui_parser = {
1437 start_element_handler,
1438 end_element_handler,
1445 add_ui_from_string (GtkUIManager *self,
1446 const gchar *buffer,
1448 gboolean needs_root,
1451 ParseContext ctx = { 0 };
1452 GMarkupParseContext *context;
1454 ctx.state = STATE_START;
1457 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1459 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1462 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1465 if (!g_markup_parse_context_parse (context, buffer, length, error))
1469 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1472 if (!g_markup_parse_context_end_parse (context, error))
1475 g_markup_parse_context_free (context);
1477 queue_update (self);
1479 g_object_notify (G_OBJECT (self), "ui");
1481 return ctx.merge_id;
1485 g_markup_parse_context_free (context);
1491 * gtk_ui_manager_add_ui_from_string:
1492 * @self: a #GtkUIManager object
1493 * @buffer: the string to parse
1494 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1495 * @error: return location for an error
1497 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1498 * merges it with the current contents of @self. An enclosing <ui>
1499 * element is added if it is missing.
1501 * Return value: The merge id for the merged UI. The merge id can be used
1502 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1503 * the return value is 0.
1508 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1509 const gchar *buffer,
1513 gboolean needs_root = TRUE;
1517 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1518 g_return_val_if_fail (buffer != NULL, 0);
1521 length = strlen (buffer);
1524 end = buffer + length;
1525 while (p != end && xml_isspace (*p))
1528 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1531 return add_ui_from_string (self, buffer, length, needs_root, error);
1535 * gtk_ui_manager_add_ui_from_file:
1536 * @self: a #GtkUIManager object
1537 * @filename: the name of the file to parse
1538 * @error: return location for an error
1540 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1541 * merges it with the current contents of @self.
1543 * Return value: The merge id for the merged UI. The merge id can be used
1544 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1545 * the return value is 0.
1550 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1551 const gchar *filename,
1558 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1560 if (!g_file_get_contents (filename, &buffer, &length, error))
1563 res = add_ui_from_string (self, buffer, length, FALSE, error);
1570 * gtk_ui_manager_add_ui:
1571 * @self: a #GtkUIManager
1572 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1574 * @name: the name for the added UI element
1575 * @action: the name of the action to be proxied, or %NULL to add a separator
1576 * @type: the type of UI element to add.
1577 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1578 * is added after its siblings.
1580 * Adds a UI element to the current contents of @self.
1582 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1583 * separator if such an element can be inserted at the place determined by
1584 * @path. Otherwise @type must indicate an element that can be inserted at
1585 * the place determined by @path.
1590 gtk_ui_manager_add_ui (GtkUIManager *self,
1594 const gchar *action,
1595 GtkUIManagerItemType type,
1601 GQuark action_quark = 0;
1603 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1604 g_return_if_fail (merge_id > 0);
1605 g_return_if_fail (name != NULL);
1607 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1612 node_type = NODE_TYPE_UNDECIDED;
1614 switch (NODE_INFO (node)->type)
1616 case NODE_TYPE_MENUBAR:
1617 case NODE_TYPE_MENU:
1618 case NODE_TYPE_POPUP:
1619 case NODE_TYPE_MENU_PLACEHOLDER:
1622 case GTK_UI_MANAGER_AUTO:
1624 node_type = NODE_TYPE_MENUITEM;
1626 node_type = NODE_TYPE_SEPARATOR;
1628 case GTK_UI_MANAGER_MENU:
1629 node_type = NODE_TYPE_MENU;
1631 case GTK_UI_MANAGER_MENUITEM:
1632 node_type = NODE_TYPE_MENUITEM;
1634 case GTK_UI_MANAGER_SEPARATOR:
1635 node_type = NODE_TYPE_SEPARATOR;
1637 case GTK_UI_MANAGER_PLACEHOLDER:
1638 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1644 case NODE_TYPE_TOOLBAR:
1645 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1648 case GTK_UI_MANAGER_AUTO:
1650 node_type = NODE_TYPE_TOOLITEM;
1652 node_type = NODE_TYPE_SEPARATOR;
1654 case GTK_UI_MANAGER_TOOLITEM:
1655 node_type = NODE_TYPE_TOOLITEM;
1657 case GTK_UI_MANAGER_SEPARATOR:
1658 node_type = NODE_TYPE_SEPARATOR;
1660 case GTK_UI_MANAGER_PLACEHOLDER:
1661 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1667 case NODE_TYPE_ROOT:
1670 case GTK_UI_MANAGER_MENUBAR:
1671 node_type = NODE_TYPE_MENUBAR;
1673 case GTK_UI_MANAGER_TOOLBAR:
1674 node_type = NODE_TYPE_TOOLBAR;
1676 case GTK_UI_MANAGER_POPUP:
1677 node_type = NODE_TYPE_POPUP;
1679 case GTK_UI_MANAGER_ACCELERATOR:
1680 node_type = NODE_TYPE_ACCELERATOR;
1690 if (node_type == NODE_TYPE_UNDECIDED)
1693 child = get_child_node (self, node,
1694 name, strlen (name),
1695 node_type, TRUE, top);
1698 action_quark = g_quark_from_string (action);
1700 node_prepend_ui_reference (child, merge_id, action_quark);
1702 if (NODE_INFO (child)->action_name == 0)
1703 NODE_INFO (child)->action_name = action_quark;
1705 queue_update (self);
1707 g_object_notify (G_OBJECT (self), "ui");
1711 remove_ui (GNode *node,
1714 guint merge_id = GPOINTER_TO_UINT (user_data);
1716 node_remove_ui_reference (node, merge_id);
1718 return FALSE; /* continue */
1722 * gtk_ui_manager_remove_ui:
1723 * @self: a #GtkUIManager object
1724 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1726 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1731 gtk_ui_manager_remove_ui (GtkUIManager *self,
1734 g_node_traverse (self->private_data->root_node,
1735 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1736 remove_ui, GUINT_TO_POINTER (merge_id));
1738 queue_update (self);
1740 g_object_notify (G_OBJECT (self), "ui");
1743 /* -------------------- Updates -------------------- */
1747 get_action_by_name (GtkUIManager *merge,
1748 const gchar *action_name)
1756 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1758 GtkActionGroup *action_group = tmp->data;
1761 action = gtk_action_group_get_action (action_group, action_name);
1771 find_menu_position (GNode *node,
1772 GtkWidget **menushell_p,
1775 GtkWidget *menushell;
1778 g_return_val_if_fail (node != NULL, FALSE);
1779 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1780 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1781 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1782 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1783 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1786 /* first sibling -- look at parent */
1787 if (node->prev == NULL)
1792 parent = node->parent;
1793 switch (NODE_INFO (parent)->type)
1795 case NODE_TYPE_MENUBAR:
1796 case NODE_TYPE_POPUP:
1797 menushell = NODE_INFO (parent)->proxy;
1800 case NODE_TYPE_MENU:
1801 menushell = NODE_INFO (parent)->proxy;
1802 if (GTK_IS_MENU_ITEM (menushell))
1803 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1804 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1805 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1809 g_list_free (siblings);
1811 case NODE_TYPE_MENU_PLACEHOLDER:
1812 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1813 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1814 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1815 NODE_INFO (parent)->proxy) + 1;
1818 g_warning("%s: bad parent node type %d", G_STRLOC,
1819 NODE_INFO (parent)->type);
1825 GtkWidget *prev_child;
1828 sibling = node->prev;
1829 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1830 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1832 prev_child = NODE_INFO (sibling)->proxy;
1834 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1835 menushell = gtk_widget_get_parent (prev_child);
1836 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1838 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1842 *menushell_p = menushell;
1850 find_toolbar_position (GNode *node,
1851 GtkWidget **toolbar_p,
1857 g_return_val_if_fail (node != NULL, FALSE);
1858 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1859 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1860 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1861 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1864 /* first sibling -- look at parent */
1865 if (node->prev == NULL)
1869 parent = node->parent;
1870 switch (NODE_INFO (parent)->type)
1872 case NODE_TYPE_TOOLBAR:
1873 toolbar = NODE_INFO (parent)->proxy;
1876 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1877 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1878 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1879 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1880 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1883 g_warning ("%s: bad parent node type %d", G_STRLOC,
1884 NODE_INFO (parent)->type);
1890 GtkWidget *prev_child;
1893 sibling = node->prev;
1894 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1895 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1897 prev_child = NODE_INFO (sibling)->proxy;
1899 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1900 toolbar = gtk_widget_get_parent (prev_child);
1901 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1903 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1904 GTK_TOOL_ITEM (prev_child)) + 1;
1908 *toolbar_p = toolbar;
1916 * _gtk_menu_is_empty:
1917 * @menu: a #GtkMenu or %NULL
1919 * Determines whether @menu is empty. A menu is considered empty if it
1920 * the only visible children are tearoff menu items or "filler" menu
1921 * items which were inserted to mark the menu as empty.
1923 * This function is used by #GtkAction.
1925 * Return value: whether @menu is empty.
1928 _gtk_menu_is_empty (GtkWidget *menu)
1930 GList *children, *cur;
1932 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1937 children = gtk_container_get_children (GTK_CONTAINER (menu));
1942 if (GTK_WIDGET_VISIBLE (cur->data))
1944 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1945 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1950 g_list_free (children);
1956 SEPARATOR_MODE_SMART,
1957 SEPARATOR_MODE_VISIBLE,
1958 SEPARATOR_MODE_HIDDEN
1961 void _gtk_action_sync_menu_visible (GtkAction *action,
1966 update_smart_separators (GtkWidget *proxy)
1968 GtkWidget *parent = NULL;
1970 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1972 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1973 parent = gtk_widget_get_parent (proxy);
1979 GList *children, *cur, *last;
1983 children = gtk_container_get_children (GTK_CONTAINER (parent));
1994 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
1998 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
1999 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2002 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2003 "gtk-separator-mode"));
2006 case SEPARATOR_MODE_VISIBLE:
2007 gtk_widget_show (GTK_WIDGET (cur->data));
2011 case SEPARATOR_MODE_HIDDEN:
2012 gtk_widget_hide (GTK_WIDGET (cur->data));
2014 case SEPARATOR_MODE_SMART:
2017 gtk_widget_show (GTK_WIDGET (cur->data));
2022 gtk_widget_hide (GTK_WIDGET (cur->data));
2026 else if (GTK_WIDGET_VISIBLE (cur->data))
2029 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2042 gtk_widget_hide (GTK_WIDGET (last->data));
2044 if (GTK_IS_MENU (parent))
2048 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2049 if (GTK_IS_MENU_ITEM (item))
2050 _gtk_action_sync_menu_visible (NULL, item, empty);
2051 if (GTK_IS_WIDGET (filler))
2054 gtk_widget_show (filler);
2056 gtk_widget_hide (filler);
2060 g_list_free (children);
2065 update_node (GtkUIManager *self,
2073 const gchar *action_name;
2074 NodeUIReference *ref;
2076 #ifdef DEBUG_UI_MANAGER
2080 g_return_if_fail (node != NULL);
2081 g_return_if_fail (NODE_INFO (node) != NULL);
2083 info = NODE_INFO (node);
2088 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2090 #ifdef DEBUG_UI_MANAGER
2091 g_print ("update_node name=%s dirty=%d popup %d (",
2092 info->name, info->dirty, in_popup);
2093 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2095 NodeUIReference *ref = tmp->data;
2096 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2103 if (info->uifiles == NULL) {
2104 /* We may need to remove this node.
2105 * This must be done in post order
2107 goto recurse_children;
2110 ref = info->uifiles->data;
2111 action_name = g_quark_to_string (ref->action_quark);
2112 action = get_action_by_name (self, action_name);
2114 info->dirty = FALSE;
2116 /* Check if the node doesn't have an action and must have an action */
2117 if (action == NULL &&
2118 info->type != NODE_TYPE_ROOT &&
2119 info->type != NODE_TYPE_MENUBAR &&
2120 info->type != NODE_TYPE_TOOLBAR &&
2121 info->type != NODE_TYPE_POPUP &&
2122 info->type != NODE_TYPE_SEPARATOR &&
2123 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2124 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2126 g_warning ("%s: missing action", info->name);
2128 goto recurse_children;
2132 gtk_action_set_accel_group (action, self->private_data->accel_group);
2134 /* If the widget already has a proxy and the action hasn't changed, then
2135 * we only have to update the tearoff menu items.
2137 if (info->proxy != NULL && action == info->action)
2139 if (info->type == NODE_TYPE_MENU)
2144 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2145 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2146 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2148 if (self->private_data->add_tearoffs && !in_popup)
2149 gtk_widget_show (GTK_WIDGET (siblings->data));
2151 gtk_widget_hide (GTK_WIDGET (siblings->data));
2153 g_list_free (siblings);
2156 goto recurse_children;
2161 case NODE_TYPE_MENUBAR:
2162 if (info->proxy == NULL)
2164 info->proxy = gtk_menu_bar_new ();
2165 g_object_ref (info->proxy);
2166 gtk_object_sink (GTK_OBJECT (info->proxy));
2167 gtk_widget_set_name (info->proxy, info->name);
2168 gtk_widget_show (info->proxy);
2169 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2172 case NODE_TYPE_POPUP:
2173 if (info->proxy == NULL)
2175 info->proxy = gtk_menu_new ();
2176 g_object_ref (info->proxy);
2177 gtk_object_sink (GTK_OBJECT (info->proxy));
2179 gtk_widget_set_name (info->proxy, info->name);
2181 case NODE_TYPE_MENU:
2183 GtkWidget *prev_submenu = NULL;
2186 /* remove the proxy if it is of the wrong type ... */
2188 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2190 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2193 g_object_ref (prev_submenu);
2194 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2196 gtk_action_disconnect_proxy (info->action, info->proxy);
2197 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2199 g_object_unref (info->proxy);
2202 /* create proxy if needed ... */
2203 if (info->proxy == NULL)
2205 GtkWidget *menushell;
2208 if (find_menu_position (node, &menushell, &pos))
2213 info->proxy = gtk_action_create_menu_item (action);
2214 g_object_ref (info->proxy);
2215 gtk_object_sink (GTK_OBJECT (info->proxy));
2216 menu = gtk_menu_new ();
2217 gtk_widget_set_name (info->proxy, info->name);
2218 gtk_widget_set_name (menu, info->name);
2219 tearoff = gtk_tearoff_menu_item_new ();
2220 gtk_widget_set_no_show_all (tearoff, TRUE);
2221 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2222 filler = gtk_menu_item_new_with_label (_("Empty"));
2223 g_object_set_data (G_OBJECT (filler),
2224 "gtk-empty-menu-item",
2225 GINT_TO_POINTER (TRUE));
2226 gtk_widget_set_sensitive (filler, FALSE);
2227 gtk_widget_set_no_show_all (filler, TRUE);
2228 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2229 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2230 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2231 g_signal_connect (info->proxy, "notify::visible",
2232 G_CALLBACK (update_smart_separators), NULL);
2236 gtk_action_connect_proxy (action, info->proxy);
2240 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2242 g_object_unref (prev_submenu);
2244 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2245 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2246 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2248 if (self->private_data->add_tearoffs && !in_popup)
2249 gtk_widget_show (GTK_WIDGET (siblings->data));
2251 gtk_widget_hide (GTK_WIDGET (siblings->data));
2253 g_list_free (siblings);
2256 case NODE_TYPE_UNDECIDED:
2257 g_warning ("found undecided node!");
2259 case NODE_TYPE_ROOT:
2261 case NODE_TYPE_TOOLBAR:
2262 if (info->proxy == NULL)
2264 info->proxy = gtk_toolbar_new ();
2265 g_object_ref (info->proxy);
2266 gtk_object_sink (GTK_OBJECT (info->proxy));
2267 gtk_widget_set_name (info->proxy, info->name);
2268 gtk_widget_show (info->proxy);
2269 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2272 case NODE_TYPE_MENU_PLACEHOLDER:
2273 /* create menu items for placeholders if necessary ... */
2274 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2275 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2279 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2281 g_object_unref (info->proxy);
2286 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2288 g_object_unref (info->extra);
2292 if (info->proxy == NULL)
2294 GtkWidget *menushell;
2297 if (find_menu_position (node, &menushell, &pos))
2299 info->proxy = gtk_separator_menu_item_new ();
2300 g_object_ref (info->proxy);
2301 gtk_object_sink (GTK_OBJECT (info->proxy));
2302 g_object_set_data (G_OBJECT (info->proxy),
2303 "gtk-separator-mode",
2304 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2305 gtk_widget_set_no_show_all (info->proxy, TRUE);
2306 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2307 NODE_INFO (node)->proxy, pos);
2309 info->extra = gtk_separator_menu_item_new ();
2310 g_object_ref (info->extra);
2311 gtk_object_sink (GTK_OBJECT (info->extra));
2312 g_object_set_data (G_OBJECT (info->extra),
2313 "gtk-separator-mode",
2314 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2315 gtk_widget_set_no_show_all (info->extra, TRUE);
2316 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2317 NODE_INFO (node)->extra, pos+1);
2321 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2322 /* create toolbar items for placeholders if necessary ... */
2323 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2324 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2328 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2330 g_object_unref (info->proxy);
2335 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2337 g_object_unref (info->extra);
2341 if (info->proxy == NULL)
2346 if (find_toolbar_position (node, &toolbar, &pos))
2350 item = gtk_separator_tool_item_new ();
2351 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2352 info->proxy = GTK_WIDGET (item);
2353 g_object_ref (info->proxy);
2354 gtk_object_sink (GTK_OBJECT (info->proxy));
2355 g_object_set_data (G_OBJECT (info->proxy),
2356 "gtk-separator-mode",
2357 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2358 gtk_widget_set_no_show_all (info->proxy, TRUE);
2360 item = gtk_separator_tool_item_new ();
2361 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2362 info->extra = GTK_WIDGET (item);
2363 g_object_ref (info->extra);
2364 gtk_object_sink (GTK_OBJECT (info->extra));
2365 g_object_set_data (G_OBJECT (info->extra),
2366 "gtk-separator-mode",
2367 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2368 gtk_widget_set_no_show_all (info->extra, TRUE);
2372 case NODE_TYPE_MENUITEM:
2373 /* remove the proxy if it is of the wrong type ... */
2375 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2377 g_signal_handlers_disconnect_by_func (info->proxy,
2378 G_CALLBACK (update_smart_separators),
2380 gtk_action_disconnect_proxy (info->action, info->proxy);
2381 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2383 g_object_unref (info->proxy);
2386 /* create proxy if needed ... */
2387 if (info->proxy == NULL)
2389 GtkWidget *menushell;
2392 if (find_menu_position (node, &menushell, &pos))
2394 info->proxy = gtk_action_create_menu_item (action);
2395 g_object_ref (info->proxy);
2396 gtk_object_sink (GTK_OBJECT (info->proxy));
2397 gtk_widget_set_name (info->proxy, info->name);
2399 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2405 g_signal_handlers_disconnect_by_func (info->proxy,
2406 G_CALLBACK (update_smart_separators),
2408 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2409 gtk_action_connect_proxy (action, info->proxy);
2411 g_signal_connect (info->proxy, "notify::visible",
2412 G_CALLBACK (update_smart_separators), NULL);
2415 /* don't show accels in popups */
2416 GtkWidget *label = GTK_BIN (info->proxy)->child;
2417 g_object_set (label,
2418 "accel_closure", NULL,
2423 case NODE_TYPE_TOOLITEM:
2424 /* remove the proxy if it is of the wrong type ... */
2426 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2428 g_signal_handlers_disconnect_by_func (info->proxy,
2429 G_CALLBACK (update_smart_separators),
2431 gtk_action_disconnect_proxy (info->action, info->proxy);
2432 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2434 g_object_unref (info->proxy);
2437 /* create proxy if needed ... */
2438 if (info->proxy == NULL)
2443 if (find_toolbar_position (node, &toolbar, &pos))
2445 info->proxy = gtk_action_create_tool_item (action);
2446 g_object_ref (info->proxy);
2447 gtk_object_sink (GTK_OBJECT (info->proxy));
2448 gtk_widget_set_name (info->proxy, info->name);
2450 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2451 GTK_TOOL_ITEM (info->proxy), pos);
2453 /* FIXME: we must trigger the notify::tooltip handler, since
2454 * tooltips on toolitems can't be set before the toolitem
2455 * is added to the toolbar.
2457 g_object_get (action, "tooltip", &tooltip, NULL);
2458 g_object_set (action, "tooltip", tooltip, NULL);
2464 g_signal_handlers_disconnect_by_func (info->proxy,
2465 G_CALLBACK (update_smart_separators),
2467 gtk_action_connect_proxy (action, info->proxy);
2469 g_signal_connect (info->proxy, "notify::visible",
2470 G_CALLBACK (update_smart_separators), NULL);
2472 case NODE_TYPE_SEPARATOR:
2473 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2474 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2478 gint separator_mode;
2480 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2482 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2484 g_object_unref (info->proxy);
2488 if (find_toolbar_position (node, &toolbar, &pos))
2490 GtkToolItem *item = gtk_separator_tool_item_new ();
2491 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2492 info->proxy = GTK_WIDGET (item);
2493 g_object_ref (info->proxy);
2494 gtk_object_sink (GTK_OBJECT (info->proxy));
2495 gtk_widget_set_no_show_all (info->proxy, TRUE);
2498 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2499 gtk_separator_tool_item_set_draw (item, FALSE);
2500 separator_mode = SEPARATOR_MODE_VISIBLE;
2503 separator_mode = SEPARATOR_MODE_SMART;
2505 g_object_set_data (G_OBJECT (info->proxy),
2506 "gtk-separator-mode",
2507 GINT_TO_POINTER (separator_mode));
2508 gtk_widget_show (info->proxy);
2513 GtkWidget *menushell;
2516 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2518 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2520 g_object_unref (info->proxy);
2524 if (find_menu_position (node, &menushell, &pos))
2526 info->proxy = gtk_separator_menu_item_new ();
2527 g_object_ref (info->proxy);
2528 gtk_object_sink (GTK_OBJECT (info->proxy));
2529 gtk_widget_set_no_show_all (info->proxy, TRUE);
2530 g_object_set_data (G_OBJECT (info->proxy),
2531 "gtk-separator-mode",
2532 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2533 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2535 gtk_widget_show (info->proxy);
2539 case NODE_TYPE_ACCELERATOR:
2540 gtk_action_connect_accelerator (action);
2545 g_object_ref (action);
2547 g_object_unref (info->action);
2548 info->action = action;
2551 /* process children */
2552 child = node->children;
2558 child = current->next;
2559 update_node (self, current, in_popup);
2564 if (info->type == NODE_TYPE_MENU)
2565 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2566 else if (info->type == NODE_TYPE_TOOLBAR || info->type == NODE_TYPE_POPUP)
2567 update_smart_separators (info->proxy);
2570 /* handle cleanup of dead nodes */
2571 if (node->children == NULL && info->uifiles == NULL)
2574 gtk_widget_destroy (info->proxy);
2576 gtk_widget_destroy (info->extra);
2577 if (info->type == NODE_TYPE_ACCELERATOR)
2578 gtk_action_disconnect_accelerator (info->action);
2580 g_node_destroy (node);
2585 do_updates (GtkUIManager *self)
2587 /* this function needs to check through the tree for dirty nodes.
2588 * For such nodes, it needs to do the following:
2590 * 1) check if they are referenced by any loaded UI files anymore.
2591 * In which case, the proxy widget should be destroyed, unless
2592 * there are any subnodes.
2594 * 2) lookup the action for this node again. If it is different to
2595 * the current one (or if no previous action has been looked up),
2596 * the proxy is reconnected to the new action (or a new proxy widget
2597 * is created and added to the parent container).
2599 update_node (self, self->private_data->root_node, FALSE);
2601 self->private_data->update_tag = 0;
2607 do_updates_idle (GtkUIManager *self)
2609 GDK_THREADS_ENTER ();
2611 GDK_THREADS_LEAVE ();
2617 queue_update (GtkUIManager *self)
2619 if (self->private_data->update_tag != 0)
2622 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates_idle,
2628 * gtk_ui_manager_ensure_update:
2629 * @self: a #GtkUIManager
2631 * Makes sure that all pending updates to the UI have been completed.
2633 * This may occasionally be necessary, since #GtkUIManager updates the
2634 * UI in an idle function. A typical example where this function is
2635 * useful is to enforce that the menubar and toolbar have been added to
2636 * the main window before showing it:
2639 * gtk_container_add (GTK_CONTAINER (window), vbox);
2640 * g_signal_connect (merge, "add_widget",
2641 * G_CALLBACK (add_widget), vbox);
2642 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2643 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2644 * gtk_ui_manager_ensure_update (merge);
2645 * gtk_widget_show (window);
2647 * </informalexample>
2652 gtk_ui_manager_ensure_update (GtkUIManager *self)
2654 if (self->private_data->update_tag != 0)
2656 g_source_remove (self->private_data->update_tag);
2662 dirty_traverse_func (GNode *node,
2665 NODE_INFO (node)->dirty = TRUE;
2670 dirty_all_nodes (GtkUIManager *self)
2672 g_node_traverse (self->private_data->root_node,
2673 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2674 dirty_traverse_func, NULL);
2675 queue_update (self);
2679 mark_node_dirty (GNode *node)
2683 /* FIXME could optimize this */
2684 for (p = node; p; p = p->parent)
2685 NODE_INFO (p)->dirty = TRUE;
2688 static const gchar *open_tag_format[] = {
2703 static const gchar *close_tag_format[] = {
2704 "%*s</UNDECIDED>\n",
2709 "%*s</placeholder>\n",
2710 "%*s</placeholder>\n",
2719 print_node (GtkUIManager *self,
2729 g_string_append_printf (buffer, open_tag_format[mnode->type],
2732 if (mnode->type != NODE_TYPE_ROOT)
2735 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2737 if (mnode->action_name)
2738 g_string_append_printf (buffer, " action=\"%s\"",
2739 g_quark_to_string (mnode->action_name));
2742 g_string_append (buffer,
2743 close_tag_format[mnode->type] ? ">\n" : "/>\n");
2745 for (child = node->children; child != NULL; child = child->next)
2746 print_node (self, child, indent_level + 2, buffer);
2748 if (close_tag_format[mnode->type])
2749 g_string_append_printf (buffer, close_tag_format[mnode->type],
2754 * gtk_ui_manager_get_ui:
2755 * @self: a #GtkUIManager
2757 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2759 * Return value: A newly allocated string containing an XML representation of
2765 gtk_ui_manager_get_ui (GtkUIManager *self)
2769 buffer = g_string_new (NULL);
2771 gtk_ui_manager_ensure_update (self);
2773 print_node (self, self->private_data->root_node, 0, buffer);
2775 return g_string_free (buffer, FALSE);
2780 #undef gtk_ui_manager_add_ui_from_file
2783 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2784 const gchar *filename,
2787 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2790 if (utf8_filename == NULL)
2793 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2795 g_free (utf8_filename);