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,
155 static void closure_accel_activate (GClosure *closure,
156 GValue *return_value,
157 guint n_param_values,
158 const GValue *param_values,
159 gpointer invocation_hint,
160 gpointer marshal_data);
162 static guint action_signals[LAST_SIGNAL] = { 0 };
166 gtk_action_class_init (GtkActionClass *klass)
168 GObjectClass *gobject_class;
170 accel_path_id = g_quark_from_static_string (accel_path_key);
171 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
173 gobject_class = G_OBJECT_CLASS (klass);
175 gobject_class->finalize = gtk_action_finalize;
176 gobject_class->set_property = gtk_action_set_property;
177 gobject_class->get_property = gtk_action_get_property;
179 klass->activate = NULL;
181 klass->create_menu_item = create_menu_item;
182 klass->create_tool_item = create_tool_item;
183 klass->create_menu = NULL;
184 klass->connect_proxy = connect_proxy;
185 klass->disconnect_proxy = disconnect_proxy;
187 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
188 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
190 g_object_class_install_property (gobject_class,
192 g_param_spec_string ("name",
194 P_("A unique name for the action."),
196 GTK_PARAM_READWRITE |
197 G_PARAM_CONSTRUCT_ONLY));
202 * The label used for menu items and buttons that activate
203 * this action. If the label is %NULL, GTK+ uses the stock
204 * label specified via the stock-id property.
206 g_object_class_install_property (gobject_class,
208 g_param_spec_string ("label",
210 P_("The label used for menu items and buttons "
211 "that activate this action."),
213 GTK_PARAM_READWRITE));
214 g_object_class_install_property (gobject_class,
216 g_param_spec_string ("short-label",
218 P_("A shorter label that may be used on toolbar buttons."),
220 GTK_PARAM_READWRITE));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_string ("tooltip",
225 P_("A tooltip for this action."),
227 GTK_PARAM_READWRITE));
228 g_object_class_install_property (gobject_class,
230 g_param_spec_string ("stock-id",
232 P_("The stock icon displayed in widgets representing "
235 GTK_PARAM_READWRITE));
237 * GtkAction:icon-name:
239 * The name of the icon from the icon theme.
240 * Note that the stock icon is preferred, if
241 * the ::stock-id property holds the id of an
242 * existing stock icon.
246 g_object_class_install_property (gobject_class,
248 g_param_spec_string ("icon-name",
250 P_("The name of the icon from the icon theme"),
252 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
254 PROP_VISIBLE_HORIZONTAL,
255 g_param_spec_boolean ("visible-horizontal",
256 P_("Visible when horizontal"),
257 P_("Whether the toolbar item is visible when the toolbar "
258 "is in a horizontal orientation."),
260 GTK_PARAM_READWRITE));
262 * GtkAction:visible-overflown:
264 * When %TRUE, toolitem proxies for this action are represented in the
265 * toolbar overflow menu.
269 g_object_class_install_property (gobject_class,
270 PROP_VISIBLE_OVERFLOWN,
271 g_param_spec_boolean ("visible-overflown",
272 P_("Visible when overflown"),
273 P_("When TRUE, toolitem proxies for this action "
274 "are represented in the toolbar overflow menu."),
276 GTK_PARAM_READWRITE));
277 g_object_class_install_property (gobject_class,
278 PROP_VISIBLE_VERTICAL,
279 g_param_spec_boolean ("visible-vertical",
280 P_("Visible when vertical"),
281 P_("Whether the toolbar item is visible when the toolbar "
282 "is in a vertical orientation."),
284 GTK_PARAM_READWRITE));
285 g_object_class_install_property (gobject_class,
287 g_param_spec_boolean ("is-important",
289 P_("Whether the action is considered important. "
290 "When TRUE, toolitem proxies for this action "
291 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
293 GTK_PARAM_READWRITE));
294 g_object_class_install_property (gobject_class,
296 g_param_spec_boolean ("hide-if-empty",
298 P_("When TRUE, empty menu proxies for this action are hidden."),
300 GTK_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_boolean ("sensitive",
305 P_("Whether the action is enabled."),
307 GTK_PARAM_READWRITE));
308 g_object_class_install_property (gobject_class,
310 g_param_spec_boolean ("visible",
312 P_("Whether the action is visible."),
314 GTK_PARAM_READWRITE));
315 g_object_class_install_property (gobject_class,
317 g_param_spec_object ("action-group",
319 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
320 GTK_TYPE_ACTION_GROUP,
321 GTK_PARAM_READWRITE));
324 * GtkAction::activate:
325 * @action: the #GtkAction
327 * The "activate" signal is emitted when the action is activated.
331 action_signals[ACTIVATE] =
332 g_signal_new (I_("activate"),
333 G_OBJECT_CLASS_TYPE (klass),
334 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
335 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
336 g_cclosure_marshal_VOID__VOID,
339 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
344 gtk_action_init (GtkAction *action)
346 action->private_data = GTK_ACTION_GET_PRIVATE (action);
348 action->private_data->name = NULL;
349 action->private_data->label = NULL;
350 action->private_data->short_label = NULL;
351 action->private_data->tooltip = NULL;
352 action->private_data->stock_id = NULL;
353 action->private_data->icon_name = NULL;
354 action->private_data->visible_horizontal = TRUE;
355 action->private_data->visible_vertical = TRUE;
356 action->private_data->visible_overflown = TRUE;
357 action->private_data->is_important = FALSE;
358 action->private_data->hide_if_empty = TRUE;
360 action->private_data->sensitive = TRUE;
361 action->private_data->visible = TRUE;
363 action->private_data->label_set = FALSE;
364 action->private_data->short_label_set = FALSE;
366 action->private_data->accel_count = 0;
367 action->private_data->accel_group = NULL;
368 action->private_data->accel_quark = 0;
369 action->private_data->accel_closure =
370 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
371 g_closure_set_marshal (action->private_data->accel_closure,
372 closure_accel_activate);
373 g_closure_ref (action->private_data->accel_closure);
374 g_closure_sink (action->private_data->accel_closure);
376 action->private_data->action_group = NULL;
378 action->private_data->proxies = NULL;
383 * @name: A unique name for the action
384 * @label: the label displayed in menu items and on buttons
385 * @tooltip: a tooltip for the action
386 * @stock_id: the stock icon to display in widgets representing the action
388 * Creates a new #GtkAction object. To add the action to a
389 * #GtkActionGroup and set the accelerator for the action,
390 * call gtk_action_group_add_action_with_accel().
391 * See <xref linkend="XML-UI"/> for information on allowed action
394 * Return value: a new #GtkAction
399 gtk_action_new (const gchar *name,
401 const gchar *tooltip,
402 const gchar *stock_id)
406 action = g_object_new (GTK_TYPE_ACTION,
410 "stock-id", stock_id,
417 gtk_action_finalize (GObject *object)
420 action = GTK_ACTION (object);
422 g_free (action->private_data->name);
423 g_free (action->private_data->label);
424 g_free (action->private_data->short_label);
425 g_free (action->private_data->tooltip);
426 g_free (action->private_data->stock_id);
427 g_free (action->private_data->icon_name);
429 g_closure_unref (action->private_data->accel_closure);
430 if (action->private_data->accel_group)
431 g_object_unref (action->private_data->accel_group);
433 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
437 gtk_action_set_property (GObject *object,
445 action = GTK_ACTION (object);
450 tmp = action->private_data->name;
451 action->private_data->name = g_value_dup_string (value);
455 gtk_action_set_label (action, g_value_get_string (value));
457 case PROP_SHORT_LABEL:
458 gtk_action_set_short_label (action, g_value_get_string (value));
461 gtk_action_set_tooltip (action, g_value_get_string (value));
464 gtk_action_set_stock_id (action, g_value_get_string (value));
467 gtk_action_set_icon_name (action, g_value_get_string (value));
469 case PROP_VISIBLE_HORIZONTAL:
470 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
472 case PROP_VISIBLE_VERTICAL:
473 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
475 case PROP_VISIBLE_OVERFLOWN:
476 action->private_data->visible_overflown = g_value_get_boolean (value);
478 case PROP_IS_IMPORTANT:
479 gtk_action_set_is_important (action, g_value_get_boolean (value));
481 case PROP_HIDE_IF_EMPTY:
482 action->private_data->hide_if_empty = g_value_get_boolean (value);
485 gtk_action_set_sensitive (action, g_value_get_boolean (value));
488 gtk_action_set_visible (action, g_value_get_boolean (value));
490 case PROP_ACTION_GROUP:
491 gtk_action_set_action_group (action, g_value_get_object (value));
494 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
500 gtk_action_get_property (GObject *object,
507 action = GTK_ACTION (object);
512 g_value_set_string (value, action->private_data->name);
515 g_value_set_string (value, action->private_data->label);
517 case PROP_SHORT_LABEL:
518 g_value_set_string (value, action->private_data->short_label);
521 g_value_set_string (value, action->private_data->tooltip);
524 g_value_set_string (value, action->private_data->stock_id);
527 g_value_set_string (value, action->private_data->icon_name);
529 case PROP_VISIBLE_HORIZONTAL:
530 g_value_set_boolean (value, action->private_data->visible_horizontal);
532 case PROP_VISIBLE_VERTICAL:
533 g_value_set_boolean (value, action->private_data->visible_vertical);
535 case PROP_VISIBLE_OVERFLOWN:
536 g_value_set_boolean (value, action->private_data->visible_overflown);
538 case PROP_IS_IMPORTANT:
539 g_value_set_boolean (value, action->private_data->is_important);
541 case PROP_HIDE_IF_EMPTY:
542 g_value_set_boolean (value, action->private_data->hide_if_empty);
545 g_value_set_boolean (value, action->private_data->sensitive);
548 g_value_set_boolean (value, action->private_data->visible);
550 case PROP_ACTION_GROUP:
551 g_value_set_object (value, action->private_data->action_group);
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 create_menu_item (GtkAction *action)
562 GType menu_item_type;
564 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
566 return g_object_new (menu_item_type, NULL);
570 create_tool_item (GtkAction *action)
572 GType toolbar_item_type;
574 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
576 return g_object_new (toolbar_item_type, NULL);
580 remove_proxy (GtkAction *action,
583 if (GTK_IS_MENU_ITEM (proxy))
584 gtk_action_disconnect_accelerator (action);
586 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
590 * _gtk_action_sync_menu_visible:
591 * @action: a #GtkAction, or %NULL to determine the action from @proxy
592 * @proxy: a proxy menu item
593 * @empty: whether the submenu attached to @proxy is empty
595 * Updates the visibility of @proxy from the visibility of @action
596 * according to the following rules:
598 * <listitem><para>if @action is invisible, @proxy is too
600 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
601 * property of @action indicates otherwise
605 * This function is used in the implementation of #GtkUIManager.
608 _gtk_action_sync_menu_visible (GtkAction *action,
612 gboolean visible, hide_if_empty;
614 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
615 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
618 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
620 visible = gtk_action_is_visible (action);
621 hide_if_empty = action->private_data->hide_if_empty;
623 if (visible && !(empty && hide_if_empty))
624 gtk_widget_show (proxy);
626 gtk_widget_hide (proxy);
629 gboolean _gtk_menu_is_empty (GtkWidget *menu);
632 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
635 GtkWidget *menu_item;
637 if (action->private_data->visible_overflown)
639 menu_item = gtk_action_create_menu_item (action);
641 g_object_ref_sink (menu_item);
643 gtk_tool_item_set_proxy_menu_item (tool_item,
644 "gtk-action-menu-item", menu_item);
645 g_object_unref (menu_item);
648 gtk_tool_item_set_proxy_menu_item (tool_item,
649 "gtk-action-menu-item", NULL);
655 connect_proxy (GtkAction *action,
658 g_object_ref (action);
659 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
662 /* add this widget to the list of proxies */
663 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
664 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
666 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
667 if (gtk_action_is_visible (action))
668 gtk_widget_show (proxy);
670 gtk_widget_hide (proxy);
671 gtk_widget_set_no_show_all (proxy, TRUE);
673 if (GTK_IS_MENU_ITEM (proxy))
676 /* menu item specific synchronisers ... */
678 if (action->private_data->accel_quark)
680 gtk_action_connect_accelerator (action);
681 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
682 g_quark_to_string (action->private_data->accel_quark));
685 label = GTK_BIN (proxy)->child;
687 /* make sure label is a label */
688 if (label && !GTK_IS_LABEL (label))
690 gtk_container_remove (GTK_CONTAINER (proxy), label);
695 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
696 "use-underline", TRUE,
702 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
704 "accel-closure", action->private_data->accel_closure,
707 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
709 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
713 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
714 if (image && !GTK_IS_IMAGE (image))
716 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
721 image = gtk_image_new ();
722 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
724 gtk_widget_show (image);
727 if (action->private_data->stock_id &&
728 gtk_icon_factory_lookup_default (action->private_data->stock_id))
729 gtk_image_set_from_stock (GTK_IMAGE (image),
730 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
731 else if (action->private_data->icon_name)
732 gtk_image_set_from_icon_name (GTK_IMAGE (image),
733 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
736 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
737 g_signal_connect_object (proxy, "activate",
738 G_CALLBACK (gtk_action_activate), action,
742 else if (GTK_IS_TOOL_ITEM (proxy))
744 /* toolbar button specific synchronisers ... */
745 if (GTK_IS_TOOL_BUTTON (proxy))
748 "visible-horizontal", action->private_data->visible_horizontal,
749 "visible-vertical", action->private_data->visible_vertical,
750 "is-important", action->private_data->is_important,
751 "label", action->private_data->short_label,
752 "use-underline", TRUE,
753 "stock-id", action->private_data->stock_id,
754 "icon-name", action->private_data->icon_name,
757 g_signal_connect_object (proxy, "clicked",
758 G_CALLBACK (gtk_action_activate), action,
764 "visible-horizontal", action->private_data->visible_horizontal,
765 "visible-vertical", action->private_data->visible_vertical,
766 "is-important", action->private_data->is_important,
770 gtk_action_sync_tooltip (action, proxy);
772 g_signal_connect_object (proxy, "create_menu_proxy",
773 G_CALLBACK (gtk_action_create_menu_proxy),
776 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
778 else if (GTK_IS_BUTTON (proxy))
780 /* button specific synchronisers ... */
781 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
783 /* synchronise stock-id */
785 "label", action->private_data->stock_id,
790 if (GTK_BIN (proxy)->child == NULL ||
791 GTK_IS_LABEL (GTK_BIN (proxy)->child))
793 /* synchronise the label */
795 "label", action->private_data->short_label,
796 "use-underline", TRUE,
800 /* we leave the button alone if there is a custom child */
801 g_signal_connect_object (proxy, "clicked",
802 G_CALLBACK (gtk_action_activate), action,
806 if (action->private_data->action_group)
807 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
811 disconnect_proxy (GtkAction *action,
814 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
816 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
817 remove_proxy (action, proxy);
819 /* disconnect the activate handler */
820 g_signal_handlers_disconnect_by_func (proxy,
821 G_CALLBACK (gtk_action_activate),
824 /* toolbar button specific synchronisers ... */
825 g_signal_handlers_disconnect_by_func (proxy,
826 G_CALLBACK (gtk_action_create_menu_proxy),
829 if (action->private_data->action_group)
830 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
834 _gtk_action_emit_activate (GtkAction *action)
836 GtkActionGroup *group = action->private_data->action_group;
840 g_object_ref (group);
841 _gtk_action_group_emit_pre_activate (group, action);
844 g_signal_emit (action, action_signals[ACTIVATE], 0);
848 _gtk_action_group_emit_post_activate (group, action);
849 g_object_unref (group);
854 * gtk_action_activate:
855 * @action: the action object
857 * Emits the "activate" signal on the specified action, if it isn't
858 * insensitive. This gets called by the proxy widgets when they get
861 * It can also be used to manually activate an action.
866 gtk_action_activate (GtkAction *action)
868 g_return_if_fail (GTK_IS_ACTION (action));
870 if (gtk_action_is_sensitive (action))
871 _gtk_action_emit_activate (action);
875 * gtk_action_create_icon:
876 * @action: the action object
877 * @icon_size: the size of the icon that should be created.
879 * This function is intended for use by action implementations to
880 * create icons displayed in the proxy widgets.
882 * Returns: a widget that displays the icon for this action.
887 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
889 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
891 if (action->private_data->stock_id)
892 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
893 else if (action->private_data->icon_name)
894 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
900 * gtk_action_create_menu_item:
901 * @action: the action object
903 * Creates a menu item widget that proxies for the given action.
905 * Returns: a menu item connected to the action.
910 gtk_action_create_menu_item (GtkAction *action)
912 GtkWidget *menu_item;
914 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
916 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
918 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
924 * gtk_action_create_tool_item:
925 * @action: the action object
927 * Creates a toolbar item widget that proxies for the given action.
929 * Returns: a toolbar item connected to the action.
934 gtk_action_create_tool_item (GtkAction *action)
938 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
940 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
942 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
948 * gtk_action_connect_proxy:
949 * @action: the action object
950 * @proxy: the proxy widget
952 * Connects a widget to an action object as a proxy. Synchronises
953 * various properties of the action with the widget (such as label
954 * text, icon, tooltip, etc), and attaches a callback so that the
955 * action gets activated when the proxy widget does.
957 * If the widget is already connected to an action, it is disconnected
963 gtk_action_connect_proxy (GtkAction *action,
966 GtkAction *prev_action;
968 g_return_if_fail (GTK_IS_ACTION (action));
969 g_return_if_fail (GTK_IS_WIDGET (proxy));
971 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
974 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
976 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
980 * gtk_action_disconnect_proxy:
981 * @action: the action object
982 * @proxy: the proxy widget
984 * Disconnects a proxy widget from an action.
985 * Does <emphasis>not</emphasis> destroy the widget, however.
990 gtk_action_disconnect_proxy (GtkAction *action,
993 g_return_if_fail (GTK_IS_ACTION (action));
994 g_return_if_fail (GTK_IS_WIDGET (proxy));
996 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
998 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1002 * gtk_action_get_proxies:
1003 * @action: the action object
1005 * Returns the proxy widgets for an action.
1006 * See also gtk_widget_get_action().
1008 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1009 * and must not be modified.
1014 gtk_action_get_proxies (GtkAction *action)
1016 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1018 return action->private_data->proxies;
1023 * gtk_widget_get_action:
1024 * @widget: a #GtkWidget
1026 * Returns the #GtkAction that @widget is a proxy for.
1027 * See also gtk_action_get_proxies().
1029 * Returns: the action that a widget is a proxy for, or
1030 * %NULL, if it is not attached to an action.
1035 gtk_widget_get_action (GtkWidget *widget)
1037 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1039 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1044 * gtk_action_get_name:
1045 * @action: the action object
1047 * Returns the name of the action.
1049 * Return value: the name of the action. The string belongs to GTK+ and should not
1054 G_CONST_RETURN gchar *
1055 gtk_action_get_name (GtkAction *action)
1057 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1059 return action->private_data->name;
1063 * gtk_action_is_sensitive:
1064 * @action: the action object
1066 * Returns whether the action is effectively sensitive.
1068 * Return value: %TRUE if the action and its associated action group
1069 * are both sensitive.
1074 gtk_action_is_sensitive (GtkAction *action)
1076 GtkActionPrivate *priv;
1077 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1079 priv = action->private_data;
1080 return priv->sensitive &&
1081 (priv->action_group == NULL ||
1082 gtk_action_group_get_sensitive (priv->action_group));
1086 * gtk_action_get_sensitive:
1087 * @action: the action object
1089 * Returns whether the action itself is sensitive. Note that this doesn't
1090 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1093 * Return value: %TRUE if the action itself is sensitive.
1098 gtk_action_get_sensitive (GtkAction *action)
1100 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1102 return action->private_data->sensitive;
1106 _gtk_action_sync_sensitive (GtkAction *action)
1112 sensitive = gtk_action_is_sensitive (action);
1114 for (p = action->private_data->proxies; p; p = p->next)
1116 proxy = (GtkWidget *)p->data;
1117 gtk_widget_set_sensitive (proxy, sensitive);
1122 * gtk_action_set_sensitive:
1123 * @action: the action object
1124 * @sensitive: %TRUE to make the action sensitive
1126 * Sets the ::sensitive property of the action to @sensitive. Note that
1127 * this doesn't necessarily mean effective sensitivity. See
1128 * gtk_action_is_sensitive()
1134 gtk_action_set_sensitive (GtkAction *action,
1137 g_return_if_fail (GTK_IS_ACTION (action));
1139 sensitive = sensitive != FALSE;
1141 if (action->private_data->sensitive != sensitive)
1143 action->private_data->sensitive = sensitive;
1145 _gtk_action_sync_sensitive (action);
1147 g_object_notify (G_OBJECT (action), "sensitive");
1152 * gtk_action_is_visible:
1153 * @action: the action object
1155 * Returns whether the action is effectively visible.
1157 * Return value: %TRUE if the action and its associated action group
1163 gtk_action_is_visible (GtkAction *action)
1165 GtkActionPrivate *priv;
1166 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1168 priv = action->private_data;
1169 return priv->visible &&
1170 (priv->action_group == NULL ||
1171 gtk_action_group_get_visible (priv->action_group));
1175 * gtk_action_get_visible:
1176 * @action: the action object
1178 * Returns whether the action itself is visible. Note that this doesn't
1179 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1182 * Return value: %TRUE if the action itself is visible.
1187 gtk_action_get_visible (GtkAction *action)
1189 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1191 return action->private_data->visible;
1195 _gtk_action_sync_visible (GtkAction *action)
1202 visible = gtk_action_is_visible (action);
1204 for (p = action->private_data->proxies; p; p = p->next)
1206 proxy = (GtkWidget *)p->data;
1208 if (GTK_IS_MENU_ITEM (proxy))
1210 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1212 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1217 gtk_widget_show (proxy);
1219 gtk_widget_hide (proxy);
1225 * gtk_action_set_visible:
1226 * @action: the action object
1227 * @visible: %TRUE to make the action visible
1229 * Sets the ::visible property of the action to @visible. Note that
1230 * this doesn't necessarily mean effective visibility. See
1231 * gtk_action_is_visible()
1237 gtk_action_set_visible (GtkAction *action,
1240 g_return_if_fail (GTK_IS_ACTION (action));
1242 visible = visible != FALSE;
1244 if (action->private_data->visible != visible)
1246 action->private_data->visible = visible;
1248 _gtk_action_sync_visible (action);
1250 g_object_notify (G_OBJECT (action), "visible");
1255 gtk_action_set_is_important (GtkAction *action,
1256 gboolean is_important)
1261 is_important = is_important != FALSE;
1263 if (action->private_data->is_important != is_important)
1265 action->private_data->is_important = is_important;
1267 for (p = action->private_data->proxies; p; p = p->next)
1269 proxy = (GtkWidget *)p->data;
1271 if (GTK_IS_TOOL_ITEM (proxy))
1272 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1276 g_object_notify (G_OBJECT (action), "is-important");
1281 gtk_action_set_label (GtkAction *action,
1285 GtkWidget *proxy, *child;
1288 tmp = action->private_data->label;
1289 action->private_data->label = g_strdup (label);
1291 action->private_data->label_set = (action->private_data->label != NULL);
1292 /* if label is unset, then use the label from the stock item */
1293 if (!action->private_data->label_set && action->private_data->stock_id)
1295 GtkStockItem stock_item;
1297 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1298 action->private_data->label = g_strdup (stock_item.label);
1301 for (p = action->private_data->proxies; p; p = p->next)
1303 proxy = (GtkWidget *)p->data;
1305 if (GTK_IS_MENU_ITEM (proxy))
1307 child = GTK_BIN (proxy)->child;
1309 if (GTK_IS_LABEL (child))
1310 gtk_label_set_label (GTK_LABEL (child),
1311 action->private_data->label);
1315 g_object_notify (G_OBJECT (action), "label");
1317 /* if short_label is unset, set short_label=label */
1318 if (!action->private_data->short_label_set)
1320 gtk_action_set_short_label (action, action->private_data->label);
1321 action->private_data->short_label_set = FALSE;
1326 gtk_action_set_short_label (GtkAction *action,
1330 GtkWidget *proxy, *child;
1333 tmp = action->private_data->short_label;
1334 action->private_data->short_label = g_strdup (label);
1336 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1337 /* if short_label is unset, then use the value of label */
1338 if (!action->private_data->short_label_set)
1339 action->private_data->short_label = g_strdup (action->private_data->label);
1341 for (p = action->private_data->proxies; p; p = p->next)
1343 proxy = (GtkWidget *)p->data;
1345 if (GTK_IS_TOOL_BUTTON (proxy))
1346 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1347 action->private_data->label);
1348 else if (GTK_IS_BUTTON (proxy) &&
1349 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1351 child = GTK_BIN (proxy)->child;
1353 if (child == NULL || GTK_IS_LABEL (child))
1354 gtk_button_set_label (GTK_BUTTON (proxy),
1355 action->private_data->label);
1359 g_object_notify (G_OBJECT (action), "short-label");
1363 gtk_action_set_visible_horizontal (GtkAction *action,
1364 gboolean visible_horizontal)
1369 visible_horizontal = visible_horizontal != FALSE;
1371 if (action->private_data->visible_horizontal != visible_horizontal)
1373 action->private_data->visible_horizontal = visible_horizontal;
1375 for (p = action->private_data->proxies; p; p = p->next)
1377 proxy = (GtkWidget *)p->data;
1379 if (GTK_IS_TOOL_ITEM (proxy))
1380 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1381 visible_horizontal);
1384 g_object_notify (G_OBJECT (action), "visible-horizontal");
1389 gtk_action_set_visible_vertical (GtkAction *action,
1390 gboolean visible_vertical)
1395 visible_vertical = visible_vertical != FALSE;
1397 if (action->private_data->visible_vertical != visible_vertical)
1399 action->private_data->visible_vertical = visible_vertical;
1401 for (p = action->private_data->proxies; p; p = p->next)
1403 proxy = (GtkWidget *)p->data;
1405 if (GTK_IS_TOOL_ITEM (proxy))
1406 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1410 g_object_notify (G_OBJECT (action), "visible-vertical");
1415 gtk_action_sync_tooltip (GtkAction *action,
1420 parent = gtk_widget_get_parent (proxy);
1422 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1423 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1424 GTK_TOOLBAR (parent)->tooltips,
1425 action->private_data->tooltip,
1430 gtk_action_set_tooltip (GtkAction *action,
1431 const gchar *tooltip)
1437 tmp = action->private_data->tooltip;
1438 action->private_data->tooltip = g_strdup (tooltip);
1441 for (p = action->private_data->proxies; p; p = p->next)
1443 proxy = (GtkWidget *)p->data;
1445 gtk_action_sync_tooltip (action, proxy);
1448 g_object_notify (G_OBJECT (action), "tooltip");
1452 gtk_action_set_stock_id (GtkAction *action,
1453 const gchar *stock_id)
1456 GtkWidget *proxy, *image;
1459 tmp = action->private_data->stock_id;
1460 action->private_data->stock_id = g_strdup (stock_id);
1463 for (p = action->private_data->proxies; p; p = p->next)
1465 proxy = (GtkWidget *)p->data;
1467 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1469 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1471 if (GTK_IS_IMAGE (image))
1472 gtk_image_set_from_stock (GTK_IMAGE (image),
1473 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1475 else if (GTK_IS_TOOL_BUTTON (proxy))
1477 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1478 action->private_data->stock_id);
1480 else if (GTK_IS_BUTTON (proxy) &&
1481 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1483 gtk_button_set_label (GTK_BUTTON (proxy),
1484 action->private_data->stock_id);
1488 g_object_notify (G_OBJECT (action), "stock-id");
1490 /* update label and short_label if appropriate */
1491 if (!action->private_data->label_set)
1493 GtkStockItem stock_item;
1495 if (action->private_data->stock_id &&
1496 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1497 gtk_action_set_label (action, stock_item.label);
1499 gtk_action_set_label (action, NULL);
1501 action->private_data->label_set = FALSE;
1506 gtk_action_set_icon_name (GtkAction *action,
1507 const gchar *icon_name)
1510 GtkWidget *proxy, *image;
1513 tmp = action->private_data->icon_name;
1514 action->private_data->icon_name = g_strdup (icon_name);
1517 for (p = action->private_data->proxies; p; p = p->next)
1519 proxy = (GtkWidget *)p->data;
1521 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1523 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1525 if (GTK_IS_IMAGE (image) &&
1526 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1527 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1528 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1529 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1531 else if (GTK_IS_TOOL_BUTTON (proxy))
1533 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1534 action->private_data->icon_name);
1536 else if (GTK_IS_BUTTON (proxy) &&
1537 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1539 image = gtk_button_get_image (GTK_BUTTON (proxy));
1541 if (GTK_IS_IMAGE (image) &&
1542 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1543 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1544 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1545 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1549 g_object_notify (G_OBJECT (action), "icon-name");
1554 * gtk_action_block_activate_from:
1555 * @action: the action object
1556 * @proxy: a proxy widget
1558 * Disables calls to the gtk_action_activate()
1559 * function by signals on the given proxy widget. This is used to
1560 * break notification loops for things like check or radio actions.
1562 * This function is intended for use by action implementations.
1567 gtk_action_block_activate_from (GtkAction *action,
1570 g_return_if_fail (GTK_IS_ACTION (action));
1572 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1577 * gtk_action_unblock_activate_from:
1578 * @action: the action object
1579 * @proxy: a proxy widget
1581 * Re-enables calls to the gtk_action_activate()
1582 * function by signals on the given proxy widget. This undoes the
1583 * blocking done by gtk_action_block_activate_from().
1585 * This function is intended for use by action implementations.
1590 gtk_action_unblock_activate_from (GtkAction *action,
1593 g_return_if_fail (GTK_IS_ACTION (action));
1595 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1600 closure_accel_activate (GClosure *closure,
1601 GValue *return_value,
1602 guint n_param_values,
1603 const GValue *param_values,
1604 gpointer invocation_hint,
1605 gpointer marshal_data)
1607 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1609 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1611 /* we handled the accelerator */
1612 g_value_set_boolean (return_value, TRUE);
1617 gtk_action_set_action_group (GtkAction *action,
1618 GtkActionGroup *action_group)
1620 g_return_if_fail (GTK_IS_ACTION (action));
1622 if (action->private_data->action_group == NULL)
1623 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1625 g_return_if_fail (action_group == NULL);
1627 action->private_data->action_group = action_group;
1631 * gtk_action_set_accel_path:
1632 * @action: the action object
1633 * @accel_path: the accelerator path
1635 * Sets the accel path for this action. All proxy widgets associated
1636 * with the action will have this accel path, so that their
1637 * accelerators are consistent.
1642 gtk_action_set_accel_path (GtkAction *action,
1643 const gchar *accel_path)
1645 g_return_if_fail (GTK_IS_ACTION (action));
1647 action->private_data->accel_quark = g_quark_from_string (accel_path);
1651 * gtk_action_get_accel_path:
1652 * @action: the action object
1654 * Returns the accel path for this action.
1658 * Returns: the accel path for this action, or %NULL
1659 * if none is set. The returned string is owned by GTK+
1660 * and must not be freed or modified.
1662 G_CONST_RETURN gchar *
1663 gtk_action_get_accel_path (GtkAction *action)
1665 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1667 if (action->private_data->accel_quark)
1668 return g_quark_to_string (action->private_data->accel_quark);
1674 * gtk_action_get_accel_closure:
1675 * @action: the action object
1677 * Returns the accel closure for this action.
1681 * Returns: the accel closure for this action. The returned closure is
1682 * owned by GTK+ and must not be unreffed or modified.
1685 gtk_action_get_accel_closure (GtkAction *action)
1687 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1689 return action->private_data->accel_closure;
1694 * gtk_action_set_accel_group:
1695 * @action: the action object
1696 * @accel_group: a #GtkAccelGroup or %NULL
1698 * Sets the #GtkAccelGroup in which the accelerator for this action
1699 * will be installed.
1704 gtk_action_set_accel_group (GtkAction *action,
1705 GtkAccelGroup *accel_group)
1707 g_return_if_fail (GTK_IS_ACTION (action));
1708 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1711 g_object_ref (accel_group);
1712 if (action->private_data->accel_group)
1713 g_object_unref (action->private_data->accel_group);
1715 action->private_data->accel_group = accel_group;
1719 * gtk_action_connect_accelerator:
1720 * @action: a #GtkAction
1722 * Installs the accelerator for @action if @action has an
1723 * accel path and group. See gtk_action_set_accel_path() and
1724 * gtk_action_set_accel_group()
1726 * Since multiple proxies may independently trigger the installation
1727 * of the accelerator, the @action counts the number of times this
1728 * function has been called and doesn't remove the accelerator until
1729 * gtk_action_disconnect_accelerator() has been called as many times.
1734 gtk_action_connect_accelerator (GtkAction *action)
1736 g_return_if_fail (GTK_IS_ACTION (action));
1738 if (!action->private_data->accel_quark ||
1739 !action->private_data->accel_group)
1742 if (action->private_data->accel_count == 0)
1744 const gchar *accel_path =
1745 g_quark_to_string (action->private_data->accel_quark);
1747 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1749 action->private_data->accel_closure);
1752 action->private_data->accel_count++;
1756 * gtk_action_disconnect_accelerator:
1757 * @action: a #GtkAction
1759 * Undoes the effect of one call to gtk_action_connect_accelerator().
1764 gtk_action_disconnect_accelerator (GtkAction *action)
1766 g_return_if_fail (GTK_IS_ACTION (action));
1768 if (!action->private_data->accel_quark ||
1769 !action->private_data->accel_group)
1772 action->private_data->accel_count--;
1774 if (action->private_data->accel_count == 0)
1775 gtk_accel_group_disconnect (action->private_data->accel_group,
1776 action->private_data->accel_closure);
1780 * gtk_action_create_menu:
1781 * @action: a #GtkAction
1783 * If @action provides a #GtkMenu widget as a submenu for the menu
1784 * item or the toolbar item it creates, this function returns an
1785 * instance of that menu.
1787 * Return value: the menu item provided by the action, or %NULL.
1792 gtk_action_create_menu (GtkAction *action)
1794 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1796 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1797 return GTK_ACTION_GET_CLASS (action)->create_menu (action);
1802 #define __GTK_ACTION_C__
1803 #include "gtkaliasdef.c"