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/.
34 #include "gtkaction.h"
35 #include "gtkactiongroup.h"
36 #include "gtkaccellabel.h"
37 #include "gtkbutton.h"
39 #include "gtkimagemenuitem.h"
42 #include "gtkmarshalers.h"
43 #include "gtkmenuitem.h"
45 #include "gtktearoffmenuitem.h"
46 #include "gtktoolbutton.h"
47 #include "gtktoolbar.h"
50 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
52 struct _GtkActionPrivate
58 gchar *stock_id; /* icon */
62 guint label_set : 1; /* these two used so we can set label */
63 guint short_label_set : 1; /* based on stock id */
64 guint visible_horizontal : 1;
65 guint visible_vertical : 1;
66 guint is_important : 1;
67 guint hide_if_empty : 1;
68 guint visible_overflown : 1;
72 GtkAccelGroup *accel_group;
73 GClosure *accel_closure;
76 GtkActionGroup *action_group;
78 /* list of proxy widgets */
96 PROP_VISIBLE_HORIZONTAL,
97 PROP_VISIBLE_VERTICAL,
98 PROP_VISIBLE_OVERFLOWN,
106 static void gtk_action_init (GtkAction *action);
107 static void gtk_action_class_init (GtkActionClass *class);
109 static GQuark accel_path_id = 0;
110 static const gchar *accel_path_key = "GtkAction::accel_path";
113 gtk_action_get_type (void)
115 static GtkType type = 0;
119 static const GTypeInfo type_info =
121 sizeof (GtkActionClass),
122 (GBaseInitFunc) NULL,
123 (GBaseFinalizeFunc) NULL,
124 (GClassInitFunc) gtk_action_class_init,
125 (GClassFinalizeFunc) NULL,
130 (GInstanceInitFunc) gtk_action_init,
133 type = g_type_register_static (G_TYPE_OBJECT,
140 static void gtk_action_finalize (GObject *object);
141 static void gtk_action_set_property (GObject *object,
145 static void gtk_action_get_property (GObject *object,
149 static void gtk_action_set_action_group (GtkAction *action,
150 GtkActionGroup *action_group);
152 static GtkWidget *create_menu_item (GtkAction *action);
153 static GtkWidget *create_tool_item (GtkAction *action);
154 static void connect_proxy (GtkAction *action,
156 static void disconnect_proxy (GtkAction *action,
158 static void closure_accel_activate (GClosure *closure,
159 GValue *return_value,
160 guint n_param_values,
161 const GValue *param_values,
162 gpointer invocation_hint,
163 gpointer marshal_data);
165 static GObjectClass *parent_class = NULL;
166 static guint action_signals[LAST_SIGNAL] = { 0 };
170 gtk_action_class_init (GtkActionClass *klass)
172 GObjectClass *gobject_class;
174 accel_path_id = g_quark_from_static_string (accel_path_key);
176 parent_class = g_type_class_peek_parent (klass);
177 gobject_class = G_OBJECT_CLASS (klass);
179 gobject_class->finalize = gtk_action_finalize;
180 gobject_class->set_property = gtk_action_set_property;
181 gobject_class->get_property = gtk_action_get_property;
183 klass->activate = NULL;
185 klass->create_menu_item = create_menu_item;
186 klass->create_tool_item = create_tool_item;
187 klass->connect_proxy = connect_proxy;
188 klass->disconnect_proxy = disconnect_proxy;
190 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
191 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
193 g_object_class_install_property (gobject_class,
195 g_param_spec_string ("name",
197 P_("A unique name for the action."),
200 G_PARAM_CONSTRUCT_ONLY));
201 g_object_class_install_property (gobject_class,
203 g_param_spec_string ("label",
205 P_("The label used for menu items and buttons "
206 "that activate this action."),
209 g_object_class_install_property (gobject_class,
211 g_param_spec_string ("short_label",
213 P_("A shorter label that may be used on toolbar buttons."),
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("tooltip",
220 P_("A tooltip for this action."),
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("stock_id",
227 P_("The stock icon displayed in widgets representing "
231 g_object_class_install_property (gobject_class,
232 PROP_VISIBLE_HORIZONTAL,
233 g_param_spec_boolean ("visible_horizontal",
234 P_("Visible when horizontal"),
235 P_("Whether the toolbar item is visible when the toolbar "
236 "is in a horizontal orientation."),
240 * GtkAction:visible-overflown:
242 * When %TRUE, toolitem proxies for this action are represented in the
243 * toolbar overflow menu.
247 g_object_class_install_property (gobject_class,
248 PROP_VISIBLE_OVERFLOWN,
249 g_param_spec_boolean ("visible_overflown",
250 P_("Visible when overflown"),
251 P_("When TRUE, toolitem proxies for this action "
252 "are represented in the toolbar overflow menu."),
255 g_object_class_install_property (gobject_class,
256 PROP_VISIBLE_VERTICAL,
257 g_param_spec_boolean ("visible_vertical",
258 P_("Visible when vertical"),
259 P_("Whether the toolbar item is visible when the toolbar "
260 "is in a vertical orientation."),
263 g_object_class_install_property (gobject_class,
265 g_param_spec_boolean ("is_important",
267 P_("Whether the action is considered important. "
268 "When TRUE, toolitem proxies for this action "
269 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
272 g_object_class_install_property (gobject_class,
274 g_param_spec_boolean ("hide_if_empty",
276 P_("When TRUE, empty menu proxies for this action are hidden."),
279 g_object_class_install_property (gobject_class,
281 g_param_spec_boolean ("sensitive",
283 P_("Whether the action is enabled."),
286 g_object_class_install_property (gobject_class,
288 g_param_spec_boolean ("visible",
290 P_("Whether the action is visible."),
293 g_object_class_install_property (gobject_class,
295 g_param_spec_object ("action_group",
297 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
298 GTK_TYPE_ACTION_GROUP,
302 * GtkAction::activate:
303 * @action: the #GtkAction
305 * The "activate" signal is emitted when the action is activated.
309 action_signals[ACTIVATE] =
310 g_signal_new ("activate",
311 G_OBJECT_CLASS_TYPE (klass),
312 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
313 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
314 g_cclosure_marshal_VOID__VOID,
317 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
322 gtk_action_init (GtkAction *action)
324 action->private_data = GTK_ACTION_GET_PRIVATE (action);
326 action->private_data->name = NULL;
327 action->private_data->label = NULL;
328 action->private_data->short_label = NULL;
329 action->private_data->tooltip = NULL;
330 action->private_data->stock_id = NULL;
331 action->private_data->visible_horizontal = TRUE;
332 action->private_data->visible_vertical = TRUE;
333 action->private_data->visible_overflown = TRUE;
334 action->private_data->is_important = FALSE;
335 action->private_data->hide_if_empty = TRUE;
337 action->private_data->sensitive = TRUE;
338 action->private_data->visible = TRUE;
340 action->private_data->label_set = FALSE;
341 action->private_data->short_label_set = FALSE;
343 action->private_data->accel_count = 0;
344 action->private_data->accel_group = NULL;
345 action->private_data->accel_quark = 0;
346 action->private_data->accel_closure =
347 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
348 g_closure_set_marshal (action->private_data->accel_closure,
349 closure_accel_activate);
350 g_closure_ref (action->private_data->accel_closure);
351 g_closure_sink (action->private_data->accel_closure);
353 action->private_data->action_group = NULL;
355 action->private_data->proxies = NULL;
360 * @name: A unique name for the action
361 * @label: the label displayed in menu items and on buttons
362 * @tooltip: a tooltip for the action
363 * @stock_id: the stock icon to display in widgets representing the action
365 * Creates a new #GtkAction object. To add the action to a
366 * #GtkActionGroup and set the accelerator for the action,
367 * call gtk_action_group_add_action_with_accel().
368 * See <xref linkend="XML-UI"/> for information on allowed action
371 * Return value: a new #GtkAction
376 gtk_action_new (const gchar *name,
378 const gchar *tooltip,
379 const gchar *stock_id)
383 action = g_object_new (GTK_TYPE_ACTION,
387 "stock_id", stock_id,
394 gtk_action_finalize (GObject *object)
397 action = GTK_ACTION (object);
399 g_free (action->private_data->name);
400 g_free (action->private_data->label);
401 g_free (action->private_data->short_label);
402 g_free (action->private_data->tooltip);
403 g_free (action->private_data->stock_id);
405 g_closure_unref (action->private_data->accel_closure);
406 if (action->private_data->accel_group)
407 g_object_unref (action->private_data->accel_group);
409 G_OBJECT_CLASS (parent_class)->finalize (object);
413 gtk_action_set_property (GObject *object,
421 action = GTK_ACTION (object);
426 tmp = action->private_data->name;
427 action->private_data->name = g_value_dup_string (value);
431 tmp = action->private_data->label;
432 action->private_data->label = g_value_dup_string (value);
434 action->private_data->label_set = (action->private_data->label != NULL);
435 /* if label is unset, then use the label from the stock item */
436 if (!action->private_data->label_set && action->private_data->stock_id)
438 GtkStockItem stock_item;
440 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
441 action->private_data->label = g_strdup (stock_item.label);
443 /* if short_label is unset, set short_label=label */
444 if (!action->private_data->short_label_set)
446 tmp = action->private_data->short_label;
447 action->private_data->short_label = g_strdup (action->private_data->label);
449 g_object_notify (object, "short_label");
452 case PROP_SHORT_LABEL:
453 tmp = action->private_data->short_label;
454 action->private_data->short_label = g_value_dup_string (value);
456 action->private_data->short_label_set = (action->private_data->short_label != NULL);
457 /* if short_label is unset, then use the value of label */
458 if (!action->private_data->short_label_set)
460 action->private_data->short_label = g_strdup (action->private_data->label);
464 tmp = action->private_data->tooltip;
465 action->private_data->tooltip = g_value_dup_string (value);
469 tmp = action->private_data->stock_id;
470 action->private_data->stock_id = g_value_dup_string (value);
472 /* update label and short_label if appropriate */
473 if (!action->private_data->label_set)
475 GtkStockItem stock_item;
477 g_free (action->private_data->label);
478 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
479 action->private_data->label = g_strdup (stock_item.label);
481 action->private_data->label = NULL;
482 g_object_notify (object, "label");
484 if (!action->private_data->short_label_set)
486 tmp = action->private_data->short_label;
487 action->private_data->short_label = g_strdup (action->private_data->label);
489 g_object_notify (object, "short_label");
492 case PROP_VISIBLE_HORIZONTAL:
493 action->private_data->visible_horizontal = g_value_get_boolean (value);
495 case PROP_VISIBLE_VERTICAL:
496 action->private_data->visible_vertical = g_value_get_boolean (value);
498 case PROP_VISIBLE_OVERFLOWN:
499 action->private_data->visible_overflown = g_value_get_boolean (value);
501 case PROP_IS_IMPORTANT:
502 action->private_data->is_important = g_value_get_boolean (value);
504 case PROP_HIDE_IF_EMPTY:
505 action->private_data->hide_if_empty = g_value_get_boolean (value);
508 action->private_data->sensitive = g_value_get_boolean (value);
511 action->private_data->visible = g_value_get_boolean (value);
513 case PROP_ACTION_GROUP:
514 gtk_action_set_action_group (action, g_value_get_object (value));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 gtk_action_get_property (GObject *object,
530 action = GTK_ACTION (object);
535 g_value_set_string (value, action->private_data->name);
538 g_value_set_string (value, action->private_data->label);
540 case PROP_SHORT_LABEL:
541 g_value_set_string (value, action->private_data->short_label);
544 g_value_set_string (value, action->private_data->tooltip);
547 g_value_set_string (value, action->private_data->stock_id);
549 case PROP_VISIBLE_HORIZONTAL:
550 g_value_set_boolean (value, action->private_data->visible_horizontal);
552 case PROP_VISIBLE_VERTICAL:
553 g_value_set_boolean (value, action->private_data->visible_vertical);
555 case PROP_VISIBLE_OVERFLOWN:
556 g_value_set_boolean (value, action->private_data->visible_overflown);
558 case PROP_IS_IMPORTANT:
559 g_value_set_boolean (value, action->private_data->is_important);
561 case PROP_HIDE_IF_EMPTY:
562 g_value_set_boolean (value, action->private_data->hide_if_empty);
565 g_value_set_boolean (value, action->private_data->sensitive);
568 g_value_set_boolean (value, action->private_data->visible);
570 case PROP_ACTION_GROUP:
571 g_value_set_object (value, action->private_data->action_group);
574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580 create_menu_item (GtkAction *action)
582 GType menu_item_type;
584 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
586 return g_object_new (menu_item_type, NULL);
590 create_tool_item (GtkAction *action)
592 GType toolbar_item_type;
594 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
596 return g_object_new (toolbar_item_type, NULL);
600 remove_proxy (GtkWidget *proxy,
603 if (GTK_IS_MENU_ITEM (proxy))
604 gtk_action_disconnect_accelerator (action);
606 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
610 gtk_action_sync_sensitivity (GtkAction *action,
614 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
618 gtk_action_sync_property (GtkAction *action,
622 const gchar *property;
623 GValue value = { 0, };
625 property = g_param_spec_get_name (pspec);
627 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
628 g_object_get_property (G_OBJECT (action), property, &value);
630 g_object_set_property (G_OBJECT (proxy), property, &value);
631 g_value_unset (&value);
635 * _gtk_action_sync_menu_visible:
636 * @action: a #GtkAction, or %NULL to determine the action from @proxy
637 * @proxy: a proxy menu item
638 * @empty: whether the submenu attached to @proxy is empty
640 * Updates the visibility of @proxy from the visibility of @action
641 * according to the following rules:
643 * <listitem><para>if @action is invisible, @proxy is too
645 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
646 * property of @action indicates otherwise
650 * This function is used in the implementation of #GtkUIManager.
653 _gtk_action_sync_menu_visible (GtkAction *action,
657 gboolean visible, hide_if_empty;
659 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
660 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
663 action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
665 visible = gtk_action_is_visible (action);
666 hide_if_empty = action->private_data->hide_if_empty;
668 if (visible && !(empty && hide_if_empty))
669 gtk_widget_show (proxy);
671 gtk_widget_hide (proxy);
674 gboolean _gtk_menu_is_empty (GtkWidget *menu);
677 gtk_action_sync_visible (GtkAction *action,
681 if (GTK_IS_MENU_ITEM (proxy))
683 GtkWidget *menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
685 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
689 if (gtk_action_is_visible (action))
690 gtk_widget_show (proxy);
692 gtk_widget_hide (proxy);
697 gtk_action_sync_label (GtkAction *action,
701 GtkWidget *label = NULL;
703 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
704 label = GTK_BIN (proxy)->child;
706 if (GTK_IS_LABEL (label))
707 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
711 gtk_action_sync_short_label (GtkAction *action,
715 GValue value = { 0, };
717 g_value_init (&value, G_TYPE_STRING);
718 g_object_get_property (G_OBJECT (action), "short_label", &value);
720 g_object_set_property (G_OBJECT (proxy), "label", &value);
721 g_value_unset (&value);
725 gtk_action_sync_stock_id (GtkAction *action,
729 GtkWidget *image = NULL;
731 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
733 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
735 if (GTK_IS_IMAGE (image))
736 gtk_image_set_from_stock (GTK_IMAGE (image),
737 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
742 gtk_action_sync_tooltip (GtkAction *action,
746 g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
748 if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
750 GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
752 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
754 action->private_data->tooltip,
761 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
764 GtkWidget *menu_item;
766 if (action->private_data->visible_overflown)
768 menu_item = gtk_action_create_menu_item (action);
770 g_object_ref (menu_item);
771 gtk_object_sink (GTK_OBJECT (menu_item));
773 gtk_tool_item_set_proxy_menu_item (tool_item,
774 "gtk-action-menu-item", menu_item);
775 g_object_unref (menu_item);
778 gtk_tool_item_set_proxy_menu_item (tool_item,
779 "gtk-action-menu-item", NULL);
785 connect_proxy (GtkAction *action,
788 g_object_ref (action);
789 g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
792 /* add this widget to the list of proxies */
793 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
794 g_signal_connect (proxy, "destroy",
795 G_CALLBACK (remove_proxy), action);
797 g_signal_connect_object (action, "notify::sensitive",
798 G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
799 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
801 g_signal_connect_object (action, "notify::visible",
802 G_CALLBACK (gtk_action_sync_visible), proxy, 0);
803 if (gtk_action_is_visible (action))
804 gtk_widget_show (proxy);
806 gtk_widget_hide (proxy);
807 gtk_widget_set_no_show_all (proxy, TRUE);
809 if (GTK_IS_MENU_ITEM (proxy))
812 /* menu item specific synchronisers ... */
814 if (action->private_data->accel_quark)
816 gtk_action_connect_accelerator (action);
817 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
818 g_quark_to_string (action->private_data->accel_quark));
821 label = GTK_BIN (proxy)->child;
823 /* make sure label is a label */
824 if (label && !GTK_IS_LABEL (label))
826 gtk_container_remove (GTK_CONTAINER (proxy), label);
831 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
832 "use_underline", TRUE,
838 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
840 "accel_closure", action->private_data->accel_closure,
843 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
844 g_signal_connect_object (action, "notify::label",
845 G_CALLBACK (gtk_action_sync_label), proxy, 0);
847 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
851 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
852 if (image && !GTK_IS_IMAGE (image))
854 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
859 image = gtk_image_new_from_stock (NULL,
861 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
863 gtk_widget_show (image);
865 gtk_image_set_from_stock (GTK_IMAGE (image),
866 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
867 g_signal_connect_object (action, "notify::stock-id",
868 G_CALLBACK (gtk_action_sync_stock_id),
872 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
873 g_signal_connect_object (proxy, "activate",
874 G_CALLBACK (gtk_action_activate), action,
878 else if (GTK_IS_TOOL_ITEM (proxy))
880 /* toolbar item specific synchronisers ... */
883 "visible_horizontal", action->private_data->visible_horizontal,
884 "visible_vertical", action->private_data->visible_vertical,
885 "is_important", action->private_data->is_important,
887 /* FIXME: we should set the tooltip here, but the current api
888 * doesn't allow it before the item is added to a toolbar.
890 g_signal_connect_object (action, "notify::visible-horizontal",
891 G_CALLBACK (gtk_action_sync_property),
893 g_signal_connect_object (action, "notify::visible-vertical",
894 G_CALLBACK (gtk_action_sync_property),
896 g_signal_connect_object (action, "notify::is-important",
897 G_CALLBACK (gtk_action_sync_property),
899 g_signal_connect_object (action, "notify::tooltip",
900 G_CALLBACK (gtk_action_sync_tooltip),
903 g_signal_connect_object (proxy, "create_menu_proxy",
904 G_CALLBACK (gtk_action_create_menu_proxy),
907 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
909 /* toolbar button specific synchronisers ... */
910 if (GTK_IS_TOOL_BUTTON (proxy))
913 "label", action->private_data->short_label,
914 "use_underline", TRUE,
915 "stock_id", action->private_data->stock_id,
917 /* FIXME: we should set the tooltip here, but the current api
918 * doesn't allow it before the item is added to a toolbar.
920 g_signal_connect_object (action, "notify::short-label",
921 G_CALLBACK (gtk_action_sync_short_label),
923 g_signal_connect_object (action, "notify::stock-id",
924 G_CALLBACK (gtk_action_sync_property),
926 g_signal_connect_object (proxy, "clicked",
927 G_CALLBACK (gtk_action_activate), action,
931 else if (GTK_IS_BUTTON (proxy))
933 /* button specific synchronisers ... */
935 /* synchronise the label */
937 "label", action->private_data->short_label,
938 "use_underline", TRUE,
940 g_signal_connect_object (action, "notify::short-label",
941 G_CALLBACK (gtk_action_sync_short_label),
944 g_signal_connect_object (proxy, "clicked",
945 G_CALLBACK (gtk_action_activate), action,
949 if (action->private_data->action_group)
950 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
954 disconnect_proxy (GtkAction *action,
957 g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
959 /* remove proxy from list of proxies */
960 g_signal_handlers_disconnect_by_func (proxy,
961 G_CALLBACK (remove_proxy),
963 remove_proxy (proxy, action);
965 /* disconnect the activate handler */
966 g_signal_handlers_disconnect_by_func (proxy,
967 G_CALLBACK (gtk_action_activate),
970 /* disconnect handlers for notify::* signals */
971 g_signal_handlers_disconnect_by_func (action,
972 G_CALLBACK (gtk_action_sync_sensitivity),
974 g_signal_handlers_disconnect_by_func (action,
975 G_CALLBACK (gtk_action_sync_property),
978 g_signal_handlers_disconnect_by_func (action,
979 G_CALLBACK (gtk_action_sync_stock_id), proxy);
981 /* menu item specific synchronisers ... */
982 g_signal_handlers_disconnect_by_func (action,
983 G_CALLBACK (gtk_action_sync_label),
986 /* toolbar button specific synchronisers ... */
987 g_signal_handlers_disconnect_by_func (action,
988 G_CALLBACK (gtk_action_sync_short_label),
991 g_signal_handlers_disconnect_by_func (proxy,
992 G_CALLBACK (gtk_action_create_menu_proxy),
995 if (action->private_data->action_group)
996 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
1000 _gtk_action_emit_activate (GtkAction *action)
1002 GtkActionGroup *group = action->private_data->action_group;
1006 g_object_ref (group);
1007 _gtk_action_group_emit_pre_activate (group, action);
1010 g_signal_emit (action, action_signals[ACTIVATE], 0);
1014 _gtk_action_group_emit_post_activate (group, action);
1015 g_object_unref (group);
1020 * gtk_action_activate:
1021 * @action: the action object
1023 * Emits the "activate" signal on the specified action, if it isn't
1024 * insensitive. This gets called by the proxy widgets when they get
1027 * It can also be used to manually activate an action.
1032 gtk_action_activate (GtkAction *action)
1034 g_return_if_fail (GTK_IS_ACTION (action));
1036 if (gtk_action_is_sensitive (action))
1037 _gtk_action_emit_activate (action);
1041 * gtk_action_create_icon:
1042 * @action: the action object
1043 * @icon_size: the size of the icon that should be created.
1045 * This function is intended for use by action implementations to
1046 * create icons displayed in the proxy widgets.
1048 * Returns: a widget that displays the icon for this action.
1053 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1055 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1057 if (action->private_data->stock_id)
1058 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1064 * gtk_action_create_menu_item:
1065 * @action: the action object
1067 * Creates a menu item widget that proxies for the given action.
1069 * Returns: a menu item connected to the action.
1074 gtk_action_create_menu_item (GtkAction *action)
1076 GtkWidget *menu_item;
1078 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1080 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1082 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1088 * gtk_action_create_tool_item:
1089 * @action: the action object
1091 * Creates a toolbar item widget that proxies for the given action.
1093 * Returns: a toolbar item connected to the action.
1098 gtk_action_create_tool_item (GtkAction *action)
1102 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1104 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1106 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1112 * gtk_action_connect_proxy:
1113 * @action: the action object
1114 * @proxy: the proxy widget
1116 * Connects a widget to an action object as a proxy. Synchronises
1117 * various properties of the action with the widget (such as label
1118 * text, icon, tooltip, etc), and attaches a callback so that the
1119 * action gets activated when the proxy widget does.
1121 * If the widget is already connected to an action, it is disconnected
1127 gtk_action_connect_proxy (GtkAction *action,
1130 GtkAction *prev_action;
1132 g_return_if_fail (GTK_IS_ACTION (action));
1133 g_return_if_fail (GTK_IS_WIDGET (proxy));
1135 prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1138 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1140 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1144 * gtk_action_disconnect_proxy:
1145 * @action: the action object
1146 * @proxy: the proxy widget
1148 * Disconnects a proxy widget from an action.
1149 * Does <emphasis>not</emphasis> destroy the widget, however.
1154 gtk_action_disconnect_proxy (GtkAction *action,
1157 g_return_if_fail (GTK_IS_ACTION (action));
1158 g_return_if_fail (GTK_IS_WIDGET (proxy));
1160 g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1162 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1166 * gtk_action_get_proxies:
1167 * @action: the action object
1169 * Returns the proxy widgets for an action.
1171 * Return value: a #GSList of proxy widgets. The list is owned by the action and
1172 * must not be modified.
1177 gtk_action_get_proxies (GtkAction *action)
1179 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1181 return action->private_data->proxies;
1186 * gtk_action_get_name:
1187 * @action: the action object
1189 * Returns the name of the action.
1191 * Return value: the name of the action. The string belongs to GTK+ and should not
1196 G_CONST_RETURN gchar *
1197 gtk_action_get_name (GtkAction *action)
1199 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1201 return action->private_data->name;
1205 * gtk_action_is_sensitive:
1206 * @action: the action object
1208 * Returns whether the action is effectively sensitive.
1210 * Return value: %TRUE if the action and its associated action group
1211 * are both sensitive.
1216 gtk_action_is_sensitive (GtkAction *action)
1218 GtkActionPrivate *priv;
1219 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1221 priv = action->private_data;
1222 return priv->sensitive &&
1223 (priv->action_group == NULL ||
1224 gtk_action_group_get_sensitive (priv->action_group));
1228 * gtk_action_get_sensitive:
1229 * @action: the action object
1231 * Returns whether the action itself is sensitive. Note that this doesn't
1232 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1235 * Return value: %TRUE if the action itself is sensitive.
1240 gtk_action_get_sensitive (GtkAction *action)
1242 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1244 return action->private_data->sensitive;
1248 * gtk_action_set_sensitive:
1249 * @action: the action object
1250 * @sensitive: %TRUE to make the action sensitive
1252 * Sets the ::sensitive property of the action to @sensitive. Note that
1253 * this doesn't necessarily mean effective sensitivity. See
1254 * gtk_action_is_sensitive()
1260 gtk_action_set_sensitive (GtkAction *action,
1263 g_return_if_fail (GTK_IS_ACTION (action));
1265 sensitive = sensitive != FALSE;
1267 if (action->private_data->sensitive != sensitive)
1269 action->private_data->sensitive = sensitive;
1271 g_object_notify (G_OBJECT (action), "sensitive");
1276 * gtk_action_is_visible:
1277 * @action: the action object
1279 * Returns whether the action is effectively visible.
1281 * Return value: %TRUE if the action and its associated action group
1287 gtk_action_is_visible (GtkAction *action)
1289 GtkActionPrivate *priv;
1290 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1292 priv = action->private_data;
1293 return priv->visible &&
1294 (priv->action_group == NULL ||
1295 gtk_action_group_get_visible (priv->action_group));
1299 * gtk_action_get_visible:
1300 * @action: the action object
1302 * Returns whether the action itself is visible. Note that this doesn't
1303 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1306 * Return value: %TRUE if the action itself is visible.
1311 gtk_action_get_visible (GtkAction *action)
1313 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1315 return action->private_data->visible;
1319 * gtk_action_set_visible:
1320 * @action: the action object
1321 * @visible: %TRUE to make the action visible
1323 * Sets the ::visible property of the action to @visible. Note that
1324 * this doesn't necessarily mean effective visibility. See
1325 * gtk_action_is_visible()
1331 gtk_action_set_visible (GtkAction *action,
1334 g_return_if_fail (GTK_IS_ACTION (action));
1336 visible = visible != FALSE;
1338 if (action->private_data->visible != visible)
1340 action->private_data->visible = visible;
1342 g_object_notify (G_OBJECT (action), "visible");
1347 * gtk_action_block_activate_from:
1348 * @action: the action object
1349 * @proxy: a proxy widget
1351 * Disables calls to the gtk_action_activate()
1352 * function by signals on the given proxy widget. This is used to
1353 * break notification loops for things like check or radio actions.
1355 * This function is intended for use by action implementations.
1360 gtk_action_block_activate_from (GtkAction *action,
1363 g_return_if_fail (GTK_IS_ACTION (action));
1365 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1370 * gtk_action_unblock_activate_from:
1371 * @action: the action object
1372 * @proxy: a proxy widget
1374 * Re-enables calls to the gtk_action_activate()
1375 * function by signals on the given proxy widget. This undoes the
1376 * blocking done by gtk_action_block_activate_from().
1378 * This function is intended for use by action implementations.
1383 gtk_action_unblock_activate_from (GtkAction *action,
1386 g_return_if_fail (GTK_IS_ACTION (action));
1388 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1393 closure_accel_activate (GClosure *closure,
1394 GValue *return_value,
1395 guint n_param_values,
1396 const GValue *param_values,
1397 gpointer invocation_hint,
1398 gpointer marshal_data)
1400 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1402 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1404 /* we handled the accelerator */
1405 g_value_set_boolean (return_value, TRUE);
1410 gtk_action_set_action_group (GtkAction *action,
1411 GtkActionGroup *action_group)
1413 g_return_if_fail (GTK_IS_ACTION (action));
1415 if (action->private_data->action_group == NULL)
1416 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1418 g_return_if_fail (action_group == NULL);
1420 action->private_data->action_group = action_group;
1424 * gtk_action_set_accel_path:
1425 * @action: the action object
1426 * @accel_path: the accelerator path
1428 * Sets the accel path for this action. All proxy widgets associated
1429 * with the action will have this accel path, so that their
1430 * accelerators are consistent.
1435 gtk_action_set_accel_path (GtkAction *action,
1436 const gchar *accel_path)
1438 g_return_if_fail (GTK_IS_ACTION (action));
1440 action->private_data->accel_quark = g_quark_from_string (accel_path);
1444 * gtk_action_get_accel_path:
1445 * @action: the action object
1447 * Returns the accel path for this action.
1451 * Returns: the accel path for this action, or %NULL
1452 * if none is set. The returned string is owned by GTK+
1453 * and must not be freed or modified.
1455 G_CONST_RETURN gchar *
1456 gtk_action_get_accel_path (GtkAction *action)
1458 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1460 if (action->private_data->accel_quark)
1461 return g_quark_to_string (action->private_data->accel_quark);
1468 * gtk_action_set_accel_group:
1469 * @action: the action object
1470 * @accel_group: a #GtkAccelGroup or %NULL
1472 * Sets the #GtkAccelGroup in which the accelerator for this action
1473 * will be installed.
1478 gtk_action_set_accel_group (GtkAction *action,
1479 GtkAccelGroup *accel_group)
1481 g_return_if_fail (GTK_IS_ACTION (action));
1482 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1485 g_object_ref (accel_group);
1486 if (action->private_data->accel_group)
1487 g_object_unref (action->private_data->accel_group);
1489 action->private_data->accel_group = accel_group;
1493 * gtk_action_connect_accelerator:
1494 * @action: a #GtkAction
1496 * Installs the accelerator for @action if @action has an
1497 * accel path and group. See gtk_action_set_accel_path() and
1498 * gtk_action_set_accel_group()
1500 * Since multiple proxies may independently trigger the installation
1501 * of the accelerator, the @action counts the number of times this
1502 * function has been called and doesn't remove the accelerator until
1503 * gtk_action_disconnect_accelerator() has been called as many times.
1508 gtk_action_connect_accelerator (GtkAction *action)
1510 g_return_if_fail (GTK_IS_ACTION (action));
1512 if (!action->private_data->accel_quark ||
1513 !action->private_data->accel_group)
1516 if (action->private_data->accel_count == 0)
1518 const gchar *accel_path =
1519 g_quark_to_string (action->private_data->accel_quark);
1521 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1523 action->private_data->accel_closure);
1526 action->private_data->accel_count++;
1530 * gtk_action_disconnect_accelerator:
1531 * @action: a #GtkAction
1533 * Undoes the effect of one call to gtk_action_connect_accelerator().
1538 gtk_action_disconnect_accelerator (GtkAction *action)
1540 g_return_if_fail (GTK_IS_ACTION (action));
1542 if (!action->private_data->accel_quark ||
1543 !action->private_data->accel_group)
1546 action->private_data->accel_count--;
1548 if (action->private_data->accel_count == 0)
1549 gtk_accel_group_disconnect (action->private_data->accel_group,
1550 action->private_data->accel_closure);