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 "gtkseparatormenuitem.h"
40 #include "gtkseparatortoolitem.h"
41 #include "gtktearoffmenuitem.h"
42 #include "gtktoolbar.h"
43 #include "gtkuimanager.h"
44 #include "gtkprivate.h"
47 #undef DEBUG_UI_MANAGER
56 NODE_TYPE_MENU_PLACEHOLDER,
57 NODE_TYPE_TOOLBAR_PLACEHOLDER,
65 typedef struct _Node Node;
75 GtkWidget *extra; /* second separator for placeholders */
80 guint expand : 1; /* used for separators */
83 #define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
85 struct _GtkUIManagerPrivate
87 GtkAccelGroup *accel_group;
96 gboolean add_tearoffs;
99 #define NODE_INFO(node) ((Node *)node->data)
101 typedef struct _NodeUIReference NodeUIReference;
103 struct _NodeUIReference
109 static void gtk_ui_manager_class_init (GtkUIManagerClass *class);
110 static void gtk_ui_manager_init (GtkUIManager *self);
111 static void gtk_ui_manager_finalize (GObject *object);
112 static void gtk_ui_manager_set_property (GObject *object,
116 static void gtk_ui_manager_get_property (GObject *object,
120 static GtkWidget * gtk_ui_manager_real_get_widget (GtkUIManager *manager,
122 static GtkAction * gtk_ui_manager_real_get_action (GtkUIManager *manager,
124 static void queue_update (GtkUIManager *self);
125 static void dirty_all_nodes (GtkUIManager *self);
126 static void mark_node_dirty (GNode *node);
127 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 GObjectClass *parent_class = NULL;
165 static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
167 static GMemChunk *merge_node_chunk = NULL;
170 gtk_ui_manager_get_type (void)
172 static GtkType type = 0;
176 static const GTypeInfo type_info =
178 sizeof (GtkUIManagerClass),
179 (GBaseInitFunc) NULL,
180 (GBaseFinalizeFunc) NULL,
181 (GClassInitFunc) gtk_ui_manager_class_init,
182 (GClassFinalizeFunc) NULL,
185 sizeof (GtkUIManager),
187 (GInstanceInitFunc) gtk_ui_manager_init,
190 type = g_type_register_static (G_TYPE_OBJECT,
198 gtk_ui_manager_class_init (GtkUIManagerClass *klass)
200 GObjectClass *gobject_class;
202 parent_class = g_type_class_peek_parent (klass);
204 gobject_class = G_OBJECT_CLASS (klass);
206 if (!merge_node_chunk)
207 merge_node_chunk = g_mem_chunk_create (Node, 64,
210 gobject_class->finalize = gtk_ui_manager_finalize;
211 gobject_class->set_property = gtk_ui_manager_set_property;
212 gobject_class->get_property = gtk_ui_manager_get_property;
213 klass->get_widget = gtk_ui_manager_real_get_widget;
214 klass->get_action = gtk_ui_manager_real_get_action;
217 * GtkUIManager:add-tearoffs:
219 * The "add-tearoffs" property controls whether generated menus
220 * have tearoff menu items.
222 * Note that this only affects regular menus. Generated popup
223 * menus never have tearoff menu items.
227 g_object_class_install_property (gobject_class,
229 g_param_spec_boolean ("add-tearoffs",
230 P_("Add tearoffs to menus"),
231 P_("Whether tearoff menu items should be added to menus"),
233 GTK_PARAM_READWRITE));
235 g_object_class_install_property (gobject_class,
237 g_param_spec_string ("ui",
238 P_("Merged UI definition"),
239 P_("An XML string describing the merged UI"),
241 GTK_PARAM_READABLE));
245 * GtkUIManager::add-widget:
246 * @merge: a #GtkUIManager
247 * @widget: the added widget
249 * The add_widget signal is emitted for each generated menubar and toolbar.
250 * It is not emitted for generated popup menus, which can be obtained by
251 * gtk_ui_manager_get_widget().
255 ui_manager_signals[ADD_WIDGET] =
256 g_signal_new ("add_widget",
257 G_OBJECT_CLASS_TYPE (klass),
258 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
259 G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
261 g_cclosure_marshal_VOID__OBJECT,
266 * GtkUIManager::actions-changed:
267 * @merge: a #GtkUIManager
269 * The "actions-changed" signal is emitted whenever the set of actions
274 ui_manager_signals[ACTIONS_CHANGED] =
275 g_signal_new ("actions_changed",
276 G_OBJECT_CLASS_TYPE (klass),
277 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
278 G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),
280 g_cclosure_marshal_VOID__VOID,
284 * GtkUIManager::connect-proxy:
285 * @uimanager: the ui manager
286 * @action: the action
289 * The connect_proxy signal is emitted after connecting a proxy to
290 * an action in the group.
292 * This is intended for simple customizations for which a custom action
293 * class would be too clumsy, e.g. showing tooltips for menuitems in the
298 ui_manager_signals[CONNECT_PROXY] =
299 g_signal_new ("connect_proxy",
300 G_OBJECT_CLASS_TYPE (klass),
301 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
302 G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
304 _gtk_marshal_VOID__OBJECT_OBJECT,
310 * GtkUIManager::disconnect-proxy:
311 * @uimanager: the ui manager
312 * @action: the action
315 * The disconnect_proxy signal is emitted after disconnecting a proxy
316 * from an action in the group.
320 ui_manager_signals[DISCONNECT_PROXY] =
321 g_signal_new ("disconnect_proxy",
322 G_OBJECT_CLASS_TYPE (klass),
323 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
324 G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
326 _gtk_marshal_VOID__OBJECT_OBJECT,
332 * GtkUIManager::pre-activate:
333 * @uimanager: the ui manager
334 * @action: the action
336 * The pre_activate signal is emitted just before the @action
339 * This is intended for applications to get notification
340 * just before any action is activated.
344 ui_manager_signals[PRE_ACTIVATE] =
345 g_signal_new ("pre_activate",
346 G_OBJECT_CLASS_TYPE (klass),
347 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
348 G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
350 _gtk_marshal_VOID__OBJECT,
355 * GtkUIManager::post-activate:
356 * @uimanager: the ui manager
357 * @action: the action
359 * The post_activate signal is emitted just after the @action
362 * This is intended for applications to get notification
363 * just after any action is activated.
367 ui_manager_signals[POST_ACTIVATE] =
368 g_signal_new ("post_activate",
369 G_OBJECT_CLASS_TYPE (klass),
370 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
371 G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
373 _gtk_marshal_VOID__OBJECT,
377 klass->add_widget = NULL;
378 klass->actions_changed = NULL;
379 klass->connect_proxy = NULL;
380 klass->disconnect_proxy = NULL;
381 klass->pre_activate = NULL;
382 klass->post_activate = NULL;
384 g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
389 gtk_ui_manager_init (GtkUIManager *self)
394 self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
396 self->private_data->accel_group = gtk_accel_group_new ();
398 self->private_data->root_node = NULL;
399 self->private_data->action_groups = NULL;
401 self->private_data->last_merge_id = 0;
402 self->private_data->add_tearoffs = FALSE;
404 merge_id = gtk_ui_manager_new_merge_id (self);
405 node = get_child_node (self, NULL, "ui", 2,
406 NODE_TYPE_ROOT, TRUE, FALSE);
407 node_prepend_ui_reference (node, merge_id, 0);
411 gtk_ui_manager_finalize (GObject *object)
413 GtkUIManager *self = GTK_UI_MANAGER (object);
415 if (self->private_data->update_tag != 0)
417 g_source_remove (self->private_data->update_tag);
418 self->private_data->update_tag = 0;
421 g_node_traverse (self->private_data->root_node,
422 G_POST_ORDER, G_TRAVERSE_ALL, -1,
423 (GNodeTraverseFunc)free_node, NULL);
424 g_node_destroy (self->private_data->root_node);
425 self->private_data->root_node = NULL;
427 g_list_foreach (self->private_data->action_groups,
428 (GFunc) g_object_unref, NULL);
429 g_list_free (self->private_data->action_groups);
430 self->private_data->action_groups = NULL;
432 g_object_unref (self->private_data->accel_group);
433 self->private_data->accel_group = NULL;
435 G_OBJECT_CLASS (parent_class)->finalize (object);
439 gtk_ui_manager_set_property (GObject *object,
444 GtkUIManager *self = GTK_UI_MANAGER (object);
448 case PROP_ADD_TEAROFFS:
449 gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gtk_ui_manager_get_property (GObject *object,
463 GtkUIManager *self = GTK_UI_MANAGER (object);
467 case PROP_ADD_TEAROFFS:
468 g_value_set_boolean (value, self->private_data->add_tearoffs);
471 g_value_set_string (value, gtk_ui_manager_get_ui (self));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480 gtk_ui_manager_real_get_widget (GtkUIManager *self,
485 /* ensure that there are no pending updates before we get the
487 gtk_ui_manager_ensure_update (self);
489 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
494 return NODE_INFO (node)->proxy;
498 gtk_ui_manager_real_get_action (GtkUIManager *self,
503 /* ensure that there are no pending updates before we get
505 gtk_ui_manager_ensure_update (self);
507 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
512 return NODE_INFO (node)->action;
517 * gtk_ui_manager_new:
519 * Creates a new ui manager object.
521 * Return value: a new ui manager object.
526 gtk_ui_manager_new (void)
528 return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
533 * gtk_ui_manager_get_add_tearoffs:
534 * @self: a #GtkUIManager
536 * Returns whether menus generated by this #GtkUIManager
537 * will have tearoff menu items.
539 * Return value: whether tearoff menu items are added
544 gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
546 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
548 return self->private_data->add_tearoffs;
553 * gtk_ui_manager_set_add_tearoffs:
554 * @self: a #GtkUIManager
555 * @add_tearoffs: whether tearoff menu items are added
557 * Sets the "add_tearoffs" property, which controls whether menus
558 * generated by this #GtkUIManager will have tearoff menu items.
560 * Note that this only affects regular menus. Generated popup
561 * menus never have tearoff menu items.
566 gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
567 gboolean add_tearoffs)
569 g_return_if_fail (GTK_IS_UI_MANAGER (self));
571 add_tearoffs = add_tearoffs != FALSE;
573 if (add_tearoffs != self->private_data->add_tearoffs)
575 self->private_data->add_tearoffs = add_tearoffs;
577 dirty_all_nodes (self);
579 g_object_notify (G_OBJECT (self), "add-tearoffs");
584 cb_proxy_connect_proxy (GtkActionGroup *group,
589 g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
593 cb_proxy_disconnect_proxy (GtkActionGroup *group,
598 g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
602 cb_proxy_pre_activate (GtkActionGroup *group,
606 g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
610 cb_proxy_post_activate (GtkActionGroup *group,
614 g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
618 * gtk_ui_manager_insert_action_group:
619 * @self: a #GtkUIManager object
620 * @action_group: the action group to be inserted
621 * @pos: the position at which the group will be inserted.
623 * Inserts an action group into the list of action groups associated
624 * with @self. Actions in earlier groups hide actions with the same
625 * name in later groups.
630 gtk_ui_manager_insert_action_group (GtkUIManager *self,
631 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 g_object_ref (action_group);
640 self->private_data->action_groups =
641 g_list_insert (self->private_data->action_groups, action_group, pos);
642 g_object_connect (action_group,
643 "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
644 "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
645 "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
646 "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
649 /* dirty all nodes, as action bindings may change */
650 dirty_all_nodes (self);
652 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
656 * gtk_ui_manager_remove_action_group:
657 * @self: a #GtkUIManager object
658 * @action_group: the action group to be removed
660 * Removes an action group from the list of action groups associated
666 gtk_ui_manager_remove_action_group (GtkUIManager *self,
667 GtkActionGroup *action_group)
669 g_return_if_fail (GTK_IS_UI_MANAGER (self));
670 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
671 g_return_if_fail (g_list_find (self->private_data->action_groups,
672 action_group) != NULL);
674 self->private_data->action_groups =
675 g_list_remove (self->private_data->action_groups, action_group);
677 g_object_disconnect (action_group,
678 "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
679 "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
680 "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
681 "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
683 g_object_unref (action_group);
685 /* dirty all nodes, as action bindings may change */
686 dirty_all_nodes (self);
688 g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
692 * gtk_ui_manager_get_action_groups:
693 * @self: a #GtkUIManager object
695 * Returns the list of action groups associated with @self.
697 * Return value: a #GList of action groups. The list is owned by GTK+
698 * and should not be modified.
703 gtk_ui_manager_get_action_groups (GtkUIManager *self)
705 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
707 return self->private_data->action_groups;
711 * gtk_ui_manager_get_accel_group:
712 * @self: a #GtkUIManager object
714 * Returns the #GtkAccelGroup associated with @self.
716 * Return value: the #GtkAccelGroup.
721 gtk_ui_manager_get_accel_group (GtkUIManager *self)
723 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
725 return self->private_data->accel_group;
729 * gtk_ui_manager_get_widget:
730 * @self: a #GtkUIManager
733 * Looks up a widget by following a path.
734 * The path consists of the names specified in the XML description of the UI.
735 * separated by '/'. Elements which don't have a name or action attribute in
736 * the XML (e.g. <popup>) can be addressed by their XML element name
737 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
739 * Note that the widget found by following a path that ends in a <menu>
740 * element is the menuitem to which the menu is attached, not the menu itself.
742 * Also note that the widgets constructed by a ui manager are not tied to
743 * the lifecycle of the ui manager. If you add the widgets returned by this
744 * function to some container or explicitly ref them, they will survive the
745 * destruction of the ui manager.
747 * Return value: the widget found by following the path, or %NULL if no widget
753 gtk_ui_manager_get_widget (GtkUIManager *self,
756 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
757 g_return_val_if_fail (path != NULL, NULL);
759 return GTK_UI_MANAGER_GET_CLASS (self)->get_widget (self, path);
763 collect_toplevels (GNode *node,
767 GtkUIManagerItemType types;
771 switch (NODE_INFO (node)->type) {
772 case NODE_TYPE_MENUBAR:
773 if (data->types & GTK_UI_MANAGER_MENUBAR)
774 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
776 case NODE_TYPE_TOOLBAR:
777 if (data->types & GTK_UI_MANAGER_TOOLBAR)
778 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
780 case NODE_TYPE_POPUP:
781 if (data->types & GTK_UI_MANAGER_POPUP)
782 data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
789 * gtk_ui_manager_get_toplevels:
790 * @self: a #GtkUIManager
791 * @types: specifies the types of toplevel widgets to include. Allowed
792 * types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
793 * #GTK_UI_MANAGER_POPUP.
795 * Obtains a list of all toplevel widgets of the requested types.
797 * Return value: a newly-allocated of all toplevel widgets of the requested
803 gtk_ui_manager_get_toplevels (GtkUIManager *self,
804 GtkUIManagerItemType types)
807 GtkUIManagerItemType types;
811 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
812 g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR |
813 GTK_UI_MANAGER_TOOLBAR |
814 GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
820 g_node_children_foreach (self->private_data->root_node,
822 collect_toplevels, &data);
829 * gtk_ui_manager_get_action:
830 * @self: a #GtkUIManager
833 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
834 * for more information about paths.
836 * Return value: the action whose proxy widget is found by following the path,
837 * or %NULL if no widget was found.
842 gtk_ui_manager_get_action (GtkUIManager *self,
845 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
846 g_return_val_if_fail (path != NULL, NULL);
848 return GTK_UI_MANAGER_GET_CLASS (self)->get_action (self, path);
852 get_child_node (GtkUIManager *self,
854 const gchar *childname,
855 gint childname_length,
862 g_return_val_if_fail (parent == NULL ||
863 (NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
864 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM),
871 for (child = parent->children; child != NULL; child = child->next)
873 if (NODE_INFO (child)->name &&
874 strlen (NODE_INFO (child)->name) == childname_length &&
875 !strncmp (NODE_INFO (child)->name, childname, childname_length))
877 /* if undecided about node type, set it */
878 if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
879 NODE_INFO (child)->type = node_type;
881 /* warn about type mismatch */
882 if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
883 node_type != NODE_TYPE_UNDECIDED &&
884 NODE_INFO (child)->type != node_type)
885 g_warning ("node type doesn't match %d (%s is type %d)",
887 NODE_INFO (child)->name,
888 NODE_INFO (child)->type);
894 if (!child && create)
898 mnode = g_chunk_new0 (Node, merge_node_chunk);
899 mnode->type = node_type;
900 mnode->name = g_strndup (childname, childname_length);
903 child = g_node_prepend_data (parent, mnode);
905 child = g_node_append_data (parent, mnode);
907 mark_node_dirty (child);
912 /* handle root node */
913 if (self->private_data->root_node)
915 child = self->private_data->root_node;
916 if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
917 g_warning ("root node name '%s' doesn't match '%s'",
918 childname, NODE_INFO (child)->name);
919 if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
920 g_warning ("base element must be of type ROOT");
926 mnode = g_chunk_new0 (Node, merge_node_chunk);
927 mnode->type = node_type;
928 mnode->name = g_strndup (childname, childname_length);
931 child = self->private_data->root_node = g_node_new (mnode);
939 get_node (GtkUIManager *self,
944 const gchar *pos, *end;
945 GNode *parent, *node;
947 if (strncmp ("/ui", path, 3) == 0)
950 end = path + strlen (path);
952 parent = node = NULL;
958 slash = strchr (pos, '/');
960 length = slash - pos;
962 length = strlen (pos);
964 node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
969 pos += length + 1; /* move past the node name and the slash too */
973 if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
974 NODE_INFO (node)->type = node_type;
980 free_node (GNode *node)
982 Node *info = NODE_INFO (node);
984 g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
985 g_list_free (info->uifiles);
988 g_object_unref (info->action);
990 g_object_unref (info->proxy);
992 g_object_unref (info->extra);
994 g_chunk_free (info, merge_node_chunk);
1000 * gtk_ui_manager_new_merge_id:
1001 * @self: a #GtkUIManager
1003 * Returns an unused merge id, suitable for use with
1004 * gtk_ui_manager_add_ui().
1006 * Return value: an unused merge id.
1011 gtk_ui_manager_new_merge_id (GtkUIManager *self)
1013 self->private_data->last_merge_id++;
1015 return self->private_data->last_merge_id;
1019 node_prepend_ui_reference (GNode *gnode,
1021 GQuark action_quark)
1023 Node *node = NODE_INFO (gnode);
1024 NodeUIReference *reference = NULL;
1026 if (node->uifiles &&
1027 ((NodeUIReference *)node->uifiles->data)->merge_id == merge_id)
1028 reference = node->uifiles->data;
1031 reference = g_new (NodeUIReference, 1);
1032 node->uifiles = g_list_prepend (node->uifiles, reference);
1035 reference->merge_id = merge_id;
1036 reference->action_quark = action_quark;
1038 mark_node_dirty (gnode);
1042 node_remove_ui_reference (GNode *gnode,
1045 Node *node = NODE_INFO (gnode);
1048 for (p = node->uifiles; p != NULL; p = p->next)
1050 NodeUIReference *reference = p->data;
1052 if (reference->merge_id == merge_id)
1054 if (p == node->uifiles)
1055 mark_node_dirty (gnode);
1056 node->uifiles = g_list_delete_link (node->uifiles, p);
1064 /* -------------------- The UI file parser -------------------- */
1078 typedef struct _ParseContext ParseContext;
1079 struct _ParseContext
1082 ParseState prev_state;
1092 start_element_handler (GMarkupParseContext *context,
1093 const gchar *element_name,
1094 const gchar **attribute_names,
1095 const gchar **attribute_values,
1099 ParseContext *ctx = user_data;
1100 GtkUIManager *self = ctx->self;
1103 const gchar *node_name;
1104 const gchar *action;
1105 GQuark action_quark;
1107 gboolean expand = FALSE;
1109 gboolean raise_error = TRUE;
1116 for (i = 0; attribute_names[i] != NULL; i++)
1118 if (!strcmp (attribute_names[i], "name"))
1120 node_name = attribute_values[i];
1122 else if (!strcmp (attribute_names[i], "action"))
1124 action = attribute_values[i];
1125 action_quark = g_quark_from_string (attribute_values[i]);
1127 else if (!strcmp (attribute_names[i], "position"))
1129 top = !strcmp (attribute_values[i], "top");
1131 else if (!strcmp (attribute_names[i], "expand"))
1133 expand = !strcmp (attribute_values[i], "true");
1137 gint line_number, char_number;
1139 g_markup_parse_context_get_position (context,
1140 &line_number, &char_number);
1143 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
1144 _("Unknown attribute '%s' on line %d char %d"),
1146 line_number, char_number);
1151 /* Work out a name for this node. Either the name attribute, or
1152 * the action, or the element name */
1153 if (node_name == NULL)
1158 node_name = element_name;
1161 switch (element_name[0])
1164 if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
1166 ctx->state = STATE_ACCELERATOR;
1167 ctx->current = get_child_node (self, ctx->current,
1168 node_name, strlen (node_name),
1169 NODE_TYPE_ACCELERATOR,
1171 if (NODE_INFO (ctx->current)->action_name == 0)
1172 NODE_INFO (ctx->current)->action_name = action_quark;
1174 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1176 raise_error = FALSE;
1180 if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1182 ctx->state = STATE_ROOT;
1183 ctx->current = self->private_data->root_node;
1184 raise_error = FALSE;
1186 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1190 if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1192 ctx->state = STATE_MENU;
1193 ctx->current = get_child_node (self, ctx->current,
1194 node_name, strlen (node_name),
1197 if (NODE_INFO (ctx->current)->action_name == 0)
1198 NODE_INFO (ctx->current)->action_name = action_quark;
1200 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1201 mark_node_dirty (ctx->current);
1203 raise_error = FALSE;
1205 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
1207 ctx->current = get_child_node (self, ctx->current,
1208 node_name, strlen (node_name),
1211 if (NODE_INFO (ctx->current)->action_name == 0)
1212 NODE_INFO (ctx->current)->action_name = action_quark;
1214 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1216 raise_error = FALSE;
1218 else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
1222 ctx->state = STATE_MENUITEM;
1223 node = get_child_node (self, ctx->current,
1224 node_name, strlen (node_name),
1227 if (NODE_INFO (node)->action_name == 0)
1228 NODE_INFO (node)->action_name = action_quark;
1230 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1232 raise_error = FALSE;
1236 if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1238 ctx->state = STATE_MENU;
1239 ctx->current = get_child_node (self, ctx->current,
1240 node_name, strlen (node_name),
1243 if (NODE_INFO (ctx->current)->action_name == 0)
1244 NODE_INFO (ctx->current)->action_name = action_quark;
1246 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1248 raise_error = FALSE;
1250 else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1251 !strcmp (element_name, "placeholder"))
1253 if (ctx->state == STATE_TOOLBAR)
1254 ctx->current = get_child_node (self, ctx->current,
1255 node_name, strlen (node_name),
1256 NODE_TYPE_TOOLBAR_PLACEHOLDER,
1259 ctx->current = get_child_node (self, ctx->current,
1260 node_name, strlen (node_name),
1261 NODE_TYPE_MENU_PLACEHOLDER,
1264 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1266 raise_error = FALSE;
1270 if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1271 !strcmp (element_name, "separator"))
1276 if (ctx->state == STATE_TOOLBAR)
1277 ctx->state = STATE_TOOLITEM;
1279 ctx->state = STATE_MENUITEM;
1280 if (!strcmp (node_name, "separator"))
1286 length = strlen (node_name);
1287 node = get_child_node (self, ctx->current,
1289 NODE_TYPE_SEPARATOR,
1292 NODE_INFO (node)->expand = expand;
1294 if (NODE_INFO (node)->action_name == 0)
1295 NODE_INFO (node)->action_name = action_quark;
1297 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1299 raise_error = FALSE;
1303 if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
1305 ctx->state = STATE_TOOLBAR;
1306 ctx->current = get_child_node (self, ctx->current,
1307 node_name, strlen (node_name),
1310 if (NODE_INFO (ctx->current)->action_name == 0)
1311 NODE_INFO (ctx->current)->action_name = action_quark;
1313 node_prepend_ui_reference (ctx->current, ctx->merge_id, action_quark);
1315 raise_error = FALSE;
1317 else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1321 ctx->state = STATE_TOOLITEM;
1322 node = get_child_node (self, ctx->current,
1323 node_name, strlen (node_name),
1326 if (NODE_INFO (node)->action_name == 0)
1327 NODE_INFO (node)->action_name = action_quark;
1329 node_prepend_ui_reference (node, ctx->merge_id, action_quark);
1331 raise_error = FALSE;
1339 gint line_number, char_number;
1341 g_markup_parse_context_get_position (context,
1342 &line_number, &char_number);
1345 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1346 _("Unexpected start tag '%s' on line %d char %d"),
1348 line_number, char_number);
1353 end_element_handler (GMarkupParseContext *context,
1354 const gchar *element_name,
1358 ParseContext *ctx = user_data;
1364 /* no need to GError here, GMarkup already catches this */
1367 ctx->current = NULL;
1368 ctx->state = STATE_END;
1372 case STATE_ACCELERATOR:
1373 ctx->current = ctx->current->parent;
1374 if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT)
1375 ctx->state = STATE_ROOT;
1376 /* else, stay in same state */
1378 case STATE_MENUITEM:
1379 ctx->state = STATE_MENU;
1381 case STATE_TOOLITEM:
1382 ctx->state = STATE_TOOLBAR;
1388 cleanup (GMarkupParseContext *context,
1392 ParseContext *ctx = user_data;
1394 ctx->current = NULL;
1395 /* should also walk through the tree and get rid of nodes related to
1396 * this UI file's tag */
1398 gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1402 xml_isspace (char c)
1404 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
1408 text_handler (GMarkupParseContext *context,
1418 end = text + text_len;
1419 while (p != end && xml_isspace (*p))
1424 gint line_number, char_number;
1426 g_markup_parse_context_get_position (context,
1427 &line_number, &char_number);
1430 G_MARKUP_ERROR_INVALID_CONTENT,
1431 _("Unexpected character data on line %d char %d"),
1432 line_number, char_number);
1437 static GMarkupParser ui_parser = {
1438 start_element_handler,
1439 end_element_handler,
1446 add_ui_from_string (GtkUIManager *self,
1447 const gchar *buffer,
1449 gboolean needs_root,
1452 ParseContext ctx = { 0 };
1453 GMarkupParseContext *context;
1455 ctx.state = STATE_START;
1458 ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1460 context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);
1463 if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1466 if (!g_markup_parse_context_parse (context, buffer, length, error))
1470 if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1473 if (!g_markup_parse_context_end_parse (context, error))
1476 g_markup_parse_context_free (context);
1478 queue_update (self);
1480 g_object_notify (G_OBJECT (self), "ui");
1482 return ctx.merge_id;
1486 g_markup_parse_context_free (context);
1492 * gtk_ui_manager_add_ui_from_string:
1493 * @self: a #GtkUIManager object
1494 * @buffer: the string to parse
1495 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1496 * @error: return location for an error
1498 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and
1499 * merges it with the current contents of @self. An enclosing <ui>
1500 * element is added if it is missing.
1502 * Return value: The merge id for the merged UI. The merge id can be used
1503 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1504 * the return value is 0.
1509 gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1510 const gchar *buffer,
1514 gboolean needs_root = TRUE;
1518 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1519 g_return_val_if_fail (buffer != NULL, 0);
1522 length = strlen (buffer);
1525 end = buffer + length;
1526 while (p != end && xml_isspace (*p))
1529 if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
1532 return add_ui_from_string (self, buffer, length, needs_root, error);
1536 * gtk_ui_manager_add_ui_from_file:
1537 * @self: a #GtkUIManager object
1538 * @filename: the name of the file to parse
1539 * @error: return location for an error
1541 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and
1542 * merges it with the current contents of @self.
1544 * Return value: The merge id for the merged UI. The merge id can be used
1545 * to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1546 * the return value is 0.
1551 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1552 const gchar *filename,
1559 g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
1561 if (!g_file_get_contents (filename, &buffer, &length, error))
1564 res = add_ui_from_string (self, buffer, length, FALSE, error);
1571 * gtk_ui_manager_add_ui:
1572 * @self: a #GtkUIManager
1573 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
1575 * @name: the name for the added UI element
1576 * @action: the name of the action to be proxied, or %NULL to add a separator
1577 * @type: the type of UI element to add.
1578 * @top: if %TRUE, the UI element is added before its siblings, otherwise it
1579 * is added after its siblings.
1581 * Adds a UI element to the current contents of @self.
1583 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
1584 * separator if such an element can be inserted at the place determined by
1585 * @path. Otherwise @type must indicate an element that can be inserted at
1586 * the place determined by @path.
1591 gtk_ui_manager_add_ui (GtkUIManager *self,
1595 const gchar *action,
1596 GtkUIManagerItemType type,
1602 GQuark action_quark = 0;
1604 g_return_if_fail (GTK_IS_UI_MANAGER (self));
1605 g_return_if_fail (merge_id > 0);
1606 g_return_if_fail (name != NULL);
1608 node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1613 node_type = NODE_TYPE_UNDECIDED;
1615 switch (NODE_INFO (node)->type)
1617 case NODE_TYPE_MENUBAR:
1618 case NODE_TYPE_MENU:
1619 case NODE_TYPE_POPUP:
1620 case NODE_TYPE_MENU_PLACEHOLDER:
1623 case GTK_UI_MANAGER_AUTO:
1625 node_type = NODE_TYPE_MENUITEM;
1627 node_type = NODE_TYPE_SEPARATOR;
1629 case GTK_UI_MANAGER_MENU:
1630 node_type = NODE_TYPE_MENU;
1632 case GTK_UI_MANAGER_MENUITEM:
1633 node_type = NODE_TYPE_MENUITEM;
1635 case GTK_UI_MANAGER_SEPARATOR:
1636 node_type = NODE_TYPE_SEPARATOR;
1638 case GTK_UI_MANAGER_PLACEHOLDER:
1639 node_type = NODE_TYPE_MENU_PLACEHOLDER;
1645 case NODE_TYPE_TOOLBAR:
1646 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1649 case GTK_UI_MANAGER_AUTO:
1651 node_type = NODE_TYPE_TOOLITEM;
1653 node_type = NODE_TYPE_SEPARATOR;
1655 case GTK_UI_MANAGER_TOOLITEM:
1656 node_type = NODE_TYPE_TOOLITEM;
1658 case GTK_UI_MANAGER_SEPARATOR:
1659 node_type = NODE_TYPE_SEPARATOR;
1661 case GTK_UI_MANAGER_PLACEHOLDER:
1662 node_type = NODE_TYPE_TOOLBAR_PLACEHOLDER;
1668 case NODE_TYPE_ROOT:
1671 case GTK_UI_MANAGER_MENUBAR:
1672 node_type = NODE_TYPE_MENUBAR;
1674 case GTK_UI_MANAGER_TOOLBAR:
1675 node_type = NODE_TYPE_TOOLBAR;
1677 case GTK_UI_MANAGER_POPUP:
1678 node_type = NODE_TYPE_POPUP;
1680 case GTK_UI_MANAGER_ACCELERATOR:
1681 node_type = NODE_TYPE_ACCELERATOR;
1691 if (node_type == NODE_TYPE_UNDECIDED)
1694 child = get_child_node (self, node,
1695 name, strlen (name),
1696 node_type, TRUE, top);
1699 action_quark = g_quark_from_string (action);
1701 node_prepend_ui_reference (child, merge_id, action_quark);
1703 if (NODE_INFO (child)->action_name == 0)
1704 NODE_INFO (child)->action_name = action_quark;
1706 queue_update (self);
1708 g_object_notify (G_OBJECT (self), "ui");
1712 remove_ui (GNode *node,
1715 guint merge_id = GPOINTER_TO_UINT (user_data);
1717 node_remove_ui_reference (node, merge_id);
1719 return FALSE; /* continue */
1723 * gtk_ui_manager_remove_ui:
1724 * @self: a #GtkUIManager object
1725 * @merge_id: a merge id as returned by gtk_ui_manager_add_ui_from_string()
1727 * Unmerges the part of @self<!-- -->s content identified by @merge_id.
1732 gtk_ui_manager_remove_ui (GtkUIManager *self,
1735 g_node_traverse (self->private_data->root_node,
1736 G_POST_ORDER, G_TRAVERSE_ALL, -1,
1737 remove_ui, GUINT_TO_POINTER (merge_id));
1739 queue_update (self);
1741 g_object_notify (G_OBJECT (self), "ui");
1744 /* -------------------- Updates -------------------- */
1748 get_action_by_name (GtkUIManager *merge,
1749 const gchar *action_name)
1757 for (tmp = merge->private_data->action_groups; tmp != NULL; tmp = tmp->next)
1759 GtkActionGroup *action_group = tmp->data;
1762 action = gtk_action_group_get_action (action_group, action_name);
1772 find_menu_position (GNode *node,
1773 GtkWidget **menushell_p,
1776 GtkWidget *menushell;
1779 g_return_val_if_fail (node != NULL, FALSE);
1780 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_MENU ||
1781 NODE_INFO (node)->type == NODE_TYPE_POPUP ||
1782 NODE_INFO (node)->type == NODE_TYPE_MENU_PLACEHOLDER ||
1783 NODE_INFO (node)->type == NODE_TYPE_MENUITEM ||
1784 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1787 /* first sibling -- look at parent */
1788 if (node->prev == NULL)
1793 parent = node->parent;
1794 switch (NODE_INFO (parent)->type)
1796 case NODE_TYPE_MENUBAR:
1797 case NODE_TYPE_POPUP:
1798 menushell = NODE_INFO (parent)->proxy;
1801 case NODE_TYPE_MENU:
1802 menushell = NODE_INFO (parent)->proxy;
1803 if (GTK_IS_MENU_ITEM (menushell))
1804 menushell = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menushell));
1805 siblings = gtk_container_get_children (GTK_CONTAINER (menushell));
1806 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
1810 g_list_free (siblings);
1812 case NODE_TYPE_MENU_PLACEHOLDER:
1813 menushell = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1814 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1815 pos = g_list_index (GTK_MENU_SHELL (menushell)->children,
1816 NODE_INFO (parent)->proxy) + 1;
1819 g_warning("%s: bad parent node type %d", G_STRLOC,
1820 NODE_INFO (parent)->type);
1826 GtkWidget *prev_child;
1829 sibling = node->prev;
1830 if (NODE_INFO (sibling)->type == NODE_TYPE_MENU_PLACEHOLDER)
1831 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1833 prev_child = NODE_INFO (sibling)->proxy;
1835 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1836 menushell = gtk_widget_get_parent (prev_child);
1837 g_return_val_if_fail (GTK_IS_MENU_SHELL (menushell), FALSE);
1839 pos = g_list_index (GTK_MENU_SHELL (menushell)->children, prev_child) + 1;
1843 *menushell_p = menushell;
1851 find_toolbar_position (GNode *node,
1852 GtkWidget **toolbar_p,
1858 g_return_val_if_fail (node != NULL, FALSE);
1859 g_return_val_if_fail (NODE_INFO (node)->type == NODE_TYPE_TOOLBAR ||
1860 NODE_INFO (node)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER ||
1861 NODE_INFO (node)->type == NODE_TYPE_TOOLITEM ||
1862 NODE_INFO (node)->type == NODE_TYPE_SEPARATOR,
1865 /* first sibling -- look at parent */
1866 if (node->prev == NULL)
1870 parent = node->parent;
1871 switch (NODE_INFO (parent)->type)
1873 case NODE_TYPE_TOOLBAR:
1874 toolbar = NODE_INFO (parent)->proxy;
1877 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1878 toolbar = gtk_widget_get_parent (NODE_INFO (parent)->proxy);
1879 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1880 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1881 GTK_TOOL_ITEM (NODE_INFO (parent)->proxy)) + 1;
1884 g_warning ("%s: bad parent node type %d", G_STRLOC,
1885 NODE_INFO (parent)->type);
1891 GtkWidget *prev_child;
1894 sibling = node->prev;
1895 if (NODE_INFO (sibling)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
1896 prev_child = NODE_INFO (sibling)->extra; /* second Separator */
1898 prev_child = NODE_INFO (sibling)->proxy;
1900 g_return_val_if_fail (GTK_IS_WIDGET (prev_child), FALSE);
1901 toolbar = gtk_widget_get_parent (prev_child);
1902 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1904 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar),
1905 GTK_TOOL_ITEM (prev_child)) + 1;
1909 *toolbar_p = toolbar;
1917 * _gtk_menu_is_empty:
1918 * @menu: a #GtkMenu or %NULL
1920 * Determines whether @menu is empty. A menu is considered empty if it
1921 * the only visible children are tearoff menu items or "filler" menu
1922 * items which were inserted to mark the menu as empty.
1924 * This function is used by #GtkAction.
1926 * Return value: whether @menu is empty.
1929 _gtk_menu_is_empty (GtkWidget *menu)
1931 GList *children, *cur;
1933 g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
1938 children = gtk_container_get_children (GTK_CONTAINER (menu));
1943 if (GTK_WIDGET_VISIBLE (cur->data))
1945 if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
1946 !g_object_get_data (cur->data, "gtk-empty-menu-item"))
1951 g_list_free (children);
1957 SEPARATOR_MODE_SMART,
1958 SEPARATOR_MODE_VISIBLE,
1959 SEPARATOR_MODE_HIDDEN
1962 void _gtk_action_sync_menu_visible (GtkAction *action,
1967 update_smart_separators (GtkWidget *proxy)
1969 GtkWidget *parent = NULL;
1971 if (GTK_IS_MENU (proxy) || GTK_IS_TOOLBAR (proxy))
1973 else if (GTK_IS_MENU_ITEM (proxy) || GTK_IS_TOOL_ITEM (proxy))
1974 parent = gtk_widget_get_parent (proxy);
1980 GList *children, *cur, *last;
1984 children = gtk_container_get_children (GTK_CONTAINER (parent));
1995 if (g_object_get_data (cur->data, "gtk-empty-menu-item"))
1999 else if (GTK_IS_SEPARATOR_MENU_ITEM (cur->data) ||
2000 GTK_IS_SEPARATOR_TOOL_ITEM (cur->data))
2003 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cur->data),
2004 "gtk-separator-mode"));
2007 case SEPARATOR_MODE_VISIBLE:
2008 gtk_widget_show (GTK_WIDGET (cur->data));
2012 case SEPARATOR_MODE_HIDDEN:
2013 gtk_widget_hide (GTK_WIDGET (cur->data));
2015 case SEPARATOR_MODE_SMART:
2018 gtk_widget_show (GTK_WIDGET (cur->data));
2023 gtk_widget_hide (GTK_WIDGET (cur->data));
2027 else if (GTK_WIDGET_VISIBLE (cur->data))
2030 if (GTK_IS_TEAROFF_MENU_ITEM (cur->data) || cur->data == filler)
2043 gtk_widget_hide (GTK_WIDGET (last->data));
2045 if (GTK_IS_MENU (parent))
2049 item = gtk_menu_get_attach_widget (GTK_MENU (parent));
2050 if (GTK_IS_MENU_ITEM (item))
2051 _gtk_action_sync_menu_visible (NULL, item, empty);
2052 if (GTK_IS_WIDGET (filler))
2055 gtk_widget_show (filler);
2057 gtk_widget_hide (filler);
2061 g_list_free (children);
2066 update_node (GtkUIManager *self,
2074 const gchar *action_name;
2075 NodeUIReference *ref;
2077 #ifdef DEBUG_UI_MANAGER
2081 g_return_if_fail (node != NULL);
2082 g_return_if_fail (NODE_INFO (node) != NULL);
2084 info = NODE_INFO (node);
2089 in_popup = in_popup || (info->type == NODE_TYPE_POPUP);
2091 #ifdef DEBUG_UI_MANAGER
2092 g_print ("update_node name=%s dirty=%d popup %d (",
2093 info->name, info->dirty, in_popup);
2094 for (tmp = info->uifiles; tmp != NULL; tmp = tmp->next)
2096 NodeUIReference *ref = tmp->data;
2097 g_print("%s:%u", g_quark_to_string (ref->action_quark), ref->merge_id);
2104 if (info->uifiles == NULL) {
2105 /* We may need to remove this node.
2106 * This must be done in post order
2108 goto recurse_children;
2111 ref = info->uifiles->data;
2112 action_name = g_quark_to_string (ref->action_quark);
2113 action = get_action_by_name (self, action_name);
2115 info->dirty = FALSE;
2117 /* Check if the node doesn't have an action and must have an action */
2118 if (action == NULL &&
2119 info->type != NODE_TYPE_ROOT &&
2120 info->type != NODE_TYPE_MENUBAR &&
2121 info->type != NODE_TYPE_TOOLBAR &&
2122 info->type != NODE_TYPE_POPUP &&
2123 info->type != NODE_TYPE_SEPARATOR &&
2124 info->type != NODE_TYPE_MENU_PLACEHOLDER &&
2125 info->type != NODE_TYPE_TOOLBAR_PLACEHOLDER)
2127 g_warning ("%s: missing action", info->name);
2129 goto recurse_children;
2133 gtk_action_set_accel_group (action, self->private_data->accel_group);
2135 /* If the widget already has a proxy and the action hasn't changed, then
2136 * we only have to update the tearoff menu items.
2138 if (info->proxy != NULL && action == info->action)
2140 if (info->type == NODE_TYPE_MENU)
2145 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2146 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2147 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2149 if (self->private_data->add_tearoffs && !in_popup)
2150 gtk_widget_show (GTK_WIDGET (siblings->data));
2152 gtk_widget_hide (GTK_WIDGET (siblings->data));
2154 g_list_free (siblings);
2157 goto recurse_children;
2162 case NODE_TYPE_MENUBAR:
2163 if (info->proxy == NULL)
2165 info->proxy = gtk_menu_bar_new ();
2166 g_object_ref (info->proxy);
2167 gtk_object_sink (GTK_OBJECT (info->proxy));
2168 gtk_widget_set_name (info->proxy, info->name);
2169 gtk_widget_show (info->proxy);
2170 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2173 case NODE_TYPE_POPUP:
2174 if (info->proxy == NULL)
2176 info->proxy = gtk_menu_new ();
2177 g_object_ref (info->proxy);
2178 gtk_object_sink (GTK_OBJECT (info->proxy));
2180 gtk_widget_set_name (info->proxy, info->name);
2182 case NODE_TYPE_MENU:
2184 GtkWidget *prev_submenu = NULL;
2187 /* remove the proxy if it is of the wrong type ... */
2189 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2191 prev_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2194 g_object_ref (prev_submenu);
2195 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2197 gtk_action_disconnect_proxy (info->action, info->proxy);
2198 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2200 g_object_unref (info->proxy);
2203 /* create proxy if needed ... */
2204 if (info->proxy == NULL)
2206 GtkWidget *menushell;
2209 if (find_menu_position (node, &menushell, &pos))
2214 info->proxy = gtk_action_create_menu_item (action);
2215 g_object_ref (info->proxy);
2216 gtk_object_sink (GTK_OBJECT (info->proxy));
2217 menu = gtk_menu_new ();
2218 gtk_widget_set_name (info->proxy, info->name);
2219 gtk_widget_set_name (menu, info->name);
2220 tearoff = gtk_tearoff_menu_item_new ();
2221 gtk_widget_set_no_show_all (tearoff, TRUE);
2222 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2223 filler = gtk_menu_item_new_with_label (_("Empty"));
2224 g_object_set_data (G_OBJECT (filler),
2225 "gtk-empty-menu-item",
2226 GINT_TO_POINTER (TRUE));
2227 gtk_widget_set_sensitive (filler, FALSE);
2228 gtk_widget_set_no_show_all (filler, TRUE);
2229 gtk_menu_shell_append (GTK_MENU_SHELL (menu), filler);
2230 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), menu);
2231 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell), info->proxy, pos);
2232 g_signal_connect (info->proxy, "notify::visible",
2233 G_CALLBACK (update_smart_separators), NULL);
2237 gtk_action_connect_proxy (action, info->proxy);
2241 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy),
2243 g_object_unref (prev_submenu);
2245 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy));
2246 siblings = gtk_container_get_children (GTK_CONTAINER (menu));
2247 if (siblings != NULL && GTK_IS_TEAROFF_MENU_ITEM (siblings->data))
2249 if (self->private_data->add_tearoffs && !in_popup)
2250 gtk_widget_show (GTK_WIDGET (siblings->data));
2252 gtk_widget_hide (GTK_WIDGET (siblings->data));
2254 g_list_free (siblings);
2257 case NODE_TYPE_UNDECIDED:
2258 g_warning ("found undecided node!");
2260 case NODE_TYPE_ROOT:
2262 case NODE_TYPE_TOOLBAR:
2263 if (info->proxy == NULL)
2265 info->proxy = gtk_toolbar_new ();
2266 g_object_ref (info->proxy);
2267 gtk_object_sink (GTK_OBJECT (info->proxy));
2268 gtk_widget_set_name (info->proxy, info->name);
2269 gtk_widget_show (info->proxy);
2270 g_signal_emit (self, ui_manager_signals[ADD_WIDGET], 0, info->proxy);
2273 case NODE_TYPE_MENU_PLACEHOLDER:
2274 /* create menu items for placeholders if necessary ... */
2275 if (!GTK_IS_SEPARATOR_MENU_ITEM (info->proxy) ||
2276 !GTK_IS_SEPARATOR_MENU_ITEM (info->extra))
2280 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2282 g_object_unref (info->proxy);
2287 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2289 g_object_unref (info->extra);
2293 if (info->proxy == NULL)
2295 GtkWidget *menushell;
2298 if (find_menu_position (node, &menushell, &pos))
2300 info->proxy = gtk_separator_menu_item_new ();
2301 g_object_ref (info->proxy);
2302 gtk_object_sink (GTK_OBJECT (info->proxy));
2303 g_object_set_data (G_OBJECT (info->proxy),
2304 "gtk-separator-mode",
2305 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2306 gtk_widget_set_no_show_all (info->proxy, TRUE);
2307 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2308 NODE_INFO (node)->proxy, pos);
2310 info->extra = gtk_separator_menu_item_new ();
2311 g_object_ref (info->extra);
2312 gtk_object_sink (GTK_OBJECT (info->extra));
2313 g_object_set_data (G_OBJECT (info->extra),
2314 "gtk-separator-mode",
2315 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2316 gtk_widget_set_no_show_all (info->extra, TRUE);
2317 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2318 NODE_INFO (node)->extra, pos+1);
2322 case NODE_TYPE_TOOLBAR_PLACEHOLDER:
2323 /* create toolbar items for placeholders if necessary ... */
2324 if (!GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy) ||
2325 !GTK_IS_SEPARATOR_TOOL_ITEM (info->extra))
2329 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2331 g_object_unref (info->proxy);
2336 gtk_container_remove (GTK_CONTAINER (info->extra->parent),
2338 g_object_unref (info->extra);
2342 if (info->proxy == NULL)
2347 if (find_toolbar_position (node, &toolbar, &pos))
2351 item = gtk_separator_tool_item_new ();
2352 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2353 info->proxy = GTK_WIDGET (item);
2354 g_object_ref (info->proxy);
2355 gtk_object_sink (GTK_OBJECT (info->proxy));
2356 g_object_set_data (G_OBJECT (info->proxy),
2357 "gtk-separator-mode",
2358 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2359 gtk_widget_set_no_show_all (info->proxy, TRUE);
2361 item = gtk_separator_tool_item_new ();
2362 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos+1);
2363 info->extra = GTK_WIDGET (item);
2364 g_object_ref (info->extra);
2365 gtk_object_sink (GTK_OBJECT (info->extra));
2366 g_object_set_data (G_OBJECT (info->extra),
2367 "gtk-separator-mode",
2368 GINT_TO_POINTER (SEPARATOR_MODE_HIDDEN));
2369 gtk_widget_set_no_show_all (info->extra, TRUE);
2373 case NODE_TYPE_MENUITEM:
2374 /* remove the proxy if it is of the wrong type ... */
2376 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->menu_item_type)
2378 g_signal_handlers_disconnect_by_func (info->proxy,
2379 G_CALLBACK (update_smart_separators),
2381 gtk_action_disconnect_proxy (info->action, info->proxy);
2382 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2384 g_object_unref (info->proxy);
2387 /* create proxy if needed ... */
2388 if (info->proxy == NULL)
2390 GtkWidget *menushell;
2393 if (find_menu_position (node, &menushell, &pos))
2395 info->proxy = gtk_action_create_menu_item (action);
2396 g_object_ref (info->proxy);
2397 gtk_object_sink (GTK_OBJECT (info->proxy));
2398 gtk_widget_set_name (info->proxy, info->name);
2400 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2406 g_signal_handlers_disconnect_by_func (info->proxy,
2407 G_CALLBACK (update_smart_separators),
2409 gtk_menu_item_set_submenu (GTK_MENU_ITEM (info->proxy), NULL);
2410 gtk_action_connect_proxy (action, info->proxy);
2412 g_signal_connect (info->proxy, "notify::visible",
2413 G_CALLBACK (update_smart_separators), NULL);
2416 /* don't show accels in popups */
2417 GtkWidget *label = GTK_BIN (info->proxy)->child;
2418 g_object_set (label,
2419 "accel_closure", NULL,
2424 case NODE_TYPE_TOOLITEM:
2425 /* remove the proxy if it is of the wrong type ... */
2427 G_OBJECT_TYPE (info->proxy) != GTK_ACTION_GET_CLASS (action)->toolbar_item_type)
2429 g_signal_handlers_disconnect_by_func (info->proxy,
2430 G_CALLBACK (update_smart_separators),
2432 gtk_action_disconnect_proxy (info->action, info->proxy);
2433 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2435 g_object_unref (info->proxy);
2438 /* create proxy if needed ... */
2439 if (info->proxy == NULL)
2444 if (find_toolbar_position (node, &toolbar, &pos))
2446 info->proxy = gtk_action_create_tool_item (action);
2447 g_object_ref (info->proxy);
2448 gtk_object_sink (GTK_OBJECT (info->proxy));
2449 gtk_widget_set_name (info->proxy, info->name);
2451 gtk_toolbar_insert (GTK_TOOLBAR (toolbar),
2452 GTK_TOOL_ITEM (info->proxy), pos);
2454 /* FIXME: we must trigger the notify::tooltip handler, since
2455 * tooltips on toolitems can't be set before the toolitem
2456 * is added to the toolbar.
2458 g_object_get (action, "tooltip", &tooltip, NULL);
2459 g_object_set (action, "tooltip", tooltip, NULL);
2465 g_signal_handlers_disconnect_by_func (info->proxy,
2466 G_CALLBACK (update_smart_separators),
2468 gtk_action_connect_proxy (action, info->proxy);
2470 g_signal_connect (info->proxy, "notify::visible",
2471 G_CALLBACK (update_smart_separators), NULL);
2473 case NODE_TYPE_SEPARATOR:
2474 if (NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR ||
2475 NODE_INFO (node->parent)->type == NODE_TYPE_TOOLBAR_PLACEHOLDER)
2479 gint separator_mode;
2481 if (GTK_IS_SEPARATOR_TOOL_ITEM (info->proxy))
2483 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2485 g_object_unref (info->proxy);
2489 if (find_toolbar_position (node, &toolbar, &pos))
2491 GtkToolItem *item = gtk_separator_tool_item_new ();
2492 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, pos);
2493 info->proxy = GTK_WIDGET (item);
2494 g_object_ref (info->proxy);
2495 gtk_object_sink (GTK_OBJECT (info->proxy));
2496 gtk_widget_set_no_show_all (info->proxy, TRUE);
2499 gtk_tool_item_set_expand (GTK_TOOL_ITEM (item), TRUE);
2500 gtk_separator_tool_item_set_draw
2501 (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
2502 separator_mode = SEPARATOR_MODE_VISIBLE;
2505 separator_mode = SEPARATOR_MODE_SMART;
2507 g_object_set_data (G_OBJECT (info->proxy),
2508 "gtk-separator-mode",
2509 GINT_TO_POINTER (separator_mode));
2510 gtk_widget_show (info->proxy);
2515 GtkWidget *menushell;
2518 if (GTK_IS_SEPARATOR_MENU_ITEM (info->proxy))
2520 gtk_container_remove (GTK_CONTAINER (info->proxy->parent),
2522 g_object_unref (info->proxy);
2526 if (find_menu_position (node, &menushell, &pos))
2528 info->proxy = gtk_separator_menu_item_new ();
2529 g_object_ref (info->proxy);
2530 gtk_object_sink (GTK_OBJECT (info->proxy));
2531 gtk_widget_set_no_show_all (info->proxy, TRUE);
2532 g_object_set_data (G_OBJECT (info->proxy),
2533 "gtk-separator-mode",
2534 GINT_TO_POINTER (SEPARATOR_MODE_SMART));
2535 gtk_menu_shell_insert (GTK_MENU_SHELL (menushell),
2537 gtk_widget_show (info->proxy);
2541 case NODE_TYPE_ACCELERATOR:
2542 gtk_action_connect_accelerator (action);
2547 g_object_ref (action);
2549 g_object_unref (info->action);
2550 info->action = action;
2553 /* process children */
2554 child = node->children;
2560 child = current->next;
2561 update_node (self, current, in_popup);
2566 if (info->type == NODE_TYPE_MENU)
2567 update_smart_separators (gtk_menu_item_get_submenu (GTK_MENU_ITEM (info->proxy)));
2568 else if (info->type == NODE_TYPE_TOOLBAR || info->type == NODE_TYPE_POPUP)
2569 update_smart_separators (info->proxy);
2572 /* handle cleanup of dead nodes */
2573 if (node->children == NULL && info->uifiles == NULL)
2576 gtk_widget_destroy (info->proxy);
2578 gtk_widget_destroy (info->extra);
2579 if (info->type == NODE_TYPE_ACCELERATOR)
2580 gtk_action_disconnect_accelerator (info->action);
2582 g_node_destroy (node);
2587 do_updates (GtkUIManager *self)
2589 /* this function needs to check through the tree for dirty nodes.
2590 * For such nodes, it needs to do the following:
2592 * 1) check if they are referenced by any loaded UI files anymore.
2593 * In which case, the proxy widget should be destroyed, unless
2594 * there are any subnodes.
2596 * 2) lookup the action for this node again. If it is different to
2597 * the current one (or if no previous action has been looked up),
2598 * the proxy is reconnected to the new action (or a new proxy widget
2599 * is created and added to the parent container).
2601 update_node (self, self->private_data->root_node, FALSE);
2603 self->private_data->update_tag = 0;
2609 do_updates_idle (GtkUIManager *self)
2611 GDK_THREADS_ENTER ();
2613 GDK_THREADS_LEAVE ();
2619 queue_update (GtkUIManager *self)
2621 if (self->private_data->update_tag != 0)
2624 self->private_data->update_tag = g_idle_add ((GSourceFunc)do_updates_idle,
2630 * gtk_ui_manager_ensure_update:
2631 * @self: a #GtkUIManager
2633 * Makes sure that all pending updates to the UI have been completed.
2635 * This may occasionally be necessary, since #GtkUIManager updates the
2636 * UI in an idle function. A typical example where this function is
2637 * useful is to enforce that the menubar and toolbar have been added to
2638 * the main window before showing it:
2641 * gtk_container_add (GTK_CONTAINER (window), vbox);
2642 * g_signal_connect (merge, "add_widget",
2643 * G_CALLBACK (add_widget), vbox);
2644 * gtk_ui_manager_add_ui_from_file (merge, "my-menus");
2645 * gtk_ui_manager_add_ui_from_file (merge, "my-toolbars");
2646 * gtk_ui_manager_ensure_update (merge);
2647 * gtk_widget_show (window);
2649 * </informalexample>
2654 gtk_ui_manager_ensure_update (GtkUIManager *self)
2656 if (self->private_data->update_tag != 0)
2658 g_source_remove (self->private_data->update_tag);
2664 dirty_traverse_func (GNode *node,
2667 NODE_INFO (node)->dirty = TRUE;
2672 dirty_all_nodes (GtkUIManager *self)
2674 g_node_traverse (self->private_data->root_node,
2675 G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2676 dirty_traverse_func, NULL);
2677 queue_update (self);
2681 mark_node_dirty (GNode *node)
2685 /* FIXME could optimize this */
2686 for (p = node; p; p = p->parent)
2687 NODE_INFO (p)->dirty = TRUE;
2690 static const gchar *const open_tag_format[] = {
2705 static const gchar *const close_tag_format[] = {
2706 "%*s</UNDECIDED>\n",
2711 "%*s</placeholder>\n",
2712 "%*s</placeholder>\n",
2721 print_node (GtkUIManager *self,
2731 g_string_append_printf (buffer, open_tag_format[mnode->type],
2734 if (mnode->type != NODE_TYPE_ROOT)
2737 g_string_append_printf (buffer, " name=\"%s\"", mnode->name);
2739 if (mnode->action_name)
2740 g_string_append_printf (buffer, " action=\"%s\"",
2741 g_quark_to_string (mnode->action_name));
2744 g_string_append (buffer,
2745 close_tag_format[mnode->type] ? ">\n" : "/>\n");
2747 for (child = node->children; child != NULL; child = child->next)
2748 print_node (self, child, indent_level + 2, buffer);
2750 if (close_tag_format[mnode->type])
2751 g_string_append_printf (buffer, close_tag_format[mnode->type],
2756 * gtk_ui_manager_get_ui:
2757 * @self: a #GtkUIManager
2759 * Creates a <link linkend="XML-UI">UI definition</link> of the merged UI.
2761 * Return value: A newly allocated string containing an XML representation of
2767 gtk_ui_manager_get_ui (GtkUIManager *self)
2771 buffer = g_string_new (NULL);
2773 gtk_ui_manager_ensure_update (self);
2775 print_node (self, self->private_data->root_node, 0, buffer);
2777 return g_string_free (buffer, FALSE);
2782 #undef gtk_ui_manager_add_ui_from_file
2785 gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
2786 const gchar *filename,
2789 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2792 if (utf8_filename == NULL)
2795 retval = gtk_ui_manager_add_ui_from_file_utf8 (self, utf8_filename, error);
2797 g_free (utf8_filename);
2804 #define __GTK_UI_MANAGER_C__
2805 #include "gtkaliasdef.c"