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"
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_button_stock_id (GtkAction *action,
746 g_object_set (G_OBJECT (proxy),
748 action->private_data->stock_id,
753 gtk_action_sync_tooltip (GtkAction *action,
757 g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
759 if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
761 GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
763 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
765 action->private_data->tooltip,
772 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
775 GtkWidget *menu_item;
777 if (action->private_data->visible_overflown)
779 menu_item = gtk_action_create_menu_item (action);
781 g_object_ref (menu_item);
782 gtk_object_sink (GTK_OBJECT (menu_item));
784 gtk_tool_item_set_proxy_menu_item (tool_item,
785 "gtk-action-menu-item", menu_item);
786 g_object_unref (menu_item);
789 gtk_tool_item_set_proxy_menu_item (tool_item,
790 "gtk-action-menu-item", NULL);
796 connect_proxy (GtkAction *action,
799 g_object_ref (action);
800 g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
803 /* add this widget to the list of proxies */
804 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
805 g_signal_connect (proxy, "destroy",
806 G_CALLBACK (remove_proxy), action);
808 g_signal_connect_object (action, "notify::sensitive",
809 G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
810 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
812 g_signal_connect_object (action, "notify::visible",
813 G_CALLBACK (gtk_action_sync_visible), proxy, 0);
814 if (gtk_action_is_visible (action))
815 gtk_widget_show (proxy);
817 gtk_widget_hide (proxy);
818 gtk_widget_set_no_show_all (proxy, TRUE);
820 if (GTK_IS_MENU_ITEM (proxy))
823 /* menu item specific synchronisers ... */
825 if (action->private_data->accel_quark)
827 gtk_action_connect_accelerator (action);
828 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
829 g_quark_to_string (action->private_data->accel_quark));
832 label = GTK_BIN (proxy)->child;
834 /* make sure label is a label */
835 if (label && !GTK_IS_LABEL (label))
837 gtk_container_remove (GTK_CONTAINER (proxy), label);
842 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
843 "use_underline", TRUE,
849 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
851 "accel_closure", action->private_data->accel_closure,
854 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
855 g_signal_connect_object (action, "notify::label",
856 G_CALLBACK (gtk_action_sync_label), proxy, 0);
858 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
862 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
863 if (image && !GTK_IS_IMAGE (image))
865 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
870 image = gtk_image_new_from_stock (NULL,
872 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
874 gtk_widget_show (image);
876 gtk_image_set_from_stock (GTK_IMAGE (image),
877 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
878 g_signal_connect_object (action, "notify::stock-id",
879 G_CALLBACK (gtk_action_sync_stock_id),
883 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
884 g_signal_connect_object (proxy, "activate",
885 G_CALLBACK (gtk_action_activate), action,
889 else if (GTK_IS_TOOL_ITEM (proxy))
891 /* toolbar item specific synchronisers ... */
894 "visible_horizontal", action->private_data->visible_horizontal,
895 "visible_vertical", action->private_data->visible_vertical,
896 "is_important", action->private_data->is_important,
898 /* FIXME: we should set the tooltip here, but the current api
899 * doesn't allow it before the item is added to a toolbar.
901 g_signal_connect_object (action, "notify::visible-horizontal",
902 G_CALLBACK (gtk_action_sync_property),
904 g_signal_connect_object (action, "notify::visible-vertical",
905 G_CALLBACK (gtk_action_sync_property),
907 g_signal_connect_object (action, "notify::is-important",
908 G_CALLBACK (gtk_action_sync_property),
910 g_signal_connect_object (action, "notify::tooltip",
911 G_CALLBACK (gtk_action_sync_tooltip),
914 g_signal_connect_object (proxy, "create_menu_proxy",
915 G_CALLBACK (gtk_action_create_menu_proxy),
918 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
920 /* toolbar button specific synchronisers ... */
921 if (GTK_IS_TOOL_BUTTON (proxy))
924 "label", action->private_data->short_label,
925 "use_underline", TRUE,
926 "stock_id", action->private_data->stock_id,
928 /* FIXME: we should set the tooltip here, but the current api
929 * doesn't allow it before the item is added to a toolbar.
931 g_signal_connect_object (action, "notify::short-label",
932 G_CALLBACK (gtk_action_sync_short_label),
934 g_signal_connect_object (action, "notify::stock-id",
935 G_CALLBACK (gtk_action_sync_property),
937 g_signal_connect_object (proxy, "clicked",
938 G_CALLBACK (gtk_action_activate), action,
942 else if (GTK_IS_BUTTON (proxy))
944 /* button specific synchronisers ... */
945 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
947 /* synchronise stock-id */
949 "stock-id", action->private_data->stock_id,
951 g_signal_connect_object (action, "notify::stock-id",
952 G_CALLBACK (gtk_action_sync_button_stock_id),
955 else if (GTK_IS_LABEL(GTK_BIN(proxy)->child))
957 /* synchronise the label */
959 "label", action->private_data->short_label,
960 "use_underline", TRUE,
962 g_signal_connect_object (action, "notify::short-label",
963 G_CALLBACK (gtk_action_sync_short_label),
968 /* we leave the button alone if there is a custom child */
969 g_signal_connect_object (proxy, "clicked",
970 G_CALLBACK (gtk_action_activate), action,
974 if (action->private_data->action_group)
975 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
979 disconnect_proxy (GtkAction *action,
982 g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
984 /* remove proxy from list of proxies */
985 g_signal_handlers_disconnect_by_func (proxy,
986 G_CALLBACK (remove_proxy),
988 remove_proxy (proxy, action);
990 /* disconnect the activate handler */
991 g_signal_handlers_disconnect_by_func (proxy,
992 G_CALLBACK (gtk_action_activate),
995 /* disconnect handlers for notify::* signals */
996 g_signal_handlers_disconnect_by_func (action,
997 G_CALLBACK (gtk_action_sync_sensitivity),
999 g_signal_handlers_disconnect_by_func (action,
1000 G_CALLBACK (gtk_action_sync_property),
1003 g_signal_handlers_disconnect_by_func (action,
1004 G_CALLBACK (gtk_action_sync_stock_id), proxy);
1006 /* menu item specific synchronisers ... */
1007 g_signal_handlers_disconnect_by_func (action,
1008 G_CALLBACK (gtk_action_sync_label),
1011 /* toolbar button specific synchronisers ... */
1012 g_signal_handlers_disconnect_by_func (action,
1013 G_CALLBACK (gtk_action_sync_short_label),
1016 g_signal_handlers_disconnect_by_func (proxy,
1017 G_CALLBACK (gtk_action_create_menu_proxy),
1020 if (action->private_data->action_group)
1021 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
1025 _gtk_action_emit_activate (GtkAction *action)
1027 GtkActionGroup *group = action->private_data->action_group;
1031 g_object_ref (group);
1032 _gtk_action_group_emit_pre_activate (group, action);
1035 g_signal_emit (action, action_signals[ACTIVATE], 0);
1039 _gtk_action_group_emit_post_activate (group, action);
1040 g_object_unref (group);
1045 * gtk_action_activate:
1046 * @action: the action object
1048 * Emits the "activate" signal on the specified action, if it isn't
1049 * insensitive. This gets called by the proxy widgets when they get
1052 * It can also be used to manually activate an action.
1057 gtk_action_activate (GtkAction *action)
1059 g_return_if_fail (GTK_IS_ACTION (action));
1061 if (gtk_action_is_sensitive (action))
1062 _gtk_action_emit_activate (action);
1066 * gtk_action_create_icon:
1067 * @action: the action object
1068 * @icon_size: the size of the icon that should be created.
1070 * This function is intended for use by action implementations to
1071 * create icons displayed in the proxy widgets.
1073 * Returns: a widget that displays the icon for this action.
1078 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1080 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1082 if (action->private_data->stock_id)
1083 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1089 * gtk_action_create_menu_item:
1090 * @action: the action object
1092 * Creates a menu item widget that proxies for the given action.
1094 * Returns: a menu item connected to the action.
1099 gtk_action_create_menu_item (GtkAction *action)
1101 GtkWidget *menu_item;
1103 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1105 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1107 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1113 * gtk_action_create_tool_item:
1114 * @action: the action object
1116 * Creates a toolbar item widget that proxies for the given action.
1118 * Returns: a toolbar item connected to the action.
1123 gtk_action_create_tool_item (GtkAction *action)
1127 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1129 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1131 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1137 * gtk_action_connect_proxy:
1138 * @action: the action object
1139 * @proxy: the proxy widget
1141 * Connects a widget to an action object as a proxy. Synchronises
1142 * various properties of the action with the widget (such as label
1143 * text, icon, tooltip, etc), and attaches a callback so that the
1144 * action gets activated when the proxy widget does.
1146 * If the widget is already connected to an action, it is disconnected
1152 gtk_action_connect_proxy (GtkAction *action,
1155 GtkAction *prev_action;
1157 g_return_if_fail (GTK_IS_ACTION (action));
1158 g_return_if_fail (GTK_IS_WIDGET (proxy));
1160 prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1163 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1165 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1169 * gtk_action_disconnect_proxy:
1170 * @action: the action object
1171 * @proxy: the proxy widget
1173 * Disconnects a proxy widget from an action.
1174 * Does <emphasis>not</emphasis> destroy the widget, however.
1179 gtk_action_disconnect_proxy (GtkAction *action,
1182 g_return_if_fail (GTK_IS_ACTION (action));
1183 g_return_if_fail (GTK_IS_WIDGET (proxy));
1185 g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1187 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1191 * gtk_action_get_proxies:
1192 * @action: the action object
1194 * Returns the proxy widgets for an action.
1196 * Return value: a #GSList of proxy widgets. The list is owned by the action and
1197 * must not be modified.
1202 gtk_action_get_proxies (GtkAction *action)
1204 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1206 return action->private_data->proxies;
1211 * gtk_action_get_name:
1212 * @action: the action object
1214 * Returns the name of the action.
1216 * Return value: the name of the action. The string belongs to GTK+ and should not
1221 G_CONST_RETURN gchar *
1222 gtk_action_get_name (GtkAction *action)
1224 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1226 return action->private_data->name;
1230 * gtk_action_is_sensitive:
1231 * @action: the action object
1233 * Returns whether the action is effectively sensitive.
1235 * Return value: %TRUE if the action and its associated action group
1236 * are both sensitive.
1241 gtk_action_is_sensitive (GtkAction *action)
1243 GtkActionPrivate *priv;
1244 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1246 priv = action->private_data;
1247 return priv->sensitive &&
1248 (priv->action_group == NULL ||
1249 gtk_action_group_get_sensitive (priv->action_group));
1253 * gtk_action_get_sensitive:
1254 * @action: the action object
1256 * Returns whether the action itself is sensitive. Note that this doesn't
1257 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1260 * Return value: %TRUE if the action itself is sensitive.
1265 gtk_action_get_sensitive (GtkAction *action)
1267 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1269 return action->private_data->sensitive;
1273 * gtk_action_set_sensitive:
1274 * @action: the action object
1275 * @sensitive: %TRUE to make the action sensitive
1277 * Sets the ::sensitive property of the action to @sensitive. Note that
1278 * this doesn't necessarily mean effective sensitivity. See
1279 * gtk_action_is_sensitive()
1285 gtk_action_set_sensitive (GtkAction *action,
1288 g_return_if_fail (GTK_IS_ACTION (action));
1290 sensitive = sensitive != FALSE;
1292 if (action->private_data->sensitive != sensitive)
1294 action->private_data->sensitive = sensitive;
1296 g_object_notify (G_OBJECT (action), "sensitive");
1301 * gtk_action_is_visible:
1302 * @action: the action object
1304 * Returns whether the action is effectively visible.
1306 * Return value: %TRUE if the action and its associated action group
1312 gtk_action_is_visible (GtkAction *action)
1314 GtkActionPrivate *priv;
1315 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1317 priv = action->private_data;
1318 return priv->visible &&
1319 (priv->action_group == NULL ||
1320 gtk_action_group_get_visible (priv->action_group));
1324 * gtk_action_get_visible:
1325 * @action: the action object
1327 * Returns whether the action itself is visible. Note that this doesn't
1328 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1331 * Return value: %TRUE if the action itself is visible.
1336 gtk_action_get_visible (GtkAction *action)
1338 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1340 return action->private_data->visible;
1344 * gtk_action_set_visible:
1345 * @action: the action object
1346 * @visible: %TRUE to make the action visible
1348 * Sets the ::visible property of the action to @visible. Note that
1349 * this doesn't necessarily mean effective visibility. See
1350 * gtk_action_is_visible()
1356 gtk_action_set_visible (GtkAction *action,
1359 g_return_if_fail (GTK_IS_ACTION (action));
1361 visible = visible != FALSE;
1363 if (action->private_data->visible != visible)
1365 action->private_data->visible = visible;
1367 g_object_notify (G_OBJECT (action), "visible");
1372 * gtk_action_block_activate_from:
1373 * @action: the action object
1374 * @proxy: a proxy widget
1376 * Disables calls to the gtk_action_activate()
1377 * function by signals on the given proxy widget. This is used to
1378 * break notification loops for things like check or radio actions.
1380 * This function is intended for use by action implementations.
1385 gtk_action_block_activate_from (GtkAction *action,
1388 g_return_if_fail (GTK_IS_ACTION (action));
1390 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1395 * gtk_action_unblock_activate_from:
1396 * @action: the action object
1397 * @proxy: a proxy widget
1399 * Re-enables calls to the gtk_action_activate()
1400 * function by signals on the given proxy widget. This undoes the
1401 * blocking done by gtk_action_block_activate_from().
1403 * This function is intended for use by action implementations.
1408 gtk_action_unblock_activate_from (GtkAction *action,
1411 g_return_if_fail (GTK_IS_ACTION (action));
1413 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1418 closure_accel_activate (GClosure *closure,
1419 GValue *return_value,
1420 guint n_param_values,
1421 const GValue *param_values,
1422 gpointer invocation_hint,
1423 gpointer marshal_data)
1425 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1427 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1429 /* we handled the accelerator */
1430 g_value_set_boolean (return_value, TRUE);
1435 gtk_action_set_action_group (GtkAction *action,
1436 GtkActionGroup *action_group)
1438 g_return_if_fail (GTK_IS_ACTION (action));
1440 if (action->private_data->action_group == NULL)
1441 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1443 g_return_if_fail (action_group == NULL);
1445 action->private_data->action_group = action_group;
1449 * gtk_action_set_accel_path:
1450 * @action: the action object
1451 * @accel_path: the accelerator path
1453 * Sets the accel path for this action. All proxy widgets associated
1454 * with the action will have this accel path, so that their
1455 * accelerators are consistent.
1460 gtk_action_set_accel_path (GtkAction *action,
1461 const gchar *accel_path)
1463 g_return_if_fail (GTK_IS_ACTION (action));
1465 action->private_data->accel_quark = g_quark_from_string (accel_path);
1469 * gtk_action_get_accel_path:
1470 * @action: the action object
1472 * Returns the accel path for this action.
1476 * Returns: the accel path for this action, or %NULL
1477 * if none is set. The returned string is owned by GTK+
1478 * and must not be freed or modified.
1480 G_CONST_RETURN gchar *
1481 gtk_action_get_accel_path (GtkAction *action)
1483 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1485 if (action->private_data->accel_quark)
1486 return g_quark_to_string (action->private_data->accel_quark);
1493 * gtk_action_set_accel_group:
1494 * @action: the action object
1495 * @accel_group: a #GtkAccelGroup or %NULL
1497 * Sets the #GtkAccelGroup in which the accelerator for this action
1498 * will be installed.
1503 gtk_action_set_accel_group (GtkAction *action,
1504 GtkAccelGroup *accel_group)
1506 g_return_if_fail (GTK_IS_ACTION (action));
1507 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1510 g_object_ref (accel_group);
1511 if (action->private_data->accel_group)
1512 g_object_unref (action->private_data->accel_group);
1514 action->private_data->accel_group = accel_group;
1518 * gtk_action_connect_accelerator:
1519 * @action: a #GtkAction
1521 * Installs the accelerator for @action if @action has an
1522 * accel path and group. See gtk_action_set_accel_path() and
1523 * gtk_action_set_accel_group()
1525 * Since multiple proxies may independently trigger the installation
1526 * of the accelerator, the @action counts the number of times this
1527 * function has been called and doesn't remove the accelerator until
1528 * gtk_action_disconnect_accelerator() has been called as many times.
1533 gtk_action_connect_accelerator (GtkAction *action)
1535 g_return_if_fail (GTK_IS_ACTION (action));
1537 if (!action->private_data->accel_quark ||
1538 !action->private_data->accel_group)
1541 if (action->private_data->accel_count == 0)
1543 const gchar *accel_path =
1544 g_quark_to_string (action->private_data->accel_quark);
1546 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1548 action->private_data->accel_closure);
1551 action->private_data->accel_count++;
1555 * gtk_action_disconnect_accelerator:
1556 * @action: a #GtkAction
1558 * Undoes the effect of one call to gtk_action_connect_accelerator().
1563 gtk_action_disconnect_accelerator (GtkAction *action)
1565 g_return_if_fail (GTK_IS_ACTION (action));
1567 if (!action->private_data->accel_quark ||
1568 !action->private_data->accel_group)
1571 action->private_data->accel_count--;
1573 if (action->private_data->accel_count == 0)
1574 gtk_accel_group_disconnect (action->private_data->accel_group,
1575 action->private_data->accel_closure);
1578 #define __GTK_ACTION_C__
1579 #include "gtkaliasdef.c"