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 * @Short_description: An action which can be triggered by a menu or toolbar item
35 * @See_also: #GtkActionGroup, #GtkUIManager, #GtkActivatable
37 * Actions represent operations that the user can be perform, along with
38 * some information how it should be presented in the interface. Each action
39 * provides methods to create icons, menu items and toolbar items
40 * representing itself.
42 * As well as the callback that is called when the action gets activated,
43 * the following also gets associated with the action:
45 * <listitem><para>a name (not translated, for path lookup)</para></listitem>
46 * <listitem><para>a label (translated, for display)</para></listitem>
47 * <listitem><para>an accelerator</para></listitem>
48 * <listitem><para>whether label indicates a stock id</para></listitem>
49 * <listitem><para>a tooltip (optional, translated)</para></listitem>
50 * <listitem><para>a toolbar label (optional, shorter than label)</para></listitem>
52 * The action will also have some state information:
54 * <listitem><para>visible (shown/hidden)</para></listitem>
55 * <listitem><para>sensitive (enabled/disabled)</para></listitem>
57 * Apart from regular actions, there are <link linkend="GtkToggleAction">toggle
58 * actions</link>, which can be toggled between two states and <link
59 * linkend="GtkRadioAction">radio actions</link>, of which only one in a group
60 * can be in the "active" state. Other actions can be implemented as #GtkAction
63 * Each action can have one or more proxy widgets. To act as an action proxy,
64 * widget needs to implement #GtkActivatable interface. Proxies mirror the state
65 * of the action and should change when the action's state changes. Properties
66 * that are always mirrored by proxies are #GtkAction:sensitive and
67 * #GtkAction:visible. #GtkAction:gicon, #GtkAction:icon-name, #GtkAction:label,
68 * #GtkAction:short-label and #GtkAction:stock-id properties are only mirorred
69 * if proxy widget has #GtkActivatable:use-action-appearance property set to
72 * When the proxy is activated, it should activate its action.
77 #include "gtkaction.h"
78 #include "gtkactiongroup.h"
79 #include "gtkaccellabel.h"
80 #include "gtkbutton.h"
81 #include "gtkiconfactory.h"
83 #include "gtkimagemenuitem.h"
86 #include "gtkmarshalers.h"
87 #include "gtkmenuitem.h"
89 #include "gtktearoffmenuitem.h"
90 #include "gtktoolbutton.h"
91 #include "gtktoolbar.h"
92 #include "gtkprivate.h"
93 #include "gtkbuildable.h"
94 #include "gtkactivatable.h"
97 struct _GtkActionPrivate
99 const gchar *name; /* interned */
103 gchar *stock_id; /* stock icon */
104 gchar *icon_name; /* themed icon */
109 guint label_set : 1; /* these two used so we can set label */
110 guint short_label_set : 1; /* based on stock id */
111 guint visible_horizontal : 1;
112 guint visible_vertical : 1;
113 guint is_important : 1;
114 guint hide_if_empty : 1;
115 guint visible_overflown : 1;
116 guint always_show_image : 1;
117 guint recursion_guard : 1;
118 guint activate_blocked : 1;
122 GtkAccelGroup *accel_group;
123 GClosure *accel_closure;
126 GtkActionGroup *action_group;
128 /* list of proxy widgets */
148 PROP_VISIBLE_HORIZONTAL,
149 PROP_VISIBLE_VERTICAL,
150 PROP_VISIBLE_OVERFLOWN,
156 PROP_ALWAYS_SHOW_IMAGE
160 static void gtk_action_buildable_init (GtkBuildableIface *iface);
161 static void gtk_action_buildable_set_name (GtkBuildable *buildable,
163 static const gchar* gtk_action_buildable_get_name (GtkBuildable *buildable);
165 G_DEFINE_TYPE_WITH_CODE (GtkAction, gtk_action, G_TYPE_OBJECT,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
167 gtk_action_buildable_init))
169 static void gtk_action_finalize (GObject *object);
170 static void gtk_action_set_property (GObject *object,
174 static void gtk_action_get_property (GObject *object,
178 static void gtk_action_set_action_group (GtkAction *action,
179 GtkActionGroup *action_group);
181 static GtkWidget *create_menu_item (GtkAction *action);
182 static GtkWidget *create_tool_item (GtkAction *action);
183 static void connect_proxy (GtkAction *action,
185 static void disconnect_proxy (GtkAction *action,
188 static void closure_accel_activate (GClosure *closure,
189 GValue *return_value,
190 guint n_param_values,
191 const GValue *param_values,
192 gpointer invocation_hint,
193 gpointer marshal_data);
195 static guint action_signals[LAST_SIGNAL] = { 0 };
199 gtk_action_class_init (GtkActionClass *klass)
201 GObjectClass *gobject_class;
203 gobject_class = G_OBJECT_CLASS (klass);
205 gobject_class->finalize = gtk_action_finalize;
206 gobject_class->set_property = gtk_action_set_property;
207 gobject_class->get_property = gtk_action_get_property;
209 klass->activate = NULL;
211 klass->create_menu_item = create_menu_item;
212 klass->create_tool_item = create_tool_item;
213 klass->create_menu = NULL;
214 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
215 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
216 klass->connect_proxy = connect_proxy;
217 klass->disconnect_proxy = disconnect_proxy;
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("name",
223 P_("A unique name for the action."),
225 GTK_PARAM_READWRITE |
226 G_PARAM_CONSTRUCT_ONLY));
231 * The label used for menu items and buttons that activate
232 * this action. If the label is %NULL, GTK+ uses the stock
233 * label specified via the stock-id property.
235 * This is an appearance property and thus only applies if
236 * #GtkActivatable:use-action-appearance is %TRUE.
238 g_object_class_install_property (gobject_class,
240 g_param_spec_string ("label",
242 P_("The label used for menu items and buttons "
243 "that activate this action."),
245 GTK_PARAM_READWRITE));
248 * GtkAction:short-label:
250 * A shorter label that may be used on toolbar buttons.
252 * This is an appearance property and thus only applies if
253 * #GtkActivatable:use-action-appearance is %TRUE.
255 g_object_class_install_property (gobject_class,
257 g_param_spec_string ("short-label",
259 P_("A shorter label that may be used on toolbar buttons."),
261 GTK_PARAM_READWRITE));
264 g_object_class_install_property (gobject_class,
266 g_param_spec_string ("tooltip",
268 P_("A tooltip for this action."),
270 GTK_PARAM_READWRITE));
273 * GtkAction:stock-id:
275 * The stock icon displayed in widgets representing this action.
277 * This is an appearance property and thus only applies if
278 * #GtkActivatable:use-action-appearance is %TRUE.
280 g_object_class_install_property (gobject_class,
282 g_param_spec_string ("stock-id",
284 P_("The stock icon displayed in widgets representing "
287 GTK_PARAM_READWRITE));
291 * The #GIcon displayed in the #GtkAction.
293 * Note that the stock icon is preferred, if the #GtkAction:stock-id
294 * property holds the id of an existing stock icon.
296 * This is an appearance property and thus only applies if
297 * #GtkActivatable:use-action-appearance is %TRUE.
301 g_object_class_install_property (gobject_class,
303 g_param_spec_object ("gicon",
305 P_("The GIcon being displayed"),
307 GTK_PARAM_READWRITE));
309 * GtkAction:icon-name:
311 * The name of the icon from the icon theme.
313 * Note that the stock icon is preferred, if the #GtkAction:stock-id
314 * property holds the id of an existing stock icon, and the #GIcon is
315 * preferred if the #GtkAction:gicon property is set.
317 * This is an appearance property and thus only applies if
318 * #GtkActivatable:use-action-appearance is %TRUE.
322 g_object_class_install_property (gobject_class,
324 g_param_spec_string ("icon-name",
326 P_("The name of the icon from the icon theme"),
328 GTK_PARAM_READWRITE));
330 g_object_class_install_property (gobject_class,
331 PROP_VISIBLE_HORIZONTAL,
332 g_param_spec_boolean ("visible-horizontal",
333 P_("Visible when horizontal"),
334 P_("Whether the toolbar item is visible when the toolbar "
335 "is in a horizontal orientation."),
337 GTK_PARAM_READWRITE));
339 * GtkAction:visible-overflown:
341 * When %TRUE, toolitem proxies for this action are represented in the
342 * toolbar overflow menu.
346 g_object_class_install_property (gobject_class,
347 PROP_VISIBLE_OVERFLOWN,
348 g_param_spec_boolean ("visible-overflown",
349 P_("Visible when overflown"),
350 P_("When TRUE, toolitem proxies for this action "
351 "are represented in the toolbar overflow menu."),
353 GTK_PARAM_READWRITE));
354 g_object_class_install_property (gobject_class,
355 PROP_VISIBLE_VERTICAL,
356 g_param_spec_boolean ("visible-vertical",
357 P_("Visible when vertical"),
358 P_("Whether the toolbar item is visible when the toolbar "
359 "is in a vertical orientation."),
361 GTK_PARAM_READWRITE));
362 g_object_class_install_property (gobject_class,
364 g_param_spec_boolean ("is-important",
366 P_("Whether the action is considered important. "
367 "When TRUE, toolitem proxies for this action "
368 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
370 GTK_PARAM_READWRITE));
371 g_object_class_install_property (gobject_class,
373 g_param_spec_boolean ("hide-if-empty",
375 P_("When TRUE, empty menu proxies for this action are hidden."),
377 GTK_PARAM_READWRITE));
378 g_object_class_install_property (gobject_class,
380 g_param_spec_boolean ("sensitive",
382 P_("Whether the action is enabled."),
384 GTK_PARAM_READWRITE));
385 g_object_class_install_property (gobject_class,
387 g_param_spec_boolean ("visible",
389 P_("Whether the action is visible."),
391 GTK_PARAM_READWRITE));
392 g_object_class_install_property (gobject_class,
394 g_param_spec_object ("action-group",
396 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
397 GTK_TYPE_ACTION_GROUP,
398 GTK_PARAM_READWRITE));
401 * GtkAction:always-show-image:
403 * If %TRUE, the action's menu item proxies will ignore the #GtkSettings:gtk-menu-images
404 * setting and always show their image, if available.
406 * Use this property if the menu item would be useless or hard to use
407 * without their image.
411 g_object_class_install_property (gobject_class,
412 PROP_ALWAYS_SHOW_IMAGE,
413 g_param_spec_boolean ("always-show-image",
414 P_("Always show image"),
415 P_("Whether the image will always be shown"),
417 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
420 * GtkAction::activate:
421 * @action: the #GtkAction
423 * The "activate" signal is emitted when the action is activated.
427 action_signals[ACTIVATE] =
428 g_signal_new (I_("activate"),
429 G_OBJECT_CLASS_TYPE (klass),
430 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
431 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
432 g_cclosure_marshal_VOID__VOID,
435 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
440 gtk_action_init (GtkAction *action)
442 action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
446 action->private_data->name = NULL;
447 action->private_data->label = NULL;
448 action->private_data->short_label = NULL;
449 action->private_data->tooltip = NULL;
450 action->private_data->stock_id = NULL;
451 action->private_data->icon_name = NULL;
452 action->private_data->visible_horizontal = TRUE;
453 action->private_data->visible_vertical = TRUE;
454 action->private_data->visible_overflown = TRUE;
455 action->private_data->is_important = FALSE;
456 action->private_data->hide_if_empty = TRUE;
457 action->private_data->always_show_image = FALSE;
458 action->private_data->activate_blocked = FALSE;
460 action->private_data->sensitive = TRUE;
461 action->private_data->visible = TRUE;
463 action->private_data->label_set = FALSE;
464 action->private_data->short_label_set = FALSE;
466 action->private_data->accel_count = 0;
467 action->private_data->accel_group = NULL;
468 action->private_data->accel_quark = 0;
469 action->private_data->accel_closure =
470 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
471 g_closure_set_marshal (action->private_data->accel_closure,
472 closure_accel_activate);
473 g_closure_ref (action->private_data->accel_closure);
474 g_closure_sink (action->private_data->accel_closure);
476 action->private_data->action_group = NULL;
478 action->private_data->proxies = NULL;
479 action->private_data->gicon = NULL;
483 gtk_action_buildable_init (GtkBuildableIface *iface)
485 iface->set_name = gtk_action_buildable_set_name;
486 iface->get_name = gtk_action_buildable_get_name;
490 gtk_action_buildable_set_name (GtkBuildable *buildable,
493 GtkAction *action = GTK_ACTION (buildable);
495 action->private_data->name = g_intern_string (name);
499 gtk_action_buildable_get_name (GtkBuildable *buildable)
501 GtkAction *action = GTK_ACTION (buildable);
503 return action->private_data->name;
508 * @name: A unique name for the action
509 * @label: (allow-none): the label displayed in menu items and on buttons, or %NULL
510 * @tooltip: (allow-none): a tooltip for the action, or %NULL
511 * @stock_id: the stock icon to display in widgets representing the
514 * Creates a new #GtkAction object. To add the action to a
515 * #GtkActionGroup and set the accelerator for the action,
516 * call gtk_action_group_add_action_with_accel().
517 * See <xref linkend="XML-UI"/> for information on allowed action
520 * Return value: a new #GtkAction
525 gtk_action_new (const gchar *name,
527 const gchar *tooltip,
528 const gchar *stock_id)
530 g_return_val_if_fail (name != NULL, NULL);
532 return g_object_new (GTK_TYPE_ACTION,
536 "stock-id", stock_id,
541 gtk_action_finalize (GObject *object)
544 action = GTK_ACTION (object);
546 g_free (action->private_data->label);
547 g_free (action->private_data->short_label);
548 g_free (action->private_data->tooltip);
549 g_free (action->private_data->stock_id);
550 g_free (action->private_data->icon_name);
552 if (action->private_data->gicon)
553 g_object_unref (action->private_data->gicon);
555 g_closure_unref (action->private_data->accel_closure);
556 if (action->private_data->accel_group)
557 g_object_unref (action->private_data->accel_group);
559 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
563 gtk_action_set_property (GObject *object,
570 action = GTK_ACTION (object);
575 action->private_data->name = g_intern_string (g_value_get_string (value));
578 gtk_action_set_label (action, g_value_get_string (value));
580 case PROP_SHORT_LABEL:
581 gtk_action_set_short_label (action, g_value_get_string (value));
584 gtk_action_set_tooltip (action, g_value_get_string (value));
587 gtk_action_set_stock_id (action, g_value_get_string (value));
590 gtk_action_set_gicon (action, g_value_get_object (value));
593 gtk_action_set_icon_name (action, g_value_get_string (value));
595 case PROP_VISIBLE_HORIZONTAL:
596 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
598 case PROP_VISIBLE_VERTICAL:
599 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
601 case PROP_VISIBLE_OVERFLOWN:
602 action->private_data->visible_overflown = g_value_get_boolean (value);
604 case PROP_IS_IMPORTANT:
605 gtk_action_set_is_important (action, g_value_get_boolean (value));
607 case PROP_HIDE_IF_EMPTY:
608 action->private_data->hide_if_empty = g_value_get_boolean (value);
611 gtk_action_set_sensitive (action, g_value_get_boolean (value));
614 gtk_action_set_visible (action, g_value_get_boolean (value));
616 case PROP_ACTION_GROUP:
617 gtk_action_set_action_group (action, g_value_get_object (value));
619 case PROP_ALWAYS_SHOW_IMAGE:
620 gtk_action_set_always_show_image (action, g_value_get_boolean (value));
623 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
629 gtk_action_get_property (GObject *object,
636 action = GTK_ACTION (object);
641 g_value_set_static_string (value, action->private_data->name);
644 g_value_set_string (value, action->private_data->label);
646 case PROP_SHORT_LABEL:
647 g_value_set_string (value, action->private_data->short_label);
650 g_value_set_string (value, action->private_data->tooltip);
653 g_value_set_string (value, action->private_data->stock_id);
656 g_value_set_string (value, action->private_data->icon_name);
659 g_value_set_object (value, action->private_data->gicon);
661 case PROP_VISIBLE_HORIZONTAL:
662 g_value_set_boolean (value, action->private_data->visible_horizontal);
664 case PROP_VISIBLE_VERTICAL:
665 g_value_set_boolean (value, action->private_data->visible_vertical);
667 case PROP_VISIBLE_OVERFLOWN:
668 g_value_set_boolean (value, action->private_data->visible_overflown);
670 case PROP_IS_IMPORTANT:
671 g_value_set_boolean (value, action->private_data->is_important);
673 case PROP_HIDE_IF_EMPTY:
674 g_value_set_boolean (value, action->private_data->hide_if_empty);
677 g_value_set_boolean (value, action->private_data->sensitive);
680 g_value_set_boolean (value, action->private_data->visible);
682 case PROP_ACTION_GROUP:
683 g_value_set_object (value, action->private_data->action_group);
685 case PROP_ALWAYS_SHOW_IMAGE:
686 g_value_set_boolean (value, action->private_data->always_show_image);
689 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
695 create_menu_item (GtkAction *action)
697 GType menu_item_type;
699 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
701 return g_object_new (menu_item_type, NULL);
705 create_tool_item (GtkAction *action)
707 GType toolbar_item_type;
709 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
711 return g_object_new (toolbar_item_type, NULL);
715 remove_proxy (GtkAction *action,
718 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
722 connect_proxy (GtkAction *action,
725 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
727 if (action->private_data->action_group)
728 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
733 disconnect_proxy (GtkAction *action,
736 remove_proxy (action, proxy);
738 if (action->private_data->action_group)
739 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
743 * _gtk_action_sync_menu_visible:
744 * @action: (allow-none): a #GtkAction, or %NULL to determine the action from @proxy
745 * @proxy: a proxy menu item
746 * @empty: whether the submenu attached to @proxy is empty
748 * Updates the visibility of @proxy from the visibility of @action
749 * according to the following rules:
751 * <listitem><para>if @action is invisible, @proxy is too
753 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
754 * property of @action indicates otherwise
758 * This function is used in the implementation of #GtkUIManager.
761 _gtk_action_sync_menu_visible (GtkAction *action,
765 gboolean visible = TRUE;
766 gboolean hide_if_empty = TRUE;
768 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
769 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
772 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (proxy));
776 /* a GtkMenu for a <popup/> doesn't have to have an action */
777 visible = gtk_action_is_visible (action);
778 hide_if_empty = action->private_data->hide_if_empty;
781 if (visible && !(empty && hide_if_empty))
782 gtk_widget_show (proxy);
784 gtk_widget_hide (proxy);
788 _gtk_action_emit_activate (GtkAction *action)
790 GtkActionGroup *group = action->private_data->action_group;
794 g_object_ref (group);
795 _gtk_action_group_emit_pre_activate (group, action);
798 g_signal_emit (action, action_signals[ACTIVATE], 0);
802 _gtk_action_group_emit_post_activate (group, action);
803 g_object_unref (group);
808 * gtk_action_activate:
809 * @action: the action object
811 * Emits the "activate" signal on the specified action, if it isn't
812 * insensitive. This gets called by the proxy widgets when they get
815 * It can also be used to manually activate an action.
820 gtk_action_activate (GtkAction *action)
822 g_return_if_fail (GTK_IS_ACTION (action));
824 if (action->private_data->activate_blocked)
827 if (gtk_action_is_sensitive (action))
828 _gtk_action_emit_activate (action);
832 * gtk_action_block_activate:
833 * @action: a #GtkAction
835 * Disable activation signals from the action
837 * This is needed when updating the state of your proxy
838 * #GtkActivatable widget could result in calling gtk_action_activate(),
839 * this is a convenience function to avoid recursing in those
840 * cases (updating toggle state for instance).
845 gtk_action_block_activate (GtkAction *action)
847 g_return_if_fail (GTK_IS_ACTION (action));
849 action->private_data->activate_blocked = TRUE;
853 * gtk_action_unblock_activate:
854 * @action: a #GtkAction
856 * Reenable activation signals from the action
861 gtk_action_unblock_activate (GtkAction *action)
863 g_return_if_fail (GTK_IS_ACTION (action));
865 action->private_data->activate_blocked = FALSE;
869 * gtk_action_create_icon:
870 * @action: the action object
871 * @icon_size: (type int): the size of the icon that should be created.
873 * This function is intended for use by action implementations to
874 * create icons displayed in the proxy widgets.
876 * Returns: (transfer full): a widget that displays the icon for this action.
881 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
883 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
885 if (action->private_data->stock_id &&
886 gtk_icon_factory_lookup_default (action->private_data->stock_id))
887 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
888 else if (action->private_data->gicon)
889 return gtk_image_new_from_gicon (action->private_data->gicon, icon_size);
890 else if (action->private_data->icon_name)
891 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
897 * gtk_action_create_menu_item:
898 * @action: the action object
900 * Creates a menu item widget that proxies for the given action.
902 * Returns: (transfer full): a menu item connected to the action.
907 gtk_action_create_menu_item (GtkAction *action)
909 GtkWidget *menu_item;
911 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
913 menu_item = GTK_ACTION_GET_CLASS (action)->create_menu_item (action);
915 gtk_activatable_set_use_action_appearance (GTK_ACTIVATABLE (menu_item), TRUE);
916 gtk_activatable_set_related_action (GTK_ACTIVATABLE (menu_item), action);
922 * gtk_action_create_tool_item:
923 * @action: the action object
925 * Creates a toolbar item widget that proxies for the given action.
927 * Returns: (transfer full): a toolbar item connected to the action.
932 gtk_action_create_tool_item (GtkAction *action)
936 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
938 button = GTK_ACTION_GET_CLASS (action)->create_tool_item (action);
940 gtk_activatable_set_use_action_appearance (GTK_ACTIVATABLE (button), TRUE);
941 gtk_activatable_set_related_action (GTK_ACTIVATABLE (button), action);
947 _gtk_action_add_to_proxy_list (GtkAction *action,
950 g_return_if_fail (GTK_IS_ACTION (action));
951 g_return_if_fail (GTK_IS_WIDGET (proxy));
953 GTK_ACTION_GET_CLASS (action)->connect_proxy (action, proxy);
957 _gtk_action_remove_from_proxy_list (GtkAction *action,
960 g_return_if_fail (GTK_IS_ACTION (action));
961 g_return_if_fail (GTK_IS_WIDGET (proxy));
963 GTK_ACTION_GET_CLASS (action)->disconnect_proxy (action, proxy);
967 * gtk_action_get_proxies:
968 * @action: the action object
970 * Returns the proxy widgets for an action.
971 * See also gtk_activatable_get_related_action().
973 * Return value: (element-type GtkWidget) (transfer none): a #GSList of proxy widgets. The list is owned by GTK+
974 * and must not be modified.
979 gtk_action_get_proxies (GtkAction *action)
981 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
983 return action->private_data->proxies;
987 * gtk_action_get_name:
988 * @action: the action object
990 * Returns the name of the action.
992 * Return value: the name of the action. The string belongs to GTK+ and should not
997 G_CONST_RETURN gchar *
998 gtk_action_get_name (GtkAction *action)
1000 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1002 return action->private_data->name;
1006 * gtk_action_is_sensitive:
1007 * @action: the action object
1009 * Returns whether the action is effectively sensitive.
1011 * Return value: %TRUE if the action and its associated action group
1012 * are both sensitive.
1017 gtk_action_is_sensitive (GtkAction *action)
1019 GtkActionPrivate *priv;
1020 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1022 priv = action->private_data;
1023 return priv->sensitive &&
1024 (priv->action_group == NULL ||
1025 gtk_action_group_get_sensitive (priv->action_group));
1029 * gtk_action_get_sensitive:
1030 * @action: the action object
1032 * Returns whether the action itself is sensitive. Note that this doesn't
1033 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1036 * Return value: %TRUE if the action itself is sensitive.
1041 gtk_action_get_sensitive (GtkAction *action)
1043 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1045 return action->private_data->sensitive;
1049 * gtk_action_set_sensitive:
1050 * @action: the action object
1051 * @sensitive: %TRUE to make the action sensitive
1053 * Sets the ::sensitive property of the action to @sensitive. Note that
1054 * this doesn't necessarily mean effective sensitivity. See
1055 * gtk_action_is_sensitive()
1061 gtk_action_set_sensitive (GtkAction *action,
1064 g_return_if_fail (GTK_IS_ACTION (action));
1066 sensitive = sensitive != FALSE;
1068 if (action->private_data->sensitive != sensitive)
1070 action->private_data->sensitive = sensitive;
1072 g_object_notify (G_OBJECT (action), "sensitive");
1077 * gtk_action_is_visible:
1078 * @action: the action object
1080 * Returns whether the action is effectively visible.
1082 * Return value: %TRUE if the action and its associated action group
1088 gtk_action_is_visible (GtkAction *action)
1090 GtkActionPrivate *priv;
1091 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1093 priv = action->private_data;
1094 return priv->visible &&
1095 (priv->action_group == NULL ||
1096 gtk_action_group_get_visible (priv->action_group));
1100 * gtk_action_get_visible:
1101 * @action: the action object
1103 * Returns whether the action itself is visible. Note that this doesn't
1104 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1107 * Return value: %TRUE if the action itself is visible.
1112 gtk_action_get_visible (GtkAction *action)
1114 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1116 return action->private_data->visible;
1120 * gtk_action_set_visible:
1121 * @action: the action object
1122 * @visible: %TRUE to make the action visible
1124 * Sets the ::visible property of the action to @visible. Note that
1125 * this doesn't necessarily mean effective visibility. See
1126 * gtk_action_is_visible()
1132 gtk_action_set_visible (GtkAction *action,
1135 g_return_if_fail (GTK_IS_ACTION (action));
1137 visible = visible != FALSE;
1139 if (action->private_data->visible != visible)
1141 action->private_data->visible = visible;
1143 g_object_notify (G_OBJECT (action), "visible");
1147 * gtk_action_set_is_important:
1148 * @action: the action object
1149 * @is_important: %TRUE to make the action important
1151 * Sets whether the action is important, this attribute is used
1152 * primarily by toolbar items to decide whether to show a label
1158 gtk_action_set_is_important (GtkAction *action,
1159 gboolean is_important)
1161 g_return_if_fail (GTK_IS_ACTION (action));
1163 is_important = is_important != FALSE;
1165 if (action->private_data->is_important != is_important)
1167 action->private_data->is_important = is_important;
1169 g_object_notify (G_OBJECT (action), "is-important");
1174 * gtk_action_get_is_important:
1175 * @action: a #GtkAction
1177 * Checks whether @action is important or not
1179 * Returns: whether @action is important
1184 gtk_action_get_is_important (GtkAction *action)
1186 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1188 return action->private_data->is_important;
1192 * gtk_action_set_always_show_image:
1193 * @action: a #GtkAction
1194 * @always_show: %TRUE if menuitem proxies should always show their image
1196 * Sets whether @action<!-- -->'s menu item proxies will ignore the
1197 * #GtkSettings:gtk-menu-images setting and always show their image, if available.
1199 * Use this if the menu item would be useless or hard to use
1200 * without their image.
1205 gtk_action_set_always_show_image (GtkAction *action,
1206 gboolean always_show)
1208 GtkActionPrivate *priv;
1210 g_return_if_fail (GTK_IS_ACTION (action));
1212 priv = action->private_data;
1214 always_show = always_show != FALSE;
1216 if (priv->always_show_image != always_show)
1218 priv->always_show_image = always_show;
1220 g_object_notify (G_OBJECT (action), "always-show-image");
1225 * gtk_action_get_always_show_image:
1226 * @action: a #GtkAction
1228 * Returns whether @action<!-- -->'s menu item proxies will ignore the
1229 * #GtkSettings:gtk-menu-images setting and always show their image,
1232 * Returns: %TRUE if the menu item proxies will always show their image
1237 gtk_action_get_always_show_image (GtkAction *action)
1239 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1241 return action->private_data->always_show_image;
1245 * gtk_action_set_label:
1246 * @action: a #GtkAction
1247 * @label: the label text to set
1249 * Sets the label of @action.
1254 gtk_action_set_label (GtkAction *action,
1259 g_return_if_fail (GTK_IS_ACTION (action));
1261 tmp = action->private_data->label;
1262 action->private_data->label = g_strdup (label);
1264 action->private_data->label_set = (action->private_data->label != NULL);
1265 /* if label is unset, then use the label from the stock item */
1266 if (!action->private_data->label_set && action->private_data->stock_id)
1268 GtkStockItem stock_item;
1270 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1271 action->private_data->label = g_strdup (stock_item.label);
1274 g_object_notify (G_OBJECT (action), "label");
1276 /* if short_label is unset, set short_label=label */
1277 if (!action->private_data->short_label_set)
1279 gtk_action_set_short_label (action, action->private_data->label);
1280 action->private_data->short_label_set = FALSE;
1285 * gtk_action_get_label:
1286 * @action: a #GtkAction
1288 * Gets the label text of @action.
1290 * Returns: the label text
1294 G_CONST_RETURN gchar *
1295 gtk_action_get_label (GtkAction *action)
1297 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1299 return action->private_data->label;
1303 * gtk_action_set_short_label:
1304 * @action: a #GtkAction
1305 * @short_label: the label text to set
1307 * Sets a shorter label text on @action.
1312 gtk_action_set_short_label (GtkAction *action,
1313 const gchar *short_label)
1317 g_return_if_fail (GTK_IS_ACTION (action));
1319 tmp = action->private_data->short_label;
1320 action->private_data->short_label = g_strdup (short_label);
1322 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1323 /* if short_label is unset, then use the value of label */
1324 if (!action->private_data->short_label_set)
1325 action->private_data->short_label = g_strdup (action->private_data->label);
1327 g_object_notify (G_OBJECT (action), "short-label");
1331 * gtk_action_get_short_label:
1332 * @action: a #GtkAction
1334 * Gets the short label text of @action.
1336 * Returns: the short label text.
1340 G_CONST_RETURN gchar *
1341 gtk_action_get_short_label (GtkAction *action)
1343 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1345 return action->private_data->short_label;
1349 * gtk_action_set_visible_horizontal:
1350 * @action: a #GtkAction
1351 * @visible_horizontal: whether the action is visible horizontally
1353 * Sets whether @action is visible when horizontal
1358 gtk_action_set_visible_horizontal (GtkAction *action,
1359 gboolean visible_horizontal)
1361 g_return_if_fail (GTK_IS_ACTION (action));
1363 g_return_if_fail (GTK_IS_ACTION (action));
1365 visible_horizontal = visible_horizontal != FALSE;
1367 if (action->private_data->visible_horizontal != visible_horizontal)
1369 action->private_data->visible_horizontal = visible_horizontal;
1371 g_object_notify (G_OBJECT (action), "visible-horizontal");
1376 * gtk_action_get_visible_horizontal:
1377 * @action: a #GtkAction
1379 * Checks whether @action is visible when horizontal
1381 * Returns: whether @action is visible when horizontal
1386 gtk_action_get_visible_horizontal (GtkAction *action)
1388 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1390 return action->private_data->visible_horizontal;
1394 * gtk_action_set_visible_vertical:
1395 * @action: a #GtkAction
1396 * @visible_vertical: whether the action is visible vertically
1398 * Sets whether @action is visible when vertical
1403 gtk_action_set_visible_vertical (GtkAction *action,
1404 gboolean visible_vertical)
1406 g_return_if_fail (GTK_IS_ACTION (action));
1408 g_return_if_fail (GTK_IS_ACTION (action));
1410 visible_vertical = visible_vertical != FALSE;
1412 if (action->private_data->visible_vertical != visible_vertical)
1414 action->private_data->visible_vertical = visible_vertical;
1416 g_object_notify (G_OBJECT (action), "visible-vertical");
1421 * gtk_action_get_visible_vertical:
1422 * @action: a #GtkAction
1424 * Checks whether @action is visible when horizontal
1426 * Returns: whether @action is visible when horizontal
1431 gtk_action_get_visible_vertical (GtkAction *action)
1433 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1435 return action->private_data->visible_vertical;
1439 * gtk_action_set_tooltip:
1440 * @action: a #GtkAction
1441 * @tooltip: the tooltip text
1443 * Sets the tooltip text on @action
1448 gtk_action_set_tooltip (GtkAction *action,
1449 const gchar *tooltip)
1453 g_return_if_fail (GTK_IS_ACTION (action));
1455 tmp = action->private_data->tooltip;
1456 action->private_data->tooltip = g_strdup (tooltip);
1459 g_object_notify (G_OBJECT (action), "tooltip");
1463 * gtk_action_get_tooltip:
1464 * @action: a #GtkAction
1466 * Gets the tooltip text of @action.
1468 * Returns: the tooltip text
1472 G_CONST_RETURN gchar *
1473 gtk_action_get_tooltip (GtkAction *action)
1475 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1477 return action->private_data->tooltip;
1481 * gtk_action_set_stock_id:
1482 * @action: a #GtkAction
1483 * @stock_id: the stock id
1485 * Sets the stock id on @action
1490 gtk_action_set_stock_id (GtkAction *action,
1491 const gchar *stock_id)
1495 g_return_if_fail (GTK_IS_ACTION (action));
1497 g_return_if_fail (GTK_IS_ACTION (action));
1499 tmp = action->private_data->stock_id;
1500 action->private_data->stock_id = g_strdup (stock_id);
1503 g_object_notify (G_OBJECT (action), "stock-id");
1505 /* update label and short_label if appropriate */
1506 if (!action->private_data->label_set)
1508 GtkStockItem stock_item;
1510 if (action->private_data->stock_id &&
1511 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1512 gtk_action_set_label (action, stock_item.label);
1514 gtk_action_set_label (action, NULL);
1516 action->private_data->label_set = FALSE;
1521 * gtk_action_get_stock_id:
1522 * @action: a #GtkAction
1524 * Gets the stock id of @action.
1526 * Returns: the stock id
1530 G_CONST_RETURN gchar *
1531 gtk_action_get_stock_id (GtkAction *action)
1533 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1535 return action->private_data->stock_id;
1539 * gtk_action_set_icon_name:
1540 * @action: a #GtkAction
1541 * @icon_name: the icon name to set
1543 * Sets the icon name on @action
1548 gtk_action_set_icon_name (GtkAction *action,
1549 const gchar *icon_name)
1553 g_return_if_fail (GTK_IS_ACTION (action));
1555 tmp = action->private_data->icon_name;
1556 action->private_data->icon_name = g_strdup (icon_name);
1559 g_object_notify (G_OBJECT (action), "icon-name");
1563 * gtk_action_get_icon_name:
1564 * @action: a #GtkAction
1566 * Gets the icon name of @action.
1568 * Returns: the icon name
1572 G_CONST_RETURN gchar *
1573 gtk_action_get_icon_name (GtkAction *action)
1575 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1577 return action->private_data->icon_name;
1581 * gtk_action_set_gicon:
1582 * @action: a #GtkAction
1583 * @icon: the #GIcon to set
1585 * Sets the icon of @action.
1590 gtk_action_set_gicon (GtkAction *action,
1593 g_return_if_fail (GTK_IS_ACTION (action));
1595 if (action->private_data->gicon)
1596 g_object_unref (action->private_data->gicon);
1598 action->private_data->gicon = icon;
1600 if (action->private_data->gicon)
1601 g_object_ref (action->private_data->gicon);
1603 g_object_notify (G_OBJECT (action), "gicon");
1607 * gtk_action_get_gicon:
1608 * @action: a #GtkAction
1610 * Gets the gicon of @action.
1612 * Returns: (transfer none): The action's #GIcon if one is set.
1617 gtk_action_get_gicon (GtkAction *action)
1619 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1621 return action->private_data->gicon;
1625 closure_accel_activate (GClosure *closure,
1626 GValue *return_value,
1627 guint n_param_values,
1628 const GValue *param_values,
1629 gpointer invocation_hint,
1630 gpointer marshal_data)
1632 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1634 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1636 /* we handled the accelerator */
1637 g_value_set_boolean (return_value, TRUE);
1642 gtk_action_set_action_group (GtkAction *action,
1643 GtkActionGroup *action_group)
1645 if (action->private_data->action_group == NULL)
1646 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1648 g_return_if_fail (action_group == NULL);
1650 action->private_data->action_group = action_group;
1654 * gtk_action_set_accel_path:
1655 * @action: the action object
1656 * @accel_path: the accelerator path
1658 * Sets the accel path for this action. All proxy widgets associated
1659 * with the action will have this accel path, so that their
1660 * accelerators are consistent.
1662 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1663 * pass a static string, you can save some memory by interning it first with
1664 * g_intern_static_string().
1669 gtk_action_set_accel_path (GtkAction *action,
1670 const gchar *accel_path)
1672 g_return_if_fail (GTK_IS_ACTION (action));
1674 action->private_data->accel_quark = g_quark_from_string (accel_path);
1678 * gtk_action_get_accel_path:
1679 * @action: the action object
1681 * Returns the accel path for this action.
1685 * Returns: the accel path for this action, or %NULL
1686 * if none is set. The returned string is owned by GTK+
1687 * and must not be freed or modified.
1689 G_CONST_RETURN gchar *
1690 gtk_action_get_accel_path (GtkAction *action)
1692 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1694 if (action->private_data->accel_quark)
1695 return g_quark_to_string (action->private_data->accel_quark);
1701 * gtk_action_get_accel_closure:
1702 * @action: the action object
1704 * Returns the accel closure for this action.
1708 * Returns: the accel closure for this action. The returned closure is
1709 * owned by GTK+ and must not be unreffed or modified.
1712 gtk_action_get_accel_closure (GtkAction *action)
1714 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1716 return action->private_data->accel_closure;
1721 * gtk_action_set_accel_group:
1722 * @action: the action object
1723 * @accel_group: (allow-none): a #GtkAccelGroup or %NULL
1725 * Sets the #GtkAccelGroup in which the accelerator for this action
1726 * will be installed.
1731 gtk_action_set_accel_group (GtkAction *action,
1732 GtkAccelGroup *accel_group)
1734 g_return_if_fail (GTK_IS_ACTION (action));
1735 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1738 g_object_ref (accel_group);
1739 if (action->private_data->accel_group)
1740 g_object_unref (action->private_data->accel_group);
1742 action->private_data->accel_group = accel_group;
1746 * gtk_action_connect_accelerator:
1747 * @action: a #GtkAction
1749 * Installs the accelerator for @action if @action has an
1750 * accel path and group. See gtk_action_set_accel_path() and
1751 * gtk_action_set_accel_group()
1753 * Since multiple proxies may independently trigger the installation
1754 * of the accelerator, the @action counts the number of times this
1755 * function has been called and doesn't remove the accelerator until
1756 * gtk_action_disconnect_accelerator() has been called as many times.
1761 gtk_action_connect_accelerator (GtkAction *action)
1763 g_return_if_fail (GTK_IS_ACTION (action));
1765 if (!action->private_data->accel_quark ||
1766 !action->private_data->accel_group)
1769 if (action->private_data->accel_count == 0)
1771 const gchar *accel_path =
1772 g_quark_to_string (action->private_data->accel_quark);
1774 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1776 action->private_data->accel_closure);
1779 action->private_data->accel_count++;
1783 * gtk_action_disconnect_accelerator:
1784 * @action: a #GtkAction
1786 * Undoes the effect of one call to gtk_action_connect_accelerator().
1791 gtk_action_disconnect_accelerator (GtkAction *action)
1793 g_return_if_fail (GTK_IS_ACTION (action));
1795 if (!action->private_data->accel_quark ||
1796 !action->private_data->accel_group)
1799 action->private_data->accel_count--;
1801 if (action->private_data->accel_count == 0)
1802 gtk_accel_group_disconnect (action->private_data->accel_group,
1803 action->private_data->accel_closure);
1807 * gtk_action_create_menu:
1808 * @action: a #GtkAction
1810 * If @action provides a #GtkMenu widget as a submenu for the menu
1811 * item or the toolbar item it creates, this function returns an
1812 * instance of that menu.
1814 * Return value: (transfer full): the menu item provided by the action, or %NULL.
1819 gtk_action_create_menu (GtkAction *action)
1821 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1823 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1824 return GTK_ACTION_GET_CLASS (action)->create_menu (action);