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 "gtktoolbutton.h"
35 #include "gtkmenuitem.h"
36 #include "gtkimagemenuitem.h"
40 #include "gtkaccellabel.h"
44 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
46 struct _GtkActionPrivate
52 gchar *stock_id; /* icon */
56 guint label_set : 1; /* these two used so we can set label */
57 guint short_label_set : 1; /* based on stock id */
62 /* list of proxy widgets */
84 static void gtk_action_init (GtkAction *action);
85 static void gtk_action_class_init (GtkActionClass *class);
87 static GQuark accel_path_id = 0;
88 static const gchar *accel_path_key = "GtkAction::accel_path";
91 gtk_action_get_type (void)
93 static GtkType type = 0;
97 static const GTypeInfo type_info =
99 sizeof (GtkActionClass),
100 (GBaseInitFunc) NULL,
101 (GBaseFinalizeFunc) NULL,
102 (GClassInitFunc) gtk_action_class_init,
103 (GClassFinalizeFunc) NULL,
108 (GInstanceInitFunc) gtk_action_init,
111 type = g_type_register_static (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,
128 static GtkWidget *create_menu_item (GtkAction *action);
129 static GtkWidget *create_tool_item (GtkAction *action);
130 static void connect_proxy (GtkAction *action,
132 static void disconnect_proxy (GtkAction *action,
135 static GObjectClass *parent_class = NULL;
136 static guint action_signals[LAST_SIGNAL] = { 0 };
140 gtk_action_class_init (GtkActionClass *klass)
142 GObjectClass *gobject_class;
144 accel_path_id = g_quark_from_static_string (accel_path_key);
146 parent_class = g_type_class_peek_parent (klass);
147 gobject_class = G_OBJECT_CLASS (klass);
149 gobject_class->finalize = gtk_action_finalize;
150 gobject_class->set_property = gtk_action_set_property;
151 gobject_class->get_property = gtk_action_get_property;
153 klass->activate = NULL;
155 klass->create_menu_item = create_menu_item;
156 klass->create_tool_item = create_tool_item;
157 klass->connect_proxy = connect_proxy;
158 klass->disconnect_proxy = disconnect_proxy;
160 klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
161 klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
163 g_object_class_install_property (gobject_class,
165 g_param_spec_string ("name",
167 _("A unique name for the action."),
170 G_PARAM_CONSTRUCT_ONLY));
171 g_object_class_install_property (gobject_class,
173 g_param_spec_string ("label",
175 _("The label used for menu items and buttons that activate this action."),
178 g_object_class_install_property (gobject_class,
180 g_param_spec_string ("short_label",
182 _("A shorter label that may be used on toolbar buttons."),
185 g_object_class_install_property (gobject_class,
187 g_param_spec_string ("tooltip",
189 _("A tooltip for this action."),
192 g_object_class_install_property (gobject_class,
194 g_param_spec_string ("stock_id",
196 _("The stock icon displayed in widgets representing this action."),
199 g_object_class_install_property (gobject_class,
201 g_param_spec_boolean ("sensitive",
203 _("Whether the action is enabled."),
207 g_object_class_install_property (gobject_class,
209 g_param_spec_boolean ("visible",
211 _("Whether the action is visible."),
215 action_signals[ACTIVATE] =
216 g_signal_new ("activate",
217 G_OBJECT_CLASS_TYPE (klass),
218 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
219 G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
220 g_cclosure_marshal_VOID__VOID,
223 g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
228 gtk_action_init (GtkAction *action)
230 action->private_data = GTK_ACTION_GET_PRIVATE (action);
232 action->private_data->name = NULL;
233 action->private_data->label = NULL;
234 action->private_data->short_label = NULL;
235 action->private_data->tooltip = NULL;
236 action->private_data->stock_id = NULL;
238 action->private_data->sensitive = TRUE;
239 action->private_data->visible = TRUE;
241 action->private_data->label_set = FALSE;
242 action->private_data->short_label_set = FALSE;
244 action->private_data->accel_quark = 0;
246 action->private_data->proxies = NULL;
250 gtk_action_finalize (GObject *object)
254 action = GTK_ACTION (object);
256 g_free (action->private_data->name);
257 g_free (action->private_data->label);
258 g_free (action->private_data->short_label);
259 g_free (action->private_data->tooltip);
260 g_free (action->private_data->stock_id);
264 gtk_action_set_property (GObject *object,
272 action = GTK_ACTION (object);
277 tmp = action->private_data->name;
278 action->private_data->name = g_value_dup_string (value);
282 tmp = action->private_data->label;
283 action->private_data->label = g_value_dup_string (value);
285 action->private_data->label_set = (action->private_data->label != NULL);
286 /* if label is unset, then use the label from the stock item */
287 if (!action->private_data->label_set && action->private_data->stock_id)
289 GtkStockItem stock_item;
291 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
292 action->private_data->label = g_strdup (stock_item.label);
294 /* if short_label is unset, set short_label=label */
295 if (!action->private_data->short_label_set)
297 tmp = action->private_data->short_label;
298 action->private_data->short_label = g_strdup (action->private_data->label);
300 g_object_notify (object, "short_label");
303 case PROP_SHORT_LABEL:
304 tmp = action->private_data->short_label;
305 action->private_data->short_label = g_value_dup_string (value);
307 action->private_data->short_label_set = (action->private_data->short_label != NULL);
308 /* if short_label is unset, then use the value of label */
309 if (!action->private_data->short_label_set)
311 action->private_data->short_label = g_strdup (action->private_data->label);
315 tmp = action->private_data->tooltip;
316 action->private_data->tooltip = g_value_dup_string (value);
320 tmp = action->private_data->stock_id;
321 action->private_data->stock_id = g_value_dup_string (value);
323 /* update label and short_label if appropriate */
324 if (!action->private_data->label_set)
326 GtkStockItem stock_item;
328 g_free (action->private_data->label);
329 if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
330 action->private_data->label = g_strdup (stock_item.label);
332 action->private_data->label = NULL;
333 g_object_notify (object, "label");
335 if (!action->private_data->short_label_set)
337 tmp = action->private_data->short_label;
338 action->private_data->short_label = g_strdup (action->private_data->label);
340 g_object_notify (object, "short_label");
344 action->private_data->sensitive = g_value_get_boolean (value);
347 action->private_data->visible = g_value_get_boolean (value);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 gtk_action_get_property (GObject *object,
363 action = GTK_ACTION (object);
368 g_value_set_string (value, action->private_data->name);
371 g_value_set_string (value, action->private_data->label);
373 case PROP_SHORT_LABEL:
374 g_value_set_string (value, action->private_data->short_label);
377 g_value_set_string (value, action->private_data->tooltip);
380 g_value_set_string (value, action->private_data->stock_id);
383 g_value_set_boolean (value, action->private_data->sensitive);
386 g_value_set_boolean (value, action->private_data->visible);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395 create_menu_item (GtkAction *action)
397 GType menu_item_type;
399 menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
401 return g_object_new (menu_item_type, NULL);
405 create_tool_item (GtkAction *action)
407 GType toolbar_item_type;
409 toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
411 return g_object_new (toolbar_item_type, NULL);
415 gtk_action_remove_proxy (GtkWidget *widget,
418 action->private_data->proxies = g_slist_remove (action->private_data->proxies, widget);
422 gtk_action_sync_property (GtkAction *action,
426 const gchar *property;
427 GValue value = { 0, };
429 property = g_param_spec_get_name (pspec);
431 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
432 g_object_get_property (G_OBJECT (action), property, &value);
434 g_object_set_property (G_OBJECT (proxy), property, &value);
435 g_value_unset (&value);
439 gtk_action_sync_label (GtkAction *action,
443 GtkWidget *label = NULL;
445 g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
446 label = GTK_BIN (proxy)->child;
448 if (GTK_IS_LABEL (label))
449 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
453 gtk_action_sync_short_label (GtkAction *action,
457 GValue value = { 0, };
459 g_value_init (&value, G_TYPE_STRING);
460 g_object_get_property (G_OBJECT (action), "short_label", &value);
462 g_object_set_property (G_OBJECT (proxy), "label", &value);
463 g_value_unset (&value);
467 gtk_action_sync_stock_id (GtkAction *action,
471 GtkWidget *image = NULL;
473 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
475 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
477 if (GTK_IS_IMAGE (image))
478 gtk_image_set_from_stock (GTK_IMAGE (image),
479 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
484 gtk_action_create_menu_proxy (GtkToolItem *tool_item,
487 GtkWidget *menu_item = gtk_action_create_menu_item (action);
489 g_object_ref (menu_item);
490 gtk_object_sink (GTK_OBJECT (menu_item));
492 gtk_tool_item_set_proxy_menu_item (tool_item, "gtk-action-menu-item", menu_item);
493 g_object_unref (menu_item);
499 connect_proxy (GtkAction *action,
502 g_object_ref (action);
503 g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
506 /* add this widget to the list of proxies */
507 action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
508 g_signal_connect (proxy, "destroy",
509 G_CALLBACK (gtk_action_remove_proxy), action);
511 g_signal_connect_object (action, "notify::sensitive",
512 G_CALLBACK (gtk_action_sync_property), proxy, 0);
513 gtk_widget_set_sensitive (proxy, action->private_data->sensitive);
515 g_signal_connect_object (action, "notify::visible",
516 G_CALLBACK (gtk_action_sync_property), proxy, 0);
517 if (action->private_data->visible)
518 gtk_widget_show (proxy);
520 gtk_widget_hide (proxy);
522 if (GTK_IS_MENU_ITEM (proxy))
525 /* menu item specific synchronisers ... */
527 label = GTK_BIN (proxy)->child;
529 /* make sure label is a label */
530 if (label && !GTK_IS_LABEL (label))
532 gtk_container_remove (GTK_CONTAINER (proxy), label);
537 label = g_object_new (GTK_TYPE_ACCEL_LABEL,
538 "use_underline", TRUE,
542 "accel_widget", proxy,
545 gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
546 g_signal_connect_object (action, "notify::label",
547 G_CALLBACK (gtk_action_sync_label), proxy, 0);
549 if (GTK_IS_IMAGE_MENU_ITEM (proxy))
553 image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
554 if (image && !GTK_IS_IMAGE (image))
556 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
561 image = gtk_image_new_from_stock (NULL,
563 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
565 gtk_widget_show (image);
567 gtk_image_set_from_stock (GTK_IMAGE (image),
568 action->private_data->stock_id, GTK_ICON_SIZE_MENU);
569 g_signal_connect_object (action, "notify::stock_id",
570 G_CALLBACK (gtk_action_sync_stock_id),
574 if (action->private_data->accel_quark)
576 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
577 g_quark_to_string (action->private_data->accel_quark));
580 g_signal_connect_object (proxy, "activate",
581 G_CALLBACK (gtk_action_activate), action,
584 else if (GTK_IS_TOOL_BUTTON (proxy))
586 /* toolbar button specific synchronisers ... */
588 /* synchronise the label */
589 g_object_set (G_OBJECT (proxy),
590 "label", action->private_data->short_label,
591 "use_underline", TRUE,
593 g_signal_connect_object (action, "notify::short_label",
594 G_CALLBACK (gtk_action_sync_short_label),
597 g_object_set (G_OBJECT (proxy), "stock_id", action->private_data->stock_id, NULL);
598 g_signal_connect_object (action, "notify::stock_id",
599 G_CALLBACK (gtk_action_sync_property), proxy, 0);
601 g_signal_connect_object (proxy, "create_menu_proxy",
602 G_CALLBACK (gtk_action_create_menu_proxy),
605 g_signal_connect_object (proxy, "clicked",
606 G_CALLBACK (gtk_action_activate), action,
612 disconnect_proxy (GtkAction *action,
615 static guint notify_id = 0;
618 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
620 g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
622 /* remove proxy from list of proxies */
623 g_signal_handlers_disconnect_by_func (proxy,
624 G_CALLBACK (gtk_action_remove_proxy),
626 gtk_action_remove_proxy (proxy, action);
628 /* disconnect the activate handler */
629 g_signal_handlers_disconnect_by_func (proxy,
630 G_CALLBACK (gtk_action_activate),
633 /* disconnect handlers for notify::* signals */
634 g_signal_handlers_disconnect_by_func (proxy,
635 G_CALLBACK (gtk_action_sync_property),
638 g_signal_handlers_disconnect_by_func (action,
639 G_CALLBACK (gtk_action_sync_stock_id), proxy);
641 /* menu item specific synchronisers ... */
642 g_signal_handlers_disconnect_by_func (action,
643 G_CALLBACK (gtk_action_sync_label),
646 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy), NULL);
648 /* toolbar button specific synchronisers ... */
649 g_signal_handlers_disconnect_by_func (action,
650 G_CALLBACK (gtk_action_sync_short_label),
652 g_signal_handlers_disconnect_by_func (proxy,
653 G_CALLBACK (gtk_action_create_menu_proxy),
658 * gtk_action_activate:
659 * @action: the action object
661 * Emits the "activate" signal on the specified action.
662 * This gets called by the proxy widgets when they get activated.
664 * It can also be used to manually activate an action.
669 gtk_action_activate (GtkAction *action)
671 g_signal_emit (action, action_signals[ACTIVATE], 0);
675 * gtk_action_create_icon:
676 * @action: the action object
677 * @icon_size: the size of the icon that should be created.
679 * This function is intended for use by action implementations to
680 * create icons displayed in the proxy widgets.
682 * Returns: a widget that displays the icon for this action.
687 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
689 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
691 if (action->private_data->stock_id)
692 return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
698 * gtk_action_create_menu_item:
699 * @action: the action object
701 * Creates a menu item widget that proxies for the given action.
703 * Returns: a menu item connected to the action.
708 gtk_action_create_menu_item (GtkAction *action)
710 GtkWidget *menu_item;
712 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
714 menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
716 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
722 * gtk_action_create_tool_item:
723 * @action: the action object
725 * Creates a toolbar item widget that proxies for the given action.
727 * Returns: a toolbar item connected to the action.
732 gtk_action_create_tool_item (GtkAction *action)
736 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
738 button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
740 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
746 * gtk_action_connect_proxy:
747 * @action: the action object
748 * @proxy: the proxy widget
750 * Connects a widget to an action object as a proxy. Synchronises
751 * various properties of the action with the widget (such as label
752 * text, icon, tooltip, etc), and attaches a callback so that the
753 * action gets activated when the proxy widget does.
755 * If the widget is already connected to an action, it is disconnected
761 gtk_action_connect_proxy (GtkAction *action,
764 GtkAction *prev_action;
766 g_return_if_fail (GTK_IS_ACTION (action));
767 g_return_if_fail (GTK_IS_WIDGET (proxy));
769 prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
773 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
776 (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
780 * gtk_action_disconnect_proxy:
781 * @action: the action object
782 * @proxy: the proxy widget
784 * Disconnects a proxy widget from an action.
785 * Does <emphasis>not</emphasis> destroy the widget, however.
790 gtk_action_disconnect_proxy (GtkAction *action,
793 g_return_if_fail (GTK_IS_ACTION (action));
794 g_return_if_fail (GTK_IS_WIDGET (proxy));
796 g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") != action);
798 (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
802 * gtk_action_get_proxies:
803 * @action: the action object
805 * Returns the proxy widgets for an action.
807 * Return value: a #GSList of proxy widgets. The list is owned by the action and
808 * must not be modified.
813 gtk_action_get_proxies (GtkAction *action)
815 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
817 return action->private_data->proxies;
822 * gtk_action_get_name:
823 * @action: the action object
825 * Returns the name of the action.
827 * Return value: the name of the action. The string belongs to GTK+ and should not
833 gtk_action_get_name (GtkAction *action)
835 g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
837 return action->private_data->name;
841 * gtk_action_block_activate_from:
842 * @action: the action object
843 * @proxy: a proxy widget
845 * Disables calls to the gtk_action_activate()
846 * function by signals on the given proxy widget. This is used to
847 * break notification loops for things like check or radio actions.
849 * This function is intended for use by action implementations.
854 gtk_action_block_activate_from (GtkAction *action,
857 g_return_if_fail (GTK_IS_ACTION (action));
859 g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
864 * gtk_action_unblock_activate_from:
865 * @action: the action object
866 * @proxy: a proxy widget
868 * Re-enables calls to the gtk_action_activate()
869 * function by signals on the given proxy widget. This undoes the
870 * blocking done by gtk_action_block_activate_from().
872 * This function is intended for use by action implementations.
877 gtk_action_unblock_activate_from (GtkAction *action,
880 g_return_if_fail (GTK_IS_ACTION (action));
882 g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
887 * gtk_action_set_accel_path:
888 * @action: the action object
889 * @accel_path: the accelerator path
891 * Sets the accel path for this action. All proxy widgets associated
892 * with the action will have this accel path, so that their
893 * accelerators are consistent.
898 gtk_action_set_accel_path (GtkAction *action,
899 const gchar *accel_path)
901 action->private_data->accel_quark = g_quark_from_string (accel_path);