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, or %NULL
418 * @tooltip: a tooltip for the action, or %NULL
419 * @stock_id: the stock icon to display in widgets representing the
422 * Creates a new #GtkAction object. To add the action to a
423 * #GtkActionGroup and set the accelerator for the action,
424 * call gtk_action_group_add_action_with_accel().
425 * See <xref linkend="XML-UI"/> for information on allowed action
428 * Return value: a new #GtkAction
433 gtk_action_new (const gchar *name,
435 const gchar *tooltip,
436 const gchar *stock_id)
438 g_return_val_if_fail (name != NULL, NULL);
440 return g_object_new (GTK_TYPE_ACTION,
444 "stock-id", stock_id,
449 gtk_action_finalize (GObject *object)
452 action = GTK_ACTION (object);
454 g_free (action->private_data->label);
455 g_free (action->private_data->short_label);
456 g_free (action->private_data->tooltip);
457 g_free (action->private_data->stock_id);
458 g_free (action->private_data->icon_name);
460 g_closure_unref (action->private_data->accel_closure);
461 if (action->private_data->accel_group)
462 g_object_unref (action->private_data->accel_group);
464 G_OBJECT_CLASS (gtk_action_parent_class)->finalize (object);
468 gtk_action_set_property (GObject *object,
475 action = GTK_ACTION (object);
480 action->private_data->name = g_intern_string (g_value_get_string (value));
483 gtk_action_set_label (action, g_value_get_string (value));
485 case PROP_SHORT_LABEL:
486 gtk_action_set_short_label (action, g_value_get_string (value));
489 gtk_action_set_tooltip (action, g_value_get_string (value));
492 gtk_action_set_stock_id (action, g_value_get_string (value));
495 gtk_action_set_icon_name (action, g_value_get_string (value));
497 case PROP_VISIBLE_HORIZONTAL:
498 gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
500 case PROP_VISIBLE_VERTICAL:
501 gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
503 case PROP_VISIBLE_OVERFLOWN:
504 action->private_data->visible_overflown = g_value_get_boolean (value);
506 case PROP_IS_IMPORTANT:
507 gtk_action_set_is_important (action, g_value_get_boolean (value));
509 case PROP_HIDE_IF_EMPTY:
510 action->private_data->hide_if_empty = g_value_get_boolean (value);
513 gtk_action_set_sensitive (action, g_value_get_boolean (value));
516 gtk_action_set_visible (action, g_value_get_boolean (value));
518 case PROP_ACTION_GROUP:
519 gtk_action_set_action_group (action, g_value_get_object (value));
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gtk_action_get_property (GObject *object,
535 action = GTK_ACTION (object);
540 g_value_set_static_string (value, action->private_data->name);
543 g_value_set_string (value, action->private_data->label);
545 case PROP_SHORT_LABEL:
546 g_value_set_string (value, action->private_data->short_label);
549 g_value_set_string (value, action->private_data->tooltip);
552 g_value_set_string (value, action->private_data->stock_id);
555 g_value_set_string (value, action->private_data->icon_name);
557 case PROP_VISIBLE_HORIZONTAL:
558 g_value_set_boolean (value, action->private_data->visible_horizontal);
560 case PROP_VISIBLE_VERTICAL:
561 g_value_set_boolean (value, action->private_data->visible_vertical);
563 case PROP_VISIBLE_OVERFLOWN:
564 g_value_set_boolean (value, action->private_data->visible_overflown);
566 case PROP_IS_IMPORTANT:
567 g_value_set_boolean (value, action->private_data->is_important);
569 case PROP_HIDE_IF_EMPTY:
570 g_value_set_boolean (value, action->private_data->hide_if_empty);
573 g_value_set_boolean (value, action->private_data->sensitive);
576 g_value_set_boolean (value, action->private_data->visible);
578 case PROP_ACTION_GROUP:
579 g_value_set_object (value, action->private_data->action_group);
582 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
588 create_menu_item (GtkAction *action)
590 GType menu_item_type;
592 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
594 return g_object_new (menu_item_type, NULL);
598 create_tool_item (GtkAction *action)
600 GType toolbar_item_type;
602 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
604 return g_object_new (toolbar_item_type, NULL);
608 remove_proxy (GtkAction *action,
611 if (GTK_IS_MENU_ITEM (proxy))
612 gtk_action_disconnect_accelerator (action);
614 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
618 * _gtk_action_sync_menu_visible:
619 * @action: a #GtkAction, or %NULL to determine the action from @proxy
620 * @proxy: a proxy menu item
621 * @empty: whether the submenu attached to @proxy is empty
623 * Updates the visibility of @proxy from the visibility of @action
624 * according to the following rules:
626 * <listitem><para>if @action is invisible, @proxy is too
628 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
629 * property of @action indicates otherwise
633 * This function is used in the implementation of #GtkUIManager.
636 _gtk_action_sync_menu_visible (GtkAction *action,
640 gboolean visible, hide_if_empty;
642 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
643 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
646 action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
648 visible = gtk_action_is_visible (action);
649 hide_if_empty = action->private_data->hide_if_empty;
651 if (visible && !(empty && hide_if_empty))
652 gtk_widget_show (proxy);
654 gtk_widget_hide (proxy);
657 gboolean _gtk_menu_is_empty (GtkWidget *menu);
660 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
663 GtkWidget *menu_item;
665 if (action->private_data->visible_overflown)
667 menu_item = gtk_action_create_menu_item (action);
669 g_object_ref_sink (menu_item);
671 gtk_tool_item_set_proxy_menu_item (tool_item,
672 "gtk-action-menu-item", menu_item);
673 g_object_unref (menu_item);
676 gtk_tool_item_set_proxy_menu_item (tool_item,
677 "gtk-action-menu-item", NULL);
683 connect_proxy (GtkAction *action,
686 g_object_ref (action);
687 g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
690 /* add this widget to the list of proxies */
691 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
692 g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
694 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
695 if (gtk_action_is_visible (action))
696 gtk_widget_show (proxy);
698 gtk_widget_hide (proxy);
699 gtk_widget_set_no_show_all (proxy, TRUE);
701 if (GTK_IS_MENU_ITEM (proxy))
704 /* menu item specific synchronisers ... */
706 if (action->private_data->accel_quark)
708 gtk_action_connect_accelerator (action);
709 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
710 g_quark_to_string (action->private_data->accel_quark));
713 label = GTK_BIN (proxy)->child;
715 /* make sure label is a label */
716 if (label && !GTK_IS_LABEL (label))
718 gtk_container_remove (GTK_CONTAINER (proxy), label);
723 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
724 "use-underline", TRUE,
730 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
732 "accel-closure", action->private_data->accel_closure,
735 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
737 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
741 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
742 if (image && !GTK_IS_IMAGE (image))
744 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
749 image = gtk_image_new ();
750 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
752 gtk_widget_show (image);
755 if (action->private_data->stock_id &&
756 gtk_icon_factory_lookup_default (action->private_data->stock_id))
757 gtk_image_set_from_stock (GTK_IMAGE (image),
758 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
759 else if (action->private_data->icon_name)
760 gtk_image_set_from_icon_name (GTK_IMAGE (image),
761 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
764 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
765 g_signal_connect_object (proxy, "activate",
766 G_CALLBACK (gtk_action_activate), action,
770 else if (GTK_IS_TOOL_ITEM (proxy))
772 /* toolbar button specific synchronisers ... */
773 if (GTK_IS_TOOL_BUTTON (proxy))
776 "visible-horizontal", action->private_data->visible_horizontal,
777 "visible-vertical", action->private_data->visible_vertical,
778 "is-important", action->private_data->is_important,
779 "label", action->private_data->short_label,
780 "use-underline", TRUE,
781 "stock-id", action->private_data->stock_id,
782 "icon-name", action->private_data->icon_name,
785 g_signal_connect_object (proxy, "clicked",
786 G_CALLBACK (gtk_action_activate), action,
792 "visible-horizontal", action->private_data->visible_horizontal,
793 "visible-vertical", action->private_data->visible_vertical,
794 "is-important", action->private_data->is_important,
798 gtk_action_sync_tooltip (action, proxy);
800 g_signal_connect_object (proxy, "create_menu_proxy",
801 G_CALLBACK (gtk_action_create_menu_proxy),
804 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
806 else if (GTK_IS_BUTTON (proxy))
808 /* button specific synchronisers ... */
809 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
811 /* synchronise stock-id */
813 "label", action->private_data->stock_id,
818 if (GTK_BIN (proxy)->child == NULL ||
819 GTK_IS_LABEL (GTK_BIN (proxy)->child))
821 /* synchronise the label */
823 "label", action->private_data->short_label,
824 "use-underline", TRUE,
828 /* we leave the button alone if there is a custom child */
829 g_signal_connect_object (proxy, "clicked",
830 G_CALLBACK (gtk_action_activate), action,
834 if (action->private_data->action_group)
835 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
839 disconnect_proxy (GtkAction *action,
842 g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
844 g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
845 remove_proxy (action, proxy);
847 /* disconnect the activate handler */
848 g_signal_handlers_disconnect_by_func (proxy,
849 G_CALLBACK (gtk_action_activate),
852 /* toolbar button specific synchronisers ... */
853 g_signal_handlers_disconnect_by_func (proxy,
854 G_CALLBACK (gtk_action_create_menu_proxy),
857 if (action->private_data->action_group)
858 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
862 _gtk_action_emit_activate (GtkAction *action)
864 GtkActionGroup *group = action->private_data->action_group;
868 g_object_ref (group);
869 _gtk_action_group_emit_pre_activate (group, action);
872 g_signal_emit (action, action_signals[ACTIVATE], 0);
876 _gtk_action_group_emit_post_activate (group, action);
877 g_object_unref (group);
882 * gtk_action_activate:
883 * @action: the action object
885 * Emits the "activate" signal on the specified action, if it isn't
886 * insensitive. This gets called by the proxy widgets when they get
889 * It can also be used to manually activate an action.
894 gtk_action_activate (GtkAction *action)
896 g_return_if_fail (GTK_IS_ACTION (action));
898 if (gtk_action_is_sensitive (action))
899 _gtk_action_emit_activate (action);
903 * gtk_action_create_icon:
904 * @action: the action object
905 * @icon_size: the size of the icon that should be created.
907 * This function is intended for use by action implementations to
908 * create icons displayed in the proxy widgets.
910 * Returns: a widget that displays the icon for this action.
915 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
917 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
919 if (action->private_data->stock_id)
920 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
921 else if (action->private_data->icon_name)
922 return gtk_image_new_from_icon_name (action->private_data->icon_name, icon_size);
928 * gtk_action_create_menu_item:
929 * @action: the action object
931 * Creates a menu item widget that proxies for the given action.
933 * Returns: a menu item connected to the action.
938 gtk_action_create_menu_item (GtkAction *action)
940 GtkWidget *menu_item;
942 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
944 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
946 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
952 * gtk_action_create_tool_item:
953 * @action: the action object
955 * Creates a toolbar item widget that proxies for the given action.
957 * Returns: a toolbar item connected to the action.
962 gtk_action_create_tool_item (GtkAction *action)
966 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
968 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
970 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
976 * gtk_action_connect_proxy:
977 * @action: the action object
978 * @proxy: the proxy widget
980 * Connects a widget to an action object as a proxy. Synchronises
981 * various properties of the action with the widget (such as label
982 * text, icon, tooltip, etc), and attaches a callback so that the
983 * action gets activated when the proxy widget does.
985 * If the widget is already connected to an action, it is disconnected
991 gtk_action_connect_proxy (GtkAction *action,
994 GtkAction *prev_action;
996 g_return_if_fail (GTK_IS_ACTION (action));
997 g_return_if_fail (GTK_IS_WIDGET (proxy));
999 prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
1002 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1004 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1008 * gtk_action_disconnect_proxy:
1009 * @action: the action object
1010 * @proxy: the proxy widget
1012 * Disconnects a proxy widget from an action.
1013 * Does <emphasis>not</emphasis> destroy the widget, however.
1018 gtk_action_disconnect_proxy (GtkAction *action,
1021 g_return_if_fail (GTK_IS_ACTION (action));
1022 g_return_if_fail (GTK_IS_WIDGET (proxy));
1024 g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
1026 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1030 * gtk_action_get_proxies:
1031 * @action: the action object
1033 * Returns the proxy widgets for an action.
1034 * See also gtk_widget_get_action().
1036 * Return value: a #GSList of proxy widgets. The list is owned by GTK+
1037 * and must not be modified.
1042 gtk_action_get_proxies (GtkAction *action)
1044 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1046 return action->private_data->proxies;
1051 * gtk_widget_get_action:
1052 * @widget: a #GtkWidget
1054 * Returns the #GtkAction that @widget is a proxy for.
1055 * See also gtk_action_get_proxies().
1057 * Returns: the action that a widget is a proxy for, or
1058 * %NULL, if it is not attached to an action.
1063 gtk_widget_get_action (GtkWidget *widget)
1065 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1067 return g_object_get_qdata (G_OBJECT (widget), quark_gtk_action_proxy);
1072 * gtk_action_get_name:
1073 * @action: the action object
1075 * Returns the name of the action.
1077 * Return value: the name of the action. The string belongs to GTK+ and should not
1082 G_CONST_RETURN gchar *
1083 gtk_action_get_name (GtkAction *action)
1085 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1087 return action->private_data->name;
1091 * gtk_action_is_sensitive:
1092 * @action: the action object
1094 * Returns whether the action is effectively sensitive.
1096 * Return value: %TRUE if the action and its associated action group
1097 * are both sensitive.
1102 gtk_action_is_sensitive (GtkAction *action)
1104 GtkActionPrivate *priv;
1105 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1107 priv = action->private_data;
1108 return priv->sensitive &&
1109 (priv->action_group == NULL ||
1110 gtk_action_group_get_sensitive (priv->action_group));
1114 * gtk_action_get_sensitive:
1115 * @action: the action object
1117 * Returns whether the action itself is sensitive. Note that this doesn't
1118 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1121 * Return value: %TRUE if the action itself is sensitive.
1126 gtk_action_get_sensitive (GtkAction *action)
1128 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1130 return action->private_data->sensitive;
1134 _gtk_action_sync_sensitive (GtkAction *action)
1140 sensitive = gtk_action_is_sensitive (action);
1142 for (p = action->private_data->proxies; p; p = p->next)
1144 proxy = (GtkWidget *)p->data;
1145 gtk_widget_set_sensitive (proxy, sensitive);
1150 * gtk_action_set_sensitive:
1151 * @action: the action object
1152 * @sensitive: %TRUE to make the action sensitive
1154 * Sets the ::sensitive property of the action to @sensitive. Note that
1155 * this doesn't necessarily mean effective sensitivity. See
1156 * gtk_action_is_sensitive()
1162 gtk_action_set_sensitive (GtkAction *action,
1165 g_return_if_fail (GTK_IS_ACTION (action));
1167 sensitive = sensitive != FALSE;
1169 if (action->private_data->sensitive != sensitive)
1171 action->private_data->sensitive = sensitive;
1173 _gtk_action_sync_sensitive (action);
1175 g_object_notify (G_OBJECT (action), "sensitive");
1180 * gtk_action_is_visible:
1181 * @action: the action object
1183 * Returns whether the action is effectively visible.
1185 * Return value: %TRUE if the action and its associated action group
1191 gtk_action_is_visible (GtkAction *action)
1193 GtkActionPrivate *priv;
1194 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1196 priv = action->private_data;
1197 return priv->visible &&
1198 (priv->action_group == NULL ||
1199 gtk_action_group_get_visible (priv->action_group));
1203 * gtk_action_get_visible:
1204 * @action: the action object
1206 * Returns whether the action itself is visible. Note that this doesn't
1207 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1210 * Return value: %TRUE if the action itself is visible.
1215 gtk_action_get_visible (GtkAction *action)
1217 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1219 return action->private_data->visible;
1223 _gtk_action_sync_visible (GtkAction *action)
1230 visible = gtk_action_is_visible (action);
1232 for (p = action->private_data->proxies; p; p = p->next)
1234 proxy = (GtkWidget *)p->data;
1236 if (GTK_IS_MENU_ITEM (proxy))
1238 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
1240 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
1245 gtk_widget_show (proxy);
1247 gtk_widget_hide (proxy);
1253 * gtk_action_set_visible:
1254 * @action: the action object
1255 * @visible: %TRUE to make the action visible
1257 * Sets the ::visible property of the action to @visible. Note that
1258 * this doesn't necessarily mean effective visibility. See
1259 * gtk_action_is_visible()
1265 gtk_action_set_visible (GtkAction *action,
1268 g_return_if_fail (GTK_IS_ACTION (action));
1270 visible = visible != FALSE;
1272 if (action->private_data->visible != visible)
1274 action->private_data->visible = visible;
1276 _gtk_action_sync_visible (action);
1278 g_object_notify (G_OBJECT (action), "visible");
1283 gtk_action_set_is_important (GtkAction *action,
1284 gboolean is_important)
1289 is_important = is_important != FALSE;
1291 if (action->private_data->is_important != is_important)
1293 action->private_data->is_important = is_important;
1295 for (p = action->private_data->proxies; p; p = p->next)
1297 proxy = (GtkWidget *)p->data;
1299 if (GTK_IS_TOOL_ITEM (proxy))
1300 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (proxy),
1304 g_object_notify (G_OBJECT (action), "is-important");
1309 gtk_action_set_label (GtkAction *action,
1313 GtkWidget *proxy, *child;
1316 tmp = action->private_data->label;
1317 action->private_data->label = g_strdup (label);
1319 action->private_data->label_set = (action->private_data->label != NULL);
1320 /* if label is unset, then use the label from the stock item */
1321 if (!action->private_data->label_set && action->private_data->stock_id)
1323 GtkStockItem stock_item;
1325 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1326 action->private_data->label = g_strdup (stock_item.label);
1329 for (p = action->private_data->proxies; p; p = p->next)
1331 proxy = (GtkWidget *)p->data;
1333 if (GTK_IS_MENU_ITEM (proxy))
1335 child = GTK_BIN (proxy)->child;
1337 if (GTK_IS_LABEL (child))
1338 gtk_label_set_label (GTK_LABEL (child),
1339 action->private_data->label);
1343 g_object_notify (G_OBJECT (action), "label");
1345 /* if short_label is unset, set short_label=label */
1346 if (!action->private_data->short_label_set)
1348 gtk_action_set_short_label (action, action->private_data->label);
1349 action->private_data->short_label_set = FALSE;
1354 gtk_action_set_short_label (GtkAction *action,
1358 GtkWidget *proxy, *child;
1361 tmp = action->private_data->short_label;
1362 action->private_data->short_label = g_strdup (label);
1364 action->private_data->short_label_set = (action->private_data->short_label != NULL);
1365 /* if short_label is unset, then use the value of label */
1366 if (!action->private_data->short_label_set)
1367 action->private_data->short_label = g_strdup (action->private_data->label);
1369 for (p = action->private_data->proxies; p; p = p->next)
1371 proxy = (GtkWidget *)p->data;
1373 if (GTK_IS_TOOL_BUTTON (proxy))
1374 gtk_tool_button_set_label (GTK_TOOL_BUTTON (proxy),
1375 action->private_data->short_label);
1376 else if (GTK_IS_BUTTON (proxy) &&
1377 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1379 child = GTK_BIN (proxy)->child;
1381 if (child == NULL || GTK_IS_LABEL (child))
1382 gtk_button_set_label (GTK_BUTTON (proxy),
1383 action->private_data->short_label);
1387 g_object_notify (G_OBJECT (action), "short-label");
1391 gtk_action_set_visible_horizontal (GtkAction *action,
1392 gboolean visible_horizontal)
1397 visible_horizontal = visible_horizontal != FALSE;
1399 if (action->private_data->visible_horizontal != visible_horizontal)
1401 action->private_data->visible_horizontal = visible_horizontal;
1403 for (p = action->private_data->proxies; p; p = p->next)
1405 proxy = (GtkWidget *)p->data;
1407 if (GTK_IS_TOOL_ITEM (proxy))
1408 gtk_tool_item_set_visible_horizontal (GTK_TOOL_ITEM (proxy),
1409 visible_horizontal);
1412 g_object_notify (G_OBJECT (action), "visible-horizontal");
1417 gtk_action_set_visible_vertical (GtkAction *action,
1418 gboolean visible_vertical)
1423 visible_vertical = visible_vertical != FALSE;
1425 if (action->private_data->visible_vertical != visible_vertical)
1427 action->private_data->visible_vertical = visible_vertical;
1429 for (p = action->private_data->proxies; p; p = p->next)
1431 proxy = (GtkWidget *)p->data;
1433 if (GTK_IS_TOOL_ITEM (proxy))
1434 gtk_tool_item_set_visible_vertical (GTK_TOOL_ITEM (proxy),
1438 g_object_notify (G_OBJECT (action), "visible-vertical");
1443 gtk_action_sync_tooltip (GtkAction *action,
1446 gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (proxy),
1447 action->private_data->tooltip);
1451 gtk_action_set_tooltip (GtkAction *action,
1452 const gchar *tooltip)
1458 tmp = action->private_data->tooltip;
1459 action->private_data->tooltip = g_strdup (tooltip);
1462 for (p = action->private_data->proxies; p; p = p->next)
1464 proxy = (GtkWidget *)p->data;
1466 if (GTK_IS_TOOL_ITEM (proxy))
1467 gtk_action_sync_tooltip (action, proxy);
1470 g_object_notify (G_OBJECT (action), "tooltip");
1474 gtk_action_set_stock_id (GtkAction *action,
1475 const gchar *stock_id)
1478 GtkWidget *proxy, *image;
1481 tmp = action->private_data->stock_id;
1482 action->private_data->stock_id = g_strdup (stock_id);
1485 for (p = action->private_data->proxies; p; p = p->next)
1487 proxy = (GtkWidget *)p->data;
1489 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1491 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1493 if (GTK_IS_IMAGE (image))
1494 gtk_image_set_from_stock (GTK_IMAGE (image),
1495 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
1497 else if (GTK_IS_TOOL_BUTTON (proxy))
1499 gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (proxy),
1500 action->private_data->stock_id);
1502 else if (GTK_IS_BUTTON (proxy) &&
1503 gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1505 gtk_button_set_label (GTK_BUTTON (proxy),
1506 action->private_data->stock_id);
1510 g_object_notify (G_OBJECT (action), "stock-id");
1512 /* update label and short_label if appropriate */
1513 if (!action->private_data->label_set)
1515 GtkStockItem stock_item;
1517 if (action->private_data->stock_id &&
1518 gtk_stock_lookup (action->private_data->stock_id, &stock_item))
1519 gtk_action_set_label (action, stock_item.label);
1521 gtk_action_set_label (action, NULL);
1523 action->private_data->label_set = FALSE;
1528 gtk_action_set_icon_name (GtkAction *action,
1529 const gchar *icon_name)
1532 GtkWidget *proxy, *image;
1535 tmp = action->private_data->icon_name;
1536 action->private_data->icon_name = g_strdup (icon_name);
1539 for (p = action->private_data->proxies; p; p = p->next)
1541 proxy = (GtkWidget *)p->data;
1543 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
1545 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
1547 if (GTK_IS_IMAGE (image) &&
1548 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1549 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1550 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1551 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1553 else if (GTK_IS_TOOL_BUTTON (proxy))
1555 gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (proxy),
1556 action->private_data->icon_name);
1558 else if (GTK_IS_BUTTON (proxy) &&
1559 !gtk_button_get_use_stock (GTK_BUTTON (proxy)))
1561 image = gtk_button_get_image (GTK_BUTTON (proxy));
1563 if (GTK_IS_IMAGE (image) &&
1564 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
1565 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1566 gtk_image_set_from_icon_name (GTK_IMAGE (image),
1567 action->private_data->icon_name, GTK_ICON_SIZE_MENU);
1571 g_object_notify (G_OBJECT (action), "icon-name");
1576 * gtk_action_block_activate_from:
1577 * @action: the action object
1578 * @proxy: a proxy widget
1580 * Disables calls to the gtk_action_activate()
1581 * function by signals on the given proxy widget. This is used to
1582 * break notification loops for things like check or radio actions.
1584 * This function is intended for use by action implementations.
1589 gtk_action_block_activate_from (GtkAction *action,
1592 g_return_if_fail (GTK_IS_ACTION (action));
1594 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1599 * gtk_action_unblock_activate_from:
1600 * @action: the action object
1601 * @proxy: a proxy widget
1603 * Re-enables calls to the gtk_action_activate()
1604 * function by signals on the given proxy widget. This undoes the
1605 * blocking done by gtk_action_block_activate_from().
1607 * This function is intended for use by action implementations.
1612 gtk_action_unblock_activate_from (GtkAction *action,
1615 g_return_if_fail (GTK_IS_ACTION (action));
1617 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1622 closure_accel_activate (GClosure *closure,
1623 GValue *return_value,
1624 guint n_param_values,
1625 const GValue *param_values,
1626 gpointer invocation_hint,
1627 gpointer marshal_data)
1629 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1631 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1633 /* we handled the accelerator */
1634 g_value_set_boolean (return_value, TRUE);
1639 gtk_action_set_action_group (GtkAction *action,
1640 GtkActionGroup *action_group)
1642 g_return_if_fail (GTK_IS_ACTION (action));
1644 if (action->private_data->action_group == NULL)
1645 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1647 g_return_if_fail (action_group == NULL);
1649 action->private_data->action_group = action_group;
1653 * gtk_action_set_accel_path:
1654 * @action: the action object
1655 * @accel_path: the accelerator path
1657 * Sets the accel path for this action. All proxy widgets associated
1658 * with the action will have this accel path, so that their
1659 * accelerators are consistent.
1664 gtk_action_set_accel_path (GtkAction *action,
1665 const gchar *accel_path)
1667 g_return_if_fail (GTK_IS_ACTION (action));
1669 action->private_data->accel_quark = g_quark_from_string (accel_path);
1673 * gtk_action_get_accel_path:
1674 * @action: the action object
1676 * Returns the accel path for this action.
1680 * Returns: the accel path for this action, or %NULL
1681 * if none is set. The returned string is owned by GTK+
1682 * and must not be freed or modified.
1684 G_CONST_RETURN gchar *
1685 gtk_action_get_accel_path (GtkAction *action)
1687 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1689 if (action->private_data->accel_quark)
1690 return g_quark_to_string (action->private_data->accel_quark);
1696 * gtk_action_get_accel_closure:
1697 * @action: the action object
1699 * Returns the accel closure for this action.
1703 * Returns: the accel closure for this action. The returned closure is
1704 * owned by GTK+ and must not be unreffed or modified.
1707 gtk_action_get_accel_closure (GtkAction *action)
1709 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1711 return action->private_data->accel_closure;
1716 * gtk_action_set_accel_group:
1717 * @action: the action object
1718 * @accel_group: a #GtkAccelGroup or %NULL
1720 * Sets the #GtkAccelGroup in which the accelerator for this action
1721 * will be installed.
1726 gtk_action_set_accel_group (GtkAction *action,
1727 GtkAccelGroup *accel_group)
1729 g_return_if_fail (GTK_IS_ACTION (action));
1730 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1733 g_object_ref (accel_group);
1734 if (action->private_data->accel_group)
1735 g_object_unref (action->private_data->accel_group);
1737 action->private_data->accel_group = accel_group;
1741 * gtk_action_connect_accelerator:
1742 * @action: a #GtkAction
1744 * Installs the accelerator for @action if @action has an
1745 * accel path and group. See gtk_action_set_accel_path() and
1746 * gtk_action_set_accel_group()
1748 * Since multiple proxies may independently trigger the installation
1749 * of the accelerator, the @action counts the number of times this
1750 * function has been called and doesn't remove the accelerator until
1751 * gtk_action_disconnect_accelerator() has been called as many times.
1756 gtk_action_connect_accelerator (GtkAction *action)
1758 g_return_if_fail (GTK_IS_ACTION (action));
1760 if (!action->private_data->accel_quark ||
1761 !action->private_data->accel_group)
1764 if (action->private_data->accel_count == 0)
1766 const gchar *accel_path =
1767 g_quark_to_string (action->private_data->accel_quark);
1769 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1771 action->private_data->accel_closure);
1774 action->private_data->accel_count++;
1778 * gtk_action_disconnect_accelerator:
1779 * @action: a #GtkAction
1781 * Undoes the effect of one call to gtk_action_connect_accelerator().
1786 gtk_action_disconnect_accelerator (GtkAction *action)
1788 g_return_if_fail (GTK_IS_ACTION (action));
1790 if (!action->private_data->accel_quark ||
1791 !action->private_data->accel_group)
1794 action->private_data->accel_count--;
1796 if (action->private_data->accel_count == 0)
1797 gtk_accel_group_disconnect (action->private_data->accel_group,
1798 action->private_data->accel_closure);
1802 * gtk_action_create_menu:
1803 * @action: a #GtkAction
1805 * If @action provides a #GtkMenu widget as a submenu for the menu
1806 * item or the toolbar item it creates, this function returns an
1807 * instance of that menu.
1809 * Return value: the menu item provided by the action, or %NULL.
1814 gtk_action_create_menu (GtkAction *action)
1816 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1818 if (GTK_ACTION_GET_CLASS (action)->create_menu)
1819 return GTK_ACTION_GET_CLASS (action)->create_menu (action);
1824 #define __GTK_ACTION_C__
1825 #include "gtkaliasdef.c"