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.
984 * Return value: a #GSList of proxy widgets. The list is owned by the action and
985 * must not be modified.
990 gtk_action_get_proxies (GtkAction *action)
992 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
994 return action->private_data->proxies;
999 * gtk_action_get_name:
1000 * @action: the action object
1002 * Returns the name of the action.
1004 * Return value: the name of the action. The string belongs to GTK+ and should not
1009 G_CONST_RETURN gchar *
1010 gtk_action_get_name (GtkAction *action)
1012 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1014 return action->private_data->name;
1018 * gtk_action_is_sensitive:
1019 * @action: the action object
1021 * Returns whether the action is effectively sensitive.
1023 * Return value: %TRUE if the action and its associated action group
1024 * are both sensitive.
1029 gtk_action_is_sensitive (GtkAction *action)
1031 GtkActionPrivate *priv;
1032 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1034 priv = action->private_data;
1035 return priv->sensitive &&
1036 (priv->action_group == NULL ||
1037 gtk_action_group_get_sensitive (priv->action_group));
1041 * gtk_action_get_sensitive:
1042 * @action: the action object
1044 * Returns whether the action itself is sensitive. Note that this doesn't
1045 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1048 * Return value: %TRUE if the action itself is sensitive.
1053 gtk_action_get_sensitive (GtkAction *action)
1055 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1057 return action->private_data->sensitive;
1061 _gtk_action_sync_sensitive (GtkAction *action)
1067 sensitive = gtk_action_is_sensitive (action);
1069 for (p = action->private_data->proxies; p; p = p->next)
1071 proxy = (GtkWidget *)p->data;
1072 gtk_widget_set_sensitive (proxy, sensitive);
1077 * gtk_action_set_sensitive:
1078 * @action: the action object
1079 * @sensitive: %TRUE to make the action sensitive
1081 * Sets the ::sensitive property of the action to @sensitive. Note that
1082 * this doesn't necessarily mean effective sensitivity. See
1083 * gtk_action_is_sensitive()
1089 gtk_action_set_sensitive (GtkAction *action,
1092 g_return_if_fail (GTK_IS_ACTION (action));
1094 sensitive = sensitive != FALSE;
1096 if (action->private_data->sensitive != sensitive)
1098 action->private_data->sensitive = sensitive;
1100 _gtk_action_sync_sensitive (action);
1102 g_object_notify (G_OBJECT (action), "sensitive");
1107 * gtk_action_is_visible:
1108 * @action: the action object
1110 * Returns whether the action is effectively visible.
1112 * Return value: %TRUE if the action and its associated action group
1118 gtk_action_is_visible (GtkAction *action)
1120 GtkActionPrivate *priv;
1121 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1123 priv = action->private_data;
1124 return priv->visible &&
1125 (priv->action_group == NULL ||
1126 gtk_action_group_get_visible (priv->action_group));
1130 * gtk_action_get_visible:
1131 * @action: the action object
1133 * Returns whether the action itself is visible. Note that this doesn't
1134 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1137 * Return value: %TRUE if the action itself is visible.
1142 gtk_action_get_visible (GtkAction *action)
1144 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1146 return action->private_data->visible;
1150 _gtk_action_sync_visible (GtkAction *action)
1157 visible = gtk_action_is_visible (action);
1159 for (p = action->private_data->proxies; p; p = p->next)
1161 proxy = (GtkWidget *)p->data;
1163 if (GTK_IS_MENU_ITEM (proxy))
1165 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1167 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1172 gtk_widget_show (proxy);
1174 gtk_widget_hide (proxy);
1180 * gtk_action_set_visible:
1181 * @action: the action object
1182 * @visible: %TRUE to make the action visible
1184 * Sets the ::visible property of the action to @visible. Note that
1185 * this doesn't necessarily mean effective visibility. See
1186 * gtk_action_is_visible()
1192 gtk_action_set_visible (GtkAction *action,
1195 g_return_if_fail (GTK_IS_ACTION (action));
1197 visible = visible != FALSE;
1199 if (action->private_data->visible != visible)
1201 action->private_data->visible = visible;
1203 _gtk_action_sync_visible (action);
1205 g_object_notify (G_OBJECT (action), "visible");
1210 gtk_action_set_is_important (GtkAction *action,
1211 gboolean is_important)
1216 is_important = is_important != FALSE;
1218 if (action->private_data->is_important != is_important)
1220 action->private_data->is_important = is_important;
1222 for (p = action->private_data->proxies; p; p = p->next)
1224 proxy = (GtkWidget *)p->data;
1226 if (GTK_IS_TOOL_ITEM (proxy))
1227 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1231 g_object_notify (G_OBJECT (action), "is-important");
1236 gtk_action_set_label (GtkAction *action,
1240 GtkWidget *proxy, *child;
1243 tmp = action->private_data->label;
1244 action->private_data->label = g_strdup (label);
1246 action->private_data->label_set = (action->private_data->label != NULL);
1247 /* if label is unset, then use the label from the stock item */
1248 if (!action->private_data->label_set && action->private_data->stock_id)
1250 GtkStockItem stock_item;
1252 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1253 action->private_data->label = g_strdup (stock_item.label);
1256 for (p = action->private_data->proxies; p; p = p->next)
1258 proxy = (GtkWidget *)p->data;
1260 if (GTK_IS_MENU_ITEM (proxy))
1262 child = GTK_BIN (proxy)->child;
1264 if (GTK_IS_LABEL (child))
1265 gtk_label_set_label (GTK_LABEL (child),
1266 action->private_data->label);
1270 g_object_notify (G_OBJECT (action), "label");
1272 /* if short_label is unset, set short_label=label */
1273 if (!action->private_data->short_label_set)
1275 gtk_action_set_short_label (action, action->private_data->label);
1276 action->private_data->short_label_set = FALSE;
1281 gtk_action_set_short_label (GtkAction *action,
1285 GtkWidget *proxy, *child;
1288 tmp = action->private_data->short_label;
1289 action->private_data->short_label = g_strdup (label);
1291 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1292 /* if short_label is unset, then use the value of label */
1293 if (!action->private_data->short_label_set)
1294 action->private_data->short_label = g_strdup (action->private_data->label);
1296 for (p = action->private_data->proxies; p; p = p->next)
1298 proxy = (GtkWidget *)p->data;
1300 if (GTK_IS_TOOL_BUTTON (proxy))
1301 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1302 action->private_data->label);
1303 else if (GTK_IS_BUTTON (proxy) &&
1304 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1306 child = GTK_BIN (proxy)->child;
1308 if (child == NULL || GTK_IS_LABEL (child))
1309 gtk_button_set_label (GTK_BUTTON (proxy),
1310 action->private_data->label);
1314 g_object_notify (G_OBJECT (action), "short-label");
1318 gtk_action_set_visible_horizontal (GtkAction *action,
1319 gboolean visible_horizontal)
1324 visible_horizontal = visible_horizontal != FALSE;
1326 if (action->private_data->visible_horizontal != visible_horizontal)
1328 action->private_data->visible_horizontal = visible_horizontal;
1330 for (p = action->private_data->proxies; p; p = p->next)
1332 proxy = (GtkWidget *)p->data;
1334 if (GTK_IS_TOOL_ITEM (proxy))
1335 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1336 visible_horizontal);
1339 g_object_notify (G_OBJECT (action), "visible-horizontal");
1344 gtk_action_set_visible_vertical (GtkAction *action,
1345 gboolean visible_vertical)
1350 visible_vertical = visible_vertical != FALSE;
1352 if (action->private_data->visible_vertical != visible_vertical)
1354 action->private_data->visible_vertical = visible_vertical;
1356 for (p = action->private_data->proxies; p; p = p->next)
1358 proxy = (GtkWidget *)p->data;
1360 if (GTK_IS_TOOL_ITEM (proxy))
1361 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1365 g_object_notify (G_OBJECT (action), "visible-vertical");
1370 gtk_action_sync_tooltip (GtkAction *action,
1375 parent = gtk_widget_get_parent (proxy);
1377 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1378 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1379 GTK_TOOLBAR (parent)->tooltips,
1380 action->private_data->tooltip,
1385 gtk_action_set_tooltip (GtkAction *action,
1386 const gchar *tooltip)
1392 tmp = action->private_data->tooltip;
1393 action->private_data->tooltip = g_strdup (tooltip);
1396 for (p = action->private_data->proxies; p; p = p->next)
1398 proxy = (GtkWidget *)p->data;
1400 gtk_action_sync_tooltip (action, proxy);
1403 g_object_notify (G_OBJECT (action), "tooltip");
1407 gtk_action_set_stock_id (GtkAction *action,
1408 const gchar *stock_id)
1411 GtkWidget *proxy, *image;
1414 tmp = action->private_data->stock_id;
1415 action->private_data->stock_id = g_strdup (stock_id);
1418 for (p = action->private_data->proxies; p; p = p->next)
1420 proxy = (GtkWidget *)p->data;
1422 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1424 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1426 if (GTK_IS_IMAGE (image))
1427 gtk_image_set_from_stock (GTK_IMAGE (image),
1428 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1430 else if (GTK_IS_TOOL_BUTTON (proxy))
1432 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1433 action->private_data->stock_id);
1435 else if (GTK_IS_BUTTON (proxy) &&
1436 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1438 gtk_button_set_label (GTK_BUTTON (proxy),
1439 action->private_data->stock_id);
1443 g_object_notify (G_OBJECT (action), "stock-id");
1445 /* update label and short_label if appropriate */
1446 if (!action->private_data->label_set)
1448 GtkStockItem stock_item;
1450 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1451 gtk_action_set_label (action, stock_item.label);
1453 gtk_action_set_label (action, NULL);
1455 action->private_data->label_set = FALSE;
1461 * gtk_action_block_activate_from:
1462 * @action: the action object
1463 * @proxy: a proxy widget
1465 * Disables calls to the gtk_action_activate()
1466 * function by signals on the given proxy widget. This is used to
1467 * break notification loops for things like check or radio actions.
1469 * This function is intended for use by action implementations.
1474 gtk_action_block_activate_from (GtkAction *action,
1477 g_return_if_fail (GTK_IS_ACTION (action));
1479 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1484 * gtk_action_unblock_activate_from:
1485 * @action: the action object
1486 * @proxy: a proxy widget
1488 * Re-enables calls to the gtk_action_activate()
1489 * function by signals on the given proxy widget. This undoes the
1490 * blocking done by gtk_action_block_activate_from().
1492 * This function is intended for use by action implementations.
1497 gtk_action_unblock_activate_from (GtkAction *action,
1500 g_return_if_fail (GTK_IS_ACTION (action));
1502 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1507 closure_accel_activate (GClosure *closure,
1508 GValue *return_value,
1509 guint n_param_values,
1510 const GValue *param_values,
1511 gpointer invocation_hint,
1512 gpointer marshal_data)
1514 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1516 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1518 /* we handled the accelerator */
1519 g_value_set_boolean (return_value, TRUE);
1524 gtk_action_set_action_group (GtkAction *action,
1525 GtkActionGroup *action_group)
1527 g_return_if_fail (GTK_IS_ACTION (action));
1529 if (action->private_data->action_group == NULL)
1530 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1532 g_return_if_fail (action_group == NULL);
1534 action->private_data->action_group = action_group;
1538 * gtk_action_set_accel_path:
1539 * @action: the action object
1540 * @accel_path: the accelerator path
1542 * Sets the accel path for this action. All proxy widgets associated
1543 * with the action will have this accel path, so that their
1544 * accelerators are consistent.
1549 gtk_action_set_accel_path (GtkAction *action,
1550 const gchar *accel_path)
1552 g_return_if_fail (GTK_IS_ACTION (action));
1554 action->private_data->accel_quark = g_quark_from_string (accel_path);
1558 * gtk_action_get_accel_path:
1559 * @action: the action object
1561 * Returns the accel path for this action.
1565 * Returns: the accel path for this action, or %NULL
1566 * if none is set. The returned string is owned by GTK+
1567 * and must not be freed or modified.
1569 G_CONST_RETURN gchar *
1570 gtk_action_get_accel_path (GtkAction *action)
1572 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1574 if (action->private_data->accel_quark)
1575 return g_quark_to_string (action->private_data->accel_quark);
1581 * gtk_action_get_accel_closure:
1582 * @action: the action object
1584 * Returns the accel closure for this action.
1588 * Returns: the accel closure for this action. The returned closure is
1589 * owned by GTK+ and must not be unreffed or modified.
1592 gtk_action_get_accel_closure (GtkAction *action)
1594 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1596 return action->private_data->accel_closure;
1601 * gtk_action_set_accel_group:
1602 * @action: the action object
1603 * @accel_group: a #GtkAccelGroup or %NULL
1605 * Sets the #GtkAccelGroup in which the accelerator for this action
1606 * will be installed.
1611 gtk_action_set_accel_group (GtkAction *action,
1612 GtkAccelGroup *accel_group)
1614 g_return_if_fail (GTK_IS_ACTION (action));
1615 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1618 g_object_ref (accel_group);
1619 if (action->private_data->accel_group)
1620 g_object_unref (action->private_data->accel_group);
1622 action->private_data->accel_group = accel_group;
1626 * gtk_action_connect_accelerator:
1627 * @action: a #GtkAction
1629 * Installs the accelerator for @action if @action has an
1630 * accel path and group. See gtk_action_set_accel_path() and
1631 * gtk_action_set_accel_group()
1633 * Since multiple proxies may independently trigger the installation
1634 * of the accelerator, the @action counts the number of times this
1635 * function has been called and doesn't remove the accelerator until
1636 * gtk_action_disconnect_accelerator() has been called as many times.
1641 gtk_action_connect_accelerator (GtkAction *action)
1643 g_return_if_fail (GTK_IS_ACTION (action));
1645 if (!action->private_data->accel_quark ||
1646 !action->private_data->accel_group)
1649 if (action->private_data->accel_count == 0)
1651 const gchar *accel_path =
1652 g_quark_to_string (action->private_data->accel_quark);
1654 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1656 action->private_data->accel_closure);
1659 action->private_data->accel_count++;
1663 * gtk_action_disconnect_accelerator:
1664 * @action: a #GtkAction
1666 * Undoes the effect of one call to gtk_action_connect_accelerator().
1671 gtk_action_disconnect_accelerator (GtkAction *action)
1673 g_return_if_fail (GTK_IS_ACTION (action));
1675 if (!action->private_data->accel_quark ||
1676 !action->private_data->accel_group)
1679 action->private_data->accel_count--;
1681 if (action->private_data->accel_count == 0)
1682 gtk_accel_group_disconnect (action->private_data->accel_group,
1683 action->private_data->accel_closure);
1686 #define __GTK_ACTION_C__
1687 #include "gtkaliasdef.c"