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"
37 #include "gtkiconfactory.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"
48 #include "gtkprivate.h"
52 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
54 struct _GtkActionPrivate
60 gchar *stock_id; /* stock icon */
61 gchar *icon_name; /* themed icon */
65 guint label_set : 1; /* these two used so we can set label */
66 guint short_label_set : 1; /* based on stock id */
67 guint visible_horizontal : 1;
68 guint visible_vertical : 1;
69 guint is_important : 1;
70 guint hide_if_empty : 1;
71 guint visible_overflown : 1;
75 GtkAccelGroup *accel_group;
76 GClosure *accel_closure;
79 GtkActionGroup *action_group;
81 /* list of proxy widgets */
100 PROP_VISIBLE_HORIZONTAL,
101 PROP_VISIBLE_VERTICAL,
102 PROP_VISIBLE_OVERFLOWN,
111 static GQuark accel_path_id = 0;
112 static GQuark quark_gtk_action_proxy = 0;
113 static const gchar accel_path_key[] = "GtkAction::accel_path";
114 static const gchar gtk_action_proxy_key[] = "gtk-action";
116 G_DEFINE_TYPE (GtkAction, gtk_action, G_TYPE_OBJECT)
118 static void gtk_action_finalize (GObject *object);
119 static void gtk_action_set_property (GObject *object,
123 static void gtk_action_get_property (GObject *object,
127 static void gtk_action_set_action_group (GtkAction *action,
128 GtkActionGroup *action_group);
129 static void gtk_action_set_is_important (GtkAction *action,
130 gboolean is_important);
131 static void gtk_action_set_label (GtkAction *action,
133 static void gtk_action_set_short_label (GtkAction *action,
135 static void gtk_action_set_visible_horizontal (GtkAction *action,
136 gboolean visible_horizontal);
137 static void gtk_action_set_visible_vertical (GtkAction *action,
138 gboolean visible_vertical);
139 static void gtk_action_set_tooltip (GtkAction *action,
140 const gchar *tooltip);
141 static void gtk_action_set_stock_id (GtkAction *action,
142 const gchar *stock_id);
143 static void gtk_action_set_icon_name (GtkAction *action,
144 const gchar *icon_name);
145 static void gtk_action_sync_tooltip (GtkAction *action,
148 static GtkWidget *create_menu_item (GtkAction *action);
149 static GtkWidget *create_tool_item (GtkAction *action);
150 static void connect_proxy (GtkAction *action,
152 static void disconnect_proxy (GtkAction *action,
154 static void closure_accel_activate (GClosure *closure,
155 GValue *return_value,
156 guint n_param_values,
157 const GValue *param_values,
158 gpointer invocation_hint,
159 gpointer marshal_data);
161 static guint action_signals[LAST_SIGNAL] = { 0 };
165 gtk_action_class_init (GtkActionClass *klass)
167 GObjectClass *gobject_class;
169 accel_path_id = g_quark_from_static_string (accel_path_key);
170 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
172 gobject_class = G_OBJECT_CLASS (klass);
174 gobject_class->finalize = gtk_action_finalize;
175 gobject_class->set_property = gtk_action_set_property;
176 gobject_class->get_property = gtk_action_get_property;
178 klass->activate = NULL;
180 klass->create_menu_item = create_menu_item;
181 klass->create_tool_item = create_tool_item;
182 klass->connect_proxy = connect_proxy;
183 klass->disconnect_proxy = disconnect_proxy;
185 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
186 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
188 g_object_class_install_property (gobject_class,
190 g_param_spec_string ("name",
192 P_("A unique name for the action."),
194 GTK_PARAM_READWRITE |
195 G_PARAM_CONSTRUCT_ONLY));
196 g_object_class_install_property (gobject_class,
198 g_param_spec_string ("label",
200 P_("The label used for menu items and buttons "
201 "that activate this action."),
203 GTK_PARAM_READWRITE));
204 g_object_class_install_property (gobject_class,
206 g_param_spec_string ("short-label",
208 P_("A shorter label that may be used on toolbar buttons."),
210 GTK_PARAM_READWRITE));
211 g_object_class_install_property (gobject_class,
213 g_param_spec_string ("tooltip",
215 P_("A tooltip for this action."),
217 GTK_PARAM_READWRITE));
218 g_object_class_install_property (gobject_class,
220 g_param_spec_string ("stock-id",
222 P_("The stock icon displayed in widgets representing "
225 GTK_PARAM_READWRITE));
227 * GtkAction:icon-name:
229 * The name of the icon from the icon theme.
230 * Note that the stock icon is preferred, if
231 * the ::stock-id property holds the id of an
232 * existing stock icon.
236 g_object_class_install_property (gobject_class,
238 g_param_spec_string ("icon-name",
240 P_("The name of the icon from the icon theme"),
242 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
244 PROP_VISIBLE_HORIZONTAL,
245 g_param_spec_boolean ("visible-horizontal",
246 P_("Visible when horizontal"),
247 P_("Whether the toolbar item is visible when the toolbar "
248 "is in a horizontal orientation."),
250 GTK_PARAM_READWRITE));
252 * GtkAction:visible-overflown:
254 * When %TRUE, toolitem proxies for this action are represented in the
255 * toolbar overflow menu.
259 g_object_class_install_property (gobject_class,
260 PROP_VISIBLE_OVERFLOWN,
261 g_param_spec_boolean ("visible-overflown",
262 P_("Visible when overflown"),
263 P_("When TRUE, toolitem proxies for this action "
264 "are represented in the toolbar overflow menu."),
266 GTK_PARAM_READWRITE));
267 g_object_class_install_property (gobject_class,
268 PROP_VISIBLE_VERTICAL,
269 g_param_spec_boolean ("visible-vertical",
270 P_("Visible when vertical"),
271 P_("Whether the toolbar item is visible when the toolbar "
272 "is in a vertical orientation."),
274 GTK_PARAM_READWRITE));
275 g_object_class_install_property (gobject_class,
277 g_param_spec_boolean ("is-important",
279 P_("Whether the action is considered important. "
280 "When TRUE, toolitem proxies for this action "
281 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
283 GTK_PARAM_READWRITE));
284 g_object_class_install_property (gobject_class,
286 g_param_spec_boolean ("hide-if-empty",
288 P_("When TRUE, empty menu proxies for this action are hidden."),
290 GTK_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class,
293 g_param_spec_boolean ("sensitive",
295 P_("Whether the action is enabled."),
297 GTK_PARAM_READWRITE));
298 g_object_class_install_property (gobject_class,
300 g_param_spec_boolean ("visible",
302 P_("Whether the action is visible."),
304 GTK_PARAM_READWRITE));
305 g_object_class_install_property (gobject_class,
307 g_param_spec_object ("action-group",
309 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
310 GTK_TYPE_ACTION_GROUP,
311 GTK_PARAM_READWRITE));
314 * GtkAction::activate:
315 * @action: the #GtkAction
317 * The "activate" signal is emitted when the action is activated.
321 action_signals[ACTIVATE] =
322 g_signal_new (I_("activate"),
323 G_OBJECT_CLASS_TYPE (klass),
324 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
325 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
326 g_cclosure_marshal_VOID__VOID,
329 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
334 gtk_action_init (GtkAction *action)
336 action->private_data = GTK_ACTION_GET_PRIVATE (action);
338 action->private_data->name = NULL;
339 action->private_data->label = NULL;
340 action->private_data->short_label = NULL;
341 action->private_data->tooltip = NULL;
342 action->private_data->stock_id = NULL;
343 action->private_data->icon_name = NULL;
344 action->private_data->visible_horizontal = TRUE;
345 action->private_data->visible_vertical = TRUE;
346 action->private_data->visible_overflown = TRUE;
347 action->private_data->is_important = FALSE;
348 action->private_data->hide_if_empty = TRUE;
350 action->private_data->sensitive = TRUE;
351 action->private_data->visible = TRUE;
353 action->private_data->label_set = FALSE;
354 action->private_data->short_label_set = FALSE;
356 action->private_data->accel_count = 0;
357 action->private_data->accel_group = NULL;
358 action->private_data->accel_quark = 0;
359 action->private_data->accel_closure =
360 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
361 g_closure_set_marshal (action->private_data->accel_closure,
362 closure_accel_activate);
363 g_closure_ref (action->private_data->accel_closure);
364 g_closure_sink (action->private_data->accel_closure);
366 action->private_data->action_group = NULL;
368 action->private_data->proxies = NULL;
373 * @name: A unique name for the action
374 * @label: the label displayed in menu items and on buttons
375 * @tooltip: a tooltip for the action
376 * @stock_id: the stock icon to display in widgets representing the action
378 * Creates a new #GtkAction object. To add the action to a
379 * #GtkActionGroup and set the accelerator for the action,
380 * call gtk_action_group_add_action_with_accel().
381 * See <xref linkend="XML-UI"/> for information on allowed action
384 * Return value: a new #GtkAction
389 gtk_action_new (const gchar *name,
391 const gchar *tooltip,
392 const gchar *stock_id)
396 action = g_object_new (GTK_TYPE_ACTION,
400 "stock_id", stock_id,
407 gtk_action_finalize (GObject *object)
410 action = GTK_ACTION (object);
412 g_free (action->private_data->name);
413 g_free (action->private_data->label);
414 g_free (action->private_data->short_label);
415 g_free (action->private_data->tooltip);
416 g_free (action->private_data->stock_id);
417 g_free (action->private_data->icon_name);
419 g_closure_unref (action->private_data->accel_closure);
420 if (action->private_data->accel_group)
421 g_object_unref (action->private_data->accel_group);
423 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
427 gtk_action_set_property (GObject *object,
435 action = GTK_ACTION (object);
440 tmp = action->private_data->name;
441 action->private_data->name = g_value_dup_string (value);
445 gtk_action_set_label (action, g_value_get_string (value));
447 case PROP_SHORT_LABEL:
448 gtk_action_set_short_label (action, g_value_get_string (value));
451 gtk_action_set_tooltip (action, g_value_get_string (value));
454 gtk_action_set_stock_id (action, g_value_get_string (value));
457 gtk_action_set_icon_name (action, g_value_get_string (value));
459 case PROP_VISIBLE_HORIZONTAL:
460 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
462 case PROP_VISIBLE_VERTICAL:
463 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
465 case PROP_VISIBLE_OVERFLOWN:
466 action->private_data->visible_overflown = g_value_get_boolean (value);
468 case PROP_IS_IMPORTANT:
469 gtk_action_set_is_important (action, g_value_get_boolean (value));
471 case PROP_HIDE_IF_EMPTY:
472 action->private_data->hide_if_empty = g_value_get_boolean (value);
475 gtk_action_set_sensitive (action, g_value_get_boolean (value));
478 gtk_action_set_visible (action, g_value_get_boolean (value));
480 case PROP_ACTION_GROUP:
481 gtk_action_set_action_group (action, g_value_get_object (value));
484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
490 gtk_action_get_property (GObject *object,
497 action = GTK_ACTION (object);
502 g_value_set_string (value, action->private_data->name);
505 g_value_set_string (value, action->private_data->label);
507 case PROP_SHORT_LABEL:
508 g_value_set_string (value, action->private_data->short_label);
511 g_value_set_string (value, action->private_data->tooltip);
514 g_value_set_string (value, action->private_data->stock_id);
517 g_value_set_string (value, action->private_data->icon_name);
519 case PROP_VISIBLE_HORIZONTAL:
520 g_value_set_boolean (value, action->private_data->visible_horizontal);
522 case PROP_VISIBLE_VERTICAL:
523 g_value_set_boolean (value, action->private_data->visible_vertical);
525 case PROP_VISIBLE_OVERFLOWN:
526 g_value_set_boolean (value, action->private_data->visible_overflown);
528 case PROP_IS_IMPORTANT:
529 g_value_set_boolean (value, action->private_data->is_important);
531 case PROP_HIDE_IF_EMPTY:
532 g_value_set_boolean (value, action->private_data->hide_if_empty);
535 g_value_set_boolean (value, action->private_data->sensitive);
538 g_value_set_boolean (value, action->private_data->visible);
540 case PROP_ACTION_GROUP:
541 g_value_set_object (value, action->private_data->action_group);
544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
550 create_menu_item (GtkAction *action)
552 GType menu_item_type;
554 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
556 return g_object_new (menu_item_type, NULL);
560 create_tool_item (GtkAction *action)
562 GType toolbar_item_type;
564 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
566 return g_object_new (toolbar_item_type, NULL);
570 remove_proxy (GtkAction *action,
573 if (GTK_IS_MENU_ITEM (proxy))
574 gtk_action_disconnect_accelerator (action);
576 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
580 * _gtk_action_sync_menu_visible:
581 * @action: a #GtkAction, or %NULL to determine the action from @proxy
582 * @proxy: a proxy menu item
583 * @empty: whether the submenu attached to @proxy is empty
585 * Updates the visibility of @proxy from the visibility of @action
586 * according to the following rules:
588 * <listitem><para>if @action is invisible, @proxy is too
590 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
591 * property of @action indicates otherwise
595 * This function is used in the implementation of #GtkUIManager.
598 _gtk_action_sync_menu_visible (GtkAction *action,
602 gboolean visible, hide_if_empty;
604 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
605 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
608 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
610 visible = gtk_action_is_visible (action);
611 hide_if_empty = action->private_data->hide_if_empty;
613 if (visible && !(empty && hide_if_empty))
614 gtk_widget_show (proxy);
616 gtk_widget_hide (proxy);
619 gboolean _gtk_menu_is_empty (GtkWidget *menu);
622 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
625 GtkWidget *menu_item;
627 if (action->private_data->visible_overflown)
629 menu_item = gtk_action_create_menu_item (action);
631 g_object_ref_sink (menu_item);
633 gtk_tool_item_set_proxy_menu_item (tool_item,
634 "gtk-action-menu-item", menu_item);
635 g_object_unref (menu_item);
638 gtk_tool_item_set_proxy_menu_item (tool_item,
639 "gtk-action-menu-item", NULL);
645 connect_proxy (GtkAction *action,
648 g_object_ref (action);
649 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
652 /* add this widget to the list of proxies */
653 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
654 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
656 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
657 if (gtk_action_is_visible (action))
658 gtk_widget_show (proxy);
660 gtk_widget_hide (proxy);
661 gtk_widget_set_no_show_all (proxy, TRUE);
663 if (GTK_IS_MENU_ITEM (proxy))
666 /* menu item specific synchronisers ... */
668 if (action->private_data->accel_quark)
670 gtk_action_connect_accelerator (action);
671 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
672 g_quark_to_string (action->private_data->accel_quark));
675 label = GTK_BIN (proxy)->child;
677 /* make sure label is a label */
678 if (label && !GTK_IS_LABEL (label))
680 gtk_container_remove (GTK_CONTAINER (proxy), label);
685 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
686 "use-underline", TRUE,
692 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
694 "accel-closure", action->private_data->accel_closure,
697 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
699 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
703 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
704 if (image && !GTK_IS_IMAGE (image))
706 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
711 image = gtk_image_new ();
712 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
714 gtk_widget_show (image);
717 if (action->private_data->stock_id &&
718 gtk_icon_factory_lookup_default (action->private_data->stock_id))
719 gtk_image_set_from_stock (GTK_IMAGE (image),
720 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
721 else if (action->private_data->icon_name)
722 gtk_image_set_from_icon_name (GTK_IMAGE (image),
723 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
726 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
727 g_signal_connect_object (proxy, "activate",
728 G_CALLBACK (gtk_action_activate), action,
732 else if (GTK_IS_TOOL_ITEM (proxy))
734 /* toolbar item specific synchronisers ... */
737 "visible-horizontal", action->private_data->visible_horizontal,
738 "visible-vertical", action->private_data->visible_vertical,
739 "is-important", action->private_data->is_important,
742 gtk_action_sync_tooltip (action, proxy);
744 g_signal_connect_object (proxy, "create_menu_proxy",
745 G_CALLBACK (gtk_action_create_menu_proxy),
748 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
750 /* toolbar button specific synchronisers ... */
751 if (GTK_IS_TOOL_BUTTON (proxy))
754 "label", action->private_data->short_label,
755 "use-underline", TRUE,
756 "stock-id", action->private_data->stock_id,
757 "icon-name", action->private_data->icon_name,
760 g_signal_connect_object (proxy, "clicked",
761 G_CALLBACK (gtk_action_activate), action,
765 else if (GTK_IS_BUTTON (proxy))
767 /* button specific synchronisers ... */
768 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
770 /* synchronise stock-id */
772 "label", action->private_data->stock_id,
777 if (GTK_BIN (proxy)->child == NULL ||
778 GTK_IS_LABEL (GTK_BIN (proxy)->child))
780 /* synchronise the label */
782 "label", action->private_data->short_label,
783 "use-underline", TRUE,
787 /* we leave the button alone if there is a custom child */
788 g_signal_connect_object (proxy, "clicked",
789 G_CALLBACK (gtk_action_activate), action,
793 if (action->private_data->action_group)
794 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
798 disconnect_proxy (GtkAction *action,
801 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
803 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
804 remove_proxy (action, proxy);
806 /* disconnect the activate handler */
807 g_signal_handlers_disconnect_by_func (proxy,
808 G_CALLBACK (gtk_action_activate),
811 /* toolbar button specific synchronisers ... */
812 g_signal_handlers_disconnect_by_func (proxy,
813 G_CALLBACK (gtk_action_create_menu_proxy),
816 if (action->private_data->action_group)
817 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
821 _gtk_action_emit_activate (GtkAction *action)
823 GtkActionGroup *group = action->private_data->action_group;
827 g_object_ref (group);
828 _gtk_action_group_emit_pre_activate (group, action);
831 g_signal_emit (action, action_signals[ACTIVATE], 0);
835 _gtk_action_group_emit_post_activate (group, action);
836 g_object_unref (group);
841 * gtk_action_activate:
842 * @action: the action object
844 * Emits the "activate" signal on the specified action, if it isn't
845 * insensitive. This gets called by the proxy widgets when they get
848 * It can also be used to manually activate an action.
853 gtk_action_activate (GtkAction *action)
855 g_return_if_fail (GTK_IS_ACTION (action));
857 if (gtk_action_is_sensitive (action))
858 _gtk_action_emit_activate (action);
862 * gtk_action_create_icon:
863 * @action: the action object
864 * @icon_size: the size of the icon that should be created.
866 * This function is intended for use by action implementations to
867 * create icons displayed in the proxy widgets.
869 * Returns: a widget that displays the icon for this action.
874 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
876 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
878 if (action->private_data->stock_id)
879 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
880 else if (action->private_data->icon_name)
881 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
887 * gtk_action_create_menu_item:
888 * @action: the action object
890 * Creates a menu item widget that proxies for the given action.
892 * Returns: a menu item connected to the action.
897 gtk_action_create_menu_item (GtkAction *action)
899 GtkWidget *menu_item;
901 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
903 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
905 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
911 * gtk_action_create_tool_item:
912 * @action: the action object
914 * Creates a toolbar item widget that proxies for the given action.
916 * Returns: a toolbar item connected to the action.
921 gtk_action_create_tool_item (GtkAction *action)
925 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
927 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
929 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
935 * gtk_action_connect_proxy:
936 * @action: the action object
937 * @proxy: the proxy widget
939 * Connects a widget to an action object as a proxy. Synchronises
940 * various properties of the action with the widget (such as label
941 * text, icon, tooltip, etc), and attaches a callback so that the
942 * action gets activated when the proxy widget does.
944 * If the widget is already connected to an action, it is disconnected
950 gtk_action_connect_proxy (GtkAction *action,
953 GtkAction *prev_action;
955 g_return_if_fail (GTK_IS_ACTION (action));
956 g_return_if_fail (GTK_IS_WIDGET (proxy));
958 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
961 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
963 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
967 * gtk_action_disconnect_proxy:
968 * @action: the action object
969 * @proxy: the proxy widget
971 * Disconnects a proxy widget from an action.
972 * Does <emphasis>not</emphasis> destroy the widget, however.
977 gtk_action_disconnect_proxy (GtkAction *action,
980 g_return_if_fail (GTK_IS_ACTION (action));
981 g_return_if_fail (GTK_IS_WIDGET (proxy));
983 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
985 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
989 * gtk_action_get_proxies:
990 * @action: the action object
992 * Returns the proxy widgets for an action.
993 * See also gtk_widget_get_action().
995 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
996 * and must not be modified.
1001 gtk_action_get_proxies (GtkAction *action)
1003 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1005 return action->private_data->proxies;
1010 * gtk_widget_get_action:
1011 * @widget: a #GtkWidget
1013 * Returns the #GtkAction that @widget is a proxy for.
1014 * See also gtk_action_get_proxies().
1016 * Returns: the action that a widget is a proxy for, or
1017 * %NULL, if it is not attached to an action.
1022 gtk_widget_get_action (GtkWidget *widget)
1024 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1026 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1031 * gtk_action_get_name:
1032 * @action: the action object
1034 * Returns the name of the action.
1036 * Return value: the name of the action. The string belongs to GTK+ and should not
1041 G_CONST_RETURN gchar *
1042 gtk_action_get_name (GtkAction *action)
1044 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1046 return action->private_data->name;
1050 * gtk_action_is_sensitive:
1051 * @action: the action object
1053 * Returns whether the action is effectively sensitive.
1055 * Return value: %TRUE if the action and its associated action group
1056 * are both sensitive.
1061 gtk_action_is_sensitive (GtkAction *action)
1063 GtkActionPrivate *priv;
1064 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1066 priv = action->private_data;
1067 return priv->sensitive &&
1068 (priv->action_group == NULL ||
1069 gtk_action_group_get_sensitive (priv->action_group));
1073 * gtk_action_get_sensitive:
1074 * @action: the action object
1076 * Returns whether the action itself is sensitive. Note that this doesn't
1077 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1080 * Return value: %TRUE if the action itself is sensitive.
1085 gtk_action_get_sensitive (GtkAction *action)
1087 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1089 return action->private_data->sensitive;
1093 _gtk_action_sync_sensitive (GtkAction *action)
1099 sensitive = gtk_action_is_sensitive (action);
1101 for (p = action->private_data->proxies; p; p = p->next)
1103 proxy = (GtkWidget *)p->data;
1104 gtk_widget_set_sensitive (proxy, sensitive);
1109 * gtk_action_set_sensitive:
1110 * @action: the action object
1111 * @sensitive: %TRUE to make the action sensitive
1113 * Sets the ::sensitive property of the action to @sensitive. Note that
1114 * this doesn't necessarily mean effective sensitivity. See
1115 * gtk_action_is_sensitive()
1121 gtk_action_set_sensitive (GtkAction *action,
1124 g_return_if_fail (GTK_IS_ACTION (action));
1126 sensitive = sensitive != FALSE;
1128 if (action->private_data->sensitive != sensitive)
1130 action->private_data->sensitive = sensitive;
1132 _gtk_action_sync_sensitive (action);
1134 g_object_notify (G_OBJECT (action), "sensitive");
1139 * gtk_action_is_visible:
1140 * @action: the action object
1142 * Returns whether the action is effectively visible.
1144 * Return value: %TRUE if the action and its associated action group
1150 gtk_action_is_visible (GtkAction *action)
1152 GtkActionPrivate *priv;
1153 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1155 priv = action->private_data;
1156 return priv->visible &&
1157 (priv->action_group == NULL ||
1158 gtk_action_group_get_visible (priv->action_group));
1162 * gtk_action_get_visible:
1163 * @action: the action object
1165 * Returns whether the action itself is visible. Note that this doesn't
1166 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1169 * Return value: %TRUE if the action itself is visible.
1174 gtk_action_get_visible (GtkAction *action)
1176 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1178 return action->private_data->visible;
1182 _gtk_action_sync_visible (GtkAction *action)
1189 visible = gtk_action_is_visible (action);
1191 for (p = action->private_data->proxies; p; p = p->next)
1193 proxy = (GtkWidget *)p->data;
1195 if (GTK_IS_MENU_ITEM (proxy))
1197 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1199 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1204 gtk_widget_show (proxy);
1206 gtk_widget_hide (proxy);
1212 * gtk_action_set_visible:
1213 * @action: the action object
1214 * @visible: %TRUE to make the action visible
1216 * Sets the ::visible property of the action to @visible. Note that
1217 * this doesn't necessarily mean effective visibility. See
1218 * gtk_action_is_visible()
1224 gtk_action_set_visible (GtkAction *action,
1227 g_return_if_fail (GTK_IS_ACTION (action));
1229 visible = visible != FALSE;
1231 if (action->private_data->visible != visible)
1233 action->private_data->visible = visible;
1235 _gtk_action_sync_visible (action);
1237 g_object_notify (G_OBJECT (action), "visible");
1242 gtk_action_set_is_important (GtkAction *action,
1243 gboolean is_important)
1248 is_important = is_important != FALSE;
1250 if (action->private_data->is_important != is_important)
1252 action->private_data->is_important = is_important;
1254 for (p = action->private_data->proxies; p; p = p->next)
1256 proxy = (GtkWidget *)p->data;
1258 if (GTK_IS_TOOL_ITEM (proxy))
1259 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1263 g_object_notify (G_OBJECT (action), "is-important");
1268 gtk_action_set_label (GtkAction *action,
1272 GtkWidget *proxy, *child;
1275 tmp = action->private_data->label;
1276 action->private_data->label = g_strdup (label);
1278 action->private_data->label_set = (action->private_data->label != NULL);
1279 /* if label is unset, then use the label from the stock item */
1280 if (!action->private_data->label_set && action->private_data->stock_id)
1282 GtkStockItem stock_item;
1284 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1285 action->private_data->label = g_strdup (stock_item.label);
1288 for (p = action->private_data->proxies; p; p = p->next)
1290 proxy = (GtkWidget *)p->data;
1292 if (GTK_IS_MENU_ITEM (proxy))
1294 child = GTK_BIN (proxy)->child;
1296 if (GTK_IS_LABEL (child))
1297 gtk_label_set_label (GTK_LABEL (child),
1298 action->private_data->label);
1302 g_object_notify (G_OBJECT (action), "label");
1304 /* if short_label is unset, set short_label=label */
1305 if (!action->private_data->short_label_set)
1307 gtk_action_set_short_label (action, action->private_data->label);
1308 action->private_data->short_label_set = FALSE;
1313 gtk_action_set_short_label (GtkAction *action,
1317 GtkWidget *proxy, *child;
1320 tmp = action->private_data->short_label;
1321 action->private_data->short_label = g_strdup (label);
1323 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1324 /* if short_label is unset, then use the value of label */
1325 if (!action->private_data->short_label_set)
1326 action->private_data->short_label = g_strdup (action->private_data->label);
1328 for (p = action->private_data->proxies; p; p = p->next)
1330 proxy = (GtkWidget *)p->data;
1332 if (GTK_IS_TOOL_BUTTON (proxy))
1333 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1334 action->private_data->label);
1335 else if (GTK_IS_BUTTON (proxy) &&
1336 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1338 child = GTK_BIN (proxy)->child;
1340 if (child == NULL || GTK_IS_LABEL (child))
1341 gtk_button_set_label (GTK_BUTTON (proxy),
1342 action->private_data->label);
1346 g_object_notify (G_OBJECT (action), "short-label");
1350 gtk_action_set_visible_horizontal (GtkAction *action,
1351 gboolean visible_horizontal)
1356 visible_horizontal = visible_horizontal != FALSE;
1358 if (action->private_data->visible_horizontal != visible_horizontal)
1360 action->private_data->visible_horizontal = visible_horizontal;
1362 for (p = action->private_data->proxies; p; p = p->next)
1364 proxy = (GtkWidget *)p->data;
1366 if (GTK_IS_TOOL_ITEM (proxy))
1367 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1368 visible_horizontal);
1371 g_object_notify (G_OBJECT (action), "visible-horizontal");
1376 gtk_action_set_visible_vertical (GtkAction *action,
1377 gboolean visible_vertical)
1382 visible_vertical = visible_vertical != FALSE;
1384 if (action->private_data->visible_vertical != visible_vertical)
1386 action->private_data->visible_vertical = visible_vertical;
1388 for (p = action->private_data->proxies; p; p = p->next)
1390 proxy = (GtkWidget *)p->data;
1392 if (GTK_IS_TOOL_ITEM (proxy))
1393 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1397 g_object_notify (G_OBJECT (action), "visible-vertical");
1402 gtk_action_sync_tooltip (GtkAction *action,
1407 parent = gtk_widget_get_parent (proxy);
1409 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1410 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1411 GTK_TOOLBAR (parent)->tooltips,
1412 action->private_data->tooltip,
1417 gtk_action_set_tooltip (GtkAction *action,
1418 const gchar *tooltip)
1424 tmp = action->private_data->tooltip;
1425 action->private_data->tooltip = g_strdup (tooltip);
1428 for (p = action->private_data->proxies; p; p = p->next)
1430 proxy = (GtkWidget *)p->data;
1432 gtk_action_sync_tooltip (action, proxy);
1435 g_object_notify (G_OBJECT (action), "tooltip");
1439 gtk_action_set_stock_id (GtkAction *action,
1440 const gchar *stock_id)
1443 GtkWidget *proxy, *image;
1446 tmp = action->private_data->stock_id;
1447 action->private_data->stock_id = g_strdup (stock_id);
1450 for (p = action->private_data->proxies; p; p = p->next)
1452 proxy = (GtkWidget *)p->data;
1454 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1456 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1458 if (GTK_IS_IMAGE (image))
1459 gtk_image_set_from_stock (GTK_IMAGE (image),
1460 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1462 else if (GTK_IS_TOOL_BUTTON (proxy))
1464 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1465 action->private_data->stock_id);
1467 else if (GTK_IS_BUTTON (proxy) &&
1468 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1470 gtk_button_set_label (GTK_BUTTON (proxy),
1471 action->private_data->stock_id);
1475 g_object_notify (G_OBJECT (action), "stock-id");
1477 /* update label and short_label if appropriate */
1478 if (!action->private_data->label_set)
1480 GtkStockItem stock_item;
1482 if (action->private_data->stock_id &&
1483 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1484 gtk_action_set_label (action, stock_item.label);
1486 gtk_action_set_label (action, NULL);
1488 action->private_data->label_set = FALSE;
1493 gtk_action_set_icon_name (GtkAction *action,
1494 const gchar *icon_name)
1497 GtkWidget *proxy, *image;
1500 tmp = action->private_data->icon_name;
1501 action->private_data->icon_name = g_strdup (icon_name);
1504 for (p = action->private_data->proxies; p; p = p->next)
1506 proxy = (GtkWidget *)p->data;
1508 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1510 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1512 if (GTK_IS_IMAGE (image) &&
1513 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1514 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1515 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1516 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1518 else if (GTK_IS_TOOL_BUTTON (proxy))
1520 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1521 action->private_data->icon_name);
1523 else if (GTK_IS_BUTTON (proxy) &&
1524 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1526 image = gtk_button_get_image (GTK_BUTTON (proxy));
1528 if (GTK_IS_IMAGE (image) &&
1529 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1530 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1531 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1532 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1536 g_object_notify (G_OBJECT (action), "icon-name");
1541 * gtk_action_block_activate_from:
1542 * @action: the action object
1543 * @proxy: a proxy widget
1545 * Disables calls to the gtk_action_activate()
1546 * function by signals on the given proxy widget. This is used to
1547 * break notification loops for things like check or radio actions.
1549 * This function is intended for use by action implementations.
1554 gtk_action_block_activate_from (GtkAction *action,
1557 g_return_if_fail (GTK_IS_ACTION (action));
1559 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1564 * gtk_action_unblock_activate_from:
1565 * @action: the action object
1566 * @proxy: a proxy widget
1568 * Re-enables calls to the gtk_action_activate()
1569 * function by signals on the given proxy widget. This undoes the
1570 * blocking done by gtk_action_block_activate_from().
1572 * This function is intended for use by action implementations.
1577 gtk_action_unblock_activate_from (GtkAction *action,
1580 g_return_if_fail (GTK_IS_ACTION (action));
1582 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1587 closure_accel_activate (GClosure *closure,
1588 GValue *return_value,
1589 guint n_param_values,
1590 const GValue *param_values,
1591 gpointer invocation_hint,
1592 gpointer marshal_data)
1594 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1596 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1598 /* we handled the accelerator */
1599 g_value_set_boolean (return_value, TRUE);
1604 gtk_action_set_action_group (GtkAction *action,
1605 GtkActionGroup *action_group)
1607 g_return_if_fail (GTK_IS_ACTION (action));
1609 if (action->private_data->action_group == NULL)
1610 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1612 g_return_if_fail (action_group == NULL);
1614 action->private_data->action_group = action_group;
1618 * gtk_action_set_accel_path:
1619 * @action: the action object
1620 * @accel_path: the accelerator path
1622 * Sets the accel path for this action. All proxy widgets associated
1623 * with the action will have this accel path, so that their
1624 * accelerators are consistent.
1629 gtk_action_set_accel_path (GtkAction *action,
1630 const gchar *accel_path)
1632 g_return_if_fail (GTK_IS_ACTION (action));
1634 action->private_data->accel_quark = g_quark_from_string (accel_path);
1638 * gtk_action_get_accel_path:
1639 * @action: the action object
1641 * Returns the accel path for this action.
1645 * Returns: the accel path for this action, or %NULL
1646 * if none is set. The returned string is owned by GTK+
1647 * and must not be freed or modified.
1649 G_CONST_RETURN gchar *
1650 gtk_action_get_accel_path (GtkAction *action)
1652 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1654 if (action->private_data->accel_quark)
1655 return g_quark_to_string (action->private_data->accel_quark);
1661 * gtk_action_get_accel_closure:
1662 * @action: the action object
1664 * Returns the accel closure for this action.
1668 * Returns: the accel closure for this action. The returned closure is
1669 * owned by GTK+ and must not be unreffed or modified.
1672 gtk_action_get_accel_closure (GtkAction *action)
1674 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1676 return action->private_data->accel_closure;
1681 * gtk_action_set_accel_group:
1682 * @action: the action object
1683 * @accel_group: a #GtkAccelGroup or %NULL
1685 * Sets the #GtkAccelGroup in which the accelerator for this action
1686 * will be installed.
1691 gtk_action_set_accel_group (GtkAction *action,
1692 GtkAccelGroup *accel_group)
1694 g_return_if_fail (GTK_IS_ACTION (action));
1695 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1698 g_object_ref (accel_group);
1699 if (action->private_data->accel_group)
1700 g_object_unref (action->private_data->accel_group);
1702 action->private_data->accel_group = accel_group;
1706 * gtk_action_connect_accelerator:
1707 * @action: a #GtkAction
1709 * Installs the accelerator for @action if @action has an
1710 * accel path and group. See gtk_action_set_accel_path() and
1711 * gtk_action_set_accel_group()
1713 * Since multiple proxies may independently trigger the installation
1714 * of the accelerator, the @action counts the number of times this
1715 * function has been called and doesn't remove the accelerator until
1716 * gtk_action_disconnect_accelerator() has been called as many times.
1721 gtk_action_connect_accelerator (GtkAction *action)
1723 g_return_if_fail (GTK_IS_ACTION (action));
1725 if (!action->private_data->accel_quark ||
1726 !action->private_data->accel_group)
1729 if (action->private_data->accel_count == 0)
1731 const gchar *accel_path =
1732 g_quark_to_string (action->private_data->accel_quark);
1734 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1736 action->private_data->accel_closure);
1739 action->private_data->accel_count++;
1743 * gtk_action_disconnect_accelerator:
1744 * @action: a #GtkAction
1746 * Undoes the effect of one call to gtk_action_connect_accelerator().
1751 gtk_action_disconnect_accelerator (GtkAction *action)
1753 g_return_if_fail (GTK_IS_ACTION (action));
1755 if (!action->private_data->accel_quark ||
1756 !action->private_data->accel_group)
1759 action->private_data->accel_count--;
1761 if (action->private_data->accel_count == 0)
1762 gtk_accel_group_disconnect (action->private_data->accel_group,
1763 action->private_data->accel_closure);
1766 #define __GTK_ACTION_C__
1767 #include "gtkaliasdef.c"