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/.
33 #include "gtkaction.h"
34 #include "gtkactiongroup.h"
35 #include "gtkaccellabel.h"
36 #include "gtkbutton.h"
38 #include "gtkimagemenuitem.h"
41 #include "gtkmarshalers.h"
42 #include "gtkmenuitem.h"
44 #include "gtktearoffmenuitem.h"
45 #include "gtktoolbutton.h"
46 #include "gtktoolbar.h"
49 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
51 struct _GtkActionPrivate
57 gchar *stock_id; /* icon */
61 guint label_set : 1; /* these two used so we can set label */
62 guint short_label_set : 1; /* based on stock id */
63 guint visible_horizontal : 1;
64 guint visible_vertical : 1;
65 guint is_important : 1;
66 guint hide_if_empty : 1;
70 GtkAccelGroup *accel_group;
71 GClosure *accel_closure;
74 GtkActionGroup *action_group;
76 /* list of proxy widgets */
94 PROP_VISIBLE_HORIZONTAL,
95 PROP_VISIBLE_VERTICAL,
103 static void gtk_action_init (GtkAction *action);
104 static void gtk_action_class_init (GtkActionClass *class);
106 static GQuark accel_path_id = 0;
107 static const gchar *accel_path_key = "GtkAction::accel_path";
110 gtk_action_get_type (void)
112 static GtkType type = 0;
116 static const GTypeInfo type_info =
118 sizeof (GtkActionClass),
119 (GBaseInitFunc) NULL,
120 (GBaseFinalizeFunc) NULL,
121 (GClassInitFunc) gtk_action_class_init,
122 (GClassFinalizeFunc) NULL,
127 (GInstanceInitFunc) gtk_action_init,
130 type = g_type_register_static (G_TYPE_OBJECT,
137 static void gtk_action_finalize (GObject *object);
138 static void gtk_action_set_property (GObject *object,
142 static void gtk_action_get_property (GObject *object,
146 static void gtk_action_set_action_group (GtkAction *action,
147 GtkActionGroup *action_group);
149 static GtkWidget *create_menu_item (GtkAction *action);
150 static GtkWidget *create_tool_item (GtkAction *action);
151 static void connect_proxy (GtkAction *action,
153 static void disconnect_proxy (GtkAction *action,
155 static void closure_accel_activate (GClosure *closure,
156 GValue *return_value,
157 guint n_param_values,
158 const GValue *param_values,
159 gpointer invocation_hint,
160 gpointer marshal_data);
162 static GObjectClass *parent_class = NULL;
163 static guint action_signals[LAST_SIGNAL] = { 0 };
167 gtk_action_class_init (GtkActionClass *klass)
169 GObjectClass *gobject_class;
171 accel_path_id = g_quark_from_static_string (accel_path_key);
173 parent_class = g_type_class_peek_parent (klass);
174 gobject_class = G_OBJECT_CLASS (klass);
176 gobject_class->finalize = gtk_action_finalize;
177 gobject_class->set_property = gtk_action_set_property;
178 gobject_class->get_property = gtk_action_get_property;
180 klass->activate = NULL;
182 klass->create_menu_item = create_menu_item;
183 klass->create_tool_item = create_tool_item;
184 klass->connect_proxy = connect_proxy;
185 klass->disconnect_proxy = disconnect_proxy;
187 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
188 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
190 g_object_class_install_property (gobject_class,
192 g_param_spec_string ("name",
194 P_("A unique name for the action."),
197 G_PARAM_CONSTRUCT_ONLY));
198 g_object_class_install_property (gobject_class,
200 g_param_spec_string ("label",
202 P_("The label used for menu items and buttons that activate this action."),
205 g_object_class_install_property (gobject_class,
207 g_param_spec_string ("short_label",
209 P_("A shorter label that may be used on toolbar buttons."),
212 g_object_class_install_property (gobject_class,
214 g_param_spec_string ("tooltip",
216 P_("A tooltip for this action."),
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("stock_id",
223 P_("The stock icon displayed in widgets representing this action."),
226 g_object_class_install_property (gobject_class,
227 PROP_VISIBLE_HORIZONTAL,
228 g_param_spec_boolean ("visible_horizontal",
229 P_("Visible when horizontal"),
230 P_("Whether the toolbar item is visible when the toolbar is in a horizontal orientation."),
233 g_object_class_install_property (gobject_class,
234 PROP_VISIBLE_VERTICAL,
235 g_param_spec_boolean ("visible_vertical",
236 P_("Visible when vertical"),
237 P_("Whether the toolbar item is visible when the toolbar is in a vertical orientation."),
240 g_object_class_install_property (gobject_class,
242 g_param_spec_boolean ("is_important",
244 P_("Whether the action is considered important. When TRUE, toolitem proxies for this action show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
247 g_object_class_install_property (gobject_class,
249 g_param_spec_boolean ("hide_if_empty",
251 P_("When TRUE, empty menu proxies for this action are hidden."),
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("sensitive",
258 P_("Whether the action is enabled."),
261 g_object_class_install_property (gobject_class,
263 g_param_spec_boolean ("visible",
265 P_("Whether the action is visible."),
268 g_object_class_install_property (gobject_class,
270 g_param_spec_object ("action_group",
272 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
273 GTK_TYPE_ACTION_GROUP,
277 * GtkAction::activate:
278 * @action: the #GtkAction
280 * The "activate" signal is emitted when the action is activated.
284 action_signals[ACTIVATE] =
285 g_signal_new ("activate",
286 G_OBJECT_CLASS_TYPE (klass),
287 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
288 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
289 g_cclosure_marshal_VOID__VOID,
292 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
297 gtk_action_init (GtkAction *action)
299 action->private_data = GTK_ACTION_GET_PRIVATE (action);
301 action->private_data->name = NULL;
302 action->private_data->label = NULL;
303 action->private_data->short_label = NULL;
304 action->private_data->tooltip = NULL;
305 action->private_data->stock_id = NULL;
306 action->private_data->visible_horizontal = TRUE;
307 action->private_data->visible_vertical = TRUE;
308 action->private_data->is_important = FALSE;
309 action->private_data->hide_if_empty = TRUE;
311 action->private_data->sensitive = TRUE;
312 action->private_data->visible = TRUE;
314 action->private_data->label_set = FALSE;
315 action->private_data->short_label_set = FALSE;
317 action->private_data->accel_count = 0;
318 action->private_data->accel_closure =
319 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
320 g_closure_set_marshal (action->private_data->accel_closure,
321 closure_accel_activate);
322 g_closure_ref (action->private_data->accel_closure);
323 g_closure_sink (action->private_data->accel_closure);
325 action->private_data->accel_quark = 0;
326 action->private_data->accel_count = 0;
327 action->private_data->accel_group = NULL;
329 action->private_data->action_group = NULL;
331 action->private_data->proxies = NULL;
336 * @name: A unique name for the action
337 * @label: the label displayed in menu items and on buttons
338 * @tooltip: a tooltip for the action
339 * @stock_id: the stock icon to display in widgets representing the action
341 * Creates a new #GtkAction object. To add the action to a
342 * #GtkActionGroup and set the accelerator for the action,
343 * call gtk_action_group_add_action_with_accel().
345 * Return value: a new #GtkAction
350 gtk_action_new (const gchar *name,
352 const gchar *tooltip,
353 const gchar *stock_id)
357 action = g_object_new (GTK_TYPE_ACTION,
361 "stock_id", stock_id,
368 gtk_action_finalize (GObject *object)
371 action = GTK_ACTION (object);
373 g_free (action->private_data->name);
374 g_free (action->private_data->label);
375 g_free (action->private_data->short_label);
376 g_free (action->private_data->tooltip);
377 g_free (action->private_data->stock_id);
379 g_closure_unref (action->private_data->accel_closure);
380 if (action->private_data->accel_group)
381 g_object_unref (action->private_data->accel_group);
383 G_OBJECT_CLASS (parent_class)->finalize (object);
387 gtk_action_set_property (GObject *object,
395 action = GTK_ACTION (object);
400 tmp = action->private_data->name;
401 action->private_data->name = g_value_dup_string (value);
405 tmp = action->private_data->label;
406 action->private_data->label = g_value_dup_string (value);
408 action->private_data->label_set = (action->private_data->label != NULL);
409 /* if label is unset, then use the label from the stock item */
410 if (!action->private_data->label_set && action->private_data->stock_id)
412 GtkStockItem stock_item;
414 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
415 action->private_data->label = g_strdup (stock_item.label);
417 /* if short_label is unset, set short_label=label */
418 if (!action->private_data->short_label_set)
420 tmp = action->private_data->short_label;
421 action->private_data->short_label = g_strdup (action->private_data->label);
423 g_object_notify (object, "short_label");
426 case PROP_SHORT_LABEL:
427 tmp = action->private_data->short_label;
428 action->private_data->short_label = g_value_dup_string (value);
430 action->private_data->short_label_set = (action->private_data->short_label != NULL);
431 /* if short_label is unset, then use the value of label */
432 if (!action->private_data->short_label_set)
434 action->private_data->short_label = g_strdup (action->private_data->label);
438 tmp = action->private_data->tooltip;
439 action->private_data->tooltip = g_value_dup_string (value);
443 tmp = action->private_data->stock_id;
444 action->private_data->stock_id = g_value_dup_string (value);
446 /* update label and short_label if appropriate */
447 if (!action->private_data->label_set)
449 GtkStockItem stock_item;
451 g_free (action->private_data->label);
452 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
453 action->private_data->label = g_strdup (stock_item.label);
455 action->private_data->label = NULL;
456 g_object_notify (object, "label");
458 if (!action->private_data->short_label_set)
460 tmp = action->private_data->short_label;
461 action->private_data->short_label = g_strdup (action->private_data->label);
463 g_object_notify (object, "short_label");
466 case PROP_VISIBLE_HORIZONTAL:
467 action->private_data->visible_horizontal = g_value_get_boolean (value);
469 case PROP_VISIBLE_VERTICAL:
470 action->private_data->visible_vertical = g_value_get_boolean (value);
472 case PROP_IS_IMPORTANT:
473 action->private_data->is_important = g_value_get_boolean (value);
475 case PROP_HIDE_IF_EMPTY:
476 action->private_data->hide_if_empty = g_value_get_boolean (value);
479 action->private_data->sensitive = g_value_get_boolean (value);
482 action->private_data->visible = g_value_get_boolean (value);
484 case PROP_ACTION_GROUP:
485 gtk_action_set_action_group (action, g_value_get_object (value));
488 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
494 gtk_action_get_property (GObject *object,
501 action = GTK_ACTION (object);
506 g_value_set_string (value, action->private_data->name);
509 g_value_set_string (value, action->private_data->label);
511 case PROP_SHORT_LABEL:
512 g_value_set_string (value, action->private_data->short_label);
515 g_value_set_string (value, action->private_data->tooltip);
518 g_value_set_string (value, action->private_data->stock_id);
520 case PROP_VISIBLE_HORIZONTAL:
521 g_value_set_boolean (value, action->private_data->visible_horizontal);
523 case PROP_VISIBLE_VERTICAL:
524 g_value_set_boolean (value, action->private_data->visible_vertical);
526 case PROP_IS_IMPORTANT:
527 g_value_set_boolean (value, action->private_data->is_important);
529 case PROP_HIDE_IF_EMPTY:
530 g_value_set_boolean (value, action->private_data->hide_if_empty);
533 g_value_set_boolean (value, action->private_data->sensitive);
536 g_value_set_boolean (value, action->private_data->visible);
538 case PROP_ACTION_GROUP:
539 g_value_set_object (value, action->private_data->action_group);
542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548 create_menu_item (GtkAction *action)
550 GType menu_item_type;
552 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
554 return g_object_new (menu_item_type, NULL);
558 create_tool_item (GtkAction *action)
560 GType toolbar_item_type;
562 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
564 return g_object_new (toolbar_item_type, NULL);
568 remove_proxy (GtkWidget *proxy,
571 if (GTK_IS_MENU_ITEM (proxy))
572 gtk_action_disconnect_accelerator (action);
574 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
578 gtk_action_sync_sensitivity (GtkAction *action,
582 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
586 gtk_action_sync_property (GtkAction *action,
590 const gchar *property;
591 GValue value = { 0, };
593 property = g_param_spec_get_name (pspec);
595 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
596 g_object_get_property (G_OBJECT (action), property, &value);
598 g_object_set_property (G_OBJECT (proxy), property, &value);
599 g_value_unset (&value);
603 * _gtk_action_sync_menu_visible:
604 * @action: a #GtkAction, or %NULL to determine the action from @proxy
605 * @proxy: a proxy menu item
606 * @empty: whether the submenu attached to @proxy is empty
608 * Updates the visibility of @proxy from the visibility of @action
609 * according to the following rules:
611 * <listitem><para>if @action is invisible, @proxy is too
613 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
614 * property of @action indicates otherwise
618 * This function is used in the implementation of #GtkUIManager.
621 _gtk_action_sync_menu_visible (GtkAction *action,
625 gboolean visible, hide_if_empty;
627 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
628 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
631 action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
633 visible = gtk_action_is_visible (action);
634 hide_if_empty = action->private_data->hide_if_empty;
636 g_object_set (G_OBJECT (proxy),
637 "visible", visible && !(empty && hide_if_empty),
641 gboolean _gtk_menu_is_empty (GtkWidget *menu);
644 gtk_action_sync_visible (GtkAction *action,
648 if (GTK_IS_MENU_ITEM (proxy))
650 GtkWidget *menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
652 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
656 if (gtk_action_is_visible (action))
657 gtk_widget_show (proxy);
659 gtk_widget_hide (proxy);
664 gtk_action_sync_label (GtkAction *action,
668 GtkWidget *label = NULL;
670 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
671 label = GTK_BIN (proxy)->child;
673 if (GTK_IS_LABEL (label))
674 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
678 gtk_action_sync_short_label (GtkAction *action,
682 GValue value = { 0, };
684 g_value_init (&value, G_TYPE_STRING);
685 g_object_get_property (G_OBJECT (action), "short_label", &value);
687 g_object_set_property (G_OBJECT (proxy), "label", &value);
688 g_value_unset (&value);
692 gtk_action_sync_stock_id (GtkAction *action,
696 GtkWidget *image = NULL;
698 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
700 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
702 if (GTK_IS_IMAGE (image))
703 gtk_image_set_from_stock (GTK_IMAGE (image),
704 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
709 gtk_action_sync_tooltip (GtkAction *action,
713 g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
715 if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
717 GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
719 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
721 action->private_data->tooltip,
728 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
731 GtkWidget *menu_item = gtk_action_create_menu_item (action);
733 g_object_ref (menu_item);
734 gtk_object_sink (GTK_OBJECT (menu_item));
736 gtk_tool_item_set_proxy_menu_item (tool_item, "gtk-action-menu-item", menu_item);
737 g_object_unref (menu_item);
743 connect_proxy (GtkAction *action,
746 GtkActionGroup *group = action->private_data->action_group;
748 g_object_ref (action);
749 g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
752 /* add this widget to the list of proxies */
753 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
754 g_signal_connect (proxy, "destroy",
755 G_CALLBACK (remove_proxy), action);
757 g_signal_connect_object (action, "notify::sensitive",
758 G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
759 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
761 g_signal_connect_object (action, "notify::visible",
762 G_CALLBACK (gtk_action_sync_visible), proxy, 0);
763 if (gtk_action_is_visible (action))
764 gtk_widget_show (proxy);
766 gtk_widget_hide (proxy);
767 gtk_widget_set_no_show_all (proxy, TRUE);
769 if (GTK_IS_MENU_ITEM (proxy))
772 /* menu item specific synchronisers ... */
774 if (action->private_data->accel_quark)
776 gtk_action_connect_accelerator (action);
777 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
778 g_quark_to_string (action->private_data->accel_quark));
781 label = GTK_BIN (proxy)->child;
783 /* make sure label is a label */
784 if (label && !GTK_IS_LABEL (label))
786 gtk_container_remove (GTK_CONTAINER (proxy), label);
791 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
792 "use_underline", TRUE,
798 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
799 g_object_set (G_OBJECT (label),
800 "accel_closure", action->private_data->accel_closure,
803 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
804 g_signal_connect_object (action, "notify::label",
805 G_CALLBACK (gtk_action_sync_label), proxy, 0);
807 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
811 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
812 if (image && !GTK_IS_IMAGE (image))
814 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
819 image = gtk_image_new_from_stock (NULL,
821 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
823 gtk_widget_show (image);
825 gtk_image_set_from_stock (GTK_IMAGE (image),
826 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
827 g_signal_connect_object (action, "notify::stock_id",
828 G_CALLBACK (gtk_action_sync_stock_id),
832 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
833 g_signal_connect_object (proxy, "activate",
834 G_CALLBACK (gtk_action_activate), action,
838 else if (GTK_IS_TOOL_ITEM (proxy))
840 /* toolbar item specific synchronisers ... */
842 g_object_set (G_OBJECT (proxy),
843 "visible_horizontal", action->private_data->visible_horizontal,
844 "visible_vertical", action->private_data->visible_vertical,
845 "is_important", action->private_data->is_important,
847 /* FIXME: we should set the tooltip here, but the current api
848 * doesn't allow it before the item is added to a toolbar.
850 g_signal_connect_object (action, "notify::visible_horizontal",
851 G_CALLBACK (gtk_action_sync_property),
853 g_signal_connect_object (action, "notify::visible_vertical",
854 G_CALLBACK (gtk_action_sync_property),
856 g_signal_connect_object (action, "notify::is_important",
857 G_CALLBACK (gtk_action_sync_property),
859 g_signal_connect_object (action, "notify::tooltip",
860 G_CALLBACK (gtk_action_sync_tooltip),
863 g_signal_connect_object (proxy, "create_menu_proxy",
864 G_CALLBACK (gtk_action_create_menu_proxy),
867 /* toolbar button specific synchronisers ... */
868 if (GTK_IS_TOOL_BUTTON (proxy))
870 g_object_set (G_OBJECT (proxy),
871 "label", action->private_data->short_label,
872 "use_underline", TRUE,
873 "stock_id", action->private_data->stock_id,
875 /* FIXME: we should set the tooltip here, but the current api
876 * doesn't allow it before the item is added to a toolbar.
878 g_signal_connect_object (action, "notify::short_label",
879 G_CALLBACK (gtk_action_sync_short_label),
881 g_signal_connect_object (action, "notify::stock_id",
882 G_CALLBACK (gtk_action_sync_property),
884 g_signal_connect_object (proxy, "clicked",
885 G_CALLBACK (gtk_action_activate), action,
889 else if (GTK_IS_BUTTON (proxy))
891 /* button specific synchronisers ... */
893 /* synchronise the label */
894 g_object_set (G_OBJECT (proxy),
895 "label", action->private_data->short_label,
896 "use_underline", TRUE,
898 g_signal_connect_object (action, "notify::short_label",
899 G_CALLBACK (gtk_action_sync_short_label),
902 g_signal_connect_object (proxy, "clicked",
903 G_CALLBACK (gtk_action_activate), action,
907 _gtk_action_group_emit_connect_proxy (group, action, proxy);
911 disconnect_proxy (GtkAction *action,
914 GtkActionGroup *group = action->private_data->action_group;
916 g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
918 /* remove proxy from list of proxies */
919 g_signal_handlers_disconnect_by_func (proxy,
920 G_CALLBACK (remove_proxy),
922 remove_proxy (proxy, action);
924 /* disconnect the activate handler */
925 g_signal_handlers_disconnect_by_func (proxy,
926 G_CALLBACK (gtk_action_activate),
929 /* disconnect handlers for notify::* signals */
930 g_signal_handlers_disconnect_by_func (proxy,
931 G_CALLBACK (gtk_action_sync_sensitivity),
933 g_signal_handlers_disconnect_by_func (proxy,
934 G_CALLBACK (gtk_action_sync_property),
937 g_signal_handlers_disconnect_by_func (action,
938 G_CALLBACK (gtk_action_sync_stock_id), proxy);
940 /* menu item specific synchronisers ... */
941 g_signal_handlers_disconnect_by_func (action,
942 G_CALLBACK (gtk_action_sync_label),
945 /* toolbar button specific synchronisers ... */
946 g_signal_handlers_disconnect_by_func (action,
947 G_CALLBACK (gtk_action_sync_short_label),
950 g_signal_handlers_disconnect_by_func (proxy,
951 G_CALLBACK (gtk_action_create_menu_proxy),
954 _gtk_action_group_emit_disconnect_proxy (group, action, proxy);
958 _gtk_action_emit_activate (GtkAction *action)
960 GtkActionGroup *group = action->private_data->action_group;
964 g_object_ref (group);
965 _gtk_action_group_emit_pre_activate (group, action);
968 g_signal_emit (action, action_signals[ACTIVATE], 0);
972 _gtk_action_group_emit_post_activate (group, action);
973 g_object_unref (group);
978 * gtk_action_activate:
979 * @action: the action object
981 * Emits the "activate" signal on the specified action, if it isn't
982 * insensitive. This gets called by the proxy widgets when they get
985 * It can also be used to manually activate an action.
990 gtk_action_activate (GtkAction *action)
992 g_return_if_fail (GTK_IS_ACTION (action));
994 if (gtk_action_is_sensitive (action))
995 _gtk_action_emit_activate (action);
999 * gtk_action_create_icon:
1000 * @action: the action object
1001 * @icon_size: the size of the icon that should be created.
1003 * This function is intended for use by action implementations to
1004 * create icons displayed in the proxy widgets.
1006 * Returns: a widget that displays the icon for this action.
1011 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1013 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1015 if (action->private_data->stock_id)
1016 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1022 * gtk_action_create_menu_item:
1023 * @action: the action object
1025 * Creates a menu item widget that proxies for the given action.
1027 * Returns: a menu item connected to the action.
1032 gtk_action_create_menu_item (GtkAction *action)
1034 GtkWidget *menu_item;
1036 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1038 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1040 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1046 * gtk_action_create_tool_item:
1047 * @action: the action object
1049 * Creates a toolbar item widget that proxies for the given action.
1051 * Returns: a toolbar item connected to the action.
1056 gtk_action_create_tool_item (GtkAction *action)
1060 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1062 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1064 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1070 * gtk_action_connect_proxy:
1071 * @action: the action object
1072 * @proxy: the proxy widget
1074 * Connects a widget to an action object as a proxy. Synchronises
1075 * various properties of the action with the widget (such as label
1076 * text, icon, tooltip, etc), and attaches a callback so that the
1077 * action gets activated when the proxy widget does.
1079 * If the widget is already connected to an action, it is disconnected
1085 gtk_action_connect_proxy (GtkAction *action,
1088 GtkAction *prev_action;
1090 g_return_if_fail (GTK_IS_ACTION (action));
1091 g_return_if_fail (GTK_IS_WIDGET (proxy));
1093 prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1096 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1098 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1102 * gtk_action_disconnect_proxy:
1103 * @action: the action object
1104 * @proxy: the proxy widget
1106 * Disconnects a proxy widget from an action.
1107 * Does <emphasis>not</emphasis> destroy the widget, however.
1112 gtk_action_disconnect_proxy (GtkAction *action,
1115 g_return_if_fail (GTK_IS_ACTION (action));
1116 g_return_if_fail (GTK_IS_WIDGET (proxy));
1118 g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1120 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1124 * gtk_action_get_proxies:
1125 * @action: the action object
1127 * Returns the proxy widgets for an action.
1129 * Return value: a #GSList of proxy widgets. The list is owned by the action and
1130 * must not be modified.
1135 gtk_action_get_proxies (GtkAction *action)
1137 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1139 return action->private_data->proxies;
1144 * gtk_action_get_name:
1145 * @action: the action object
1147 * Returns the name of the action.
1149 * Return value: the name of the action. The string belongs to GTK+ and should not
1155 gtk_action_get_name (GtkAction *action)
1157 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1159 return action->private_data->name;
1163 * gtk_action_is_sensitive:
1164 * @action: the action object
1166 * Returns whether the action is effectively sensitive.
1168 * Return value: %TRUE if the action and its associated action group
1169 * are both sensitive.
1174 gtk_action_is_sensitive (GtkAction *action)
1176 GtkActionPrivate *priv;
1177 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1179 priv = action->private_data;
1180 return priv->sensitive &&
1181 (priv->action_group == NULL ||
1182 gtk_action_group_get_sensitive (priv->action_group));
1186 * gtk_action_get_sensitive:
1187 * @action: the action object
1189 * Returns whether the action itself is sensitive. Note that this doesn't
1190 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1193 * Return value: %TRUE if the action itself is sensitive.
1198 gtk_action_get_sensitive (GtkAction *action)
1200 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1202 return action->private_data->sensitive;
1206 * gtk_action_is_visible:
1207 * @action: the action object
1209 * Returns whether the action is effectively visible.
1211 * Return value: %TRUE if the action and its associated action group
1217 gtk_action_is_visible (GtkAction *action)
1219 GtkActionPrivate *priv;
1220 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1222 priv = action->private_data;
1223 return priv->visible &&
1224 (priv->action_group == NULL ||
1225 gtk_action_group_get_visible (priv->action_group));
1229 * gtk_action_get_visible:
1230 * @action: the action object
1232 * Returns whether the action itself is visible. Note that this doesn't
1233 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1236 * Return value: %TRUE if the action itself is visible.
1241 gtk_action_get_visible (GtkAction *action)
1243 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1245 return action->private_data->visible;
1249 * gtk_action_block_activate_from:
1250 * @action: the action object
1251 * @proxy: a proxy widget
1253 * Disables calls to the gtk_action_activate()
1254 * function by signals on the given proxy widget. This is used to
1255 * break notification loops for things like check or radio actions.
1257 * This function is intended for use by action implementations.
1262 gtk_action_block_activate_from (GtkAction *action,
1265 g_return_if_fail (GTK_IS_ACTION (action));
1267 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1272 * gtk_action_unblock_activate_from:
1273 * @action: the action object
1274 * @proxy: a proxy widget
1276 * Re-enables calls to the gtk_action_activate()
1277 * function by signals on the given proxy widget. This undoes the
1278 * blocking done by gtk_action_block_activate_from().
1280 * This function is intended for use by action implementations.
1285 gtk_action_unblock_activate_from (GtkAction *action,
1288 g_return_if_fail (GTK_IS_ACTION (action));
1290 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1295 closure_accel_activate (GClosure *closure,
1296 GValue *return_value,
1297 guint n_param_values,
1298 const GValue *param_values,
1299 gpointer invocation_hint,
1300 gpointer marshal_data)
1302 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1303 g_signal_emit (closure->data, action_signals[ACTIVATE], 0);
1305 /* we handled the accelerator */
1306 g_value_set_boolean (return_value, TRUE);
1310 gtk_action_set_action_group (GtkAction *action,
1311 GtkActionGroup *action_group)
1313 g_return_if_fail (GTK_IS_ACTION (action));
1315 if (action->private_data->action_group == NULL)
1316 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1318 g_return_if_fail (action_group == NULL);
1320 action->private_data->action_group = action_group;
1324 * gtk_action_set_accel_path:
1325 * @action: the action object
1326 * @accel_path: the accelerator path
1328 * Sets the accel path for this action. All proxy widgets associated
1329 * with the action will have this accel path, so that their
1330 * accelerators are consistent.
1335 gtk_action_set_accel_path (GtkAction *action,
1336 const gchar *accel_path)
1338 g_return_if_fail (GTK_IS_ACTION (action));
1340 action->private_data->accel_quark = g_quark_from_string (accel_path);
1344 * gtk_action_set_accel_group:
1345 * @action: the action object
1346 * @accel_group: a #GtkAccelGroup or %NULL
1348 * Sets the #GtkAccelGroup in which the accelerator for this action
1349 * will be installed.
1354 gtk_action_set_accel_group (GtkAction *action,
1355 GtkAccelGroup *accel_group)
1357 g_return_if_fail (GTK_IS_ACTION (action));
1358 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1361 g_object_ref (accel_group);
1362 if (action->private_data->accel_group)
1363 g_object_unref (action->private_data->accel_group);
1365 action->private_data->accel_group = accel_group;
1369 * gtk_action_connect_accelerator:
1370 * @action: a #GtkAction
1372 * Installs the accelerator for @action if @action has an
1373 * accel path and group. See gtk_action_set_accel_path() and
1374 * gtk_action_set_accel_group()
1376 * Since multiple proxies may independently trigger the installation
1377 * of the accelerator, the @action counts the number of times this
1378 * function has been called and doesn't remove the accelerator until
1379 * gtk_action_disconnect_accelerator() has been called as many times.
1384 gtk_action_connect_accelerator (GtkAction *action)
1386 g_return_if_fail (GTK_IS_ACTION (action));
1388 if (!action->private_data->accel_quark ||
1389 !action->private_data->accel_group)
1392 if (action->private_data->accel_count == 0)
1394 const gchar *accel_path =
1395 g_quark_to_string (action->private_data->accel_quark);
1397 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1399 action->private_data->accel_closure);
1402 action->private_data->accel_count++;
1406 * gtk_action_disconnect_accelerator:
1407 * @action: a #GtkAction
1409 * Undoes the effect of one call to gtk_action_connect_accelerator().
1414 gtk_action_disconnect_accelerator (GtkAction *action)
1416 g_return_if_fail (GTK_IS_ACTION (action));
1418 if (!action->private_data->accel_quark ||
1419 !action->private_data->accel_group)
1422 action->private_data->accel_count--;
1424 if (action->private_data->accel_count == 0)
1425 gtk_accel_group_disconnect (action->private_data->accel_group,
1426 action->private_data->accel_closure);