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"
38 #include "gtkimagemenuitem.h"
41 #include "gtkmarshalers.h"
42 #include "gtkmenuitem.h"
44 #include "gtktearoffmenuitem.h"
45 #include "gtktoolbutton.h"
46 #include "gtktoolbar.h"
47 #include "gtkprivate.h"
51 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
53 struct _GtkActionPrivate
59 gchar *stock_id; /* icon */
63 guint label_set : 1; /* these two used so we can set label */
64 guint short_label_set : 1; /* based on stock id */
65 guint visible_horizontal : 1;
66 guint visible_vertical : 1;
67 guint is_important : 1;
68 guint hide_if_empty : 1;
69 guint visible_overflown : 1;
73 GtkAccelGroup *accel_group;
74 GClosure *accel_closure;
77 GtkActionGroup *action_group;
79 /* list of proxy widgets */
97 PROP_VISIBLE_HORIZONTAL,
98 PROP_VISIBLE_VERTICAL,
99 PROP_VISIBLE_OVERFLOWN,
107 static void gtk_action_init (GtkAction *action);
108 static void gtk_action_class_init (GtkActionClass *class);
110 static GQuark accel_path_id = 0;
111 static const gchar accel_path_key[] = "GtkAction::accel_path";
114 gtk_action_get_type (void)
116 static GtkType type = 0;
120 static const GTypeInfo type_info =
122 sizeof (GtkActionClass),
123 (GBaseInitFunc) NULL,
124 (GBaseFinalizeFunc) NULL,
125 (GClassInitFunc) gtk_action_class_init,
126 (GClassFinalizeFunc) NULL,
131 (GInstanceInitFunc) gtk_action_init,
134 type = g_type_register_static (G_TYPE_OBJECT,
141 static void gtk_action_finalize (GObject *object);
142 static void gtk_action_set_property (GObject *object,
146 static void gtk_action_get_property (GObject *object,
150 static void gtk_action_set_action_group (GtkAction *action,
151 GtkActionGroup *action_group);
153 static GtkWidget *create_menu_item (GtkAction *action);
154 static GtkWidget *create_tool_item (GtkAction *action);
155 static void connect_proxy (GtkAction *action,
157 static void disconnect_proxy (GtkAction *action,
159 static void closure_accel_activate (GClosure *closure,
160 GValue *return_value,
161 guint n_param_values,
162 const GValue *param_values,
163 gpointer invocation_hint,
164 gpointer marshal_data);
166 static GObjectClass *parent_class = NULL;
167 static guint action_signals[LAST_SIGNAL] = { 0 };
171 gtk_action_class_init (GtkActionClass *klass)
173 GObjectClass *gobject_class;
175 accel_path_id = g_quark_from_static_string (accel_path_key);
177 parent_class = g_type_class_peek_parent (klass);
178 gobject_class = G_OBJECT_CLASS (klass);
180 gobject_class->finalize = gtk_action_finalize;
181 gobject_class->set_property = gtk_action_set_property;
182 gobject_class->get_property = gtk_action_get_property;
184 klass->activate = NULL;
186 klass->create_menu_item = create_menu_item;
187 klass->create_tool_item = create_tool_item;
188 klass->connect_proxy = connect_proxy;
189 klass->disconnect_proxy = disconnect_proxy;
191 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
192 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
194 g_object_class_install_property (gobject_class,
196 g_param_spec_string ("name",
198 P_("A unique name for the action."),
200 GTK_PARAM_READWRITE |
201 G_PARAM_CONSTRUCT_ONLY));
202 g_object_class_install_property (gobject_class,
204 g_param_spec_string ("label",
206 P_("The label used for menu items and buttons "
207 "that activate this action."),
209 GTK_PARAM_READWRITE));
210 g_object_class_install_property (gobject_class,
212 g_param_spec_string ("short-label",
214 P_("A shorter label that may be used on toolbar buttons."),
216 GTK_PARAM_READWRITE));
217 g_object_class_install_property (gobject_class,
219 g_param_spec_string ("tooltip",
221 P_("A tooltip for this action."),
223 GTK_PARAM_READWRITE));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("stock-id",
228 P_("The stock icon displayed in widgets representing "
231 GTK_PARAM_READWRITE));
232 g_object_class_install_property (gobject_class,
233 PROP_VISIBLE_HORIZONTAL,
234 g_param_spec_boolean ("visible-horizontal",
235 P_("Visible when horizontal"),
236 P_("Whether the toolbar item is visible when the toolbar "
237 "is in a horizontal orientation."),
239 GTK_PARAM_READWRITE));
241 * GtkAction:visible-overflown:
243 * When %TRUE, toolitem proxies for this action are represented in the
244 * toolbar overflow menu.
248 g_object_class_install_property (gobject_class,
249 PROP_VISIBLE_OVERFLOWN,
250 g_param_spec_boolean ("visible-overflown",
251 P_("Visible when overflown"),
252 P_("When TRUE, toolitem proxies for this action "
253 "are represented in the toolbar overflow menu."),
255 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
257 PROP_VISIBLE_VERTICAL,
258 g_param_spec_boolean ("visible-vertical",
259 P_("Visible when vertical"),
260 P_("Whether the toolbar item is visible when the toolbar "
261 "is in a vertical orientation."),
263 GTK_PARAM_READWRITE));
264 g_object_class_install_property (gobject_class,
266 g_param_spec_boolean ("is-important",
268 P_("Whether the action is considered important. "
269 "When TRUE, toolitem proxies for this action "
270 "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
272 GTK_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boolean ("hide-if-empty",
277 P_("When TRUE, empty menu proxies for this action are hidden."),
279 GTK_PARAM_READWRITE));
280 g_object_class_install_property (gobject_class,
282 g_param_spec_boolean ("sensitive",
284 P_("Whether the action is enabled."),
286 GTK_PARAM_READWRITE));
287 g_object_class_install_property (gobject_class,
289 g_param_spec_boolean ("visible",
291 P_("Whether the action is visible."),
293 GTK_PARAM_READWRITE));
294 g_object_class_install_property (gobject_class,
296 g_param_spec_object ("action-group",
298 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
299 GTK_TYPE_ACTION_GROUP,
300 GTK_PARAM_READWRITE));
303 * GtkAction::activate:
304 * @action: the #GtkAction
306 * The "activate" signal is emitted when the action is activated.
310 action_signals[ACTIVATE] =
311 g_signal_new (I_("activate"),
312 G_OBJECT_CLASS_TYPE (klass),
313 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
314 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
315 g_cclosure_marshal_VOID__VOID,
318 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
323 gtk_action_init (GtkAction *action)
325 action->private_data = GTK_ACTION_GET_PRIVATE (action);
327 action->private_data->name = NULL;
328 action->private_data->label = NULL;
329 action->private_data->short_label = NULL;
330 action->private_data->tooltip = NULL;
331 action->private_data->stock_id = NULL;
332 action->private_data->visible_horizontal = TRUE;
333 action->private_data->visible_vertical = TRUE;
334 action->private_data->visible_overflown = TRUE;
335 action->private_data->is_important = FALSE;
336 action->private_data->hide_if_empty = TRUE;
338 action->private_data->sensitive = TRUE;
339 action->private_data->visible = TRUE;
341 action->private_data->label_set = FALSE;
342 action->private_data->short_label_set = FALSE;
344 action->private_data->accel_count = 0;
345 action->private_data->accel_group = NULL;
346 action->private_data->accel_quark = 0;
347 action->private_data->accel_closure =
348 g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
349 g_closure_set_marshal (action->private_data->accel_closure,
350 closure_accel_activate);
351 g_closure_ref (action->private_data->accel_closure);
352 g_closure_sink (action->private_data->accel_closure);
354 action->private_data->action_group = NULL;
356 action->private_data->proxies = NULL;
361 * @name: A unique name for the action
362 * @label: the label displayed in menu items and on buttons
363 * @tooltip: a tooltip for the action
364 * @stock_id: the stock icon to display in widgets representing the action
366 * Creates a new #GtkAction object. To add the action to a
367 * #GtkActionGroup and set the accelerator for the action,
368 * call gtk_action_group_add_action_with_accel().
369 * See <xref linkend="XML-UI"/> for information on allowed action
372 * Return value: a new #GtkAction
377 gtk_action_new (const gchar *name,
379 const gchar *tooltip,
380 const gchar *stock_id)
384 action = g_object_new (GTK_TYPE_ACTION,
388 "stock_id", stock_id,
395 gtk_action_finalize (GObject *object)
398 action = GTK_ACTION (object);
400 g_free (action->private_data->name);
401 g_free (action->private_data->label);
402 g_free (action->private_data->short_label);
403 g_free (action->private_data->tooltip);
404 g_free (action->private_data->stock_id);
406 g_closure_unref (action->private_data->accel_closure);
407 if (action->private_data->accel_group)
408 g_object_unref (action->private_data->accel_group);
410 G_OBJECT_CLASS (parent_class)->finalize (object);
414 gtk_action_set_property (GObject *object,
422 action = GTK_ACTION (object);
427 tmp = action->private_data->name;
428 action->private_data->name = g_value_dup_string (value);
432 tmp = action->private_data->label;
433 action->private_data->label = g_value_dup_string (value);
435 action->private_data->label_set = (action->private_data->label != NULL);
436 /* if label is unset, then use the label from the stock item */
437 if (!action->private_data->label_set && action->private_data->stock_id)
439 GtkStockItem stock_item;
441 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
442 action->private_data->label = g_strdup (stock_item.label);
444 /* if short_label is unset, set short_label=label */
445 if (!action->private_data->short_label_set)
447 tmp = action->private_data->short_label;
448 action->private_data->short_label = g_strdup (action->private_data->label);
450 g_object_notify (object, "short-label");
453 case PROP_SHORT_LABEL:
454 tmp = action->private_data->short_label;
455 action->private_data->short_label = g_value_dup_string (value);
457 action->private_data->short_label_set = (action->private_data->short_label != NULL);
458 /* if short_label is unset, then use the value of label */
459 if (!action->private_data->short_label_set)
461 action->private_data->short_label = g_strdup (action->private_data->label);
465 tmp = action->private_data->tooltip;
466 action->private_data->tooltip = g_value_dup_string (value);
470 tmp = action->private_data->stock_id;
471 action->private_data->stock_id = g_value_dup_string (value);
473 /* update label and short_label if appropriate */
474 if (!action->private_data->label_set)
476 GtkStockItem stock_item;
478 g_free (action->private_data->label);
479 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
480 action->private_data->label = g_strdup (stock_item.label);
482 action->private_data->label = NULL;
483 g_object_notify (object, "label");
485 if (!action->private_data->short_label_set)
487 tmp = action->private_data->short_label;
488 action->private_data->short_label = g_strdup (action->private_data->label);
490 g_object_notify (object, "short-label");
493 case PROP_VISIBLE_HORIZONTAL:
494 action->private_data->visible_horizontal = g_value_get_boolean (value);
496 case PROP_VISIBLE_VERTICAL:
497 action->private_data->visible_vertical = g_value_get_boolean (value);
499 case PROP_VISIBLE_OVERFLOWN:
500 action->private_data->visible_overflown = g_value_get_boolean (value);
502 case PROP_IS_IMPORTANT:
503 action->private_data->is_important = g_value_get_boolean (value);
505 case PROP_HIDE_IF_EMPTY:
506 action->private_data->hide_if_empty = g_value_get_boolean (value);
509 action->private_data->sensitive = g_value_get_boolean (value);
512 action->private_data->visible = g_value_get_boolean (value);
514 case PROP_ACTION_GROUP:
515 gtk_action_set_action_group (action, g_value_get_object (value));
518 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524 gtk_action_get_property (GObject *object,
531 action = GTK_ACTION (object);
536 g_value_set_string (value, action->private_data->name);
539 g_value_set_string (value, action->private_data->label);
541 case PROP_SHORT_LABEL:
542 g_value_set_string (value, action->private_data->short_label);
545 g_value_set_string (value, action->private_data->tooltip);
548 g_value_set_string (value, action->private_data->stock_id);
550 case PROP_VISIBLE_HORIZONTAL:
551 g_value_set_boolean (value, action->private_data->visible_horizontal);
553 case PROP_VISIBLE_VERTICAL:
554 g_value_set_boolean (value, action->private_data->visible_vertical);
556 case PROP_VISIBLE_OVERFLOWN:
557 g_value_set_boolean (value, action->private_data->visible_overflown);
559 case PROP_IS_IMPORTANT:
560 g_value_set_boolean (value, action->private_data->is_important);
562 case PROP_HIDE_IF_EMPTY:
563 g_value_set_boolean (value, action->private_data->hide_if_empty);
566 g_value_set_boolean (value, action->private_data->sensitive);
569 g_value_set_boolean (value, action->private_data->visible);
571 case PROP_ACTION_GROUP:
572 g_value_set_object (value, action->private_data->action_group);
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 create_menu_item (GtkAction *action)
583 GType menu_item_type;
585 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
587 return g_object_new (menu_item_type, NULL);
591 create_tool_item (GtkAction *action)
593 GType toolbar_item_type;
595 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
597 return g_object_new (toolbar_item_type, NULL);
601 remove_proxy (GtkWidget *proxy,
604 if (GTK_IS_MENU_ITEM (proxy))
605 gtk_action_disconnect_accelerator (action);
607 action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
611 gtk_action_sync_sensitivity (GtkAction *action,
615 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
619 gtk_action_sync_property (GtkAction *action,
623 const gchar *property;
624 GValue value = { 0, };
626 property = g_param_spec_get_name (pspec);
628 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
629 g_object_get_property (G_OBJECT (action), property, &value);
631 g_object_set_property (G_OBJECT (proxy), property, &value);
632 g_value_unset (&value);
636 * _gtk_action_sync_menu_visible:
637 * @action: a #GtkAction, or %NULL to determine the action from @proxy
638 * @proxy: a proxy menu item
639 * @empty: whether the submenu attached to @proxy is empty
641 * Updates the visibility of @proxy from the visibility of @action
642 * according to the following rules:
644 * <listitem><para>if @action is invisible, @proxy is too
646 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty"
647 * property of @action indicates otherwise
651 * This function is used in the implementation of #GtkUIManager.
654 _gtk_action_sync_menu_visible (GtkAction *action,
658 gboolean visible, hide_if_empty;
660 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
661 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
664 action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
666 visible = gtk_action_is_visible (action);
667 hide_if_empty = action->private_data->hide_if_empty;
669 if (visible && !(empty && hide_if_empty))
670 gtk_widget_show (proxy);
672 gtk_widget_hide (proxy);
675 gboolean _gtk_menu_is_empty (GtkWidget *menu);
678 gtk_action_sync_visible (GtkAction *action,
682 if (GTK_IS_MENU_ITEM (proxy))
684 GtkWidget *menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
686 _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
690 if (gtk_action_is_visible (action))
691 gtk_widget_show (proxy);
693 gtk_widget_hide (proxy);
698 gtk_action_sync_label (GtkAction *action,
702 GtkWidget *label = NULL;
704 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
705 label = GTK_BIN (proxy)->child;
707 if (GTK_IS_LABEL (label))
708 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
712 gtk_action_sync_short_label (GtkAction *action,
716 GValue value = { 0, };
718 g_value_init (&value, G_TYPE_STRING);
719 g_object_get_property (G_OBJECT (action), "short-label", &value);
721 g_object_set_property (G_OBJECT (proxy), "label", &value);
722 g_value_unset (&value);
726 gtk_action_sync_stock_id (GtkAction *action,
730 GtkWidget *image = NULL;
732 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
734 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
736 if (GTK_IS_IMAGE (image))
737 gtk_image_set_from_stock (GTK_IMAGE (image),
738 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
743 gtk_action_sync_button_stock_id (GtkAction *action,
747 g_object_set (G_OBJECT (proxy),
749 action->private_data->stock_id,
754 gtk_action_sync_tooltip (GtkAction *action,
758 g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
760 if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
762 GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
764 gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy),
766 action->private_data->tooltip,
773 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
776 GtkWidget *menu_item;
778 if (action->private_data->visible_overflown)
780 menu_item = gtk_action_create_menu_item (action);
782 g_object_ref (menu_item);
783 gtk_object_sink (GTK_OBJECT (menu_item));
785 gtk_tool_item_set_proxy_menu_item (tool_item,
786 "gtk-action-menu-item", menu_item);
787 g_object_unref (menu_item);
790 gtk_tool_item_set_proxy_menu_item (tool_item,
791 "gtk-action-menu-item", NULL);
797 connect_proxy (GtkAction *action,
800 g_object_ref (action);
801 g_object_set_data_full (G_OBJECT (proxy), I_("gtk-action"), action,
804 /* add this widget to the list of proxies */
805 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
806 g_signal_connect (proxy, "destroy",
807 G_CALLBACK (remove_proxy), action);
809 g_signal_connect_object (action, "notify::sensitive",
810 G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
811 gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
813 g_signal_connect_object (action, "notify::visible",
814 G_CALLBACK (gtk_action_sync_visible), proxy, 0);
815 if (gtk_action_is_visible (action))
816 gtk_widget_show (proxy);
818 gtk_widget_hide (proxy);
819 gtk_widget_set_no_show_all (proxy, TRUE);
821 if (GTK_IS_MENU_ITEM (proxy))
824 /* menu item specific synchronisers ... */
826 if (action->private_data->accel_quark)
828 gtk_action_connect_accelerator (action);
829 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
830 g_quark_to_string (action->private_data->accel_quark));
833 label = GTK_BIN (proxy)->child;
835 /* make sure label is a label */
836 if (label && !GTK_IS_LABEL (label))
838 gtk_container_remove (GTK_CONTAINER (proxy), label);
843 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
844 "use_underline", TRUE,
850 if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
852 "accel_closure", action->private_data->accel_closure,
855 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
856 g_signal_connect_object (action, "notify::label",
857 G_CALLBACK (gtk_action_sync_label), proxy, 0);
859 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
863 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
864 if (image && !GTK_IS_IMAGE (image))
866 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
871 image = gtk_image_new_from_stock (NULL,
873 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
875 gtk_widget_show (image);
877 gtk_image_set_from_stock (GTK_IMAGE (image),
878 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
879 g_signal_connect_object (action, "notify::stock-id",
880 G_CALLBACK (gtk_action_sync_stock_id),
884 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
885 g_signal_connect_object (proxy, "activate",
886 G_CALLBACK (gtk_action_activate), action,
890 else if (GTK_IS_TOOL_ITEM (proxy))
894 /* toolbar item specific synchronisers ... */
897 "visible_horizontal", action->private_data->visible_horizontal,
898 "visible_vertical", action->private_data->visible_vertical,
899 "is_important", action->private_data->is_important,
902 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (action),
904 gtk_action_sync_tooltip (action, pspec, proxy);
906 g_signal_connect_object (action, "notify::visible-horizontal",
907 G_CALLBACK (gtk_action_sync_property),
909 g_signal_connect_object (action, "notify::visible-vertical",
910 G_CALLBACK (gtk_action_sync_property),
912 g_signal_connect_object (action, "notify::is-important",
913 G_CALLBACK (gtk_action_sync_property),
915 g_signal_connect_object (action, "notify::tooltip",
916 G_CALLBACK (gtk_action_sync_tooltip),
919 g_signal_connect_object (proxy, "create_menu_proxy",
920 G_CALLBACK (gtk_action_create_menu_proxy),
923 gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
925 /* toolbar button specific synchronisers ... */
926 if (GTK_IS_TOOL_BUTTON (proxy))
929 "label", action->private_data->short_label,
930 "use_underline", TRUE,
931 "stock_id", action->private_data->stock_id,
934 g_signal_connect_object (action, "notify::short-label",
935 G_CALLBACK (gtk_action_sync_short_label),
937 g_signal_connect_object (action, "notify::stock-id",
938 G_CALLBACK (gtk_action_sync_property),
940 g_signal_connect_object (proxy, "clicked",
941 G_CALLBACK (gtk_action_activate), action,
945 else if (GTK_IS_BUTTON (proxy))
947 /* button specific synchronisers ... */
948 if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
950 /* synchronise stock-id */
952 "stock-id", action->private_data->stock_id,
954 g_signal_connect_object (action, "notify::stock-id",
955 G_CALLBACK (gtk_action_sync_button_stock_id),
958 else if (GTK_BIN (proxy)->child == NULL ||
959 GTK_IS_LABEL (GTK_BIN (proxy)->child))
961 /* synchronise the label */
963 "label", action->private_data->short_label,
964 "use_underline", TRUE,
966 g_signal_connect_object (action, "notify::short-label",
967 G_CALLBACK (gtk_action_sync_short_label),
972 /* we leave the button alone if there is a custom child */
973 g_signal_connect_object (proxy, "clicked",
974 G_CALLBACK (gtk_action_activate), action,
978 if (action->private_data->action_group)
979 _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
983 disconnect_proxy (GtkAction *action,
986 g_object_set_data (G_OBJECT (proxy), I_("gtk-action"), NULL);
988 /* remove proxy from list of proxies */
989 g_signal_handlers_disconnect_by_func (proxy,
990 G_CALLBACK (remove_proxy),
992 remove_proxy (proxy, action);
994 /* disconnect the activate handler */
995 g_signal_handlers_disconnect_by_func (proxy,
996 G_CALLBACK (gtk_action_activate),
999 /* disconnect handlers for notify::* signals */
1000 g_signal_handlers_disconnect_by_func (action,
1001 G_CALLBACK (gtk_action_sync_sensitivity),
1003 g_signal_handlers_disconnect_by_func (action,
1004 G_CALLBACK (gtk_action_sync_visible),
1006 g_signal_handlers_disconnect_by_func (action,
1007 G_CALLBACK (gtk_action_sync_property),
1010 g_signal_handlers_disconnect_by_func (action,
1011 G_CALLBACK (gtk_action_sync_stock_id), proxy);
1013 /* menu item specific synchronisers ... */
1014 g_signal_handlers_disconnect_by_func (action,
1015 G_CALLBACK (gtk_action_sync_label),
1018 /* toolbar button specific synchronisers ... */
1019 g_signal_handlers_disconnect_by_func (action,
1020 G_CALLBACK (gtk_action_sync_short_label),
1023 g_signal_handlers_disconnect_by_func (proxy,
1024 G_CALLBACK (gtk_action_create_menu_proxy),
1027 if (action->private_data->action_group)
1028 _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
1032 _gtk_action_emit_activate (GtkAction *action)
1034 GtkActionGroup *group = action->private_data->action_group;
1038 g_object_ref (group);
1039 _gtk_action_group_emit_pre_activate (group, action);
1042 g_signal_emit (action, action_signals[ACTIVATE], 0);
1046 _gtk_action_group_emit_post_activate (group, action);
1047 g_object_unref (group);
1052 * gtk_action_activate:
1053 * @action: the action object
1055 * Emits the "activate" signal on the specified action, if it isn't
1056 * insensitive. This gets called by the proxy widgets when they get
1059 * It can also be used to manually activate an action.
1064 gtk_action_activate (GtkAction *action)
1066 g_return_if_fail (GTK_IS_ACTION (action));
1068 if (gtk_action_is_sensitive (action))
1069 _gtk_action_emit_activate (action);
1073 * gtk_action_create_icon:
1074 * @action: the action object
1075 * @icon_size: the size of the icon that should be created.
1077 * This function is intended for use by action implementations to
1078 * create icons displayed in the proxy widgets.
1080 * Returns: a widget that displays the icon for this action.
1085 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1087 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1089 if (action->private_data->stock_id)
1090 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1096 * gtk_action_create_menu_item:
1097 * @action: the action object
1099 * Creates a menu item widget that proxies for the given action.
1101 * Returns: a menu item connected to the action.
1106 gtk_action_create_menu_item (GtkAction *action)
1108 GtkWidget *menu_item;
1110 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1112 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1114 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1120 * gtk_action_create_tool_item:
1121 * @action: the action object
1123 * Creates a toolbar item widget that proxies for the given action.
1125 * Returns: a toolbar item connected to the action.
1130 gtk_action_create_tool_item (GtkAction *action)
1134 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1136 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1138 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1144 * gtk_action_connect_proxy:
1145 * @action: the action object
1146 * @proxy: the proxy widget
1148 * Connects a widget to an action object as a proxy. Synchronises
1149 * various properties of the action with the widget (such as label
1150 * text, icon, tooltip, etc), and attaches a callback so that the
1151 * action gets activated when the proxy widget does.
1153 * If the widget is already connected to an action, it is disconnected
1159 gtk_action_connect_proxy (GtkAction *action,
1162 GtkAction *prev_action;
1164 g_return_if_fail (GTK_IS_ACTION (action));
1165 g_return_if_fail (GTK_IS_WIDGET (proxy));
1167 prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1170 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);
1172 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1176 * gtk_action_disconnect_proxy:
1177 * @action: the action object
1178 * @proxy: the proxy widget
1180 * Disconnects a proxy widget from an action.
1181 * Does <emphasis>not</emphasis> destroy the widget, however.
1186 gtk_action_disconnect_proxy (GtkAction *action,
1189 g_return_if_fail (GTK_IS_ACTION (action));
1190 g_return_if_fail (GTK_IS_WIDGET (proxy));
1192 g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1194 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
1198 * gtk_action_get_proxies:
1199 * @action: the action object
1201 * Returns the proxy widgets for an action.
1203 * Return value: a #GSList of proxy widgets. The list is owned by the action and
1204 * must not be modified.
1209 gtk_action_get_proxies (GtkAction *action)
1211 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1213 return action->private_data->proxies;
1218 * gtk_action_get_name:
1219 * @action: the action object
1221 * Returns the name of the action.
1223 * Return value: the name of the action. The string belongs to GTK+ and should not
1228 G_CONST_RETURN gchar *
1229 gtk_action_get_name (GtkAction *action)
1231 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1233 return action->private_data->name;
1237 * gtk_action_is_sensitive:
1238 * @action: the action object
1240 * Returns whether the action is effectively sensitive.
1242 * Return value: %TRUE if the action and its associated action group
1243 * are both sensitive.
1248 gtk_action_is_sensitive (GtkAction *action)
1250 GtkActionPrivate *priv;
1251 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1253 priv = action->private_data;
1254 return priv->sensitive &&
1255 (priv->action_group == NULL ||
1256 gtk_action_group_get_sensitive (priv->action_group));
1260 * gtk_action_get_sensitive:
1261 * @action: the action object
1263 * Returns whether the action itself is sensitive. Note that this doesn't
1264 * necessarily mean effective sensitivity. See gtk_action_is_sensitive()
1267 * Return value: %TRUE if the action itself is sensitive.
1272 gtk_action_get_sensitive (GtkAction *action)
1274 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1276 return action->private_data->sensitive;
1280 * gtk_action_set_sensitive:
1281 * @action: the action object
1282 * @sensitive: %TRUE to make the action sensitive
1284 * Sets the ::sensitive property of the action to @sensitive. Note that
1285 * this doesn't necessarily mean effective sensitivity. See
1286 * gtk_action_is_sensitive()
1292 gtk_action_set_sensitive (GtkAction *action,
1295 g_return_if_fail (GTK_IS_ACTION (action));
1297 sensitive = sensitive != FALSE;
1299 if (action->private_data->sensitive != sensitive)
1301 action->private_data->sensitive = sensitive;
1303 g_object_notify (G_OBJECT (action), "sensitive");
1308 * gtk_action_is_visible:
1309 * @action: the action object
1311 * Returns whether the action is effectively visible.
1313 * Return value: %TRUE if the action and its associated action group
1319 gtk_action_is_visible (GtkAction *action)
1321 GtkActionPrivate *priv;
1322 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1324 priv = action->private_data;
1325 return priv->visible &&
1326 (priv->action_group == NULL ||
1327 gtk_action_group_get_visible (priv->action_group));
1331 * gtk_action_get_visible:
1332 * @action: the action object
1334 * Returns whether the action itself is visible. Note that this doesn't
1335 * necessarily mean effective visibility. See gtk_action_is_sensitive()
1338 * Return value: %TRUE if the action itself is visible.
1343 gtk_action_get_visible (GtkAction *action)
1345 g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1347 return action->private_data->visible;
1351 * gtk_action_set_visible:
1352 * @action: the action object
1353 * @visible: %TRUE to make the action visible
1355 * Sets the ::visible property of the action to @visible. Note that
1356 * this doesn't necessarily mean effective visibility. See
1357 * gtk_action_is_visible()
1363 gtk_action_set_visible (GtkAction *action,
1366 g_return_if_fail (GTK_IS_ACTION (action));
1368 visible = visible != FALSE;
1370 if (action->private_data->visible != visible)
1372 action->private_data->visible = visible;
1374 g_object_notify (G_OBJECT (action), "visible");
1379 * gtk_action_block_activate_from:
1380 * @action: the action object
1381 * @proxy: a proxy widget
1383 * Disables calls to the gtk_action_activate()
1384 * function by signals on the given proxy widget. This is used to
1385 * break notification loops for things like check or radio actions.
1387 * This function is intended for use by action implementations.
1392 gtk_action_block_activate_from (GtkAction *action,
1395 g_return_if_fail (GTK_IS_ACTION (action));
1397 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1402 * gtk_action_unblock_activate_from:
1403 * @action: the action object
1404 * @proxy: a proxy widget
1406 * Re-enables calls to the gtk_action_activate()
1407 * function by signals on the given proxy widget. This undoes the
1408 * blocking done by gtk_action_block_activate_from().
1410 * This function is intended for use by action implementations.
1415 gtk_action_unblock_activate_from (GtkAction *action,
1418 g_return_if_fail (GTK_IS_ACTION (action));
1420 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1425 closure_accel_activate (GClosure *closure,
1426 GValue *return_value,
1427 guint n_param_values,
1428 const GValue *param_values,
1429 gpointer invocation_hint,
1430 gpointer marshal_data)
1432 if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1434 _gtk_action_emit_activate (GTK_ACTION (closure->data));
1436 /* we handled the accelerator */
1437 g_value_set_boolean (return_value, TRUE);
1442 gtk_action_set_action_group (GtkAction *action,
1443 GtkActionGroup *action_group)
1445 g_return_if_fail (GTK_IS_ACTION (action));
1447 if (action->private_data->action_group == NULL)
1448 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1450 g_return_if_fail (action_group == NULL);
1452 action->private_data->action_group = action_group;
1456 * gtk_action_set_accel_path:
1457 * @action: the action object
1458 * @accel_path: the accelerator path
1460 * Sets the accel path for this action. All proxy widgets associated
1461 * with the action will have this accel path, so that their
1462 * accelerators are consistent.
1467 gtk_action_set_accel_path (GtkAction *action,
1468 const gchar *accel_path)
1470 g_return_if_fail (GTK_IS_ACTION (action));
1472 action->private_data->accel_quark = g_quark_from_string (accel_path);
1476 * gtk_action_get_accel_path:
1477 * @action: the action object
1479 * Returns the accel path for this action.
1483 * Returns: the accel path for this action, or %NULL
1484 * if none is set. The returned string is owned by GTK+
1485 * and must not be freed or modified.
1487 G_CONST_RETURN gchar *
1488 gtk_action_get_accel_path (GtkAction *action)
1490 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1492 if (action->private_data->accel_quark)
1493 return g_quark_to_string (action->private_data->accel_quark);
1499 * gtk_action_get_accel_closure:
1500 * @action: the action object
1502 * Returns the accel closure for this action.
1506 * Returns: the accel closure for this action. The returned closure is
1507 * owned by GTK+ and must not be unreffed or modified.
1510 gtk_action_get_accel_closure (GtkAction *action)
1512 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1514 return action->private_data->accel_closure;
1519 * gtk_action_set_accel_group:
1520 * @action: the action object
1521 * @accel_group: a #GtkAccelGroup or %NULL
1523 * Sets the #GtkAccelGroup in which the accelerator for this action
1524 * will be installed.
1529 gtk_action_set_accel_group (GtkAction *action,
1530 GtkAccelGroup *accel_group)
1532 g_return_if_fail (GTK_IS_ACTION (action));
1533 g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1536 g_object_ref (accel_group);
1537 if (action->private_data->accel_group)
1538 g_object_unref (action->private_data->accel_group);
1540 action->private_data->accel_group = accel_group;
1544 * gtk_action_connect_accelerator:
1545 * @action: a #GtkAction
1547 * Installs the accelerator for @action if @action has an
1548 * accel path and group. See gtk_action_set_accel_path() and
1549 * gtk_action_set_accel_group()
1551 * Since multiple proxies may independently trigger the installation
1552 * of the accelerator, the @action counts the number of times this
1553 * function has been called and doesn't remove the accelerator until
1554 * gtk_action_disconnect_accelerator() has been called as many times.
1559 gtk_action_connect_accelerator (GtkAction *action)
1561 g_return_if_fail (GTK_IS_ACTION (action));
1563 if (!action->private_data->accel_quark ||
1564 !action->private_data->accel_group)
1567 if (action->private_data->accel_count == 0)
1569 const gchar *accel_path =
1570 g_quark_to_string (action->private_data->accel_quark);
1572 gtk_accel_group_connect_by_path (action->private_data->accel_group,
1574 action->private_data->accel_closure);
1577 action->private_data->accel_count++;
1581 * gtk_action_disconnect_accelerator:
1582 * @action: a #GtkAction
1584 * Undoes the effect of one call to gtk_action_connect_accelerator().
1589 gtk_action_disconnect_accelerator (GtkAction *action)
1591 g_return_if_fail (GTK_IS_ACTION (action));
1593 if (!action->private_data->accel_quark ||
1594 !action->private_data->accel_group)
1597 action->private_data->accel_count--;
1599 if (action->private_data->accel_count == 0)
1600 gtk_accel_group_disconnect (action->private_data->accel_group,
1601 action->private_data->accel_closure);
1604 #define __GTK_ACTION_C__
1605 #include "gtkaliasdef.c"