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/.
35 #include "gtkmarshalers.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenushell.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtkseparatortoolitem.h"
41 #include "gtktearoffmenuitem.h"
42 #include "gtktoolbar.h"
43 #include "gtkuimanager.h"
45 #undef DEBUG_UI_MANAGER
54 NODE_TYPE_MENU_PLACEHOLDER,
55 NODE_TYPE_TOOLBAR_PLACEHOLDER,
63 typedef struct _Node Node;
73 GtkWidget *extra; /* second separator for placeholders */
80 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
82 struct _GtkUIManagerPrivate
84 GtkAccelGroup *accel_group;
93 gboolean add_tearoffs;
96 #define NODE_INFO(node) ((Node *)node->data)
98 typedef struct _NodeUIReference NodeUIReference;
100 struct _NodeUIReference
106 static void gtk_ui_manager_class_init (GtkUIManagerClass *class);
107 static void gtk_ui_manager_init (GtkUIManager *self);
108 static void gtk_ui_manager_finalize (GObject *object);
109 static void gtk_ui_manager_set_property (GObject *object,
113 static void gtk_ui_manager_get_property (GObject *object,
117 static void queue_update (GtkUIManager *self);
118 static void dirty_all_nodes (GtkUIManager *self);
119 static GNode * get_child_node (GtkUIManager *self,
121 const gchar *childname,
122 gint childname_length,
126 static GNode * get_node (GtkUIManager *self,
130 static gboolean free_node (GNode *node);
131 static void node_prepend_ui_reference (Node *node,
133 GQuark action_quark);
134 static void node_remove_ui_reference (Node *node,
156 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
158 static GMemChunk *merge_node_chunk = NULL;
161 gtk_ui_manager_get_type (void)
163 static GtkType type = 0;
167 static const GTypeInfo type_info =
169 sizeof (GtkUIManagerClass),
170 (GBaseInitFunc) NULL,
171 (GBaseFinalizeFunc) NULL,
172 (GClassInitFunc) gtk_ui_manager_class_init,
173 (GClassFinalizeFunc) NULL,
176 sizeof (GtkUIManager),
178 (GInstanceInitFunc) gtk_ui_manager_init,
181 type = g_type_register_static (G_TYPE_OBJECT,
189 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
191 GObjectClass *gobject_class;
193 gobject_class = G_OBJECT_CLASS (klass);
195 if (!merge_node_chunk)
196 merge_node_chunk = g_mem_chunk_create (Node, 64,
199 gobject_class->finalize = gtk_ui_manager_finalize;
200 gobject_class->set_property = gtk_ui_manager_set_property;
201 gobject_class->get_property = gtk_ui_manager_get_property;
204 * GtkUIManager:add-tearoffs:
206 * The "add-tearoffs" property controls whether generated menus
207 * have tearoff menu items.
209 * Note that this only affects regular menus. Generated popup
210 * menus never have tearoff menu items.
214 g_object_class_install_property (gobject_class,
216 g_param_spec_boolean ("add_tearoffs",
217 P_("Add tearoffs to menus"),
218 P_("Whether tearoff menu items should be added to menus"),
222 g_object_class_install_property (gobject_class,
224 g_param_spec_string ("ui",
225 P_("Merged UI definition"),
226 P_("An XML string describing the merged UI"),
232 * GtkUIManager::add-widget:
233 * @merge: a #GtkUIManager
234 * @widget: the added widget
236 * The add_widget signal is emitted for each generated menubar and toolbar.
237 * It is not emitted for generated popup menus, which can be obtained by
238 * gtk_ui_manager_get_widget().
242 ui_manager_signals[ADD_WIDGET] =
243 g_signal_new ("add_widget",
244 G_OBJECT_CLASS_TYPE (klass),
245 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
246 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget), NULL, NULL,
247 g_cclosure_marshal_VOID__OBJECT,
252 * GtkUIManager::actions-changed:
253 * @merge: a #GtkUIManager
255 * The "actions-changed" signal is emitted whenever the set of actions
260 ui_manager_signals[ACTIONS_CHANGED] =
261 g_signal_new ("actions_changed",
262 G_OBJECT_CLASS_TYPE (klass),
263 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
264 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
266 g_cclosure_marshal_VOID__VOID,
270 * GtkUIManager::connect_proxy:
271 * @uimanager: the ui manager
272 * @action: the action
275 * The connect_proxy signal is emitted after connecting a proxy to
276 * an action in the group.
278 * This is intended for simple customizations for which a custom action
279 * class would be too clumsy, e.g. showing tooltips for menuitems in the
284 ui_manager_signals[CONNECT_PROXY] =
285 g_signal_new ("connect_proxy",
286 G_OBJECT_CLASS_TYPE (klass),
288 _gtk_marshal_VOID__OBJECT_OBJECT,
290 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
293 * GtkUIManager::disconnect_proxy:
294 * @uimanager: the ui manager
295 * @action: the action
298 * The disconnect_proxy signal is emitted after disconnecting a proxy
299 * from an action in the group.
303 ui_manager_signals[DISCONNECT_PROXY] =
304 g_signal_new ("disconnect_proxy",
305 G_OBJECT_CLASS_TYPE (klass),
307 _gtk_marshal_VOID__OBJECT_OBJECT,
309 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
312 * GtkUIManager::pre_activate:
313 * @uimanager: the ui manager
314 * @action: the action
316 * The pre_activate signal is emitted just before the @action
319 * This is intended for applications to get notification
320 * just before any action is activated.
324 ui_manager_signals[PRE_ACTIVATE] =
325 g_signal_new ("pre_activate",
326 G_OBJECT_CLASS_TYPE (klass),
328 _gtk_marshal_VOID__OBJECT,
333 * GtkUIManager::post_activate:
334 * @uimanager: the ui manager
335 * @action: the action
337 * The post_activate signal is emitted just after the @action
340 * This is intended for applications to get notification
341 * just after any action is activated.
345 ui_manager_signals[POST_ACTIVATE] =
346 g_signal_new ("post_activate",
347 G_OBJECT_CLASS_TYPE (klass),
349 _gtk_marshal_VOID__OBJECT,
353 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
358 gtk_ui_manager_init (GtkUIManager *self)
363 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
365 self->private_data->accel_group = gtk_accel_group_new ();
367 self->private_data->root_node = NULL;
368 self->private_data->action_groups = NULL;
370 self->private_data->last_merge_id = 0;
371 self->private_data->add_tearoffs = FALSE;
373 merge_id = gtk_ui_manager_new_merge_id (self);
374 node = get_child_node (self, NULL, "ui", 2,
375 NODE_TYPE_ROOT, TRUE, FALSE);
376 node_prepend_ui_reference (NODE_INFO (node), merge_id, 0);
380 gtk_ui_manager_finalize (GObject *object)
382 GtkUIManager *self = GTK_UI_MANAGER (object);
384 if (self->private_data->update_tag != 0)
386 g_source_remove (self->private_data->update_tag);
387 self->private_data->update_tag = 0;
390 g_node_traverse (self->private_data->root_node,
391 G_POST_ORDER, G_TRAVERSE_ALL, -1,
392 (GNodeTraverseFunc)free_node, 0);
393 g_node_destroy (self->private_data->root_node);
394 self->private_data->root_node = NULL;
396 g_list_foreach (self->private_data->action_groups,
397 (GFunc) g_object_unref, NULL);
398 g_list_free (self->private_data->action_groups);
399 self->private_data->action_groups = NULL;
401 g_object_unref (self->private_data->accel_group);
402 self->private_data->accel_group = NULL;
406 gtk_ui_manager_set_property (GObject *object,
411 GtkUIManager *self = GTK_UI_MANAGER (object);
415 case PROP_ADD_TEAROFFS:
416 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425 gtk_ui_manager_get_property (GObject *object,
430 GtkUIManager *self = GTK_UI_MANAGER (object);
434 case PROP_ADD_TEAROFFS:
435 g_value_set_boolean (value, self->private_data->add_tearoffs);
438 g_value_set_string (value, gtk_ui_manager_get_ui (self));
441 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 * gtk_ui_manager_new:
450 * Creates a new ui manager object.
452 * Return value: a new ui manager object.
457 gtk_ui_manager_new (void)
459 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
464 * gtk_ui_manager_get_add_tearoffs:
465 * @self: a #GtkUIManager
467 * Returns whether menus generated by this #GtkUIManager
468 * will have tearoff menu items.
470 * Return value: whether tearoff menu items are added
475 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
477 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
479 return self->private_data->add_tearoffs;
484 * gtk_ui_manager_set_add_tearoffs:
485 * @self: a #GtkUIManager
486 * @add_tearoffs: whether tearoff menu items are added
488 * Sets the "add_tearoffs" property, which controls whether menus
489 * generated by this #GtkUIManager will have tearoff menu items.
491 * Note that this only affects regular menus. Generated popup
492 * menus never have tearoff menu items.
497 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
498 gboolean add_tearoffs)
500 g_return_if_fail (GTK_IS_UI_MANAGER (self));
502 add_tearoffs = add_tearoffs != FALSE;
504 if (add_tearoffs != self->private_data->add_tearoffs)
506 self->private_data->add_tearoffs = add_tearoffs;
508 dirty_all_nodes (self);
510 g_object_notify (G_OBJECT (self), "add_tearoffs");
515 cb_proxy_connect_proxy (GtkActionGroup *group,
520 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
524 cb_proxy_disconnect_proxy (GtkActionGroup *group,
529 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
533 cb_proxy_pre_activate (GtkActionGroup *group,
537 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
541 cb_proxy_post_activate (GtkActionGroup *group,
545 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
549 * gtk_ui_manager_insert_action_group:
550 * @self: a #GtkUIManager object
551 * @action_group: the action group to be inserted
552 * @pos: the position at which the group will be inserted.
554 * Inserts an action group into the list of action groups associated
555 * with @self. Actions in earlier groups hide actions with the same
556 * name in later groups.
561 gtk_ui_manager_insert_action_group (GtkUIManager *self,
562 GtkActionGroup *action_group,
565 g_return_if_fail (GTK_IS_UI_MANAGER (self));
566 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
567 g_return_if_fail (g_list_find (self->private_data->action_groups,
568 action_group) == NULL);
570 g_object_ref (action_group);
571 self->private_data->action_groups =
572 g_list_insert (self->private_data->action_groups, action_group, pos);
573 g_object_connect (action_group,
574 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
575 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
576 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
577 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self, 0);
579 /* dirty all nodes, as action bindings may change */
580 dirty_all_nodes (self);
582 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
586 * gtk_ui_manager_remove_action_group:
587 * @self: a #GtkUIManager object
588 * @action_group: the action group to be removed
590 * Removes an action group from the list of action groups associated
596 gtk_ui_manager_remove_action_group (GtkUIManager *self,
597 GtkActionGroup *action_group)
599 g_return_if_fail (GTK_IS_UI_MANAGER (self));
600 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
601 g_return_if_fail (g_list_find (self->private_data->action_groups,
602 action_group) != NULL);
604 self->private_data->action_groups =
605 g_list_remove (self->private_data->action_groups, action_group);
607 g_object_disconnect (action_group,
608 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
609 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
610 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
611 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
613 g_object_unref (action_group);
615 /* dirty all nodes, as action bindings may change */
616 dirty_all_nodes (self);
618 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
622 * gtk_ui_manager_get_action_groups:
623 * @self: a #GtkUIManager object
625 * Returns the list of action groups associated with @self.
627 * Return value: a #GList of action groups. The list is owned by GTK+
628 * and should not be modified.
633 gtk_ui_manager_get_action_groups (GtkUIManager *self)
635 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
637 return self->private_data->action_groups;
641 * gtk_ui_manager_get_accel_group:
642 * @self: a #GtkUIManager object
644 * Returns the #GtkAccelGroup associated with @self.
646 * Return value: the #GtkAccelGroup.
651 gtk_ui_manager_get_accel_group (GtkUIManager *self)
653 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
655 return self->private_data->accel_group;
659 * gtk_ui_manager_get_widget:
660 * @self: a #GtkUIManager
663 * Looks up a widget by following a path.
664 * The path consists of the names specified in the XML description of the UI.
665 * separated by '/'. Elements which don't have a name or action attribute in
666 * the XML (e.g. <popup>) can be addressed by their XML element name
667 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
669 * Return value: the widget found by following the path, or %NULL if no widget
675 gtk_ui_manager_get_widget (GtkUIManager *self,
680 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
681 g_return_val_if_fail (path != NULL, NULL);
683 /* ensure that there are no pending updates before we get the
685 gtk_ui_manager_ensure_update (self);
687 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
692 return NODE_INFO (node)->proxy;
696 collect_toplevels (GNode *node,
700 GtkUIManagerItemType types;
704 switch (NODE_INFO (node)->type) {
705 case NODE_TYPE_MENUBAR:
706 if (data->types & GTK_UI_MANAGER_MENUBAR)
707 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
709 case NODE_TYPE_TOOLBAR:
710 if (data->types & GTK_UI_MANAGER_TOOLBAR)
711 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
713 case NODE_TYPE_POPUP:
714 if (data->types & GTK_UI_MANAGER_POPUP)
715 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
722 * gtk_ui_manager_get_toplevels:
723 * @self: a #GtkUIManager
724 * @types: specifies the types of toplevel widgets to include. Allowed
725 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
726 * #GTK_UI_MANAGER_POPUP.
728 * Obtains a list of all toplevel widgets of the requested types.
730 * Return value: a newly-allocated of all toplevel widgets of the requested
736 gtk_ui_manager_get_toplevels (GtkUIManager *self,
737 GtkUIManagerItemType types)
740 GtkUIManagerItemType types;
744 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
745 GTK_UI_MANAGER_TOOLBAR |
746 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
752 g_node_children_foreach (self->private_data->root_node,
754 collect_toplevels, &data);
761 * gtk_ui_manager_get_action:
762 * @self: a #GtkUIManager
765 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
766 * for more information about paths.
768 * Return value: the action whose proxy widget is found by following the path,
769 * or %NULL if no widget was found.
774 gtk_ui_manager_get_action (GtkUIManager *self,
779 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
780 g_return_val_if_fail (path != NULL, NULL);
782 /* ensure that there are no pending updates before we get
784 gtk_ui_manager_ensure_update (self);
786 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
791 return NODE_INFO (node)->action;
795 get_child_node (GtkUIManager *self,
797 const gchar *childname,
798 gint childname_length,
805 g_return_val_if_fail (parent == NULL ||
806 (NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
807 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM),
814 for (child = parent->children; child != NULL; child = child->next)
816 if (strlen (NODE_INFO (child)->name) == childname_length &&
817 !strncmp (NODE_INFO (child)->name, childname, childname_length))
819 /* if undecided about node type, set it */
820 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
821 NODE_INFO (child)->type = node_type;
823 /* warn about type mismatch */
824 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
825 node_type != NODE_TYPE_UNDECIDED &&
826 NODE_INFO (child)->type != node_type)
827 g_warning ("node type doesn't match %d (%s is type %d)",
829 NODE_INFO (child)->name,
830 NODE_INFO (child)->type);
836 if (!child && create)
840 mnode = g_chunk_new0 (Node, merge_node_chunk);
841 mnode->type = node_type;
842 mnode->name = g_strndup (childname, childname_length);
846 child = g_node_prepend_data (parent, mnode);
848 child = g_node_append_data (parent, mnode);
853 /* handle root node */
854 if (self->private_data->root_node)
856 child = self->private_data->root_node;
857 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
858 g_warning ("root node name '%s' doesn't match '%s'",
859 childname, NODE_INFO (child)->name);
860 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
861 g_warning ("base element must be of type ROOT");
867 mnode = g_chunk_new0 (Node, merge_node_chunk);
868 mnode->type = node_type;
869 mnode->name = g_strndup (childname, childname_length);
872 child = self->private_data->root_node = g_node_new (mnode);
880 get_node (GtkUIManager *self,
885 const gchar *pos, *end;
886 GNode *parent, *node;
888 end = path + strlen (path);
890 parent = node = NULL;
896 slash = strchr (pos, '/');
898 length = slash - pos;
900 length = strlen (pos);
902 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
907 pos += length + 1; /* move past the node name and the slash too */
911 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
912 NODE_INFO (node)->type = node_type;
918 free_node (GNode *node)
920 Node *info = NODE_INFO (node);
922 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
923 g_list_free (info->uifiles);
926 g_object_unref (info->action);
928 g_chunk_free (info, merge_node_chunk);
934 * gtk_ui_manager_new_merge_id:
935 * @self: a #GtkUIManager
937 * Returns an unused merge id, suitable for use with
938 * gtk_ui_manager_add_ui().
940 * Return value: an unused merge id.
945 gtk_ui_manager_new_merge_id (GtkUIManager *self)
947 self->private_data->last_merge_id++;
949 return self->private_data->last_merge_id;
953 node_prepend_ui_reference (Node *node,
957 NodeUIReference *reference;
959 reference = g_new (NodeUIReference, 1);
960 reference->action_quark = action_quark;
961 reference->merge_id = merge_id;
963 /* Prepend the reference */
964 node->uifiles = g_list_prepend (node->uifiles, reference);
970 node_remove_ui_reference (Node *node,
975 for (p = node->uifiles; p != NULL; p = p->next)
977 NodeUIReference *reference = p->data;
979 if (reference->merge_id == merge_id)
981 node->uifiles = g_list_remove_link (node->uifiles, p);
990 /* -------------------- The UI file parser -------------------- */
1004 typedef struct _ParseContext ParseContext;
1005 struct _ParseContext
1008 ParseState prev_state;
1018 start_element_handler (GMarkupParseContext *context,
1019 const gchar *element_name,
1020 const gchar **attribute_names,
1021 const gchar **attribute_values,
1025 ParseContext *ctx = user_data;
1026 GtkUIManager *self = ctx->self;
1029 const gchar *node_name;
1030 const gchar *action;
1031 GQuark action_quark;
1034 gboolean raise_error = TRUE;
1041 for (i = 0; attribute_names[i] != NULL; i++)
1043 if (!strcmp (attribute_names[i], "name"))
1045 node_name = attribute_values[i];
1047 else if (!strcmp (attribute_names[i], "action"))
1049 action = attribute_values[i];
1050 action_quark = g_quark_from_string (attribute_values[i]);
1052 else if (!strcmp (attribute_names[i], "position"))
1054 top = !strcmp (attribute_values[i], "top");
1058 gint line_number, char_number;
1060 g_markup_parse_context_get_position (context,
1061 &line_number, &char_number);
1064 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1065 _("Unknown attribute '%s' on line %d char %d"),
1067 line_number, char_number);
1072 /* Work out a name for this node. Either the name attribute, or
1073 * the action, or the element name */
1074 if (node_name == NULL)
1079 node_name = element_name;
1082 switch (element_name[0])
1085 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1087 ctx->state = STATE_ACCELERATOR;
1088 ctx->current = get_child_node (self, ctx->current,
1089 node_name, strlen (node_name),
1090 NODE_TYPE_ACCELERATOR,
1092 if (NODE_INFO (ctx->current)->action_name == 0)
1093 NODE_INFO (ctx->current)->action_name = action_quark;
1095 node_prepend_ui_reference (NODE_INFO (ctx->current),
1096 ctx->merge_id, action_quark);
1097 NODE_INFO (ctx->current)->dirty = TRUE;
1099 raise_error = FALSE;
1103 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1105 ctx->state = STATE_ROOT;
1106 ctx->current = self->private_data->root_node;
1107 raise_error = FALSE;
1109 node_prepend_ui_reference (NODE_INFO (ctx->current),
1110 ctx->merge_id, action_quark);
1114 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1116 ctx->state = STATE_MENU;
1117 ctx->current = get_child_node (self, ctx->current,
1118 node_name, strlen (node_name),
1121 if (NODE_INFO (ctx->current)->action_name == 0)
1122 NODE_INFO (ctx->current)->action_name = action_quark;
1124 node_prepend_ui_reference (NODE_INFO (ctx->current),
1125 ctx->merge_id, action_quark);
1126 NODE_INFO (ctx->current)->dirty = TRUE;
1128 raise_error = FALSE;
1130 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1132 ctx->current = get_child_node (self, ctx->current,
1133 node_name, strlen (node_name),
1136 if (NODE_INFO (ctx->current)->action_name == 0)
1137 NODE_INFO (ctx->current)->action_name = action_quark;
1139 node_prepend_ui_reference (NODE_INFO (ctx->current),
1140 ctx->merge_id, action_quark);
1141 NODE_INFO (ctx->current)->dirty = TRUE;
1143 raise_error = FALSE;
1145 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1149 ctx->state = STATE_MENUITEM;
1150 node = get_child_node (self, ctx->current,
1151 node_name, strlen (node_name),
1154 if (NODE_INFO (node)->action_name == 0)
1155 NODE_INFO (node)->action_name = action_quark;
1157 node_prepend_ui_reference (NODE_INFO (node),
1158 ctx->merge_id, action_quark);
1159 NODE_INFO (node)->dirty = TRUE;
1161 raise_error = FALSE;
1165 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1167 ctx->state = STATE_MENU;
1168 ctx->current = get_child_node (self, ctx->current,
1169 node_name, strlen (node_name),
1172 if (NODE_INFO (ctx->current)->action_name == 0)
1173 NODE_INFO (ctx->current)->action_name = action_quark;
1175 node_prepend_ui_reference (NODE_INFO (ctx->current),
1176 ctx->merge_id, action_quark);
1177 NODE_INFO (ctx->current)->dirty = TRUE;
1179 raise_error = FALSE;
1181 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1182 !strcmp (element_name, "placeholder"))
1184 if (ctx->state == STATE_TOOLBAR)
1185 ctx->current = get_child_node (self, ctx->current,
1186 node_name, strlen (node_name),
1187 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1190 ctx->current = get_child_node (self, ctx->current,
1191 node_name, strlen (node_name),
1192 NODE_TYPE_MENU_PLACEHOLDER,
1195 node_prepend_ui_reference (NODE_INFO (ctx->current),
1196 ctx->merge_id, action_quark);
1197 NODE_INFO (ctx->current)->dirty = TRUE;
1199 raise_error = FALSE;
1203 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1204 !strcmp (element_name, "separator"))
1208 if (ctx->state == STATE_TOOLBAR)
1209 ctx->state = STATE_TOOLITEM;
1211 ctx->state = STATE_MENUITEM;
1212 node = get_child_node (self, ctx->current,
1213 node_name, strlen (node_name),
1214 NODE_TYPE_SEPARATOR,
1216 if (NODE_INFO (node)->action_name == 0)
1217 NODE_INFO (node)->action_name = action_quark;
1219 node_prepend_ui_reference (NODE_INFO (node),
1220 ctx->merge_id, action_quark);
1221 NODE_INFO (node)->dirty = TRUE;
1223 raise_error = FALSE;
1227 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1229 ctx->state = STATE_TOOLBAR;
1230 ctx->current = get_child_node (self, ctx->current,
1231 node_name, strlen (node_name),
1234 if (NODE_INFO (ctx->current)->action_name == 0)
1235 NODE_INFO (ctx->current)->action_name = action_quark;
1237 node_prepend_ui_reference (NODE_INFO (ctx->current),
1238 ctx->merge_id, action_quark);
1239 NODE_INFO (ctx->current)->dirty = TRUE;
1241 raise_error = FALSE;
1243 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1247 ctx->state = STATE_TOOLITEM;
1248 node = get_child_node (self, ctx->current,
1249 node_name, strlen (node_name),
1252 if (NODE_INFO (node)->action_name == 0)
1253 NODE_INFO (node)->action_name = action_quark;
1255 node_prepend_ui_reference (NODE_INFO (node),
1256 ctx->merge_id, action_quark);
1257 NODE_INFO (node)->dirty = TRUE;
1259 raise_error = FALSE;
1267 gint line_number, char_number;
1269 g_markup_parse_context_get_position (context,
1270 &line_number, &char_number);
1273 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1274 _("Unexpected start tag '%s' on line %d char %d"),
1276 line_number, char_number);
1281 end_element_handler (GMarkupParseContext *context,
1282 const gchar *element_name,
1286 ParseContext *ctx = user_data;
1292 /* no need to GError here, GMarkup already catches this */
1295 ctx->current = NULL;
1296 ctx->state = STATE_END;
1300 case STATE_ACCELERATOR:
1301 ctx->current = ctx->current->parent;
1302 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1303 ctx->state = STATE_ROOT;
1304 /* else, stay in same state */
1306 case STATE_MENUITEM:
1307 ctx->state = STATE_MENU;
1309 case STATE_TOOLITEM:
1310 ctx->state = STATE_TOOLBAR;
1316 cleanup (GMarkupParseContext *context,
1320 ParseContext *ctx = user_data;
1322 ctx->current = NULL;
1323 /* should also walk through the tree and get rid of nodes related to
1324 * this UI file's tag */
1326 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1330 xml_isspace (char c)
1332 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1336 text_handler (GMarkupParseContext *context,
1346 end = text + text_len;
1347 while (p != end && xml_isspace (*p))
1352 gint line_number, char_number;
1354 g_markup_parse_context_get_position (context,
1355 &line_number, &char_number);
1358 G_MARKUP_ERROR_INVALID_CONTENT,
1359 _("Unexpected character data on line %d char %d"),
1360 line_number, char_number);
1365 static GMarkupParser ui_parser = {
1366 start_element_handler,
1367 end_element_handler,
1374 add_ui_from_string (GtkUIManager *self,
1375 const gchar *buffer,
1377 gboolean needs_root,
1380 ParseContext ctx = { 0 };
1381 GMarkupParseContext *context;
1383 ctx.state = STATE_START;
1386 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1388 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1391 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1394 if (!g_markup_parse_context_parse (context, buffer, length, error))
1398 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1401 if (!g_markup_parse_context_end_parse (context, error))
1404 g_markup_parse_context_free (context);
1406 queue_update (self);
1408 g_object_notify (G_OBJECT (self), "ui");
1410 return ctx.merge_id;
1414 g_markup_parse_context_free (context);
1420 * gtk_ui_manager_add_ui_from_string:
1421 * @self: a #GtkUIManager object
1422 * @buffer: the string to parse
1423 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1424 * @error: return location for an error
1426 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1427 * merges it with the current contents of @self. An enclosing <ui>
1428 * element is added if it is missing.
1430 * Return value: The merge id for the merged UI. The merge id can be used
1431 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1432 * the return value is 0.
1437 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1438 const gchar *buffer,
1442 gboolean needs_root = TRUE;
1446 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1447 g_return_val_if_fail (buffer != NULL, 0);
1450 length = strlen (buffer);
1453 end = buffer + length;
1454 while (p != end && xml_isspace (*p))
1457 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1460 return add_ui_from_string (self, buffer, length, needs_root, error);
1464 * gtk_ui_manager_add_ui_from_file:
1465 * @self: a #GtkUIManager object
1466 * @filename: the name of the file to parse
1467 * @error: return location for an error
1469 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1470 * merges it with the current contents of @self.
1472 * Return value: The merge id for the merged UI. The merge id can be used
1473 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1474 * the return value is 0.
1479 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1480 const gchar *filename,
1487 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1489 if (!g_file_get_contents (filename, &buffer, &length, error))
1492 res = add_ui_from_string (self, buffer, length, FALSE, error);
1499 * gtk_ui_manager_add_ui:
1500 * @self: a #GtkUIManager
1501 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1503 * @name: the name for the added UI element
1504 * @action: the name of the action to be proxied, or %NULL to add a separator
1505 * @type: the type of UI element to add.
1506 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1507 * is added after its siblings.
1509 * Adds a UI element to the current contents of @self.
1511 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1512 * separator if such an element can be inserted at the place determined by
1513 * @path. Otherwise @type must indicate an element that can be inserted at
1514 * the place determined by @path.
1519 gtk_ui_manager_add_ui (GtkUIManager *self,
1523 const gchar *action,
1524 GtkUIManagerItemType type,
1530 GQuark action_quark = 0;
1532 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1533 g_return_if_fail (merge_id > 0);
1534 g_return_if_fail (name != NULL);
1536 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1541 node_type = NODE_TYPE_UNDECIDED;
1543 switch (NODE_INFO (node)->type)
1545 case NODE_TYPE_MENUBAR:
1546 case NODE_TYPE_MENU:
1547 case NODE_TYPE_POPUP:
1548 case NODE_TYPE_MENU_PLACEHOLDER:
1551 case GTK_UI_MANAGER_AUTO:
1553 node_type = NODE_TYPE_MENUITEM;
1555 node_type = NODE_TYPE_SEPARATOR;
1557 case GTK_UI_MANAGER_MENU:
1558 node_type = NODE_TYPE_MENU;
1560 case GTK_UI_MANAGER_MENUITEM:
1561 node_type = NODE_TYPE_MENUITEM;
1563 case GTK_UI_MANAGER_SEPARATOR:
1564 node_type = NODE_TYPE_SEPARATOR;
1566 case GTK_UI_MANAGER_PLACEHOLDER:
1567 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1573 case NODE_TYPE_TOOLBAR:
1574 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1577 case GTK_UI_MANAGER_AUTO:
1579 node_type = NODE_TYPE_TOOLITEM;
1581 node_type = NODE_TYPE_SEPARATOR;
1583 case GTK_UI_MANAGER_TOOLITEM:
1584 node_type = NODE_TYPE_TOOLITEM;
1586 case GTK_UI_MANAGER_SEPARATOR:
1587 node_type = NODE_TYPE_SEPARATOR;
1589 case GTK_UI_MANAGER_PLACEHOLDER:
1590 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1596 case NODE_TYPE_ROOT:
1599 case GTK_UI_MANAGER_MENUBAR:
1600 node_type = NODE_TYPE_MENUBAR;
1602 case GTK_UI_MANAGER_TOOLBAR:
1603 node_type = NODE_TYPE_TOOLBAR;
1605 case GTK_UI_MANAGER_POPUP:
1606 node_type = NODE_TYPE_POPUP;
1608 case GTK_UI_MANAGER_ACCELERATOR:
1609 node_type = NODE_TYPE_ACCELERATOR;
1619 if (node_type == NODE_TYPE_UNDECIDED)
1622 child = get_child_node (self, node,
1623 name, strlen (name),
1624 node_type, TRUE, top);
1627 action_quark = g_quark_from_string (action);
1629 node_prepend_ui_reference (NODE_INFO (child),
1630 merge_id, action_quark);
1632 if (NODE_INFO (node)->action_name == 0)
1633 NODE_INFO (child)->action_name = action_quark;
1635 NODE_INFO (child)->dirty = TRUE;
1637 queue_update (self);
1639 g_object_notify (G_OBJECT (self), "ui");
1643 remove_ui (GNode *node,
1646 guint merge_id = GPOINTER_TO_UINT (user_data);
1648 node_remove_ui_reference (NODE_INFO (node), merge_id);
1650 return FALSE; /* continue */
1654 * gtk_ui_manager_remove_ui:
1655 * @self: a #GtkUIManager object
1656 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1658 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1663 gtk_ui_manager_remove_ui (GtkUIManager *self,
1666 g_node_traverse (self->private_data->root_node,
1667 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1668 remove_ui, GUINT_TO_POINTER (merge_id));
1670 queue_update (self);
1672 g_object_notify (G_OBJECT (self), "ui");
1675 /* -------------------- Updates -------------------- */
1679 get_action_by_name (GtkUIManager *merge,
1680 const gchar *action_name)
1688 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1690 GtkActionGroup *action_group = tmp->data;
1693 action = gtk_action_group_get_action (action_group, action_name);
1703 find_menu_position (GNode *node,
1704 GtkWidget **menushell_p,
1707 GtkWidget *menushell;
1710 g_return_val_if_fail (node != NULL, FALSE);
1711 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1712 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1713 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1714 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1715 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1718 /* first sibling -- look at parent */
1719 if (node->prev == NULL)
1724 parent = node->parent;
1725 switch (NODE_INFO (parent)->type)
1727 case NODE_TYPE_MENUBAR:
1728 case NODE_TYPE_POPUP:
1729 menushell = NODE_INFO (parent)->proxy;
1732 case NODE_TYPE_MENU:
1733 menushell = NODE_INFO (parent)->proxy;
1734 if (GTK_IS_MENU_ITEM (menushell))
1735 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1736 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1737 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1741 g_list_free (siblings);
1743 case NODE_TYPE_MENU_PLACEHOLDER:
1744 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1745 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1746 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1747 NODE_INFO (parent)->proxy) + 1;
1750 g_warning("%s: bad parent node type %d", G_STRLOC,
1751 NODE_INFO (parent)->type);
1757 GtkWidget *prev_child;
1760 sibling = node->prev;
1761 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1762 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1764 prev_child = NODE_INFO (sibling)->proxy;
1766 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1767 menushell = gtk_widget_get_parent (prev_child);
1768 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1770 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1774 *menushell_p = menushell;
1782 find_toolbar_position (GNode *node,
1783 GtkWidget **toolbar_p,
1789 g_return_val_if_fail (node != NULL, FALSE);
1790 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1791 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1792 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1793 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1796 /* first sibling -- look at parent */
1797 if (node->prev == NULL)
1801 parent = node->parent;
1802 switch (NODE_INFO (parent)->type)
1804 case NODE_TYPE_TOOLBAR:
1805 toolbar = NODE_INFO (parent)->proxy;
1808 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1809 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1810 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1811 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1812 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1815 g_warning ("%s: bad parent node type %d", G_STRLOC,
1816 NODE_INFO (parent)->type);
1822 GtkWidget *prev_child;
1825 sibling = node->prev;
1826 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1827 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1829 prev_child = NODE_INFO (sibling)->proxy;
1831 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1832 toolbar = gtk_widget_get_parent (prev_child);
1833 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1835 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1836 GTK_TOOL_ITEM (prev_child)) + 1;
1840 *toolbar_p = toolbar;
1848 * _gtk_menu_is_empty:
1849 * @menu: a #GtkMenu or %NULL
1851 * Determines whether @menu is empty. A menu is considered empty if it
1852 * the only visible children are tearoff menu items or "filler" menu
1853 * items which were inserted to mark the menu as empty.
1855 * This function is used by #GtkAction.
1857 * Return value: whether @menu is empty.
1860 _gtk_menu_is_empty (GtkWidget *menu)
1862 GList *children, *cur;
1864 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1869 children = gtk_container_get_children (GTK_CONTAINER (menu));
1874 if (GTK_WIDGET_VISIBLE (cur->data))
1876 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1877 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1882 g_list_free (children);
1888 SEPARATOR_MODE_SMART,
1889 SEPARATOR_MODE_VISIBLE,
1890 SEPARATOR_MODE_HIDDEN
1893 void _gtk_action_sync_menu_visible (GtkAction *action,
1898 update_smart_separators (GtkWidget *proxy)
1900 GtkWidget *parent = NULL;
1902 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1904 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1905 parent = gtk_widget_get_parent (proxy);
1912 GList *children, *cur, *last;
1916 children = gtk_container_get_children (GTK_CONTAINER (parent));
1927 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
1930 if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
1931 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
1934 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
1935 "gtk-separator-mode"));
1938 case SEPARATOR_MODE_VISIBLE:
1939 gtk_widget_show (GTK_WIDGET (cur->data));
1943 case SEPARATOR_MODE_HIDDEN:
1944 gtk_widget_hide (GTK_WIDGET (cur->data));
1946 case SEPARATOR_MODE_SMART:
1949 gtk_widget_show (GTK_WIDGET (cur->data));
1954 gtk_widget_hide (GTK_WIDGET (cur->data));
1958 else if (GTK_WIDGET_VISIBLE (cur->data))
1961 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
1974 gtk_widget_hide (GTK_WIDGET (last->data));
1976 if (GTK_IS_MENU (parent))
1980 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
1981 if (GTK_IS_MENU_ITEM (item))
1982 _gtk_action_sync_menu_visible (NULL, item, empty);
1983 if (GTK_IS_WIDGET (filler))
1984 g_object_set (G_OBJECT (filler), "visible", empty, NULL);
1987 g_list_free (children);
1992 update_node (GtkUIManager *self,
2000 #ifdef DEBUG_UI_MANAGER
2004 g_return_if_fail (node != NULL);
2005 g_return_if_fail (NODE_INFO (node) != NULL);
2007 info = NODE_INFO (node);
2009 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2011 #ifdef DEBUG_UI_MANAGER
2012 g_print ("update_node name=%s dirty=%d popup %d (",
2013 info->name, info->dirty, in_popup);
2014 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2016 NodeUIReference *ref = tmp->data;
2017 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2026 const gchar *action_name;
2027 NodeUIReference *ref;
2029 if (info->uifiles == NULL) {
2030 /* We may need to remove this node.
2031 * This must be done in post order
2033 goto recurse_children;
2036 ref = info->uifiles->data;
2037 action_name = g_quark_to_string (ref->action_quark);
2038 action = get_action_by_name (self, action_name);
2040 info->dirty = FALSE;
2042 /* Check if the node doesn't have an action and must have an action */
2043 if (action == NULL &&
2044 info->type != NODE_TYPE_ROOT &&
2045 info->type != NODE_TYPE_MENUBAR &&
2046 info->type != NODE_TYPE_TOOLBAR &&
2047 info->type != NODE_TYPE_POPUP &&
2048 info->type != NODE_TYPE_SEPARATOR &&
2049 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2050 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2052 g_warning ("%s: missing action", info->name);
2054 goto recurse_children;
2058 gtk_action_set_accel_group (action, self->private_data->accel_group);
2060 /* If the widget already has a proxy and the action hasn't changed, then
2061 * we only have to update the tearoff menu items.
2063 if (info->proxy != NULL && action == info->action)
2065 if (info->type == NODE_TYPE_MENU)
2070 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2071 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2072 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2073 g_object_set (G_OBJECT (siblings->data),
2074 "visible", self->private_data->add_tearoffs && !in_popup,
2076 g_list_free (siblings);
2079 goto recurse_children;
2084 case NODE_TYPE_MENUBAR:
2085 if (info->proxy == NULL)
2087 info->proxy = gtk_menu_bar_new ();
2088 gtk_widget_set_name (info->proxy, info->name);
2089 gtk_widget_show (info->proxy);
2090 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2093 case NODE_TYPE_POPUP:
2094 if (info->proxy == NULL)
2095 info->proxy = gtk_menu_new ();
2096 gtk_widget_set_name (info->proxy, info->name);
2098 case NODE_TYPE_MENU:
2100 GtkWidget *prev_submenu = NULL;
2103 /* remove the proxy if it is of the wrong type ... */
2105 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2107 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2110 g_object_ref (prev_submenu);
2111 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2113 gtk_action_disconnect_proxy (info->action, info->proxy);
2114 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2118 /* create proxy if needed ... */
2119 if (info->proxy == NULL)
2121 GtkWidget *menushell;
2124 if (find_menu_position (node, &menushell, &pos))
2129 info->proxy = gtk_action_create_menu_item (action);
2130 menu = gtk_menu_new ();
2131 gtk_widget_set_name (info->proxy, info->name);
2132 gtk_widget_set_name (menu, info->name);
2133 tearoff = gtk_tearoff_menu_item_new ();
2134 gtk_widget_set_no_show_all (tearoff, TRUE);
2135 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2136 filler = gtk_menu_item_new_with_label (_("Empty"));
2137 g_object_set_data (G_OBJECT (filler),
2138 "gtk-empty-menu-item",
2139 GINT_TO_POINTER (TRUE));
2140 gtk_widget_set_sensitive (filler, FALSE);
2141 gtk_widget_set_no_show_all (filler, TRUE);
2142 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2143 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2144 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2148 gtk_action_connect_proxy (action, info->proxy);
2152 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2154 g_object_unref (prev_submenu);
2156 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2157 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2158 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2159 g_object_set (G_OBJECT (siblings->data),
2160 "visible", self->private_data->add_tearoffs && !in_popup,
2162 g_list_free (siblings);
2165 case NODE_TYPE_UNDECIDED:
2166 g_warning ("found undecided node!");
2168 case NODE_TYPE_ROOT:
2170 case NODE_TYPE_TOOLBAR:
2171 if (info->proxy == NULL)
2173 info->proxy = gtk_toolbar_new ();
2174 gtk_widget_set_name (info->proxy, info->name);
2175 gtk_widget_show (info->proxy);
2176 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2179 case NODE_TYPE_MENU_PLACEHOLDER:
2180 /* create menu items for placeholders if necessary ... */
2181 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2182 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2186 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2192 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2197 if (info->proxy == NULL)
2199 GtkWidget *menushell;
2202 if (find_menu_position (node, &menushell, &pos))
2204 info->proxy = gtk_separator_menu_item_new ();
2205 g_object_set_data (G_OBJECT (info->proxy),
2206 "gtk-separator-mode",
2207 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2208 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2209 NODE_INFO (node)->proxy, pos);
2211 info->extra = gtk_separator_menu_item_new ();
2212 g_object_set_data (G_OBJECT (info->extra),
2213 "gtk-separator-mode",
2214 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2215 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2216 NODE_INFO (node)->extra, pos+1);
2220 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2221 /* create toolbar items for placeholders if necessary ... */
2222 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2223 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2227 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2233 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2238 if (info->proxy == NULL)
2243 if (find_toolbar_position (node, &toolbar, &pos))
2247 item = gtk_separator_tool_item_new ();
2248 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2249 info->proxy = GTK_WIDGET (item);
2250 g_object_set_data (G_OBJECT (info->proxy),
2251 "gtk-separator-mode",
2252 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2254 item = gtk_separator_tool_item_new ();
2255 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2256 info->extra = GTK_WIDGET (item);
2257 g_object_set_data (G_OBJECT (info->extra),
2258 "gtk-separator-mode",
2259 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2263 case NODE_TYPE_MENUITEM:
2264 /* remove the proxy if it is of the wrong type ... */
2266 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2268 g_signal_handlers_disconnect_by_func (info->proxy,
2269 G_CALLBACK (update_smart_separators),
2271 gtk_action_disconnect_proxy (info->action, info->proxy);
2272 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2276 /* create proxy if needed ... */
2277 if (info->proxy == NULL)
2279 GtkWidget *menushell;
2282 if (find_menu_position (node, &menushell, &pos))
2284 info->proxy = gtk_action_create_menu_item (action);
2285 gtk_widget_set_name (info->proxy, info->name);
2287 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2293 g_signal_handlers_disconnect_by_func (info->proxy,
2294 G_CALLBACK (update_smart_separators),
2296 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2297 gtk_action_connect_proxy (action, info->proxy);
2299 g_signal_connect (info->proxy, "notify::visible",
2300 G_CALLBACK (update_smart_separators), 0);
2303 /* don't show accels in popups */
2304 GtkWidget *label = GTK_BIN (info->proxy)->child;
2305 g_object_set (G_OBJECT (label),
2306 "accel_closure", NULL,
2311 case NODE_TYPE_TOOLITEM:
2312 /* remove the proxy if it is of the wrong type ... */
2313 if (info->proxy && G_OBJECT_TYPE (info->proxy) !=
2314 GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2316 g_signal_handlers_disconnect_by_func (info->proxy,
2317 G_CALLBACK (update_smart_separators),
2319 gtk_action_disconnect_proxy (info->action, info->proxy);
2320 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2324 /* create proxy if needed ... */
2325 if (info->proxy == NULL)
2330 if (find_toolbar_position (node, &toolbar, &pos))
2332 info->proxy = gtk_action_create_tool_item (action);
2333 gtk_widget_set_name (info->proxy, info->name);
2335 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2336 GTK_TOOL_ITEM (info->proxy), pos);
2338 /* FIXME: we must trigger the notify::tooltip handler, since
2339 * tooltips on toolitems can't be set before the toolitem
2340 * is added to the toolbar.
2342 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2343 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2349 g_signal_handlers_disconnect_by_func (info->proxy,
2350 G_CALLBACK (update_smart_separators),
2352 gtk_action_connect_proxy (action, info->proxy);
2354 g_signal_connect (info->proxy, "notify::visible",
2355 G_CALLBACK (update_smart_separators), 0);
2357 case NODE_TYPE_SEPARATOR:
2358 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2359 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2364 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2366 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2371 if (find_toolbar_position (node, &toolbar, &pos))
2373 GtkToolItem *item = gtk_separator_tool_item_new ();
2374 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2375 info->proxy = GTK_WIDGET (item);
2376 gtk_widget_set_no_show_all (info->proxy, TRUE);
2377 g_object_set_data (G_OBJECT (info->proxy),
2378 "gtk-separator-mode",
2379 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2380 gtk_widget_show (info->proxy);
2385 GtkWidget *menushell;
2388 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2390 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2395 if (find_menu_position (node, &menushell, &pos))
2397 info->proxy = gtk_separator_menu_item_new ();
2398 gtk_widget_set_no_show_all (info->proxy, TRUE);
2399 g_object_set_data (G_OBJECT (info->proxy),
2400 "gtk-separator-mode",
2401 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2402 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2404 gtk_widget_show (info->proxy);
2408 case NODE_TYPE_ACCELERATOR:
2409 gtk_action_connect_accelerator (action);
2414 g_object_ref (action);
2416 g_object_unref (info->action);
2417 info->action = action;
2421 /* process children */
2422 child = node->children;
2428 child = current->next;
2429 update_node (self, current, in_popup);
2434 if (info->type == NODE_TYPE_MENU)
2435 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2436 else if (info->type == NODE_TYPE_TOOLBAR)
2437 update_smart_separators (info->proxy);
2440 /* handle cleanup of dead nodes */
2441 if (node->children == NULL && info->uifiles == NULL)
2444 gtk_widget_destroy (info->proxy);
2446 gtk_widget_destroy (info->extra);
2447 if (info->type == NODE_TYPE_ACCELERATOR)
2448 gtk_action_disconnect_accelerator (info->action);
2450 g_node_destroy (node);
2455 do_updates (GtkUIManager *self)
2457 GDK_THREADS_ENTER ();
2459 /* this function needs to check through the tree for dirty nodes.
2460 * For such nodes, it needs to do the following:
2462 * 1) check if they are referenced by any loaded UI files anymore.
2463 * In which case, the proxy widget should be destroyed, unless
2464 * there are any subnodes.
2466 * 2) lookup the action for this node again. If it is different to
2467 * the current one (or if no previous action has been looked up),
2468 * the proxy is reconnected to the new action (or a new proxy widget
2469 * is created and added to the parent container).
2471 update_node (self, self->private_data->root_node, FALSE);
2473 self->private_data->update_tag = 0;
2475 GDK_THREADS_LEAVE ();
2481 queue_update (GtkUIManager *self)
2483 if (self->private_data->update_tag != 0)
2486 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates, self);
2491 * gtk_ui_manager_ensure_update:
2492 * @self: a #GtkUIManager
2494 * Makes sure that all pending updates to the UI have been completed.
2496 * This may occasionally be necessary, since #GtkUIManager updates the
2497 * UI in an idle function. A typical example where this function is
2498 * useful is to enforce that the menubar and toolbar have been added to
2499 * the main window before showing it:
2502 * gtk_container_add (GTK_CONTAINER (window), vbox);
2503 * g_signal_connect (merge, "add_widget",
2504 * G_CALLBACK (add_widget), vbox);
2505 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2506 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2507 * gtk_ui_manager_ensure_update (merge);
2508 * gtk_widget_show (window);
2510 * </informalexample>
2515 gtk_ui_manager_ensure_update (GtkUIManager *self)
2517 if (self->private_data->update_tag != 0)
2519 g_source_remove (self->private_data->update_tag);
2525 dirty_traverse_func (GNode *node,
2528 NODE_INFO (node)->dirty = TRUE;
2533 dirty_all_nodes (GtkUIManager *self)
2535 g_node_traverse (self->private_data->root_node,
2536 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2537 dirty_traverse_func, NULL);
2538 queue_update (self);
2541 static const gchar *open_tag_format[] = {
2544 "%*s<menubar name=\"%s\">\n",
2545 "%*s<menu name=\"%s\" action=\"%s\">\n",
2546 "%*s<toolbar name=\"%s\">\n",
2547 "%*s<placeholder name=\"%s\">\n",
2548 "%*s<placeholder name=\"%s\">\n",
2549 "%*s<popup name=\"%s\">\n",
2550 "%*s<menuitem name=\"%s\" action=\"%s\"/>\n",
2551 "%*s<toolitem name=\"%s\" action=\"%s\"/>\n",
2552 "%*s<separator name=\"%s\"/>\n",
2553 "%*s<accelerator name=\"%s\" action=\"%s\"/>\n",
2556 static const gchar *close_tag_format[] = {
2557 "%*s</UNDECIDED>\n",
2562 "%*s</placeholder>\n",
2563 "%*s</placeholder>\n",
2572 print_node (GtkUIManager *self,
2582 g_string_append_printf (buffer, open_tag_format[mnode->type],
2585 g_quark_to_string (mnode->action_name));
2587 for (child = node->children; child != NULL; child = child->next)
2588 print_node (self, child, indent_level + 2, buffer);
2590 g_string_append_printf (buffer, close_tag_format[mnode->type],
2596 * gtk_ui_manager_get_ui:
2597 * @self: a #GtkUIManager
2599 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2601 * Return value: A newly allocated string containing an XML representation of
2607 gtk_ui_manager_get_ui (GtkUIManager *self)
2611 buffer = g_string_new (NULL);
2613 gtk_ui_manager_ensure_update (self);
2615 print_node (self, self->private_data->root_node, 0, buffer);
2617 return g_string_free (buffer, FALSE);