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_finalize (GObject *object);
111 static void gtk_ui_manager_set_property (GObject *object,
115 static void gtk_ui_manager_get_property (GObject *object,
119 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
121 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
123 static void queue_update (GtkUIManager *self);
124 static void dirty_all_nodes (GtkUIManager *self);
125 static void mark_node_dirty (GNode *node);
126 static GNode * get_child_node (GtkUIManager *self,
129 const gchar *childname,
130 gint childname_length,
134 static GNode * get_node (GtkUIManager *self,
138 static gboolean free_node (GNode *node);
139 static void node_prepend_ui_reference (GNode *node,
141 GQuark action_quark);
142 static void node_remove_ui_reference (GNode *node,
164 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
166 G_DEFINE_TYPE (GtkUIManager, gtk_ui_manager, G_TYPE_OBJECT)
169 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
171 GObjectClass *gobject_class;
173 gobject_class = G_OBJECT_CLASS (klass);
175 gobject_class->finalize = gtk_ui_manager_finalize;
176 gobject_class->set_property = gtk_ui_manager_set_property;
177 gobject_class->get_property = gtk_ui_manager_get_property;
178 klass->get_widget = gtk_ui_manager_real_get_widget;
179 klass->get_action = gtk_ui_manager_real_get_action;
182 * GtkUIManager:add-tearoffs:
184 * The "add-tearoffs" property controls whether generated menus
185 * have tearoff menu items.
187 * Note that this only affects regular menus. Generated popup
188 * menus never have tearoff menu items.
192 g_object_class_install_property (gobject_class,
194 g_param_spec_boolean ("add-tearoffs",
195 P_("Add tearoffs to menus"),
196 P_("Whether tearoff menu items should be added to menus"),
198 GTK_PARAM_READWRITE));
200 g_object_class_install_property (gobject_class,
202 g_param_spec_string ("ui",
203 P_("Merged UI definition"),
204 P_("An XML string describing the merged UI"),
206 GTK_PARAM_READABLE));
210 * GtkUIManager::add-widget:
211 * @merge: a #GtkUIManager
212 * @widget: the added widget
214 * The add_widget signal is emitted for each generated menubar and toolbar.
215 * It is not emitted for generated popup menus, which can be obtained by
216 * gtk_ui_manager_get_widget().
220 ui_manager_signals[ADD_WIDGET] =
221 g_signal_new (I_("add_widget"),
222 G_OBJECT_CLASS_TYPE (klass),
223 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
224 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
226 g_cclosure_marshal_VOID__OBJECT,
231 * GtkUIManager::actions-changed:
232 * @merge: a #GtkUIManager
234 * The "actions-changed" signal is emitted whenever the set of actions
239 ui_manager_signals[ACTIONS_CHANGED] =
240 g_signal_new (I_("actions_changed"),
241 G_OBJECT_CLASS_TYPE (klass),
242 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
243 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
245 g_cclosure_marshal_VOID__VOID,
249 * GtkUIManager::connect-proxy:
250 * @uimanager: the ui manager
251 * @action: the action
254 * The connect_proxy signal is emitted after connecting a proxy to
255 * an action in the group.
257 * This is intended for simple customizations for which a custom action
258 * class would be too clumsy, e.g. showing tooltips for menuitems in the
263 ui_manager_signals[CONNECT_PROXY] =
264 g_signal_new (I_("connect_proxy"),
265 G_OBJECT_CLASS_TYPE (klass),
266 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
267 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
269 _gtk_marshal_VOID__OBJECT_OBJECT,
275 * GtkUIManager::disconnect-proxy:
276 * @uimanager: the ui manager
277 * @action: the action
280 * The disconnect_proxy signal is emitted after disconnecting a proxy
281 * from an action in the group.
285 ui_manager_signals[DISCONNECT_PROXY] =
286 g_signal_new (I_("disconnect_proxy"),
287 G_OBJECT_CLASS_TYPE (klass),
288 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
289 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
291 _gtk_marshal_VOID__OBJECT_OBJECT,
297 * GtkUIManager::pre-activate:
298 * @uimanager: the ui manager
299 * @action: the action
301 * The pre_activate signal is emitted just before the @action
304 * This is intended for applications to get notification
305 * just before any action is activated.
309 ui_manager_signals[PRE_ACTIVATE] =
310 g_signal_new (I_("pre_activate"),
311 G_OBJECT_CLASS_TYPE (klass),
312 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
313 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
315 _gtk_marshal_VOID__OBJECT,
320 * GtkUIManager::post-activate:
321 * @uimanager: the ui manager
322 * @action: the action
324 * The post_activate signal is emitted just after the @action
327 * This is intended for applications to get notification
328 * just after any action is activated.
332 ui_manager_signals[POST_ACTIVATE] =
333 g_signal_new (I_("post_activate"),
334 G_OBJECT_CLASS_TYPE (klass),
335 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
336 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
338 _gtk_marshal_VOID__OBJECT,
342 klass->add_widget = NULL;
343 klass->actions_changed = NULL;
344 klass->connect_proxy = NULL;
345 klass->disconnect_proxy = NULL;
346 klass->pre_activate = NULL;
347 klass->post_activate = NULL;
349 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
354 gtk_ui_manager_init (GtkUIManager *self)
359 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
361 self->private_data->accel_group = gtk_accel_group_new ();
363 self->private_data->root_node = NULL;
364 self->private_data->action_groups = NULL;
366 self->private_data->last_merge_id = 0;
367 self->private_data->add_tearoffs = FALSE;
369 merge_id = gtk_ui_manager_new_merge_id (self);
370 node = get_child_node (self, NULL, NULL, "ui", 2,
371 NODE_TYPE_ROOT, TRUE, FALSE);
372 node_prepend_ui_reference (node, merge_id, 0);
376 gtk_ui_manager_finalize (GObject *object)
378 GtkUIManager *self = GTK_UI_MANAGER (object);
380 if (self->private_data->update_tag != 0)
382 g_source_remove (self->private_data->update_tag);
383 self->private_data->update_tag = 0;
386 g_node_traverse (self->private_data->root_node,
387 G_POST_ORDER, G_TRAVERSE_ALL, -1,
388 (GNodeTraverseFunc)free_node, NULL);
389 g_node_destroy (self->private_data->root_node);
390 self->private_data->root_node = NULL;
392 g_list_foreach (self->private_data->action_groups,
393 (GFunc) g_object_unref, NULL);
394 g_list_free (self->private_data->action_groups);
395 self->private_data->action_groups = NULL;
397 g_object_unref (self->private_data->accel_group);
398 self->private_data->accel_group = NULL;
400 G_OBJECT_CLASS (gtk_ui_manager_parent_class)->finalize (object);
404 gtk_ui_manager_set_property (GObject *object,
409 GtkUIManager *self = GTK_UI_MANAGER (object);
413 case PROP_ADD_TEAROFFS:
414 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
417 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
423 gtk_ui_manager_get_property (GObject *object,
428 GtkUIManager *self = GTK_UI_MANAGER (object);
432 case PROP_ADD_TEAROFFS:
433 g_value_set_boolean (value, self->private_data->add_tearoffs);
436 g_value_take_string (value, gtk_ui_manager_get_ui (self));
439 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
445 gtk_ui_manager_real_get_widget (GtkUIManager *self,
450 /* ensure that there are no pending updates before we get the
452 gtk_ui_manager_ensure_update (self);
454 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
459 return NODE_INFO (node)->proxy;
463 gtk_ui_manager_real_get_action (GtkUIManager *self,
468 /* ensure that there are no pending updates before we get
470 gtk_ui_manager_ensure_update (self);
472 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
477 return NODE_INFO (node)->action;
482 * gtk_ui_manager_new:
484 * Creates a new ui manager object.
486 * Return value: a new ui manager object.
491 gtk_ui_manager_new (void)
493 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
498 * gtk_ui_manager_get_add_tearoffs:
499 * @self: a #GtkUIManager
501 * Returns whether menus generated by this #GtkUIManager
502 * will have tearoff menu items.
504 * Return value: whether tearoff menu items are added
509 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
511 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
513 return self->private_data->add_tearoffs;
518 * gtk_ui_manager_set_add_tearoffs:
519 * @self: a #GtkUIManager
520 * @add_tearoffs: whether tearoff menu items are added
522 * Sets the "add_tearoffs" property, which controls whether menus
523 * generated by this #GtkUIManager will have tearoff menu items.
525 * Note that this only affects regular menus. Generated popup
526 * menus never have tearoff menu items.
531 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
532 gboolean add_tearoffs)
534 g_return_if_fail (GTK_IS_UI_MANAGER (self));
536 add_tearoffs = add_tearoffs != FALSE;
538 if (add_tearoffs != self->private_data->add_tearoffs)
540 self->private_data->add_tearoffs = add_tearoffs;
542 dirty_all_nodes (self);
544 g_object_notify (G_OBJECT (self), "add-tearoffs");
549 cb_proxy_connect_proxy (GtkActionGroup *group,
554 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
558 cb_proxy_disconnect_proxy (GtkActionGroup *group,
563 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
567 cb_proxy_pre_activate (GtkActionGroup *group,
571 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
575 cb_proxy_post_activate (GtkActionGroup *group,
579 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
583 * gtk_ui_manager_insert_action_group:
584 * @self: a #GtkUIManager object
585 * @action_group: the action group to be inserted
586 * @pos: the position at which the group will be inserted.
588 * Inserts an action group into the list of action groups associated
589 * with @self. Actions in earlier groups hide actions with the same
590 * name in later groups.
595 gtk_ui_manager_insert_action_group (GtkUIManager *self,
596 GtkActionGroup *action_group,
599 g_return_if_fail (GTK_IS_UI_MANAGER (self));
600 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
601 g_return_if_fail (g_list_find (self->private_data->action_groups,
602 action_group) == NULL);
604 g_object_ref (action_group);
605 self->private_data->action_groups =
606 g_list_insert (self->private_data->action_groups, action_group, pos);
607 g_object_connect (action_group,
608 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
609 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
610 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
611 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
614 /* dirty all nodes, as action bindings may change */
615 dirty_all_nodes (self);
617 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
621 * gtk_ui_manager_remove_action_group:
622 * @self: a #GtkUIManager object
623 * @action_group: the action group to be removed
625 * Removes an action group from the list of action groups associated
631 gtk_ui_manager_remove_action_group (GtkUIManager *self,
632 GtkActionGroup *action_group)
634 g_return_if_fail (GTK_IS_UI_MANAGER (self));
635 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
636 g_return_if_fail (g_list_find (self->private_data->action_groups,
637 action_group) != NULL);
639 self->private_data->action_groups =
640 g_list_remove (self->private_data->action_groups, action_group);
642 g_object_disconnect (action_group,
643 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
644 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
645 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
646 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
648 g_object_unref (action_group);
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_get_action_groups:
658 * @self: a #GtkUIManager object
660 * Returns the list of action groups associated with @self.
662 * Return value: a #GList of action groups. The list is owned by GTK+
663 * and should not be modified.
668 gtk_ui_manager_get_action_groups (GtkUIManager *self)
670 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
672 return self->private_data->action_groups;
676 * gtk_ui_manager_get_accel_group:
677 * @self: a #GtkUIManager object
679 * Returns the #GtkAccelGroup associated with @self.
681 * Return value: the #GtkAccelGroup.
686 gtk_ui_manager_get_accel_group (GtkUIManager *self)
688 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
690 return self->private_data->accel_group;
694 * gtk_ui_manager_get_widget:
695 * @self: a #GtkUIManager
698 * Looks up a widget by following a path.
699 * The path consists of the names specified in the XML description of the UI.
700 * separated by '/'. Elements which don't have a name or action attribute in
701 * the XML (e.g. <popup>) can be addressed by their XML element name
702 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
704 * Note that the widget found by following a path that ends in a <menu>
705 * element is the menuitem to which the menu is attached, not the menu itself.
707 * Also note that the widgets constructed by a ui manager are not tied to
708 * the lifecycle of the ui manager. If you add the widgets returned by this
709 * function to some container or explicitly ref them, they will survive the
710 * destruction of the ui manager.
712 * Return value: the widget found by following the path, or %NULL if no widget
718 gtk_ui_manager_get_widget (GtkUIManager *self,
721 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
722 g_return_val_if_fail (path != NULL, NULL);
724 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
728 GtkUIManagerItemType types;
733 collect_toplevels (GNode *node,
736 ToplevelData *data = user_data;
738 if (NODE_INFO (node)->proxy)
740 switch (NODE_INFO (node)->type)
742 case NODE_TYPE_MENUBAR:
743 if (data->types & GTK_UI_MANAGER_MENUBAR)
744 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
746 case NODE_TYPE_TOOLBAR:
747 if (data->types & GTK_UI_MANAGER_TOOLBAR)
748 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
750 case NODE_TYPE_POPUP:
751 if (data->types & GTK_UI_MANAGER_POPUP)
752 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
760 * gtk_ui_manager_get_toplevels:
761 * @self: a #GtkUIManager
762 * @types: specifies the types of toplevel widgets to include. Allowed
763 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
764 * #GTK_UI_MANAGER_POPUP.
766 * Obtains a list of all toplevel widgets of the requested types.
768 * Return value: a newly-allocated of all toplevel widgets of the requested
774 gtk_ui_manager_get_toplevels (GtkUIManager *self,
775 GtkUIManagerItemType types)
779 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
780 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
781 GTK_UI_MANAGER_TOOLBAR |
782 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
788 g_node_children_foreach (self->private_data->root_node,
790 collect_toplevels, &data);
797 * gtk_ui_manager_get_action:
798 * @self: a #GtkUIManager
801 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
802 * for more information about paths.
804 * Return value: the action whose proxy widget is found by following the path,
805 * or %NULL if no widget was found.
810 gtk_ui_manager_get_action (GtkUIManager *self,
813 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
814 g_return_val_if_fail (path != NULL, NULL);
816 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
820 get_child_node (GtkUIManager *self,
823 const gchar *childname,
824 gint childname_length,
835 for (child = parent->children; child != NULL; child = child->next)
837 if (NODE_INFO (child)->name &&
838 strlen (NODE_INFO (child)->name) == childname_length &&
839 !strncmp (NODE_INFO (child)->name, childname, childname_length))
841 /* if undecided about node type, set it */
842 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
843 NODE_INFO (child)->type = node_type;
845 /* warn about type mismatch */
846 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
847 node_type != NODE_TYPE_UNDECIDED &&
848 NODE_INFO (child)->type != node_type)
849 g_warning ("node type doesn't match %d (%s is type %d)",
851 NODE_INFO (child)->name,
852 NODE_INFO (child)->type);
858 if (!child && create)
862 mnode = g_slice_new0 (Node);
863 mnode->type = node_type;
864 mnode->name = g_strndup (childname, childname_length);
869 child = g_node_insert_before (parent, sibling,
872 child = g_node_insert_after (parent, sibling,
878 child = g_node_prepend_data (parent, mnode);
880 child = g_node_append_data (parent, mnode);
883 mark_node_dirty (child);
888 /* handle root node */
889 if (self->private_data->root_node)
891 child = self->private_data->root_node;
892 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
893 g_warning ("root node name '%s' doesn't match '%s'",
894 childname, NODE_INFO (child)->name);
895 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
896 g_warning ("base element must be of type ROOT");
902 mnode = g_slice_new0 (Node);
903 mnode->type = node_type;
904 mnode->name = g_strndup (childname, childname_length);
907 child = self->private_data->root_node = g_node_new (mnode);
915 get_node (GtkUIManager *self,
920 const gchar *pos, *end;
921 GNode *parent, *node;
923 if (strncmp ("/ui", path, 3) == 0)
926 end = path + strlen (path);
928 parent = node = NULL;
934 slash = strchr (pos, '/');
936 length = slash - pos;
938 length = strlen (pos);
940 node = get_child_node (self, parent, NULL, pos, length, NODE_TYPE_UNDECIDED,
945 pos += length + 1; /* move past the node name and the slash too */
949 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
950 NODE_INFO (node)->type = node_type;
956 node_ui_reference_free (gpointer data)
958 g_slice_free (NodeUIReference, data);
962 free_node (GNode *node)
964 Node *info = NODE_INFO (node);
966 g_list_foreach (info->uifiles, (GFunc) node_ui_reference_free, NULL);
967 g_list_free (info->uifiles);
970 g_object_unref (info->action);
972 g_object_unref (info->proxy);
974 g_object_unref (info->extra);
976 g_slice_free (Node, info);
982 * gtk_ui_manager_new_merge_id:
983 * @self: a #GtkUIManager
985 * Returns an unused merge id, suitable for use with
986 * gtk_ui_manager_add_ui().
988 * Return value: an unused merge id.
993 gtk_ui_manager_new_merge_id (GtkUIManager *self)
995 self->private_data->last_merge_id++;
997 return self->private_data->last_merge_id;
1001 node_prepend_ui_reference (GNode *gnode,
1003 GQuark action_quark)
1005 Node *node = NODE_INFO (gnode);
1006 NodeUIReference *reference = NULL;
1008 if (node->uifiles &&
1009 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1010 reference = node->uifiles->data;
1013 reference = g_slice_new (NodeUIReference);
1014 node->uifiles = g_list_prepend (node->uifiles, reference);
1017 reference->merge_id = merge_id;
1018 reference->action_quark = action_quark;
1020 mark_node_dirty (gnode);
1024 node_remove_ui_reference (GNode *gnode,
1027 Node *node = NODE_INFO (gnode);
1030 for (p = node->uifiles; p != NULL; p = p->next)
1032 NodeUIReference *reference = p->data;
1034 if (reference->merge_id == merge_id)
1036 if (p == node->uifiles)
1037 mark_node_dirty (gnode);
1038 node->uifiles = g_list_delete_link (node->uifiles, p);
1039 g_slice_free (NodeUIReference, reference);
1046 /* -------------------- The UI file parser -------------------- */
1060 typedef struct _ParseContext ParseContext;
1061 struct _ParseContext
1064 ParseState prev_state;
1074 start_element_handler (GMarkupParseContext *context,
1075 const gchar *element_name,
1076 const gchar **attribute_names,
1077 const gchar **attribute_values,
1081 ParseContext *ctx = user_data;
1082 GtkUIManager *self = ctx->self;
1085 const gchar *node_name;
1086 const gchar *action;
1087 GQuark action_quark;
1089 gboolean expand = FALSE;
1091 gboolean raise_error = TRUE;
1098 for (i = 0; attribute_names[i] != NULL; i++)
1100 if (!strcmp (attribute_names[i], "name"))
1102 node_name = attribute_values[i];
1104 else if (!strcmp (attribute_names[i], "action"))
1106 action = attribute_values[i];
1107 action_quark = g_quark_from_string (attribute_values[i]);
1109 else if (!strcmp (attribute_names[i], "position"))
1111 top = !strcmp (attribute_values[i], "top");
1113 else if (!strcmp (attribute_names[i], "expand"))
1115 expand = !strcmp (attribute_values[i], "true");
1119 gint line_number, char_number;
1121 g_markup_parse_context_get_position (context,
1122 &line_number, &char_number);
1125 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1126 _("Unknown attribute '%s' on line %d char %d"),
1128 line_number, char_number);
1133 /* Work out a name for this node. Either the name attribute, or
1134 * the action, or the element name */
1135 if (node_name == NULL)
1140 node_name = element_name;
1143 switch (element_name[0])
1146 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1148 ctx->state = STATE_ACCELERATOR;
1149 ctx->current = get_child_node (self, ctx->current, NULL,
1150 node_name, strlen (node_name),
1151 NODE_TYPE_ACCELERATOR,
1153 if (NODE_INFO (ctx->current)->action_name == 0)
1154 NODE_INFO (ctx->current)->action_name = action_quark;
1156 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1158 raise_error = FALSE;
1162 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1164 ctx->state = STATE_ROOT;
1165 ctx->current = self->private_data->root_node;
1166 raise_error = FALSE;
1168 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1172 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1174 ctx->state = STATE_MENU;
1175 ctx->current = get_child_node (self, ctx->current, NULL,
1176 node_name, strlen (node_name),
1179 if (NODE_INFO (ctx->current)->action_name == 0)
1180 NODE_INFO (ctx->current)->action_name = action_quark;
1182 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1183 mark_node_dirty (ctx->current);
1185 raise_error = FALSE;
1187 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1189 ctx->current = get_child_node (self, ctx->current, NULL,
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);
1198 raise_error = FALSE;
1200 else if (ctx->state == STATE_TOOLITEM && !strcmp (element_name, "menu"))
1202 ctx->state = STATE_MENU;
1204 ctx->current = get_child_node (self, g_node_last_child (ctx->current), NULL,
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 (ctx->current, ctx->merge_id, action_quark);
1213 raise_error = FALSE;
1215 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1219 ctx->state = STATE_MENUITEM;
1220 node = get_child_node (self, ctx->current, NULL,
1221 node_name, strlen (node_name),
1224 if (NODE_INFO (node)->action_name == 0)
1225 NODE_INFO (node)->action_name = action_quark;
1227 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1229 raise_error = FALSE;
1233 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1235 ctx->state = STATE_MENU;
1236 ctx->current = get_child_node (self, ctx->current, NULL,
1237 node_name, strlen (node_name),
1240 if (NODE_INFO (ctx->current)->action_name == 0)
1241 NODE_INFO (ctx->current)->action_name = action_quark;
1243 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1245 raise_error = FALSE;
1247 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1248 !strcmp (element_name, "placeholder"))
1250 if (ctx->state == STATE_TOOLBAR)
1251 ctx->current = get_child_node (self, ctx->current, NULL,
1252 node_name, strlen (node_name),
1253 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1256 ctx->current = get_child_node (self, ctx->current, NULL,
1257 node_name, strlen (node_name),
1258 NODE_TYPE_MENU_PLACEHOLDER,
1261 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1263 raise_error = FALSE;
1267 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1268 !strcmp (element_name, "separator"))
1273 if (ctx->state == STATE_TOOLBAR)
1274 ctx->state = STATE_TOOLITEM;
1276 ctx->state = STATE_MENUITEM;
1277 if (!strcmp (node_name, "separator"))
1283 length = strlen (node_name);
1284 node = get_child_node (self, ctx->current, NULL,
1286 NODE_TYPE_SEPARATOR,
1289 NODE_INFO (node)->expand = expand;
1291 if (NODE_INFO (node)->action_name == 0)
1292 NODE_INFO (node)->action_name = action_quark;
1294 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1296 raise_error = FALSE;
1300 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1302 ctx->state = STATE_TOOLBAR;
1303 ctx->current = get_child_node (self, ctx->current, NULL,
1304 node_name, strlen (node_name),
1307 if (NODE_INFO (ctx->current)->action_name == 0)
1308 NODE_INFO (ctx->current)->action_name = action_quark;
1310 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1312 raise_error = FALSE;
1314 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1318 ctx->state = STATE_TOOLITEM;
1319 node = get_child_node (self, ctx->current, NULL,
1320 node_name, strlen (node_name),
1323 if (NODE_INFO (node)->action_name == 0)
1324 NODE_INFO (node)->action_name = action_quark;
1326 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1328 raise_error = FALSE;
1336 gint line_number, char_number;
1338 g_markup_parse_context_get_position (context,
1339 &line_number, &char_number);
1342 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1343 _("Unexpected start tag '%s' on line %d char %d"),
1345 line_number, char_number);
1350 end_element_handler (GMarkupParseContext *context,
1351 const gchar *element_name,
1355 ParseContext *ctx = user_data;
1361 /* no need to GError here, GMarkup already catches this */
1364 ctx->current = NULL;
1365 ctx->state = STATE_END;
1369 case STATE_ACCELERATOR:
1370 ctx->current = ctx->current->parent;
1371 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1372 ctx->state = STATE_ROOT;
1373 else if (NODE_INFO (ctx->current)->type == NODE_TYPE_TOOLITEM)
1375 ctx->current = ctx->current->parent;
1376 ctx->state = STATE_TOOLITEM;
1378 /* else, stay in same state */
1380 case STATE_MENUITEM:
1381 ctx->state = STATE_MENU;
1383 case STATE_TOOLITEM:
1384 ctx->state = STATE_TOOLBAR;
1390 cleanup (GMarkupParseContext *context,
1394 ParseContext *ctx = user_data;
1396 ctx->current = NULL;
1397 /* should also walk through the tree and get rid of nodes related to
1398 * this UI file's tag */
1400 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1404 xml_isspace (char c)
1406 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1410 text_handler (GMarkupParseContext *context,
1420 end = text + text_len;
1421 while (p != end && xml_isspace (*p))
1426 gint line_number, char_number;
1428 g_markup_parse_context_get_position (context,
1429 &line_number, &char_number);
1432 G_MARKUP_ERROR_INVALID_CONTENT,
1433 _("Unexpected character data on line %d char %d"),
1434 line_number, char_number);
1439 static const GMarkupParser ui_parser = {
1440 start_element_handler,
1441 end_element_handler,
1448 add_ui_from_string (GtkUIManager *self,
1449 const gchar *buffer,
1451 gboolean needs_root,
1454 ParseContext ctx = { 0 };
1455 GMarkupParseContext *context;
1457 ctx.state = STATE_START;
1460 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1462 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1465 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1468 if (!g_markup_parse_context_parse (context, buffer, length, error))
1472 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1475 if (!g_markup_parse_context_end_parse (context, error))
1478 g_markup_parse_context_free (context);
1480 queue_update (self);
1482 g_object_notify (G_OBJECT (self), "ui");
1484 return ctx.merge_id;
1488 g_markup_parse_context_free (context);
1494 * gtk_ui_manager_add_ui_from_string:
1495 * @self: a #GtkUIManager object
1496 * @buffer: the string to parse
1497 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1498 * @error: return location for an error
1500 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1501 * merges it with the current contents of @self. An enclosing <ui>
1502 * element is added if it is missing.
1504 * Return value: The merge id for the merged UI. The merge id can be used
1505 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1506 * the return value is 0.
1511 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1512 const gchar *buffer,
1516 gboolean needs_root = TRUE;
1520 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1521 g_return_val_if_fail (buffer != NULL, 0);
1524 length = strlen (buffer);
1527 end = buffer + length;
1528 while (p != end && xml_isspace (*p))
1531 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1534 return add_ui_from_string (self, buffer, length, needs_root, error);
1538 * gtk_ui_manager_add_ui_from_file:
1539 * @self: a #GtkUIManager object
1540 * @filename: the name of the file to parse
1541 * @error: return location for an error
1543 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1544 * merges it with the current contents of @self.
1546 * Return value: The merge id for the merged UI. The merge id can be used
1547 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1548 * the return value is 0.
1553 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1554 const gchar *filename,
1561 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1563 if (!g_file_get_contents (filename, &buffer, &length, error))
1566 res = add_ui_from_string (self, buffer, length, FALSE, error);
1573 * gtk_ui_manager_add_ui:
1574 * @self: a #GtkUIManager
1575 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1577 * @name: the name for the added UI element
1578 * @action: the name of the action to be proxied, or %NULL to add a separator
1579 * @type: the type of UI element to add.
1580 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1581 * is added after its siblings.
1583 * Adds a UI element to the current contents of @self.
1585 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1586 * separator if such an element can be inserted at the place determined by
1587 * @path. Otherwise @type must indicate an element that can be inserted at
1588 * the place determined by @path.
1590 * If @path points to a menuitem or toolitem, the new element will be inserted
1591 * before or after this item, depending on @top.
1596 gtk_ui_manager_add_ui (GtkUIManager *self,
1600 const gchar *action,
1601 GtkUIManagerItemType type,
1608 GQuark action_quark = 0;
1610 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1611 g_return_if_fail (merge_id > 0);
1612 g_return_if_fail (name != NULL);
1614 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1620 node_type = NODE_TYPE_UNDECIDED;
1623 switch (NODE_INFO (node)->type)
1625 case NODE_TYPE_SEPARATOR:
1626 case NODE_TYPE_MENUITEM:
1627 case NODE_TYPE_TOOLITEM:
1629 node = node->parent;
1631 case NODE_TYPE_MENUBAR:
1632 case NODE_TYPE_MENU:
1633 case NODE_TYPE_POPUP:
1634 case NODE_TYPE_MENU_PLACEHOLDER:
1637 case GTK_UI_MANAGER_AUTO:
1639 node_type = NODE_TYPE_MENUITEM;
1641 node_type = NODE_TYPE_SEPARATOR;
1643 case GTK_UI_MANAGER_MENU:
1644 node_type = NODE_TYPE_MENU;
1646 case GTK_UI_MANAGER_MENUITEM:
1647 node_type = NODE_TYPE_MENUITEM;
1649 case GTK_UI_MANAGER_SEPARATOR:
1650 node_type = NODE_TYPE_SEPARATOR;
1652 case GTK_UI_MANAGER_PLACEHOLDER:
1653 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1659 case NODE_TYPE_TOOLBAR:
1660 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1663 case GTK_UI_MANAGER_AUTO:
1665 node_type = NODE_TYPE_TOOLITEM;
1667 node_type = NODE_TYPE_SEPARATOR;
1669 case GTK_UI_MANAGER_TOOLITEM:
1670 node_type = NODE_TYPE_TOOLITEM;
1672 case GTK_UI_MANAGER_SEPARATOR:
1673 node_type = NODE_TYPE_SEPARATOR;
1675 case GTK_UI_MANAGER_PLACEHOLDER:
1676 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1682 case NODE_TYPE_ROOT:
1685 case GTK_UI_MANAGER_MENUBAR:
1686 node_type = NODE_TYPE_MENUBAR;
1688 case GTK_UI_MANAGER_TOOLBAR:
1689 node_type = NODE_TYPE_TOOLBAR;
1691 case GTK_UI_MANAGER_POPUP:
1692 node_type = NODE_TYPE_POPUP;
1694 case GTK_UI_MANAGER_ACCELERATOR:
1695 node_type = NODE_TYPE_ACCELERATOR;
1705 if (node_type == NODE_TYPE_UNDECIDED)
1707 g_warning ("item type %d not suitable for adding at '%s'",
1712 child = get_child_node (self, node, sibling,
1713 name, strlen (name),
1714 node_type, TRUE, top);
1717 action_quark = g_quark_from_string (action);
1719 node_prepend_ui_reference (child, merge_id, action_quark);
1721 if (NODE_INFO (child)->action_name == 0)
1722 NODE_INFO (child)->action_name = action_quark;
1724 queue_update (self);
1726 g_object_notify (G_OBJECT (self), "ui");
1730 remove_ui (GNode *node,
1733 guint merge_id = GPOINTER_TO_UINT (user_data);
1735 node_remove_ui_reference (node, merge_id);
1737 return FALSE; /* continue */
1741 * gtk_ui_manager_remove_ui:
1742 * @self: a #GtkUIManager object
1743 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1745 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1750 gtk_ui_manager_remove_ui (GtkUIManager *self,
1753 g_node_traverse (self->private_data->root_node,
1754 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1755 remove_ui, GUINT_TO_POINTER (merge_id));
1757 queue_update (self);
1759 g_object_notify (G_OBJECT (self), "ui");
1762 /* -------------------- Updates -------------------- */
1766 get_action_by_name (GtkUIManager *merge,
1767 const gchar *action_name)
1775 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1777 GtkActionGroup *action_group = tmp->data;
1780 action = gtk_action_group_get_action (action_group, action_name);
1790 find_menu_position (GNode *node,
1791 GtkWidget **menushell_p,
1794 GtkWidget *menushell;
1797 g_return_if_fail (node != NULL);
1798 g_return_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1799 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1800 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1801 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1802 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR);
1804 /* first sibling -- look at parent */
1805 if (node->prev == NULL)
1810 parent = node->parent;
1811 switch (NODE_INFO (parent)->type)
1813 case NODE_TYPE_MENUBAR:
1814 case NODE_TYPE_POPUP:
1815 menushell = NODE_INFO (parent)->proxy;
1818 case NODE_TYPE_MENU:
1819 menushell = NODE_INFO (parent)->proxy;
1820 if (GTK_IS_MENU_ITEM (menushell))
1821 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1822 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1823 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1827 g_list_free (siblings);
1829 case NODE_TYPE_MENU_PLACEHOLDER:
1830 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1831 g_return_if_fail (GTK_IS_MENU_SHELL (menushell));
1832 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1833 NODE_INFO (parent)->proxy) + 1;
1836 g_warning("%s: bad parent node type %d", G_STRLOC,
1837 NODE_INFO (parent)->type);
1843 GtkWidget *prev_child;
1846 sibling = node->prev;
1847 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1848 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1850 prev_child = NODE_INFO (sibling)->proxy;
1852 g_return_if_fail (GTK_IS_WIDGET (prev_child));
1853 menushell = gtk_widget_get_parent (prev_child);
1854 g_return_if_fail (GTK_IS_MENU_SHELL (menushell));
1856 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1860 *menushell_p = menushell;
1866 find_toolbar_position (GNode *node,
1867 GtkWidget **toolbar_p,
1873 g_return_if_fail (node != NULL);
1874 g_return_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1875 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1876 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1877 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR);
1879 /* first sibling -- look at parent */
1880 if (node->prev == NULL)
1884 parent = node->parent;
1885 switch (NODE_INFO (parent)->type)
1887 case NODE_TYPE_TOOLBAR:
1888 toolbar = NODE_INFO (parent)->proxy;
1891 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1892 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1893 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1894 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1895 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1898 g_warning ("%s: bad parent node type %d", G_STRLOC,
1899 NODE_INFO (parent)->type);
1905 GtkWidget *prev_child;
1908 sibling = node->prev;
1909 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1910 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1912 prev_child = NODE_INFO (sibling)->proxy;
1914 g_return_if_fail (GTK_IS_WIDGET (prev_child));
1915 toolbar = gtk_widget_get_parent (prev_child);
1916 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1918 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1919 GTK_TOOL_ITEM (prev_child)) + 1;
1923 *toolbar_p = toolbar;
1929 * _gtk_menu_is_empty:
1930 * @menu: a #GtkMenu or %NULL
1932 * Determines whether @menu is empty. A menu is considered empty if it
1933 * the only visible children are tearoff menu items or "filler" menu
1934 * items which were inserted to mark the menu as empty.
1936 * This function is used by #GtkAction.
1938 * Return value: whether @menu is empty.
1941 _gtk_menu_is_empty (GtkWidget *menu)
1943 GList *children, *cur;
1945 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1950 children = gtk_container_get_children (GTK_CONTAINER (menu));
1955 if (GTK_WIDGET_VISIBLE (cur->data))
1957 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1958 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1963 g_list_free (children);
1969 SEPARATOR_MODE_SMART,
1970 SEPARATOR_MODE_VISIBLE,
1971 SEPARATOR_MODE_HIDDEN
1975 update_smart_separators (GtkWidget *proxy)
1977 GtkWidget *parent = NULL;
1979 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1981 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1982 parent = gtk_widget_get_parent (proxy);
1988 GList *children, *cur, *last;
1991 children = gtk_container_get_children (GTK_CONTAINER (parent));
2001 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2005 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2006 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2009 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2010 "gtk-separator-mode"));
2013 case SEPARATOR_MODE_VISIBLE:
2014 gtk_widget_show (GTK_WIDGET (cur->data));
2018 case SEPARATOR_MODE_HIDDEN:
2019 gtk_widget_hide (GTK_WIDGET (cur->data));
2021 case SEPARATOR_MODE_SMART:
2024 gtk_widget_show (GTK_WIDGET (cur->data));
2029 gtk_widget_hide (GTK_WIDGET (cur->data));
2033 else if (GTK_WIDGET_VISIBLE (cur->data))
2036 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2049 gtk_widget_hide (GTK_WIDGET (last->data));
2051 if (GTK_IS_MENU (parent))
2055 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2056 if (GTK_IS_MENU_ITEM (item))
2057 _gtk_action_sync_menu_visible (NULL, item, empty);
2058 if (GTK_IS_WIDGET (filler))
2061 gtk_widget_show (filler);
2063 gtk_widget_hide (filler);
2067 g_list_free (children);
2072 update_node (GtkUIManager *self,
2079 const gchar *action_name;
2080 NodeUIReference *ref;
2082 #ifdef DEBUG_UI_MANAGER
2086 g_return_if_fail (node != NULL);
2087 g_return_if_fail (NODE_INFO (node) != NULL);
2089 info = NODE_INFO (node);
2094 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2096 #ifdef DEBUG_UI_MANAGER
2097 g_print ("update_node name=%s dirty=%d popup %d (",
2098 info->name, info->dirty, in_popup);
2099 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2101 NodeUIReference *ref = tmp->data;
2102 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2109 if (info->uifiles == NULL) {
2110 /* We may need to remove this node.
2111 * This must be done in post order
2113 goto recurse_children;
2116 ref = info->uifiles->data;
2117 action_name = g_quark_to_string (ref->action_quark);
2118 action = get_action_by_name (self, action_name);
2120 info->dirty = FALSE;
2122 /* Check if the node doesn't have an action and must have an action */
2123 if (action == NULL &&
2124 info->type != NODE_TYPE_ROOT &&
2125 info->type != NODE_TYPE_MENUBAR &&
2126 info->type != NODE_TYPE_TOOLBAR &&
2127 info->type != NODE_TYPE_POPUP &&
2128 info->type != NODE_TYPE_SEPARATOR &&
2129 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2130 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2132 g_warning ("%s: missing action", info->name);
2134 goto recurse_children;
2138 gtk_action_set_accel_group (action, self->private_data->accel_group);
2140 /* If the widget already has a proxy and the action hasn't changed, then
2141 * we only have to update the tearoff menu items.
2143 if (info->proxy != NULL && action == info->action)
2145 if (info->type == NODE_TYPE_MENU)
2150 if (GTK_IS_MENU (info->proxy))
2153 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2154 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2155 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2157 if (self->private_data->add_tearoffs && !in_popup)
2158 gtk_widget_show (GTK_WIDGET (siblings->data));
2160 gtk_widget_hide (GTK_WIDGET (siblings->data));
2162 g_list_free (siblings);
2165 goto recurse_children;
2170 case NODE_TYPE_MENUBAR:
2171 if (info->proxy == NULL)
2173 info->proxy = gtk_menu_bar_new ();
2174 g_object_ref_sink (info->proxy);
2175 gtk_widget_set_name (info->proxy, info->name);
2176 gtk_widget_show (info->proxy);
2177 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2180 case NODE_TYPE_POPUP:
2181 if (info->proxy == NULL)
2183 info->proxy = gtk_menu_new ();
2184 g_object_ref_sink (info->proxy);
2186 gtk_widget_set_name (info->proxy, info->name);
2188 case NODE_TYPE_MENU:
2190 GtkWidget *prev_submenu = NULL;
2193 /* remove the proxy if it is of the wrong type ... */
2195 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2197 if (GTK_IS_MENU_ITEM (info->proxy))
2199 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2202 g_object_ref (prev_submenu);
2203 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2207 gtk_action_disconnect_proxy (info->action, info->proxy);
2208 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2210 g_object_unref (info->proxy);
2213 /* create proxy if needed ... */
2214 if (info->proxy == NULL)
2219 menu = gtk_menu_new ();
2220 gtk_widget_set_name (menu, info->name);
2221 tearoff = gtk_tearoff_menu_item_new ();
2222 gtk_widget_set_no_show_all (tearoff, TRUE);
2223 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2224 filler = gtk_menu_item_new_with_label (_("Empty"));
2225 g_object_set_data (G_OBJECT (filler),
2226 I_("gtk-empty-menu-item"),
2227 GINT_TO_POINTER (TRUE));
2228 gtk_widget_set_sensitive (filler, FALSE);
2229 gtk_widget_set_no_show_all (filler, TRUE);
2230 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2232 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2235 g_object_ref_sink (info->proxy);
2236 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2241 GtkWidget *menushell;
2244 find_menu_position (node, &menushell, &pos);
2245 info->proxy = gtk_action_create_menu_item (action);
2246 g_object_ref_sink (info->proxy);
2247 g_signal_connect (info->proxy, "notify::visible",
2248 G_CALLBACK (update_smart_separators), NULL);
2249 gtk_widget_set_name (info->proxy, info->name);
2251 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2252 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2256 gtk_action_connect_proxy (action, info->proxy);
2260 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2262 g_object_unref (prev_submenu);
2265 if (GTK_IS_MENU (info->proxy))
2268 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2269 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2270 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2272 if (self->private_data->add_tearoffs && !in_popup)
2273 gtk_widget_show (GTK_WIDGET (siblings->data));
2275 gtk_widget_hide (GTK_WIDGET (siblings->data));
2277 g_list_free (siblings);
2280 case NODE_TYPE_UNDECIDED:
2281 g_warning ("found undecided node!");
2283 case NODE_TYPE_ROOT:
2285 case NODE_TYPE_TOOLBAR:
2286 if (info->proxy == NULL)
2288 info->proxy = gtk_toolbar_new ();
2289 g_object_ref_sink (info->proxy);
2290 gtk_widget_set_name (info->proxy, info->name);
2291 gtk_widget_show (info->proxy);
2292 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2295 case NODE_TYPE_MENU_PLACEHOLDER:
2296 /* create menu items for placeholders if necessary ... */
2297 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2298 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2302 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2304 g_object_unref (info->proxy);
2309 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2311 g_object_unref (info->extra);
2315 if (info->proxy == NULL)
2317 GtkWidget *menushell;
2320 find_menu_position (node, &menushell, &pos);
2321 info->proxy = gtk_separator_menu_item_new ();
2322 g_object_ref_sink (info->proxy);
2323 g_object_set_data (G_OBJECT (info->proxy),
2324 I_("gtk-separator-mode"),
2325 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2326 gtk_widget_set_no_show_all (info->proxy, TRUE);
2327 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2328 NODE_INFO (node)->proxy, pos);
2330 info->extra = gtk_separator_menu_item_new ();
2331 g_object_ref_sink (info->extra);
2332 g_object_set_data (G_OBJECT (info->extra),
2333 I_("gtk-separator-mode"),
2334 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2335 gtk_widget_set_no_show_all (info->extra, TRUE);
2336 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2337 NODE_INFO (node)->extra, pos+1);
2340 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2341 /* create toolbar items for placeholders if necessary ... */
2342 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2343 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2347 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2349 g_object_unref (info->proxy);
2354 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2356 g_object_unref (info->extra);
2360 if (info->proxy == NULL)
2366 find_toolbar_position (node, &toolbar, &pos);
2367 item = gtk_separator_tool_item_new ();
2368 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2369 info->proxy = GTK_WIDGET (item);
2370 g_object_ref_sink (info->proxy);
2371 g_object_set_data (G_OBJECT (info->proxy),
2372 I_("gtk-separator-mode"),
2373 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2374 gtk_widget_set_no_show_all (info->proxy, TRUE);
2376 item = gtk_separator_tool_item_new ();
2377 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2378 info->extra = GTK_WIDGET (item);
2379 g_object_ref_sink (info->extra);
2380 g_object_set_data (G_OBJECT (info->extra),
2381 I_("gtk-separator-mode"),
2382 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2383 gtk_widget_set_no_show_all (info->extra, TRUE);
2386 case NODE_TYPE_MENUITEM:
2387 /* remove the proxy if it is of the wrong type ... */
2389 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2391 g_signal_handlers_disconnect_by_func (info->proxy,
2392 G_CALLBACK (update_smart_separators),
2394 gtk_action_disconnect_proxy (info->action, info->proxy);
2395 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2397 g_object_unref (info->proxy);
2400 /* create proxy if needed ... */
2401 if (info->proxy == NULL)
2403 GtkWidget *menushell;
2406 find_menu_position (node, &menushell, &pos);
2407 info->proxy = gtk_action_create_menu_item (action);
2408 g_object_ref_sink (info->proxy);
2409 gtk_widget_set_name (info->proxy, info->name);
2411 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2416 g_signal_handlers_disconnect_by_func (info->proxy,
2417 G_CALLBACK (update_smart_separators),
2419 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2420 gtk_action_connect_proxy (action, info->proxy);
2422 g_signal_connect (info->proxy, "notify::visible",
2423 G_CALLBACK (update_smart_separators), NULL);
2426 /* don't show accels in popups */
2427 GtkWidget *label = GTK_BIN (info->proxy)->child;
2428 g_object_set (label,
2429 "accel_closure", NULL,
2434 case NODE_TYPE_TOOLITEM:
2435 /* remove the proxy if it is of the wrong type ... */
2437 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2439 g_signal_handlers_disconnect_by_func (info->proxy,
2440 G_CALLBACK (update_smart_separators),
2442 gtk_action_disconnect_proxy (info->action, info->proxy);
2443 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2445 g_object_unref (info->proxy);
2448 /* create proxy if needed ... */
2449 if (info->proxy == NULL)
2454 find_toolbar_position (node, &toolbar, &pos);
2455 info->proxy = gtk_action_create_tool_item (action);
2456 g_object_ref_sink (info->proxy);
2457 gtk_widget_set_name (info->proxy, info->name);
2459 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2460 GTK_TOOL_ITEM (info->proxy), pos);
2464 g_signal_handlers_disconnect_by_func (info->proxy,
2465 G_CALLBACK (update_smart_separators),
2467 gtk_action_connect_proxy (action, info->proxy);
2470 /* FIXME: we must re-set the tooltip, since tooltips on toolitems can't be
2471 * set before the toolitem is added to the toolbar.
2476 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2477 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2481 g_signal_connect (info->proxy, "notify::visible",
2482 G_CALLBACK (update_smart_separators), NULL);
2484 case NODE_TYPE_SEPARATOR:
2485 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2486 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2490 gint separator_mode;
2493 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2495 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2497 g_object_unref (info->proxy);
2501 find_toolbar_position (node, &toolbar, &pos);
2502 item = gtk_separator_tool_item_new ();
2503 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2504 info->proxy = GTK_WIDGET (item);
2505 g_object_ref_sink (info->proxy);
2506 gtk_widget_set_no_show_all (info->proxy, TRUE);
2509 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2510 gtk_separator_tool_item_set_draw
2511 (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2512 separator_mode = SEPARATOR_MODE_VISIBLE;
2515 separator_mode = SEPARATOR_MODE_SMART;
2517 g_object_set_data (G_OBJECT (info->proxy),
2518 I_("gtk-separator-mode"),
2519 GINT_TO_POINTER (separator_mode));
2520 gtk_widget_show (info->proxy);
2524 GtkWidget *menushell;
2527 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2529 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2531 g_object_unref (info->proxy);
2535 find_menu_position (node, &menushell, &pos);
2536 info->proxy = gtk_separator_menu_item_new ();
2537 g_object_ref_sink (info->proxy);
2538 gtk_widget_set_no_show_all (info->proxy, TRUE);
2539 g_object_set_data (G_OBJECT (info->proxy),
2540 I_("gtk-separator-mode"),
2541 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2542 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2544 gtk_widget_show (info->proxy);
2547 case NODE_TYPE_ACCELERATOR:
2548 gtk_action_connect_accelerator (action);
2553 g_object_ref (action);
2555 g_object_unref (info->action);
2556 info->action = action;
2559 /* process children */
2560 child = node->children;
2566 child = current->next;
2567 update_node (self, current, in_popup);
2572 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2573 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2574 else if (info->type == NODE_TYPE_MENU ||
2575 info->type == NODE_TYPE_TOOLBAR ||
2576 info->type == NODE_TYPE_POPUP)
2577 update_smart_separators (info->proxy);
2580 /* handle cleanup of dead nodes */
2581 if (node->children == NULL && info->uifiles == NULL)
2584 gtk_widget_destroy (info->proxy);
2586 gtk_widget_destroy (info->extra);
2587 if (info->type == NODE_TYPE_ACCELERATOR)
2588 gtk_action_disconnect_accelerator (info->action);
2590 g_node_destroy (node);
2595 do_updates (GtkUIManager *self)
2597 /* this function needs to check through the tree for dirty nodes.
2598 * For such nodes, it needs to do the following:
2600 * 1) check if they are referenced by any loaded UI files anymore.
2601 * In which case, the proxy widget should be destroyed, unless
2602 * there are any subnodes.
2604 * 2) lookup the action for this node again. If it is different to
2605 * the current one (or if no previous action has been looked up),
2606 * the proxy is reconnected to the new action (or a new proxy widget
2607 * is created and added to the parent container).
2609 update_node (self, self->private_data->root_node, FALSE);
2611 self->private_data->update_tag = 0;
2617 do_updates_idle (GtkUIManager *self)
2619 GDK_THREADS_ENTER ();
2621 GDK_THREADS_LEAVE ();
2627 queue_update (GtkUIManager *self)
2629 if (self->private_data->update_tag != 0)
2632 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates_idle,
2638 * gtk_ui_manager_ensure_update:
2639 * @self: a #GtkUIManager
2641 * Makes sure that all pending updates to the UI have been completed.
2643 * This may occasionally be necessary, since #GtkUIManager updates the
2644 * UI in an idle function. A typical example where this function is
2645 * useful is to enforce that the menubar and toolbar have been added to
2646 * the main window before showing it:
2649 * gtk_container_add (GTK_CONTAINER (window), vbox);
2650 * g_signal_connect (merge, "add_widget",
2651 * G_CALLBACK (add_widget), vbox);
2652 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2653 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2654 * gtk_ui_manager_ensure_update (merge);
2655 * gtk_widget_show (window);
2657 * </informalexample>
2662 gtk_ui_manager_ensure_update (GtkUIManager *self)
2664 if (self->private_data->update_tag != 0)
2666 g_source_remove (self->private_data->update_tag);
2672 dirty_traverse_func (GNode *node,
2675 NODE_INFO (node)->dirty = TRUE;
2680 dirty_all_nodes (GtkUIManager *self)
2682 g_node_traverse (self->private_data->root_node,
2683 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2684 dirty_traverse_func, NULL);
2685 queue_update (self);
2689 mark_node_dirty (GNode *node)
2693 /* FIXME could optimize this */
2694 for (p = node; p; p = p->parent)
2695 NODE_INFO (p)->dirty = TRUE;
2698 static const gchar *
2699 open_tag_format (NodeType type)
2703 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2704 case NODE_TYPE_ROOT: return "%*s<ui";
2705 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2706 case NODE_TYPE_MENU: return "%*s<menu";
2707 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2708 case NODE_TYPE_MENU_PLACEHOLDER:
2709 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2710 case NODE_TYPE_POPUP: return "%*s<popup";
2711 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2712 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2713 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2714 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2715 default: return NULL;
2719 static const gchar *
2720 close_tag_format (NodeType type)
2724 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2725 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2726 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2727 case NODE_TYPE_MENU: return "%*s</menu>\n";
2728 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2729 case NODE_TYPE_MENU_PLACEHOLDER:
2730 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2731 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2732 default: return NULL;
2737 print_node (GtkUIManager *self,
2744 const gchar *open_fmt;
2745 const gchar *close_fmt;
2749 open_fmt = open_tag_format (mnode->type);
2750 close_fmt = close_tag_format (mnode->type);
2752 g_string_append_printf (buffer, open_fmt, indent_level, "");
2754 if (mnode->type != NODE_TYPE_ROOT)
2757 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2759 if (mnode->action_name)
2760 g_string_append_printf (buffer, " action=\"%s\"",
2761 g_quark_to_string (mnode->action_name));
2764 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2766 for (child = node->children; child != NULL; child = child->next)
2767 print_node (self, child, indent_level + 2, buffer);
2770 g_string_append_printf (buffer, close_fmt, indent_level, "");
2774 * gtk_ui_manager_get_ui:
2775 * @self: a #GtkUIManager
2777 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2779 * Return value: A newly allocated string containing an XML representation of
2785 gtk_ui_manager_get_ui (GtkUIManager *self)
2789 buffer = g_string_new (NULL);
2791 gtk_ui_manager_ensure_update (self);
2793 print_node (self, self->private_data->root_node, 0, buffer);
2795 return g_string_free (buffer, FALSE);
2800 #undef gtk_ui_manager_add_ui_from_file
2803 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2804 const gchar *filename,
2807 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2810 if (utf8_filename == NULL)
2813 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2815 g_free (utf8_filename);
2822 #define __GTK_UI_MANAGER_C__
2823 #include "gtkaliasdef.c"