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"
47 #include "gtkprivate.h"
51 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
53 struct _GtkActionPrivate
59 gchar *stock_id; /* icon */
63 guint label_set : 1; /* these two used so we can set label */
64 guint short_label_set : 1; /* based on stock id */
65 guint visible_horizontal : 1;
66 guint visible_vertical : 1;
67 guint is_important : 1;
68 guint hide_if_empty : 1;
69 guint visible_overflown : 1;
73 GtkAccelGroup *accel_group;
74 GClosure *accel_closure;
77 GtkActionGroup *action_group;
79 /* list of proxy widgets */
97 PROP_VISIBLE_HORIZONTAL,
98 PROP_VISIBLE_VERTICAL,
99 PROP_VISIBLE_OVERFLOWN,
107 static void gtk_action_init (GtkAction *action);
108 static void gtk_action_class_init (GtkActionClass *class);
110 static GQuark accel_path_id = 0;
111 static GQuark quark_gtk_action_proxy = 0;
112 static const gchar accel_path_key[] = "GtkAction::accel_path";
113 static const gchar gtk_action_proxy_key[] = "gtk-action";
116 gtk_action_get_type (void)
118 static GtkType type = 0;
122 static const GTypeInfo type_info =
124 sizeof (GtkActionClass),
125 (GBaseInitFunc) NULL,
126 (GBaseFinalizeFunc) NULL,
127 (GClassInitFunc) gtk_action_class_init,
128 (GClassFinalizeFunc) NULL,
133 (GInstanceInitFunc) gtk_action_init,
136 type = g_type_register_static (G_TYPE_OBJECT,
143 static void gtk_action_finalize (GObject *object);
144 static void gtk_action_set_property (GObject *object,
148 static void gtk_action_get_property (GObject *object,
152 static void gtk_action_set_action_group (GtkAction *action,
153 GtkActionGroup *action_group);
154 static void gtk_action_set_is_important (GtkAction *action,
155 gboolean is_important);
156 static void gtk_action_set_label (GtkAction *action,
158 static void gtk_action_set_short_label (GtkAction *action,
160 static void gtk_action_set_visible_horizontal (GtkAction *action,
161 gboolean visible_horizontal);
162 static void gtk_action_set_visible_vertical (GtkAction *action,
163 gboolean visible_vertical);
164 static void gtk_action_set_tooltip (GtkAction *action,
165 const gchar *tooltip);
166 static void gtk_action_set_stock_id (GtkAction *action,
167 const gchar *stock_id);
168 static void gtk_action_sync_tooltip (GtkAction *action,
171 static GtkWidget *create_menu_item (GtkAction *action);
172 static GtkWidget *create_tool_item (GtkAction *action);
173 static void connect_proxy (GtkAction *action,
175 static void disconnect_proxy (GtkAction *action,
177 static void closure_accel_activate (GClosure *closure,
178 GValue *return_value,
179 guint n_param_values,
180 const GValue *param_values,
181 gpointer invocation_hint,
182 gpointer marshal_data);
184 static GObjectClass *parent_class = NULL;
185 static guint action_signals[LAST_SIGNAL] = { 0 };
189 gtk_action_class_init (GtkActionClass *klass)
191 GObjectClass *gobject_class;
193 accel_path_id = g_quark_from_static_string (accel_path_key);
194 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
196 parent_class = g_type_class_peek_parent (klass);
197 gobject_class = G_OBJECT_CLASS (klass);
199 gobject_class->finalize = gtk_action_finalize;
200 gobject_class->set_property = gtk_action_set_property;
201 gobject_class->get_property = gtk_action_get_property;
203 klass->activate = NULL;
205 klass->create_menu_item = create_menu_item;
206 klass->create_tool_item = create_tool_item;
207 klass->connect_proxy = connect_proxy;
208 klass->disconnect_proxy = disconnect_proxy;
210 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
211 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
213 g_object_class_install_property (gobject_class,
215 g_param_spec_string ("name",
217 P_("A unique name for the action."),
219 GTK_PARAM_READWRITE |
220 G_PARAM_CONSTRUCT_ONLY));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_string ("label",
225 P_("The label used for menu items and buttons "
226 "that activate this action."),
228 GTK_PARAM_READWRITE));
229 g_object_class_install_property (gobject_class,
231 g_param_spec_string ("short-label",
233 P_("A shorter label that may be used on toolbar buttons."),
235 GTK_PARAM_READWRITE));
236 g_object_class_install_property (gobject_class,
238 g_param_spec_string ("tooltip",
240 P_("A tooltip for this action."),
242 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_string ("stock-id",
247 P_("The stock icon displayed in widgets representing "
250 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
252 PROP_VISIBLE_HORIZONTAL,
253 g_param_spec_boolean ("visible-horizontal",
254 P_("Visible when horizontal"),
255 P_("Whether the toolbar item is visible when the toolbar "
256 "is in a horizontal orientation."),
258 GTK_PARAM_READWRITE));
260 * GtkAction:visible-overflown:
262 * When %TRUE, toolitem proxies for this action are represented in the
263 * toolbar overflow menu.
267 g_object_class_install_property (gobject_class,
268 PROP_VISIBLE_OVERFLOWN,
269 g_param_spec_boolean ("visible-overflown",
270 P_("Visible when overflown"),
271 P_("When TRUE, toolitem proxies for this action "
272 "are represented in the toolbar overflow menu."),
274 GTK_PARAM_READWRITE));
275 g_object_class_install_property (gobject_class,
276 PROP_VISIBLE_VERTICAL,
277 g_param_spec_boolean ("visible-vertical",
278 P_("Visible when vertical"),
279 P_("Whether the toolbar item is visible when the toolbar "
280 "is in a vertical orientation."),
282 GTK_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class,
285 g_param_spec_boolean ("is-important",
287 P_("Whether the action is considered important. "
288 "When TRUE, toolitem proxies for this action "
289 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
291 GTK_PARAM_READWRITE));
292 g_object_class_install_property (gobject_class,
294 g_param_spec_boolean ("hide-if-empty",
296 P_("When TRUE, empty menu proxies for this action are hidden."),
298 GTK_PARAM_READWRITE));
299 g_object_class_install_property (gobject_class,
301 g_param_spec_boolean ("sensitive",
303 P_("Whether the action is enabled."),
305 GTK_PARAM_READWRITE));
306 g_object_class_install_property (gobject_class,
308 g_param_spec_boolean ("visible",
310 P_("Whether the action is visible."),
312 GTK_PARAM_READWRITE));
313 g_object_class_install_property (gobject_class,
315 g_param_spec_object ("action-group",
317 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
318 GTK_TYPE_ACTION_GROUP,
319 GTK_PARAM_READWRITE));
322 * GtkAction::activate:
323 * @action: the #GtkAction
325 * The "activate" signal is emitted when the action is activated.
329 action_signals[ACTIVATE] =
330 g_signal_new (I_("activate"),
331 G_OBJECT_CLASS_TYPE (klass),
332 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
333 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
334 g_cclosure_marshal_VOID__VOID,
337 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
342 gtk_action_init (GtkAction *action)
344 action->private_data = GTK_ACTION_GET_PRIVATE (action);
346 action->private_data->name = NULL;
347 action->private_data->label = NULL;
348 action->private_data->short_label = NULL;
349 action->private_data->tooltip = NULL;
350 action->private_data->stock_id = NULL;
351 action->private_data->visible_horizontal = TRUE;
352 action->private_data->visible_vertical = TRUE;
353 action->private_data->visible_overflown = TRUE;
354 action->private_data->is_important = FALSE;
355 action->private_data->hide_if_empty = TRUE;
357 action->private_data->sensitive = TRUE;
358 action->private_data->visible = TRUE;
360 action->private_data->label_set = FALSE;
361 action->private_data->short_label_set = FALSE;
363 action->private_data->accel_count = 0;
364 action->private_data->accel_group = NULL;
365 action->private_data->accel_quark = 0;
366 action->private_data->accel_closure =
367 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
368 g_closure_set_marshal (action->private_data->accel_closure,
369 closure_accel_activate);
370 g_closure_ref (action->private_data->accel_closure);
371 g_closure_sink (action->private_data->accel_closure);
373 action->private_data->action_group = NULL;
375 action->private_data->proxies = NULL;
380 * @name: A unique name for the action
381 * @label: the label displayed in menu items and on buttons
382 * @tooltip: a tooltip for the action
383 * @stock_id: the stock icon to display in widgets representing the action
385 * Creates a new #GtkAction object. To add the action to a
386 * #GtkActionGroup and set the accelerator for the action,
387 * call gtk_action_group_add_action_with_accel().
388 * See <xref linkend="XML-UI"/> for information on allowed action
391 * Return value: a new #GtkAction
396 gtk_action_new (const gchar *name,
398 const gchar *tooltip,
399 const gchar *stock_id)
403 action = g_object_new (GTK_TYPE_ACTION,
407 "stock_id", stock_id,
414 gtk_action_finalize (GObject *object)
417 action = GTK_ACTION (object);
419 g_free (action->private_data->name);
420 g_free (action->private_data->label);
421 g_free (action->private_data->short_label);
422 g_free (action->private_data->tooltip);
423 g_free (action->private_data->stock_id);
425 g_closure_unref (action->private_data->accel_closure);
426 if (action->private_data->accel_group)
427 g_object_unref (action->private_data->accel_group);
429 G_OBJECT_CLASS (parent_class)->finalize (object);
433 gtk_action_set_property (GObject *object,
441 action = GTK_ACTION (object);
446 tmp = action->private_data->name;
447 action->private_data->name = g_value_dup_string (value);
451 gtk_action_set_label (action, g_value_get_string (value));
453 case PROP_SHORT_LABEL:
454 gtk_action_set_short_label (action, g_value_get_string (value));
457 gtk_action_set_tooltip (action, g_value_get_string (value));
460 gtk_action_set_stock_id (action, g_value_get_string (value));
462 case PROP_VISIBLE_HORIZONTAL:
463 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
465 case PROP_VISIBLE_VERTICAL:
466 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
468 case PROP_VISIBLE_OVERFLOWN:
469 action->private_data->visible_overflown = g_value_get_boolean (value);
471 case PROP_IS_IMPORTANT:
472 gtk_action_set_is_important (action, g_value_get_boolean (value));
474 case PROP_HIDE_IF_EMPTY:
475 action->private_data->hide_if_empty = g_value_get_boolean (value);
478 gtk_action_set_sensitive (action, g_value_get_boolean (value));
481 gtk_action_set_visible (action, g_value_get_boolean (value));
483 case PROP_ACTION_GROUP:
484 gtk_action_set_action_group (action, g_value_get_object (value));
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
493 gtk_action_get_property (GObject *object,
500 action = GTK_ACTION (object);
505 g_value_set_string (value, action->private_data->name);
508 g_value_set_string (value, action->private_data->label);
510 case PROP_SHORT_LABEL:
511 g_value_set_string (value, action->private_data->short_label);
514 g_value_set_string (value, action->private_data->tooltip);
517 g_value_set_string (value, action->private_data->stock_id);
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_from_stock (NULL,
713 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
715 gtk_widget_show (image);
717 gtk_image_set_from_stock (GTK_IMAGE (image),
718 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
721 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
722 g_signal_connect_object (proxy, "activate",
723 G_CALLBACK (gtk_action_activate), action,
727 else if (GTK_IS_TOOL_ITEM (proxy))
729 /* toolbar item specific synchronisers ... */
732 "visible_horizontal", action->private_data->visible_horizontal,
733 "visible_vertical", action->private_data->visible_vertical,
734 "is_important", action->private_data->is_important,
737 gtk_action_sync_tooltip (action, proxy);
739 g_signal_connect_object (proxy, "create_menu_proxy",
740 G_CALLBACK (gtk_action_create_menu_proxy),
743 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
745 /* toolbar button specific synchronisers ... */
746 if (GTK_IS_TOOL_BUTTON (proxy))
749 "label", action->private_data->short_label,
750 "use_underline", TRUE,
751 "stock_id", action->private_data->stock_id,
754 g_signal_connect_object (proxy, "clicked",
755 G_CALLBACK (gtk_action_activate), action,
759 else if (GTK_IS_BUTTON (proxy))
761 /* button specific synchronisers ... */
762 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
764 /* synchronise stock-id */
766 "label", action->private_data->stock_id,
769 else if (GTK_BIN (proxy)->child == NULL ||
770 GTK_IS_LABEL (GTK_BIN (proxy)->child))
772 /* synchronise the label */
774 "label", action->private_data->short_label,
775 "use_underline", TRUE,
779 /* we leave the button alone if there is a custom child */
780 g_signal_connect_object (proxy, "clicked",
781 G_CALLBACK (gtk_action_activate), action,
785 if (action->private_data->action_group)
786 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
790 disconnect_proxy (GtkAction *action,
793 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
795 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
796 remove_proxy (action, proxy);
798 /* disconnect the activate handler */
799 g_signal_handlers_disconnect_by_func (proxy,
800 G_CALLBACK (gtk_action_activate),
803 /* toolbar button specific synchronisers ... */
804 g_signal_handlers_disconnect_by_func (proxy,
805 G_CALLBACK (gtk_action_create_menu_proxy),
808 if (action->private_data->action_group)
809 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
813 _gtk_action_emit_activate (GtkAction *action)
815 GtkActionGroup *group = action->private_data->action_group;
819 g_object_ref (group);
820 _gtk_action_group_emit_pre_activate (group, action);
823 g_signal_emit (action, action_signals[ACTIVATE], 0);
827 _gtk_action_group_emit_post_activate (group, action);
828 g_object_unref (group);
833 * gtk_action_activate:
834 * @action: the action object
836 * Emits the "activate" signal on the specified action, if it isn't
837 * insensitive. This gets called by the proxy widgets when they get
840 * It can also be used to manually activate an action.
845 gtk_action_activate (GtkAction *action)
847 g_return_if_fail (GTK_IS_ACTION (action));
849 if (gtk_action_is_sensitive (action))
850 _gtk_action_emit_activate (action);
854 * gtk_action_create_icon:
855 * @action: the action object
856 * @icon_size: the size of the icon that should be created.
858 * This function is intended for use by action implementations to
859 * create icons displayed in the proxy widgets.
861 * Returns: a widget that displays the icon for this action.
866 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
868 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
870 if (action->private_data->stock_id)
871 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
877 * gtk_action_create_menu_item:
878 * @action: the action object
880 * Creates a menu item widget that proxies for the given action.
882 * Returns: a menu item connected to the action.
887 gtk_action_create_menu_item (GtkAction *action)
889 GtkWidget *menu_item;
891 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
893 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
895 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
901 * gtk_action_create_tool_item:
902 * @action: the action object
904 * Creates a toolbar item widget that proxies for the given action.
906 * Returns: a toolbar item connected to the action.
911 gtk_action_create_tool_item (GtkAction *action)
915 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
917 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
919 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
925 * gtk_action_connect_proxy:
926 * @action: the action object
927 * @proxy: the proxy widget
929 * Connects a widget to an action object as a proxy. Synchronises
930 * various properties of the action with the widget (such as label
931 * text, icon, tooltip, etc), and attaches a callback so that the
932 * action gets activated when the proxy widget does.
934 * If the widget is already connected to an action, it is disconnected
940 gtk_action_connect_proxy (GtkAction *action,
943 GtkAction *prev_action;
945 g_return_if_fail (GTK_IS_ACTION (action));
946 g_return_if_fail (GTK_IS_WIDGET (proxy));
948 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
951 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
953 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
957 * gtk_action_disconnect_proxy:
958 * @action: the action object
959 * @proxy: the proxy widget
961 * Disconnects a proxy widget from an action.
962 * Does <emphasis>not</emphasis> destroy the widget, however.
967 gtk_action_disconnect_proxy (GtkAction *action,
970 g_return_if_fail (GTK_IS_ACTION (action));
971 g_return_if_fail (GTK_IS_WIDGET (proxy));
973 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
975 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
979 * gtk_action_get_proxies:
980 * @action: the action object
982 * Returns the proxy widgets for an action.
983 * See also gtk_widget_get_action().
985 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
986 * and must not be modified.
991 gtk_action_get_proxies (GtkAction *action)
993 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
995 return action->private_data->proxies;
1000 * gtk_widget_get_action:
1001 * @widget: a #GtkWidget
1003 * Returns the #GtkAction that @widget is a proxy for.
1004 * See also gtk_action_get_proxies().
1006 * Returns: the action that a widget is a proxy for, or
1007 * %NULL, if it is not attached to an action.
1012 gtk_widget_get_action (GtkWidget *widget)
1014 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1016 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1021 * gtk_action_get_name:
1022 * @action: the action object
1024 * Returns the name of the action.
1026 * Return value: the name of the action. The string belongs to GTK+ and should not
1031 G_CONST_RETURN gchar *
1032 gtk_action_get_name (GtkAction *action)
1034 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1036 return action->private_data->name;
1040 * gtk_action_is_sensitive:
1041 * @action: the action object
1043 * Returns whether the action is effectively sensitive.
1045 * Return value: %TRUE if the action and its associated action group
1046 * are both sensitive.
1051 gtk_action_is_sensitive (GtkAction *action)
1053 GtkActionPrivate *priv;
1054 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1056 priv = action->private_data;
1057 return priv->sensitive &&
1058 (priv->action_group == NULL ||
1059 gtk_action_group_get_sensitive (priv->action_group));
1063 * gtk_action_get_sensitive:
1064 * @action: the action object
1066 * Returns whether the action itself is sensitive. Note that this doesn't
1067 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1070 * Return value: %TRUE if the action itself is sensitive.
1075 gtk_action_get_sensitive (GtkAction *action)
1077 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1079 return action->private_data->sensitive;
1083 _gtk_action_sync_sensitive (GtkAction *action)
1089 sensitive = gtk_action_is_sensitive (action);
1091 for (p = action->private_data->proxies; p; p = p->next)
1093 proxy = (GtkWidget *)p->data;
1094 gtk_widget_set_sensitive (proxy, sensitive);
1099 * gtk_action_set_sensitive:
1100 * @action: the action object
1101 * @sensitive: %TRUE to make the action sensitive
1103 * Sets the ::sensitive property of the action to @sensitive. Note that
1104 * this doesn't necessarily mean effective sensitivity. See
1105 * gtk_action_is_sensitive()
1111 gtk_action_set_sensitive (GtkAction *action,
1114 g_return_if_fail (GTK_IS_ACTION (action));
1116 sensitive = sensitive != FALSE;
1118 if (action->private_data->sensitive != sensitive)
1120 action->private_data->sensitive = sensitive;
1122 _gtk_action_sync_sensitive (action);
1124 g_object_notify (G_OBJECT (action), "sensitive");
1129 * gtk_action_is_visible:
1130 * @action: the action object
1132 * Returns whether the action is effectively visible.
1134 * Return value: %TRUE if the action and its associated action group
1140 gtk_action_is_visible (GtkAction *action)
1142 GtkActionPrivate *priv;
1143 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1145 priv = action->private_data;
1146 return priv->visible &&
1147 (priv->action_group == NULL ||
1148 gtk_action_group_get_visible (priv->action_group));
1152 * gtk_action_get_visible:
1153 * @action: the action object
1155 * Returns whether the action itself is visible. Note that this doesn't
1156 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1159 * Return value: %TRUE if the action itself is visible.
1164 gtk_action_get_visible (GtkAction *action)
1166 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1168 return action->private_data->visible;
1172 _gtk_action_sync_visible (GtkAction *action)
1179 visible = gtk_action_is_visible (action);
1181 for (p = action->private_data->proxies; p; p = p->next)
1183 proxy = (GtkWidget *)p->data;
1185 if (GTK_IS_MENU_ITEM (proxy))
1187 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1189 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1194 gtk_widget_show (proxy);
1196 gtk_widget_hide (proxy);
1202 * gtk_action_set_visible:
1203 * @action: the action object
1204 * @visible: %TRUE to make the action visible
1206 * Sets the ::visible property of the action to @visible. Note that
1207 * this doesn't necessarily mean effective visibility. See
1208 * gtk_action_is_visible()
1214 gtk_action_set_visible (GtkAction *action,
1217 g_return_if_fail (GTK_IS_ACTION (action));
1219 visible = visible != FALSE;
1221 if (action->private_data->visible != visible)
1223 action->private_data->visible = visible;
1225 _gtk_action_sync_visible (action);
1227 g_object_notify (G_OBJECT (action), "visible");
1232 gtk_action_set_is_important (GtkAction *action,
1233 gboolean is_important)
1238 is_important = is_important != FALSE;
1240 if (action->private_data->is_important != is_important)
1242 action->private_data->is_important = is_important;
1244 for (p = action->private_data->proxies; p; p = p->next)
1246 proxy = (GtkWidget *)p->data;
1248 if (GTK_IS_TOOL_ITEM (proxy))
1249 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1253 g_object_notify (G_OBJECT (action), "is-important");
1258 gtk_action_set_label (GtkAction *action,
1262 GtkWidget *proxy, *child;
1265 tmp = action->private_data->label;
1266 action->private_data->label = g_strdup (label);
1268 action->private_data->label_set = (action->private_data->label != NULL);
1269 /* if label is unset, then use the label from the stock item */
1270 if (!action->private_data->label_set && action->private_data->stock_id)
1272 GtkStockItem stock_item;
1274 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1275 action->private_data->label = g_strdup (stock_item.label);
1278 for (p = action->private_data->proxies; p; p = p->next)
1280 proxy = (GtkWidget *)p->data;
1282 if (GTK_IS_MENU_ITEM (proxy))
1284 child = GTK_BIN (proxy)->child;
1286 if (GTK_IS_LABEL (child))
1287 gtk_label_set_label (GTK_LABEL (child),
1288 action->private_data->label);
1292 g_object_notify (G_OBJECT (action), "label");
1294 /* if short_label is unset, set short_label=label */
1295 if (!action->private_data->short_label_set)
1297 gtk_action_set_short_label (action, action->private_data->label);
1298 action->private_data->short_label_set = FALSE;
1303 gtk_action_set_short_label (GtkAction *action,
1307 GtkWidget *proxy, *child;
1310 tmp = action->private_data->short_label;
1311 action->private_data->short_label = g_strdup (label);
1313 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1314 /* if short_label is unset, then use the value of label */
1315 if (!action->private_data->short_label_set)
1316 action->private_data->short_label = g_strdup (action->private_data->label);
1318 for (p = action->private_data->proxies; p; p = p->next)
1320 proxy = (GtkWidget *)p->data;
1322 if (GTK_IS_TOOL_BUTTON (proxy))
1323 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1324 action->private_data->label);
1325 else if (GTK_IS_BUTTON (proxy) &&
1326 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1328 child = GTK_BIN (proxy)->child;
1330 if (child == NULL || GTK_IS_LABEL (child))
1331 gtk_button_set_label (GTK_BUTTON (proxy),
1332 action->private_data->label);
1336 g_object_notify (G_OBJECT (action), "short-label");
1340 gtk_action_set_visible_horizontal (GtkAction *action,
1341 gboolean visible_horizontal)
1346 visible_horizontal = visible_horizontal != FALSE;
1348 if (action->private_data->visible_horizontal != visible_horizontal)
1350 action->private_data->visible_horizontal = visible_horizontal;
1352 for (p = action->private_data->proxies; p; p = p->next)
1354 proxy = (GtkWidget *)p->data;
1356 if (GTK_IS_TOOL_ITEM (proxy))
1357 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1358 visible_horizontal);
1361 g_object_notify (G_OBJECT (action), "visible-horizontal");
1366 gtk_action_set_visible_vertical (GtkAction *action,
1367 gboolean visible_vertical)
1372 visible_vertical = visible_vertical != FALSE;
1374 if (action->private_data->visible_vertical != visible_vertical)
1376 action->private_data->visible_vertical = visible_vertical;
1378 for (p = action->private_data->proxies; p; p = p->next)
1380 proxy = (GtkWidget *)p->data;
1382 if (GTK_IS_TOOL_ITEM (proxy))
1383 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1387 g_object_notify (G_OBJECT (action), "visible-vertical");
1392 gtk_action_sync_tooltip (GtkAction *action,
1397 parent = gtk_widget_get_parent (proxy);
1399 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1400 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1401 GTK_TOOLBAR (parent)->tooltips,
1402 action->private_data->tooltip,
1407 gtk_action_set_tooltip (GtkAction *action,
1408 const gchar *tooltip)
1414 tmp = action->private_data->tooltip;
1415 action->private_data->tooltip = g_strdup (tooltip);
1418 for (p = action->private_data->proxies; p; p = p->next)
1420 proxy = (GtkWidget *)p->data;
1422 gtk_action_sync_tooltip (action, proxy);
1425 g_object_notify (G_OBJECT (action), "tooltip");
1429 gtk_action_set_stock_id (GtkAction *action,
1430 const gchar *stock_id)
1433 GtkWidget *proxy, *image;
1436 tmp = action->private_data->stock_id;
1437 action->private_data->stock_id = g_strdup (stock_id);
1440 for (p = action->private_data->proxies; p; p = p->next)
1442 proxy = (GtkWidget *)p->data;
1444 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1446 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1448 if (GTK_IS_IMAGE (image))
1449 gtk_image_set_from_stock (GTK_IMAGE (image),
1450 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1452 else if (GTK_IS_TOOL_BUTTON (proxy))
1454 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1455 action->private_data->stock_id);
1457 else if (GTK_IS_BUTTON (proxy) &&
1458 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1460 gtk_button_set_label (GTK_BUTTON (proxy),
1461 action->private_data->stock_id);
1465 g_object_notify (G_OBJECT (action), "stock-id");
1467 /* update label and short_label if appropriate */
1468 if (!action->private_data->label_set)
1470 GtkStockItem stock_item;
1472 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1473 gtk_action_set_label (action, stock_item.label);
1475 gtk_action_set_label (action, NULL);
1477 action->private_data->label_set = FALSE;
1483 * gtk_action_block_activate_from:
1484 * @action: the action object
1485 * @proxy: a proxy widget
1487 * Disables calls to the gtk_action_activate()
1488 * function by signals on the given proxy widget. This is used to
1489 * break notification loops for things like check or radio actions.
1491 * This function is intended for use by action implementations.
1496 gtk_action_block_activate_from (GtkAction *action,
1499 g_return_if_fail (GTK_IS_ACTION (action));
1501 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1506 * gtk_action_unblock_activate_from:
1507 * @action: the action object
1508 * @proxy: a proxy widget
1510 * Re-enables calls to the gtk_action_activate()
1511 * function by signals on the given proxy widget. This undoes the
1512 * blocking done by gtk_action_block_activate_from().
1514 * This function is intended for use by action implementations.
1519 gtk_action_unblock_activate_from (GtkAction *action,
1522 g_return_if_fail (GTK_IS_ACTION (action));
1524 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1529 closure_accel_activate (GClosure *closure,
1530 GValue *return_value,
1531 guint n_param_values,
1532 const GValue *param_values,
1533 gpointer invocation_hint,
1534 gpointer marshal_data)
1536 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1538 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1540 /* we handled the accelerator */
1541 g_value_set_boolean (return_value, TRUE);
1546 gtk_action_set_action_group (GtkAction *action,
1547 GtkActionGroup *action_group)
1549 g_return_if_fail (GTK_IS_ACTION (action));
1551 if (action->private_data->action_group == NULL)
1552 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1554 g_return_if_fail (action_group == NULL);
1556 action->private_data->action_group = action_group;
1560 * gtk_action_set_accel_path:
1561 * @action: the action object
1562 * @accel_path: the accelerator path
1564 * Sets the accel path for this action. All proxy widgets associated
1565 * with the action will have this accel path, so that their
1566 * accelerators are consistent.
1571 gtk_action_set_accel_path (GtkAction *action,
1572 const gchar *accel_path)
1574 g_return_if_fail (GTK_IS_ACTION (action));
1576 action->private_data->accel_quark = g_quark_from_string (accel_path);
1580 * gtk_action_get_accel_path:
1581 * @action: the action object
1583 * Returns the accel path for this action.
1587 * Returns: the accel path for this action, or %NULL
1588 * if none is set. The returned string is owned by GTK+
1589 * and must not be freed or modified.
1591 G_CONST_RETURN gchar *
1592 gtk_action_get_accel_path (GtkAction *action)
1594 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1596 if (action->private_data->accel_quark)
1597 return g_quark_to_string (action->private_data->accel_quark);
1603 * gtk_action_get_accel_closure:
1604 * @action: the action object
1606 * Returns the accel closure for this action.
1610 * Returns: the accel closure for this action. The returned closure is
1611 * owned by GTK+ and must not be unreffed or modified.
1614 gtk_action_get_accel_closure (GtkAction *action)
1616 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1618 return action->private_data->accel_closure;
1623 * gtk_action_set_accel_group:
1624 * @action: the action object
1625 * @accel_group: a #GtkAccelGroup or %NULL
1627 * Sets the #GtkAccelGroup in which the accelerator for this action
1628 * will be installed.
1633 gtk_action_set_accel_group (GtkAction *action,
1634 GtkAccelGroup *accel_group)
1636 g_return_if_fail (GTK_IS_ACTION (action));
1637 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1640 g_object_ref (accel_group);
1641 if (action->private_data->accel_group)
1642 g_object_unref (action->private_data->accel_group);
1644 action->private_data->accel_group = accel_group;
1648 * gtk_action_connect_accelerator:
1649 * @action: a #GtkAction
1651 * Installs the accelerator for @action if @action has an
1652 * accel path and group. See gtk_action_set_accel_path() and
1653 * gtk_action_set_accel_group()
1655 * Since multiple proxies may independently trigger the installation
1656 * of the accelerator, the @action counts the number of times this
1657 * function has been called and doesn't remove the accelerator until
1658 * gtk_action_disconnect_accelerator() has been called as many times.
1663 gtk_action_connect_accelerator (GtkAction *action)
1665 g_return_if_fail (GTK_IS_ACTION (action));
1667 if (!action->private_data->accel_quark ||
1668 !action->private_data->accel_group)
1671 if (action->private_data->accel_count == 0)
1673 const gchar *accel_path =
1674 g_quark_to_string (action->private_data->accel_quark);
1676 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1678 action->private_data->accel_closure);
1681 action->private_data->accel_count++;
1685 * gtk_action_disconnect_accelerator:
1686 * @action: a #GtkAction
1688 * Undoes the effect of one call to gtk_action_connect_accelerator().
1693 gtk_action_disconnect_accelerator (GtkAction *action)
1695 g_return_if_fail (GTK_IS_ACTION (action));
1697 if (!action->private_data->accel_quark ||
1698 !action->private_data->accel_group)
1701 action->private_data->accel_count--;
1703 if (action->private_data->accel_count == 0)
1704 gtk_accel_group_disconnect (action->private_data->accel_group,
1705 action->private_data->accel_closure);
1708 #define __GTK_ACTION_C__
1709 #include "gtkaliasdef.c"