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 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,
401 GtkAction *action = GTK_ACTION (buildable);
403 action->private_data->name = g_intern_string (name);
407 gtk_action_buildable_get_name (GtkBuildable *buildable)
409 GtkAction *action = GTK_ACTION (buildable);
411 return action->private_data->name;
416 * @name: A unique name for the action
417 * @label: the label displayed in menu items and on buttons
418 * @tooltip: a tooltip for the action
419 * @stock_id: the stock icon to display in widgets representing the action
421 * Creates a new #GtkAction object. To add the action to a
422 * #GtkActionGroup and set the accelerator for the action,
423 * call gtk_action_group_add_action_with_accel().
424 * See <xref linkend="XML-UI"/> for information on allowed action
427 * Return value: a new #GtkAction
432 gtk_action_new (const gchar *name,
434 const gchar *tooltip,
435 const gchar *stock_id)
439 action = g_object_new (GTK_TYPE_ACTION,
443 "stock-id", stock_id,
450 gtk_action_finalize (GObject *object)
453 action = GTK_ACTION (object);
455 g_free (action->private_data->label);
456 g_free (action->private_data->short_label);
457 g_free (action->private_data->tooltip);
458 g_free (action->private_data->stock_id);
459 g_free (action->private_data->icon_name);
461 g_closure_unref (action->private_data->accel_closure);
462 if (action->private_data->accel_group)
463 g_object_unref (action->private_data->accel_group);
465 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
469 gtk_action_set_property (GObject *object,
476 action = GTK_ACTION (object);
481 action->private_data->name = g_intern_string (g_value_get_string (value));
484 gtk_action_set_label (action, g_value_get_string (value));
486 case PROP_SHORT_LABEL:
487 gtk_action_set_short_label (action, g_value_get_string (value));
490 gtk_action_set_tooltip (action, g_value_get_string (value));
493 gtk_action_set_stock_id (action, g_value_get_string (value));
496 gtk_action_set_icon_name (action, g_value_get_string (value));
498 case PROP_VISIBLE_HORIZONTAL:
499 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
501 case PROP_VISIBLE_VERTICAL:
502 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
504 case PROP_VISIBLE_OVERFLOWN:
505 action->private_data->visible_overflown = g_value_get_boolean (value);
507 case PROP_IS_IMPORTANT:
508 gtk_action_set_is_important (action, g_value_get_boolean (value));
510 case PROP_HIDE_IF_EMPTY:
511 action->private_data->hide_if_empty = g_value_get_boolean (value);
514 gtk_action_set_sensitive (action, g_value_get_boolean (value));
517 gtk_action_set_visible (action, g_value_get_boolean (value));
519 case PROP_ACTION_GROUP:
520 gtk_action_set_action_group (action, g_value_get_object (value));
523 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529 gtk_action_get_property (GObject *object,
536 action = GTK_ACTION (object);
541 g_value_set_static_string (value, action->private_data->name);
544 g_value_set_string (value, action->private_data->label);
546 case PROP_SHORT_LABEL:
547 g_value_set_string (value, action->private_data->short_label);
550 g_value_set_string (value, action->private_data->tooltip);
553 g_value_set_string (value, action->private_data->stock_id);
556 g_value_set_string (value, action->private_data->icon_name);
558 case PROP_VISIBLE_HORIZONTAL:
559 g_value_set_boolean (value, action->private_data->visible_horizontal);
561 case PROP_VISIBLE_VERTICAL:
562 g_value_set_boolean (value, action->private_data->visible_vertical);
564 case PROP_VISIBLE_OVERFLOWN:
565 g_value_set_boolean (value, action->private_data->visible_overflown);
567 case PROP_IS_IMPORTANT:
568 g_value_set_boolean (value, action->private_data->is_important);
570 case PROP_HIDE_IF_EMPTY:
571 g_value_set_boolean (value, action->private_data->hide_if_empty);
574 g_value_set_boolean (value, action->private_data->sensitive);
577 g_value_set_boolean (value, action->private_data->visible);
579 case PROP_ACTION_GROUP:
580 g_value_set_object (value, action->private_data->action_group);
583 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
589 create_menu_item (GtkAction *action)
591 GType menu_item_type;
593 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
595 return g_object_new (menu_item_type, NULL);
599 create_tool_item (GtkAction *action)
601 GType toolbar_item_type;
603 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
605 return g_object_new (toolbar_item_type, NULL);
609 remove_proxy (GtkAction *action,
612 if (GTK_IS_MENU_ITEM (proxy))
613 gtk_action_disconnect_accelerator (action);
615 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
619 * _gtk_action_sync_menu_visible:
620 * @action: a #GtkAction, or %NULL to determine the action from @proxy
621 * @proxy: a proxy menu item
622 * @empty: whether the submenu attached to @proxy is empty
624 * Updates the visibility of @proxy from the visibility of @action
625 * according to the following rules:
627 * <listitem><para>if @action is invisible, @proxy is too
629 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
630 * property of @action indicates otherwise
634 * This function is used in the implementation of #GtkUIManager.
637 _gtk_action_sync_menu_visible (GtkAction *action,
641 gboolean visible, hide_if_empty;
643 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
644 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
647 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
649 visible = gtk_action_is_visible (action);
650 hide_if_empty = action->private_data->hide_if_empty;
652 if (visible && !(empty && hide_if_empty))
653 gtk_widget_show (proxy);
655 gtk_widget_hide (proxy);
658 gboolean _gtk_menu_is_empty (GtkWidget *menu);
661 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
664 GtkWidget *menu_item;
666 if (action->private_data->visible_overflown)
668 menu_item = gtk_action_create_menu_item (action);
670 g_object_ref_sink (menu_item);
672 gtk_tool_item_set_proxy_menu_item (tool_item,
673 "gtk-action-menu-item", menu_item);
674 g_object_unref (menu_item);
677 gtk_tool_item_set_proxy_menu_item (tool_item,
678 "gtk-action-menu-item", NULL);
684 connect_proxy (GtkAction *action,
687 g_object_ref (action);
688 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
691 /* add this widget to the list of proxies */
692 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
693 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
695 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
696 if (gtk_action_is_visible (action))
697 gtk_widget_show (proxy);
699 gtk_widget_hide (proxy);
700 gtk_widget_set_no_show_all (proxy, TRUE);
702 if (GTK_IS_MENU_ITEM (proxy))
705 /* menu item specific synchronisers ... */
707 if (action->private_data->accel_quark)
709 gtk_action_connect_accelerator (action);
710 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
711 g_quark_to_string (action->private_data->accel_quark));
714 label = GTK_BIN (proxy)->child;
716 /* make sure label is a label */
717 if (label && !GTK_IS_LABEL (label))
719 gtk_container_remove (GTK_CONTAINER (proxy), label);
724 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
725 "use-underline", TRUE,
731 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
733 "accel-closure", action->private_data->accel_closure,
736 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
738 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
742 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
743 if (image && !GTK_IS_IMAGE (image))
745 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
750 image = gtk_image_new ();
751 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
753 gtk_widget_show (image);
756 if (action->private_data->stock_id &&
757 gtk_icon_factory_lookup_default (action->private_data->stock_id))
758 gtk_image_set_from_stock (GTK_IMAGE (image),
759 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
760 else if (action->private_data->icon_name)
761 gtk_image_set_from_icon_name (GTK_IMAGE (image),
762 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
765 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
766 g_signal_connect_object (proxy, "activate",
767 G_CALLBACK (gtk_action_activate), action,
771 else if (GTK_IS_TOOL_ITEM (proxy))
773 /* toolbar button specific synchronisers ... */
774 if (GTK_IS_TOOL_BUTTON (proxy))
777 "visible-horizontal", action->private_data->visible_horizontal,
778 "visible-vertical", action->private_data->visible_vertical,
779 "is-important", action->private_data->is_important,
780 "label", action->private_data->short_label,
781 "use-underline", TRUE,
782 "stock-id", action->private_data->stock_id,
783 "icon-name", action->private_data->icon_name,
786 g_signal_connect_object (proxy, "clicked",
787 G_CALLBACK (gtk_action_activate), action,
793 "visible-horizontal", action->private_data->visible_horizontal,
794 "visible-vertical", action->private_data->visible_vertical,
795 "is-important", action->private_data->is_important,
799 gtk_action_sync_tooltip (action, proxy);
801 g_signal_connect_object (proxy, "create_menu_proxy",
802 G_CALLBACK (gtk_action_create_menu_proxy),
805 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
807 else if (GTK_IS_BUTTON (proxy))
809 /* button specific synchronisers ... */
810 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
812 /* synchronise stock-id */
814 "label", action->private_data->stock_id,
819 if (GTK_BIN (proxy)->child == NULL ||
820 GTK_IS_LABEL (GTK_BIN (proxy)->child))
822 /* synchronise the label */
824 "label", action->private_data->short_label,
825 "use-underline", TRUE,
829 /* we leave the button alone if there is a custom child */
830 g_signal_connect_object (proxy, "clicked",
831 G_CALLBACK (gtk_action_activate), action,
835 if (action->private_data->action_group)
836 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
840 disconnect_proxy (GtkAction *action,
843 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
845 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
846 remove_proxy (action, proxy);
848 /* disconnect the activate handler */
849 g_signal_handlers_disconnect_by_func (proxy,
850 G_CALLBACK (gtk_action_activate),
853 /* toolbar button specific synchronisers ... */
854 g_signal_handlers_disconnect_by_func (proxy,
855 G_CALLBACK (gtk_action_create_menu_proxy),
858 if (action->private_data->action_group)
859 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
863 _gtk_action_emit_activate (GtkAction *action)
865 GtkActionGroup *group = action->private_data->action_group;
869 g_object_ref (group);
870 _gtk_action_group_emit_pre_activate (group, action);
873 g_signal_emit (action, action_signals[ACTIVATE], 0);
877 _gtk_action_group_emit_post_activate (group, action);
878 g_object_unref (group);
883 * gtk_action_activate:
884 * @action: the action object
886 * Emits the "activate" signal on the specified action, if it isn't
887 * insensitive. This gets called by the proxy widgets when they get
890 * It can also be used to manually activate an action.
895 gtk_action_activate (GtkAction *action)
897 g_return_if_fail (GTK_IS_ACTION (action));
899 if (gtk_action_is_sensitive (action))
900 _gtk_action_emit_activate (action);
904 * gtk_action_create_icon:
905 * @action: the action object
906 * @icon_size: the size of the icon that should be created.
908 * This function is intended for use by action implementations to
909 * create icons displayed in the proxy widgets.
911 * Returns: a widget that displays the icon for this action.
916 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
918 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
920 if (action->private_data->stock_id)
921 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
922 else if (action->private_data->icon_name)
923 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
929 * gtk_action_create_menu_item:
930 * @action: the action object
932 * Creates a menu item widget that proxies for the given action.
934 * Returns: a menu item connected to the action.
939 gtk_action_create_menu_item (GtkAction *action)
941 GtkWidget *menu_item;
943 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
945 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
947 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
953 * gtk_action_create_tool_item:
954 * @action: the action object
956 * Creates a toolbar item widget that proxies for the given action.
958 * Returns: a toolbar item connected to the action.
963 gtk_action_create_tool_item (GtkAction *action)
967 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
969 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
971 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
977 * gtk_action_connect_proxy:
978 * @action: the action object
979 * @proxy: the proxy widget
981 * Connects a widget to an action object as a proxy. Synchronises
982 * various properties of the action with the widget (such as label
983 * text, icon, tooltip, etc), and attaches a callback so that the
984 * action gets activated when the proxy widget does.
986 * If the widget is already connected to an action, it is disconnected
992 gtk_action_connect_proxy (GtkAction *action,
995 GtkAction *prev_action;
997 g_return_if_fail (GTK_IS_ACTION (action));
998 g_return_if_fail (GTK_IS_WIDGET (proxy));
1000 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
1003 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1005 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1009 * gtk_action_disconnect_proxy:
1010 * @action: the action object
1011 * @proxy: the proxy widget
1013 * Disconnects a proxy widget from an action.
1014 * Does <emphasis>not</emphasis> destroy the widget, however.
1019 gtk_action_disconnect_proxy (GtkAction *action,
1022 g_return_if_fail (GTK_IS_ACTION (action));
1023 g_return_if_fail (GTK_IS_WIDGET (proxy));
1025 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
1027 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1031 * gtk_action_get_proxies:
1032 * @action: the action object
1034 * Returns the proxy widgets for an action.
1035 * See also gtk_widget_get_action().
1037 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1038 * and must not be modified.
1043 gtk_action_get_proxies (GtkAction *action)
1045 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1047 return action->private_data->proxies;
1052 * gtk_widget_get_action:
1053 * @widget: a #GtkWidget
1055 * Returns the #GtkAction that @widget is a proxy for.
1056 * See also gtk_action_get_proxies().
1058 * Returns: the action that a widget is a proxy for, or
1059 * %NULL, if it is not attached to an action.
1064 gtk_widget_get_action (GtkWidget *widget)
1066 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1068 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1073 * gtk_action_get_name:
1074 * @action: the action object
1076 * Returns the name of the action.
1078 * Return value: the name of the action. The string belongs to GTK+ and should not
1083 G_CONST_RETURN gchar *
1084 gtk_action_get_name (GtkAction *action)
1086 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1088 return action->private_data->name;
1092 * gtk_action_is_sensitive:
1093 * @action: the action object
1095 * Returns whether the action is effectively sensitive.
1097 * Return value: %TRUE if the action and its associated action group
1098 * are both sensitive.
1103 gtk_action_is_sensitive (GtkAction *action)
1105 GtkActionPrivate *priv;
1106 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1108 priv = action->private_data;
1109 return priv->sensitive &&
1110 (priv->action_group == NULL ||
1111 gtk_action_group_get_sensitive (priv->action_group));
1115 * gtk_action_get_sensitive:
1116 * @action: the action object
1118 * Returns whether the action itself is sensitive. Note that this doesn't
1119 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1122 * Return value: %TRUE if the action itself is sensitive.
1127 gtk_action_get_sensitive (GtkAction *action)
1129 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1131 return action->private_data->sensitive;
1135 _gtk_action_sync_sensitive (GtkAction *action)
1141 sensitive = gtk_action_is_sensitive (action);
1143 for (p = action->private_data->proxies; p; p = p->next)
1145 proxy = (GtkWidget *)p->data;
1146 gtk_widget_set_sensitive (proxy, sensitive);
1151 * gtk_action_set_sensitive:
1152 * @action: the action object
1153 * @sensitive: %TRUE to make the action sensitive
1155 * Sets the ::sensitive property of the action to @sensitive. Note that
1156 * this doesn't necessarily mean effective sensitivity. See
1157 * gtk_action_is_sensitive()
1163 gtk_action_set_sensitive (GtkAction *action,
1166 g_return_if_fail (GTK_IS_ACTION (action));
1168 sensitive = sensitive != FALSE;
1170 if (action->private_data->sensitive != sensitive)
1172 action->private_data->sensitive = sensitive;
1174 _gtk_action_sync_sensitive (action);
1176 g_object_notify (G_OBJECT (action), "sensitive");
1181 * gtk_action_is_visible:
1182 * @action: the action object
1184 * Returns whether the action is effectively visible.
1186 * Return value: %TRUE if the action and its associated action group
1192 gtk_action_is_visible (GtkAction *action)
1194 GtkActionPrivate *priv;
1195 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1197 priv = action->private_data;
1198 return priv->visible &&
1199 (priv->action_group == NULL ||
1200 gtk_action_group_get_visible (priv->action_group));
1204 * gtk_action_get_visible:
1205 * @action: the action object
1207 * Returns whether the action itself is visible. Note that this doesn't
1208 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1211 * Return value: %TRUE if the action itself is visible.
1216 gtk_action_get_visible (GtkAction *action)
1218 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1220 return action->private_data->visible;
1224 _gtk_action_sync_visible (GtkAction *action)
1231 visible = gtk_action_is_visible (action);
1233 for (p = action->private_data->proxies; p; p = p->next)
1235 proxy = (GtkWidget *)p->data;
1237 if (GTK_IS_MENU_ITEM (proxy))
1239 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1241 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1246 gtk_widget_show (proxy);
1248 gtk_widget_hide (proxy);
1254 * gtk_action_set_visible:
1255 * @action: the action object
1256 * @visible: %TRUE to make the action visible
1258 * Sets the ::visible property of the action to @visible. Note that
1259 * this doesn't necessarily mean effective visibility. See
1260 * gtk_action_is_visible()
1266 gtk_action_set_visible (GtkAction *action,
1269 g_return_if_fail (GTK_IS_ACTION (action));
1271 visible = visible != FALSE;
1273 if (action->private_data->visible != visible)
1275 action->private_data->visible = visible;
1277 _gtk_action_sync_visible (action);
1279 g_object_notify (G_OBJECT (action), "visible");
1284 gtk_action_set_is_important (GtkAction *action,
1285 gboolean is_important)
1290 is_important = is_important != FALSE;
1292 if (action->private_data->is_important != is_important)
1294 action->private_data->is_important = is_important;
1296 for (p = action->private_data->proxies; p; p = p->next)
1298 proxy = (GtkWidget *)p->data;
1300 if (GTK_IS_TOOL_ITEM (proxy))
1301 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1305 g_object_notify (G_OBJECT (action), "is-important");
1310 gtk_action_set_label (GtkAction *action,
1314 GtkWidget *proxy, *child;
1317 tmp = action->private_data->label;
1318 action->private_data->label = g_strdup (label);
1320 action->private_data->label_set = (action->private_data->label != NULL);
1321 /* if label is unset, then use the label from the stock item */
1322 if (!action->private_data->label_set && action->private_data->stock_id)
1324 GtkStockItem stock_item;
1326 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1327 action->private_data->label = g_strdup (stock_item.label);
1330 for (p = action->private_data->proxies; p; p = p->next)
1332 proxy = (GtkWidget *)p->data;
1334 if (GTK_IS_MENU_ITEM (proxy))
1336 child = GTK_BIN (proxy)->child;
1338 if (GTK_IS_LABEL (child))
1339 gtk_label_set_label (GTK_LABEL (child),
1340 action->private_data->label);
1344 g_object_notify (G_OBJECT (action), "label");
1346 /* if short_label is unset, set short_label=label */
1347 if (!action->private_data->short_label_set)
1349 gtk_action_set_short_label (action, action->private_data->label);
1350 action->private_data->short_label_set = FALSE;
1355 gtk_action_set_short_label (GtkAction *action,
1359 GtkWidget *proxy, *child;
1362 tmp = action->private_data->short_label;
1363 action->private_data->short_label = g_strdup (label);
1365 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1366 /* if short_label is unset, then use the value of label */
1367 if (!action->private_data->short_label_set)
1368 action->private_data->short_label = g_strdup (action->private_data->label);
1370 for (p = action->private_data->proxies; p; p = p->next)
1372 proxy = (GtkWidget *)p->data;
1374 if (GTK_IS_TOOL_BUTTON (proxy))
1375 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1376 action->private_data->short_label);
1377 else if (GTK_IS_BUTTON (proxy) &&
1378 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1380 child = GTK_BIN (proxy)->child;
1382 if (child == NULL || GTK_IS_LABEL (child))
1383 gtk_button_set_label (GTK_BUTTON (proxy),
1384 action->private_data->short_label);
1388 g_object_notify (G_OBJECT (action), "short-label");
1392 gtk_action_set_visible_horizontal (GtkAction *action,
1393 gboolean visible_horizontal)
1398 visible_horizontal = visible_horizontal != FALSE;
1400 if (action->private_data->visible_horizontal != visible_horizontal)
1402 action->private_data->visible_horizontal = visible_horizontal;
1404 for (p = action->private_data->proxies; p; p = p->next)
1406 proxy = (GtkWidget *)p->data;
1408 if (GTK_IS_TOOL_ITEM (proxy))
1409 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1410 visible_horizontal);
1413 g_object_notify (G_OBJECT (action), "visible-horizontal");
1418 gtk_action_set_visible_vertical (GtkAction *action,
1419 gboolean visible_vertical)
1424 visible_vertical = visible_vertical != FALSE;
1426 if (action->private_data->visible_vertical != visible_vertical)
1428 action->private_data->visible_vertical = visible_vertical;
1430 for (p = action->private_data->proxies; p; p = p->next)
1432 proxy = (GtkWidget *)p->data;
1434 if (GTK_IS_TOOL_ITEM (proxy))
1435 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1439 g_object_notify (G_OBJECT (action), "visible-vertical");
1444 gtk_action_sync_tooltip (GtkAction *action,
1447 gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (proxy),
1448 action->private_data->tooltip);
1452 gtk_action_set_tooltip (GtkAction *action,
1453 const gchar *tooltip)
1459 tmp = action->private_data->tooltip;
1460 action->private_data->tooltip = g_strdup (tooltip);
1463 for (p = action->private_data->proxies; p; p = p->next)
1465 proxy = (GtkWidget *)p->data;
1467 if (GTK_IS_TOOL_ITEM (proxy))
1468 gtk_action_sync_tooltip (action, proxy);
1471 g_object_notify (G_OBJECT (action), "tooltip");
1475 gtk_action_set_stock_id (GtkAction *action,
1476 const gchar *stock_id)
1479 GtkWidget *proxy, *image;
1482 tmp = action->private_data->stock_id;
1483 action->private_data->stock_id = g_strdup (stock_id);
1486 for (p = action->private_data->proxies; p; p = p->next)
1488 proxy = (GtkWidget *)p->data;
1490 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1492 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1494 if (GTK_IS_IMAGE (image))
1495 gtk_image_set_from_stock (GTK_IMAGE (image),
1496 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1498 else if (GTK_IS_TOOL_BUTTON (proxy))
1500 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1501 action->private_data->stock_id);
1503 else if (GTK_IS_BUTTON (proxy) &&
1504 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1506 gtk_button_set_label (GTK_BUTTON (proxy),
1507 action->private_data->stock_id);
1511 g_object_notify (G_OBJECT (action), "stock-id");
1513 /* update label and short_label if appropriate */
1514 if (!action->private_data->label_set)
1516 GtkStockItem stock_item;
1518 if (action->private_data->stock_id &&
1519 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1520 gtk_action_set_label (action, stock_item.label);
1522 gtk_action_set_label (action, NULL);
1524 action->private_data->label_set = FALSE;
1529 gtk_action_set_icon_name (GtkAction *action,
1530 const gchar *icon_name)
1533 GtkWidget *proxy, *image;
1536 tmp = action->private_data->icon_name;
1537 action->private_data->icon_name = g_strdup (icon_name);
1540 for (p = action->private_data->proxies; p; p = p->next)
1542 proxy = (GtkWidget *)p->data;
1544 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1546 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1548 if (GTK_IS_IMAGE (image) &&
1549 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1550 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1551 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1552 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1554 else if (GTK_IS_TOOL_BUTTON (proxy))
1556 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1557 action->private_data->icon_name);
1559 else if (GTK_IS_BUTTON (proxy) &&
1560 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1562 image = gtk_button_get_image (GTK_BUTTON (proxy));
1564 if (GTK_IS_IMAGE (image) &&
1565 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1566 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1567 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1568 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1572 g_object_notify (G_OBJECT (action), "icon-name");
1577 * gtk_action_block_activate_from:
1578 * @action: the action object
1579 * @proxy: a proxy widget
1581 * Disables calls to the gtk_action_activate()
1582 * function by signals on the given proxy widget. This is used to
1583 * break notification loops for things like check or radio actions.
1585 * This function is intended for use by action implementations.
1590 gtk_action_block_activate_from (GtkAction *action,
1593 g_return_if_fail (GTK_IS_ACTION (action));
1595 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1600 * gtk_action_unblock_activate_from:
1601 * @action: the action object
1602 * @proxy: a proxy widget
1604 * Re-enables calls to the gtk_action_activate()
1605 * function by signals on the given proxy widget. This undoes the
1606 * blocking done by gtk_action_block_activate_from().
1608 * This function is intended for use by action implementations.
1613 gtk_action_unblock_activate_from (GtkAction *action,
1616 g_return_if_fail (GTK_IS_ACTION (action));
1618 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1623 closure_accel_activate (GClosure *closure,
1624 GValue *return_value,
1625 guint n_param_values,
1626 const GValue *param_values,
1627 gpointer invocation_hint,
1628 gpointer marshal_data)
1630 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1632 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1634 /* we handled the accelerator */
1635 g_value_set_boolean (return_value, TRUE);
1640 gtk_action_set_action_group (GtkAction *action,
1641 GtkActionGroup *action_group)
1643 g_return_if_fail (GTK_IS_ACTION (action));
1645 if (action->private_data->action_group == NULL)
1646 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1648 g_return_if_fail (action_group == NULL);
1650 action->private_data->action_group = action_group;
1654 * gtk_action_set_accel_path:
1655 * @action: the action object
1656 * @accel_path: the accelerator path
1658 * Sets the accel path for this action. All proxy widgets associated
1659 * with the action will have this accel path, so that their
1660 * accelerators are consistent.
1665 gtk_action_set_accel_path (GtkAction *action,
1666 const gchar *accel_path)
1668 g_return_if_fail (GTK_IS_ACTION (action));
1670 action->private_data->accel_quark = g_quark_from_string (accel_path);
1674 * gtk_action_get_accel_path:
1675 * @action: the action object
1677 * Returns the accel path for this action.
1681 * Returns: the accel path for this action, or %NULL
1682 * if none is set. The returned string is owned by GTK+
1683 * and must not be freed or modified.
1685 G_CONST_RETURN gchar *
1686 gtk_action_get_accel_path (GtkAction *action)
1688 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1690 if (action->private_data->accel_quark)
1691 return g_quark_to_string (action->private_data->accel_quark);
1697 * gtk_action_get_accel_closure:
1698 * @action: the action object
1700 * Returns the accel closure for this action.
1704 * Returns: the accel closure for this action. The returned closure is
1705 * owned by GTK+ and must not be unreffed or modified.
1708 gtk_action_get_accel_closure (GtkAction *action)
1710 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1712 return action->private_data->accel_closure;
1717 * gtk_action_set_accel_group:
1718 * @action: the action object
1719 * @accel_group: a #GtkAccelGroup or %NULL
1721 * Sets the #GtkAccelGroup in which the accelerator for this action
1722 * will be installed.
1727 gtk_action_set_accel_group (GtkAction *action,
1728 GtkAccelGroup *accel_group)
1730 g_return_if_fail (GTK_IS_ACTION (action));
1731 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1734 g_object_ref (accel_group);
1735 if (action->private_data->accel_group)
1736 g_object_unref (action->private_data->accel_group);
1738 action->private_data->accel_group = accel_group;
1742 * gtk_action_connect_accelerator:
1743 * @action: a #GtkAction
1745 * Installs the accelerator for @action if @action has an
1746 * accel path and group. See gtk_action_set_accel_path() and
1747 * gtk_action_set_accel_group()
1749 * Since multiple proxies may independently trigger the installation
1750 * of the accelerator, the @action counts the number of times this
1751 * function has been called and doesn't remove the accelerator until
1752 * gtk_action_disconnect_accelerator() has been called as many times.
1757 gtk_action_connect_accelerator (GtkAction *action)
1759 g_return_if_fail (GTK_IS_ACTION (action));
1761 if (!action->private_data->accel_quark ||
1762 !action->private_data->accel_group)
1765 if (action->private_data->accel_count == 0)
1767 const gchar *accel_path =
1768 g_quark_to_string (action->private_data->accel_quark);
1770 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1772 action->private_data->accel_closure);
1775 action->private_data->accel_count++;
1779 * gtk_action_disconnect_accelerator:
1780 * @action: a #GtkAction
1782 * Undoes the effect of one call to gtk_action_connect_accelerator().
1787 gtk_action_disconnect_accelerator (GtkAction *action)
1789 g_return_if_fail (GTK_IS_ACTION (action));
1791 if (!action->private_data->accel_quark ||
1792 !action->private_data->accel_group)
1795 action->private_data->accel_count--;
1797 if (action->private_data->accel_count == 0)
1798 gtk_accel_group_disconnect (action->private_data->accel_group,
1799 action->private_data->accel_closure);
1803 * gtk_action_create_menu:
1804 * @action: a #GtkAction
1806 * If @action provides a #GtkMenu widget as a submenu for the menu
1807 * item or the toolbar item it creates, this function returns an
1808 * instance of that menu.
1810 * Return value: the menu item provided by the action, or %NULL.
1815 gtk_action_create_menu (GtkAction *action)
1817 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1819 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1820 return GTK_ACTION_GET_CLASS (action)->create_menu (action);
1825 #define __GTK_ACTION_C__
1826 #include "gtkaliasdef.c"