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 "gtkmenutoolbutton.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtkseparatortoolitem.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtktoolbar.h"
44 #include "gtkuimanager.h"
45 #include "gtkprivate.h"
48 #undef DEBUG_UI_MANAGER
57 NODE_TYPE_MENU_PLACEHOLDER,
58 NODE_TYPE_TOOLBAR_PLACEHOLDER,
66 typedef struct _Node Node;
76 GtkWidget *extra; /* second separator for placeholders */
81 guint expand : 1; /* used for separators */
84 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
86 struct _GtkUIManagerPrivate
88 GtkAccelGroup *accel_group;
97 gboolean add_tearoffs;
100 #define NODE_INFO(node) ((Node *)node->data)
102 typedef struct _NodeUIReference NodeUIReference;
104 struct _NodeUIReference
110 static void gtk_ui_manager_class_init (GtkUIManagerClass *class);
111 static void gtk_ui_manager_init (GtkUIManager *self);
112 static void gtk_ui_manager_finalize (GObject *object);
113 static void gtk_ui_manager_set_property (GObject *object,
117 static void gtk_ui_manager_get_property (GObject *object,
121 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
123 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
125 static void queue_update (GtkUIManager *self);
126 static void dirty_all_nodes (GtkUIManager *self);
127 static void mark_node_dirty (GNode *node);
128 static GNode * get_child_node (GtkUIManager *self,
130 const gchar *childname,
131 gint childname_length,
135 static GNode * get_node (GtkUIManager *self,
139 static gboolean free_node (GNode *node);
140 static void node_prepend_ui_reference (GNode *node,
142 GQuark action_quark);
143 static void node_remove_ui_reference (GNode *node,
165 static GObjectClass *parent_class = NULL;
166 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
168 static GMemChunk *merge_node_chunk = NULL;
171 gtk_ui_manager_get_type (void)
173 static GtkType type = 0;
177 static const GTypeInfo type_info =
179 sizeof (GtkUIManagerClass),
180 (GBaseInitFunc) NULL,
181 (GBaseFinalizeFunc) NULL,
182 (GClassInitFunc) gtk_ui_manager_class_init,
183 (GClassFinalizeFunc) NULL,
186 sizeof (GtkUIManager),
188 (GInstanceInitFunc) gtk_ui_manager_init,
191 type = g_type_register_static (G_TYPE_OBJECT,
199 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
201 GObjectClass *gobject_class;
203 parent_class = g_type_class_peek_parent (klass);
205 gobject_class = G_OBJECT_CLASS (klass);
207 if (!merge_node_chunk)
208 merge_node_chunk = g_mem_chunk_create (Node, 64,
211 gobject_class->finalize = gtk_ui_manager_finalize;
212 gobject_class->set_property = gtk_ui_manager_set_property;
213 gobject_class->get_property = gtk_ui_manager_get_property;
214 klass->get_widget = gtk_ui_manager_real_get_widget;
215 klass->get_action = gtk_ui_manager_real_get_action;
218 * GtkUIManager:add-tearoffs:
220 * The "add-tearoffs" property controls whether generated menus
221 * have tearoff menu items.
223 * Note that this only affects regular menus. Generated popup
224 * menus never have tearoff menu items.
228 g_object_class_install_property (gobject_class,
230 g_param_spec_boolean ("add-tearoffs",
231 P_("Add tearoffs to menus"),
232 P_("Whether tearoff menu items should be added to menus"),
234 GTK_PARAM_READWRITE));
236 g_object_class_install_property (gobject_class,
238 g_param_spec_string ("ui",
239 P_("Merged UI definition"),
240 P_("An XML string describing the merged UI"),
242 GTK_PARAM_READABLE));
246 * GtkUIManager::add-widget:
247 * @merge: a #GtkUIManager
248 * @widget: the added widget
250 * The add_widget signal is emitted for each generated menubar and toolbar.
251 * It is not emitted for generated popup menus, which can be obtained by
252 * gtk_ui_manager_get_widget().
256 ui_manager_signals[ADD_WIDGET] =
257 g_signal_new ("add_widget",
258 G_OBJECT_CLASS_TYPE (klass),
259 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
260 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
262 g_cclosure_marshal_VOID__OBJECT,
267 * GtkUIManager::actions-changed:
268 * @merge: a #GtkUIManager
270 * The "actions-changed" signal is emitted whenever the set of actions
275 ui_manager_signals[ACTIONS_CHANGED] =
276 g_signal_new ("actions_changed",
277 G_OBJECT_CLASS_TYPE (klass),
278 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
279 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
281 g_cclosure_marshal_VOID__VOID,
285 * GtkUIManager::connect-proxy:
286 * @uimanager: the ui manager
287 * @action: the action
290 * The connect_proxy signal is emitted after connecting a proxy to
291 * an action in the group.
293 * This is intended for simple customizations for which a custom action
294 * class would be too clumsy, e.g. showing tooltips for menuitems in the
299 ui_manager_signals[CONNECT_PROXY] =
300 g_signal_new ("connect_proxy",
301 G_OBJECT_CLASS_TYPE (klass),
302 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
303 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
305 _gtk_marshal_VOID__OBJECT_OBJECT,
311 * GtkUIManager::disconnect-proxy:
312 * @uimanager: the ui manager
313 * @action: the action
316 * The disconnect_proxy signal is emitted after disconnecting a proxy
317 * from an action in the group.
321 ui_manager_signals[DISCONNECT_PROXY] =
322 g_signal_new ("disconnect_proxy",
323 G_OBJECT_CLASS_TYPE (klass),
324 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
325 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
327 _gtk_marshal_VOID__OBJECT_OBJECT,
333 * GtkUIManager::pre-activate:
334 * @uimanager: the ui manager
335 * @action: the action
337 * The pre_activate signal is emitted just before the @action
340 * This is intended for applications to get notification
341 * just before any action is activated.
345 ui_manager_signals[PRE_ACTIVATE] =
346 g_signal_new ("pre_activate",
347 G_OBJECT_CLASS_TYPE (klass),
348 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
349 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
351 _gtk_marshal_VOID__OBJECT,
356 * GtkUIManager::post-activate:
357 * @uimanager: the ui manager
358 * @action: the action
360 * The post_activate signal is emitted just after the @action
363 * This is intended for applications to get notification
364 * just after any action is activated.
368 ui_manager_signals[POST_ACTIVATE] =
369 g_signal_new ("post_activate",
370 G_OBJECT_CLASS_TYPE (klass),
371 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
372 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
374 _gtk_marshal_VOID__OBJECT,
378 klass->add_widget = NULL;
379 klass->actions_changed = NULL;
380 klass->connect_proxy = NULL;
381 klass->disconnect_proxy = NULL;
382 klass->pre_activate = NULL;
383 klass->post_activate = NULL;
385 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
390 gtk_ui_manager_init (GtkUIManager *self)
395 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
397 self->private_data->accel_group = gtk_accel_group_new ();
399 self->private_data->root_node = NULL;
400 self->private_data->action_groups = NULL;
402 self->private_data->last_merge_id = 0;
403 self->private_data->add_tearoffs = FALSE;
405 merge_id = gtk_ui_manager_new_merge_id (self);
406 node = get_child_node (self, NULL, "ui", 2,
407 NODE_TYPE_ROOT, TRUE, FALSE);
408 node_prepend_ui_reference (node, merge_id, 0);
412 gtk_ui_manager_finalize (GObject *object)
414 GtkUIManager *self = GTK_UI_MANAGER (object);
416 if (self->private_data->update_tag != 0)
418 g_source_remove (self->private_data->update_tag);
419 self->private_data->update_tag = 0;
422 g_node_traverse (self->private_data->root_node,
423 G_POST_ORDER, G_TRAVERSE_ALL, -1,
424 (GNodeTraverseFunc)free_node, NULL);
425 g_node_destroy (self->private_data->root_node);
426 self->private_data->root_node = NULL;
428 g_list_foreach (self->private_data->action_groups,
429 (GFunc) g_object_unref, NULL);
430 g_list_free (self->private_data->action_groups);
431 self->private_data->action_groups = NULL;
433 g_object_unref (self->private_data->accel_group);
434 self->private_data->accel_group = NULL;
436 G_OBJECT_CLASS (parent_class)->finalize (object);
440 gtk_ui_manager_set_property (GObject *object,
445 GtkUIManager *self = GTK_UI_MANAGER (object);
449 case PROP_ADD_TEAROFFS:
450 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459 gtk_ui_manager_get_property (GObject *object,
464 GtkUIManager *self = GTK_UI_MANAGER (object);
468 case PROP_ADD_TEAROFFS:
469 g_value_set_boolean (value, self->private_data->add_tearoffs);
472 g_value_set_string (value, gtk_ui_manager_get_ui (self));
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 gtk_ui_manager_real_get_widget (GtkUIManager *self,
486 /* ensure that there are no pending updates before we get the
488 gtk_ui_manager_ensure_update (self);
490 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
495 return NODE_INFO (node)->proxy;
499 gtk_ui_manager_real_get_action (GtkUIManager *self,
504 /* ensure that there are no pending updates before we get
506 gtk_ui_manager_ensure_update (self);
508 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
513 return NODE_INFO (node)->action;
518 * gtk_ui_manager_new:
520 * Creates a new ui manager object.
522 * Return value: a new ui manager object.
527 gtk_ui_manager_new (void)
529 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
534 * gtk_ui_manager_get_add_tearoffs:
535 * @self: a #GtkUIManager
537 * Returns whether menus generated by this #GtkUIManager
538 * will have tearoff menu items.
540 * Return value: whether tearoff menu items are added
545 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
547 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
549 return self->private_data->add_tearoffs;
554 * gtk_ui_manager_set_add_tearoffs:
555 * @self: a #GtkUIManager
556 * @add_tearoffs: whether tearoff menu items are added
558 * Sets the "add_tearoffs" property, which controls whether menus
559 * generated by this #GtkUIManager will have tearoff menu items.
561 * Note that this only affects regular menus. Generated popup
562 * menus never have tearoff menu items.
567 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
568 gboolean add_tearoffs)
570 g_return_if_fail (GTK_IS_UI_MANAGER (self));
572 add_tearoffs = add_tearoffs != FALSE;
574 if (add_tearoffs != self->private_data->add_tearoffs)
576 self->private_data->add_tearoffs = add_tearoffs;
578 dirty_all_nodes (self);
580 g_object_notify (G_OBJECT (self), "add-tearoffs");
585 cb_proxy_connect_proxy (GtkActionGroup *group,
590 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
594 cb_proxy_disconnect_proxy (GtkActionGroup *group,
599 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
603 cb_proxy_pre_activate (GtkActionGroup *group,
607 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
611 cb_proxy_post_activate (GtkActionGroup *group,
615 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
619 * gtk_ui_manager_insert_action_group:
620 * @self: a #GtkUIManager object
621 * @action_group: the action group to be inserted
622 * @pos: the position at which the group will be inserted.
624 * Inserts an action group into the list of action groups associated
625 * with @self. Actions in earlier groups hide actions with the same
626 * name in later groups.
631 gtk_ui_manager_insert_action_group (GtkUIManager *self,
632 GtkActionGroup *action_group,
635 g_return_if_fail (GTK_IS_UI_MANAGER (self));
636 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
637 g_return_if_fail (g_list_find (self->private_data->action_groups,
638 action_group) == NULL);
640 g_object_ref (action_group);
641 self->private_data->action_groups =
642 g_list_insert (self->private_data->action_groups, action_group, pos);
643 g_object_connect (action_group,
644 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
645 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
646 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
647 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
650 /* dirty all nodes, as action bindings may change */
651 dirty_all_nodes (self);
653 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
657 * gtk_ui_manager_remove_action_group:
658 * @self: a #GtkUIManager object
659 * @action_group: the action group to be removed
661 * Removes an action group from the list of action groups associated
667 gtk_ui_manager_remove_action_group (GtkUIManager *self,
668 GtkActionGroup *action_group)
670 g_return_if_fail (GTK_IS_UI_MANAGER (self));
671 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
672 g_return_if_fail (g_list_find (self->private_data->action_groups,
673 action_group) != NULL);
675 self->private_data->action_groups =
676 g_list_remove (self->private_data->action_groups, action_group);
678 g_object_disconnect (action_group,
679 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
680 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
681 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
682 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
684 g_object_unref (action_group);
686 /* dirty all nodes, as action bindings may change */
687 dirty_all_nodes (self);
689 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
693 * gtk_ui_manager_get_action_groups:
694 * @self: a #GtkUIManager object
696 * Returns the list of action groups associated with @self.
698 * Return value: a #GList of action groups. The list is owned by GTK+
699 * and should not be modified.
704 gtk_ui_manager_get_action_groups (GtkUIManager *self)
706 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
708 return self->private_data->action_groups;
712 * gtk_ui_manager_get_accel_group:
713 * @self: a #GtkUIManager object
715 * Returns the #GtkAccelGroup associated with @self.
717 * Return value: the #GtkAccelGroup.
722 gtk_ui_manager_get_accel_group (GtkUIManager *self)
724 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
726 return self->private_data->accel_group;
730 * gtk_ui_manager_get_widget:
731 * @self: a #GtkUIManager
734 * Looks up a widget by following a path.
735 * The path consists of the names specified in the XML description of the UI.
736 * separated by '/'. Elements which don't have a name or action attribute in
737 * the XML (e.g. <popup>) can be addressed by their XML element name
738 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
740 * Note that the widget found by following a path that ends in a <menu>
741 * element is the menuitem to which the menu is attached, not the menu itself.
743 * Also note that the widgets constructed by a ui manager are not tied to
744 * the lifecycle of the ui manager. If you add the widgets returned by this
745 * function to some container or explicitly ref them, they will survive the
746 * destruction of the ui manager.
748 * Return value: the widget found by following the path, or %NULL if no widget
754 gtk_ui_manager_get_widget (GtkUIManager *self,
757 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
758 g_return_val_if_fail (path != NULL, NULL);
760 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
764 collect_toplevels (GNode *node,
768 GtkUIManagerItemType types;
772 switch (NODE_INFO (node)->type) {
773 case NODE_TYPE_MENUBAR:
774 if (data->types & GTK_UI_MANAGER_MENUBAR)
775 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
777 case NODE_TYPE_TOOLBAR:
778 if (data->types & GTK_UI_MANAGER_TOOLBAR)
779 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
781 case NODE_TYPE_POPUP:
782 if (data->types & GTK_UI_MANAGER_POPUP)
783 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
790 * gtk_ui_manager_get_toplevels:
791 * @self: a #GtkUIManager
792 * @types: specifies the types of toplevel widgets to include. Allowed
793 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
794 * #GTK_UI_MANAGER_POPUP.
796 * Obtains a list of all toplevel widgets of the requested types.
798 * Return value: a newly-allocated of all toplevel widgets of the requested
804 gtk_ui_manager_get_toplevels (GtkUIManager *self,
805 GtkUIManagerItemType types)
808 GtkUIManagerItemType types;
812 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
813 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
814 GTK_UI_MANAGER_TOOLBAR |
815 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
821 g_node_children_foreach (self->private_data->root_node,
823 collect_toplevels, &data);
830 * gtk_ui_manager_get_action:
831 * @self: a #GtkUIManager
834 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
835 * for more information about paths.
837 * Return value: the action whose proxy widget is found by following the path,
838 * or %NULL if no widget was found.
843 gtk_ui_manager_get_action (GtkUIManager *self,
846 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
847 g_return_val_if_fail (path != NULL, NULL);
849 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
853 get_child_node (GtkUIManager *self,
855 const gchar *childname,
856 gint childname_length,
867 for (child = parent->children; child != NULL; child = child->next)
869 if (NODE_INFO (child)->name &&
870 strlen (NODE_INFO (child)->name) == childname_length &&
871 !strncmp (NODE_INFO (child)->name, childname, childname_length))
873 /* if undecided about node type, set it */
874 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
875 NODE_INFO (child)->type = node_type;
877 /* warn about type mismatch */
878 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
879 node_type != NODE_TYPE_UNDECIDED &&
880 NODE_INFO (child)->type != node_type)
881 g_warning ("node type doesn't match %d (%s is type %d)",
883 NODE_INFO (child)->name,
884 NODE_INFO (child)->type);
890 if (!child && create)
894 mnode = g_chunk_new0 (Node, merge_node_chunk);
895 mnode->type = node_type;
896 mnode->name = g_strndup (childname, childname_length);
899 child = g_node_prepend_data (parent, mnode);
901 child = g_node_append_data (parent, mnode);
903 mark_node_dirty (child);
908 /* handle root node */
909 if (self->private_data->root_node)
911 child = self->private_data->root_node;
912 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
913 g_warning ("root node name '%s' doesn't match '%s'",
914 childname, NODE_INFO (child)->name);
915 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
916 g_warning ("base element must be of type ROOT");
922 mnode = g_chunk_new0 (Node, merge_node_chunk);
923 mnode->type = node_type;
924 mnode->name = g_strndup (childname, childname_length);
927 child = self->private_data->root_node = g_node_new (mnode);
935 get_node (GtkUIManager *self,
940 const gchar *pos, *end;
941 GNode *parent, *node;
943 if (strncmp ("/ui", path, 3) == 0)
946 end = path + strlen (path);
948 parent = node = NULL;
954 slash = strchr (pos, '/');
956 length = slash - pos;
958 length = strlen (pos);
960 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
965 pos += length + 1; /* move past the node name and the slash too */
969 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
970 NODE_INFO (node)->type = node_type;
976 free_node (GNode *node)
978 Node *info = NODE_INFO (node);
980 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
981 g_list_free (info->uifiles);
984 g_object_unref (info->action);
986 g_object_unref (info->proxy);
988 g_object_unref (info->extra);
990 g_chunk_free (info, merge_node_chunk);
996 * gtk_ui_manager_new_merge_id:
997 * @self: a #GtkUIManager
999 * Returns an unused merge id, suitable for use with
1000 * gtk_ui_manager_add_ui().
1002 * Return value: an unused merge id.
1007 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1009 self->private_data->last_merge_id++;
1011 return self->private_data->last_merge_id;
1015 node_prepend_ui_reference (GNode *gnode,
1017 GQuark action_quark)
1019 Node *node = NODE_INFO (gnode);
1020 NodeUIReference *reference = NULL;
1022 if (node->uifiles &&
1023 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1024 reference = node->uifiles->data;
1027 reference = g_new (NodeUIReference, 1);
1028 node->uifiles = g_list_prepend (node->uifiles, reference);
1031 reference->merge_id = merge_id;
1032 reference->action_quark = action_quark;
1034 mark_node_dirty (gnode);
1038 node_remove_ui_reference (GNode *gnode,
1041 Node *node = NODE_INFO (gnode);
1044 for (p = node->uifiles; p != NULL; p = p->next)
1046 NodeUIReference *reference = p->data;
1048 if (reference->merge_id == merge_id)
1050 if (p == node->uifiles)
1051 mark_node_dirty (gnode);
1052 node->uifiles = g_list_delete_link (node->uifiles, p);
1060 /* -------------------- The UI file parser -------------------- */
1074 typedef struct _ParseContext ParseContext;
1075 struct _ParseContext
1078 ParseState prev_state;
1088 start_element_handler (GMarkupParseContext *context,
1089 const gchar *element_name,
1090 const gchar **attribute_names,
1091 const gchar **attribute_values,
1095 ParseContext *ctx = user_data;
1096 GtkUIManager *self = ctx->self;
1099 const gchar *node_name;
1100 const gchar *action;
1101 GQuark action_quark;
1103 gboolean expand = FALSE;
1105 gboolean raise_error = TRUE;
1112 for (i = 0; attribute_names[i] != NULL; i++)
1114 if (!strcmp (attribute_names[i], "name"))
1116 node_name = attribute_values[i];
1118 else if (!strcmp (attribute_names[i], "action"))
1120 action = attribute_values[i];
1121 action_quark = g_quark_from_string (attribute_values[i]);
1123 else if (!strcmp (attribute_names[i], "position"))
1125 top = !strcmp (attribute_values[i], "top");
1127 else if (!strcmp (attribute_names[i], "expand"))
1129 expand = !strcmp (attribute_values[i], "true");
1133 gint line_number, char_number;
1135 g_markup_parse_context_get_position (context,
1136 &line_number, &char_number);
1139 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1140 _("Unknown attribute '%s' on line %d char %d"),
1142 line_number, char_number);
1147 /* Work out a name for this node. Either the name attribute, or
1148 * the action, or the element name */
1149 if (node_name == NULL)
1154 node_name = element_name;
1157 switch (element_name[0])
1160 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1162 ctx->state = STATE_ACCELERATOR;
1163 ctx->current = get_child_node (self, ctx->current,
1164 node_name, strlen (node_name),
1165 NODE_TYPE_ACCELERATOR,
1167 if (NODE_INFO (ctx->current)->action_name == 0)
1168 NODE_INFO (ctx->current)->action_name = action_quark;
1170 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1172 raise_error = FALSE;
1176 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1178 ctx->state = STATE_ROOT;
1179 ctx->current = self->private_data->root_node;
1180 raise_error = FALSE;
1182 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1186 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1188 ctx->state = STATE_MENU;
1189 ctx->current = get_child_node (self, ctx->current,
1190 node_name, strlen (node_name),
1193 if (NODE_INFO (ctx->current)->action_name == 0)
1194 NODE_INFO (ctx->current)->action_name = action_quark;
1196 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1197 mark_node_dirty (ctx->current);
1199 raise_error = FALSE;
1201 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1203 ctx->current = get_child_node (self, ctx->current,
1204 node_name, strlen (node_name),
1207 if (NODE_INFO (ctx->current)->action_name == 0)
1208 NODE_INFO (ctx->current)->action_name = action_quark;
1210 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1212 raise_error = FALSE;
1214 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1216 ctx->state = STATE_MENU;
1218 ctx->current = get_child_node (self, g_node_last_child (ctx->current),
1219 node_name, strlen (node_name),
1222 if (NODE_INFO (ctx->current)->action_name == 0)
1223 NODE_INFO (ctx->current)->action_name = action_quark;
1225 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1227 raise_error = FALSE;
1229 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1233 ctx->state = STATE_MENUITEM;
1234 node = get_child_node (self, ctx->current,
1235 node_name, strlen (node_name),
1238 if (NODE_INFO (node)->action_name == 0)
1239 NODE_INFO (node)->action_name = action_quark;
1241 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1243 raise_error = FALSE;
1247 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1249 ctx->state = STATE_MENU;
1250 ctx->current = get_child_node (self, ctx->current,
1251 node_name, strlen (node_name),
1254 if (NODE_INFO (ctx->current)->action_name == 0)
1255 NODE_INFO (ctx->current)->action_name = action_quark;
1257 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1259 raise_error = FALSE;
1261 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1262 !strcmp (element_name, "placeholder"))
1264 if (ctx->state == STATE_TOOLBAR)
1265 ctx->current = get_child_node (self, ctx->current,
1266 node_name, strlen (node_name),
1267 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1270 ctx->current = get_child_node (self, ctx->current,
1271 node_name, strlen (node_name),
1272 NODE_TYPE_MENU_PLACEHOLDER,
1275 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1277 raise_error = FALSE;
1281 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1282 !strcmp (element_name, "separator"))
1287 if (ctx->state == STATE_TOOLBAR)
1288 ctx->state = STATE_TOOLITEM;
1290 ctx->state = STATE_MENUITEM;
1291 if (!strcmp (node_name, "separator"))
1297 length = strlen (node_name);
1298 node = get_child_node (self, ctx->current,
1300 NODE_TYPE_SEPARATOR,
1303 NODE_INFO (node)->expand = expand;
1305 if (NODE_INFO (node)->action_name == 0)
1306 NODE_INFO (node)->action_name = action_quark;
1308 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1310 raise_error = FALSE;
1314 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1316 ctx->state = STATE_TOOLBAR;
1317 ctx->current = get_child_node (self, ctx->current,
1318 node_name, strlen (node_name),
1321 if (NODE_INFO (ctx->current)->action_name == 0)
1322 NODE_INFO (ctx->current)->action_name = action_quark;
1324 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1326 raise_error = FALSE;
1328 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1332 ctx->state = STATE_TOOLITEM;
1333 node = get_child_node (self, ctx->current,
1334 node_name, strlen (node_name),
1337 if (NODE_INFO (node)->action_name == 0)
1338 NODE_INFO (node)->action_name = action_quark;
1340 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1342 raise_error = FALSE;
1350 gint line_number, char_number;
1352 g_markup_parse_context_get_position (context,
1353 &line_number, &char_number);
1356 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1357 _("Unexpected start tag '%s' on line %d char %d"),
1359 line_number, char_number);
1364 end_element_handler (GMarkupParseContext *context,
1365 const gchar *element_name,
1369 ParseContext *ctx = user_data;
1375 /* no need to GError here, GMarkup already catches this */
1378 ctx->current = NULL;
1379 ctx->state = STATE_END;
1383 case STATE_ACCELERATOR:
1384 ctx->current = ctx->current->parent;
1385 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1386 ctx->state = STATE_ROOT;
1387 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1389 ctx->current = ctx->current->parent;
1390 ctx->state = STATE_TOOLITEM;
1392 /* else, stay in same state */
1394 case STATE_MENUITEM:
1395 ctx->state = STATE_MENU;
1397 case STATE_TOOLITEM:
1398 ctx->state = STATE_TOOLBAR;
1404 cleanup (GMarkupParseContext *context,
1408 ParseContext *ctx = user_data;
1410 ctx->current = NULL;
1411 /* should also walk through the tree and get rid of nodes related to
1412 * this UI file's tag */
1414 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1418 xml_isspace (char c)
1420 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1424 text_handler (GMarkupParseContext *context,
1434 end = text + text_len;
1435 while (p != end && xml_isspace (*p))
1440 gint line_number, char_number;
1442 g_markup_parse_context_get_position (context,
1443 &line_number, &char_number);
1446 G_MARKUP_ERROR_INVALID_CONTENT,
1447 _("Unexpected character data on line %d char %d"),
1448 line_number, char_number);
1453 static GMarkupParser ui_parser = {
1454 start_element_handler,
1455 end_element_handler,
1462 add_ui_from_string (GtkUIManager *self,
1463 const gchar *buffer,
1465 gboolean needs_root,
1468 ParseContext ctx = { 0 };
1469 GMarkupParseContext *context;
1471 ctx.state = STATE_START;
1474 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1476 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1479 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1482 if (!g_markup_parse_context_parse (context, buffer, length, error))
1486 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1489 if (!g_markup_parse_context_end_parse (context, error))
1492 g_markup_parse_context_free (context);
1494 queue_update (self);
1496 g_object_notify (G_OBJECT (self), "ui");
1498 return ctx.merge_id;
1502 g_markup_parse_context_free (context);
1508 * gtk_ui_manager_add_ui_from_string:
1509 * @self: a #GtkUIManager object
1510 * @buffer: the string to parse
1511 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1512 * @error: return location for an error
1514 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1515 * merges it with the current contents of @self. An enclosing <ui>
1516 * element is added if it is missing.
1518 * Return value: The merge id for the merged UI. The merge id can be used
1519 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1520 * the return value is 0.
1525 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1526 const gchar *buffer,
1530 gboolean needs_root = TRUE;
1534 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1535 g_return_val_if_fail (buffer != NULL, 0);
1538 length = strlen (buffer);
1541 end = buffer + length;
1542 while (p != end && xml_isspace (*p))
1545 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1548 return add_ui_from_string (self, buffer, length, needs_root, error);
1552 * gtk_ui_manager_add_ui_from_file:
1553 * @self: a #GtkUIManager object
1554 * @filename: the name of the file to parse
1555 * @error: return location for an error
1557 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1558 * merges it with the current contents of @self.
1560 * Return value: The merge id for the merged UI. The merge id can be used
1561 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1562 * the return value is 0.
1567 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1568 const gchar *filename,
1575 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1577 if (!g_file_get_contents (filename, &buffer, &length, error))
1580 res = add_ui_from_string (self, buffer, length, FALSE, error);
1587 * gtk_ui_manager_add_ui:
1588 * @self: a #GtkUIManager
1589 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1591 * @name: the name for the added UI element
1592 * @action: the name of the action to be proxied, or %NULL to add a separator
1593 * @type: the type of UI element to add.
1594 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1595 * is added after its siblings.
1597 * Adds a UI element to the current contents of @self.
1599 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1600 * separator if such an element can be inserted at the place determined by
1601 * @path. Otherwise @type must indicate an element that can be inserted at
1602 * the place determined by @path.
1607 gtk_ui_manager_add_ui (GtkUIManager *self,
1611 const gchar *action,
1612 GtkUIManagerItemType type,
1618 GQuark action_quark = 0;
1620 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1621 g_return_if_fail (merge_id > 0);
1622 g_return_if_fail (name != NULL);
1624 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1629 node_type = NODE_TYPE_UNDECIDED;
1631 switch (NODE_INFO (node)->type)
1633 case NODE_TYPE_MENUBAR:
1634 case NODE_TYPE_MENU:
1635 case NODE_TYPE_POPUP:
1636 case NODE_TYPE_MENU_PLACEHOLDER:
1639 case GTK_UI_MANAGER_AUTO:
1641 node_type = NODE_TYPE_MENUITEM;
1643 node_type = NODE_TYPE_SEPARATOR;
1645 case GTK_UI_MANAGER_MENU:
1646 node_type = NODE_TYPE_MENU;
1648 case GTK_UI_MANAGER_MENUITEM:
1649 node_type = NODE_TYPE_MENUITEM;
1651 case GTK_UI_MANAGER_SEPARATOR:
1652 node_type = NODE_TYPE_SEPARATOR;
1654 case GTK_UI_MANAGER_PLACEHOLDER:
1655 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1661 case NODE_TYPE_TOOLBAR:
1662 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1665 case GTK_UI_MANAGER_AUTO:
1667 node_type = NODE_TYPE_TOOLITEM;
1669 node_type = NODE_TYPE_SEPARATOR;
1671 case GTK_UI_MANAGER_TOOLITEM:
1672 node_type = NODE_TYPE_TOOLITEM;
1674 case GTK_UI_MANAGER_SEPARATOR:
1675 node_type = NODE_TYPE_SEPARATOR;
1677 case GTK_UI_MANAGER_PLACEHOLDER:
1678 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1684 case NODE_TYPE_ROOT:
1687 case GTK_UI_MANAGER_MENUBAR:
1688 node_type = NODE_TYPE_MENUBAR;
1690 case GTK_UI_MANAGER_TOOLBAR:
1691 node_type = NODE_TYPE_TOOLBAR;
1693 case GTK_UI_MANAGER_POPUP:
1694 node_type = NODE_TYPE_POPUP;
1696 case GTK_UI_MANAGER_ACCELERATOR:
1697 node_type = NODE_TYPE_ACCELERATOR;
1707 if (node_type == NODE_TYPE_UNDECIDED)
1710 child = get_child_node (self, node,
1711 name, strlen (name),
1712 node_type, TRUE, top);
1715 action_quark = g_quark_from_string (action);
1717 node_prepend_ui_reference (child, merge_id, action_quark);
1719 if (NODE_INFO (child)->action_name == 0)
1720 NODE_INFO (child)->action_name = action_quark;
1722 queue_update (self);
1724 g_object_notify (G_OBJECT (self), "ui");
1728 remove_ui (GNode *node,
1731 guint merge_id = GPOINTER_TO_UINT (user_data);
1733 node_remove_ui_reference (node, merge_id);
1735 return FALSE; /* continue */
1739 * gtk_ui_manager_remove_ui:
1740 * @self: a #GtkUIManager object
1741 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1743 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1748 gtk_ui_manager_remove_ui (GtkUIManager *self,
1751 g_node_traverse (self->private_data->root_node,
1752 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1753 remove_ui, GUINT_TO_POINTER (merge_id));
1755 queue_update (self);
1757 g_object_notify (G_OBJECT (self), "ui");
1760 /* -------------------- Updates -------------------- */
1764 get_action_by_name (GtkUIManager *merge,
1765 const gchar *action_name)
1773 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1775 GtkActionGroup *action_group = tmp->data;
1778 action = gtk_action_group_get_action (action_group, action_name);
1788 find_menu_position (GNode *node,
1789 GtkWidget **menushell_p,
1792 GtkWidget *menushell;
1795 g_return_val_if_fail (node != NULL, FALSE);
1796 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1797 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1798 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1799 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1800 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1803 /* first sibling -- look at parent */
1804 if (node->prev == NULL)
1809 parent = node->parent;
1810 switch (NODE_INFO (parent)->type)
1812 case NODE_TYPE_MENUBAR:
1813 case NODE_TYPE_POPUP:
1814 menushell = NODE_INFO (parent)->proxy;
1817 case NODE_TYPE_MENU:
1818 menushell = NODE_INFO (parent)->proxy;
1819 if (GTK_IS_MENU_ITEM (menushell))
1820 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1821 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1822 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1826 g_list_free (siblings);
1828 case NODE_TYPE_MENU_PLACEHOLDER:
1829 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1830 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1831 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1832 NODE_INFO (parent)->proxy) + 1;
1835 g_warning("%s: bad parent node type %d", G_STRLOC,
1836 NODE_INFO (parent)->type);
1842 GtkWidget *prev_child;
1845 sibling = node->prev;
1846 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1847 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1849 prev_child = NODE_INFO (sibling)->proxy;
1851 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1852 menushell = gtk_widget_get_parent (prev_child);
1853 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1855 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1859 *menushell_p = menushell;
1867 find_toolbar_position (GNode *node,
1868 GtkWidget **toolbar_p,
1874 g_return_val_if_fail (node != NULL, FALSE);
1875 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1876 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1877 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1878 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1881 /* first sibling -- look at parent */
1882 if (node->prev == NULL)
1886 parent = node->parent;
1887 switch (NODE_INFO (parent)->type)
1889 case NODE_TYPE_TOOLBAR:
1890 toolbar = NODE_INFO (parent)->proxy;
1893 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1894 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1895 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1896 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1897 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1900 g_warning ("%s: bad parent node type %d", G_STRLOC,
1901 NODE_INFO (parent)->type);
1907 GtkWidget *prev_child;
1910 sibling = node->prev;
1911 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1912 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1914 prev_child = NODE_INFO (sibling)->proxy;
1916 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1917 toolbar = gtk_widget_get_parent (prev_child);
1918 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1920 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1921 GTK_TOOL_ITEM (prev_child)) + 1;
1925 *toolbar_p = toolbar;
1933 * _gtk_menu_is_empty:
1934 * @menu: a #GtkMenu or %NULL
1936 * Determines whether @menu is empty. A menu is considered empty if it
1937 * the only visible children are tearoff menu items or "filler" menu
1938 * items which were inserted to mark the menu as empty.
1940 * This function is used by #GtkAction.
1942 * Return value: whether @menu is empty.
1945 _gtk_menu_is_empty (GtkWidget *menu)
1947 GList *children, *cur;
1949 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1954 children = gtk_container_get_children (GTK_CONTAINER (menu));
1959 if (GTK_WIDGET_VISIBLE (cur->data))
1961 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1962 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1967 g_list_free (children);
1973 SEPARATOR_MODE_SMART,
1974 SEPARATOR_MODE_VISIBLE,
1975 SEPARATOR_MODE_HIDDEN
1978 void _gtk_action_sync_menu_visible (GtkAction *action,
1983 update_smart_separators (GtkWidget *proxy)
1985 GtkWidget *parent = NULL;
1987 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1989 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1990 parent = gtk_widget_get_parent (proxy);
1996 GList *children, *cur, *last;
2000 children = gtk_container_get_children (GTK_CONTAINER (parent));
2011 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2015 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2016 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2019 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2020 "gtk-separator-mode"));
2023 case SEPARATOR_MODE_VISIBLE:
2024 gtk_widget_show (GTK_WIDGET (cur->data));
2028 case SEPARATOR_MODE_HIDDEN:
2029 gtk_widget_hide (GTK_WIDGET (cur->data));
2031 case SEPARATOR_MODE_SMART:
2034 gtk_widget_show (GTK_WIDGET (cur->data));
2039 gtk_widget_hide (GTK_WIDGET (cur->data));
2043 else if (GTK_WIDGET_VISIBLE (cur->data))
2046 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2059 gtk_widget_hide (GTK_WIDGET (last->data));
2061 if (GTK_IS_MENU (parent))
2065 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2066 if (GTK_IS_MENU_ITEM (item))
2067 _gtk_action_sync_menu_visible (NULL, item, empty);
2068 if (GTK_IS_WIDGET (filler))
2071 gtk_widget_show (filler);
2073 gtk_widget_hide (filler);
2077 g_list_free (children);
2082 update_node (GtkUIManager *self,
2089 const gchar *action_name;
2090 NodeUIReference *ref;
2092 #ifdef DEBUG_UI_MANAGER
2096 g_return_if_fail (node != NULL);
2097 g_return_if_fail (NODE_INFO (node) != NULL);
2099 info = NODE_INFO (node);
2104 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2106 #ifdef DEBUG_UI_MANAGER
2107 g_print ("update_node name=%s dirty=%d popup %d (",
2108 info->name, info->dirty, in_popup);
2109 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2111 NodeUIReference *ref = tmp->data;
2112 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2119 if (info->uifiles == NULL) {
2120 /* We may need to remove this node.
2121 * This must be done in post order
2123 goto recurse_children;
2126 ref = info->uifiles->data;
2127 action_name = g_quark_to_string (ref->action_quark);
2128 action = get_action_by_name (self, action_name);
2130 info->dirty = FALSE;
2132 /* Check if the node doesn't have an action and must have an action */
2133 if (action == NULL &&
2134 info->type != NODE_TYPE_ROOT &&
2135 info->type != NODE_TYPE_MENUBAR &&
2136 info->type != NODE_TYPE_TOOLBAR &&
2137 info->type != NODE_TYPE_POPUP &&
2138 info->type != NODE_TYPE_SEPARATOR &&
2139 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2140 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2142 g_warning ("%s: missing action", info->name);
2144 goto recurse_children;
2148 gtk_action_set_accel_group (action, self->private_data->accel_group);
2150 /* If the widget already has a proxy and the action hasn't changed, then
2151 * we only have to update the tearoff menu items.
2153 if (info->proxy != NULL && action == info->action)
2155 if (info->type == NODE_TYPE_MENU)
2160 if (GTK_IS_MENU (info->proxy))
2163 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2164 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2165 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2167 if (self->private_data->add_tearoffs && !in_popup)
2168 gtk_widget_show (GTK_WIDGET (siblings->data));
2170 gtk_widget_hide (GTK_WIDGET (siblings->data));
2172 g_list_free (siblings);
2175 goto recurse_children;
2180 case NODE_TYPE_MENUBAR:
2181 if (info->proxy == NULL)
2183 info->proxy = gtk_menu_bar_new ();
2184 g_object_ref (info->proxy);
2185 gtk_object_sink (GTK_OBJECT (info->proxy));
2186 gtk_widget_set_name (info->proxy, info->name);
2187 gtk_widget_show (info->proxy);
2188 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2191 case NODE_TYPE_POPUP:
2192 if (info->proxy == NULL)
2194 info->proxy = gtk_menu_new ();
2195 g_object_ref (info->proxy);
2196 gtk_object_sink (GTK_OBJECT (info->proxy));
2198 gtk_widget_set_name (info->proxy, info->name);
2200 case NODE_TYPE_MENU:
2202 GtkWidget *prev_submenu = NULL;
2205 /* remove the proxy if it is of the wrong type ... */
2207 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2209 if (GTK_IS_MENU_ITEM (info->proxy))
2211 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2214 g_object_ref (prev_submenu);
2215 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2219 gtk_action_disconnect_proxy (info->action, info->proxy);
2220 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2222 g_object_unref (info->proxy);
2225 /* create proxy if needed ... */
2226 if (info->proxy == NULL)
2228 GtkWidget *menushell;
2231 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM ||
2232 find_menu_position (node, &menushell, &pos))
2237 menu = gtk_menu_new ();
2238 gtk_widget_set_name (menu, info->name);
2239 tearoff = gtk_tearoff_menu_item_new ();
2240 gtk_widget_set_no_show_all (tearoff, TRUE);
2241 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2242 filler = gtk_menu_item_new_with_label (_("Empty"));
2243 g_object_set_data (G_OBJECT (filler),
2244 "gtk-empty-menu-item",
2245 GINT_TO_POINTER (TRUE));
2246 gtk_widget_set_sensitive (filler, FALSE);
2247 gtk_widget_set_no_show_all (filler, TRUE);
2248 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2250 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2253 g_object_ref (info->proxy);
2254 gtk_object_sink (GTK_OBJECT (info->proxy));
2255 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2260 info->proxy = gtk_action_create_menu_item (action);
2261 g_object_ref (info->proxy);
2262 gtk_object_sink (GTK_OBJECT (info->proxy));
2263 g_signal_connect (info->proxy, "notify::visible",
2264 G_CALLBACK (update_smart_separators), NULL);
2265 gtk_widget_set_name (info->proxy, info->name);
2267 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2268 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2273 gtk_action_connect_proxy (action, info->proxy);
2277 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2279 g_object_unref (prev_submenu);
2282 if (GTK_IS_MENU (info->proxy))
2285 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2286 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2287 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2289 if (self->private_data->add_tearoffs && !in_popup)
2290 gtk_widget_show (GTK_WIDGET (siblings->data));
2292 gtk_widget_hide (GTK_WIDGET (siblings->data));
2294 g_list_free (siblings);
2297 case NODE_TYPE_UNDECIDED:
2298 g_warning ("found undecided node!");
2300 case NODE_TYPE_ROOT:
2302 case NODE_TYPE_TOOLBAR:
2303 if (info->proxy == NULL)
2305 info->proxy = gtk_toolbar_new ();
2306 g_object_ref (info->proxy);
2307 gtk_object_sink (GTK_OBJECT (info->proxy));
2308 gtk_widget_set_name (info->proxy, info->name);
2309 gtk_widget_show (info->proxy);
2310 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2313 case NODE_TYPE_MENU_PLACEHOLDER:
2314 /* create menu items for placeholders if necessary ... */
2315 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2316 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2320 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2322 g_object_unref (info->proxy);
2327 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2329 g_object_unref (info->extra);
2333 if (info->proxy == NULL)
2335 GtkWidget *menushell;
2338 if (find_menu_position (node, &menushell, &pos))
2340 info->proxy = gtk_separator_menu_item_new ();
2341 g_object_ref (info->proxy);
2342 gtk_object_sink (GTK_OBJECT (info->proxy));
2343 g_object_set_data (G_OBJECT (info->proxy),
2344 "gtk-separator-mode",
2345 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2346 gtk_widget_set_no_show_all (info->proxy, TRUE);
2347 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2348 NODE_INFO (node)->proxy, pos);
2350 info->extra = gtk_separator_menu_item_new ();
2351 g_object_ref (info->extra);
2352 gtk_object_sink (GTK_OBJECT (info->extra));
2353 g_object_set_data (G_OBJECT (info->extra),
2354 "gtk-separator-mode",
2355 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2356 gtk_widget_set_no_show_all (info->extra, TRUE);
2357 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2358 NODE_INFO (node)->extra, pos+1);
2362 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2363 /* create toolbar items for placeholders if necessary ... */
2364 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2365 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2369 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2371 g_object_unref (info->proxy);
2376 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2378 g_object_unref (info->extra);
2382 if (info->proxy == NULL)
2387 if (find_toolbar_position (node, &toolbar, &pos))
2391 item = gtk_separator_tool_item_new ();
2392 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2393 info->proxy = GTK_WIDGET (item);
2394 g_object_ref (info->proxy);
2395 gtk_object_sink (GTK_OBJECT (info->proxy));
2396 g_object_set_data (G_OBJECT (info->proxy),
2397 "gtk-separator-mode",
2398 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2399 gtk_widget_set_no_show_all (info->proxy, TRUE);
2401 item = gtk_separator_tool_item_new ();
2402 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2403 info->extra = GTK_WIDGET (item);
2404 g_object_ref (info->extra);
2405 gtk_object_sink (GTK_OBJECT (info->extra));
2406 g_object_set_data (G_OBJECT (info->extra),
2407 "gtk-separator-mode",
2408 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2409 gtk_widget_set_no_show_all (info->extra, TRUE);
2413 case NODE_TYPE_MENUITEM:
2414 /* remove the proxy if it is of the wrong type ... */
2416 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2418 g_signal_handlers_disconnect_by_func (info->proxy,
2419 G_CALLBACK (update_smart_separators),
2421 gtk_action_disconnect_proxy (info->action, info->proxy);
2422 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2424 g_object_unref (info->proxy);
2427 /* create proxy if needed ... */
2428 if (info->proxy == NULL)
2430 GtkWidget *menushell;
2433 if (find_menu_position (node, &menushell, &pos))
2435 info->proxy = gtk_action_create_menu_item (action);
2436 g_object_ref (info->proxy);
2437 gtk_object_sink (GTK_OBJECT (info->proxy));
2438 gtk_widget_set_name (info->proxy, info->name);
2440 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2446 g_signal_handlers_disconnect_by_func (info->proxy,
2447 G_CALLBACK (update_smart_separators),
2449 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2450 gtk_action_connect_proxy (action, info->proxy);
2452 g_signal_connect (info->proxy, "notify::visible",
2453 G_CALLBACK (update_smart_separators), NULL);
2456 /* don't show accels in popups */
2457 GtkWidget *label = GTK_BIN (info->proxy)->child;
2458 g_object_set (label,
2459 "accel_closure", NULL,
2464 case NODE_TYPE_TOOLITEM:
2465 /* remove the proxy if it is of the wrong type ... */
2467 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2469 g_signal_handlers_disconnect_by_func (info->proxy,
2470 G_CALLBACK (update_smart_separators),
2472 gtk_action_disconnect_proxy (info->action, info->proxy);
2473 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2475 g_object_unref (info->proxy);
2478 /* create proxy if needed ... */
2479 if (info->proxy == NULL)
2484 if (find_toolbar_position (node, &toolbar, &pos))
2486 info->proxy = gtk_action_create_tool_item (action);
2487 g_object_ref (info->proxy);
2488 gtk_object_sink (GTK_OBJECT (info->proxy));
2489 gtk_widget_set_name (info->proxy, info->name);
2491 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2492 GTK_TOOL_ITEM (info->proxy), pos);
2494 /* FIXME: we must trigger the notify::tooltip handler, since
2495 * tooltips on toolitems can't be set before the toolitem
2496 * is added to the toolbar.
2498 g_object_notify (G_OBJECT (action), "tooltip");
2503 g_signal_handlers_disconnect_by_func (info->proxy,
2504 G_CALLBACK (update_smart_separators),
2506 gtk_action_connect_proxy (action, info->proxy);
2508 g_signal_connect (info->proxy, "notify::visible",
2509 G_CALLBACK (update_smart_separators), NULL);
2511 case NODE_TYPE_SEPARATOR:
2512 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2513 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2517 gint separator_mode;
2519 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2521 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2523 g_object_unref (info->proxy);
2527 if (find_toolbar_position (node, &toolbar, &pos))
2529 GtkToolItem *item = gtk_separator_tool_item_new ();
2530 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2531 info->proxy = GTK_WIDGET (item);
2532 g_object_ref (info->proxy);
2533 gtk_object_sink (GTK_OBJECT (info->proxy));
2534 gtk_widget_set_no_show_all (info->proxy, TRUE);
2537 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2538 gtk_separator_tool_item_set_draw
2539 (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2540 separator_mode = SEPARATOR_MODE_VISIBLE;
2543 separator_mode = SEPARATOR_MODE_SMART;
2545 g_object_set_data (G_OBJECT (info->proxy),
2546 "gtk-separator-mode",
2547 GINT_TO_POINTER (separator_mode));
2548 gtk_widget_show (info->proxy);
2553 GtkWidget *menushell;
2556 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2558 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2560 g_object_unref (info->proxy);
2564 if (find_menu_position (node, &menushell, &pos))
2566 info->proxy = gtk_separator_menu_item_new ();
2567 g_object_ref (info->proxy);
2568 gtk_object_sink (GTK_OBJECT (info->proxy));
2569 gtk_widget_set_no_show_all (info->proxy, TRUE);
2570 g_object_set_data (G_OBJECT (info->proxy),
2571 "gtk-separator-mode",
2572 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2573 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2575 gtk_widget_show (info->proxy);
2579 case NODE_TYPE_ACCELERATOR:
2580 gtk_action_connect_accelerator (action);
2585 g_object_ref (action);
2587 g_object_unref (info->action);
2588 info->action = action;
2591 /* process children */
2592 child = node->children;
2598 child = current->next;
2599 update_node (self, current, in_popup);
2604 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2605 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2606 else if (info->type == NODE_TYPE_MENU ||
2607 info->type == NODE_TYPE_TOOLBAR ||
2608 info->type == NODE_TYPE_POPUP)
2609 update_smart_separators (info->proxy);
2612 /* handle cleanup of dead nodes */
2613 if (node->children == NULL && info->uifiles == NULL)
2616 gtk_widget_destroy (info->proxy);
2618 gtk_widget_destroy (info->extra);
2619 if (info->type == NODE_TYPE_ACCELERATOR)
2620 gtk_action_disconnect_accelerator (info->action);
2622 g_node_destroy (node);
2627 do_updates (GtkUIManager *self)
2629 /* this function needs to check through the tree for dirty nodes.
2630 * For such nodes, it needs to do the following:
2632 * 1) check if they are referenced by any loaded UI files anymore.
2633 * In which case, the proxy widget should be destroyed, unless
2634 * there are any subnodes.
2636 * 2) lookup the action for this node again. If it is different to
2637 * the current one (or if no previous action has been looked up),
2638 * the proxy is reconnected to the new action (or a new proxy widget
2639 * is created and added to the parent container).
2641 update_node (self, self->private_data->root_node, FALSE);
2643 self->private_data->update_tag = 0;
2649 do_updates_idle (GtkUIManager *self)
2651 GDK_THREADS_ENTER ();
2653 GDK_THREADS_LEAVE ();
2659 queue_update (GtkUIManager *self)
2661 if (self->private_data->update_tag != 0)
2664 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates_idle,
2670 * gtk_ui_manager_ensure_update:
2671 * @self: a #GtkUIManager
2673 * Makes sure that all pending updates to the UI have been completed.
2675 * This may occasionally be necessary, since #GtkUIManager updates the
2676 * UI in an idle function. A typical example where this function is
2677 * useful is to enforce that the menubar and toolbar have been added to
2678 * the main window before showing it:
2681 * gtk_container_add (GTK_CONTAINER (window), vbox);
2682 * g_signal_connect (merge, "add_widget",
2683 * G_CALLBACK (add_widget), vbox);
2684 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2685 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2686 * gtk_ui_manager_ensure_update (merge);
2687 * gtk_widget_show (window);
2689 * </informalexample>
2694 gtk_ui_manager_ensure_update (GtkUIManager *self)
2696 if (self->private_data->update_tag != 0)
2698 g_source_remove (self->private_data->update_tag);
2704 dirty_traverse_func (GNode *node,
2707 NODE_INFO (node)->dirty = TRUE;
2712 dirty_all_nodes (GtkUIManager *self)
2714 g_node_traverse (self->private_data->root_node,
2715 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2716 dirty_traverse_func, NULL);
2717 queue_update (self);
2721 mark_node_dirty (GNode *node)
2725 /* FIXME could optimize this */
2726 for (p = node; p; p = p->parent)
2727 NODE_INFO (p)->dirty = TRUE;
2730 static const gchar *const open_tag_format[] = {
2745 static const gchar *const close_tag_format[] = {
2746 "%*s</UNDECIDED>\n",
2751 "%*s</placeholder>\n",
2752 "%*s</placeholder>\n",
2761 print_node (GtkUIManager *self,
2771 g_string_append_printf (buffer, open_tag_format[mnode->type],
2774 if (mnode->type != NODE_TYPE_ROOT)
2777 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2779 if (mnode->action_name)
2780 g_string_append_printf (buffer, " action=\"%s\"",
2781 g_quark_to_string (mnode->action_name));
2784 g_string_append (buffer,
2785 close_tag_format[mnode->type] ? ">\n" : "/>\n");
2787 for (child = node->children; child != NULL; child = child->next)
2788 print_node (self, child, indent_level + 2, buffer);
2790 if (close_tag_format[mnode->type])
2791 g_string_append_printf (buffer, close_tag_format[mnode->type],
2796 * gtk_ui_manager_get_ui:
2797 * @self: a #GtkUIManager
2799 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2801 * Return value: A newly allocated string containing an XML representation of
2807 gtk_ui_manager_get_ui (GtkUIManager *self)
2811 buffer = g_string_new (NULL);
2813 gtk_ui_manager_ensure_update (self);
2815 print_node (self, self->private_data->root_node, 0, buffer);
2817 return g_string_free (buffer, FALSE);
2822 #undef gtk_ui_manager_add_ui_from_file
2825 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2826 const gchar *filename,
2829 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2832 if (utf8_filename == NULL)
2835 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2837 g_free (utf8_filename);
2844 #define __GTK_UI_MANAGER_C__
2845 #include "gtkaliasdef.c"