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_val_if_fail (node != NULL, FALSE);
1798 g_return_val_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,
1805 /* first sibling -- look at parent */
1806 if (node->prev == NULL)
1811 parent = node->parent;
1812 switch (NODE_INFO (parent)->type)
1814 case NODE_TYPE_MENUBAR:
1815 case NODE_TYPE_POPUP:
1816 menushell = NODE_INFO (parent)->proxy;
1819 case NODE_TYPE_MENU:
1820 menushell = NODE_INFO (parent)->proxy;
1821 if (GTK_IS_MENU_ITEM (menushell))
1822 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1823 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1824 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1828 g_list_free (siblings);
1830 case NODE_TYPE_MENU_PLACEHOLDER:
1831 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1832 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1833 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1834 NODE_INFO (parent)->proxy) + 1;
1837 g_warning ("%s: bad parent node type %d", G_STRLOC,
1838 NODE_INFO (parent)->type);
1844 GtkWidget *prev_child;
1847 sibling = node->prev;
1848 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1849 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1851 prev_child = NODE_INFO (sibling)->proxy;
1853 if (!GTK_IS_WIDGET (prev_child))
1856 menushell = gtk_widget_get_parent (prev_child);
1857 if (!GTK_IS_MENU_SHELL (menushell))
1860 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1864 *menushell_p = menushell;
1872 find_toolbar_position (GNode *node,
1873 GtkWidget **toolbar_p,
1879 g_return_val_if_fail (node != NULL, FALSE);
1880 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1881 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1882 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1883 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1886 /* first sibling -- look at parent */
1887 if (node->prev == NULL)
1891 parent = node->parent;
1892 switch (NODE_INFO (parent)->type)
1894 case NODE_TYPE_TOOLBAR:
1895 toolbar = NODE_INFO (parent)->proxy;
1898 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1899 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1900 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1901 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1902 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1905 g_warning ("%s: bad parent node type %d", G_STRLOC,
1906 NODE_INFO (parent)->type);
1912 GtkWidget *prev_child;
1915 sibling = node->prev;
1916 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1917 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1919 prev_child = NODE_INFO (sibling)->proxy;
1921 if (!GTK_IS_WIDGET (prev_child))
1924 toolbar = gtk_widget_get_parent (prev_child);
1925 if (!GTK_IS_TOOLBAR (toolbar))
1928 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1929 GTK_TOOL_ITEM (prev_child)) + 1;
1933 *toolbar_p = toolbar;
1941 * _gtk_menu_is_empty:
1942 * @menu: a #GtkMenu or %NULL
1944 * Determines whether @menu is empty. A menu is considered empty if it
1945 * the only visible children are tearoff menu items or "filler" menu
1946 * items which were inserted to mark the menu as empty.
1948 * This function is used by #GtkAction.
1950 * Return value: whether @menu is empty.
1953 _gtk_menu_is_empty (GtkWidget *menu)
1955 GList *children, *cur;
1956 gboolean result = TRUE;
1958 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1963 children = gtk_container_get_children (GTK_CONTAINER (menu));
1968 if (GTK_WIDGET_VISIBLE (cur->data))
1970 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1971 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1979 g_list_free (children);
1985 SEPARATOR_MODE_SMART,
1986 SEPARATOR_MODE_VISIBLE,
1987 SEPARATOR_MODE_HIDDEN
1991 update_smart_separators (GtkWidget *proxy)
1993 GtkWidget *parent = NULL;
1995 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1997 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1998 parent = gtk_widget_get_parent (proxy);
2004 GList *children, *cur, *last;
2007 children = gtk_container_get_children (GTK_CONTAINER (parent));
2017 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2021 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2022 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2025 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2026 "gtk-separator-mode"));
2029 case SEPARATOR_MODE_VISIBLE:
2030 gtk_widget_show (GTK_WIDGET (cur->data));
2034 case SEPARATOR_MODE_HIDDEN:
2035 gtk_widget_hide (GTK_WIDGET (cur->data));
2037 case SEPARATOR_MODE_SMART:
2040 gtk_widget_show (GTK_WIDGET (cur->data));
2045 gtk_widget_hide (GTK_WIDGET (cur->data));
2049 else if (GTK_WIDGET_VISIBLE (cur->data))
2052 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2065 gtk_widget_hide (GTK_WIDGET (last->data));
2067 if (GTK_IS_MENU (parent))
2071 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2072 if (GTK_IS_MENU_ITEM (item))
2073 _gtk_action_sync_menu_visible (NULL, item, empty);
2074 if (GTK_IS_WIDGET (filler))
2077 gtk_widget_show (filler);
2079 gtk_widget_hide (filler);
2083 g_list_free (children);
2088 update_node (GtkUIManager *self,
2095 const gchar *action_name;
2096 NodeUIReference *ref;
2098 #ifdef DEBUG_UI_MANAGER
2102 g_return_if_fail (node != NULL);
2103 g_return_if_fail (NODE_INFO (node) != NULL);
2105 info = NODE_INFO (node);
2110 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2112 #ifdef DEBUG_UI_MANAGER
2113 g_print ("update_node name=%s dirty=%d popup %d (",
2114 info->name, info->dirty, in_popup);
2115 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2117 NodeUIReference *ref = tmp->data;
2118 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2125 if (info->uifiles == NULL) {
2126 /* We may need to remove this node.
2127 * This must be done in post order
2129 goto recurse_children;
2132 ref = info->uifiles->data;
2133 action_name = g_quark_to_string (ref->action_quark);
2134 action = get_action_by_name (self, action_name);
2136 info->dirty = FALSE;
2138 /* Check if the node doesn't have an action and must have an action */
2139 if (action == NULL &&
2140 info->type != NODE_TYPE_ROOT &&
2141 info->type != NODE_TYPE_MENUBAR &&
2142 info->type != NODE_TYPE_TOOLBAR &&
2143 info->type != NODE_TYPE_POPUP &&
2144 info->type != NODE_TYPE_SEPARATOR &&
2145 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2146 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2148 g_warning ("%s: missing action %s", info->name, action_name);
2154 gtk_action_set_accel_group (action, self->private_data->accel_group);
2156 /* If the widget already has a proxy and the action hasn't changed, then
2157 * we only have to update the tearoff menu items.
2159 if (info->proxy != NULL && action == info->action)
2161 if (info->type == NODE_TYPE_MENU)
2166 if (GTK_IS_MENU (info->proxy))
2169 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2170 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2171 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2173 if (self->private_data->add_tearoffs && !in_popup)
2174 gtk_widget_show (GTK_WIDGET (siblings->data));
2176 gtk_widget_hide (GTK_WIDGET (siblings->data));
2178 g_list_free (siblings);
2181 goto recurse_children;
2186 case NODE_TYPE_MENUBAR:
2187 if (info->proxy == NULL)
2189 info->proxy = gtk_menu_bar_new ();
2190 g_object_ref_sink (info->proxy);
2191 gtk_widget_set_name (info->proxy, info->name);
2192 gtk_widget_show (info->proxy);
2193 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2196 case NODE_TYPE_POPUP:
2197 if (info->proxy == NULL)
2199 info->proxy = gtk_menu_new ();
2200 g_object_ref_sink (info->proxy);
2202 gtk_widget_set_name (info->proxy, info->name);
2204 case NODE_TYPE_MENU:
2206 GtkWidget *prev_submenu = NULL;
2209 /* remove the proxy if it is of the wrong type ... */
2211 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2213 if (GTK_IS_MENU_ITEM (info->proxy))
2215 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2218 g_object_ref (prev_submenu);
2219 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2223 gtk_action_disconnect_proxy (info->action, info->proxy);
2224 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2226 g_object_unref (info->proxy);
2229 /* create proxy if needed ... */
2230 if (info->proxy == NULL)
2235 menu = gtk_menu_new ();
2236 gtk_widget_set_name (menu, info->name);
2237 tearoff = gtk_tearoff_menu_item_new ();
2238 gtk_widget_set_no_show_all (tearoff, TRUE);
2239 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2240 filler = gtk_menu_item_new_with_label (_("Empty"));
2241 g_object_set_data (G_OBJECT (filler),
2242 I_("gtk-empty-menu-item"),
2243 GINT_TO_POINTER (TRUE));
2244 gtk_widget_set_sensitive (filler, FALSE);
2245 gtk_widget_set_no_show_all (filler, TRUE);
2246 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2248 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2251 g_object_ref_sink (info->proxy);
2252 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2257 GtkWidget *menushell;
2260 if (find_menu_position (node, &menushell, &pos))
2262 info->proxy = gtk_action_create_menu_item (action);
2263 g_object_ref_sink (info->proxy);
2264 g_signal_connect (info->proxy, "notify::visible",
2265 G_CALLBACK (update_smart_separators), NULL);
2266 gtk_widget_set_name (info->proxy, info->name);
2268 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2269 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2274 gtk_action_connect_proxy (action, info->proxy);
2278 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2280 g_object_unref (prev_submenu);
2283 if (GTK_IS_MENU (info->proxy))
2286 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2287 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2288 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2290 if (self->private_data->add_tearoffs && !in_popup)
2291 gtk_widget_show (GTK_WIDGET (siblings->data));
2293 gtk_widget_hide (GTK_WIDGET (siblings->data));
2295 g_list_free (siblings);
2298 case NODE_TYPE_UNDECIDED:
2299 g_warning ("found undecided node!");
2301 case NODE_TYPE_ROOT:
2303 case NODE_TYPE_TOOLBAR:
2304 if (info->proxy == NULL)
2306 info->proxy = gtk_toolbar_new ();
2307 g_object_ref_sink (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_sink (info->proxy);
2342 g_object_set_data (G_OBJECT (info->proxy),
2343 I_("gtk-separator-mode"),
2344 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2345 gtk_widget_set_no_show_all (info->proxy, TRUE);
2346 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2347 NODE_INFO (node)->proxy, pos);
2349 info->extra = gtk_separator_menu_item_new ();
2350 g_object_ref_sink (info->extra);
2351 g_object_set_data (G_OBJECT (info->extra),
2352 I_("gtk-separator-mode"),
2353 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2354 gtk_widget_set_no_show_all (info->extra, TRUE);
2355 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2356 NODE_INFO (node)->extra, pos + 1);
2360 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2361 /* create toolbar items for placeholders if necessary ... */
2362 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2363 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2367 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2369 g_object_unref (info->proxy);
2374 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2376 g_object_unref (info->extra);
2380 if (info->proxy == NULL)
2386 if (find_toolbar_position (node, &toolbar, &pos))
2388 item = gtk_separator_tool_item_new ();
2389 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2390 info->proxy = GTK_WIDGET (item);
2391 g_object_ref_sink (info->proxy);
2392 g_object_set_data (G_OBJECT (info->proxy),
2393 I_("gtk-separator-mode"),
2394 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2395 gtk_widget_set_no_show_all (info->proxy, TRUE);
2397 item = gtk_separator_tool_item_new ();
2398 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2399 info->extra = GTK_WIDGET (item);
2400 g_object_ref_sink (info->extra);
2401 g_object_set_data (G_OBJECT (info->extra),
2402 I_("gtk-separator-mode"),
2403 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2404 gtk_widget_set_no_show_all (info->extra, TRUE);
2408 case NODE_TYPE_MENUITEM:
2409 /* remove the proxy if it is of the wrong type ... */
2411 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2413 g_signal_handlers_disconnect_by_func (info->proxy,
2414 G_CALLBACK (update_smart_separators),
2416 gtk_action_disconnect_proxy (info->action, info->proxy);
2417 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2419 g_object_unref (info->proxy);
2422 /* create proxy if needed ... */
2423 if (info->proxy == NULL)
2425 GtkWidget *menushell;
2428 if (find_menu_position (node, &menushell, &pos))
2430 info->proxy = gtk_action_create_menu_item (action);
2431 g_object_ref_sink (info->proxy);
2432 gtk_widget_set_name (info->proxy, info->name);
2434 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2440 g_signal_handlers_disconnect_by_func (info->proxy,
2441 G_CALLBACK (update_smart_separators),
2443 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2444 gtk_action_connect_proxy (action, info->proxy);
2449 g_signal_connect (info->proxy, "notify::visible",
2450 G_CALLBACK (update_smart_separators), NULL);
2453 /* don't show accels in popups */
2454 GtkWidget *label = GTK_BIN (info->proxy)->child;
2455 g_object_set (label, "accel-closure", NULL, NULL);
2460 case NODE_TYPE_TOOLITEM:
2461 /* remove the proxy if it is of the wrong type ... */
2463 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2465 g_signal_handlers_disconnect_by_func (info->proxy,
2466 G_CALLBACK (update_smart_separators),
2468 gtk_action_disconnect_proxy (info->action, info->proxy);
2469 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2471 g_object_unref (info->proxy);
2474 /* create proxy if needed ... */
2475 if (info->proxy == NULL)
2480 if (find_toolbar_position (node, &toolbar, &pos))
2482 info->proxy = gtk_action_create_tool_item (action);
2483 g_object_ref_sink (info->proxy);
2484 gtk_widget_set_name (info->proxy, info->name);
2486 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2487 GTK_TOOL_ITEM (info->proxy), pos);
2492 g_signal_handlers_disconnect_by_func (info->proxy,
2493 G_CALLBACK (update_smart_separators),
2495 gtk_action_connect_proxy (action, info->proxy);
2500 /* FIXME: we must re-set the tooltip, since tooltips on
2501 * toolitems can't be set before the toolitem is added
2506 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2507 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2510 g_signal_connect (info->proxy, "notify::visible",
2511 G_CALLBACK (update_smart_separators), NULL);
2514 case NODE_TYPE_SEPARATOR:
2515 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2516 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2520 gint separator_mode;
2523 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2525 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2527 g_object_unref (info->proxy);
2531 if (find_toolbar_position (node, &toolbar, &pos))
2533 item = gtk_separator_tool_item_new ();
2534 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2535 info->proxy = GTK_WIDGET (item);
2536 g_object_ref_sink (info->proxy);
2537 gtk_widget_set_no_show_all (info->proxy, TRUE);
2540 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2541 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2542 separator_mode = SEPARATOR_MODE_VISIBLE;
2545 separator_mode = SEPARATOR_MODE_SMART;
2547 g_object_set_data (G_OBJECT (info->proxy),
2548 I_("gtk-separator-mode"),
2549 GINT_TO_POINTER (separator_mode));
2550 gtk_widget_show (info->proxy);
2555 GtkWidget *menushell;
2558 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2560 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2562 g_object_unref (info->proxy);
2566 if (find_menu_position (node, &menushell, &pos))
2568 info->proxy = gtk_separator_menu_item_new ();
2569 g_object_ref_sink (info->proxy);
2570 gtk_widget_set_no_show_all (info->proxy, TRUE);
2571 g_object_set_data (G_OBJECT (info->proxy),
2572 I_("gtk-separator-mode"),
2573 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2574 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2576 gtk_widget_show (info->proxy);
2580 case NODE_TYPE_ACCELERATOR:
2581 gtk_action_connect_accelerator (action);
2586 g_object_ref (action);
2588 g_object_unref (info->action);
2589 info->action = action;
2592 /* process children */
2593 child = node->children;
2599 child = current->next;
2600 update_node (self, current, in_popup);
2605 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2606 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2607 else if (info->type == NODE_TYPE_MENU ||
2608 info->type == NODE_TYPE_TOOLBAR ||
2609 info->type == NODE_TYPE_POPUP)
2610 update_smart_separators (info->proxy);
2613 /* handle cleanup of dead nodes */
2614 if (node->children == NULL && info->uifiles == NULL)
2617 gtk_widget_destroy (info->proxy);
2619 gtk_widget_destroy (info->extra);
2620 if (info->type == NODE_TYPE_ACCELERATOR)
2621 gtk_action_disconnect_accelerator (info->action);
2623 g_node_destroy (node);
2628 do_updates (GtkUIManager *self)
2630 /* this function needs to check through the tree for dirty nodes.
2631 * For such nodes, it needs to do the following:
2633 * 1) check if they are referenced by any loaded UI files anymore.
2634 * In which case, the proxy widget should be destroyed, unless
2635 * there are any subnodes.
2637 * 2) lookup the action for this node again. If it is different to
2638 * the current one (or if no previous action has been looked up),
2639 * the proxy is reconnected to the new action (or a new proxy widget
2640 * is created and added to the parent container).
2642 update_node (self, self->private_data->root_node, FALSE);
2644 self->private_data->update_tag = 0;
2650 do_updates_idle (GtkUIManager *self)
2658 queue_update (GtkUIManager *self)
2660 if (self->private_data->update_tag != 0)
2663 self->private_data->update_tag = gdk_threads_add_idle (
2664 (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 *
2731 open_tag_format (NodeType type)
2735 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2736 case NODE_TYPE_ROOT: return "%*s<ui";
2737 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2738 case NODE_TYPE_MENU: return "%*s<menu";
2739 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2740 case NODE_TYPE_MENU_PLACEHOLDER:
2741 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2742 case NODE_TYPE_POPUP: return "%*s<popup";
2743 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2744 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2745 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2746 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2747 default: return NULL;
2751 static const gchar *
2752 close_tag_format (NodeType type)
2756 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2757 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2758 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2759 case NODE_TYPE_MENU: return "%*s</menu>\n";
2760 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2761 case NODE_TYPE_MENU_PLACEHOLDER:
2762 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2763 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2764 default: return NULL;
2769 print_node (GtkUIManager *self,
2776 const gchar *open_fmt;
2777 const gchar *close_fmt;
2781 open_fmt = open_tag_format (mnode->type);
2782 close_fmt = close_tag_format (mnode->type);
2784 g_string_append_printf (buffer, open_fmt, indent_level, "");
2786 if (mnode->type != NODE_TYPE_ROOT)
2789 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2791 if (mnode->action_name)
2792 g_string_append_printf (buffer, " action=\"%s\"",
2793 g_quark_to_string (mnode->action_name));
2796 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2798 for (child = node->children; child != NULL; child = child->next)
2799 print_node (self, child, indent_level + 2, buffer);
2802 g_string_append_printf (buffer, close_fmt, indent_level, "");
2806 * gtk_ui_manager_get_ui:
2807 * @self: a #GtkUIManager
2809 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2811 * Return value: A newly allocated string containing an XML representation of
2817 gtk_ui_manager_get_ui (GtkUIManager *self)
2821 buffer = g_string_new (NULL);
2823 gtk_ui_manager_ensure_update (self);
2825 print_node (self, self->private_data->root_node, 0, buffer);
2827 return g_string_free (buffer, FALSE);
2832 #undef gtk_ui_manager_add_ui_from_file
2835 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2836 const gchar *filename,
2839 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2842 if (utf8_filename == NULL)
2845 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2847 g_free (utf8_filename);
2854 #define __GTK_UI_MANAGER_C__
2855 #include "gtkaliasdef.c"