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;
1944 gboolean result = TRUE;
1946 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1951 children = gtk_container_get_children (GTK_CONTAINER (menu));
1956 if (GTK_WIDGET_VISIBLE (cur->data))
1958 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1959 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1967 g_list_free (children);
1973 SEPARATOR_MODE_SMART,
1974 SEPARATOR_MODE_VISIBLE,
1975 SEPARATOR_MODE_HIDDEN
1979 update_smart_separators (GtkWidget *proxy)
1981 GtkWidget *parent = NULL;
1983 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1985 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1986 parent = gtk_widget_get_parent (proxy);
1992 GList *children, *cur, *last;
1995 children = gtk_container_get_children (GTK_CONTAINER (parent));
2005 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
2009 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2010 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2013 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2014 "gtk-separator-mode"));
2017 case SEPARATOR_MODE_VISIBLE:
2018 gtk_widget_show (GTK_WIDGET (cur->data));
2022 case SEPARATOR_MODE_HIDDEN:
2023 gtk_widget_hide (GTK_WIDGET (cur->data));
2025 case SEPARATOR_MODE_SMART:
2028 gtk_widget_show (GTK_WIDGET (cur->data));
2033 gtk_widget_hide (GTK_WIDGET (cur->data));
2037 else if (GTK_WIDGET_VISIBLE (cur->data))
2040 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2053 gtk_widget_hide (GTK_WIDGET (last->data));
2055 if (GTK_IS_MENU (parent))
2059 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2060 if (GTK_IS_MENU_ITEM (item))
2061 _gtk_action_sync_menu_visible (NULL, item, empty);
2062 if (GTK_IS_WIDGET (filler))
2065 gtk_widget_show (filler);
2067 gtk_widget_hide (filler);
2071 g_list_free (children);
2076 update_node (GtkUIManager *self,
2083 const gchar *action_name;
2084 NodeUIReference *ref;
2086 #ifdef DEBUG_UI_MANAGER
2090 g_return_if_fail (node != NULL);
2091 g_return_if_fail (NODE_INFO (node) != NULL);
2093 info = NODE_INFO (node);
2098 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2100 #ifdef DEBUG_UI_MANAGER
2101 g_print ("update_node name=%s dirty=%d popup %d (",
2102 info->name, info->dirty, in_popup);
2103 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2105 NodeUIReference *ref = tmp->data;
2106 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2113 if (info->uifiles == NULL) {
2114 /* We may need to remove this node.
2115 * This must be done in post order
2117 goto recurse_children;
2120 ref = info->uifiles->data;
2121 action_name = g_quark_to_string (ref->action_quark);
2122 action = get_action_by_name (self, action_name);
2124 info->dirty = FALSE;
2126 /* Check if the node doesn't have an action and must have an action */
2127 if (action == NULL &&
2128 info->type != NODE_TYPE_ROOT &&
2129 info->type != NODE_TYPE_MENUBAR &&
2130 info->type != NODE_TYPE_TOOLBAR &&
2131 info->type != NODE_TYPE_POPUP &&
2132 info->type != NODE_TYPE_SEPARATOR &&
2133 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2134 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2136 g_warning ("%s: missing action", info->name);
2138 goto recurse_children;
2142 gtk_action_set_accel_group (action, self->private_data->accel_group);
2144 /* If the widget already has a proxy and the action hasn't changed, then
2145 * we only have to update the tearoff menu items.
2147 if (info->proxy != NULL && action == info->action)
2149 if (info->type == NODE_TYPE_MENU)
2154 if (GTK_IS_MENU (info->proxy))
2157 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2158 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2159 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2161 if (self->private_data->add_tearoffs && !in_popup)
2162 gtk_widget_show (GTK_WIDGET (siblings->data));
2164 gtk_widget_hide (GTK_WIDGET (siblings->data));
2166 g_list_free (siblings);
2169 goto recurse_children;
2174 case NODE_TYPE_MENUBAR:
2175 if (info->proxy == NULL)
2177 info->proxy = gtk_menu_bar_new ();
2178 g_object_ref_sink (info->proxy);
2179 gtk_widget_set_name (info->proxy, info->name);
2180 gtk_widget_show (info->proxy);
2181 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2184 case NODE_TYPE_POPUP:
2185 if (info->proxy == NULL)
2187 info->proxy = gtk_menu_new ();
2188 g_object_ref_sink (info->proxy);
2190 gtk_widget_set_name (info->proxy, info->name);
2192 case NODE_TYPE_MENU:
2194 GtkWidget *prev_submenu = NULL;
2197 /* remove the proxy if it is of the wrong type ... */
2199 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2201 if (GTK_IS_MENU_ITEM (info->proxy))
2203 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2206 g_object_ref (prev_submenu);
2207 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2211 gtk_action_disconnect_proxy (info->action, info->proxy);
2212 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2214 g_object_unref (info->proxy);
2217 /* create proxy if needed ... */
2218 if (info->proxy == NULL)
2223 menu = gtk_menu_new ();
2224 gtk_widget_set_name (menu, info->name);
2225 tearoff = gtk_tearoff_menu_item_new ();
2226 gtk_widget_set_no_show_all (tearoff, TRUE);
2227 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2228 filler = gtk_menu_item_new_with_label (_("Empty"));
2229 g_object_set_data (G_OBJECT (filler),
2230 I_("gtk-empty-menu-item"),
2231 GINT_TO_POINTER (TRUE));
2232 gtk_widget_set_sensitive (filler, FALSE);
2233 gtk_widget_set_no_show_all (filler, TRUE);
2234 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2236 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLITEM)
2239 g_object_ref_sink (info->proxy);
2240 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (NODE_INFO (node->parent)->proxy),
2245 GtkWidget *menushell;
2248 find_menu_position (node, &menushell, &pos);
2249 info->proxy = gtk_action_create_menu_item (action);
2250 g_object_ref_sink (info->proxy);
2251 g_signal_connect (info->proxy, "notify::visible",
2252 G_CALLBACK (update_smart_separators), NULL);
2253 gtk_widget_set_name (info->proxy, info->name);
2255 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2256 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2260 gtk_action_connect_proxy (action, info->proxy);
2264 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2266 g_object_unref (prev_submenu);
2269 if (GTK_IS_MENU (info->proxy))
2272 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2273 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2274 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2276 if (self->private_data->add_tearoffs && !in_popup)
2277 gtk_widget_show (GTK_WIDGET (siblings->data));
2279 gtk_widget_hide (GTK_WIDGET (siblings->data));
2281 g_list_free (siblings);
2284 case NODE_TYPE_UNDECIDED:
2285 g_warning ("found undecided node!");
2287 case NODE_TYPE_ROOT:
2289 case NODE_TYPE_TOOLBAR:
2290 if (info->proxy == NULL)
2292 info->proxy = gtk_toolbar_new ();
2293 g_object_ref_sink (info->proxy);
2294 gtk_widget_set_name (info->proxy, info->name);
2295 gtk_widget_show (info->proxy);
2296 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2299 case NODE_TYPE_MENU_PLACEHOLDER:
2300 /* create menu items for placeholders if necessary ... */
2301 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2302 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2306 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2308 g_object_unref (info->proxy);
2313 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2315 g_object_unref (info->extra);
2319 if (info->proxy == NULL)
2321 GtkWidget *menushell;
2324 find_menu_position (node, &menushell, &pos);
2325 info->proxy = gtk_separator_menu_item_new ();
2326 g_object_ref_sink (info->proxy);
2327 g_object_set_data (G_OBJECT (info->proxy),
2328 I_("gtk-separator-mode"),
2329 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2330 gtk_widget_set_no_show_all (info->proxy, TRUE);
2331 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2332 NODE_INFO (node)->proxy, pos);
2334 info->extra = gtk_separator_menu_item_new ();
2335 g_object_ref_sink (info->extra);
2336 g_object_set_data (G_OBJECT (info->extra),
2337 I_("gtk-separator-mode"),
2338 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2339 gtk_widget_set_no_show_all (info->extra, TRUE);
2340 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2341 NODE_INFO (node)->extra, pos+1);
2344 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2345 /* create toolbar items for placeholders if necessary ... */
2346 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2347 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2351 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2353 g_object_unref (info->proxy);
2358 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2360 g_object_unref (info->extra);
2364 if (info->proxy == NULL)
2370 find_toolbar_position (node, &toolbar, &pos);
2371 item = gtk_separator_tool_item_new ();
2372 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2373 info->proxy = GTK_WIDGET (item);
2374 g_object_ref_sink (info->proxy);
2375 g_object_set_data (G_OBJECT (info->proxy),
2376 I_("gtk-separator-mode"),
2377 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2378 gtk_widget_set_no_show_all (info->proxy, TRUE);
2380 item = gtk_separator_tool_item_new ();
2381 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2382 info->extra = GTK_WIDGET (item);
2383 g_object_ref_sink (info->extra);
2384 g_object_set_data (G_OBJECT (info->extra),
2385 I_("gtk-separator-mode"),
2386 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2387 gtk_widget_set_no_show_all (info->extra, TRUE);
2390 case NODE_TYPE_MENUITEM:
2391 /* remove the proxy if it is of the wrong type ... */
2393 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2395 g_signal_handlers_disconnect_by_func (info->proxy,
2396 G_CALLBACK (update_smart_separators),
2398 gtk_action_disconnect_proxy (info->action, info->proxy);
2399 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2401 g_object_unref (info->proxy);
2404 /* create proxy if needed ... */
2405 if (info->proxy == NULL)
2407 GtkWidget *menushell;
2410 find_menu_position (node, &menushell, &pos);
2411 info->proxy = gtk_action_create_menu_item (action);
2412 g_object_ref_sink (info->proxy);
2413 gtk_widget_set_name (info->proxy, info->name);
2415 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2420 g_signal_handlers_disconnect_by_func (info->proxy,
2421 G_CALLBACK (update_smart_separators),
2423 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2424 gtk_action_connect_proxy (action, info->proxy);
2426 g_signal_connect (info->proxy, "notify::visible",
2427 G_CALLBACK (update_smart_separators), NULL);
2430 /* don't show accels in popups */
2431 GtkWidget *label = GTK_BIN (info->proxy)->child;
2432 g_object_set (label,
2433 "accel-closure", NULL,
2438 case NODE_TYPE_TOOLITEM:
2439 /* remove the proxy if it is of the wrong type ... */
2441 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2443 g_signal_handlers_disconnect_by_func (info->proxy,
2444 G_CALLBACK (update_smart_separators),
2446 gtk_action_disconnect_proxy (info->action, info->proxy);
2447 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2449 g_object_unref (info->proxy);
2452 /* create proxy if needed ... */
2453 if (info->proxy == NULL)
2458 find_toolbar_position (node, &toolbar, &pos);
2459 info->proxy = gtk_action_create_tool_item (action);
2460 g_object_ref_sink (info->proxy);
2461 gtk_widget_set_name (info->proxy, info->name);
2463 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2464 GTK_TOOL_ITEM (info->proxy), pos);
2468 g_signal_handlers_disconnect_by_func (info->proxy,
2469 G_CALLBACK (update_smart_separators),
2471 gtk_action_connect_proxy (action, info->proxy);
2474 /* FIXME: we must re-set the tooltip, since tooltips on toolitems can't be
2475 * set before the toolitem is added to the toolbar.
2480 g_object_get (G_OBJECT (action), "tooltip", &tooltip, NULL);
2481 g_object_set (G_OBJECT (action), "tooltip", tooltip, NULL);
2485 g_signal_connect (info->proxy, "notify::visible",
2486 G_CALLBACK (update_smart_separators), NULL);
2488 case NODE_TYPE_SEPARATOR:
2489 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2490 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2494 gint separator_mode;
2497 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2499 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2501 g_object_unref (info->proxy);
2505 find_toolbar_position (node, &toolbar, &pos);
2506 item = gtk_separator_tool_item_new ();
2507 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2508 info->proxy = GTK_WIDGET (item);
2509 g_object_ref_sink (info->proxy);
2510 gtk_widget_set_no_show_all (info->proxy, TRUE);
2513 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2514 gtk_separator_tool_item_set_draw
2515 (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2516 separator_mode = SEPARATOR_MODE_VISIBLE;
2519 separator_mode = SEPARATOR_MODE_SMART;
2521 g_object_set_data (G_OBJECT (info->proxy),
2522 I_("gtk-separator-mode"),
2523 GINT_TO_POINTER (separator_mode));
2524 gtk_widget_show (info->proxy);
2528 GtkWidget *menushell;
2531 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2533 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2535 g_object_unref (info->proxy);
2539 find_menu_position (node, &menushell, &pos);
2540 info->proxy = gtk_separator_menu_item_new ();
2541 g_object_ref_sink (info->proxy);
2542 gtk_widget_set_no_show_all (info->proxy, TRUE);
2543 g_object_set_data (G_OBJECT (info->proxy),
2544 I_("gtk-separator-mode"),
2545 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2546 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2548 gtk_widget_show (info->proxy);
2551 case NODE_TYPE_ACCELERATOR:
2552 gtk_action_connect_accelerator (action);
2557 g_object_ref (action);
2559 g_object_unref (info->action);
2560 info->action = action;
2563 /* process children */
2564 child = node->children;
2570 child = current->next;
2571 update_node (self, current, in_popup);
2576 if (info->type == NODE_TYPE_MENU && GTK_IS_MENU_ITEM (info->proxy))
2577 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2578 else if (info->type == NODE_TYPE_MENU ||
2579 info->type == NODE_TYPE_TOOLBAR ||
2580 info->type == NODE_TYPE_POPUP)
2581 update_smart_separators (info->proxy);
2584 /* handle cleanup of dead nodes */
2585 if (node->children == NULL && info->uifiles == NULL)
2588 gtk_widget_destroy (info->proxy);
2590 gtk_widget_destroy (info->extra);
2591 if (info->type == NODE_TYPE_ACCELERATOR)
2592 gtk_action_disconnect_accelerator (info->action);
2594 g_node_destroy (node);
2599 do_updates (GtkUIManager *self)
2601 /* this function needs to check through the tree for dirty nodes.
2602 * For such nodes, it needs to do the following:
2604 * 1) check if they are referenced by any loaded UI files anymore.
2605 * In which case, the proxy widget should be destroyed, unless
2606 * there are any subnodes.
2608 * 2) lookup the action for this node again. If it is different to
2609 * the current one (or if no previous action has been looked up),
2610 * the proxy is reconnected to the new action (or a new proxy widget
2611 * is created and added to the parent container).
2613 update_node (self, self->private_data->root_node, FALSE);
2615 self->private_data->update_tag = 0;
2621 do_updates_idle (GtkUIManager *self)
2629 queue_update (GtkUIManager *self)
2631 if (self->private_data->update_tag != 0)
2634 self->private_data->update_tag = gdk_threads_add_idle (
2635 (GSourceFunc)do_updates_idle,
2641 * gtk_ui_manager_ensure_update:
2642 * @self: a #GtkUIManager
2644 * Makes sure that all pending updates to the UI have been completed.
2646 * This may occasionally be necessary, since #GtkUIManager updates the
2647 * UI in an idle function. A typical example where this function is
2648 * useful is to enforce that the menubar and toolbar have been added to
2649 * the main window before showing it:
2652 * gtk_container_add (GTK_CONTAINER (window), vbox);
2653 * g_signal_connect (merge, "add_widget",
2654 * G_CALLBACK (add_widget), vbox);
2655 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2656 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2657 * gtk_ui_manager_ensure_update (merge);
2658 * gtk_widget_show (window);
2660 * </informalexample>
2665 gtk_ui_manager_ensure_update (GtkUIManager *self)
2667 if (self->private_data->update_tag != 0)
2669 g_source_remove (self->private_data->update_tag);
2675 dirty_traverse_func (GNode *node,
2678 NODE_INFO (node)->dirty = TRUE;
2683 dirty_all_nodes (GtkUIManager *self)
2685 g_node_traverse (self->private_data->root_node,
2686 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2687 dirty_traverse_func, NULL);
2688 queue_update (self);
2692 mark_node_dirty (GNode *node)
2696 /* FIXME could optimize this */
2697 for (p = node; p; p = p->parent)
2698 NODE_INFO (p)->dirty = TRUE;
2701 static const gchar *
2702 open_tag_format (NodeType type)
2706 case NODE_TYPE_UNDECIDED: return "%*s<UNDECIDED";
2707 case NODE_TYPE_ROOT: return "%*s<ui";
2708 case NODE_TYPE_MENUBAR: return "%*s<menubar";
2709 case NODE_TYPE_MENU: return "%*s<menu";
2710 case NODE_TYPE_TOOLBAR: return "%*s<toolbar";
2711 case NODE_TYPE_MENU_PLACEHOLDER:
2712 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s<placeholder";
2713 case NODE_TYPE_POPUP: return "%*s<popup";
2714 case NODE_TYPE_MENUITEM: return "%*s<menuitem";
2715 case NODE_TYPE_TOOLITEM: return "%*s<toolitem";
2716 case NODE_TYPE_SEPARATOR: return "%*s<separator";
2717 case NODE_TYPE_ACCELERATOR: return "%*s<accelerator";
2718 default: return NULL;
2722 static const gchar *
2723 close_tag_format (NodeType type)
2727 case NODE_TYPE_UNDECIDED: return "%*s</UNDECIDED>\n";
2728 case NODE_TYPE_ROOT: return "%*s</ui>\n";
2729 case NODE_TYPE_MENUBAR: return "%*s</menubar>\n";
2730 case NODE_TYPE_MENU: return "%*s</menu>\n";
2731 case NODE_TYPE_TOOLBAR: return "%*s</toolbar>\n";
2732 case NODE_TYPE_MENU_PLACEHOLDER:
2733 case NODE_TYPE_TOOLBAR_PLACEHOLDER: return "%*s</placeholder>\n";
2734 case NODE_TYPE_POPUP: return "%*s</popup>\n";
2735 default: return NULL;
2740 print_node (GtkUIManager *self,
2747 const gchar *open_fmt;
2748 const gchar *close_fmt;
2752 open_fmt = open_tag_format (mnode->type);
2753 close_fmt = close_tag_format (mnode->type);
2755 g_string_append_printf (buffer, open_fmt, indent_level, "");
2757 if (mnode->type != NODE_TYPE_ROOT)
2760 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2762 if (mnode->action_name)
2763 g_string_append_printf (buffer, " action=\"%s\"",
2764 g_quark_to_string (mnode->action_name));
2767 g_string_append (buffer, close_fmt ? ">\n" : "/>\n");
2769 for (child = node->children; child != NULL; child = child->next)
2770 print_node (self, child, indent_level + 2, buffer);
2773 g_string_append_printf (buffer, close_fmt, indent_level, "");
2777 * gtk_ui_manager_get_ui:
2778 * @self: a #GtkUIManager
2780 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2782 * Return value: A newly allocated string containing an XML representation of
2788 gtk_ui_manager_get_ui (GtkUIManager *self)
2792 buffer = g_string_new (NULL);
2794 gtk_ui_manager_ensure_update (self);
2796 print_node (self, self->private_data->root_node, 0, buffer);
2798 return g_string_free (buffer, FALSE);
2803 #undef gtk_ui_manager_add_ui_from_file
2806 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2807 const gchar *filename,
2810 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2813 if (utf8_filename == NULL)
2816 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2818 g_free (utf8_filename);
2825 #define __GTK_UI_MANAGER_C__
2826 #include "gtkaliasdef.c"