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"
49 #include "gtkbuildable.h"
53 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
55 struct _GtkActionPrivate
61 gchar *stock_id; /* stock icon */
62 gchar *icon_name; /* themed icon */
66 guint label_set : 1; /* these two used so we can set label */
67 guint short_label_set : 1; /* based on stock id */
68 guint visible_horizontal : 1;
69 guint visible_vertical : 1;
70 guint is_important : 1;
71 guint hide_if_empty : 1;
72 guint visible_overflown : 1;
76 GtkAccelGroup *accel_group;
77 GClosure *accel_closure;
80 GtkActionGroup *action_group;
82 /* list of proxy widgets */
101 PROP_VISIBLE_HORIZONTAL,
102 PROP_VISIBLE_VERTICAL,
103 PROP_VISIBLE_OVERFLOWN,
112 static void gtk_action_buildable_init (GtkBuildableIface *iface);
113 static void gtk_action_buildable_set_name (GtkBuildable *buildable,
115 static const gchar* gtk_action_buildable_get_name (GtkBuildable *buildable);
117 G_DEFINE_TYPE_WITH_CODE (GtkAction, gtk_action, G_TYPE_OBJECT,
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
119 gtk_action_buildable_init))
122 static GQuark accel_path_id = 0;
123 static GQuark quark_gtk_action_proxy = 0;
124 static const gchar accel_path_key[] = "GtkAction::accel_path";
125 static const gchar gtk_action_proxy_key[] = "gtk-action";
127 static void gtk_action_finalize (GObject *object);
128 static void gtk_action_set_property (GObject *object,
132 static void gtk_action_get_property (GObject *object,
136 static void gtk_action_set_action_group (GtkAction *action,
137 GtkActionGroup *action_group);
138 static void gtk_action_set_is_important (GtkAction *action,
139 gboolean is_important);
140 static void gtk_action_set_label (GtkAction *action,
142 static void gtk_action_set_short_label (GtkAction *action,
144 static void gtk_action_set_visible_horizontal (GtkAction *action,
145 gboolean visible_horizontal);
146 static void gtk_action_set_visible_vertical (GtkAction *action,
147 gboolean visible_vertical);
148 static void gtk_action_set_tooltip (GtkAction *action,
149 const gchar *tooltip);
150 static void gtk_action_set_stock_id (GtkAction *action,
151 const gchar *stock_id);
152 static void gtk_action_set_icon_name (GtkAction *action,
153 const gchar *icon_name);
154 static void gtk_action_sync_tooltip (GtkAction *action,
157 static GtkWidget *create_menu_item (GtkAction *action);
158 static GtkWidget *create_tool_item (GtkAction *action);
159 static void connect_proxy (GtkAction *action,
161 static void disconnect_proxy (GtkAction *action,
164 static void closure_accel_activate (GClosure *closure,
165 GValue *return_value,
166 guint n_param_values,
167 const GValue *param_values,
168 gpointer invocation_hint,
169 gpointer marshal_data);
171 static guint action_signals[LAST_SIGNAL] = { 0 };
175 gtk_action_class_init (GtkActionClass *klass)
177 GObjectClass *gobject_class;
179 accel_path_id = g_quark_from_static_string (accel_path_key);
180 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
182 gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->finalize = gtk_action_finalize;
185 gobject_class->set_property = gtk_action_set_property;
186 gobject_class->get_property = gtk_action_get_property;
188 klass->activate = NULL;
190 klass->create_menu_item = create_menu_item;
191 klass->create_tool_item = create_tool_item;
192 klass->create_menu = NULL;
193 klass->connect_proxy = connect_proxy;
194 klass->disconnect_proxy = disconnect_proxy;
196 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
197 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("name",
203 P_("A unique name for the action."),
205 GTK_PARAM_READWRITE |
206 G_PARAM_CONSTRUCT_ONLY));
211 * The label used for menu items and buttons that activate
212 * this action. If the label is %NULL, GTK+ uses the stock
213 * label specified via the stock-id property.
215 g_object_class_install_property (gobject_class,
217 g_param_spec_string ("label",
219 P_("The label used for menu items and buttons "
220 "that activate this action."),
222 GTK_PARAM_READWRITE));
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("short-label",
227 P_("A shorter label that may be used on toolbar buttons."),
229 GTK_PARAM_READWRITE));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_string ("tooltip",
234 P_("A tooltip for this action."),
236 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_string ("stock-id",
241 P_("The stock icon displayed in widgets representing "
244 GTK_PARAM_READWRITE));
246 * GtkAction:icon-name:
248 * The name of the icon from the icon theme.
249 * Note that the stock icon is preferred, if
250 * the ::stock-id property holds the id of an
251 * existing stock icon.
255 g_object_class_install_property (gobject_class,
257 g_param_spec_string ("icon-name",
259 P_("The name of the icon from the icon theme"),
261 GTK_PARAM_READWRITE));
262 g_object_class_install_property (gobject_class,
263 PROP_VISIBLE_HORIZONTAL,
264 g_param_spec_boolean ("visible-horizontal",
265 P_("Visible when horizontal"),
266 P_("Whether the toolbar item is visible when the toolbar "
267 "is in a horizontal orientation."),
269 GTK_PARAM_READWRITE));
271 * GtkAction:visible-overflown:
273 * When %TRUE, toolitem proxies for this action are represented in the
274 * toolbar overflow menu.
278 g_object_class_install_property (gobject_class,
279 PROP_VISIBLE_OVERFLOWN,
280 g_param_spec_boolean ("visible-overflown",
281 P_("Visible when overflown"),
282 P_("When TRUE, toolitem proxies for this action "
283 "are represented in the toolbar overflow menu."),
285 GTK_PARAM_READWRITE));
286 g_object_class_install_property (gobject_class,
287 PROP_VISIBLE_VERTICAL,
288 g_param_spec_boolean ("visible-vertical",
289 P_("Visible when vertical"),
290 P_("Whether the toolbar item is visible when the toolbar "
291 "is in a vertical orientation."),
293 GTK_PARAM_READWRITE));
294 g_object_class_install_property (gobject_class,
296 g_param_spec_boolean ("is-important",
298 P_("Whether the action is considered important. "
299 "When TRUE, toolitem proxies for this action "
300 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
302 GTK_PARAM_READWRITE));
303 g_object_class_install_property (gobject_class,
305 g_param_spec_boolean ("hide-if-empty",
307 P_("When TRUE, empty menu proxies for this action are hidden."),
309 GTK_PARAM_READWRITE));
310 g_object_class_install_property (gobject_class,
312 g_param_spec_boolean ("sensitive",
314 P_("Whether the action is enabled."),
316 GTK_PARAM_READWRITE));
317 g_object_class_install_property (gobject_class,
319 g_param_spec_boolean ("visible",
321 P_("Whether the action is visible."),
323 GTK_PARAM_READWRITE));
324 g_object_class_install_property (gobject_class,
326 g_param_spec_object ("action-group",
328 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
329 GTK_TYPE_ACTION_GROUP,
330 GTK_PARAM_READWRITE));
333 * GtkAction::activate:
334 * @action: the #GtkAction
336 * The "activate" signal is emitted when the action is activated.
340 action_signals[ACTIVATE] =
341 g_signal_new (I_("activate"),
342 G_OBJECT_CLASS_TYPE (klass),
343 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
344 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
345 g_cclosure_marshal_VOID__VOID,
348 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
353 gtk_action_init (GtkAction *action)
355 action->private_data = GTK_ACTION_GET_PRIVATE (action);
357 action->private_data->name = NULL;
358 action->private_data->label = NULL;
359 action->private_data->short_label = NULL;
360 action->private_data->tooltip = NULL;
361 action->private_data->stock_id = NULL;
362 action->private_data->icon_name = NULL;
363 action->private_data->visible_horizontal = TRUE;
364 action->private_data->visible_vertical = TRUE;
365 action->private_data->visible_overflown = TRUE;
366 action->private_data->is_important = FALSE;
367 action->private_data->hide_if_empty = TRUE;
369 action->private_data->sensitive = TRUE;
370 action->private_data->visible = TRUE;
372 action->private_data->label_set = FALSE;
373 action->private_data->short_label_set = FALSE;
375 action->private_data->accel_count = 0;
376 action->private_data->accel_group = NULL;
377 action->private_data->accel_quark = 0;
378 action->private_data->accel_closure =
379 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
380 g_closure_set_marshal (action->private_data->accel_closure,
381 closure_accel_activate);
382 g_closure_ref (action->private_data->accel_closure);
383 g_closure_sink (action->private_data->accel_closure);
385 action->private_data->action_group = NULL;
387 action->private_data->proxies = NULL;
391 gtk_action_buildable_init (GtkBuildableIface *iface)
393 iface->set_name = gtk_action_buildable_set_name;
394 iface->get_name = gtk_action_buildable_get_name;
398 gtk_action_buildable_set_name (GtkBuildable *buildable,
402 GtkAction *action = GTK_ACTION (buildable);
404 tmp = action->private_data->name;
405 action->private_data->name = g_strdup (name);
410 gtk_action_buildable_get_name (GtkBuildable *buildable)
412 GtkAction *action = GTK_ACTION (buildable);
414 return action->private_data->name;
419 * @name: A unique name for the action
420 * @label: the label displayed in menu items and on buttons
421 * @tooltip: a tooltip for the action
422 * @stock_id: the stock icon to display in widgets representing the action
424 * Creates a new #GtkAction object. To add the action to a
425 * #GtkActionGroup and set the accelerator for the action,
426 * call gtk_action_group_add_action_with_accel().
427 * See <xref linkend="XML-UI"/> for information on allowed action
430 * Return value: a new #GtkAction
435 gtk_action_new (const gchar *name,
437 const gchar *tooltip,
438 const gchar *stock_id)
442 action = g_object_new (GTK_TYPE_ACTION,
446 "stock-id", stock_id,
453 gtk_action_finalize (GObject *object)
456 action = GTK_ACTION (object);
458 g_free (action->private_data->name);
459 g_free (action->private_data->label);
460 g_free (action->private_data->short_label);
461 g_free (action->private_data->tooltip);
462 g_free (action->private_data->stock_id);
463 g_free (action->private_data->icon_name);
465 g_closure_unref (action->private_data->accel_closure);
466 if (action->private_data->accel_group)
467 g_object_unref (action->private_data->accel_group);
469 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
473 gtk_action_set_property (GObject *object,
481 action = GTK_ACTION (object);
486 tmp = action->private_data->name;
487 action->private_data->name = g_value_dup_string (value);
491 gtk_action_set_label (action, g_value_get_string (value));
493 case PROP_SHORT_LABEL:
494 gtk_action_set_short_label (action, g_value_get_string (value));
497 gtk_action_set_tooltip (action, g_value_get_string (value));
500 gtk_action_set_stock_id (action, g_value_get_string (value));
503 gtk_action_set_icon_name (action, g_value_get_string (value));
505 case PROP_VISIBLE_HORIZONTAL:
506 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
508 case PROP_VISIBLE_VERTICAL:
509 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
511 case PROP_VISIBLE_OVERFLOWN:
512 action->private_data->visible_overflown = g_value_get_boolean (value);
514 case PROP_IS_IMPORTANT:
515 gtk_action_set_is_important (action, g_value_get_boolean (value));
517 case PROP_HIDE_IF_EMPTY:
518 action->private_data->hide_if_empty = g_value_get_boolean (value);
521 gtk_action_set_sensitive (action, g_value_get_boolean (value));
524 gtk_action_set_visible (action, g_value_get_boolean (value));
526 case PROP_ACTION_GROUP:
527 gtk_action_set_action_group (action, g_value_get_object (value));
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 gtk_action_get_property (GObject *object,
543 action = GTK_ACTION (object);
548 g_value_set_string (value, action->private_data->name);
551 g_value_set_string (value, action->private_data->label);
553 case PROP_SHORT_LABEL:
554 g_value_set_string (value, action->private_data->short_label);
557 g_value_set_string (value, action->private_data->tooltip);
560 g_value_set_string (value, action->private_data->stock_id);
563 g_value_set_string (value, action->private_data->icon_name);
565 case PROP_VISIBLE_HORIZONTAL:
566 g_value_set_boolean (value, action->private_data->visible_horizontal);
568 case PROP_VISIBLE_VERTICAL:
569 g_value_set_boolean (value, action->private_data->visible_vertical);
571 case PROP_VISIBLE_OVERFLOWN:
572 g_value_set_boolean (value, action->private_data->visible_overflown);
574 case PROP_IS_IMPORTANT:
575 g_value_set_boolean (value, action->private_data->is_important);
577 case PROP_HIDE_IF_EMPTY:
578 g_value_set_boolean (value, action->private_data->hide_if_empty);
581 g_value_set_boolean (value, action->private_data->sensitive);
584 g_value_set_boolean (value, action->private_data->visible);
586 case PROP_ACTION_GROUP:
587 g_value_set_object (value, action->private_data->action_group);
590 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596 create_menu_item (GtkAction *action)
598 GType menu_item_type;
600 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
602 return g_object_new (menu_item_type, NULL);
606 create_tool_item (GtkAction *action)
608 GType toolbar_item_type;
610 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
612 return g_object_new (toolbar_item_type, NULL);
616 remove_proxy (GtkAction *action,
619 if (GTK_IS_MENU_ITEM (proxy))
620 gtk_action_disconnect_accelerator (action);
622 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
626 * _gtk_action_sync_menu_visible:
627 * @action: a #GtkAction, or %NULL to determine the action from @proxy
628 * @proxy: a proxy menu item
629 * @empty: whether the submenu attached to @proxy is empty
631 * Updates the visibility of @proxy from the visibility of @action
632 * according to the following rules:
634 * <listitem><para>if @action is invisible, @proxy is too
636 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
637 * property of @action indicates otherwise
641 * This function is used in the implementation of #GtkUIManager.
644 _gtk_action_sync_menu_visible (GtkAction *action,
648 gboolean visible, hide_if_empty;
650 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
651 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
654 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
656 visible = gtk_action_is_visible (action);
657 hide_if_empty = action->private_data->hide_if_empty;
659 if (visible && !(empty && hide_if_empty))
660 gtk_widget_show (proxy);
662 gtk_widget_hide (proxy);
665 gboolean _gtk_menu_is_empty (GtkWidget *menu);
668 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
671 GtkWidget *menu_item;
673 if (action->private_data->visible_overflown)
675 menu_item = gtk_action_create_menu_item (action);
677 g_object_ref_sink (menu_item);
679 gtk_tool_item_set_proxy_menu_item (tool_item,
680 "gtk-action-menu-item", menu_item);
681 g_object_unref (menu_item);
684 gtk_tool_item_set_proxy_menu_item (tool_item,
685 "gtk-action-menu-item", NULL);
691 connect_proxy (GtkAction *action,
694 g_object_ref (action);
695 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
698 /* add this widget to the list of proxies */
699 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
700 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
702 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
703 if (gtk_action_is_visible (action))
704 gtk_widget_show (proxy);
706 gtk_widget_hide (proxy);
707 gtk_widget_set_no_show_all (proxy, TRUE);
709 if (GTK_IS_MENU_ITEM (proxy))
712 /* menu item specific synchronisers ... */
714 if (action->private_data->accel_quark)
716 gtk_action_connect_accelerator (action);
717 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
718 g_quark_to_string (action->private_data->accel_quark));
721 label = GTK_BIN (proxy)->child;
723 /* make sure label is a label */
724 if (label && !GTK_IS_LABEL (label))
726 gtk_container_remove (GTK_CONTAINER (proxy), label);
731 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
732 "use-underline", TRUE,
738 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
740 "accel-closure", action->private_data->accel_closure,
743 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
745 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
749 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
750 if (image && !GTK_IS_IMAGE (image))
752 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
757 image = gtk_image_new ();
758 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
760 gtk_widget_show (image);
763 if (action->private_data->stock_id &&
764 gtk_icon_factory_lookup_default (action->private_data->stock_id))
765 gtk_image_set_from_stock (GTK_IMAGE (image),
766 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
767 else if (action->private_data->icon_name)
768 gtk_image_set_from_icon_name (GTK_IMAGE (image),
769 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
772 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
773 g_signal_connect_object (proxy, "activate",
774 G_CALLBACK (gtk_action_activate), action,
778 else if (GTK_IS_TOOL_ITEM (proxy))
780 /* toolbar button specific synchronisers ... */
781 if (GTK_IS_TOOL_BUTTON (proxy))
784 "visible-horizontal", action->private_data->visible_horizontal,
785 "visible-vertical", action->private_data->visible_vertical,
786 "is-important", action->private_data->is_important,
787 "label", action->private_data->short_label,
788 "use-underline", TRUE,
789 "stock-id", action->private_data->stock_id,
790 "icon-name", action->private_data->icon_name,
793 g_signal_connect_object (proxy, "clicked",
794 G_CALLBACK (gtk_action_activate), action,
800 "visible-horizontal", action->private_data->visible_horizontal,
801 "visible-vertical", action->private_data->visible_vertical,
802 "is-important", action->private_data->is_important,
806 gtk_action_sync_tooltip (action, proxy);
808 g_signal_connect_object (proxy, "create_menu_proxy",
809 G_CALLBACK (gtk_action_create_menu_proxy),
812 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
814 else if (GTK_IS_BUTTON (proxy))
816 /* button specific synchronisers ... */
817 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
819 /* synchronise stock-id */
821 "label", action->private_data->stock_id,
826 if (GTK_BIN (proxy)->child == NULL ||
827 GTK_IS_LABEL (GTK_BIN (proxy)->child))
829 /* synchronise the label */
831 "label", action->private_data->short_label,
832 "use-underline", TRUE,
836 /* we leave the button alone if there is a custom child */
837 g_signal_connect_object (proxy, "clicked",
838 G_CALLBACK (gtk_action_activate), action,
842 if (action->private_data->action_group)
843 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
847 disconnect_proxy (GtkAction *action,
850 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
852 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
853 remove_proxy (action, proxy);
855 /* disconnect the activate handler */
856 g_signal_handlers_disconnect_by_func (proxy,
857 G_CALLBACK (gtk_action_activate),
860 /* toolbar button specific synchronisers ... */
861 g_signal_handlers_disconnect_by_func (proxy,
862 G_CALLBACK (gtk_action_create_menu_proxy),
865 if (action->private_data->action_group)
866 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
870 _gtk_action_emit_activate (GtkAction *action)
872 GtkActionGroup *group = action->private_data->action_group;
876 g_object_ref (group);
877 _gtk_action_group_emit_pre_activate (group, action);
880 g_signal_emit (action, action_signals[ACTIVATE], 0);
884 _gtk_action_group_emit_post_activate (group, action);
885 g_object_unref (group);
890 * gtk_action_activate:
891 * @action: the action object
893 * Emits the "activate" signal on the specified action, if it isn't
894 * insensitive. This gets called by the proxy widgets when they get
897 * It can also be used to manually activate an action.
902 gtk_action_activate (GtkAction *action)
904 g_return_if_fail (GTK_IS_ACTION (action));
906 if (gtk_action_is_sensitive (action))
907 _gtk_action_emit_activate (action);
911 * gtk_action_create_icon:
912 * @action: the action object
913 * @icon_size: the size of the icon that should be created.
915 * This function is intended for use by action implementations to
916 * create icons displayed in the proxy widgets.
918 * Returns: a widget that displays the icon for this action.
923 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
925 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
927 if (action->private_data->stock_id)
928 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
929 else if (action->private_data->icon_name)
930 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
936 * gtk_action_create_menu_item:
937 * @action: the action object
939 * Creates a menu item widget that proxies for the given action.
941 * Returns: a menu item connected to the action.
946 gtk_action_create_menu_item (GtkAction *action)
948 GtkWidget *menu_item;
950 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
952 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
954 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
960 * gtk_action_create_tool_item:
961 * @action: the action object
963 * Creates a toolbar item widget that proxies for the given action.
965 * Returns: a toolbar item connected to the action.
970 gtk_action_create_tool_item (GtkAction *action)
974 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
976 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
978 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
984 * gtk_action_connect_proxy:
985 * @action: the action object
986 * @proxy: the proxy widget
988 * Connects a widget to an action object as a proxy. Synchronises
989 * various properties of the action with the widget (such as label
990 * text, icon, tooltip, etc), and attaches a callback so that the
991 * action gets activated when the proxy widget does.
993 * If the widget is already connected to an action, it is disconnected
999 gtk_action_connect_proxy (GtkAction *action,
1002 GtkAction *prev_action;
1004 g_return_if_fail (GTK_IS_ACTION (action));
1005 g_return_if_fail (GTK_IS_WIDGET (proxy));
1007 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
1010 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1012 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1016 * gtk_action_disconnect_proxy:
1017 * @action: the action object
1018 * @proxy: the proxy widget
1020 * Disconnects a proxy widget from an action.
1021 * Does <emphasis>not</emphasis> destroy the widget, however.
1026 gtk_action_disconnect_proxy (GtkAction *action,
1029 g_return_if_fail (GTK_IS_ACTION (action));
1030 g_return_if_fail (GTK_IS_WIDGET (proxy));
1032 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
1034 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1038 * gtk_action_get_proxies:
1039 * @action: the action object
1041 * Returns the proxy widgets for an action.
1042 * See also gtk_widget_get_action().
1044 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1045 * and must not be modified.
1050 gtk_action_get_proxies (GtkAction *action)
1052 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1054 return action->private_data->proxies;
1059 * gtk_widget_get_action:
1060 * @widget: a #GtkWidget
1062 * Returns the #GtkAction that @widget is a proxy for.
1063 * See also gtk_action_get_proxies().
1065 * Returns: the action that a widget is a proxy for, or
1066 * %NULL, if it is not attached to an action.
1071 gtk_widget_get_action (GtkWidget *widget)
1073 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1075 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1080 * gtk_action_get_name:
1081 * @action: the action object
1083 * Returns the name of the action.
1085 * Return value: the name of the action. The string belongs to GTK+ and should not
1090 G_CONST_RETURN gchar *
1091 gtk_action_get_name (GtkAction *action)
1093 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1095 return action->private_data->name;
1099 * gtk_action_is_sensitive:
1100 * @action: the action object
1102 * Returns whether the action is effectively sensitive.
1104 * Return value: %TRUE if the action and its associated action group
1105 * are both sensitive.
1110 gtk_action_is_sensitive (GtkAction *action)
1112 GtkActionPrivate *priv;
1113 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1115 priv = action->private_data;
1116 return priv->sensitive &&
1117 (priv->action_group == NULL ||
1118 gtk_action_group_get_sensitive (priv->action_group));
1122 * gtk_action_get_sensitive:
1123 * @action: the action object
1125 * Returns whether the action itself is sensitive. Note that this doesn't
1126 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1129 * Return value: %TRUE if the action itself is sensitive.
1134 gtk_action_get_sensitive (GtkAction *action)
1136 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1138 return action->private_data->sensitive;
1142 _gtk_action_sync_sensitive (GtkAction *action)
1148 sensitive = gtk_action_is_sensitive (action);
1150 for (p = action->private_data->proxies; p; p = p->next)
1152 proxy = (GtkWidget *)p->data;
1153 gtk_widget_set_sensitive (proxy, sensitive);
1158 * gtk_action_set_sensitive:
1159 * @action: the action object
1160 * @sensitive: %TRUE to make the action sensitive
1162 * Sets the ::sensitive property of the action to @sensitive. Note that
1163 * this doesn't necessarily mean effective sensitivity. See
1164 * gtk_action_is_sensitive()
1170 gtk_action_set_sensitive (GtkAction *action,
1173 g_return_if_fail (GTK_IS_ACTION (action));
1175 sensitive = sensitive != FALSE;
1177 if (action->private_data->sensitive != sensitive)
1179 action->private_data->sensitive = sensitive;
1181 _gtk_action_sync_sensitive (action);
1183 g_object_notify (G_OBJECT (action), "sensitive");
1188 * gtk_action_is_visible:
1189 * @action: the action object
1191 * Returns whether the action is effectively visible.
1193 * Return value: %TRUE if the action and its associated action group
1199 gtk_action_is_visible (GtkAction *action)
1201 GtkActionPrivate *priv;
1202 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1204 priv = action->private_data;
1205 return priv->visible &&
1206 (priv->action_group == NULL ||
1207 gtk_action_group_get_visible (priv->action_group));
1211 * gtk_action_get_visible:
1212 * @action: the action object
1214 * Returns whether the action itself is visible. Note that this doesn't
1215 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1218 * Return value: %TRUE if the action itself is visible.
1223 gtk_action_get_visible (GtkAction *action)
1225 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1227 return action->private_data->visible;
1231 _gtk_action_sync_visible (GtkAction *action)
1238 visible = gtk_action_is_visible (action);
1240 for (p = action->private_data->proxies; p; p = p->next)
1242 proxy = (GtkWidget *)p->data;
1244 if (GTK_IS_MENU_ITEM (proxy))
1246 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1248 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1253 gtk_widget_show (proxy);
1255 gtk_widget_hide (proxy);
1261 * gtk_action_set_visible:
1262 * @action: the action object
1263 * @visible: %TRUE to make the action visible
1265 * Sets the ::visible property of the action to @visible. Note that
1266 * this doesn't necessarily mean effective visibility. See
1267 * gtk_action_is_visible()
1273 gtk_action_set_visible (GtkAction *action,
1276 g_return_if_fail (GTK_IS_ACTION (action));
1278 visible = visible != FALSE;
1280 if (action->private_data->visible != visible)
1282 action->private_data->visible = visible;
1284 _gtk_action_sync_visible (action);
1286 g_object_notify (G_OBJECT (action), "visible");
1291 gtk_action_set_is_important (GtkAction *action,
1292 gboolean is_important)
1297 is_important = is_important != FALSE;
1299 if (action->private_data->is_important != is_important)
1301 action->private_data->is_important = is_important;
1303 for (p = action->private_data->proxies; p; p = p->next)
1305 proxy = (GtkWidget *)p->data;
1307 if (GTK_IS_TOOL_ITEM (proxy))
1308 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1312 g_object_notify (G_OBJECT (action), "is-important");
1317 gtk_action_set_label (GtkAction *action,
1321 GtkWidget *proxy, *child;
1324 tmp = action->private_data->label;
1325 action->private_data->label = g_strdup (label);
1327 action->private_data->label_set = (action->private_data->label != NULL);
1328 /* if label is unset, then use the label from the stock item */
1329 if (!action->private_data->label_set && action->private_data->stock_id)
1331 GtkStockItem stock_item;
1333 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1334 action->private_data->label = g_strdup (stock_item.label);
1337 for (p = action->private_data->proxies; p; p = p->next)
1339 proxy = (GtkWidget *)p->data;
1341 if (GTK_IS_MENU_ITEM (proxy))
1343 child = GTK_BIN (proxy)->child;
1345 if (GTK_IS_LABEL (child))
1346 gtk_label_set_label (GTK_LABEL (child),
1347 action->private_data->label);
1351 g_object_notify (G_OBJECT (action), "label");
1353 /* if short_label is unset, set short_label=label */
1354 if (!action->private_data->short_label_set)
1356 gtk_action_set_short_label (action, action->private_data->label);
1357 action->private_data->short_label_set = FALSE;
1362 gtk_action_set_short_label (GtkAction *action,
1366 GtkWidget *proxy, *child;
1369 tmp = action->private_data->short_label;
1370 action->private_data->short_label = g_strdup (label);
1372 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1373 /* if short_label is unset, then use the value of label */
1374 if (!action->private_data->short_label_set)
1375 action->private_data->short_label = g_strdup (action->private_data->label);
1377 for (p = action->private_data->proxies; p; p = p->next)
1379 proxy = (GtkWidget *)p->data;
1381 if (GTK_IS_TOOL_BUTTON (proxy))
1382 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1383 action->private_data->short_label);
1384 else if (GTK_IS_BUTTON (proxy) &&
1385 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1387 child = GTK_BIN (proxy)->child;
1389 if (child == NULL || GTK_IS_LABEL (child))
1390 gtk_button_set_label (GTK_BUTTON (proxy),
1391 action->private_data->short_label);
1395 g_object_notify (G_OBJECT (action), "short-label");
1399 gtk_action_set_visible_horizontal (GtkAction *action,
1400 gboolean visible_horizontal)
1405 visible_horizontal = visible_horizontal != FALSE;
1407 if (action->private_data->visible_horizontal != visible_horizontal)
1409 action->private_data->visible_horizontal = visible_horizontal;
1411 for (p = action->private_data->proxies; p; p = p->next)
1413 proxy = (GtkWidget *)p->data;
1415 if (GTK_IS_TOOL_ITEM (proxy))
1416 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1417 visible_horizontal);
1420 g_object_notify (G_OBJECT (action), "visible-horizontal");
1425 gtk_action_set_visible_vertical (GtkAction *action,
1426 gboolean visible_vertical)
1431 visible_vertical = visible_vertical != FALSE;
1433 if (action->private_data->visible_vertical != visible_vertical)
1435 action->private_data->visible_vertical = visible_vertical;
1437 for (p = action->private_data->proxies; p; p = p->next)
1439 proxy = (GtkWidget *)p->data;
1441 if (GTK_IS_TOOL_ITEM (proxy))
1442 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1446 g_object_notify (G_OBJECT (action), "visible-vertical");
1451 gtk_action_sync_tooltip (GtkAction *action,
1454 gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (proxy),
1455 action->private_data->tooltip);
1459 gtk_action_set_tooltip (GtkAction *action,
1460 const gchar *tooltip)
1466 tmp = action->private_data->tooltip;
1467 action->private_data->tooltip = g_strdup (tooltip);
1470 for (p = action->private_data->proxies; p; p = p->next)
1472 proxy = (GtkWidget *)p->data;
1474 if (GTK_IS_TOOL_ITEM (proxy))
1475 gtk_action_sync_tooltip (action, proxy);
1478 g_object_notify (G_OBJECT (action), "tooltip");
1482 gtk_action_set_stock_id (GtkAction *action,
1483 const gchar *stock_id)
1486 GtkWidget *proxy, *image;
1489 tmp = action->private_data->stock_id;
1490 action->private_data->stock_id = g_strdup (stock_id);
1493 for (p = action->private_data->proxies; p; p = p->next)
1495 proxy = (GtkWidget *)p->data;
1497 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1499 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1501 if (GTK_IS_IMAGE (image))
1502 gtk_image_set_from_stock (GTK_IMAGE (image),
1503 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1505 else if (GTK_IS_TOOL_BUTTON (proxy))
1507 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1508 action->private_data->stock_id);
1510 else if (GTK_IS_BUTTON (proxy) &&
1511 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1513 gtk_button_set_label (GTK_BUTTON (proxy),
1514 action->private_data->stock_id);
1518 g_object_notify (G_OBJECT (action), "stock-id");
1520 /* update label and short_label if appropriate */
1521 if (!action->private_data->label_set)
1523 GtkStockItem stock_item;
1525 if (action->private_data->stock_id &&
1526 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1527 gtk_action_set_label (action, stock_item.label);
1529 gtk_action_set_label (action, NULL);
1531 action->private_data->label_set = FALSE;
1536 gtk_action_set_icon_name (GtkAction *action,
1537 const gchar *icon_name)
1540 GtkWidget *proxy, *image;
1543 tmp = action->private_data->icon_name;
1544 action->private_data->icon_name = g_strdup (icon_name);
1547 for (p = action->private_data->proxies; p; p = p->next)
1549 proxy = (GtkWidget *)p->data;
1551 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1553 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1555 if (GTK_IS_IMAGE (image) &&
1556 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1557 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1558 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1559 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1561 else if (GTK_IS_TOOL_BUTTON (proxy))
1563 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1564 action->private_data->icon_name);
1566 else if (GTK_IS_BUTTON (proxy) &&
1567 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1569 image = gtk_button_get_image (GTK_BUTTON (proxy));
1571 if (GTK_IS_IMAGE (image) &&
1572 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1573 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1574 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1575 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1579 g_object_notify (G_OBJECT (action), "icon-name");
1584 * gtk_action_block_activate_from:
1585 * @action: the action object
1586 * @proxy: a proxy widget
1588 * Disables calls to the gtk_action_activate()
1589 * function by signals on the given proxy widget. This is used to
1590 * break notification loops for things like check or radio actions.
1592 * This function is intended for use by action implementations.
1597 gtk_action_block_activate_from (GtkAction *action,
1600 g_return_if_fail (GTK_IS_ACTION (action));
1602 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1607 * gtk_action_unblock_activate_from:
1608 * @action: the action object
1609 * @proxy: a proxy widget
1611 * Re-enables calls to the gtk_action_activate()
1612 * function by signals on the given proxy widget. This undoes the
1613 * blocking done by gtk_action_block_activate_from().
1615 * This function is intended for use by action implementations.
1620 gtk_action_unblock_activate_from (GtkAction *action,
1623 g_return_if_fail (GTK_IS_ACTION (action));
1625 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1630 closure_accel_activate (GClosure *closure,
1631 GValue *return_value,
1632 guint n_param_values,
1633 const GValue *param_values,
1634 gpointer invocation_hint,
1635 gpointer marshal_data)
1637 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1639 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1641 /* we handled the accelerator */
1642 g_value_set_boolean (return_value, TRUE);
1647 gtk_action_set_action_group (GtkAction *action,
1648 GtkActionGroup *action_group)
1650 g_return_if_fail (GTK_IS_ACTION (action));
1652 if (action->private_data->action_group == NULL)
1653 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1655 g_return_if_fail (action_group == NULL);
1657 action->private_data->action_group = action_group;
1661 * gtk_action_set_accel_path:
1662 * @action: the action object
1663 * @accel_path: the accelerator path
1665 * Sets the accel path for this action. All proxy widgets associated
1666 * with the action will have this accel path, so that their
1667 * accelerators are consistent.
1672 gtk_action_set_accel_path (GtkAction *action,
1673 const gchar *accel_path)
1675 g_return_if_fail (GTK_IS_ACTION (action));
1677 action->private_data->accel_quark = g_quark_from_string (accel_path);
1681 * gtk_action_get_accel_path:
1682 * @action: the action object
1684 * Returns the accel path for this action.
1688 * Returns: the accel path for this action, or %NULL
1689 * if none is set. The returned string is owned by GTK+
1690 * and must not be freed or modified.
1692 G_CONST_RETURN gchar *
1693 gtk_action_get_accel_path (GtkAction *action)
1695 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1697 if (action->private_data->accel_quark)
1698 return g_quark_to_string (action->private_data->accel_quark);
1704 * gtk_action_get_accel_closure:
1705 * @action: the action object
1707 * Returns the accel closure for this action.
1711 * Returns: the accel closure for this action. The returned closure is
1712 * owned by GTK+ and must not be unreffed or modified.
1715 gtk_action_get_accel_closure (GtkAction *action)
1717 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1719 return action->private_data->accel_closure;
1724 * gtk_action_set_accel_group:
1725 * @action: the action object
1726 * @accel_group: a #GtkAccelGroup or %NULL
1728 * Sets the #GtkAccelGroup in which the accelerator for this action
1729 * will be installed.
1734 gtk_action_set_accel_group (GtkAction *action,
1735 GtkAccelGroup *accel_group)
1737 g_return_if_fail (GTK_IS_ACTION (action));
1738 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1741 g_object_ref (accel_group);
1742 if (action->private_data->accel_group)
1743 g_object_unref (action->private_data->accel_group);
1745 action->private_data->accel_group = accel_group;
1749 * gtk_action_connect_accelerator:
1750 * @action: a #GtkAction
1752 * Installs the accelerator for @action if @action has an
1753 * accel path and group. See gtk_action_set_accel_path() and
1754 * gtk_action_set_accel_group()
1756 * Since multiple proxies may independently trigger the installation
1757 * of the accelerator, the @action counts the number of times this
1758 * function has been called and doesn't remove the accelerator until
1759 * gtk_action_disconnect_accelerator() has been called as many times.
1764 gtk_action_connect_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 if (action->private_data->accel_count == 0)
1774 const gchar *accel_path =
1775 g_quark_to_string (action->private_data->accel_quark);
1777 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1779 action->private_data->accel_closure);
1782 action->private_data->accel_count++;
1786 * gtk_action_disconnect_accelerator:
1787 * @action: a #GtkAction
1789 * Undoes the effect of one call to gtk_action_connect_accelerator().
1794 gtk_action_disconnect_accelerator (GtkAction *action)
1796 g_return_if_fail (GTK_IS_ACTION (action));
1798 if (!action->private_data->accel_quark ||
1799 !action->private_data->accel_group)
1802 action->private_data->accel_count--;
1804 if (action->private_data->accel_count == 0)
1805 gtk_accel_group_disconnect (action->private_data->accel_group,
1806 action->private_data->accel_closure);
1810 * gtk_action_create_menu:
1811 * @action: a #GtkAction
1813 * If @action provides a #GtkMenu widget as a submenu for the menu
1814 * item or the toolbar item it creates, this function returns an
1815 * instance of that menu.
1817 * Return value: the menu item provided by the action, or %NULL.
1822 gtk_action_create_menu (GtkAction *action)
1824 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1826 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1827 return GTK_ACTION_GET_CLASS (action)->create_menu (action);
1832 #define __GTK_ACTION_C__
1833 #include "gtkaliasdef.c"