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 _("Add tearoffs to menus"),
218 _("Whether tearoff menu items should be added to menus"),
222 g_object_class_install_property (gobject_class,
224 g_param_spec_string ("ui",
225 _("Merged UI definition"),
226 _("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 list of all toplevel widgets of the requested types.
735 gtk_ui_manager_get_toplevels (GtkUIManager *self,
736 GtkUIManagerItemType types)
739 GtkUIManagerItemType types;
743 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
744 GTK_UI_MANAGER_TOOLBAR |
745 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
751 g_node_children_foreach (self->private_data->root_node,
753 collect_toplevels, &data);
760 * gtk_ui_manager_get_action:
761 * @self: a #GtkUIManager
764 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
765 * for more information about paths.
767 * Return value: the action whose proxy widget is found by following the path,
768 * or %NULL if no widget was found.
773 gtk_ui_manager_get_action (GtkUIManager *self,
778 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
779 g_return_val_if_fail (path != NULL, NULL);
781 /* ensure that there are no pending updates before we get
783 gtk_ui_manager_ensure_update (self);
785 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
790 return NODE_INFO (node)->action;
794 get_child_node (GtkUIManager *self,
796 const gchar *childname,
797 gint childname_length,
804 g_return_val_if_fail (parent == NULL ||
805 (NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
806 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM),
813 for (child = parent->children; child != NULL; child = child->next)
815 if (strlen (NODE_INFO (child)->name) == childname_length &&
816 !strncmp (NODE_INFO (child)->name, childname, childname_length))
818 /* if undecided about node type, set it */
819 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
820 NODE_INFO (child)->type = node_type;
822 /* warn about type mismatch */
823 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
824 node_type != NODE_TYPE_UNDECIDED &&
825 NODE_INFO (child)->type != node_type)
826 g_warning ("node type doesn't match %d (%s is type %d)",
828 NODE_INFO (child)->name,
829 NODE_INFO (child)->type);
835 if (!child && create)
839 mnode = g_chunk_new0 (Node, merge_node_chunk);
840 mnode->type = node_type;
841 mnode->name = g_strndup (childname, childname_length);
845 child = g_node_prepend_data (parent, mnode);
847 child = g_node_append_data (parent, mnode);
852 /* handle root node */
853 if (self->private_data->root_node)
855 child = self->private_data->root_node;
856 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
857 g_warning ("root node name '%s' doesn't match '%s'",
858 childname, NODE_INFO (child)->name);
859 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
860 g_warning ("base element must be of type ROOT");
866 mnode = g_chunk_new0 (Node, merge_node_chunk);
867 mnode->type = node_type;
868 mnode->name = g_strndup (childname, childname_length);
871 child = self->private_data->root_node = g_node_new (mnode);
879 get_node (GtkUIManager *self,
884 const gchar *pos, *end;
885 GNode *parent, *node;
887 end = path + strlen (path);
889 parent = node = NULL;
895 slash = strchr (pos, '/');
897 length = slash - pos;
899 length = strlen (pos);
901 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
906 pos += length + 1; /* move past the node name and the slash too */
910 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
911 NODE_INFO (node)->type = node_type;
917 free_node (GNode *node)
919 Node *info = NODE_INFO (node);
921 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
922 g_list_free (info->uifiles);
925 g_object_unref (info->action);
927 g_chunk_free (info, merge_node_chunk);
933 * gtk_ui_manager_new_merge_id:
934 * @self: a #GtkUIManager
936 * Returns an unused merge id, suitable for use with
937 * gtk_ui_manager_add_ui().
939 * Return value: an unused merge id.
944 gtk_ui_manager_new_merge_id (GtkUIManager *self)
946 self->private_data->last_merge_id++;
948 return self->private_data->last_merge_id;
952 node_prepend_ui_reference (Node *node,
956 NodeUIReference *reference;
958 reference = g_new (NodeUIReference, 1);
959 reference->action_quark = action_quark;
960 reference->merge_id = merge_id;
962 /* Prepend the reference */
963 node->uifiles = g_list_prepend (node->uifiles, reference);
969 node_remove_ui_reference (Node *node,
974 for (p = node->uifiles; p != NULL; p = p->next)
976 NodeUIReference *reference = p->data;
978 if (reference->merge_id == merge_id)
980 node->uifiles = g_list_remove_link (node->uifiles, p);
989 /* -------------------- The UI file parser -------------------- */
1003 typedef struct _ParseContext ParseContext;
1004 struct _ParseContext
1007 ParseState prev_state;
1017 start_element_handler (GMarkupParseContext *context,
1018 const gchar *element_name,
1019 const gchar **attribute_names,
1020 const gchar **attribute_values,
1024 ParseContext *ctx = user_data;
1025 GtkUIManager *self = ctx->self;
1028 const gchar *node_name;
1029 const gchar *action;
1030 GQuark action_quark;
1033 gboolean raise_error = TRUE;
1040 for (i = 0; attribute_names[i] != NULL; i++)
1042 if (!strcmp (attribute_names[i], "name"))
1044 node_name = attribute_values[i];
1046 else if (!strcmp (attribute_names[i], "action"))
1048 action = attribute_values[i];
1049 action_quark = g_quark_from_string (attribute_values[i]);
1051 else if (!strcmp (attribute_names[i], "position"))
1053 top = !strcmp (attribute_values[i], "top");
1057 gint line_number, char_number;
1059 g_markup_parse_context_get_position (context,
1060 &line_number, &char_number);
1063 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1064 _("Unknown attribute '%s' on line %d char %d"),
1066 line_number, char_number);
1071 /* Work out a name for this node. Either the name attribute, or
1072 * the action, or the element name */
1073 if (node_name == NULL)
1078 node_name = element_name;
1081 switch (element_name[0])
1084 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1086 ctx->state = STATE_ACCELERATOR;
1087 ctx->current = get_child_node (self, ctx->current,
1088 node_name, strlen (node_name),
1089 NODE_TYPE_ACCELERATOR,
1091 if (NODE_INFO (ctx->current)->action_name == 0)
1092 NODE_INFO (ctx->current)->action_name = action_quark;
1094 node_prepend_ui_reference (NODE_INFO (ctx->current),
1095 ctx->merge_id, action_quark);
1096 NODE_INFO (ctx->current)->dirty = TRUE;
1098 raise_error = FALSE;
1102 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1104 ctx->state = STATE_ROOT;
1105 ctx->current = self->private_data->root_node;
1106 raise_error = FALSE;
1108 node_prepend_ui_reference (NODE_INFO (ctx->current),
1109 ctx->merge_id, action_quark);
1113 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1115 ctx->state = STATE_MENU;
1116 ctx->current = get_child_node (self, ctx->current,
1117 node_name, strlen (node_name),
1120 if (NODE_INFO (ctx->current)->action_name == 0)
1121 NODE_INFO (ctx->current)->action_name = action_quark;
1123 node_prepend_ui_reference (NODE_INFO (ctx->current),
1124 ctx->merge_id, action_quark);
1125 NODE_INFO (ctx->current)->dirty = TRUE;
1127 raise_error = FALSE;
1129 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1131 ctx->current = get_child_node (self, ctx->current,
1132 node_name, strlen (node_name),
1135 if (NODE_INFO (ctx->current)->action_name == 0)
1136 NODE_INFO (ctx->current)->action_name = action_quark;
1138 node_prepend_ui_reference (NODE_INFO (ctx->current),
1139 ctx->merge_id, action_quark);
1140 NODE_INFO (ctx->current)->dirty = TRUE;
1142 raise_error = FALSE;
1144 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1148 ctx->state = STATE_MENUITEM;
1149 node = get_child_node (self, ctx->current,
1150 node_name, strlen (node_name),
1153 if (NODE_INFO (node)->action_name == 0)
1154 NODE_INFO (node)->action_name = action_quark;
1156 node_prepend_ui_reference (NODE_INFO (node),
1157 ctx->merge_id, action_quark);
1158 NODE_INFO (node)->dirty = TRUE;
1160 raise_error = FALSE;
1164 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1166 ctx->state = STATE_MENU;
1167 ctx->current = get_child_node (self, ctx->current,
1168 node_name, strlen (node_name),
1171 if (NODE_INFO (ctx->current)->action_name == 0)
1172 NODE_INFO (ctx->current)->action_name = action_quark;
1174 node_prepend_ui_reference (NODE_INFO (ctx->current),
1175 ctx->merge_id, action_quark);
1176 NODE_INFO (ctx->current)->dirty = TRUE;
1178 raise_error = FALSE;
1180 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1181 !strcmp (element_name, "placeholder"))
1183 if (ctx->state == STATE_TOOLBAR)
1184 ctx->current = get_child_node (self, ctx->current,
1185 node_name, strlen (node_name),
1186 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1189 ctx->current = get_child_node (self, ctx->current,
1190 node_name, strlen (node_name),
1191 NODE_TYPE_MENU_PLACEHOLDER,
1194 node_prepend_ui_reference (NODE_INFO (ctx->current),
1195 ctx->merge_id, action_quark);
1196 NODE_INFO (ctx->current)->dirty = TRUE;
1198 raise_error = FALSE;
1202 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1203 !strcmp (element_name, "separator"))
1207 if (ctx->state == STATE_TOOLBAR)
1208 ctx->state = STATE_TOOLITEM;
1210 ctx->state = STATE_MENUITEM;
1211 node = get_child_node (self, ctx->current,
1212 node_name, strlen (node_name),
1213 NODE_TYPE_SEPARATOR,
1215 if (NODE_INFO (node)->action_name == 0)
1216 NODE_INFO (node)->action_name = action_quark;
1218 node_prepend_ui_reference (NODE_INFO (node),
1219 ctx->merge_id, action_quark);
1220 NODE_INFO (node)->dirty = TRUE;
1222 raise_error = FALSE;
1226 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1228 ctx->state = STATE_TOOLBAR;
1229 ctx->current = get_child_node (self, ctx->current,
1230 node_name, strlen (node_name),
1233 if (NODE_INFO (ctx->current)->action_name == 0)
1234 NODE_INFO (ctx->current)->action_name = action_quark;
1236 node_prepend_ui_reference (NODE_INFO (ctx->current),
1237 ctx->merge_id, action_quark);
1238 NODE_INFO (ctx->current)->dirty = TRUE;
1240 raise_error = FALSE;
1242 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1246 ctx->state = STATE_TOOLITEM;
1247 node = get_child_node (self, ctx->current,
1248 node_name, strlen (node_name),
1251 if (NODE_INFO (node)->action_name == 0)
1252 NODE_INFO (node)->action_name = action_quark;
1254 node_prepend_ui_reference (NODE_INFO (node),
1255 ctx->merge_id, action_quark);
1256 NODE_INFO (node)->dirty = TRUE;
1258 raise_error = FALSE;
1266 gint line_number, char_number;
1268 g_markup_parse_context_get_position (context,
1269 &line_number, &char_number);
1272 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1273 _("Unexpected start tag '%s' on line %d char %d"),
1275 line_number, char_number);
1280 end_element_handler (GMarkupParseContext *context,
1281 const gchar *element_name,
1285 ParseContext *ctx = user_data;
1291 /* no need to GError here, GMarkup already catches this */
1294 ctx->current = NULL;
1295 ctx->state = STATE_END;
1299 case STATE_ACCELERATOR:
1300 ctx->current = ctx->current->parent;
1301 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1302 ctx->state = STATE_ROOT;
1303 /* else, stay in same state */
1305 case STATE_MENUITEM:
1306 ctx->state = STATE_MENU;
1308 case STATE_TOOLITEM:
1309 ctx->state = STATE_TOOLBAR;
1315 cleanup (GMarkupParseContext *context,
1319 ParseContext *ctx = user_data;
1321 ctx->current = NULL;
1322 /* should also walk through the tree and get rid of nodes related to
1323 * this UI file's tag */
1325 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1329 xml_isspace (char c)
1331 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1335 text_handler (GMarkupParseContext *context,
1345 end = text + text_len;
1346 while (p != end && xml_isspace (*p))
1351 gint line_number, char_number;
1353 g_markup_parse_context_get_position (context,
1354 &line_number, &char_number);
1357 G_MARKUP_ERROR_INVALID_CONTENT,
1358 _("Unexpected character data on line %d char %d"),
1359 line_number, char_number);
1364 static GMarkupParser ui_parser = {
1365 start_element_handler,
1366 end_element_handler,
1373 add_ui_from_string (GtkUIManager *self,
1374 const gchar *buffer,
1376 gboolean needs_root,
1379 ParseContext ctx = { 0 };
1380 GMarkupParseContext *context;
1382 ctx.state = STATE_START;
1385 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1387 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1390 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1393 if (!g_markup_parse_context_parse (context, buffer, length, error))
1397 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1400 if (!g_markup_parse_context_end_parse (context, error))
1403 g_markup_parse_context_free (context);
1405 queue_update (self);
1407 g_object_notify (G_OBJECT (self), "ui");
1409 return ctx.merge_id;
1413 g_markup_parse_context_free (context);
1419 * gtk_ui_manager_add_ui_from_string:
1420 * @self: a #GtkUIManager object
1421 * @buffer: the string to parse
1422 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1423 * @error: return location for an error
1425 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1426 * merges it with the current contents of @self. An enclosing <ui>
1427 * element is added if it is missing.
1429 * Return value: The merge id for the merged UI. The merge id can be used
1430 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1431 * the return value is 0.
1436 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1437 const gchar *buffer,
1441 gboolean needs_root = TRUE;
1445 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1446 g_return_val_if_fail (buffer != NULL, 0);
1449 length = strlen (buffer);
1452 end = buffer + length;
1453 while (p != end && xml_isspace (*p))
1456 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1459 return add_ui_from_string (self, buffer, length, needs_root, error);
1463 * gtk_ui_manager_add_ui_from_file:
1464 * @self: a #GtkUIManager object
1465 * @filename: the name of the file to parse
1466 * @error: return location for an error
1468 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1469 * merges it with the current contents of @self.
1471 * Return value: The merge id for the merged UI. The merge id can be used
1472 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1473 * the return value is 0.
1478 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1479 const gchar *filename,
1486 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1488 if (!g_file_get_contents (filename, &buffer, &length, error))
1491 res = add_ui_from_string (self, buffer, length, FALSE, error);
1498 * gtk_ui_manager_add_ui:
1499 * @self: a #GtkUIManager
1500 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1502 * @name: the name for the added UI element
1503 * @action: the name of the action to be proxied, or %NULL to add a separator
1504 * @type: the type of UI element to add.
1505 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1506 * is added after its siblings.
1508 * Adds a UI element to the current contents of @self.
1510 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1511 * separator if such an element can be inserted at the place determined by
1512 * @path. Otherwise @type must indicate an element that can be inserted at
1513 * the place determined by @path.
1518 gtk_ui_manager_add_ui (GtkUIManager *self,
1522 const gchar *action,
1523 GtkUIManagerItemType type,
1529 GQuark action_quark = 0;
1531 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1532 g_return_if_fail (merge_id > 0);
1533 g_return_if_fail (name != NULL);
1535 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1540 node_type = NODE_TYPE_UNDECIDED;
1542 switch (NODE_INFO (node)->type)
1544 case NODE_TYPE_MENUBAR:
1545 case NODE_TYPE_MENU:
1546 case NODE_TYPE_POPUP:
1547 case NODE_TYPE_MENU_PLACEHOLDER:
1550 case GTK_UI_MANAGER_AUTO:
1552 node_type = NODE_TYPE_MENUITEM;
1554 node_type = NODE_TYPE_SEPARATOR;
1556 case GTK_UI_MANAGER_MENU:
1557 node_type = NODE_TYPE_MENU;
1559 case GTK_UI_MANAGER_MENUITEM:
1560 node_type = NODE_TYPE_MENUITEM;
1562 case GTK_UI_MANAGER_SEPARATOR:
1563 node_type = NODE_TYPE_SEPARATOR;
1565 case GTK_UI_MANAGER_PLACEHOLDER:
1566 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1572 case NODE_TYPE_TOOLBAR:
1573 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1576 case GTK_UI_MANAGER_AUTO:
1578 node_type = NODE_TYPE_TOOLITEM;
1580 node_type = NODE_TYPE_SEPARATOR;
1582 case GTK_UI_MANAGER_TOOLITEM:
1583 node_type = NODE_TYPE_TOOLITEM;
1585 case GTK_UI_MANAGER_SEPARATOR:
1586 node_type = NODE_TYPE_SEPARATOR;
1588 case GTK_UI_MANAGER_PLACEHOLDER:
1589 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1595 case NODE_TYPE_ROOT:
1598 case GTK_UI_MANAGER_MENUBAR:
1599 node_type = NODE_TYPE_MENUBAR;
1601 case GTK_UI_MANAGER_TOOLBAR:
1602 node_type = NODE_TYPE_TOOLBAR;
1604 case GTK_UI_MANAGER_POPUP:
1605 node_type = NODE_TYPE_POPUP;
1607 case GTK_UI_MANAGER_ACCELERATOR:
1608 node_type = NODE_TYPE_ACCELERATOR;
1618 if (node_type == NODE_TYPE_UNDECIDED)
1621 child = get_child_node (self, node,
1622 name, strlen (name),
1623 node_type, TRUE, top);
1626 action_quark = g_quark_from_string (action);
1628 node_prepend_ui_reference (NODE_INFO (child),
1629 merge_id, action_quark);
1631 if (NODE_INFO (node)->action_name == 0)
1632 NODE_INFO (child)->action_name = action_quark;
1634 NODE_INFO (child)->dirty = TRUE;
1636 queue_update (self);
1638 g_object_notify (G_OBJECT (self), "ui");
1642 remove_ui (GNode *node,
1645 guint merge_id = GPOINTER_TO_UINT (user_data);
1647 node_remove_ui_reference (NODE_INFO (node), merge_id);
1649 return FALSE; /* continue */
1653 * gtk_ui_manager_remove_ui:
1654 * @self: a #GtkUIManager object
1655 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1657 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1662 gtk_ui_manager_remove_ui (GtkUIManager *self,
1665 g_node_traverse (self->private_data->root_node,
1666 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1667 remove_ui, GUINT_TO_POINTER (merge_id));
1669 queue_update (self);
1671 g_object_notify (G_OBJECT (self), "ui");
1674 /* -------------------- Updates -------------------- */
1678 get_action_by_name (GtkUIManager *merge,
1679 const gchar *action_name)
1687 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1689 GtkActionGroup *action_group = tmp->data;
1692 action = gtk_action_group_get_action (action_group, action_name);
1702 find_menu_position (GNode *node,
1703 GtkWidget **menushell_p,
1706 GtkWidget *menushell;
1709 g_return_val_if_fail (node != NULL, FALSE);
1710 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1711 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1712 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1713 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1714 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1717 /* first sibling -- look at parent */
1718 if (node->prev == NULL)
1723 parent = node->parent;
1724 switch (NODE_INFO (parent)->type)
1726 case NODE_TYPE_MENUBAR:
1727 case NODE_TYPE_POPUP:
1728 menushell = NODE_INFO (parent)->proxy;
1731 case NODE_TYPE_MENU:
1732 menushell = NODE_INFO (parent)->proxy;
1733 if (GTK_IS_MENU_ITEM (menushell))
1734 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1735 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1736 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1740 g_list_free (siblings);
1742 case NODE_TYPE_MENU_PLACEHOLDER:
1743 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1744 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1745 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1746 NODE_INFO (parent)->proxy) + 1;
1749 g_warning("%s: bad parent node type %d", G_STRLOC,
1750 NODE_INFO (parent)->type);
1756 GtkWidget *prev_child;
1759 sibling = node->prev;
1760 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1761 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1763 prev_child = NODE_INFO (sibling)->proxy;
1765 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1766 menushell = gtk_widget_get_parent (prev_child);
1767 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1769 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1773 *menushell_p = menushell;
1781 find_toolbar_position (GNode *node,
1782 GtkWidget **toolbar_p,
1788 g_return_val_if_fail (node != NULL, FALSE);
1789 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1790 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1791 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1792 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1795 /* first sibling -- look at parent */
1796 if (node->prev == NULL)
1800 parent = node->parent;
1801 switch (NODE_INFO (parent)->type)
1803 case NODE_TYPE_TOOLBAR:
1804 toolbar = NODE_INFO (parent)->proxy;
1807 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1808 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1809 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1810 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1811 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1814 g_warning ("%s: bad parent node type %d", G_STRLOC,
1815 NODE_INFO (parent)->type);
1821 GtkWidget *prev_child;
1824 sibling = node->prev;
1825 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1826 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1828 prev_child = NODE_INFO (sibling)->proxy;
1830 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1831 toolbar = gtk_widget_get_parent (prev_child);
1832 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1834 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1835 GTK_TOOL_ITEM (prev_child)) + 1;
1839 *toolbar_p = toolbar;
1847 * _gtk_menu_is_empty:
1848 * @menu: a #GtkMenu or %NULL
1850 * Determines whether @menu is empty. A menu is considered empty if it
1851 * the only visible children are tearoff menu items or "filler" menu
1852 * items which were inserted to mark the menu as empty.
1854 * This function is used by #GtkAction.
1856 * Return value: whether @menu is empty.
1859 _gtk_menu_is_empty (GtkWidget *menu)
1861 GList *children, *cur;
1863 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1868 children = gtk_container_get_children (GTK_CONTAINER (menu));
1873 if (GTK_WIDGET_VISIBLE (cur->data))
1875 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1876 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1881 g_list_free (children);
1887 SEPARATOR_MODE_SMART,
1888 SEPARATOR_MODE_VISIBLE,
1889 SEPARATOR_MODE_HIDDEN
1892 void _gtk_action_sync_menu_visible (GtkAction *action,
1897 update_smart_separators (GtkWidget *proxy)
1899 GtkWidget *parent = NULL;
1901 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1903 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1904 parent = gtk_widget_get_parent (proxy);
1911 GList *children, *cur, *last;
1915 children = gtk_container_get_children (GTK_CONTAINER (parent));
1926 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
1929 if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
1930 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
1933 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
1934 "gtk-separator-mode"));
1937 case SEPARATOR_MODE_VISIBLE:
1938 gtk_widget_show (GTK_WIDGET (cur->data));
1942 case SEPARATOR_MODE_HIDDEN:
1943 gtk_widget_hide (GTK_WIDGET (cur->data));
1945 case SEPARATOR_MODE_SMART:
1948 gtk_widget_show (GTK_WIDGET (cur->data));
1953 gtk_widget_hide (GTK_WIDGET (cur->data));
1957 else if (GTK_WIDGET_VISIBLE (cur->data))
1960 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
1973 gtk_widget_hide (GTK_WIDGET (last->data));
1975 if (GTK_IS_MENU (parent))
1979 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
1980 if (GTK_IS_MENU_ITEM (item))
1981 _gtk_action_sync_menu_visible (NULL, item, empty);
1982 if (GTK_IS_WIDGET (filler))
1983 g_object_set (G_OBJECT (filler), "visible", empty, NULL);
1986 g_list_free (children);
1991 update_node (GtkUIManager *self,
1999 #ifdef DEBUG_UI_MANAGER
2003 g_return_if_fail (node != NULL);
2004 g_return_if_fail (NODE_INFO (node) != NULL);
2006 info = NODE_INFO (node);
2008 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2010 #ifdef DEBUG_UI_MANAGER
2011 g_print ("update_node name=%s dirty=%d popup %d (",
2012 info->name, info->dirty, in_popup);
2013 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2015 NodeUIReference *ref = tmp->data;
2016 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2025 const gchar *action_name;
2026 NodeUIReference *ref;
2028 if (info->uifiles == NULL) {
2029 /* We may need to remove this node.
2030 * This must be done in post order
2032 goto recurse_children;
2035 ref = info->uifiles->data;
2036 action_name = g_quark_to_string (ref->action_quark);
2037 action = get_action_by_name (self, action_name);
2039 info->dirty = FALSE;
2041 /* Check if the node doesn't have an action and must have an action */
2042 if (action == NULL &&
2043 info->type != NODE_TYPE_ROOT &&
2044 info->type != NODE_TYPE_MENUBAR &&
2045 info->type != NODE_TYPE_TOOLBAR &&
2046 info->type != NODE_TYPE_POPUP &&
2047 info->type != NODE_TYPE_SEPARATOR &&
2048 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2049 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2051 g_warning ("%s: missing action", info->name);
2053 goto recurse_children;
2057 gtk_action_set_accel_group (action, self->private_data->accel_group);
2059 /* If the widget already has a proxy and the action hasn't changed, then
2060 * we only have to update the tearoff menu items.
2062 if (info->proxy != NULL && action == info->action)
2064 if (info->type == NODE_TYPE_MENU)
2069 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2070 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2071 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2072 g_object_set (G_OBJECT (siblings->data),
2073 "visible", self->private_data->add_tearoffs && !in_popup,
2075 g_list_free (siblings);
2078 goto recurse_children;
2083 case NODE_TYPE_MENUBAR:
2084 if (info->proxy == NULL)
2086 info->proxy = gtk_menu_bar_new ();
2087 gtk_widget_set_name (info->proxy, info->name);
2088 gtk_widget_show (info->proxy);
2089 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2092 case NODE_TYPE_POPUP:
2093 if (info->proxy == NULL)
2094 info->proxy = gtk_menu_new ();
2095 gtk_widget_set_name (info->proxy, info->name);
2097 case NODE_TYPE_MENU:
2099 GtkWidget *prev_submenu = NULL;
2102 /* remove the proxy if it is of the wrong type ... */
2104 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2106 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2109 g_object_ref (prev_submenu);
2110 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2112 gtk_action_disconnect_proxy (info->action, info->proxy);
2113 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2117 /* create proxy if needed ... */
2118 if (info->proxy == NULL)
2120 GtkWidget *menushell;
2123 if (find_menu_position (node, &menushell, &pos))
2128 info->proxy = gtk_action_create_menu_item (action);
2129 menu = gtk_menu_new ();
2130 gtk_widget_set_name (info->proxy, info->name);
2131 gtk_widget_set_name (menu, info->name);
2132 tearoff = gtk_tearoff_menu_item_new ();
2133 gtk_widget_set_no_show_all (tearoff, TRUE);
2134 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2135 filler = gtk_menu_item_new_with_label (_("Empty"));
2136 g_object_set_data (G_OBJECT (filler),
2137 "gtk-empty-menu-item",
2138 GINT_TO_POINTER (TRUE));
2139 gtk_widget_set_sensitive (filler, FALSE);
2140 gtk_widget_set_no_show_all (filler, TRUE);
2141 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2142 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2143 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2147 gtk_action_connect_proxy (action, info->proxy);
2151 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2153 g_object_unref (prev_submenu);
2155 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2156 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2157 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2158 g_object_set (G_OBJECT (siblings->data),
2159 "visible", self->private_data->add_tearoffs && !in_popup,
2161 g_list_free (siblings);
2164 case NODE_TYPE_UNDECIDED:
2165 g_warning ("found undecided node!");
2167 case NODE_TYPE_ROOT:
2169 case NODE_TYPE_TOOLBAR:
2170 if (info->proxy == NULL)
2172 info->proxy = gtk_toolbar_new ();
2173 gtk_widget_set_name (info->proxy, info->name);
2174 gtk_widget_show (info->proxy);
2175 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2178 case NODE_TYPE_MENU_PLACEHOLDER:
2179 /* create menu items for placeholders if necessary ... */
2180 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2181 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2185 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2191 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2196 if (info->proxy == NULL)
2198 GtkWidget *menushell;
2201 if (find_menu_position (node, &menushell, &pos))
2203 info->proxy = gtk_separator_menu_item_new ();
2204 g_object_set_data (G_OBJECT (info->proxy),
2205 "gtk-separator-mode",
2206 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2207 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2208 NODE_INFO (node)->proxy, pos);
2210 info->extra = gtk_separator_menu_item_new ();
2211 g_object_set_data (G_OBJECT (info->extra),
2212 "gtk-separator-mode",
2213 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2214 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2215 NODE_INFO (node)->extra, pos+1);
2219 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2220 /* create toolbar items for placeholders if necessary ... */
2221 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2222 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2226 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2232 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2237 if (info->proxy == NULL)
2242 if (find_toolbar_position (node, &toolbar, &pos))
2246 item = gtk_separator_tool_item_new ();
2247 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2248 info->proxy = GTK_WIDGET (item);
2249 g_object_set_data (G_OBJECT (info->proxy),
2250 "gtk-separator-mode",
2251 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2253 item = gtk_separator_tool_item_new ();
2254 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2255 info->extra = GTK_WIDGET (item);
2256 g_object_set_data (G_OBJECT (info->extra),
2257 "gtk-separator-mode",
2258 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2262 case NODE_TYPE_MENUITEM:
2263 /* remove the proxy if it is of the wrong type ... */
2265 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2267 g_signal_handlers_disconnect_by_func (info->proxy,
2268 G_CALLBACK (update_smart_separators),
2270 gtk_action_disconnect_proxy (info->action, info->proxy);
2271 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2275 /* create proxy if needed ... */
2276 if (info->proxy == NULL)
2278 GtkWidget *menushell;
2281 if (find_menu_position (node, &menushell, &pos))
2283 info->proxy = gtk_action_create_menu_item (action);
2284 gtk_widget_set_name (info->proxy, info->name);
2286 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2292 g_signal_handlers_disconnect_by_func (info->proxy,
2293 G_CALLBACK (update_smart_separators),
2295 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2296 gtk_action_connect_proxy (action, info->proxy);
2298 g_signal_connect (info->proxy, "notify::visible",
2299 G_CALLBACK (update_smart_separators), 0);
2302 /* don't show accels in popups */
2303 GtkWidget *label = GTK_BIN (info->proxy)->child;
2304 g_object_set (G_OBJECT (label),
2305 "accel_closure", NULL,
2310 case NODE_TYPE_TOOLITEM:
2311 /* remove the proxy if it is of the wrong type ... */
2312 if (info->proxy && G_OBJECT_TYPE (info->proxy) !=
2313 GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2315 g_signal_handlers_disconnect_by_func (info->proxy,
2316 G_CALLBACK (update_smart_separators),
2318 gtk_action_disconnect_proxy (info->action, info->proxy);
2319 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2323 /* create proxy if needed ... */
2324 if (info->proxy == NULL)
2329 if (find_toolbar_position (node, &toolbar, &pos))
2331 info->proxy = gtk_action_create_tool_item (action);
2332 gtk_widget_set_name (info->proxy, info->name);
2334 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2335 GTK_TOOL_ITEM (info->proxy), pos);
2337 /* FIXME: we must trigger the notify::tooltip handler, since
2338 * tooltips on toolitems can't be set before the toolitem
2339 * is added to the toolbar.
2341 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2342 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2348 g_signal_handlers_disconnect_by_func (info->proxy,
2349 G_CALLBACK (update_smart_separators),
2351 gtk_action_connect_proxy (action, info->proxy);
2353 g_signal_connect (info->proxy, "notify::visible",
2354 G_CALLBACK (update_smart_separators), 0);
2356 case NODE_TYPE_SEPARATOR:
2357 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2358 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2363 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2365 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2370 if (find_toolbar_position (node, &toolbar, &pos))
2372 GtkToolItem *item = gtk_separator_tool_item_new ();
2373 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2374 info->proxy = GTK_WIDGET (item);
2375 gtk_widget_set_no_show_all (info->proxy, TRUE);
2376 g_object_set_data (G_OBJECT (info->proxy),
2377 "gtk-separator-mode",
2378 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2379 gtk_widget_show (info->proxy);
2384 GtkWidget *menushell;
2387 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2389 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2394 if (find_menu_position (node, &menushell, &pos))
2396 info->proxy = gtk_separator_menu_item_new ();
2397 gtk_widget_set_no_show_all (info->proxy, TRUE);
2398 g_object_set_data (G_OBJECT (info->proxy),
2399 "gtk-separator-mode",
2400 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2401 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2403 gtk_widget_show (info->proxy);
2407 case NODE_TYPE_ACCELERATOR:
2408 gtk_action_connect_accelerator (action);
2413 g_object_ref (action);
2415 g_object_unref (info->action);
2416 info->action = action;
2420 /* process children */
2421 child = node->children;
2427 child = current->next;
2428 update_node (self, current, in_popup);
2433 if (info->type == NODE_TYPE_MENU)
2434 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2435 else if (info->type == NODE_TYPE_TOOLBAR)
2436 update_smart_separators (info->proxy);
2439 /* handle cleanup of dead nodes */
2440 if (node->children == NULL && info->uifiles == NULL)
2443 gtk_widget_destroy (info->proxy);
2445 gtk_widget_destroy (info->extra);
2446 if (info->type == NODE_TYPE_ACCELERATOR)
2447 gtk_action_disconnect_accelerator (info->action);
2449 g_node_destroy (node);
2454 do_updates (GtkUIManager *self)
2456 GDK_THREADS_ENTER ();
2458 /* this function needs to check through the tree for dirty nodes.
2459 * For such nodes, it needs to do the following:
2461 * 1) check if they are referenced by any loaded UI files anymore.
2462 * In which case, the proxy widget should be destroyed, unless
2463 * there are any subnodes.
2465 * 2) lookup the action for this node again. If it is different to
2466 * the current one (or if no previous action has been looked up),
2467 * the proxy is reconnected to the new action (or a new proxy widget
2468 * is created and added to the parent container).
2470 update_node (self, self->private_data->root_node, FALSE);
2472 self->private_data->update_tag = 0;
2474 GDK_THREADS_LEAVE ();
2480 queue_update (GtkUIManager *self)
2482 if (self->private_data->update_tag != 0)
2485 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates, self);
2490 * gtk_ui_manager_ensure_update:
2491 * @self: a #GtkUIManager
2493 * Makes sure that all pending updates to the UI have been completed.
2495 * This may occasionally be necessary, since #GtkUIManager updates the
2496 * UI in an idle function. A typical example where this function is
2497 * useful is to enforce that the menubar and toolbar have been added to
2498 * the main window before showing it:
2501 * gtk_container_add (GTK_CONTAINER (window), vbox);
2502 * g_signal_connect (merge, "add_widget",
2503 * G_CALLBACK (add_widget), vbox);
2504 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2505 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2506 * gtk_ui_manager_ensure_update (merge);
2507 * gtk_widget_show (window);
2509 * </informalexample>
2514 gtk_ui_manager_ensure_update (GtkUIManager *self)
2516 if (self->private_data->update_tag != 0)
2518 g_source_remove (self->private_data->update_tag);
2524 dirty_traverse_func (GNode *node,
2527 NODE_INFO (node)->dirty = TRUE;
2532 dirty_all_nodes (GtkUIManager *self)
2534 g_node_traverse (self->private_data->root_node,
2535 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2536 dirty_traverse_func, NULL);
2537 queue_update (self);
2540 static const gchar *open_tag_format[] = {
2543 "%*s<menubar name=\"%s\">\n",
2544 "%*s<menu name=\"%s\" action=\"%s\">\n",
2545 "%*s<toolbar name=\"%s\">\n",
2546 "%*s<placeholder name=\"%s\">\n",
2547 "%*s<placeholder name=\"%s\">\n",
2548 "%*s<popup name=\"%s\">\n",
2549 "%*s<menuitem name=\"%s\" action=\"%s\"/>\n",
2550 "%*s<toolitem name=\"%s\" action=\"%s\"/>\n",
2551 "%*s<separator name=\"%s\"/>\n",
2552 "%*s<accelerator name=\"%s\" action=\"%s\"/>\n",
2555 static const gchar *close_tag_format[] = {
2556 "%*s</UNDECIDED>\n",
2561 "%*s</placeholder>\n",
2562 "%*s</placeholder>\n",
2571 print_node (GtkUIManager *self,
2581 g_string_append_printf (buffer, open_tag_format[mnode->type],
2584 g_quark_to_string (mnode->action_name));
2586 for (child = node->children; child != NULL; child = child->next)
2587 print_node (self, child, indent_level + 2, buffer);
2589 g_string_append_printf (buffer, close_tag_format[mnode->type],
2595 * gtk_ui_manager_get_ui:
2596 * @self: a #GtkUIManager
2598 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2600 * Return value: A newly allocated string containing an XML representation of
2606 gtk_ui_manager_get_ui (GtkUIManager *self)
2610 buffer = g_string_new (NULL);
2612 gtk_ui_manager_ensure_update (self);
2614 print_node (self, self->private_data->root_node, 0, buffer);
2616 return g_string_free (buffer, FALSE);