2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gtkmarshalers.h"
38 #include "gtkmenubar.h"
39 #include "gtkmenushell.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtkseparatortoolitem.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtktoolbar.h"
44 #include "gtkuimanager.h"
46 #undef DEBUG_UI_MANAGER
55 NODE_TYPE_MENU_PLACEHOLDER,
56 NODE_TYPE_TOOLBAR_PLACEHOLDER,
64 typedef struct _Node Node;
74 GtkWidget *extra; /* second separator for placeholders */
81 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
83 struct _GtkUIManagerPrivate
85 GtkAccelGroup *accel_group;
94 gboolean add_tearoffs;
97 #define NODE_INFO(node) ((Node *)node->data)
99 typedef struct _NodeUIReference NodeUIReference;
101 struct _NodeUIReference
107 static void gtk_ui_manager_class_init (GtkUIManagerClass *class);
108 static void gtk_ui_manager_init (GtkUIManager *self);
109 static void gtk_ui_manager_finalize (GObject *object);
110 static void gtk_ui_manager_set_property (GObject *object,
114 static void gtk_ui_manager_get_property (GObject *object,
118 static void queue_update (GtkUIManager *self);
119 static void dirty_all_nodes (GtkUIManager *self);
120 static GNode * get_child_node (GtkUIManager *self,
122 const gchar *childname,
123 gint childname_length,
127 static GNode * get_node (GtkUIManager *self,
131 static gboolean free_node (GNode *node);
132 static void node_prepend_ui_reference (Node *node,
134 GQuark action_quark);
135 static void node_remove_ui_reference (Node *node,
157 static GObjectClass *parent_class = NULL;
158 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
160 static GMemChunk *merge_node_chunk = NULL;
163 gtk_ui_manager_get_type (void)
165 static GtkType type = 0;
169 static const GTypeInfo type_info =
171 sizeof (GtkUIManagerClass),
172 (GBaseInitFunc) NULL,
173 (GBaseFinalizeFunc) NULL,
174 (GClassInitFunc) gtk_ui_manager_class_init,
175 (GClassFinalizeFunc) NULL,
178 sizeof (GtkUIManager),
180 (GInstanceInitFunc) gtk_ui_manager_init,
183 type = g_type_register_static (G_TYPE_OBJECT,
191 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
193 GObjectClass *gobject_class;
195 parent_class = g_type_class_peek_parent (klass);
197 gobject_class = G_OBJECT_CLASS (klass);
199 if (!merge_node_chunk)
200 merge_node_chunk = g_mem_chunk_create (Node, 64,
203 gobject_class->finalize = gtk_ui_manager_finalize;
204 gobject_class->set_property = gtk_ui_manager_set_property;
205 gobject_class->get_property = gtk_ui_manager_get_property;
208 * GtkUIManager:add-tearoffs:
210 * The "add-tearoffs" property controls whether generated menus
211 * have tearoff menu items.
213 * Note that this only affects regular menus. Generated popup
214 * menus never have tearoff menu items.
218 g_object_class_install_property (gobject_class,
220 g_param_spec_boolean ("add_tearoffs",
221 P_("Add tearoffs to menus"),
222 P_("Whether tearoff menu items should be added to menus"),
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("ui",
229 P_("Merged UI definition"),
230 P_("An XML string describing the merged UI"),
236 * GtkUIManager::add-widget:
237 * @merge: a #GtkUIManager
238 * @widget: the added widget
240 * The add_widget signal is emitted for each generated menubar and toolbar.
241 * It is not emitted for generated popup menus, which can be obtained by
242 * gtk_ui_manager_get_widget().
246 ui_manager_signals[ADD_WIDGET] =
247 g_signal_new ("add_widget",
248 G_OBJECT_CLASS_TYPE (klass),
249 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
250 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
252 g_cclosure_marshal_VOID__OBJECT,
257 * GtkUIManager::actions-changed:
258 * @merge: a #GtkUIManager
260 * The "actions-changed" signal is emitted whenever the set of actions
265 ui_manager_signals[ACTIONS_CHANGED] =
266 g_signal_new ("actions_changed",
267 G_OBJECT_CLASS_TYPE (klass),
268 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
269 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
271 g_cclosure_marshal_VOID__VOID,
275 * GtkUIManager::connect-proxy:
276 * @uimanager: the ui manager
277 * @action: the action
280 * The connect_proxy signal is emitted after connecting a proxy to
281 * an action in the group.
283 * This is intended for simple customizations for which a custom action
284 * class would be too clumsy, e.g. showing tooltips for menuitems in the
289 ui_manager_signals[CONNECT_PROXY] =
290 g_signal_new ("connect_proxy",
291 G_OBJECT_CLASS_TYPE (klass),
292 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
293 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
295 _gtk_marshal_VOID__OBJECT_OBJECT,
301 * GtkUIManager::disconnect-proxy:
302 * @uimanager: the ui manager
303 * @action: the action
306 * The disconnect_proxy signal is emitted after disconnecting a proxy
307 * from an action in the group.
311 ui_manager_signals[DISCONNECT_PROXY] =
312 g_signal_new ("disconnect_proxy",
313 G_OBJECT_CLASS_TYPE (klass),
314 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
315 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
317 _gtk_marshal_VOID__OBJECT_OBJECT,
323 * GtkUIManager::pre-activate:
324 * @uimanager: the ui manager
325 * @action: the action
327 * The pre_activate signal is emitted just before the @action
330 * This is intended for applications to get notification
331 * just before any action is activated.
335 ui_manager_signals[PRE_ACTIVATE] =
336 g_signal_new ("pre_activate",
337 G_OBJECT_CLASS_TYPE (klass),
338 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
339 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
341 _gtk_marshal_VOID__OBJECT,
346 * GtkUIManager::post-activate:
347 * @uimanager: the ui manager
348 * @action: the action
350 * The post_activate signal is emitted just after the @action
353 * This is intended for applications to get notification
354 * just after any action is activated.
358 ui_manager_signals[POST_ACTIVATE] =
359 g_signal_new ("post_activate",
360 G_OBJECT_CLASS_TYPE (klass),
361 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
362 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
364 _gtk_marshal_VOID__OBJECT,
368 klass->add_widget = NULL;
369 klass->actions_changed = NULL;
370 klass->connect_proxy = NULL;
371 klass->disconnect_proxy = NULL;
372 klass->pre_activate = NULL;
373 klass->post_activate = NULL;
375 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
380 gtk_ui_manager_init (GtkUIManager *self)
385 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
387 self->private_data->accel_group = gtk_accel_group_new ();
389 self->private_data->root_node = NULL;
390 self->private_data->action_groups = NULL;
392 self->private_data->last_merge_id = 0;
393 self->private_data->add_tearoffs = FALSE;
395 merge_id = gtk_ui_manager_new_merge_id (self);
396 node = get_child_node (self, NULL, "ui", 2,
397 NODE_TYPE_ROOT, TRUE, FALSE);
398 node_prepend_ui_reference (NODE_INFO (node), merge_id, 0);
402 gtk_ui_manager_finalize (GObject *object)
404 GtkUIManager *self = GTK_UI_MANAGER (object);
406 if (self->private_data->update_tag != 0)
408 g_source_remove (self->private_data->update_tag);
409 self->private_data->update_tag = 0;
412 g_node_traverse (self->private_data->root_node,
413 G_POST_ORDER, G_TRAVERSE_ALL, -1,
414 (GNodeTraverseFunc)free_node, NULL);
415 g_node_destroy (self->private_data->root_node);
416 self->private_data->root_node = NULL;
418 g_list_foreach (self->private_data->action_groups,
419 (GFunc) g_object_unref, NULL);
420 g_list_free (self->private_data->action_groups);
421 self->private_data->action_groups = NULL;
423 g_object_unref (self->private_data->accel_group);
424 self->private_data->accel_group = NULL;
426 G_OBJECT_CLASS (parent_class)->finalize (object);
430 gtk_ui_manager_set_property (GObject *object,
435 GtkUIManager *self = GTK_UI_MANAGER (object);
439 case PROP_ADD_TEAROFFS:
440 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_ui_manager_get_property (GObject *object,
454 GtkUIManager *self = GTK_UI_MANAGER (object);
458 case PROP_ADD_TEAROFFS:
459 g_value_set_boolean (value, self->private_data->add_tearoffs);
462 g_value_set_string (value, gtk_ui_manager_get_ui (self));
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
472 * gtk_ui_manager_new:
474 * Creates a new ui manager object.
476 * Return value: a new ui manager object.
481 gtk_ui_manager_new (void)
483 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
488 * gtk_ui_manager_get_add_tearoffs:
489 * @self: a #GtkUIManager
491 * Returns whether menus generated by this #GtkUIManager
492 * will have tearoff menu items.
494 * Return value: whether tearoff menu items are added
499 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
501 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
503 return self->private_data->add_tearoffs;
508 * gtk_ui_manager_set_add_tearoffs:
509 * @self: a #GtkUIManager
510 * @add_tearoffs: whether tearoff menu items are added
512 * Sets the "add_tearoffs" property, which controls whether menus
513 * generated by this #GtkUIManager will have tearoff menu items.
515 * Note that this only affects regular menus. Generated popup
516 * menus never have tearoff menu items.
521 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
522 gboolean add_tearoffs)
524 g_return_if_fail (GTK_IS_UI_MANAGER (self));
526 add_tearoffs = add_tearoffs != FALSE;
528 if (add_tearoffs != self->private_data->add_tearoffs)
530 self->private_data->add_tearoffs = add_tearoffs;
532 dirty_all_nodes (self);
534 g_object_notify (G_OBJECT (self), "add_tearoffs");
539 cb_proxy_connect_proxy (GtkActionGroup *group,
544 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
548 cb_proxy_disconnect_proxy (GtkActionGroup *group,
553 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
557 cb_proxy_pre_activate (GtkActionGroup *group,
561 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
565 cb_proxy_post_activate (GtkActionGroup *group,
569 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
573 * gtk_ui_manager_insert_action_group:
574 * @self: a #GtkUIManager object
575 * @action_group: the action group to be inserted
576 * @pos: the position at which the group will be inserted.
578 * Inserts an action group into the list of action groups associated
579 * with @self. Actions in earlier groups hide actions with the same
580 * name in later groups.
585 gtk_ui_manager_insert_action_group (GtkUIManager *self,
586 GtkActionGroup *action_group,
589 g_return_if_fail (GTK_IS_UI_MANAGER (self));
590 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
591 g_return_if_fail (g_list_find (self->private_data->action_groups,
592 action_group) == NULL);
594 g_object_ref (action_group);
595 self->private_data->action_groups =
596 g_list_insert (self->private_data->action_groups, action_group, pos);
597 g_object_connect (action_group,
598 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
599 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
600 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
601 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
604 /* dirty all nodes, as action bindings may change */
605 dirty_all_nodes (self);
607 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
611 * gtk_ui_manager_remove_action_group:
612 * @self: a #GtkUIManager object
613 * @action_group: the action group to be removed
615 * Removes an action group from the list of action groups associated
621 gtk_ui_manager_remove_action_group (GtkUIManager *self,
622 GtkActionGroup *action_group)
624 g_return_if_fail (GTK_IS_UI_MANAGER (self));
625 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
626 g_return_if_fail (g_list_find (self->private_data->action_groups,
627 action_group) != NULL);
629 self->private_data->action_groups =
630 g_list_remove (self->private_data->action_groups, action_group);
632 g_object_disconnect (action_group,
633 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
634 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
635 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
636 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
638 g_object_unref (action_group);
640 /* dirty all nodes, as action bindings may change */
641 dirty_all_nodes (self);
643 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
647 * gtk_ui_manager_get_action_groups:
648 * @self: a #GtkUIManager object
650 * Returns the list of action groups associated with @self.
652 * Return value: a #GList of action groups. The list is owned by GTK+
653 * and should not be modified.
658 gtk_ui_manager_get_action_groups (GtkUIManager *self)
660 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
662 return self->private_data->action_groups;
666 * gtk_ui_manager_get_accel_group:
667 * @self: a #GtkUIManager object
669 * Returns the #GtkAccelGroup associated with @self.
671 * Return value: the #GtkAccelGroup.
676 gtk_ui_manager_get_accel_group (GtkUIManager *self)
678 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
680 return self->private_data->accel_group;
684 * gtk_ui_manager_get_widget:
685 * @self: a #GtkUIManager
688 * Looks up a widget by following a path.
689 * The path consists of the names specified in the XML description of the UI.
690 * separated by '/'. Elements which don't have a name or action attribute in
691 * the XML (e.g. <popup>) can be addressed by their XML element name
692 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
694 * Note that the widget found by following a path that ends in a <menu>
695 * element is the menuitem to which the menu is attached, not the menu itself.
697 * Return value: the widget found by following the path, or %NULL if no widget
703 gtk_ui_manager_get_widget (GtkUIManager *self,
708 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
709 g_return_val_if_fail (path != NULL, NULL);
711 /* ensure that there are no pending updates before we get the
713 gtk_ui_manager_ensure_update (self);
715 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
720 return NODE_INFO (node)->proxy;
724 collect_toplevels (GNode *node,
728 GtkUIManagerItemType types;
732 switch (NODE_INFO (node)->type) {
733 case NODE_TYPE_MENUBAR:
734 if (data->types & GTK_UI_MANAGER_MENUBAR)
735 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
737 case NODE_TYPE_TOOLBAR:
738 if (data->types & GTK_UI_MANAGER_TOOLBAR)
739 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
741 case NODE_TYPE_POPUP:
742 if (data->types & GTK_UI_MANAGER_POPUP)
743 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
750 * gtk_ui_manager_get_toplevels:
751 * @self: a #GtkUIManager
752 * @types: specifies the types of toplevel widgets to include. Allowed
753 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
754 * #GTK_UI_MANAGER_POPUP.
756 * Obtains a list of all toplevel widgets of the requested types.
758 * Return value: a newly-allocated of all toplevel widgets of the requested
764 gtk_ui_manager_get_toplevels (GtkUIManager *self,
765 GtkUIManagerItemType types)
768 GtkUIManagerItemType types;
772 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
773 GTK_UI_MANAGER_TOOLBAR |
774 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
780 g_node_children_foreach (self->private_data->root_node,
782 collect_toplevels, &data);
789 * gtk_ui_manager_get_action:
790 * @self: a #GtkUIManager
793 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
794 * for more information about paths.
796 * Return value: the action whose proxy widget is found by following the path,
797 * or %NULL if no widget was found.
802 gtk_ui_manager_get_action (GtkUIManager *self,
807 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
808 g_return_val_if_fail (path != NULL, NULL);
810 /* ensure that there are no pending updates before we get
812 gtk_ui_manager_ensure_update (self);
814 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
819 return NODE_INFO (node)->action;
823 get_child_node (GtkUIManager *self,
825 const gchar *childname,
826 gint childname_length,
833 g_return_val_if_fail (parent == NULL ||
834 (NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
835 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM),
842 for (child = parent->children; child != NULL; child = child->next)
844 if (NODE_INFO (child)->name &&
845 strlen (NODE_INFO (child)->name) == childname_length &&
846 !strncmp (NODE_INFO (child)->name, childname, childname_length))
848 /* if undecided about node type, set it */
849 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
850 NODE_INFO (child)->type = node_type;
852 /* warn about type mismatch */
853 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
854 node_type != NODE_TYPE_UNDECIDED &&
855 NODE_INFO (child)->type != node_type)
856 g_warning ("node type doesn't match %d (%s is type %d)",
858 NODE_INFO (child)->name,
859 NODE_INFO (child)->type);
865 if (!child && create)
869 mnode = g_chunk_new0 (Node, merge_node_chunk);
870 mnode->type = node_type;
871 mnode->name = g_strndup (childname, childname_length);
875 child = g_node_prepend_data (parent, mnode);
877 child = g_node_append_data (parent, mnode);
882 /* handle root node */
883 if (self->private_data->root_node)
885 child = self->private_data->root_node;
886 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
887 g_warning ("root node name '%s' doesn't match '%s'",
888 childname, NODE_INFO (child)->name);
889 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
890 g_warning ("base element must be of type ROOT");
896 mnode = g_chunk_new0 (Node, merge_node_chunk);
897 mnode->type = node_type;
898 mnode->name = g_strndup (childname, childname_length);
901 child = self->private_data->root_node = g_node_new (mnode);
909 get_node (GtkUIManager *self,
914 const gchar *pos, *end;
915 GNode *parent, *node;
917 if (strncmp ("/ui", path, 3) == 0)
920 end = path + strlen (path);
922 parent = node = NULL;
928 slash = strchr (pos, '/');
930 length = slash - pos;
932 length = strlen (pos);
934 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
939 pos += length + 1; /* move past the node name and the slash too */
943 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
944 NODE_INFO (node)->type = node_type;
950 free_node (GNode *node)
952 Node *info = NODE_INFO (node);
954 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
955 g_list_free (info->uifiles);
958 g_object_unref (info->action);
960 g_object_unref (info->proxy);
962 g_object_unref (info->extra);
964 g_chunk_free (info, merge_node_chunk);
970 * gtk_ui_manager_new_merge_id:
971 * @self: a #GtkUIManager
973 * Returns an unused merge id, suitable for use with
974 * gtk_ui_manager_add_ui().
976 * Return value: an unused merge id.
981 gtk_ui_manager_new_merge_id (GtkUIManager *self)
983 self->private_data->last_merge_id++;
985 return self->private_data->last_merge_id;
989 node_prepend_ui_reference (Node *node,
993 NodeUIReference *reference;
995 reference = g_new (NodeUIReference, 1);
996 reference->action_quark = action_quark;
997 reference->merge_id = merge_id;
999 /* Prepend the reference */
1000 node->uifiles = g_list_prepend (node->uifiles, reference);
1006 node_remove_ui_reference (Node *node,
1011 for (p = node->uifiles; p != NULL; p = p->next)
1013 NodeUIReference *reference = p->data;
1015 if (reference->merge_id == merge_id)
1017 node->uifiles = g_list_delete_link (node->uifiles, p);
1026 /* -------------------- The UI file parser -------------------- */
1040 typedef struct _ParseContext ParseContext;
1041 struct _ParseContext
1044 ParseState prev_state;
1054 start_element_handler (GMarkupParseContext *context,
1055 const gchar *element_name,
1056 const gchar **attribute_names,
1057 const gchar **attribute_values,
1061 ParseContext *ctx = user_data;
1062 GtkUIManager *self = ctx->self;
1065 const gchar *node_name;
1066 const gchar *action;
1067 GQuark action_quark;
1070 gboolean raise_error = TRUE;
1077 for (i = 0; attribute_names[i] != NULL; i++)
1079 if (!strcmp (attribute_names[i], "name"))
1081 node_name = attribute_values[i];
1083 else if (!strcmp (attribute_names[i], "action"))
1085 action = attribute_values[i];
1086 action_quark = g_quark_from_string (attribute_values[i]);
1088 else if (!strcmp (attribute_names[i], "position"))
1090 top = !strcmp (attribute_values[i], "top");
1094 gint line_number, char_number;
1096 g_markup_parse_context_get_position (context,
1097 &line_number, &char_number);
1100 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1101 _("Unknown attribute '%s' on line %d char %d"),
1103 line_number, char_number);
1108 /* Work out a name for this node. Either the name attribute, or
1109 * the action, or the element name */
1110 if (node_name == NULL)
1115 node_name = element_name;
1118 switch (element_name[0])
1121 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1123 ctx->state = STATE_ACCELERATOR;
1124 ctx->current = get_child_node (self, ctx->current,
1125 node_name, strlen (node_name),
1126 NODE_TYPE_ACCELERATOR,
1128 if (NODE_INFO (ctx->current)->action_name == 0)
1129 NODE_INFO (ctx->current)->action_name = action_quark;
1131 node_prepend_ui_reference (NODE_INFO (ctx->current),
1132 ctx->merge_id, action_quark);
1133 NODE_INFO (ctx->current)->dirty = TRUE;
1135 raise_error = FALSE;
1139 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1141 ctx->state = STATE_ROOT;
1142 ctx->current = self->private_data->root_node;
1143 raise_error = FALSE;
1145 node_prepend_ui_reference (NODE_INFO (ctx->current),
1146 ctx->merge_id, action_quark);
1150 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1152 ctx->state = STATE_MENU;
1153 ctx->current = get_child_node (self, ctx->current,
1154 node_name, strlen (node_name),
1157 if (NODE_INFO (ctx->current)->action_name == 0)
1158 NODE_INFO (ctx->current)->action_name = action_quark;
1160 node_prepend_ui_reference (NODE_INFO (ctx->current),
1161 ctx->merge_id, action_quark);
1162 NODE_INFO (ctx->current)->dirty = TRUE;
1164 raise_error = FALSE;
1166 else if (ctx->state == STATE_MENU && !strcmp (element_name, "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 && !strcmp (element_name, "menuitem"))
1185 ctx->state = STATE_MENUITEM;
1186 node = get_child_node (self, ctx->current,
1187 node_name, strlen (node_name),
1190 if (NODE_INFO (node)->action_name == 0)
1191 NODE_INFO (node)->action_name = action_quark;
1193 node_prepend_ui_reference (NODE_INFO (node),
1194 ctx->merge_id, action_quark);
1195 NODE_INFO (node)->dirty = TRUE;
1197 raise_error = FALSE;
1201 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1203 ctx->state = STATE_MENU;
1204 ctx->current = get_child_node (self, ctx->current,
1205 node_name, strlen (node_name),
1208 if (NODE_INFO (ctx->current)->action_name == 0)
1209 NODE_INFO (ctx->current)->action_name = action_quark;
1211 node_prepend_ui_reference (NODE_INFO (ctx->current),
1212 ctx->merge_id, action_quark);
1213 NODE_INFO (ctx->current)->dirty = TRUE;
1215 raise_error = FALSE;
1217 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1218 !strcmp (element_name, "placeholder"))
1220 if (ctx->state == STATE_TOOLBAR)
1221 ctx->current = get_child_node (self, ctx->current,
1222 node_name, strlen (node_name),
1223 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1226 ctx->current = get_child_node (self, ctx->current,
1227 node_name, strlen (node_name),
1228 NODE_TYPE_MENU_PLACEHOLDER,
1231 node_prepend_ui_reference (NODE_INFO (ctx->current),
1232 ctx->merge_id, action_quark);
1233 NODE_INFO (ctx->current)->dirty = TRUE;
1235 raise_error = FALSE;
1239 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1240 !strcmp (element_name, "separator"))
1245 if (ctx->state == STATE_TOOLBAR)
1246 ctx->state = STATE_TOOLITEM;
1248 ctx->state = STATE_MENUITEM;
1249 if (!strcmp (node_name, "separator"))
1255 length = strlen (node_name);
1256 node = get_child_node (self, ctx->current,
1258 NODE_TYPE_SEPARATOR,
1261 if (NODE_INFO (node)->action_name == 0)
1262 NODE_INFO (node)->action_name = action_quark;
1264 node_prepend_ui_reference (NODE_INFO (node),
1265 ctx->merge_id, action_quark);
1266 NODE_INFO (node)->dirty = TRUE;
1268 raise_error = FALSE;
1272 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1274 ctx->state = STATE_TOOLBAR;
1275 ctx->current = get_child_node (self, ctx->current,
1276 node_name, strlen (node_name),
1279 if (NODE_INFO (ctx->current)->action_name == 0)
1280 NODE_INFO (ctx->current)->action_name = action_quark;
1282 node_prepend_ui_reference (NODE_INFO (ctx->current),
1283 ctx->merge_id, action_quark);
1284 NODE_INFO (ctx->current)->dirty = TRUE;
1286 raise_error = FALSE;
1288 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1292 ctx->state = STATE_TOOLITEM;
1293 node = get_child_node (self, ctx->current,
1294 node_name, strlen (node_name),
1297 if (NODE_INFO (node)->action_name == 0)
1298 NODE_INFO (node)->action_name = action_quark;
1300 node_prepend_ui_reference (NODE_INFO (node),
1301 ctx->merge_id, action_quark);
1302 NODE_INFO (node)->dirty = TRUE;
1304 raise_error = FALSE;
1312 gint line_number, char_number;
1314 g_markup_parse_context_get_position (context,
1315 &line_number, &char_number);
1318 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1319 _("Unexpected start tag '%s' on line %d char %d"),
1321 line_number, char_number);
1326 end_element_handler (GMarkupParseContext *context,
1327 const gchar *element_name,
1331 ParseContext *ctx = user_data;
1337 /* no need to GError here, GMarkup already catches this */
1340 ctx->current = NULL;
1341 ctx->state = STATE_END;
1345 case STATE_ACCELERATOR:
1346 ctx->current = ctx->current->parent;
1347 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1348 ctx->state = STATE_ROOT;
1349 /* else, stay in same state */
1351 case STATE_MENUITEM:
1352 ctx->state = STATE_MENU;
1354 case STATE_TOOLITEM:
1355 ctx->state = STATE_TOOLBAR;
1361 cleanup (GMarkupParseContext *context,
1365 ParseContext *ctx = user_data;
1367 ctx->current = NULL;
1368 /* should also walk through the tree and get rid of nodes related to
1369 * this UI file's tag */
1371 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1375 xml_isspace (char c)
1377 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1381 text_handler (GMarkupParseContext *context,
1391 end = text + text_len;
1392 while (p != end && xml_isspace (*p))
1397 gint line_number, char_number;
1399 g_markup_parse_context_get_position (context,
1400 &line_number, &char_number);
1403 G_MARKUP_ERROR_INVALID_CONTENT,
1404 _("Unexpected character data on line %d char %d"),
1405 line_number, char_number);
1410 static GMarkupParser ui_parser = {
1411 start_element_handler,
1412 end_element_handler,
1419 add_ui_from_string (GtkUIManager *self,
1420 const gchar *buffer,
1422 gboolean needs_root,
1425 ParseContext ctx = { 0 };
1426 GMarkupParseContext *context;
1428 ctx.state = STATE_START;
1431 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1433 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1436 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1439 if (!g_markup_parse_context_parse (context, buffer, length, error))
1443 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1446 if (!g_markup_parse_context_end_parse (context, error))
1449 g_markup_parse_context_free (context);
1451 queue_update (self);
1453 g_object_notify (G_OBJECT (self), "ui");
1455 return ctx.merge_id;
1459 g_markup_parse_context_free (context);
1465 * gtk_ui_manager_add_ui_from_string:
1466 * @self: a #GtkUIManager object
1467 * @buffer: the string to parse
1468 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1469 * @error: return location for an error
1471 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1472 * merges it with the current contents of @self. An enclosing <ui>
1473 * element is added if it is missing.
1475 * Return value: The merge id for the merged UI. The merge id can be used
1476 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1477 * the return value is 0.
1482 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1483 const gchar *buffer,
1487 gboolean needs_root = TRUE;
1491 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1492 g_return_val_if_fail (buffer != NULL, 0);
1495 length = strlen (buffer);
1498 end = buffer + length;
1499 while (p != end && xml_isspace (*p))
1502 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1505 return add_ui_from_string (self, buffer, length, needs_root, error);
1509 * gtk_ui_manager_add_ui_from_file:
1510 * @self: a #GtkUIManager object
1511 * @filename: the name of the file to parse
1512 * @error: return location for an error
1514 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1515 * merges it with the current contents of @self.
1517 * Return value: The merge id for the merged UI. The merge id can be used
1518 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1519 * the return value is 0.
1524 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1525 const gchar *filename,
1532 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1534 if (!g_file_get_contents (filename, &buffer, &length, error))
1537 res = add_ui_from_string (self, buffer, length, FALSE, error);
1544 * gtk_ui_manager_add_ui:
1545 * @self: a #GtkUIManager
1546 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1548 * @name: the name for the added UI element
1549 * @action: the name of the action to be proxied, or %NULL to add a separator
1550 * @type: the type of UI element to add.
1551 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1552 * is added after its siblings.
1554 * Adds a UI element to the current contents of @self.
1556 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1557 * separator if such an element can be inserted at the place determined by
1558 * @path. Otherwise @type must indicate an element that can be inserted at
1559 * the place determined by @path.
1564 gtk_ui_manager_add_ui (GtkUIManager *self,
1568 const gchar *action,
1569 GtkUIManagerItemType type,
1575 GQuark action_quark = 0;
1577 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1578 g_return_if_fail (merge_id > 0);
1579 g_return_if_fail (name != NULL);
1581 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1586 node_type = NODE_TYPE_UNDECIDED;
1588 switch (NODE_INFO (node)->type)
1590 case NODE_TYPE_MENUBAR:
1591 case NODE_TYPE_MENU:
1592 case NODE_TYPE_POPUP:
1593 case NODE_TYPE_MENU_PLACEHOLDER:
1596 case GTK_UI_MANAGER_AUTO:
1598 node_type = NODE_TYPE_MENUITEM;
1600 node_type = NODE_TYPE_SEPARATOR;
1602 case GTK_UI_MANAGER_MENU:
1603 node_type = NODE_TYPE_MENU;
1605 case GTK_UI_MANAGER_MENUITEM:
1606 node_type = NODE_TYPE_MENUITEM;
1608 case GTK_UI_MANAGER_SEPARATOR:
1609 node_type = NODE_TYPE_SEPARATOR;
1611 case GTK_UI_MANAGER_PLACEHOLDER:
1612 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1618 case NODE_TYPE_TOOLBAR:
1619 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1622 case GTK_UI_MANAGER_AUTO:
1624 node_type = NODE_TYPE_TOOLITEM;
1626 node_type = NODE_TYPE_SEPARATOR;
1628 case GTK_UI_MANAGER_TOOLITEM:
1629 node_type = NODE_TYPE_TOOLITEM;
1631 case GTK_UI_MANAGER_SEPARATOR:
1632 node_type = NODE_TYPE_SEPARATOR;
1634 case GTK_UI_MANAGER_PLACEHOLDER:
1635 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1641 case NODE_TYPE_ROOT:
1644 case GTK_UI_MANAGER_MENUBAR:
1645 node_type = NODE_TYPE_MENUBAR;
1647 case GTK_UI_MANAGER_TOOLBAR:
1648 node_type = NODE_TYPE_TOOLBAR;
1650 case GTK_UI_MANAGER_POPUP:
1651 node_type = NODE_TYPE_POPUP;
1653 case GTK_UI_MANAGER_ACCELERATOR:
1654 node_type = NODE_TYPE_ACCELERATOR;
1664 if (node_type == NODE_TYPE_UNDECIDED)
1667 child = get_child_node (self, node,
1668 name, strlen (name),
1669 node_type, TRUE, top);
1672 action_quark = g_quark_from_string (action);
1674 node_prepend_ui_reference (NODE_INFO (child),
1675 merge_id, action_quark);
1677 if (NODE_INFO (child)->action_name == 0)
1678 NODE_INFO (child)->action_name = action_quark;
1680 NODE_INFO (child)->dirty = TRUE;
1682 queue_update (self);
1684 g_object_notify (G_OBJECT (self), "ui");
1688 remove_ui (GNode *node,
1691 guint merge_id = GPOINTER_TO_UINT (user_data);
1693 node_remove_ui_reference (NODE_INFO (node), merge_id);
1695 return FALSE; /* continue */
1699 * gtk_ui_manager_remove_ui:
1700 * @self: a #GtkUIManager object
1701 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1703 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1708 gtk_ui_manager_remove_ui (GtkUIManager *self,
1711 g_node_traverse (self->private_data->root_node,
1712 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1713 remove_ui, GUINT_TO_POINTER (merge_id));
1715 queue_update (self);
1717 g_object_notify (G_OBJECT (self), "ui");
1720 /* -------------------- Updates -------------------- */
1724 get_action_by_name (GtkUIManager *merge,
1725 const gchar *action_name)
1733 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1735 GtkActionGroup *action_group = tmp->data;
1738 action = gtk_action_group_get_action (action_group, action_name);
1748 find_menu_position (GNode *node,
1749 GtkWidget **menushell_p,
1752 GtkWidget *menushell;
1755 g_return_val_if_fail (node != NULL, FALSE);
1756 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1757 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1758 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1759 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1760 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1763 /* first sibling -- look at parent */
1764 if (node->prev == NULL)
1769 parent = node->parent;
1770 switch (NODE_INFO (parent)->type)
1772 case NODE_TYPE_MENUBAR:
1773 case NODE_TYPE_POPUP:
1774 menushell = NODE_INFO (parent)->proxy;
1777 case NODE_TYPE_MENU:
1778 menushell = NODE_INFO (parent)->proxy;
1779 if (GTK_IS_MENU_ITEM (menushell))
1780 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1781 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1782 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1786 g_list_free (siblings);
1788 case NODE_TYPE_MENU_PLACEHOLDER:
1789 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1790 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1791 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1792 NODE_INFO (parent)->proxy) + 1;
1795 g_warning("%s: bad parent node type %d", G_STRLOC,
1796 NODE_INFO (parent)->type);
1802 GtkWidget *prev_child;
1805 sibling = node->prev;
1806 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1807 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1809 prev_child = NODE_INFO (sibling)->proxy;
1811 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1812 menushell = gtk_widget_get_parent (prev_child);
1813 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1815 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1819 *menushell_p = menushell;
1827 find_toolbar_position (GNode *node,
1828 GtkWidget **toolbar_p,
1834 g_return_val_if_fail (node != NULL, FALSE);
1835 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1836 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1837 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1838 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1841 /* first sibling -- look at parent */
1842 if (node->prev == NULL)
1846 parent = node->parent;
1847 switch (NODE_INFO (parent)->type)
1849 case NODE_TYPE_TOOLBAR:
1850 toolbar = NODE_INFO (parent)->proxy;
1853 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1854 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1855 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1856 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1857 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1860 g_warning ("%s: bad parent node type %d", G_STRLOC,
1861 NODE_INFO (parent)->type);
1867 GtkWidget *prev_child;
1870 sibling = node->prev;
1871 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1872 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1874 prev_child = NODE_INFO (sibling)->proxy;
1876 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1877 toolbar = gtk_widget_get_parent (prev_child);
1878 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1880 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1881 GTK_TOOL_ITEM (prev_child)) + 1;
1885 *toolbar_p = toolbar;
1893 * _gtk_menu_is_empty:
1894 * @menu: a #GtkMenu or %NULL
1896 * Determines whether @menu is empty. A menu is considered empty if it
1897 * the only visible children are tearoff menu items or "filler" menu
1898 * items which were inserted to mark the menu as empty.
1900 * This function is used by #GtkAction.
1902 * Return value: whether @menu is empty.
1905 _gtk_menu_is_empty (GtkWidget *menu)
1907 GList *children, *cur;
1909 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1914 children = gtk_container_get_children (GTK_CONTAINER (menu));
1919 if (GTK_WIDGET_VISIBLE (cur->data))
1921 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1922 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1927 g_list_free (children);
1933 SEPARATOR_MODE_SMART,
1934 SEPARATOR_MODE_VISIBLE,
1935 SEPARATOR_MODE_HIDDEN
1938 void _gtk_action_sync_menu_visible (GtkAction *action,
1943 update_smart_separators (GtkWidget *proxy)
1945 GtkWidget *parent = NULL;
1947 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1949 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1950 parent = gtk_widget_get_parent (proxy);
1957 GList *children, *cur, *last;
1961 children = gtk_container_get_children (GTK_CONTAINER (parent));
1972 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
1975 if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
1976 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
1979 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
1980 "gtk-separator-mode"));
1983 case SEPARATOR_MODE_VISIBLE:
1984 gtk_widget_show (GTK_WIDGET (cur->data));
1988 case SEPARATOR_MODE_HIDDEN:
1989 gtk_widget_hide (GTK_WIDGET (cur->data));
1991 case SEPARATOR_MODE_SMART:
1994 gtk_widget_show (GTK_WIDGET (cur->data));
1999 gtk_widget_hide (GTK_WIDGET (cur->data));
2003 else if (GTK_WIDGET_VISIBLE (cur->data))
2006 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2019 gtk_widget_hide (GTK_WIDGET (last->data));
2021 if (GTK_IS_MENU (parent))
2025 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2026 if (GTK_IS_MENU_ITEM (item))
2027 _gtk_action_sync_menu_visible (NULL, item, empty);
2028 if (GTK_IS_WIDGET (filler))
2029 g_object_set (G_OBJECT (filler), "visible", empty, NULL);
2032 g_list_free (children);
2037 update_node (GtkUIManager *self,
2045 #ifdef DEBUG_UI_MANAGER
2049 g_return_if_fail (node != NULL);
2050 g_return_if_fail (NODE_INFO (node) != NULL);
2052 info = NODE_INFO (node);
2054 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2056 #ifdef DEBUG_UI_MANAGER
2057 g_print ("update_node name=%s dirty=%d popup %d (",
2058 info->name, info->dirty, in_popup);
2059 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2061 NodeUIReference *ref = tmp->data;
2062 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2071 const gchar *action_name;
2072 NodeUIReference *ref;
2074 if (info->uifiles == NULL) {
2075 /* We may need to remove this node.
2076 * This must be done in post order
2078 goto recurse_children;
2081 ref = info->uifiles->data;
2082 action_name = g_quark_to_string (ref->action_quark);
2083 action = get_action_by_name (self, action_name);
2085 info->dirty = FALSE;
2087 /* Check if the node doesn't have an action and must have an action */
2088 if (action == NULL &&
2089 info->type != NODE_TYPE_ROOT &&
2090 info->type != NODE_TYPE_MENUBAR &&
2091 info->type != NODE_TYPE_TOOLBAR &&
2092 info->type != NODE_TYPE_POPUP &&
2093 info->type != NODE_TYPE_SEPARATOR &&
2094 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2095 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2097 g_warning ("%s: missing action", info->name);
2099 goto recurse_children;
2103 gtk_action_set_accel_group (action, self->private_data->accel_group);
2105 /* If the widget already has a proxy and the action hasn't changed, then
2106 * we only have to update the tearoff menu items.
2108 if (info->proxy != NULL && action == info->action)
2110 if (info->type == NODE_TYPE_MENU)
2115 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2116 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2117 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2118 g_object_set (G_OBJECT (siblings->data),
2119 "visible", self->private_data->add_tearoffs && !in_popup,
2121 g_list_free (siblings);
2124 goto recurse_children;
2129 case NODE_TYPE_MENUBAR:
2130 if (info->proxy == NULL)
2132 info->proxy = gtk_menu_bar_new ();
2133 g_object_ref (info->proxy);
2134 gtk_object_sink (GTK_OBJECT (info->proxy));
2135 gtk_widget_set_name (info->proxy, info->name);
2136 gtk_widget_show (info->proxy);
2137 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2140 case NODE_TYPE_POPUP:
2141 if (info->proxy == NULL)
2143 info->proxy = gtk_menu_new ();
2144 g_object_ref (info->proxy);
2145 gtk_object_sink (GTK_OBJECT (info->proxy));
2147 gtk_widget_set_name (info->proxy, info->name);
2149 case NODE_TYPE_MENU:
2151 GtkWidget *prev_submenu = NULL;
2154 /* remove the proxy if it is of the wrong type ... */
2156 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2158 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2161 g_object_ref (prev_submenu);
2162 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2164 gtk_action_disconnect_proxy (info->action, info->proxy);
2165 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2167 g_object_unref (info->proxy);
2170 /* create proxy if needed ... */
2171 if (info->proxy == NULL)
2173 GtkWidget *menushell;
2176 if (find_menu_position (node, &menushell, &pos))
2181 info->proxy = gtk_action_create_menu_item (action);
2182 g_object_ref (info->proxy);
2183 gtk_object_sink (GTK_OBJECT (info->proxy));
2184 menu = gtk_menu_new ();
2185 gtk_widget_set_name (info->proxy, info->name);
2186 gtk_widget_set_name (menu, info->name);
2187 tearoff = gtk_tearoff_menu_item_new ();
2188 gtk_widget_set_no_show_all (tearoff, TRUE);
2189 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2190 filler = gtk_menu_item_new_with_label (_("Empty"));
2191 g_object_set_data (G_OBJECT (filler),
2192 "gtk-empty-menu-item",
2193 GINT_TO_POINTER (TRUE));
2194 gtk_widget_set_sensitive (filler, FALSE);
2195 gtk_widget_set_no_show_all (filler, TRUE);
2196 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2197 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2198 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2199 g_signal_connect (info->proxy, "notify::visible",
2200 G_CALLBACK (update_smart_separators), NULL);
2204 gtk_action_connect_proxy (action, info->proxy);
2208 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2210 g_object_unref (prev_submenu);
2212 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2213 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2214 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2215 g_object_set (G_OBJECT (siblings->data),
2216 "visible", self->private_data->add_tearoffs && !in_popup,
2218 g_list_free (siblings);
2221 case NODE_TYPE_UNDECIDED:
2222 g_warning ("found undecided node!");
2224 case NODE_TYPE_ROOT:
2226 case NODE_TYPE_TOOLBAR:
2227 if (info->proxy == NULL)
2229 info->proxy = gtk_toolbar_new ();
2230 g_object_ref (info->proxy);
2231 gtk_object_sink (GTK_OBJECT (info->proxy));
2232 gtk_widget_set_name (info->proxy, info->name);
2233 gtk_widget_show (info->proxy);
2234 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2237 case NODE_TYPE_MENU_PLACEHOLDER:
2238 /* create menu items for placeholders if necessary ... */
2239 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2240 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2244 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2246 g_object_unref (info->proxy);
2251 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2253 g_object_unref (info->extra);
2257 if (info->proxy == NULL)
2259 GtkWidget *menushell;
2262 if (find_menu_position (node, &menushell, &pos))
2264 info->proxy = gtk_separator_menu_item_new ();
2265 g_object_ref (info->proxy);
2266 gtk_object_sink (GTK_OBJECT (info->proxy));
2267 g_object_set_data (G_OBJECT (info->proxy),
2268 "gtk-separator-mode",
2269 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2270 gtk_widget_set_no_show_all (info->proxy, TRUE);
2271 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2272 NODE_INFO (node)->proxy, pos);
2274 info->extra = gtk_separator_menu_item_new ();
2275 g_object_ref (info->extra);
2276 gtk_object_sink (GTK_OBJECT (info->extra));
2277 g_object_set_data (G_OBJECT (info->extra),
2278 "gtk-separator-mode",
2279 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2280 gtk_widget_set_no_show_all (info->extra, TRUE);
2281 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2282 NODE_INFO (node)->extra, pos+1);
2286 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2287 /* create toolbar items for placeholders if necessary ... */
2288 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2289 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2293 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2295 g_object_unref (info->proxy);
2300 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2302 g_object_unref (info->extra);
2306 if (info->proxy == NULL)
2311 if (find_toolbar_position (node, &toolbar, &pos))
2315 item = gtk_separator_tool_item_new ();
2316 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2317 info->proxy = GTK_WIDGET (item);
2318 g_object_ref (info->proxy);
2319 gtk_object_sink (GTK_OBJECT (info->proxy));
2320 g_object_set_data (G_OBJECT (info->proxy),
2321 "gtk-separator-mode",
2322 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2323 gtk_widget_set_no_show_all (info->proxy, TRUE);
2325 item = gtk_separator_tool_item_new ();
2326 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2327 info->extra = GTK_WIDGET (item);
2328 g_object_ref (info->extra);
2329 gtk_object_sink (GTK_OBJECT (info->extra));
2330 g_object_set_data (G_OBJECT (info->extra),
2331 "gtk-separator-mode",
2332 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2333 gtk_widget_set_no_show_all (info->extra, TRUE);
2337 case NODE_TYPE_MENUITEM:
2338 /* remove the proxy if it is of the wrong type ... */
2340 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2342 g_signal_handlers_disconnect_by_func (info->proxy,
2343 G_CALLBACK (update_smart_separators),
2345 gtk_action_disconnect_proxy (info->action, info->proxy);
2346 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2348 g_object_unref (info->proxy);
2351 /* create proxy if needed ... */
2352 if (info->proxy == NULL)
2354 GtkWidget *menushell;
2357 if (find_menu_position (node, &menushell, &pos))
2359 info->proxy = gtk_action_create_menu_item (action);
2360 g_object_ref (info->proxy);
2361 gtk_object_sink (GTK_OBJECT (info->proxy));
2362 gtk_widget_set_name (info->proxy, info->name);
2364 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2370 g_signal_handlers_disconnect_by_func (info->proxy,
2371 G_CALLBACK (update_smart_separators),
2373 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2374 gtk_action_connect_proxy (action, info->proxy);
2376 g_signal_connect (info->proxy, "notify::visible",
2377 G_CALLBACK (update_smart_separators), NULL);
2380 /* don't show accels in popups */
2381 GtkWidget *label = GTK_BIN (info->proxy)->child;
2382 g_object_set (G_OBJECT (label),
2383 "accel_closure", NULL,
2388 case NODE_TYPE_TOOLITEM:
2389 /* remove the proxy if it is of the wrong type ... */
2391 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2393 g_signal_handlers_disconnect_by_func (info->proxy,
2394 G_CALLBACK (update_smart_separators),
2396 gtk_action_disconnect_proxy (info->action, info->proxy);
2397 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2399 g_object_unref (info->proxy);
2402 /* create proxy if needed ... */
2403 if (info->proxy == NULL)
2408 if (find_toolbar_position (node, &toolbar, &pos))
2410 info->proxy = gtk_action_create_tool_item (action);
2411 g_object_ref (info->proxy);
2412 gtk_object_sink (GTK_OBJECT (info->proxy));
2413 gtk_widget_set_name (info->proxy, info->name);
2415 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2416 GTK_TOOL_ITEM (info->proxy), pos);
2418 /* FIXME: we must trigger the notify::tooltip handler, since
2419 * tooltips on toolitems can't be set before the toolitem
2420 * is added to the toolbar.
2422 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2423 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2429 g_signal_handlers_disconnect_by_func (info->proxy,
2430 G_CALLBACK (update_smart_separators),
2432 gtk_action_connect_proxy (action, info->proxy);
2434 g_signal_connect (info->proxy, "notify::visible",
2435 G_CALLBACK (update_smart_separators), NULL);
2437 case NODE_TYPE_SEPARATOR:
2438 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2439 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2444 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2446 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2448 g_object_unref (info->proxy);
2452 if (find_toolbar_position (node, &toolbar, &pos))
2454 GtkToolItem *item = gtk_separator_tool_item_new ();
2455 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2456 info->proxy = GTK_WIDGET (item);
2457 g_object_ref (info->proxy);
2458 gtk_object_sink (GTK_OBJECT (info->proxy));
2459 gtk_widget_set_no_show_all (info->proxy, TRUE);
2460 g_object_set_data (G_OBJECT (info->proxy),
2461 "gtk-separator-mode",
2462 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2463 gtk_widget_show (info->proxy);
2468 GtkWidget *menushell;
2471 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2473 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2475 g_object_unref (info->proxy);
2479 if (find_menu_position (node, &menushell, &pos))
2481 info->proxy = gtk_separator_menu_item_new ();
2482 g_object_ref (info->proxy);
2483 gtk_object_sink (GTK_OBJECT (info->proxy));
2484 gtk_widget_set_no_show_all (info->proxy, TRUE);
2485 g_object_set_data (G_OBJECT (info->proxy),
2486 "gtk-separator-mode",
2487 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2488 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2490 gtk_widget_show (info->proxy);
2494 case NODE_TYPE_ACCELERATOR:
2495 gtk_action_connect_accelerator (action);
2500 g_object_ref (action);
2502 g_object_unref (info->action);
2503 info->action = action;
2507 /* process children */
2508 child = node->children;
2514 child = current->next;
2515 update_node (self, current, in_popup);
2520 if (info->type == NODE_TYPE_MENU)
2521 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2522 else if (info->type == NODE_TYPE_TOOLBAR || info->type == NODE_TYPE_POPUP)
2523 update_smart_separators (info->proxy);
2526 /* handle cleanup of dead nodes */
2527 if (node->children == NULL && info->uifiles == NULL)
2530 gtk_widget_destroy (info->proxy);
2532 gtk_widget_destroy (info->extra);
2533 if (info->type == NODE_TYPE_ACCELERATOR)
2534 gtk_action_disconnect_accelerator (info->action);
2536 g_node_destroy (node);
2541 do_updates (GtkUIManager *self)
2543 /* this function needs to check through the tree for dirty nodes.
2544 * For such nodes, it needs to do the following:
2546 * 1) check if they are referenced by any loaded UI files anymore.
2547 * In which case, the proxy widget should be destroyed, unless
2548 * there are any subnodes.
2550 * 2) lookup the action for this node again. If it is different to
2551 * the current one (or if no previous action has been looked up),
2552 * the proxy is reconnected to the new action (or a new proxy widget
2553 * is created and added to the parent container).
2555 update_node (self, self->private_data->root_node, FALSE);
2557 self->private_data->update_tag = 0;
2563 do_updates_idle (GtkUIManager *self)
2565 GDK_THREADS_ENTER ();
2567 GDK_THREADS_LEAVE ();
2573 queue_update (GtkUIManager *self)
2575 if (self->private_data->update_tag != 0)
2578 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates_idle,
2584 * gtk_ui_manager_ensure_update:
2585 * @self: a #GtkUIManager
2587 * Makes sure that all pending updates to the UI have been completed.
2589 * This may occasionally be necessary, since #GtkUIManager updates the
2590 * UI in an idle function. A typical example where this function is
2591 * useful is to enforce that the menubar and toolbar have been added to
2592 * the main window before showing it:
2595 * gtk_container_add (GTK_CONTAINER (window), vbox);
2596 * g_signal_connect (merge, "add_widget",
2597 * G_CALLBACK (add_widget), vbox);
2598 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2599 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2600 * gtk_ui_manager_ensure_update (merge);
2601 * gtk_widget_show (window);
2603 * </informalexample>
2608 gtk_ui_manager_ensure_update (GtkUIManager *self)
2610 if (self->private_data->update_tag != 0)
2612 g_source_remove (self->private_data->update_tag);
2618 dirty_traverse_func (GNode *node,
2621 NODE_INFO (node)->dirty = TRUE;
2626 dirty_all_nodes (GtkUIManager *self)
2628 g_node_traverse (self->private_data->root_node,
2629 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2630 dirty_traverse_func, NULL);
2631 queue_update (self);
2634 static const gchar *open_tag_format[] = {
2649 static const gchar *close_tag_format[] = {
2650 "%*s</UNDECIDED>\n",
2655 "%*s</placeholder>\n",
2656 "%*s</placeholder>\n",
2665 print_node (GtkUIManager *self,
2675 g_string_append_printf (buffer, open_tag_format[mnode->type],
2678 if (mnode->type != NODE_TYPE_ROOT)
2681 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2683 if (mnode->action_name)
2684 g_string_append_printf (buffer, " action=\"%s\"",
2685 g_quark_to_string (mnode->action_name));
2688 g_string_append (buffer,
2689 close_tag_format[mnode->type] ? ">\n" : "/>\n");
2691 for (child = node->children; child != NULL; child = child->next)
2692 print_node (self, child, indent_level + 2, buffer);
2694 if (close_tag_format[mnode->type])
2695 g_string_append_printf (buffer, close_tag_format[mnode->type],
2700 * gtk_ui_manager_get_ui:
2701 * @self: a #GtkUIManager
2703 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2705 * Return value: A newly allocated string containing an XML representation of
2711 gtk_ui_manager_get_ui (GtkUIManager *self)
2715 buffer = g_string_new (NULL);
2717 gtk_ui_manager_ensure_update (self);
2719 print_node (self, self->private_data->root_node, 0, buffer);
2721 return g_string_free (buffer, FALSE);