2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gtkaction.h"
34 #include "gtkactiongroup.h"
35 #include "gtkaccellabel.h"
36 #include "gtkbutton.h"
37 #include "gtkiconfactory.h"
39 #include "gtkimagemenuitem.h"
42 #include "gtkmarshalers.h"
43 #include "gtkmenuitem.h"
45 #include "gtktearoffmenuitem.h"
46 #include "gtktoolbutton.h"
47 #include "gtktoolbar.h"
48 #include "gtkprivate.h"
52 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
54 struct _GtkActionPrivate
60 gchar *stock_id; /* stock icon */
61 gchar *icon_name; /* themed icon */
65 guint label_set : 1; /* these two used so we can set label */
66 guint short_label_set : 1; /* based on stock id */
67 guint visible_horizontal : 1;
68 guint visible_vertical : 1;
69 guint is_important : 1;
70 guint hide_if_empty : 1;
71 guint visible_overflown : 1;
75 GtkAccelGroup *accel_group;
76 GClosure *accel_closure;
79 GtkActionGroup *action_group;
81 /* list of proxy widgets */
100 PROP_VISIBLE_HORIZONTAL,
101 PROP_VISIBLE_VERTICAL,
102 PROP_VISIBLE_OVERFLOWN,
111 static GQuark accel_path_id = 0;
112 static GQuark quark_gtk_action_proxy = 0;
113 static const gchar accel_path_key[] = "GtkAction::accel_path";
114 static const gchar gtk_action_proxy_key[] = "gtk-action";
116 G_DEFINE_TYPE (GtkAction, gtk_action, G_TYPE_OBJECT)
118 static void gtk_action_finalize (GObject *object);
119 static void gtk_action_set_property (GObject *object,
123 static void gtk_action_get_property (GObject *object,
127 static void gtk_action_set_action_group (GtkAction *action,
128 GtkActionGroup *action_group);
129 static void gtk_action_set_is_important (GtkAction *action,
130 gboolean is_important);
131 static void gtk_action_set_label (GtkAction *action,
133 static void gtk_action_set_short_label (GtkAction *action,
135 static void gtk_action_set_visible_horizontal (GtkAction *action,
136 gboolean visible_horizontal);
137 static void gtk_action_set_visible_vertical (GtkAction *action,
138 gboolean visible_vertical);
139 static void gtk_action_set_tooltip (GtkAction *action,
140 const gchar *tooltip);
141 static void gtk_action_set_stock_id (GtkAction *action,
142 const gchar *stock_id);
143 static void gtk_action_set_icon_name (GtkAction *action,
144 const gchar *icon_name);
145 static void gtk_action_sync_tooltip (GtkAction *action,
148 static GtkWidget *create_menu_item (GtkAction *action);
149 static GtkWidget *create_tool_item (GtkAction *action);
150 static void connect_proxy (GtkAction *action,
152 static void disconnect_proxy (GtkAction *action,
154 static void closure_accel_activate (GClosure *closure,
155 GValue *return_value,
156 guint n_param_values,
157 const GValue *param_values,
158 gpointer invocation_hint,
159 gpointer marshal_data);
161 static guint action_signals[LAST_SIGNAL] = { 0 };
165 gtk_action_class_init (GtkActionClass *klass)
167 GObjectClass *gobject_class;
169 accel_path_id = g_quark_from_static_string (accel_path_key);
170 quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
172 gobject_class = G_OBJECT_CLASS (klass);
174 gobject_class->finalize = gtk_action_finalize;
175 gobject_class->set_property = gtk_action_set_property;
176 gobject_class->get_property = gtk_action_get_property;
178 klass->activate = NULL;
180 klass->create_menu_item = create_menu_item;
181 klass->create_tool_item = create_tool_item;
182 klass->connect_proxy = connect_proxy;
183 klass->disconnect_proxy = disconnect_proxy;
185 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
186 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
188 g_object_class_install_property (gobject_class,
190 g_param_spec_string ("name",
192 P_("A unique name for the action."),
194 GTK_PARAM_READWRITE |
195 G_PARAM_CONSTRUCT_ONLY));
200 * The label used for menu items and buttons that activate
201 * this action. If the label is %NULL, GTK+ uses the stock
202 * label specified via the stock-id property.
204 g_object_class_install_property (gobject_class,
206 g_param_spec_string ("label",
208 P_("The label used for menu items and buttons "
209 "that activate this action."),
211 GTK_PARAM_READWRITE));
212 g_object_class_install_property (gobject_class,
214 g_param_spec_string ("short-label",
216 P_("A shorter label that may be used on toolbar buttons."),
218 GTK_PARAM_READWRITE));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("tooltip",
223 P_("A tooltip for this action."),
225 GTK_PARAM_READWRITE));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("stock-id",
230 P_("The stock icon displayed in widgets representing "
233 GTK_PARAM_READWRITE));
235 * GtkAction:icon-name:
237 * The name of the icon from the icon theme.
238 * Note that the stock icon is preferred, if
239 * the ::stock-id property holds the id of an
240 * existing stock icon.
244 g_object_class_install_property (gobject_class,
246 g_param_spec_string ("icon-name",
248 P_("The name of the icon from the icon theme"),
250 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
252 PROP_VISIBLE_HORIZONTAL,
253 g_param_spec_boolean ("visible-horizontal",
254 P_("Visible when horizontal"),
255 P_("Whether the toolbar item is visible when the toolbar "
256 "is in a horizontal orientation."),
258 GTK_PARAM_READWRITE));
260 * GtkAction:visible-overflown:
262 * When %TRUE, toolitem proxies for this action are represented in the
263 * toolbar overflow menu.
267 g_object_class_install_property (gobject_class,
268 PROP_VISIBLE_OVERFLOWN,
269 g_param_spec_boolean ("visible-overflown",
270 P_("Visible when overflown"),
271 P_("When TRUE, toolitem proxies for this action "
272 "are represented in the toolbar overflow menu."),
274 GTK_PARAM_READWRITE));
275 g_object_class_install_property (gobject_class,
276 PROP_VISIBLE_VERTICAL,
277 g_param_spec_boolean ("visible-vertical",
278 P_("Visible when vertical"),
279 P_("Whether the toolbar item is visible when the toolbar "
280 "is in a vertical orientation."),
282 GTK_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class,
285 g_param_spec_boolean ("is-important",
287 P_("Whether the action is considered important. "
288 "When TRUE, toolitem proxies for this action "
289 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
291 GTK_PARAM_READWRITE));
292 g_object_class_install_property (gobject_class,
294 g_param_spec_boolean ("hide-if-empty",
296 P_("When TRUE, empty menu proxies for this action are hidden."),
298 GTK_PARAM_READWRITE));
299 g_object_class_install_property (gobject_class,
301 g_param_spec_boolean ("sensitive",
303 P_("Whether the action is enabled."),
305 GTK_PARAM_READWRITE));
306 g_object_class_install_property (gobject_class,
308 g_param_spec_boolean ("visible",
310 P_("Whether the action is visible."),
312 GTK_PARAM_READWRITE));
313 g_object_class_install_property (gobject_class,
315 g_param_spec_object ("action-group",
317 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
318 GTK_TYPE_ACTION_GROUP,
319 GTK_PARAM_READWRITE));
322 * GtkAction::activate:
323 * @action: the #GtkAction
325 * The "activate" signal is emitted when the action is activated.
329 action_signals[ACTIVATE] =
330 g_signal_new (I_("activate"),
331 G_OBJECT_CLASS_TYPE (klass),
332 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
333 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
334 g_cclosure_marshal_VOID__VOID,
337 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
342 gtk_action_init (GtkAction *action)
344 action->private_data = GTK_ACTION_GET_PRIVATE (action);
346 action->private_data->name = NULL;
347 action->private_data->label = NULL;
348 action->private_data->short_label = NULL;
349 action->private_data->tooltip = NULL;
350 action->private_data->stock_id = NULL;
351 action->private_data->icon_name = NULL;
352 action->private_data->visible_horizontal = TRUE;
353 action->private_data->visible_vertical = TRUE;
354 action->private_data->visible_overflown = TRUE;
355 action->private_data->is_important = FALSE;
356 action->private_data->hide_if_empty = TRUE;
358 action->private_data->sensitive = TRUE;
359 action->private_data->visible = TRUE;
361 action->private_data->label_set = FALSE;
362 action->private_data->short_label_set = FALSE;
364 action->private_data->accel_count = 0;
365 action->private_data->accel_group = NULL;
366 action->private_data->accel_quark = 0;
367 action->private_data->accel_closure =
368 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
369 g_closure_set_marshal (action->private_data->accel_closure,
370 closure_accel_activate);
371 g_closure_ref (action->private_data->accel_closure);
372 g_closure_sink (action->private_data->accel_closure);
374 action->private_data->action_group = NULL;
376 action->private_data->proxies = NULL;
381 * @name: A unique name for the action
382 * @label: the label displayed in menu items and on buttons
383 * @tooltip: a tooltip for the action
384 * @stock_id: the stock icon to display in widgets representing the action
386 * Creates a new #GtkAction object. To add the action to a
387 * #GtkActionGroup and set the accelerator for the action,
388 * call gtk_action_group_add_action_with_accel().
389 * See <xref linkend="XML-UI"/> for information on allowed action
392 * Return value: a new #GtkAction
397 gtk_action_new (const gchar *name,
399 const gchar *tooltip,
400 const gchar *stock_id)
404 action = g_object_new (GTK_TYPE_ACTION,
408 "stock_id", stock_id,
415 gtk_action_finalize (GObject *object)
418 action = GTK_ACTION (object);
420 g_free (action->private_data->name);
421 g_free (action->private_data->label);
422 g_free (action->private_data->short_label);
423 g_free (action->private_data->tooltip);
424 g_free (action->private_data->stock_id);
425 g_free (action->private_data->icon_name);
427 g_closure_unref (action->private_data->accel_closure);
428 if (action->private_data->accel_group)
429 g_object_unref (action->private_data->accel_group);
431 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
435 gtk_action_set_property (GObject *object,
443 action = GTK_ACTION (object);
448 tmp = action->private_data->name;
449 action->private_data->name = g_value_dup_string (value);
453 gtk_action_set_label (action, g_value_get_string (value));
455 case PROP_SHORT_LABEL:
456 gtk_action_set_short_label (action, g_value_get_string (value));
459 gtk_action_set_tooltip (action, g_value_get_string (value));
462 gtk_action_set_stock_id (action, g_value_get_string (value));
465 gtk_action_set_icon_name (action, g_value_get_string (value));
467 case PROP_VISIBLE_HORIZONTAL:
468 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
470 case PROP_VISIBLE_VERTICAL:
471 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
473 case PROP_VISIBLE_OVERFLOWN:
474 action->private_data->visible_overflown = g_value_get_boolean (value);
476 case PROP_IS_IMPORTANT:
477 gtk_action_set_is_important (action, g_value_get_boolean (value));
479 case PROP_HIDE_IF_EMPTY:
480 action->private_data->hide_if_empty = g_value_get_boolean (value);
483 gtk_action_set_sensitive (action, g_value_get_boolean (value));
486 gtk_action_set_visible (action, g_value_get_boolean (value));
488 case PROP_ACTION_GROUP:
489 gtk_action_set_action_group (action, g_value_get_object (value));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_action_get_property (GObject *object,
505 action = GTK_ACTION (object);
510 g_value_set_string (value, action->private_data->name);
513 g_value_set_string (value, action->private_data->label);
515 case PROP_SHORT_LABEL:
516 g_value_set_string (value, action->private_data->short_label);
519 g_value_set_string (value, action->private_data->tooltip);
522 g_value_set_string (value, action->private_data->stock_id);
525 g_value_set_string (value, action->private_data->icon_name);
527 case PROP_VISIBLE_HORIZONTAL:
528 g_value_set_boolean (value, action->private_data->visible_horizontal);
530 case PROP_VISIBLE_VERTICAL:
531 g_value_set_boolean (value, action->private_data->visible_vertical);
533 case PROP_VISIBLE_OVERFLOWN:
534 g_value_set_boolean (value, action->private_data->visible_overflown);
536 case PROP_IS_IMPORTANT:
537 g_value_set_boolean (value, action->private_data->is_important);
539 case PROP_HIDE_IF_EMPTY:
540 g_value_set_boolean (value, action->private_data->hide_if_empty);
543 g_value_set_boolean (value, action->private_data->sensitive);
546 g_value_set_boolean (value, action->private_data->visible);
548 case PROP_ACTION_GROUP:
549 g_value_set_object (value, action->private_data->action_group);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 create_menu_item (GtkAction *action)
560 GType menu_item_type;
562 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
564 return g_object_new (menu_item_type, NULL);
568 create_tool_item (GtkAction *action)
570 GType toolbar_item_type;
572 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
574 return g_object_new (toolbar_item_type, NULL);
578 remove_proxy (GtkAction *action,
581 if (GTK_IS_MENU_ITEM (proxy))
582 gtk_action_disconnect_accelerator (action);
584 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
588 * _gtk_action_sync_menu_visible:
589 * @action: a #GtkAction, or %NULL to determine the action from @proxy
590 * @proxy: a proxy menu item
591 * @empty: whether the submenu attached to @proxy is empty
593 * Updates the visibility of @proxy from the visibility of @action
594 * according to the following rules:
596 * <listitem><para>if @action is invisible, @proxy is too
598 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
599 * property of @action indicates otherwise
603 * This function is used in the implementation of #GtkUIManager.
606 _gtk_action_sync_menu_visible (GtkAction *action,
610 gboolean visible, hide_if_empty;
612 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
613 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
616 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
618 visible = gtk_action_is_visible (action);
619 hide_if_empty = action->private_data->hide_if_empty;
621 if (visible && !(empty && hide_if_empty))
622 gtk_widget_show (proxy);
624 gtk_widget_hide (proxy);
627 gboolean _gtk_menu_is_empty (GtkWidget *menu);
630 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
633 GtkWidget *menu_item;
635 if (action->private_data->visible_overflown)
637 menu_item = gtk_action_create_menu_item (action);
639 g_object_ref_sink (menu_item);
641 gtk_tool_item_set_proxy_menu_item (tool_item,
642 "gtk-action-menu-item", menu_item);
643 g_object_unref (menu_item);
646 gtk_tool_item_set_proxy_menu_item (tool_item,
647 "gtk-action-menu-item", NULL);
653 connect_proxy (GtkAction *action,
656 g_object_ref (action);
657 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
660 /* add this widget to the list of proxies */
661 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
662 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
664 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
665 if (gtk_action_is_visible (action))
666 gtk_widget_show (proxy);
668 gtk_widget_hide (proxy);
669 gtk_widget_set_no_show_all (proxy, TRUE);
671 if (GTK_IS_MENU_ITEM (proxy))
674 /* menu item specific synchronisers ... */
676 if (action->private_data->accel_quark)
678 gtk_action_connect_accelerator (action);
679 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
680 g_quark_to_string (action->private_data->accel_quark));
683 label = GTK_BIN (proxy)->child;
685 /* make sure label is a label */
686 if (label && !GTK_IS_LABEL (label))
688 gtk_container_remove (GTK_CONTAINER (proxy), label);
693 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
694 "use-underline", TRUE,
700 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
702 "accel-closure", action->private_data->accel_closure,
705 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
707 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
711 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
712 if (image && !GTK_IS_IMAGE (image))
714 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
719 image = gtk_image_new ();
720 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
722 gtk_widget_show (image);
725 if (action->private_data->stock_id &&
726 gtk_icon_factory_lookup_default (action->private_data->stock_id))
727 gtk_image_set_from_stock (GTK_IMAGE (image),
728 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
729 else if (action->private_data->icon_name)
730 gtk_image_set_from_icon_name (GTK_IMAGE (image),
731 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
734 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
735 g_signal_connect_object (proxy, "activate",
736 G_CALLBACK (gtk_action_activate), action,
740 else if (GTK_IS_TOOL_ITEM (proxy))
742 /* toolbar button specific synchronisers ... */
743 if (GTK_IS_TOOL_BUTTON (proxy))
746 "visible-horizontal", action->private_data->visible_horizontal,
747 "visible-vertical", action->private_data->visible_vertical,
748 "is-important", action->private_data->is_important,
749 "label", action->private_data->short_label,
750 "use-underline", TRUE,
751 "stock-id", action->private_data->stock_id,
752 "icon-name", action->private_data->icon_name,
755 g_signal_connect_object (proxy, "clicked",
756 G_CALLBACK (gtk_action_activate), action,
762 "visible-horizontal", action->private_data->visible_horizontal,
763 "visible-vertical", action->private_data->visible_vertical,
764 "is-important", action->private_data->is_important,
768 gtk_action_sync_tooltip (action, proxy);
770 g_signal_connect_object (proxy, "create_menu_proxy",
771 G_CALLBACK (gtk_action_create_menu_proxy),
774 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
776 else if (GTK_IS_BUTTON (proxy))
778 /* button specific synchronisers ... */
779 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
781 /* synchronise stock-id */
783 "label", action->private_data->stock_id,
788 if (GTK_BIN (proxy)->child == NULL ||
789 GTK_IS_LABEL (GTK_BIN (proxy)->child))
791 /* synchronise the label */
793 "label", action->private_data->short_label,
794 "use-underline", TRUE,
798 /* we leave the button alone if there is a custom child */
799 g_signal_connect_object (proxy, "clicked",
800 G_CALLBACK (gtk_action_activate), action,
804 if (action->private_data->action_group)
805 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
809 disconnect_proxy (GtkAction *action,
812 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
814 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
815 remove_proxy (action, proxy);
817 /* disconnect the activate handler */
818 g_signal_handlers_disconnect_by_func (proxy,
819 G_CALLBACK (gtk_action_activate),
822 /* toolbar button specific synchronisers ... */
823 g_signal_handlers_disconnect_by_func (proxy,
824 G_CALLBACK (gtk_action_create_menu_proxy),
827 if (action->private_data->action_group)
828 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
832 _gtk_action_emit_activate (GtkAction *action)
834 GtkActionGroup *group = action->private_data->action_group;
838 g_object_ref (group);
839 _gtk_action_group_emit_pre_activate (group, action);
842 g_signal_emit (action, action_signals[ACTIVATE], 0);
846 _gtk_action_group_emit_post_activate (group, action);
847 g_object_unref (group);
852 * gtk_action_activate:
853 * @action: the action object
855 * Emits the "activate" signal on the specified action, if it isn't
856 * insensitive. This gets called by the proxy widgets when they get
859 * It can also be used to manually activate an action.
864 gtk_action_activate (GtkAction *action)
866 g_return_if_fail (GTK_IS_ACTION (action));
868 if (gtk_action_is_sensitive (action))
869 _gtk_action_emit_activate (action);
873 * gtk_action_create_icon:
874 * @action: the action object
875 * @icon_size: the size of the icon that should be created.
877 * This function is intended for use by action implementations to
878 * create icons displayed in the proxy widgets.
880 * Returns: a widget that displays the icon for this action.
885 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
887 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
889 if (action->private_data->stock_id)
890 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
891 else if (action->private_data->icon_name)
892 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
898 * gtk_action_create_menu_item:
899 * @action: the action object
901 * Creates a menu item widget that proxies for the given action.
903 * Returns: a menu item connected to the action.
908 gtk_action_create_menu_item (GtkAction *action)
910 GtkWidget *menu_item;
912 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
914 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
916 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
922 * gtk_action_create_tool_item:
923 * @action: the action object
925 * Creates a toolbar item widget that proxies for the given action.
927 * Returns: a toolbar item connected to the action.
932 gtk_action_create_tool_item (GtkAction *action)
936 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
938 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
940 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
946 * gtk_action_connect_proxy:
947 * @action: the action object
948 * @proxy: the proxy widget
950 * Connects a widget to an action object as a proxy. Synchronises
951 * various properties of the action with the widget (such as label
952 * text, icon, tooltip, etc), and attaches a callback so that the
953 * action gets activated when the proxy widget does.
955 * If the widget is already connected to an action, it is disconnected
961 gtk_action_connect_proxy (GtkAction *action,
964 GtkAction *prev_action;
966 g_return_if_fail (GTK_IS_ACTION (action));
967 g_return_if_fail (GTK_IS_WIDGET (proxy));
969 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
972 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
974 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
978 * gtk_action_disconnect_proxy:
979 * @action: the action object
980 * @proxy: the proxy widget
982 * Disconnects a proxy widget from an action.
983 * Does <emphasis>not</emphasis> destroy the widget, however.
988 gtk_action_disconnect_proxy (GtkAction *action,
991 g_return_if_fail (GTK_IS_ACTION (action));
992 g_return_if_fail (GTK_IS_WIDGET (proxy));
994 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
996 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1000 * gtk_action_get_proxies:
1001 * @action: the action object
1003 * Returns the proxy widgets for an action.
1004 * See also gtk_widget_get_action().
1006 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1007 * and must not be modified.
1012 gtk_action_get_proxies (GtkAction *action)
1014 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1016 return action->private_data->proxies;
1021 * gtk_widget_get_action:
1022 * @widget: a #GtkWidget
1024 * Returns the #GtkAction that @widget is a proxy for.
1025 * See also gtk_action_get_proxies().
1027 * Returns: the action that a widget is a proxy for, or
1028 * %NULL, if it is not attached to an action.
1033 gtk_widget_get_action (GtkWidget *widget)
1035 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1037 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1042 * gtk_action_get_name:
1043 * @action: the action object
1045 * Returns the name of the action.
1047 * Return value: the name of the action. The string belongs to GTK+ and should not
1052 G_CONST_RETURN gchar *
1053 gtk_action_get_name (GtkAction *action)
1055 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1057 return action->private_data->name;
1061 * gtk_action_is_sensitive:
1062 * @action: the action object
1064 * Returns whether the action is effectively sensitive.
1066 * Return value: %TRUE if the action and its associated action group
1067 * are both sensitive.
1072 gtk_action_is_sensitive (GtkAction *action)
1074 GtkActionPrivate *priv;
1075 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1077 priv = action->private_data;
1078 return priv->sensitive &&
1079 (priv->action_group == NULL ||
1080 gtk_action_group_get_sensitive (priv->action_group));
1084 * gtk_action_get_sensitive:
1085 * @action: the action object
1087 * Returns whether the action itself is sensitive. Note that this doesn't
1088 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1091 * Return value: %TRUE if the action itself is sensitive.
1096 gtk_action_get_sensitive (GtkAction *action)
1098 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1100 return action->private_data->sensitive;
1104 _gtk_action_sync_sensitive (GtkAction *action)
1110 sensitive = gtk_action_is_sensitive (action);
1112 for (p = action->private_data->proxies; p; p = p->next)
1114 proxy = (GtkWidget *)p->data;
1115 gtk_widget_set_sensitive (proxy, sensitive);
1120 * gtk_action_set_sensitive:
1121 * @action: the action object
1122 * @sensitive: %TRUE to make the action sensitive
1124 * Sets the ::sensitive property of the action to @sensitive. Note that
1125 * this doesn't necessarily mean effective sensitivity. See
1126 * gtk_action_is_sensitive()
1132 gtk_action_set_sensitive (GtkAction *action,
1135 g_return_if_fail (GTK_IS_ACTION (action));
1137 sensitive = sensitive != FALSE;
1139 if (action->private_data->sensitive != sensitive)
1141 action->private_data->sensitive = sensitive;
1143 _gtk_action_sync_sensitive (action);
1145 g_object_notify (G_OBJECT (action), "sensitive");
1150 * gtk_action_is_visible:
1151 * @action: the action object
1153 * Returns whether the action is effectively visible.
1155 * Return value: %TRUE if the action and its associated action group
1161 gtk_action_is_visible (GtkAction *action)
1163 GtkActionPrivate *priv;
1164 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1166 priv = action->private_data;
1167 return priv->visible &&
1168 (priv->action_group == NULL ||
1169 gtk_action_group_get_visible (priv->action_group));
1173 * gtk_action_get_visible:
1174 * @action: the action object
1176 * Returns whether the action itself is visible. Note that this doesn't
1177 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1180 * Return value: %TRUE if the action itself is visible.
1185 gtk_action_get_visible (GtkAction *action)
1187 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1189 return action->private_data->visible;
1193 _gtk_action_sync_visible (GtkAction *action)
1200 visible = gtk_action_is_visible (action);
1202 for (p = action->private_data->proxies; p; p = p->next)
1204 proxy = (GtkWidget *)p->data;
1206 if (GTK_IS_MENU_ITEM (proxy))
1208 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1210 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1215 gtk_widget_show (proxy);
1217 gtk_widget_hide (proxy);
1223 * gtk_action_set_visible:
1224 * @action: the action object
1225 * @visible: %TRUE to make the action visible
1227 * Sets the ::visible property of the action to @visible. Note that
1228 * this doesn't necessarily mean effective visibility. See
1229 * gtk_action_is_visible()
1235 gtk_action_set_visible (GtkAction *action,
1238 g_return_if_fail (GTK_IS_ACTION (action));
1240 visible = visible != FALSE;
1242 if (action->private_data->visible != visible)
1244 action->private_data->visible = visible;
1246 _gtk_action_sync_visible (action);
1248 g_object_notify (G_OBJECT (action), "visible");
1253 gtk_action_set_is_important (GtkAction *action,
1254 gboolean is_important)
1259 is_important = is_important != FALSE;
1261 if (action->private_data->is_important != is_important)
1263 action->private_data->is_important = is_important;
1265 for (p = action->private_data->proxies; p; p = p->next)
1267 proxy = (GtkWidget *)p->data;
1269 if (GTK_IS_TOOL_ITEM (proxy))
1270 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1274 g_object_notify (G_OBJECT (action), "is-important");
1279 gtk_action_set_label (GtkAction *action,
1283 GtkWidget *proxy, *child;
1286 tmp = action->private_data->label;
1287 action->private_data->label = g_strdup (label);
1289 action->private_data->label_set = (action->private_data->label != NULL);
1290 /* if label is unset, then use the label from the stock item */
1291 if (!action->private_data->label_set && action->private_data->stock_id)
1293 GtkStockItem stock_item;
1295 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1296 action->private_data->label = g_strdup (stock_item.label);
1299 for (p = action->private_data->proxies; p; p = p->next)
1301 proxy = (GtkWidget *)p->data;
1303 if (GTK_IS_MENU_ITEM (proxy))
1305 child = GTK_BIN (proxy)->child;
1307 if (GTK_IS_LABEL (child))
1308 gtk_label_set_label (GTK_LABEL (child),
1309 action->private_data->label);
1313 g_object_notify (G_OBJECT (action), "label");
1315 /* if short_label is unset, set short_label=label */
1316 if (!action->private_data->short_label_set)
1318 gtk_action_set_short_label (action, action->private_data->label);
1319 action->private_data->short_label_set = FALSE;
1324 gtk_action_set_short_label (GtkAction *action,
1328 GtkWidget *proxy, *child;
1331 tmp = action->private_data->short_label;
1332 action->private_data->short_label = g_strdup (label);
1334 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1335 /* if short_label is unset, then use the value of label */
1336 if (!action->private_data->short_label_set)
1337 action->private_data->short_label = g_strdup (action->private_data->label);
1339 for (p = action->private_data->proxies; p; p = p->next)
1341 proxy = (GtkWidget *)p->data;
1343 if (GTK_IS_TOOL_BUTTON (proxy))
1344 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1345 action->private_data->label);
1346 else if (GTK_IS_BUTTON (proxy) &&
1347 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1349 child = GTK_BIN (proxy)->child;
1351 if (child == NULL || GTK_IS_LABEL (child))
1352 gtk_button_set_label (GTK_BUTTON (proxy),
1353 action->private_data->label);
1357 g_object_notify (G_OBJECT (action), "short-label");
1361 gtk_action_set_visible_horizontal (GtkAction *action,
1362 gboolean visible_horizontal)
1367 visible_horizontal = visible_horizontal != FALSE;
1369 if (action->private_data->visible_horizontal != visible_horizontal)
1371 action->private_data->visible_horizontal = visible_horizontal;
1373 for (p = action->private_data->proxies; p; p = p->next)
1375 proxy = (GtkWidget *)p->data;
1377 if (GTK_IS_TOOL_ITEM (proxy))
1378 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1379 visible_horizontal);
1382 g_object_notify (G_OBJECT (action), "visible-horizontal");
1387 gtk_action_set_visible_vertical (GtkAction *action,
1388 gboolean visible_vertical)
1393 visible_vertical = visible_vertical != FALSE;
1395 if (action->private_data->visible_vertical != visible_vertical)
1397 action->private_data->visible_vertical = visible_vertical;
1399 for (p = action->private_data->proxies; p; p = p->next)
1401 proxy = (GtkWidget *)p->data;
1403 if (GTK_IS_TOOL_ITEM (proxy))
1404 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1408 g_object_notify (G_OBJECT (action), "visible-vertical");
1413 gtk_action_sync_tooltip (GtkAction *action,
1418 parent = gtk_widget_get_parent (proxy);
1420 if (GTK_IS_TOOL_ITEM (proxy) && GTK_IS_TOOLBAR (parent))
1421 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
1422 GTK_TOOLBAR (parent)->tooltips,
1423 action->private_data->tooltip,
1428 gtk_action_set_tooltip (GtkAction *action,
1429 const gchar *tooltip)
1435 tmp = action->private_data->tooltip;
1436 action->private_data->tooltip = g_strdup (tooltip);
1439 for (p = action->private_data->proxies; p; p = p->next)
1441 proxy = (GtkWidget *)p->data;
1443 gtk_action_sync_tooltip (action, proxy);
1446 g_object_notify (G_OBJECT (action), "tooltip");
1450 gtk_action_set_stock_id (GtkAction *action,
1451 const gchar *stock_id)
1454 GtkWidget *proxy, *image;
1457 tmp = action->private_data->stock_id;
1458 action->private_data->stock_id = g_strdup (stock_id);
1461 for (p = action->private_data->proxies; p; p = p->next)
1463 proxy = (GtkWidget *)p->data;
1465 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1467 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1469 if (GTK_IS_IMAGE (image))
1470 gtk_image_set_from_stock (GTK_IMAGE (image),
1471 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1473 else if (GTK_IS_TOOL_BUTTON (proxy))
1475 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1476 action->private_data->stock_id);
1478 else if (GTK_IS_BUTTON (proxy) &&
1479 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1481 gtk_button_set_label (GTK_BUTTON (proxy),
1482 action->private_data->stock_id);
1486 g_object_notify (G_OBJECT (action), "stock-id");
1488 /* update label and short_label if appropriate */
1489 if (!action->private_data->label_set)
1491 GtkStockItem stock_item;
1493 if (action->private_data->stock_id &&
1494 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1495 gtk_action_set_label (action, stock_item.label);
1497 gtk_action_set_label (action, NULL);
1499 action->private_data->label_set = FALSE;
1504 gtk_action_set_icon_name (GtkAction *action,
1505 const gchar *icon_name)
1508 GtkWidget *proxy, *image;
1511 tmp = action->private_data->icon_name;
1512 action->private_data->icon_name = g_strdup (icon_name);
1515 for (p = action->private_data->proxies; p; p = p->next)
1517 proxy = (GtkWidget *)p->data;
1519 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1521 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1523 if (GTK_IS_IMAGE (image) &&
1524 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1525 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1526 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1527 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1529 else if (GTK_IS_TOOL_BUTTON (proxy))
1531 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1532 action->private_data->icon_name);
1534 else if (GTK_IS_BUTTON (proxy) &&
1535 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1537 image = gtk_button_get_image (GTK_BUTTON (proxy));
1539 if (GTK_IS_IMAGE (image) &&
1540 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1541 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1542 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1543 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1547 g_object_notify (G_OBJECT (action), "icon-name");
1552 * gtk_action_block_activate_from:
1553 * @action: the action object
1554 * @proxy: a proxy widget
1556 * Disables calls to the gtk_action_activate()
1557 * function by signals on the given proxy widget. This is used to
1558 * break notification loops for things like check or radio actions.
1560 * This function is intended for use by action implementations.
1565 gtk_action_block_activate_from (GtkAction *action,
1568 g_return_if_fail (GTK_IS_ACTION (action));
1570 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1575 * gtk_action_unblock_activate_from:
1576 * @action: the action object
1577 * @proxy: a proxy widget
1579 * Re-enables calls to the gtk_action_activate()
1580 * function by signals on the given proxy widget. This undoes the
1581 * blocking done by gtk_action_block_activate_from().
1583 * This function is intended for use by action implementations.
1588 gtk_action_unblock_activate_from (GtkAction *action,
1591 g_return_if_fail (GTK_IS_ACTION (action));
1593 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1598 closure_accel_activate (GClosure *closure,
1599 GValue *return_value,
1600 guint n_param_values,
1601 const GValue *param_values,
1602 gpointer invocation_hint,
1603 gpointer marshal_data)
1605 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1607 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1609 /* we handled the accelerator */
1610 g_value_set_boolean (return_value, TRUE);
1615 gtk_action_set_action_group (GtkAction *action,
1616 GtkActionGroup *action_group)
1618 g_return_if_fail (GTK_IS_ACTION (action));
1620 if (action->private_data->action_group == NULL)
1621 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1623 g_return_if_fail (action_group == NULL);
1625 action->private_data->action_group = action_group;
1629 * gtk_action_set_accel_path:
1630 * @action: the action object
1631 * @accel_path: the accelerator path
1633 * Sets the accel path for this action. All proxy widgets associated
1634 * with the action will have this accel path, so that their
1635 * accelerators are consistent.
1640 gtk_action_set_accel_path (GtkAction *action,
1641 const gchar *accel_path)
1643 g_return_if_fail (GTK_IS_ACTION (action));
1645 action->private_data->accel_quark = g_quark_from_string (accel_path);
1649 * gtk_action_get_accel_path:
1650 * @action: the action object
1652 * Returns the accel path for this action.
1656 * Returns: the accel path for this action, or %NULL
1657 * if none is set. The returned string is owned by GTK+
1658 * and must not be freed or modified.
1660 G_CONST_RETURN gchar *
1661 gtk_action_get_accel_path (GtkAction *action)
1663 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1665 if (action->private_data->accel_quark)
1666 return g_quark_to_string (action->private_data->accel_quark);
1672 * gtk_action_get_accel_closure:
1673 * @action: the action object
1675 * Returns the accel closure for this action.
1679 * Returns: the accel closure for this action. The returned closure is
1680 * owned by GTK+ and must not be unreffed or modified.
1683 gtk_action_get_accel_closure (GtkAction *action)
1685 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1687 return action->private_data->accel_closure;
1692 * gtk_action_set_accel_group:
1693 * @action: the action object
1694 * @accel_group: a #GtkAccelGroup or %NULL
1696 * Sets the #GtkAccelGroup in which the accelerator for this action
1697 * will be installed.
1702 gtk_action_set_accel_group (GtkAction *action,
1703 GtkAccelGroup *accel_group)
1705 g_return_if_fail (GTK_IS_ACTION (action));
1706 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1709 g_object_ref (accel_group);
1710 if (action->private_data->accel_group)
1711 g_object_unref (action->private_data->accel_group);
1713 action->private_data->accel_group = accel_group;
1717 * gtk_action_connect_accelerator:
1718 * @action: a #GtkAction
1720 * Installs the accelerator for @action if @action has an
1721 * accel path and group. See gtk_action_set_accel_path() and
1722 * gtk_action_set_accel_group()
1724 * Since multiple proxies may independently trigger the installation
1725 * of the accelerator, the @action counts the number of times this
1726 * function has been called and doesn't remove the accelerator until
1727 * gtk_action_disconnect_accelerator() has been called as many times.
1732 gtk_action_connect_accelerator (GtkAction *action)
1734 g_return_if_fail (GTK_IS_ACTION (action));
1736 if (!action->private_data->accel_quark ||
1737 !action->private_data->accel_group)
1740 if (action->private_data->accel_count == 0)
1742 const gchar *accel_path =
1743 g_quark_to_string (action->private_data->accel_quark);
1745 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1747 action->private_data->accel_closure);
1750 action->private_data->accel_count++;
1754 * gtk_action_disconnect_accelerator:
1755 * @action: a #GtkAction
1757 * Undoes the effect of one call to gtk_action_connect_accelerator().
1762 gtk_action_disconnect_accelerator (GtkAction *action)
1764 g_return_if_fail (GTK_IS_ACTION (action));
1766 if (!action->private_data->accel_quark ||
1767 !action->private_data->accel_group)
1770 action->private_data->accel_count--;
1772 if (action->private_data->accel_count == 0)
1773 gtk_accel_group_disconnect (action->private_data->accel_group,
1774 action->private_data->accel_closure);
1777 #define __GTK_ACTION_C__
1778 #include "gtkaliasdef.c"