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
57 const gchar *name; /* interned */
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 quark_gtk_action_proxy = 0;
123 static const gchar gtk_action_proxy_key[] = "gtk-action";
125 static void gtk_action_finalize (GObject *object);
126 static void gtk_action_set_property (GObject *object,
130 static void gtk_action_get_property (GObject *object,
134 static void gtk_action_set_action_group (GtkAction *action,
135 GtkActionGroup *action_group);
136 static void gtk_action_set_is_important (GtkAction *action,
137 gboolean is_important);
138 static void gtk_action_set_label (GtkAction *action,
140 static void gtk_action_set_short_label (GtkAction *action,
142 static void gtk_action_set_visible_horizontal (GtkAction *action,
143 gboolean visible_horizontal);
144 static void gtk_action_set_visible_vertical (GtkAction *action,
145 gboolean visible_vertical);
146 static void gtk_action_set_tooltip (GtkAction *action,
147 const gchar *tooltip);
148 static void gtk_action_set_stock_id (GtkAction *action,
149 const gchar *stock_id);
150 static void gtk_action_set_icon_name (GtkAction *action,
151 const gchar *icon_name);
152 static void gtk_action_sync_tooltip (GtkAction *action,
155 static GtkWidget *create_menu_item (GtkAction *action);
156 static GtkWidget *create_tool_item (GtkAction *action);
157 static void connect_proxy (GtkAction *action,
159 static void disconnect_proxy (GtkAction *action,
162 static void closure_accel_activate (GClosure *closure,
163 GValue *return_value,
164 guint n_param_values,
165 const GValue *param_values,
166 gpointer invocation_hint,
167 gpointer marshal_data);
169 static guint action_signals[LAST_SIGNAL] = { 0 };
173 gtk_action_class_init (GtkActionClass *klass)
175 GObjectClass *gobject_class;
177 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
179 gobject_class = G_OBJECT_CLASS (klass);
181 gobject_class->finalize = gtk_action_finalize;
182 gobject_class->set_property = gtk_action_set_property;
183 gobject_class->get_property = gtk_action_get_property;
185 klass->activate = NULL;
187 klass->create_menu_item = create_menu_item;
188 klass->create_tool_item = create_tool_item;
189 klass->create_menu = NULL;
190 klass->connect_proxy = connect_proxy;
191 klass->disconnect_proxy = disconnect_proxy;
193 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
194 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
196 g_object_class_install_property (gobject_class,
198 g_param_spec_string ("name",
200 P_("A unique name for the action."),
202 GTK_PARAM_READWRITE |
203 G_PARAM_CONSTRUCT_ONLY));
208 * The label used for menu items and buttons that activate
209 * this action. If the label is %NULL, GTK+ uses the stock
210 * label specified via the stock-id property.
212 g_object_class_install_property (gobject_class,
214 g_param_spec_string ("label",
216 P_("The label used for menu items and buttons "
217 "that activate this action."),
219 GTK_PARAM_READWRITE));
220 g_object_class_install_property (gobject_class,
222 g_param_spec_string ("short-label",
224 P_("A shorter label that may be used on toolbar buttons."),
226 GTK_PARAM_READWRITE));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_string ("tooltip",
231 P_("A tooltip for this action."),
233 GTK_PARAM_READWRITE));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_string ("stock-id",
238 P_("The stock icon displayed in widgets representing "
241 GTK_PARAM_READWRITE));
243 * GtkAction:icon-name:
245 * The name of the icon from the icon theme.
246 * Note that the stock icon is preferred, if
247 * the ::stock-id property holds the id of an
248 * existing stock icon.
252 g_object_class_install_property (gobject_class,
254 g_param_spec_string ("icon-name",
256 P_("The name of the icon from the icon theme"),
258 GTK_PARAM_READWRITE));
259 g_object_class_install_property (gobject_class,
260 PROP_VISIBLE_HORIZONTAL,
261 g_param_spec_boolean ("visible-horizontal",
262 P_("Visible when horizontal"),
263 P_("Whether the toolbar item is visible when the toolbar "
264 "is in a horizontal orientation."),
266 GTK_PARAM_READWRITE));
268 * GtkAction:visible-overflown:
270 * When %TRUE, toolitem proxies for this action are represented in the
271 * toolbar overflow menu.
275 g_object_class_install_property (gobject_class,
276 PROP_VISIBLE_OVERFLOWN,
277 g_param_spec_boolean ("visible-overflown",
278 P_("Visible when overflown"),
279 P_("When TRUE, toolitem proxies for this action "
280 "are represented in the toolbar overflow menu."),
282 GTK_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class,
284 PROP_VISIBLE_VERTICAL,
285 g_param_spec_boolean ("visible-vertical",
286 P_("Visible when vertical"),
287 P_("Whether the toolbar item is visible when the toolbar "
288 "is in a vertical orientation."),
290 GTK_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class,
293 g_param_spec_boolean ("is-important",
295 P_("Whether the action is considered important. "
296 "When TRUE, toolitem proxies for this action "
297 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
299 GTK_PARAM_READWRITE));
300 g_object_class_install_property (gobject_class,
302 g_param_spec_boolean ("hide-if-empty",
304 P_("When TRUE, empty menu proxies for this action are hidden."),
306 GTK_PARAM_READWRITE));
307 g_object_class_install_property (gobject_class,
309 g_param_spec_boolean ("sensitive",
311 P_("Whether the action is enabled."),
313 GTK_PARAM_READWRITE));
314 g_object_class_install_property (gobject_class,
316 g_param_spec_boolean ("visible",
318 P_("Whether the action is visible."),
320 GTK_PARAM_READWRITE));
321 g_object_class_install_property (gobject_class,
323 g_param_spec_object ("action-group",
325 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
326 GTK_TYPE_ACTION_GROUP,
327 GTK_PARAM_READWRITE));
330 * GtkAction::activate:
331 * @action: the #GtkAction
333 * The "activate" signal is emitted when the action is activated.
337 action_signals[ACTIVATE] =
338 g_signal_new (I_("activate"),
339 G_OBJECT_CLASS_TYPE (klass),
340 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
341 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
342 g_cclosure_marshal_VOID__VOID,
345 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
350 gtk_action_init (GtkAction *action)
352 action->private_data = GTK_ACTION_GET_PRIVATE (action);
354 action->private_data->name = NULL;
355 action->private_data->label = NULL;
356 action->private_data->short_label = NULL;
357 action->private_data->tooltip = NULL;
358 action->private_data->stock_id = NULL;
359 action->private_data->icon_name = NULL;
360 action->private_data->visible_horizontal = TRUE;
361 action->private_data->visible_vertical = TRUE;
362 action->private_data->visible_overflown = TRUE;
363 action->private_data->is_important = FALSE;
364 action->private_data->hide_if_empty = TRUE;
366 action->private_data->sensitive = TRUE;
367 action->private_data->visible = TRUE;
369 action->private_data->label_set = FALSE;
370 action->private_data->short_label_set = FALSE;
372 action->private_data->accel_count = 0;
373 action->private_data->accel_group = NULL;
374 action->private_data->accel_quark = 0;
375 action->private_data->accel_closure =
376 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
377 g_closure_set_marshal (action->private_data->accel_closure,
378 closure_accel_activate);
379 g_closure_ref (action->private_data->accel_closure);
380 g_closure_sink (action->private_data->accel_closure);
382 action->private_data->action_group = NULL;
384 action->private_data->proxies = NULL;
388 gtk_action_buildable_init (GtkBuildableIface *iface)
390 iface->set_name = gtk_action_buildable_set_name;
391 iface->get_name = gtk_action_buildable_get_name;
395 gtk_action_buildable_set_name (GtkBuildable *buildable,
398 GtkAction *action = GTK_ACTION (buildable);
400 action->private_data->name = g_intern_string (name);
404 gtk_action_buildable_get_name (GtkBuildable *buildable)
406 GtkAction *action = GTK_ACTION (buildable);
408 return action->private_data->name;
413 * @name: A unique name for the action
414 * @label: the label displayed in menu items and on buttons, or %NULL
415 * @tooltip: a tooltip for the action, or %NULL
416 * @stock_id: the stock icon to display in widgets representing the
419 * Creates a new #GtkAction object. To add the action to a
420 * #GtkActionGroup and set the accelerator for the action,
421 * call gtk_action_group_add_action_with_accel().
422 * See <xref linkend="XML-UI"/> for information on allowed action
425 * Return value: a new #GtkAction
430 gtk_action_new (const gchar *name,
432 const gchar *tooltip,
433 const gchar *stock_id)
435 g_return_val_if_fail (name != NULL, NULL);
437 return g_object_new (GTK_TYPE_ACTION,
441 "stock-id", stock_id,
446 gtk_action_finalize (GObject *object)
449 action = GTK_ACTION (object);
451 g_free (action->private_data->label);
452 g_free (action->private_data->short_label);
453 g_free (action->private_data->tooltip);
454 g_free (action->private_data->stock_id);
455 g_free (action->private_data->icon_name);
457 g_closure_unref (action->private_data->accel_closure);
458 if (action->private_data->accel_group)
459 g_object_unref (action->private_data->accel_group);
461 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
465 gtk_action_set_property (GObject *object,
472 action = GTK_ACTION (object);
477 action->private_data->name = g_intern_string (g_value_get_string (value));
480 gtk_action_set_label (action, g_value_get_string (value));
482 case PROP_SHORT_LABEL:
483 gtk_action_set_short_label (action, g_value_get_string (value));
486 gtk_action_set_tooltip (action, g_value_get_string (value));
489 gtk_action_set_stock_id (action, g_value_get_string (value));
492 gtk_action_set_icon_name (action, g_value_get_string (value));
494 case PROP_VISIBLE_HORIZONTAL:
495 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
497 case PROP_VISIBLE_VERTICAL:
498 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
500 case PROP_VISIBLE_OVERFLOWN:
501 action->private_data->visible_overflown = g_value_get_boolean (value);
503 case PROP_IS_IMPORTANT:
504 gtk_action_set_is_important (action, g_value_get_boolean (value));
506 case PROP_HIDE_IF_EMPTY:
507 action->private_data->hide_if_empty = g_value_get_boolean (value);
510 gtk_action_set_sensitive (action, g_value_get_boolean (value));
513 gtk_action_set_visible (action, g_value_get_boolean (value));
515 case PROP_ACTION_GROUP:
516 gtk_action_set_action_group (action, g_value_get_object (value));
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525 gtk_action_get_property (GObject *object,
532 action = GTK_ACTION (object);
537 g_value_set_static_string (value, action->private_data->name);
540 g_value_set_string (value, action->private_data->label);
542 case PROP_SHORT_LABEL:
543 g_value_set_string (value, action->private_data->short_label);
546 g_value_set_string (value, action->private_data->tooltip);
549 g_value_set_string (value, action->private_data->stock_id);
552 g_value_set_string (value, action->private_data->icon_name);
554 case PROP_VISIBLE_HORIZONTAL:
555 g_value_set_boolean (value, action->private_data->visible_horizontal);
557 case PROP_VISIBLE_VERTICAL:
558 g_value_set_boolean (value, action->private_data->visible_vertical);
560 case PROP_VISIBLE_OVERFLOWN:
561 g_value_set_boolean (value, action->private_data->visible_overflown);
563 case PROP_IS_IMPORTANT:
564 g_value_set_boolean (value, action->private_data->is_important);
566 case PROP_HIDE_IF_EMPTY:
567 g_value_set_boolean (value, action->private_data->hide_if_empty);
570 g_value_set_boolean (value, action->private_data->sensitive);
573 g_value_set_boolean (value, action->private_data->visible);
575 case PROP_ACTION_GROUP:
576 g_value_set_object (value, action->private_data->action_group);
579 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585 create_menu_item (GtkAction *action)
587 GType menu_item_type;
589 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
591 return g_object_new (menu_item_type, NULL);
595 create_tool_item (GtkAction *action)
597 GType toolbar_item_type;
599 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
601 return g_object_new (toolbar_item_type, NULL);
605 remove_proxy (GtkAction *action,
608 if (GTK_IS_MENU_ITEM (proxy))
609 gtk_action_disconnect_accelerator (action);
611 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
615 * _gtk_action_sync_menu_visible:
616 * @action: a #GtkAction, or %NULL to determine the action from @proxy
617 * @proxy: a proxy menu item
618 * @empty: whether the submenu attached to @proxy is empty
620 * Updates the visibility of @proxy from the visibility of @action
621 * according to the following rules:
623 * <listitem><para>if @action is invisible, @proxy is too
625 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
626 * property of @action indicates otherwise
630 * This function is used in the implementation of #GtkUIManager.
633 _gtk_action_sync_menu_visible (GtkAction *action,
637 gboolean visible = TRUE;
638 gboolean hide_if_empty = TRUE;
640 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
641 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
644 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
648 /* a GtkMenu for a <popup/> doesn't have to have an action */
649 visible = gtk_action_is_visible (action);
650 hide_if_empty = action->private_data->hide_if_empty;
653 if (visible && !(empty && hide_if_empty))
654 gtk_widget_show (proxy);
656 gtk_widget_hide (proxy);
659 gboolean _gtk_menu_is_empty (GtkWidget *menu);
662 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
665 GtkWidget *menu_item;
667 if (action->private_data->visible_overflown)
669 menu_item = gtk_action_create_menu_item (action);
671 g_object_ref_sink (menu_item);
673 gtk_tool_item_set_proxy_menu_item (tool_item,
674 "gtk-action-menu-item", menu_item);
675 g_object_unref (menu_item);
678 gtk_tool_item_set_proxy_menu_item (tool_item,
679 "gtk-action-menu-item", NULL);
685 connect_proxy (GtkAction *action,
688 g_object_ref (action);
689 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
692 /* add this widget to the list of proxies */
693 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
694 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
696 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
697 if (gtk_action_is_visible (action))
698 gtk_widget_show (proxy);
700 gtk_widget_hide (proxy);
701 gtk_widget_set_no_show_all (proxy, TRUE);
703 if (GTK_IS_MENU_ITEM (proxy))
706 /* menu item specific synchronisers ... */
708 if (action->private_data->accel_quark)
710 gtk_action_connect_accelerator (action);
711 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
712 g_quark_to_string (action->private_data->accel_quark));
715 label = GTK_BIN (proxy)->child;
717 /* make sure label is a label */
718 if (label && !GTK_IS_LABEL (label))
720 gtk_container_remove (GTK_CONTAINER (proxy), label);
725 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
726 "use-underline", TRUE,
732 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
734 "accel-closure", action->private_data->accel_closure,
737 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
739 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
743 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
744 if (image && !GTK_IS_IMAGE (image))
746 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
751 image = gtk_image_new ();
752 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
754 gtk_widget_show (image);
757 if (action->private_data->stock_id &&
758 gtk_icon_factory_lookup_default (action->private_data->stock_id))
759 gtk_image_set_from_stock (GTK_IMAGE (image),
760 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
761 else if (action->private_data->icon_name)
762 gtk_image_set_from_icon_name (GTK_IMAGE (image),
763 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
766 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
767 g_signal_connect_object (proxy, "activate",
768 G_CALLBACK (gtk_action_activate), action,
772 else if (GTK_IS_TOOL_ITEM (proxy))
774 /* toolbar button specific synchronisers ... */
775 if (GTK_IS_TOOL_BUTTON (proxy))
778 "visible-horizontal", action->private_data->visible_horizontal,
779 "visible-vertical", action->private_data->visible_vertical,
780 "is-important", action->private_data->is_important,
781 "label", action->private_data->short_label,
782 "use-underline", TRUE,
783 "stock-id", action->private_data->stock_id,
784 "icon-name", action->private_data->icon_name,
787 g_signal_connect_object (proxy, "clicked",
788 G_CALLBACK (gtk_action_activate), action,
794 "visible-horizontal", action->private_data->visible_horizontal,
795 "visible-vertical", action->private_data->visible_vertical,
796 "is-important", action->private_data->is_important,
800 gtk_action_sync_tooltip (action, proxy);
802 g_signal_connect_object (proxy, "create_menu_proxy",
803 G_CALLBACK (gtk_action_create_menu_proxy),
806 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
808 else if (GTK_IS_BUTTON (proxy))
810 /* button specific synchronisers ... */
811 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
813 /* synchronise stock-id */
815 "label", action->private_data->stock_id,
822 image = gtk_button_get_image (GTK_BUTTON (proxy));
824 if (GTK_IS_IMAGE (image) ||
825 GTK_BIN (proxy)->child == NULL ||
826 GTK_IS_LABEL (GTK_BIN (proxy)->child))
828 /* synchronise the label */
830 "label", action->private_data->short_label,
831 "use-underline", TRUE,
835 if (GTK_IS_IMAGE (image) &&
836 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
837 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
838 gtk_image_set_from_icon_name (GTK_IMAGE (image),
839 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
841 /* we leave the button alone if there is a custom child */
842 g_signal_connect_object (proxy, "clicked",
843 G_CALLBACK (gtk_action_activate), action,
847 if (action->private_data->action_group)
848 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
852 disconnect_proxy (GtkAction *action,
855 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
857 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
858 remove_proxy (action, proxy);
860 /* disconnect the activate handler */
861 g_signal_handlers_disconnect_by_func (proxy,
862 G_CALLBACK (gtk_action_activate),
865 /* toolbar button specific synchronisers ... */
866 g_signal_handlers_disconnect_by_func (proxy,
867 G_CALLBACK (gtk_action_create_menu_proxy),
870 if (action->private_data->action_group)
871 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
875 _gtk_action_emit_activate (GtkAction *action)
877 GtkActionGroup *group = action->private_data->action_group;
881 g_object_ref (group);
882 _gtk_action_group_emit_pre_activate (group, action);
885 g_signal_emit (action, action_signals[ACTIVATE], 0);
889 _gtk_action_group_emit_post_activate (group, action);
890 g_object_unref (group);
895 * gtk_action_activate:
896 * @action: the action object
898 * Emits the "activate" signal on the specified action, if it isn't
899 * insensitive. This gets called by the proxy widgets when they get
902 * It can also be used to manually activate an action.
907 gtk_action_activate (GtkAction *action)
909 g_return_if_fail (GTK_IS_ACTION (action));
911 if (gtk_action_is_sensitive (action))
912 _gtk_action_emit_activate (action);
916 * gtk_action_create_icon:
917 * @action: the action object
918 * @icon_size: the size of the icon that should be created.
920 * This function is intended for use by action implementations to
921 * create icons displayed in the proxy widgets.
923 * Returns: a widget that displays the icon for this action.
928 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
930 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
932 if (action->private_data->stock_id &&
933 gtk_icon_factory_lookup_default (action->private_data->stock_id))
934 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
935 else if (action->private_data->icon_name)
936 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
942 * gtk_action_create_menu_item:
943 * @action: the action object
945 * Creates a menu item widget that proxies for the given action.
947 * Returns: a menu item connected to the action.
952 gtk_action_create_menu_item (GtkAction *action)
954 GtkWidget *menu_item;
956 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
958 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
960 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
966 * gtk_action_create_tool_item:
967 * @action: the action object
969 * Creates a toolbar item widget that proxies for the given action.
971 * Returns: a toolbar item connected to the action.
976 gtk_action_create_tool_item (GtkAction *action)
980 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
982 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
984 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
990 * gtk_action_connect_proxy:
991 * @action: the action object
992 * @proxy: the proxy widget
994 * Connects a widget to an action object as a proxy. Synchronises
995 * various properties of the action with the widget (such as label
996 * text, icon, tooltip, etc), and attaches a callback so that the
997 * action gets activated when the proxy widget does.
999 * If the widget is already connected to an action, it is disconnected
1005 gtk_action_connect_proxy (GtkAction *action,
1008 GtkAction *prev_action;
1010 g_return_if_fail (GTK_IS_ACTION (action));
1011 g_return_if_fail (GTK_IS_WIDGET (proxy));
1013 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
1016 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1018 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1022 * gtk_action_disconnect_proxy:
1023 * @action: the action object
1024 * @proxy: the proxy widget
1026 * Disconnects a proxy widget from an action.
1027 * Does <emphasis>not</emphasis> destroy the widget, however.
1032 gtk_action_disconnect_proxy (GtkAction *action,
1035 g_return_if_fail (GTK_IS_ACTION (action));
1036 g_return_if_fail (GTK_IS_WIDGET (proxy));
1038 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
1040 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1044 * gtk_action_get_proxies:
1045 * @action: the action object
1047 * Returns the proxy widgets for an action.
1048 * See also gtk_widget_get_action().
1050 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1051 * and must not be modified.
1056 gtk_action_get_proxies (GtkAction *action)
1058 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1060 return action->private_data->proxies;
1065 * gtk_widget_get_action:
1066 * @widget: a #GtkWidget
1068 * Returns the #GtkAction that @widget is a proxy for.
1069 * See also gtk_action_get_proxies().
1071 * Returns: the action that a widget is a proxy for, or
1072 * %NULL, if it is not attached to an action.
1077 gtk_widget_get_action (GtkWidget *widget)
1079 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1081 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1086 * gtk_action_get_name:
1087 * @action: the action object
1089 * Returns the name of the action.
1091 * Return value: the name of the action. The string belongs to GTK+ and should not
1096 G_CONST_RETURN gchar *
1097 gtk_action_get_name (GtkAction *action)
1099 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1101 return action->private_data->name;
1105 * gtk_action_is_sensitive:
1106 * @action: the action object
1108 * Returns whether the action is effectively sensitive.
1110 * Return value: %TRUE if the action and its associated action group
1111 * are both sensitive.
1116 gtk_action_is_sensitive (GtkAction *action)
1118 GtkActionPrivate *priv;
1119 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1121 priv = action->private_data;
1122 return priv->sensitive &&
1123 (priv->action_group == NULL ||
1124 gtk_action_group_get_sensitive (priv->action_group));
1128 * gtk_action_get_sensitive:
1129 * @action: the action object
1131 * Returns whether the action itself is sensitive. Note that this doesn't
1132 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1135 * Return value: %TRUE if the action itself is sensitive.
1140 gtk_action_get_sensitive (GtkAction *action)
1142 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1144 return action->private_data->sensitive;
1148 _gtk_action_sync_sensitive (GtkAction *action)
1154 sensitive = gtk_action_is_sensitive (action);
1156 for (p = action->private_data->proxies; p; p = p->next)
1158 proxy = (GtkWidget *)p->data;
1159 gtk_widget_set_sensitive (proxy, sensitive);
1164 * gtk_action_set_sensitive:
1165 * @action: the action object
1166 * @sensitive: %TRUE to make the action sensitive
1168 * Sets the ::sensitive property of the action to @sensitive. Note that
1169 * this doesn't necessarily mean effective sensitivity. See
1170 * gtk_action_is_sensitive()
1176 gtk_action_set_sensitive (GtkAction *action,
1179 g_return_if_fail (GTK_IS_ACTION (action));
1181 sensitive = sensitive != FALSE;
1183 if (action->private_data->sensitive != sensitive)
1185 action->private_data->sensitive = sensitive;
1187 _gtk_action_sync_sensitive (action);
1189 g_object_notify (G_OBJECT (action), "sensitive");
1194 * gtk_action_is_visible:
1195 * @action: the action object
1197 * Returns whether the action is effectively visible.
1199 * Return value: %TRUE if the action and its associated action group
1205 gtk_action_is_visible (GtkAction *action)
1207 GtkActionPrivate *priv;
1208 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1210 priv = action->private_data;
1211 return priv->visible &&
1212 (priv->action_group == NULL ||
1213 gtk_action_group_get_visible (priv->action_group));
1217 * gtk_action_get_visible:
1218 * @action: the action object
1220 * Returns whether the action itself is visible. Note that this doesn't
1221 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1224 * Return value: %TRUE if the action itself is visible.
1229 gtk_action_get_visible (GtkAction *action)
1231 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1233 return action->private_data->visible;
1237 _gtk_action_sync_visible (GtkAction *action)
1244 visible = gtk_action_is_visible (action);
1246 for (p = action->private_data->proxies; p; p = p->next)
1248 proxy = (GtkWidget *)p->data;
1250 if (GTK_IS_MENU_ITEM (proxy))
1252 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1254 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1259 gtk_widget_show (proxy);
1261 gtk_widget_hide (proxy);
1267 * gtk_action_set_visible:
1268 * @action: the action object
1269 * @visible: %TRUE to make the action visible
1271 * Sets the ::visible property of the action to @visible. Note that
1272 * this doesn't necessarily mean effective visibility. See
1273 * gtk_action_is_visible()
1279 gtk_action_set_visible (GtkAction *action,
1282 g_return_if_fail (GTK_IS_ACTION (action));
1284 visible = visible != FALSE;
1286 if (action->private_data->visible != visible)
1288 action->private_data->visible = visible;
1290 _gtk_action_sync_visible (action);
1292 g_object_notify (G_OBJECT (action), "visible");
1297 gtk_action_set_is_important (GtkAction *action,
1298 gboolean is_important)
1303 is_important = is_important != FALSE;
1305 if (action->private_data->is_important != is_important)
1307 action->private_data->is_important = is_important;
1309 for (p = action->private_data->proxies; p; p = p->next)
1311 proxy = (GtkWidget *)p->data;
1313 if (GTK_IS_TOOL_ITEM (proxy))
1314 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1318 g_object_notify (G_OBJECT (action), "is-important");
1323 gtk_action_set_label (GtkAction *action,
1327 GtkWidget *proxy, *child;
1330 tmp = action->private_data->label;
1331 action->private_data->label = g_strdup (label);
1333 action->private_data->label_set = (action->private_data->label != NULL);
1334 /* if label is unset, then use the label from the stock item */
1335 if (!action->private_data->label_set && action->private_data->stock_id)
1337 GtkStockItem stock_item;
1339 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1340 action->private_data->label = g_strdup (stock_item.label);
1343 for (p = action->private_data->proxies; p; p = p->next)
1345 proxy = (GtkWidget *)p->data;
1347 if (GTK_IS_MENU_ITEM (proxy))
1349 child = GTK_BIN (proxy)->child;
1351 if (GTK_IS_LABEL (child))
1352 gtk_label_set_label (GTK_LABEL (child),
1353 action->private_data->label);
1357 g_object_notify (G_OBJECT (action), "label");
1359 /* if short_label is unset, set short_label=label */
1360 if (!action->private_data->short_label_set)
1362 gtk_action_set_short_label (action, action->private_data->label);
1363 action->private_data->short_label_set = FALSE;
1368 gtk_action_set_short_label (GtkAction *action,
1372 GtkWidget *proxy, *child;
1375 tmp = action->private_data->short_label;
1376 action->private_data->short_label = g_strdup (label);
1378 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1379 /* if short_label is unset, then use the value of label */
1380 if (!action->private_data->short_label_set)
1381 action->private_data->short_label = g_strdup (action->private_data->label);
1383 for (p = action->private_data->proxies; p; p = p->next)
1385 proxy = (GtkWidget *)p->data;
1387 if (GTK_IS_TOOL_BUTTON (proxy))
1388 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1389 action->private_data->short_label);
1390 else if (GTK_IS_BUTTON (proxy) &&
1391 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1394 child = GTK_BIN (proxy)->child;
1396 image = gtk_button_get_image (GTK_BUTTON (proxy));
1398 if (GTK_IS_IMAGE (image) ||
1399 child == NULL || GTK_IS_LABEL (child))
1400 gtk_button_set_label (GTK_BUTTON (proxy),
1401 action->private_data->short_label);
1405 g_object_notify (G_OBJECT (action), "short-label");
1409 gtk_action_set_visible_horizontal (GtkAction *action,
1410 gboolean visible_horizontal)
1415 visible_horizontal = visible_horizontal != FALSE;
1417 if (action->private_data->visible_horizontal != visible_horizontal)
1419 action->private_data->visible_horizontal = visible_horizontal;
1421 for (p = action->private_data->proxies; p; p = p->next)
1423 proxy = (GtkWidget *)p->data;
1425 if (GTK_IS_TOOL_ITEM (proxy))
1426 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1427 visible_horizontal);
1430 g_object_notify (G_OBJECT (action), "visible-horizontal");
1435 gtk_action_set_visible_vertical (GtkAction *action,
1436 gboolean visible_vertical)
1441 visible_vertical = visible_vertical != FALSE;
1443 if (action->private_data->visible_vertical != visible_vertical)
1445 action->private_data->visible_vertical = visible_vertical;
1447 for (p = action->private_data->proxies; p; p = p->next)
1449 proxy = (GtkWidget *)p->data;
1451 if (GTK_IS_TOOL_ITEM (proxy))
1452 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1456 g_object_notify (G_OBJECT (action), "visible-vertical");
1461 gtk_action_sync_tooltip (GtkAction *action,
1464 gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (proxy),
1465 action->private_data->tooltip);
1469 gtk_action_set_tooltip (GtkAction *action,
1470 const gchar *tooltip)
1476 tmp = action->private_data->tooltip;
1477 action->private_data->tooltip = g_strdup (tooltip);
1480 for (p = action->private_data->proxies; p; p = p->next)
1482 proxy = (GtkWidget *)p->data;
1484 if (GTK_IS_TOOL_ITEM (proxy))
1485 gtk_action_sync_tooltip (action, proxy);
1488 g_object_notify (G_OBJECT (action), "tooltip");
1492 gtk_action_set_stock_id (GtkAction *action,
1493 const gchar *stock_id)
1496 GtkWidget *proxy, *image;
1499 tmp = action->private_data->stock_id;
1500 action->private_data->stock_id = g_strdup (stock_id);
1503 for (p = action->private_data->proxies; p; p = p->next)
1505 proxy = (GtkWidget *)p->data;
1507 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1509 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1511 if (GTK_IS_IMAGE (image))
1512 gtk_image_set_from_stock (GTK_IMAGE (image),
1513 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1515 else if (GTK_IS_TOOL_BUTTON (proxy))
1517 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1518 action->private_data->stock_id);
1520 else if (GTK_IS_BUTTON (proxy) &&
1521 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1523 gtk_button_set_label (GTK_BUTTON (proxy),
1524 action->private_data->stock_id);
1528 g_object_notify (G_OBJECT (action), "stock-id");
1530 /* update label and short_label if appropriate */
1531 if (!action->private_data->label_set)
1533 GtkStockItem stock_item;
1535 if (action->private_data->stock_id &&
1536 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1537 gtk_action_set_label (action, stock_item.label);
1539 gtk_action_set_label (action, NULL);
1541 action->private_data->label_set = FALSE;
1546 gtk_action_set_icon_name (GtkAction *action,
1547 const gchar *icon_name)
1550 GtkWidget *proxy, *image;
1553 tmp = action->private_data->icon_name;
1554 action->private_data->icon_name = g_strdup (icon_name);
1557 for (p = action->private_data->proxies; p; p = p->next)
1559 proxy = (GtkWidget *)p->data;
1561 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1563 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1565 if (GTK_IS_IMAGE (image) &&
1566 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1567 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1568 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1569 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1571 else if (GTK_IS_TOOL_BUTTON (proxy))
1573 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1574 action->private_data->icon_name);
1576 else if (GTK_IS_BUTTON (proxy) &&
1577 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1579 image = gtk_button_get_image (GTK_BUTTON (proxy));
1581 if (GTK_IS_IMAGE (image) &&
1582 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1583 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1584 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1585 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1589 g_object_notify (G_OBJECT (action), "icon-name");
1594 * gtk_action_block_activate_from:
1595 * @action: the action object
1596 * @proxy: a proxy widget
1598 * Disables calls to the gtk_action_activate()
1599 * function by signals on the given proxy widget. This is used to
1600 * break notification loops for things like check or radio actions.
1602 * This function is intended for use by action implementations.
1607 gtk_action_block_activate_from (GtkAction *action,
1610 g_return_if_fail (GTK_IS_ACTION (action));
1612 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1617 * gtk_action_unblock_activate_from:
1618 * @action: the action object
1619 * @proxy: a proxy widget
1621 * Re-enables calls to the gtk_action_activate()
1622 * function by signals on the given proxy widget. This undoes the
1623 * blocking done by gtk_action_block_activate_from().
1625 * This function is intended for use by action implementations.
1630 gtk_action_unblock_activate_from (GtkAction *action,
1633 g_return_if_fail (GTK_IS_ACTION (action));
1635 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1640 closure_accel_activate (GClosure *closure,
1641 GValue *return_value,
1642 guint n_param_values,
1643 const GValue *param_values,
1644 gpointer invocation_hint,
1645 gpointer marshal_data)
1647 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1649 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1651 /* we handled the accelerator */
1652 g_value_set_boolean (return_value, TRUE);
1657 gtk_action_set_action_group (GtkAction *action,
1658 GtkActionGroup *action_group)
1660 if (action->private_data->action_group == NULL)
1661 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1663 g_return_if_fail (action_group == NULL);
1665 action->private_data->action_group = action_group;
1669 * gtk_action_set_accel_path:
1670 * @action: the action object
1671 * @accel_path: the accelerator path
1673 * Sets the accel path for this action. All proxy widgets associated
1674 * with the action will have this accel path, so that their
1675 * accelerators are consistent.
1677 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1678 * pass a static string, you can save some memory by interning it first with
1679 * g_intern_static_string().
1684 gtk_action_set_accel_path (GtkAction *action,
1685 const gchar *accel_path)
1687 g_return_if_fail (GTK_IS_ACTION (action));
1689 action->private_data->accel_quark = g_quark_from_string (accel_path);
1693 * gtk_action_get_accel_path:
1694 * @action: the action object
1696 * Returns the accel path for this action.
1700 * Returns: the accel path for this action, or %NULL
1701 * if none is set. The returned string is owned by GTK+
1702 * and must not be freed or modified.
1704 G_CONST_RETURN gchar *
1705 gtk_action_get_accel_path (GtkAction *action)
1707 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1709 if (action->private_data->accel_quark)
1710 return g_quark_to_string (action->private_data->accel_quark);
1716 * gtk_action_get_accel_closure:
1717 * @action: the action object
1719 * Returns the accel closure for this action.
1723 * Returns: the accel closure for this action. The returned closure is
1724 * owned by GTK+ and must not be unreffed or modified.
1727 gtk_action_get_accel_closure (GtkAction *action)
1729 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1731 return action->private_data->accel_closure;
1736 * gtk_action_set_accel_group:
1737 * @action: the action object
1738 * @accel_group: a #GtkAccelGroup or %NULL
1740 * Sets the #GtkAccelGroup in which the accelerator for this action
1741 * will be installed.
1746 gtk_action_set_accel_group (GtkAction *action,
1747 GtkAccelGroup *accel_group)
1749 g_return_if_fail (GTK_IS_ACTION (action));
1750 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1753 g_object_ref (accel_group);
1754 if (action->private_data->accel_group)
1755 g_object_unref (action->private_data->accel_group);
1757 action->private_data->accel_group = accel_group;
1761 * gtk_action_connect_accelerator:
1762 * @action: a #GtkAction
1764 * Installs the accelerator for @action if @action has an
1765 * accel path and group. See gtk_action_set_accel_path() and
1766 * gtk_action_set_accel_group()
1768 * Since multiple proxies may independently trigger the installation
1769 * of the accelerator, the @action counts the number of times this
1770 * function has been called and doesn't remove the accelerator until
1771 * gtk_action_disconnect_accelerator() has been called as many times.
1776 gtk_action_connect_accelerator (GtkAction *action)
1778 g_return_if_fail (GTK_IS_ACTION (action));
1780 if (!action->private_data->accel_quark ||
1781 !action->private_data->accel_group)
1784 if (action->private_data->accel_count == 0)
1786 const gchar *accel_path =
1787 g_quark_to_string (action->private_data->accel_quark);
1789 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1791 action->private_data->accel_closure);
1794 action->private_data->accel_count++;
1798 * gtk_action_disconnect_accelerator:
1799 * @action: a #GtkAction
1801 * Undoes the effect of one call to gtk_action_connect_accelerator().
1806 gtk_action_disconnect_accelerator (GtkAction *action)
1808 g_return_if_fail (GTK_IS_ACTION (action));
1810 if (!action->private_data->accel_quark ||
1811 !action->private_data->accel_group)
1814 action->private_data->accel_count--;
1816 if (action->private_data->accel_count == 0)
1817 gtk_accel_group_disconnect (action->private_data->accel_group,
1818 action->private_data->accel_closure);
1822 * gtk_action_create_menu:
1823 * @action: a #GtkAction
1825 * If @action provides a #GtkMenu widget as a submenu for the menu
1826 * item or the toolbar item it creates, this function returns an
1827 * instance of that menu.
1829 * Return value: the menu item provided by the action, or %NULL.
1834 gtk_action_create_menu (GtkAction *action)
1836 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1838 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1839 return GTK_ACTION_GET_CLASS (action)->create_menu (action);
1844 #define __GTK_ACTION_C__
1845 #include "gtkaliasdef.c"