2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gtkaction.h"
34 #include "gtkactiongroup.h"
35 #include "gtkaccellabel.h"
36 #include "gtkbutton.h"
37 #include "gtkiconfactory.h"
39 #include "gtkimagemenuitem.h"
42 #include "gtkmarshalers.h"
43 #include "gtkmenuitem.h"
45 #include "gtktearoffmenuitem.h"
46 #include "gtktoolbutton.h"
47 #include "gtktoolbar.h"
48 #include "gtkprivate.h"
52 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
54 struct _GtkActionPrivate
60 gchar *stock_id; /* stock icon */
61 gchar *icon_name; /* themed icon */
65 guint label_set : 1; /* these two used so we can set label */
66 guint short_label_set : 1; /* based on stock id */
67 guint visible_horizontal : 1;
68 guint visible_vertical : 1;
69 guint is_important : 1;
70 guint hide_if_empty : 1;
71 guint visible_overflown : 1;
75 GtkAccelGroup *accel_group;
76 GClosure *accel_closure;
79 GtkActionGroup *action_group;
81 /* list of proxy widgets */
100 PROP_VISIBLE_HORIZONTAL,
101 PROP_VISIBLE_VERTICAL,
102 PROP_VISIBLE_OVERFLOWN,
111 static GQuark accel_path_id = 0;
112 static GQuark quark_gtk_action_proxy = 0;
113 static const gchar accel_path_key[] = "GtkAction::accel_path";
114 static const gchar gtk_action_proxy_key[] = "gtk-action";
116 G_DEFINE_TYPE (GtkAction, gtk_action, G_TYPE_OBJECT)
118 static void gtk_action_finalize (GObject *object);
119 static void gtk_action_set_property (GObject *object,
123 static void gtk_action_get_property (GObject *object,
127 static void gtk_action_set_action_group (GtkAction *action,
128 GtkActionGroup *action_group);
129 static void gtk_action_set_is_important (GtkAction *action,
130 gboolean is_important);
131 static void gtk_action_set_label (GtkAction *action,
133 static void gtk_action_set_short_label (GtkAction *action,
135 static void gtk_action_set_visible_horizontal (GtkAction *action,
136 gboolean visible_horizontal);
137 static void gtk_action_set_visible_vertical (GtkAction *action,
138 gboolean visible_vertical);
139 static void gtk_action_set_tooltip (GtkAction *action,
140 const gchar *tooltip);
141 static void gtk_action_set_stock_id (GtkAction *action,
142 const gchar *stock_id);
143 static void gtk_action_set_icon_name (GtkAction *action,
144 const gchar *icon_name);
145 static void gtk_action_sync_tooltip (GtkAction *action,
148 static GtkWidget *create_menu_item (GtkAction *action);
149 static GtkWidget *create_tool_item (GtkAction *action);
150 static void connect_proxy (GtkAction *action,
152 static void disconnect_proxy (GtkAction *action,
154 static void closure_accel_activate (GClosure *closure,
155 GValue *return_value,
156 guint n_param_values,
157 const GValue *param_values,
158 gpointer invocation_hint,
159 gpointer marshal_data);
161 static guint action_signals[LAST_SIGNAL] = { 0 };
165 gtk_action_class_init (GtkActionClass *klass)
167 GObjectClass *gobject_class;
169 accel_path_id = g_quark_from_static_string (accel_path_key);
170 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
172 gobject_class = G_OBJECT_CLASS (klass);
174 gobject_class->finalize = gtk_action_finalize;
175 gobject_class->set_property = gtk_action_set_property;
176 gobject_class->get_property = gtk_action_get_property;
178 klass->activate = NULL;
180 klass->create_menu_item = create_menu_item;
181 klass->create_tool_item = create_tool_item;
182 klass->connect_proxy = connect_proxy;
183 klass->disconnect_proxy = disconnect_proxy;
185 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
186 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
188 g_object_class_install_property (gobject_class,
190 g_param_spec_string ("name",
192 P_("A unique name for the action."),
194 GTK_PARAM_READWRITE |
195 G_PARAM_CONSTRUCT_ONLY));
200 * The label used for menu items and buttons that activate
201 * this action. If the label is %NULL, GTK+ uses the stock
202 * label specified via the stock-id property.
204 g_object_class_install_property (gobject_class,
206 g_param_spec_string ("label",
208 P_("The label used for menu items and buttons "
209 "that activate this action."),
211 GTK_PARAM_READWRITE));
212 g_object_class_install_property (gobject_class,
214 g_param_spec_string ("short-label",
216 P_("A shorter label that may be used on toolbar buttons."),
218 GTK_PARAM_READWRITE));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("tooltip",
223 P_("A tooltip for this action."),
225 GTK_PARAM_READWRITE));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("stock-id",
230 P_("The stock icon displayed in widgets representing "
233 GTK_PARAM_READWRITE));
235 * GtkAction:icon-name:
237 * The name of the icon from the icon theme.
238 * Note that the stock icon is preferred, if
239 * the ::stock-id property holds the id of an
240 * existing stock icon.
244 g_object_class_install_property (gobject_class,
246 g_param_spec_string ("icon-name",
248 P_("The name of the icon from the icon theme"),
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->icon_name = NULL;
352 action->private_data->visible_horizontal = TRUE;
353 action->private_data->visible_vertical = TRUE;
354 action->private_data->visible_overflown = TRUE;
355 action->private_data->is_important = FALSE;
356 action->private_data->hide_if_empty = TRUE;
358 action->private_data->sensitive = TRUE;
359 action->private_data->visible = TRUE;
361 action->private_data->label_set = FALSE;
362 action->private_data->short_label_set = FALSE;
364 action->private_data->accel_count = 0;
365 action->private_data->accel_group = NULL;
366 action->private_data->accel_quark = 0;
367 action->private_data->accel_closure =
368 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
369 g_closure_set_marshal (action->private_data->accel_closure,
370 closure_accel_activate);
371 g_closure_ref (action->private_data->accel_closure);
372 g_closure_sink (action->private_data->accel_closure);
374 action->private_data->action_group = NULL;
376 action->private_data->proxies = NULL;
381 * @name: A unique name for the action
382 * @label: the label displayed in menu items and on buttons
383 * @tooltip: a tooltip for the action
384 * @stock_id: the stock icon to display in widgets representing the action
386 * Creates a new #GtkAction object. To add the action to a
387 * #GtkActionGroup and set the accelerator for the action,
388 * call gtk_action_group_add_action_with_accel().
389 * See <xref linkend="XML-UI"/> for information on allowed action
392 * Return value: a new #GtkAction
397 gtk_action_new (const gchar *name,
399 const gchar *tooltip,
400 const gchar *stock_id)
404 action = g_object_new (GTK_TYPE_ACTION,
408 "stock_id", stock_id,
415 gtk_action_finalize (GObject *object)
418 action = GTK_ACTION (object);
420 g_free (action->private_data->name);
421 g_free (action->private_data->label);
422 g_free (action->private_data->short_label);
423 g_free (action->private_data->tooltip);
424 g_free (action->private_data->stock_id);
425 g_free (action->private_data->icon_name);
427 g_closure_unref (action->private_data->accel_closure);
428 if (action->private_data->accel_group)
429 g_object_unref (action->private_data->accel_group);
431 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
435 gtk_action_set_property (GObject *object,
443 action = GTK_ACTION (object);
448 tmp = action->private_data->name;
449 action->private_data->name = g_value_dup_string (value);
453 gtk_action_set_label (action, g_value_get_string (value));
455 case PROP_SHORT_LABEL:
456 gtk_action_set_short_label (action, g_value_get_string (value));
459 gtk_action_set_tooltip (action, g_value_get_string (value));
462 gtk_action_set_stock_id (action, g_value_get_string (value));
465 gtk_action_set_icon_name (action, g_value_get_string (value));
467 case PROP_VISIBLE_HORIZONTAL:
468 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
470 case PROP_VISIBLE_VERTICAL:
471 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
473 case PROP_VISIBLE_OVERFLOWN:
474 action->private_data->visible_overflown = g_value_get_boolean (value);
476 case PROP_IS_IMPORTANT:
477 gtk_action_set_is_important (action, g_value_get_boolean (value));
479 case PROP_HIDE_IF_EMPTY:
480 action->private_data->hide_if_empty = g_value_get_boolean (value);
483 gtk_action_set_sensitive (action, g_value_get_boolean (value));
486 gtk_action_set_visible (action, g_value_get_boolean (value));
488 case PROP_ACTION_GROUP:
489 gtk_action_set_action_group (action, g_value_get_object (value));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_action_get_property (GObject *object,
505 action = GTK_ACTION (object);
510 g_value_set_string (value, action->private_data->name);
513 g_value_set_string (value, action->private_data->label);
515 case PROP_SHORT_LABEL:
516 g_value_set_string (value, action->private_data->short_label);
519 g_value_set_string (value, action->private_data->tooltip);
522 g_value_set_string (value, action->private_data->stock_id);
525 g_value_set_string (value, action->private_data->icon_name);
527 case PROP_VISIBLE_HORIZONTAL:
528 g_value_set_boolean (value, action->private_data->visible_horizontal);
530 case PROP_VISIBLE_VERTICAL:
531 g_value_set_boolean (value, action->private_data->visible_vertical);
533 case PROP_VISIBLE_OVERFLOWN:
534 g_value_set_boolean (value, action->private_data->visible_overflown);
536 case PROP_IS_IMPORTANT:
537 g_value_set_boolean (value, action->private_data->is_important);
539 case PROP_HIDE_IF_EMPTY:
540 g_value_set_boolean (value, action->private_data->hide_if_empty);
543 g_value_set_boolean (value, action->private_data->sensitive);
546 g_value_set_boolean (value, action->private_data->visible);
548 case PROP_ACTION_GROUP:
549 g_value_set_object (value, action->private_data->action_group);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 create_menu_item (GtkAction *action)
560 GType menu_item_type;
562 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
564 return g_object_new (menu_item_type, NULL);
568 create_tool_item (GtkAction *action)
570 GType toolbar_item_type;
572 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
574 return g_object_new (toolbar_item_type, NULL);
578 remove_proxy (GtkAction *action,
581 if (GTK_IS_MENU_ITEM (proxy))
582 gtk_action_disconnect_accelerator (action);
584 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
588 * _gtk_action_sync_menu_visible:
589 * @action: a #GtkAction, or %NULL to determine the action from @proxy
590 * @proxy: a proxy menu item
591 * @empty: whether the submenu attached to @proxy is empty
593 * Updates the visibility of @proxy from the visibility of @action
594 * according to the following rules:
596 * <listitem><para>if @action is invisible, @proxy is too
598 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
599 * property of @action indicates otherwise
603 * This function is used in the implementation of #GtkUIManager.
606 _gtk_action_sync_menu_visible (GtkAction *action,
610 gboolean visible, hide_if_empty;
612 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
613 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
616 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
618 visible = gtk_action_is_visible (action);
619 hide_if_empty = action->private_data->hide_if_empty;
621 if (visible && !(empty && hide_if_empty))
622 gtk_widget_show (proxy);
624 gtk_widget_hide (proxy);
627 gboolean _gtk_menu_is_empty (GtkWidget *menu);
630 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
633 GtkWidget *menu_item;
635 if (action->private_data->visible_overflown)
637 menu_item = gtk_action_create_menu_item (action);
639 g_object_ref_sink (menu_item);
641 gtk_tool_item_set_proxy_menu_item (tool_item,
642 "gtk-action-menu-item", menu_item);
643 g_object_unref (menu_item);
646 gtk_tool_item_set_proxy_menu_item (tool_item,
647 "gtk-action-menu-item", NULL);
653 connect_proxy (GtkAction *action,
656 g_object_ref (action);
657 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
660 /* add this widget to the list of proxies */
661 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
662 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
664 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
665 if (gtk_action_is_visible (action))
666 gtk_widget_show (proxy);
668 gtk_widget_hide (proxy);
669 gtk_widget_set_no_show_all (proxy, TRUE);
671 if (GTK_IS_MENU_ITEM (proxy))
674 /* menu item specific synchronisers ... */
676 if (action->private_data->accel_quark)
678 gtk_action_connect_accelerator (action);
679 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
680 g_quark_to_string (action->private_data->accel_quark));
683 label = GTK_BIN (proxy)->child;
685 /* make sure label is a label */
686 if (label && !GTK_IS_LABEL (label))
688 gtk_container_remove (GTK_CONTAINER (proxy), label);
693 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
694 "use-underline", TRUE,
700 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
702 "accel-closure", action->private_data->accel_closure,
705 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
707 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
711 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
712 if (image && !GTK_IS_IMAGE (image))
714 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
719 image = gtk_image_new ();
720 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
722 gtk_widget_show (image);
725 if (action->private_data->stock_id &&
726 gtk_icon_factory_lookup_default (action->private_data->stock_id))
727 gtk_image_set_from_stock (GTK_IMAGE (image),
728 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
729 else if (action->private_data->icon_name)
730 gtk_image_set_from_icon_name (GTK_IMAGE (image),
731 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
734 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
735 g_signal_connect_object (proxy, "activate",
736 G_CALLBACK (gtk_action_activate), action,
740 else if (GTK_IS_TOOL_ITEM (proxy))
742 /* toolbar item specific synchronisers ... */
745 "visible-horizontal", action->private_data->visible_horizontal,
746 "visible-vertical", action->private_data->visible_vertical,
747 "is-important", action->private_data->is_important,
750 gtk_action_sync_tooltip (action, proxy);
752 g_signal_connect_object (proxy, "create_menu_proxy",
753 G_CALLBACK (gtk_action_create_menu_proxy),
756 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
758 /* toolbar button specific synchronisers ... */
759 if (GTK_IS_TOOL_BUTTON (proxy))
762 "label", action->private_data->short_label,
763 "use-underline", TRUE,
764 "stock-id", action->private_data->stock_id,
765 "icon-name", action->private_data->icon_name,
768 g_signal_connect_object (proxy, "clicked",
769 G_CALLBACK (gtk_action_activate), action,
773 else if (GTK_IS_BUTTON (proxy))
775 /* button specific synchronisers ... */
776 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
778 /* synchronise stock-id */
780 "label", action->private_data->stock_id,
785 if (GTK_BIN (proxy)->child == NULL ||
786 GTK_IS_LABEL (GTK_BIN (proxy)->child))
788 /* synchronise the label */
790 "label", action->private_data->short_label,
791 "use-underline", TRUE,
795 /* we leave the button alone if there is a custom child */
796 g_signal_connect_object (proxy, "clicked",
797 G_CALLBACK (gtk_action_activate), action,
801 if (action->private_data->action_group)
802 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
806 disconnect_proxy (GtkAction *action,
809 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
811 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
812 remove_proxy (action, proxy);
814 /* disconnect the activate handler */
815 g_signal_handlers_disconnect_by_func (proxy,
816 G_CALLBACK (gtk_action_activate),
819 /* toolbar button specific synchronisers ... */
820 g_signal_handlers_disconnect_by_func (proxy,
821 G_CALLBACK (gtk_action_create_menu_proxy),
824 if (action->private_data->action_group)
825 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
829 _gtk_action_emit_activate (GtkAction *action)
831 GtkActionGroup *group = action->private_data->action_group;
835 g_object_ref (group);
836 _gtk_action_group_emit_pre_activate (group, action);
839 g_signal_emit (action, action_signals[ACTIVATE], 0);
843 _gtk_action_group_emit_post_activate (group, action);
844 g_object_unref (group);
849 * gtk_action_activate:
850 * @action: the action object
852 * Emits the "activate" signal on the specified action, if it isn't
853 * insensitive. This gets called by the proxy widgets when they get
856 * It can also be used to manually activate an action.
861 gtk_action_activate (GtkAction *action)
863 g_return_if_fail (GTK_IS_ACTION (action));
865 if (gtk_action_is_sensitive (action))
866 _gtk_action_emit_activate (action);
870 * gtk_action_create_icon:
871 * @action: the action object
872 * @icon_size: the size of the icon that should be created.
874 * This function is intended for use by action implementations to
875 * create icons displayed in the proxy widgets.
877 * Returns: a widget that displays the icon for this action.
882 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
884 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
886 if (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->icon_name)
889 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
895 * gtk_action_create_menu_item:
896 * @action: the action object
898 * Creates a menu item widget that proxies for the given action.
900 * Returns: a menu item connected to the action.
905 gtk_action_create_menu_item (GtkAction *action)
907 GtkWidget *menu_item;
909 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
911 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
913 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
919 * gtk_action_create_tool_item:
920 * @action: the action object
922 * Creates a toolbar item widget that proxies for the given action.
924 * Returns: a toolbar item connected to the action.
929 gtk_action_create_tool_item (GtkAction *action)
933 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
935 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
937 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
943 * gtk_action_connect_proxy:
944 * @action: the action object
945 * @proxy: the proxy widget
947 * Connects a widget to an action object as a proxy. Synchronises
948 * various properties of the action with the widget (such as label
949 * text, icon, tooltip, etc), and attaches a callback so that the
950 * action gets activated when the proxy widget does.
952 * If the widget is already connected to an action, it is disconnected
958 gtk_action_connect_proxy (GtkAction *action,
961 GtkAction *prev_action;
963 g_return_if_fail (GTK_IS_ACTION (action));
964 g_return_if_fail (GTK_IS_WIDGET (proxy));
966 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
969 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
971 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
975 * gtk_action_disconnect_proxy:
976 * @action: the action object
977 * @proxy: the proxy widget
979 * Disconnects a proxy widget from an action.
980 * Does <emphasis>not</emphasis> destroy the widget, however.
985 gtk_action_disconnect_proxy (GtkAction *action,
988 g_return_if_fail (GTK_IS_ACTION (action));
989 g_return_if_fail (GTK_IS_WIDGET (proxy));
991 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
993 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
997 * gtk_action_get_proxies:
998 * @action: the action object
1000 * Returns the proxy widgets for an action.
1001 * See also gtk_widget_get_action().
1003 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1004 * and must not be modified.
1009 gtk_action_get_proxies (GtkAction *action)
1011 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1013 return action->private_data->proxies;
1018 * gtk_widget_get_action:
1019 * @widget: a #GtkWidget
1021 * Returns the #GtkAction that @widget is a proxy for.
1022 * See also gtk_action_get_proxies().
1024 * Returns: the action that a widget is a proxy for, or
1025 * %NULL, if it is not attached to an action.
1030 gtk_widget_get_action (GtkWidget *widget)
1032 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1034 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1039 * gtk_action_get_name:
1040 * @action: the action object
1042 * Returns the name of the action.
1044 * Return value: the name of the action. The string belongs to GTK+ and should not
1049 G_CONST_RETURN gchar *
1050 gtk_action_get_name (GtkAction *action)
1052 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1054 return action->private_data->name;
1058 * gtk_action_is_sensitive:
1059 * @action: the action object
1061 * Returns whether the action is effectively sensitive.
1063 * Return value: %TRUE if the action and its associated action group
1064 * are both sensitive.
1069 gtk_action_is_sensitive (GtkAction *action)
1071 GtkActionPrivate *priv;
1072 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1074 priv = action->private_data;
1075 return priv->sensitive &&
1076 (priv->action_group == NULL ||
1077 gtk_action_group_get_sensitive (priv->action_group));
1081 * gtk_action_get_sensitive:
1082 * @action: the action object
1084 * Returns whether the action itself is sensitive. Note that this doesn't
1085 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1088 * Return value: %TRUE if the action itself is sensitive.
1093 gtk_action_get_sensitive (GtkAction *action)
1095 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1097 return action->private_data->sensitive;
1101 _gtk_action_sync_sensitive (GtkAction *action)
1107 sensitive = gtk_action_is_sensitive (action);
1109 for (p = action->private_data->proxies; p; p = p->next)
1111 proxy = (GtkWidget *)p->data;
1112 gtk_widget_set_sensitive (proxy, sensitive);
1117 * gtk_action_set_sensitive:
1118 * @action: the action object
1119 * @sensitive: %TRUE to make the action sensitive
1121 * Sets the ::sensitive property of the action to @sensitive. Note that
1122 * this doesn't necessarily mean effective sensitivity. See
1123 * gtk_action_is_sensitive()
1129 gtk_action_set_sensitive (GtkAction *action,
1132 g_return_if_fail (GTK_IS_ACTION (action));
1134 sensitive = sensitive != FALSE;
1136 if (action->private_data->sensitive != sensitive)
1138 action->private_data->sensitive = sensitive;
1140 _gtk_action_sync_sensitive (action);
1142 g_object_notify (G_OBJECT (action), "sensitive");
1147 * gtk_action_is_visible:
1148 * @action: the action object
1150 * Returns whether the action is effectively visible.
1152 * Return value: %TRUE if the action and its associated action group
1158 gtk_action_is_visible (GtkAction *action)
1160 GtkActionPrivate *priv;
1161 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1163 priv = action->private_data;
1164 return priv->visible &&
1165 (priv->action_group == NULL ||
1166 gtk_action_group_get_visible (priv->action_group));
1170 * gtk_action_get_visible:
1171 * @action: the action object
1173 * Returns whether the action itself is visible. Note that this doesn't
1174 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1177 * Return value: %TRUE if the action itself is visible.
1182 gtk_action_get_visible (GtkAction *action)
1184 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1186 return action->private_data->visible;
1190 _gtk_action_sync_visible (GtkAction *action)
1197 visible = gtk_action_is_visible (action);
1199 for (p = action->private_data->proxies; p; p = p->next)
1201 proxy = (GtkWidget *)p->data;
1203 if (GTK_IS_MENU_ITEM (proxy))
1205 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1207 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1212 gtk_widget_show (proxy);
1214 gtk_widget_hide (proxy);
1220 * gtk_action_set_visible:
1221 * @action: the action object
1222 * @visible: %TRUE to make the action visible
1224 * Sets the ::visible property of the action to @visible. Note that
1225 * this doesn't necessarily mean effective visibility. See
1226 * gtk_action_is_visible()
1232 gtk_action_set_visible (GtkAction *action,
1235 g_return_if_fail (GTK_IS_ACTION (action));
1237 visible = visible != FALSE;
1239 if (action->private_data->visible != visible)
1241 action->private_data->visible = visible;
1243 _gtk_action_sync_visible (action);
1245 g_object_notify (G_OBJECT (action), "visible");
1250 gtk_action_set_is_important (GtkAction *action,
1251 gboolean is_important)
1256 is_important = is_important != FALSE;
1258 if (action->private_data->is_important != is_important)
1260 action->private_data->is_important = is_important;
1262 for (p = action->private_data->proxies; p; p = p->next)
1264 proxy = (GtkWidget *)p->data;
1266 if (GTK_IS_TOOL_ITEM (proxy))
1267 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1271 g_object_notify (G_OBJECT (action), "is-important");
1276 gtk_action_set_label (GtkAction *action,
1280 GtkWidget *proxy, *child;
1283 tmp = action->private_data->label;
1284 action->private_data->label = g_strdup (label);
1286 action->private_data->label_set = (action->private_data->label != NULL);
1287 /* if label is unset, then use the label from the stock item */
1288 if (!action->private_data->label_set && action->private_data->stock_id)
1290 GtkStockItem stock_item;
1292 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1293 action->private_data->label = g_strdup (stock_item.label);
1296 for (p = action->private_data->proxies; p; p = p->next)
1298 proxy = (GtkWidget *)p->data;
1300 if (GTK_IS_MENU_ITEM (proxy))
1302 child = GTK_BIN (proxy)->child;
1304 if (GTK_IS_LABEL (child))
1305 gtk_label_set_label (GTK_LABEL (child),
1306 action->private_data->label);
1310 g_object_notify (G_OBJECT (action), "label");
1312 /* if short_label is unset, set short_label=label */
1313 if (!action->private_data->short_label_set)
1315 gtk_action_set_short_label (action, action->private_data->label);
1316 action->private_data->short_label_set = FALSE;
1321 gtk_action_set_short_label (GtkAction *action,
1325 GtkWidget *proxy, *child;
1328 tmp = action->private_data->short_label;
1329 action->private_data->short_label = g_strdup (label);
1331 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1332 /* if short_label is unset, then use the value of label */
1333 if (!action->private_data->short_label_set)
1334 action->private_data->short_label = g_strdup (action->private_data->label);
1336 for (p = action->private_data->proxies; p; p = p->next)
1338 proxy = (GtkWidget *)p->data;
1340 if (GTK_IS_TOOL_BUTTON (proxy))
1341 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1342 action->private_data->label);
1343 else if (GTK_IS_BUTTON (proxy) &&
1344 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1346 child = GTK_BIN (proxy)->child;
1348 if (child == NULL || GTK_IS_LABEL (child))
1349 gtk_button_set_label (GTK_BUTTON (proxy),
1350 action->private_data->label);
1354 g_object_notify (G_OBJECT (action), "short-label");
1358 gtk_action_set_visible_horizontal (GtkAction *action,
1359 gboolean visible_horizontal)
1364 visible_horizontal = visible_horizontal != FALSE;
1366 if (action->private_data->visible_horizontal != visible_horizontal)
1368 action->private_data->visible_horizontal = visible_horizontal;
1370 for (p = action->private_data->proxies; p; p = p->next)
1372 proxy = (GtkWidget *)p->data;
1374 if (GTK_IS_TOOL_ITEM (proxy))
1375 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1376 visible_horizontal);
1379 g_object_notify (G_OBJECT (action), "visible-horizontal");
1384 gtk_action_set_visible_vertical (GtkAction *action,
1385 gboolean visible_vertical)
1390 visible_vertical = visible_vertical != FALSE;
1392 if (action->private_data->visible_vertical != visible_vertical)
1394 action->private_data->visible_vertical = visible_vertical;
1396 for (p = action->private_data->proxies; p; p = p->next)
1398 proxy = (GtkWidget *)p->data;
1400 if (GTK_IS_TOOL_ITEM (proxy))
1401 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1405 g_object_notify (G_OBJECT (action), "visible-vertical");
1410 gtk_action_sync_tooltip (GtkAction *action,
1415 parent = gtk_widget_get_parent (proxy);
1417 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1418 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1419 GTK_TOOLBAR (parent)->tooltips,
1420 action->private_data->tooltip,
1425 gtk_action_set_tooltip (GtkAction *action,
1426 const gchar *tooltip)
1432 tmp = action->private_data->tooltip;
1433 action->private_data->tooltip = g_strdup (tooltip);
1436 for (p = action->private_data->proxies; p; p = p->next)
1438 proxy = (GtkWidget *)p->data;
1440 gtk_action_sync_tooltip (action, proxy);
1443 g_object_notify (G_OBJECT (action), "tooltip");
1447 gtk_action_set_stock_id (GtkAction *action,
1448 const gchar *stock_id)
1451 GtkWidget *proxy, *image;
1454 tmp = action->private_data->stock_id;
1455 action->private_data->stock_id = g_strdup (stock_id);
1458 for (p = action->private_data->proxies; p; p = p->next)
1460 proxy = (GtkWidget *)p->data;
1462 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1464 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1466 if (GTK_IS_IMAGE (image))
1467 gtk_image_set_from_stock (GTK_IMAGE (image),
1468 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1470 else if (GTK_IS_TOOL_BUTTON (proxy))
1472 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1473 action->private_data->stock_id);
1475 else if (GTK_IS_BUTTON (proxy) &&
1476 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1478 gtk_button_set_label (GTK_BUTTON (proxy),
1479 action->private_data->stock_id);
1483 g_object_notify (G_OBJECT (action), "stock-id");
1485 /* update label and short_label if appropriate */
1486 if (!action->private_data->label_set)
1488 GtkStockItem stock_item;
1490 if (action->private_data->stock_id &&
1491 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1492 gtk_action_set_label (action, stock_item.label);
1494 gtk_action_set_label (action, NULL);
1496 action->private_data->label_set = FALSE;
1501 gtk_action_set_icon_name (GtkAction *action,
1502 const gchar *icon_name)
1505 GtkWidget *proxy, *image;
1508 tmp = action->private_data->icon_name;
1509 action->private_data->icon_name = g_strdup (icon_name);
1512 for (p = action->private_data->proxies; p; p = p->next)
1514 proxy = (GtkWidget *)p->data;
1516 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1518 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1520 if (GTK_IS_IMAGE (image) &&
1521 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1522 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1523 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1524 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1526 else if (GTK_IS_TOOL_BUTTON (proxy))
1528 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1529 action->private_data->icon_name);
1531 else if (GTK_IS_BUTTON (proxy) &&
1532 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1534 image = gtk_button_get_image (GTK_BUTTON (proxy));
1536 if (GTK_IS_IMAGE (image) &&
1537 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1538 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1539 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1540 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1544 g_object_notify (G_OBJECT (action), "icon-name");
1549 * gtk_action_block_activate_from:
1550 * @action: the action object
1551 * @proxy: a proxy widget
1553 * Disables calls to the gtk_action_activate()
1554 * function by signals on the given proxy widget. This is used to
1555 * break notification loops for things like check or radio actions.
1557 * This function is intended for use by action implementations.
1562 gtk_action_block_activate_from (GtkAction *action,
1565 g_return_if_fail (GTK_IS_ACTION (action));
1567 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1572 * gtk_action_unblock_activate_from:
1573 * @action: the action object
1574 * @proxy: a proxy widget
1576 * Re-enables calls to the gtk_action_activate()
1577 * function by signals on the given proxy widget. This undoes the
1578 * blocking done by gtk_action_block_activate_from().
1580 * This function is intended for use by action implementations.
1585 gtk_action_unblock_activate_from (GtkAction *action,
1588 g_return_if_fail (GTK_IS_ACTION (action));
1590 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1595 closure_accel_activate (GClosure *closure,
1596 GValue *return_value,
1597 guint n_param_values,
1598 const GValue *param_values,
1599 gpointer invocation_hint,
1600 gpointer marshal_data)
1602 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1604 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1606 /* we handled the accelerator */
1607 g_value_set_boolean (return_value, TRUE);
1612 gtk_action_set_action_group (GtkAction *action,
1613 GtkActionGroup *action_group)
1615 g_return_if_fail (GTK_IS_ACTION (action));
1617 if (action->private_data->action_group == NULL)
1618 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1620 g_return_if_fail (action_group == NULL);
1622 action->private_data->action_group = action_group;
1626 * gtk_action_set_accel_path:
1627 * @action: the action object
1628 * @accel_path: the accelerator path
1630 * Sets the accel path for this action. All proxy widgets associated
1631 * with the action will have this accel path, so that their
1632 * accelerators are consistent.
1637 gtk_action_set_accel_path (GtkAction *action,
1638 const gchar *accel_path)
1640 g_return_if_fail (GTK_IS_ACTION (action));
1642 action->private_data->accel_quark = g_quark_from_string (accel_path);
1646 * gtk_action_get_accel_path:
1647 * @action: the action object
1649 * Returns the accel path for this action.
1653 * Returns: the accel path for this action, or %NULL
1654 * if none is set. The returned string is owned by GTK+
1655 * and must not be freed or modified.
1657 G_CONST_RETURN gchar *
1658 gtk_action_get_accel_path (GtkAction *action)
1660 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1662 if (action->private_data->accel_quark)
1663 return g_quark_to_string (action->private_data->accel_quark);
1669 * gtk_action_get_accel_closure:
1670 * @action: the action object
1672 * Returns the accel closure for this action.
1676 * Returns: the accel closure for this action. The returned closure is
1677 * owned by GTK+ and must not be unreffed or modified.
1680 gtk_action_get_accel_closure (GtkAction *action)
1682 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1684 return action->private_data->accel_closure;
1689 * gtk_action_set_accel_group:
1690 * @action: the action object
1691 * @accel_group: a #GtkAccelGroup or %NULL
1693 * Sets the #GtkAccelGroup in which the accelerator for this action
1694 * will be installed.
1699 gtk_action_set_accel_group (GtkAction *action,
1700 GtkAccelGroup *accel_group)
1702 g_return_if_fail (GTK_IS_ACTION (action));
1703 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1706 g_object_ref (accel_group);
1707 if (action->private_data->accel_group)
1708 g_object_unref (action->private_data->accel_group);
1710 action->private_data->accel_group = accel_group;
1714 * gtk_action_connect_accelerator:
1715 * @action: a #GtkAction
1717 * Installs the accelerator for @action if @action has an
1718 * accel path and group. See gtk_action_set_accel_path() and
1719 * gtk_action_set_accel_group()
1721 * Since multiple proxies may independently trigger the installation
1722 * of the accelerator, the @action counts the number of times this
1723 * function has been called and doesn't remove the accelerator until
1724 * gtk_action_disconnect_accelerator() has been called as many times.
1729 gtk_action_connect_accelerator (GtkAction *action)
1731 g_return_if_fail (GTK_IS_ACTION (action));
1733 if (!action->private_data->accel_quark ||
1734 !action->private_data->accel_group)
1737 if (action->private_data->accel_count == 0)
1739 const gchar *accel_path =
1740 g_quark_to_string (action->private_data->accel_quark);
1742 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1744 action->private_data->accel_closure);
1747 action->private_data->accel_count++;
1751 * gtk_action_disconnect_accelerator:
1752 * @action: a #GtkAction
1754 * Undoes the effect of one call to gtk_action_connect_accelerator().
1759 gtk_action_disconnect_accelerator (GtkAction *action)
1761 g_return_if_fail (GTK_IS_ACTION (action));
1763 if (!action->private_data->accel_quark ||
1764 !action->private_data->accel_group)
1767 action->private_data->accel_count--;
1769 if (action->private_data->accel_count == 0)
1770 gtk_accel_group_disconnect (action->private_data->accel_group,
1771 action->private_data->accel_closure);
1774 #define __GTK_ACTION_C__
1775 #include "gtkaliasdef.c"