]> Pileus Git - ~andy/gtk/blob - gtk/gtkaction.c
50bc3c4f0f39903476f9b351b289e7e4727a6e3a
[~andy/gtk] / gtk / gtkaction.c
1 /*
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 1998, 1999 Red Hat, Inc.
4  * All rights reserved.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /*
23  * Author: James Henstridge <james@daa.com.au>
24  *
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/. 
29  */
30
31 #include <config.h>
32
33 #include "gtkaction.h"
34 #include "gtkaccellabel.h"
35 #include "gtkbutton.h"
36 #include "gtkimage.h"
37 #include "gtkimagemenuitem.h"
38 #include "gtkintl.h"
39 #include "gtklabel.h"
40 #include "gtkmarshalers.h"
41 #include "gtkmenuitem.h"
42 #include "gtkstock.h"
43 #include "gtktoolbutton.h"
44 #include "gtktoolbar.h"
45
46
47 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
48
49 struct _GtkActionPrivate 
50 {
51   gchar *name;
52   gchar *label;
53   gchar *short_label;
54   gchar *tooltip;
55   gchar *stock_id; /* icon */
56
57   guint sensitive       : 1;
58   guint visible         : 1;
59   guint label_set       : 1; /* these two used so we can set label */
60   guint short_label_set : 1; /* based on stock id */
61   guint is_important    : 1;
62
63   /* accelerator */
64   guint          accel_count;
65   GtkAccelGroup *accel_group;
66   GClosure      *accel_closure;
67   GQuark         accel_quark;
68
69   /* list of proxy widgets */
70   GSList *proxies;
71 };
72
73 enum 
74 {
75   CONNECT_PROXY,
76   DISCONNECT_PROXY,
77   ACTIVATE,
78   LAST_SIGNAL
79 };
80
81 enum 
82 {
83   PROP_0,
84   PROP_NAME,
85   PROP_LABEL,
86   PROP_SHORT_LABEL,
87   PROP_TOOLTIP,
88   PROP_STOCK_ID,
89   PROP_IS_IMPORTANT,
90   PROP_SENSITIVE,
91   PROP_VISIBLE,
92 };
93
94 static void gtk_action_init       (GtkAction *action);
95 static void gtk_action_class_init (GtkActionClass *class);
96
97 static GQuark       accel_path_id  = 0;
98 static const gchar *accel_path_key = "GtkAction::accel_path";
99
100 GType
101 gtk_action_get_type (void)
102 {
103   static GtkType type = 0;
104
105   if (!type)
106     {
107       static const GTypeInfo type_info =
108       {
109         sizeof (GtkActionClass),
110         (GBaseInitFunc) NULL,
111         (GBaseFinalizeFunc) NULL,
112         (GClassInitFunc) gtk_action_class_init,
113         (GClassFinalizeFunc) NULL,
114         NULL,
115         
116         sizeof (GtkAction),
117         0, /* n_preallocs */
118         (GInstanceInitFunc) gtk_action_init,
119       };
120
121       type = g_type_register_static (G_TYPE_OBJECT,
122                                      "GtkAction",
123                                      &type_info, 0);
124     }
125   return type;
126 }
127
128 static void gtk_action_finalize     (GObject *object);
129 static void gtk_action_set_property (GObject         *object,
130                                      guint            prop_id,
131                                      const GValue    *value,
132                                      GParamSpec      *pspec);
133 static void gtk_action_get_property (GObject         *object,
134                                      guint            prop_id,
135                                      GValue          *value,
136                                      GParamSpec      *pspec);
137
138 static GtkWidget *create_menu_item    (GtkAction *action);
139 static GtkWidget *create_tool_item    (GtkAction *action);
140 static void       connect_proxy       (GtkAction     *action,
141                                        GtkWidget     *proxy);
142 static void       disconnect_proxy    (GtkAction *action,
143                                        GtkWidget *proxy);
144 static void       closure_accel_activate (GClosure     *closure,
145                                           GValue       *return_value,
146                                           guint         n_param_values,
147                                           const GValue *param_values,
148                                           gpointer      invocation_hint,
149                                           gpointer      marshal_data);
150
151 static GObjectClass *parent_class = NULL;
152 static guint         action_signals[LAST_SIGNAL] = { 0 };
153
154
155 static void
156 gtk_action_class_init (GtkActionClass *klass)
157 {
158   GObjectClass *gobject_class;
159
160   accel_path_id = g_quark_from_static_string (accel_path_key);
161
162   parent_class = g_type_class_peek_parent (klass);
163   gobject_class = G_OBJECT_CLASS (klass);
164
165   gobject_class->finalize     = gtk_action_finalize;
166   gobject_class->set_property = gtk_action_set_property;
167   gobject_class->get_property = gtk_action_get_property;
168
169   klass->activate = NULL;
170
171   klass->create_menu_item = create_menu_item;
172   klass->create_tool_item = create_tool_item;
173   klass->connect_proxy = connect_proxy;
174   klass->disconnect_proxy = disconnect_proxy;
175
176   klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
177   klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
178
179   g_object_class_install_property (gobject_class,
180                                    PROP_NAME,
181                                    g_param_spec_string ("name",
182                                                         _("Name"),
183                                                         _("A unique name for the action."),
184                                                         NULL,
185                                                         G_PARAM_READWRITE |
186                                                         G_PARAM_CONSTRUCT_ONLY));
187   g_object_class_install_property (gobject_class,
188                                    PROP_LABEL,
189                                    g_param_spec_string ("label",
190                                                         _("Label"),
191                                                         _("The label used for menu items and buttons that activate this action."),
192                                                         NULL,
193                                                         G_PARAM_READWRITE));
194   g_object_class_install_property (gobject_class,
195                                    PROP_SHORT_LABEL,
196                                    g_param_spec_string ("short_label",
197                                                         _("Short label"),
198                                                         _("A shorter label that may be used on toolbar buttons."),
199                                                         NULL,
200                                                         G_PARAM_READWRITE));
201   g_object_class_install_property (gobject_class,
202                                    PROP_TOOLTIP,
203                                    g_param_spec_string ("tooltip",
204                                                         _("Tooltip"),
205                                                         _("A tooltip for this action."),
206                                                         NULL,
207                                                         G_PARAM_READWRITE));
208   g_object_class_install_property (gobject_class,
209                                    PROP_STOCK_ID,
210                                    g_param_spec_string ("stock_id",
211                                                         _("Stock Icon"),
212                                                         _("The stock icon displayed in widgets representing this action."),
213                                                         NULL,
214                                                         G_PARAM_READWRITE));
215   g_object_class_install_property (gobject_class,
216                                    PROP_IS_IMPORTANT,
217                                    g_param_spec_boolean ("is_important",
218                                                          _("Is important"),
219                                                          _("Whether the action is considered important. When TRUE, toolitem proxies for this action show text in GTK_TOOLBAR_BOTH_HORIZ mode"),
220                                                          FALSE,
221                                                          G_PARAM_READWRITE));
222   g_object_class_install_property (gobject_class,
223                                    PROP_SENSITIVE,
224                                    g_param_spec_boolean ("sensitive",
225                                                          _("Sensitive"),
226                                                          _("Whether the action is enabled."),
227                                                          TRUE,
228                                                          G_PARAM_READWRITE));
229   g_object_class_install_property (gobject_class,
230                                    PROP_VISIBLE,
231                                    g_param_spec_boolean ("visible",
232                                                          _("Visible"),
233                                                          _("Whether the action is visible."),
234                                                          TRUE,
235                                                          G_PARAM_READWRITE));
236
237
238   /**
239    * GtkAction::activate:
240    * @action: the #GtkAction
241    *
242    * The "activate" signal is emitted when the action is activated.
243    *
244    * Since: 2.4
245    */
246   action_signals[ACTIVATE] =
247     g_signal_new ("activate",
248                   G_OBJECT_CLASS_TYPE (klass),
249                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
250                   G_STRUCT_OFFSET (GtkActionClass, activate),  NULL, NULL,
251                   g_cclosure_marshal_VOID__VOID,
252                   G_TYPE_NONE, 0);
253
254   /**
255    * GtkAction::connect-proxy:
256    * @action: the action
257    * @proxy: the proxy
258    *
259    * The connect_proxy signal is emitted after connecting a proxy to 
260    * an action. Note that the proxy may have been connected to a different
261    * action before.
262    *
263    * This is intended for simple customizations for which a custom action
264    * class would be too clumsy, e.g. showing tooltips for menuitems in the
265    * statusbar.
266    *
267    * Since: 2.4
268    */
269   action_signals[CONNECT_PROXY] =
270     g_signal_new ("connect_proxy",
271                   G_OBJECT_CLASS_TYPE (klass),
272                   0, 0, NULL, NULL,
273                   _gtk_marshal_VOID__OBJECT,
274                   G_TYPE_NONE, 1, 
275                   GTK_TYPE_WIDGET);
276
277   /**
278    * GtkAction::disconnect-proxy:
279    * @action: the action
280    * @proxy: the proxy
281    *
282    * The disconnect_proxy signal is emitted after disconnecting a proxy 
283    * from an action. 
284    *
285    * Since: 2.4
286    */
287   action_signals[DISCONNECT_PROXY] =
288     g_signal_new ("disconnect_proxy",
289                   G_OBJECT_CLASS_TYPE (klass),
290                   0, 0, NULL, NULL,
291                   _gtk_marshal_VOID__OBJECT,
292                   G_TYPE_NONE, 1, 
293                   GTK_TYPE_WIDGET);
294
295   g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
296 }
297
298
299 static void
300 gtk_action_init (GtkAction *action)
301 {
302   action->private_data = GTK_ACTION_GET_PRIVATE (action);
303
304   action->private_data->name = NULL;
305   action->private_data->label = NULL;
306   action->private_data->short_label = NULL;
307   action->private_data->tooltip = NULL;
308   action->private_data->stock_id = NULL;
309   action->private_data->is_important = FALSE;
310
311   action->private_data->sensitive = TRUE;
312   action->private_data->visible = TRUE;
313
314   action->private_data->label_set = FALSE;
315   action->private_data->short_label_set = FALSE;
316
317   action->private_data->accel_count = 0;
318   action->private_data->accel_closure = 
319     g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
320   g_closure_set_marshal (action->private_data->accel_closure, 
321                          closure_accel_activate);
322   g_closure_ref (action->private_data->accel_closure);
323   g_closure_sink (action->private_data->accel_closure);
324
325   action->private_data->accel_quark = 0;
326   action->private_data->accel_count = 0;
327   action->private_data->accel_group = NULL;
328
329   action->private_data->proxies = NULL;
330 }
331
332 static void
333 gtk_action_finalize (GObject *object)
334 {
335   GtkAction *action;
336   action = GTK_ACTION (object);
337
338   g_free (action->private_data->name);
339   g_free (action->private_data->label);
340   g_free (action->private_data->short_label);
341   g_free (action->private_data->tooltip);
342   g_free (action->private_data->stock_id);
343
344   g_closure_unref (action->private_data->accel_closure);
345   if (action->private_data->accel_group)
346     g_object_unref (action->private_data->accel_group);
347 }
348
349 static void
350 gtk_action_set_property (GObject         *object,
351                          guint            prop_id,
352                          const GValue    *value,
353                          GParamSpec      *pspec)
354 {
355   GtkAction *action;
356   gchar *tmp;
357   
358   action = GTK_ACTION (object);
359
360   switch (prop_id)
361     {
362     case PROP_NAME:
363       tmp = action->private_data->name;
364       action->private_data->name = g_value_dup_string (value);
365       g_free (tmp);
366       break;
367     case PROP_LABEL:
368       tmp = action->private_data->label;
369       action->private_data->label = g_value_dup_string (value);
370       g_free (tmp);
371       action->private_data->label_set = (action->private_data->label != NULL);
372       /* if label is unset, then use the label from the stock item */
373       if (!action->private_data->label_set && action->private_data->stock_id)
374         {
375           GtkStockItem stock_item;
376
377           if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
378             action->private_data->label = g_strdup (stock_item.label);
379         }
380       /* if short_label is unset, set short_label=label */
381       if (!action->private_data->short_label_set)
382         {
383           tmp = action->private_data->short_label;
384           action->private_data->short_label = g_strdup (action->private_data->label);
385           g_free (tmp);
386           g_object_notify (object, "short_label");
387         }
388       break;
389     case PROP_SHORT_LABEL:
390       tmp = action->private_data->short_label;
391       action->private_data->short_label = g_value_dup_string (value);
392       g_free (tmp);
393       action->private_data->short_label_set = (action->private_data->short_label != NULL);
394       /* if short_label is unset, then use the value of label */
395       if (!action->private_data->short_label_set)
396         {
397           action->private_data->short_label = g_strdup (action->private_data->label);
398         }
399       break;
400     case PROP_TOOLTIP:
401       tmp = action->private_data->tooltip;
402       action->private_data->tooltip = g_value_dup_string (value);
403       g_free (tmp);
404       break;
405     case PROP_STOCK_ID:
406       tmp = action->private_data->stock_id;
407       action->private_data->stock_id = g_value_dup_string (value);
408       g_free (tmp);
409       /* update label and short_label if appropriate */
410       if (!action->private_data->label_set)
411         {
412           GtkStockItem stock_item;
413
414           g_free (action->private_data->label);
415           if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
416             action->private_data->label = g_strdup (stock_item.label);
417           else
418             action->private_data->label = NULL;
419           g_object_notify (object, "label");
420         }
421       if (!action->private_data->short_label_set)
422         {
423           tmp = action->private_data->short_label;
424           action->private_data->short_label = g_strdup (action->private_data->label);
425           g_free (tmp);
426           g_object_notify (object, "short_label");
427         }
428       break;
429     case PROP_IS_IMPORTANT:
430       action->private_data->is_important = g_value_get_boolean (value);
431       break;
432     case PROP_SENSITIVE:
433       action->private_data->sensitive = g_value_get_boolean (value);
434       break;
435     case PROP_VISIBLE:
436       action->private_data->visible = g_value_get_boolean (value);
437       break;
438     default:
439       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440       break;
441     }
442 }
443
444 static void
445 gtk_action_get_property (GObject    *object,
446                          guint       prop_id,
447                          GValue     *value,
448                          GParamSpec *pspec)
449 {
450   GtkAction *action;
451
452   action = GTK_ACTION (object);
453
454   switch (prop_id)
455     {
456     case PROP_NAME:
457       g_value_set_string (value, action->private_data->name);
458       break;
459     case PROP_LABEL:
460       g_value_set_string (value, action->private_data->label);
461       break;
462     case PROP_SHORT_LABEL:
463       g_value_set_string (value, action->private_data->short_label);
464       break;
465     case PROP_TOOLTIP:
466       g_value_set_string (value, action->private_data->tooltip);
467       break;
468     case PROP_STOCK_ID:
469       g_value_set_string (value, action->private_data->stock_id);
470       break;
471     case PROP_IS_IMPORTANT:
472       g_value_set_boolean (value, action->private_data->is_important);
473       break;
474     case PROP_SENSITIVE:
475       g_value_set_boolean (value, action->private_data->sensitive);
476       break;
477     case PROP_VISIBLE:
478       g_value_set_boolean (value, action->private_data->visible);
479       break;
480     default:
481       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482       break;
483     }
484 }
485
486 static GtkWidget *
487 create_menu_item (GtkAction *action)
488 {
489   GType menu_item_type;
490
491   menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
492
493   return g_object_new (menu_item_type, NULL);
494 }
495
496 static GtkWidget *
497 create_tool_item (GtkAction *action)
498 {
499   GType toolbar_item_type;
500
501   toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
502
503   return g_object_new (toolbar_item_type, NULL);
504 }
505
506 static void
507 remove_proxy (GtkWidget *proxy, 
508               GtkAction *action)
509 {
510   if (GTK_IS_MENU_ITEM (proxy))
511     gtk_action_disconnect_accelerator (action);
512
513   action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
514 }
515
516 static void
517 gtk_action_sync_property (GtkAction  *action, 
518                           GParamSpec *pspec,
519                           GtkWidget  *proxy)
520 {
521   const gchar *property;
522   GValue value = { 0, };
523
524   property = g_param_spec_get_name (pspec);
525
526   g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
527   g_object_get_property (G_OBJECT (action), property, &value);
528
529   g_object_set_property (G_OBJECT (proxy), property, &value);
530   g_value_unset (&value);
531 }
532
533 static void
534 gtk_action_sync_label (GtkAction  *action, 
535                        GParamSpec *pspec, 
536                        GtkWidget  *proxy)
537 {
538   GtkWidget *label = NULL;
539
540   g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
541   label = GTK_BIN (proxy)->child;
542
543   if (GTK_IS_LABEL (label))
544     gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
545 }
546
547 static void
548 gtk_action_sync_short_label (GtkAction  *action, 
549                              GParamSpec *pspec,
550                              GtkWidget  *proxy)
551 {
552   GValue value = { 0, };
553
554   g_value_init (&value, G_TYPE_STRING);
555   g_object_get_property (G_OBJECT (action), "short_label", &value);
556
557   g_object_set_property (G_OBJECT (proxy), "label", &value);
558   g_value_unset (&value);
559 }
560
561 static void
562 gtk_action_sync_stock_id (GtkAction  *action, 
563                           GParamSpec *pspec,
564                           GtkWidget  *proxy)
565 {
566   GtkWidget *image = NULL;
567
568   if (GTK_IS_IMAGE_MENU_ITEM (proxy))
569     {
570       image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
571
572       if (GTK_IS_IMAGE (image))
573         gtk_image_set_from_stock (GTK_IMAGE (image),
574                                   action->private_data->stock_id, GTK_ICON_SIZE_MENU);
575     }
576 }
577
578 static void
579 gtk_action_sync_tooltip (GtkAction  *action, 
580                          GParamSpec *pspec, 
581                          GtkWidget  *proxy)
582 {
583   g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
584
585   if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
586     {
587       GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
588       
589       gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy), 
590                                  toolbar->tooltips,
591                                  action->private_data->tooltip,
592                                  NULL);
593     }
594 }
595
596
597 static gboolean
598 gtk_action_create_menu_proxy (GtkToolItem *tool_item, 
599                               GtkAction   *action)
600 {
601   GtkWidget *menu_item = gtk_action_create_menu_item (action);
602
603   g_object_ref (menu_item);
604   gtk_object_sink (GTK_OBJECT (menu_item));
605   
606   gtk_tool_item_set_proxy_menu_item (tool_item, "gtk-action-menu-item", menu_item);
607   g_object_unref (menu_item);
608
609   return TRUE;
610 }
611
612 static void
613 connect_proxy (GtkAction     *action, 
614                GtkWidget     *proxy)
615 {
616   g_object_ref (action);
617   g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
618                           g_object_unref);
619
620   /* add this widget to the list of proxies */
621   action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
622   g_signal_connect (proxy, "destroy",
623                     G_CALLBACK (remove_proxy), action);
624
625   g_signal_connect_object (action, "notify::sensitive",
626                            G_CALLBACK (gtk_action_sync_property), proxy, 0);
627   gtk_widget_set_sensitive (proxy, action->private_data->sensitive);
628
629   g_signal_connect_object (action, "notify::visible",
630                            G_CALLBACK (gtk_action_sync_property), proxy, 0);
631   if (action->private_data->visible)
632     gtk_widget_show (proxy);
633   else
634     gtk_widget_hide (proxy);
635   gtk_widget_set_no_show_all (proxy, TRUE);
636
637   if (GTK_IS_MENU_ITEM (proxy))
638     {
639       GtkWidget *label;
640       /* menu item specific synchronisers ... */
641       
642       if (action->private_data->accel_quark)
643         {
644           gtk_action_connect_accelerator (action);
645           gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
646                                         g_quark_to_string (action->private_data->accel_quark));
647         }
648       
649       label = GTK_BIN (proxy)->child;
650
651       /* make sure label is a label */
652       if (label && !GTK_IS_LABEL (label))
653         {
654           gtk_container_remove (GTK_CONTAINER (proxy), label);
655           label = NULL;
656         }
657
658       if (!label)
659         label = g_object_new (GTK_TYPE_ACCEL_LABEL,
660                               "use_underline", TRUE,
661                               "xalign", 0.0,
662                               "visible", TRUE,
663                               "parent", proxy,
664                               NULL);
665       
666       if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
667         g_object_set (G_OBJECT (label),
668                       "accel_closure", action->private_data->accel_closure,
669                       NULL);
670
671       gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
672       g_signal_connect_object (action, "notify::label",
673                                G_CALLBACK (gtk_action_sync_label), proxy, 0);
674
675       if (GTK_IS_IMAGE_MENU_ITEM (proxy))
676         {
677           GtkWidget *image;
678
679           image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
680           if (image && !GTK_IS_IMAGE (image))
681             {
682               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
683               image = NULL;
684             }
685           if (!image)
686             {
687               image = gtk_image_new_from_stock (NULL,
688                                                 GTK_ICON_SIZE_MENU);
689               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
690                                              image);
691               gtk_widget_show (image);
692             }
693           gtk_image_set_from_stock (GTK_IMAGE (image),
694                                     action->private_data->stock_id, GTK_ICON_SIZE_MENU);
695           g_signal_connect_object (action, "notify::stock_id",
696                                    G_CALLBACK (gtk_action_sync_stock_id),
697                                    proxy, 0);
698         }
699
700       g_signal_connect_object (proxy, "activate",
701                                G_CALLBACK (gtk_action_activate), action,
702                                G_CONNECT_SWAPPED);
703
704     }
705   else if (GTK_IS_TOOL_BUTTON (proxy))
706     {
707       /* toolbar button specific synchronisers ... */
708
709       g_object_set (G_OBJECT (proxy),
710                     "label", action->private_data->short_label,
711                     "use_underline", TRUE,
712                     "stock_id", action->private_data->stock_id,
713                     "is_important", action->private_data->is_important,
714                     NULL);
715       /* FIXME: we should set the tooltip here, but the current api
716        * doesn't allow it before the item is added to a toolbar. 
717        */
718       g_signal_connect_object (action, "notify::short_label",
719                                G_CALLBACK (gtk_action_sync_short_label),
720                                proxy, 0);      
721       g_signal_connect_object (action, "notify::stock_id",
722                                G_CALLBACK (gtk_action_sync_property), 
723                                proxy, 0);
724       g_signal_connect_object (action, "notify::is_important",
725                                G_CALLBACK (gtk_action_sync_property), 
726                                proxy, 0);
727       g_signal_connect_object (action, "notify::tooltip",
728                                G_CALLBACK (gtk_action_sync_tooltip), 
729                                proxy, 0);
730
731       g_signal_connect_object (proxy, "create_menu_proxy",
732                                G_CALLBACK (gtk_action_create_menu_proxy),
733                                action, 0);
734
735       g_signal_connect_object (proxy, "clicked",
736                                G_CALLBACK (gtk_action_activate), action,
737                                G_CONNECT_SWAPPED);
738     }
739   else if (GTK_IS_BUTTON (proxy))
740     {
741       /* button specific synchronisers ... */
742
743       /* synchronise the label */
744       g_object_set (G_OBJECT (proxy),
745                     "label", action->private_data->short_label,
746                     "use_underline", TRUE,
747                     NULL);
748       g_signal_connect_object (action, "notify::short_label",
749                                G_CALLBACK (gtk_action_sync_short_label),
750                                proxy, 0);
751       
752       g_signal_connect_object (proxy, "clicked",
753                                G_CALLBACK (gtk_action_activate), action,
754                                G_CONNECT_SWAPPED);
755     }
756
757   g_signal_emit (action, action_signals[CONNECT_PROXY], 0, proxy);
758 }
759
760 static void
761 disconnect_proxy (GtkAction *action, 
762                   GtkWidget *proxy)
763 {
764   g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
765
766   /* remove proxy from list of proxies */
767   g_signal_handlers_disconnect_by_func (proxy,
768                                         G_CALLBACK (remove_proxy),
769                                         action);
770   remove_proxy (proxy, action);
771
772   /* disconnect the activate handler */
773   g_signal_handlers_disconnect_by_func (proxy,
774                                         G_CALLBACK (gtk_action_activate),
775                                         action);
776
777   /* disconnect handlers for notify::* signals */
778   g_signal_handlers_disconnect_by_func (proxy,
779                                         G_CALLBACK (gtk_action_sync_property),
780                                         action);
781
782   g_signal_handlers_disconnect_by_func (action,
783                                 G_CALLBACK (gtk_action_sync_stock_id), proxy);
784
785   /* menu item specific synchronisers ... */
786   g_signal_handlers_disconnect_by_func (action,
787                                         G_CALLBACK (gtk_action_sync_label),
788                                         proxy);
789   
790   /* toolbar button specific synchronisers ... */
791   g_signal_handlers_disconnect_by_func (action,
792                                         G_CALLBACK (gtk_action_sync_short_label),
793                                         proxy);
794
795   g_signal_handlers_disconnect_by_func (proxy,
796                                         G_CALLBACK (gtk_action_create_menu_proxy),
797                                         action);
798
799   g_signal_emit (action, action_signals[DISCONNECT_PROXY], 0, proxy);
800 }
801
802 /**
803  * gtk_action_activate:
804  * @action: the action object
805  *
806  * Emits the "activate" signal on the specified action. 
807  * This gets called by the proxy widgets when they get activated.
808  *
809  * It can also be used to manually activate an action.
810  *
811  * Since: 2.4
812  */
813 void
814 gtk_action_activate (GtkAction *action)
815 {
816   g_signal_emit (action, action_signals[ACTIVATE], 0);
817 }
818
819 /**
820  * gtk_action_create_icon:
821  * @action: the action object
822  * @icon_size: the size of the icon that should be created.
823  *
824  * This function is intended for use by action implementations to
825  * create icons displayed in the proxy widgets.
826  *
827  * Returns: a widget that displays the icon for this action.
828  *
829  * Since: 2.4
830  */
831 GtkWidget *
832 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
833 {
834   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
835
836   if (action->private_data->stock_id)
837     return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
838   else
839     return NULL;
840 }
841
842 /**
843  * gtk_action_create_menu_item:
844  * @action: the action object
845  *
846  * Creates a menu item widget that proxies for the given action.
847  *
848  * Returns: a menu item connected to the action.
849  *
850  * Since: 2.4
851  */
852 GtkWidget *
853 gtk_action_create_menu_item (GtkAction *action)
854 {
855   GtkWidget *menu_item;
856
857   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
858
859   menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
860
861   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
862
863   return menu_item;
864 }
865
866 /**
867  * gtk_action_create_tool_item:
868  * @action: the action object
869  *
870  * Creates a toolbar item widget that proxies for the given action.
871  *
872  * Returns: a toolbar item connected to the action.
873  *
874  * Since: 2.4
875  */
876 GtkWidget *
877 gtk_action_create_tool_item (GtkAction *action)
878 {
879   GtkWidget *button;
880
881   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
882
883   button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
884
885   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
886
887   return button;
888 }
889
890 /**
891  * gtk_action_connect_proxy:
892  * @action: the action object
893  * @proxy: the proxy widget
894  *
895  * Connects a widget to an action object as a proxy.  Synchronises 
896  * various properties of the action with the widget (such as label 
897  * text, icon, tooltip, etc), and attaches a callback so that the 
898  * action gets activated when the proxy widget does.
899  *
900  * If the widget is already connected to an action, it is disconnected
901  * first.
902  *
903  * Since: 2.4
904  */
905 void
906 gtk_action_connect_proxy (GtkAction *action,
907                           GtkWidget *proxy)
908 {
909   GtkAction *prev_action;
910
911   g_return_if_fail (GTK_IS_ACTION (action));
912   g_return_if_fail (GTK_IS_WIDGET (proxy));
913
914   prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
915
916   if (prev_action)
917     (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);  
918
919   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
920 }
921
922 /**
923  * gtk_action_disconnect_proxy:
924  * @action: the action object
925  * @proxy: the proxy widget
926  *
927  * Disconnects a proxy widget from an action.  
928  * Does <emphasis>not</emphasis> destroy the widget, however.
929  *
930  * Since: 2.4
931  */
932 void
933 gtk_action_disconnect_proxy (GtkAction *action,
934                              GtkWidget *proxy)
935 {
936   g_return_if_fail (GTK_IS_ACTION (action));
937   g_return_if_fail (GTK_IS_WIDGET (proxy));
938
939   g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
940
941   (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);  
942 }
943
944 /**
945  * gtk_action_get_proxies:
946  * @action: the action object
947  * 
948  * Returns the proxy widgets for an action.
949  * 
950  * Return value: a #GSList of proxy widgets. The list is owned by the action and
951  * must not be modified.
952  *
953  * Since: 2.4
954  **/
955 GSList*
956 gtk_action_get_proxies (GtkAction *action)
957 {
958   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
959
960   return action->private_data->proxies;
961 }
962
963
964 /**
965  * gtk_action_get_name:
966  * @action: the action object
967  * 
968  * Returns the name of the action.
969  * 
970  * Return value: the name of the action. The string belongs to GTK+ and should not
971  *   be freed.
972  *
973  * Since: 2.4
974  **/
975 const gchar *
976 gtk_action_get_name (GtkAction *action)
977 {
978   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
979
980   return action->private_data->name;
981 }
982
983 /**
984  * gtk_action_block_activate_from:
985  * @action: the action object
986  * @proxy: a proxy widget
987  *
988  * Disables calls to the gtk_action_activate()
989  * function by signals on the given proxy widget.  This is used to
990  * break notification loops for things like check or radio actions.
991  *
992  * This function is intended for use by action implementations.
993  * 
994  * Since: 2.4
995  */
996 void
997 gtk_action_block_activate_from (GtkAction *action, 
998                                 GtkWidget *proxy)
999 {
1000   g_return_if_fail (GTK_IS_ACTION (action));
1001   
1002   g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1003                                    action);
1004 }
1005
1006 /**
1007  * gtk_action_unblock_activate_from:
1008  * @action: the action object
1009  * @proxy: a proxy widget
1010  *
1011  * Re-enables calls to the gtk_action_activate()
1012  * function by signals on the given proxy widget.  This undoes the
1013  * blocking done by gtk_action_block_activate_from().
1014  *
1015  * This function is intended for use by action implementations.
1016  * 
1017  * Since: 2.4
1018  */
1019 void
1020 gtk_action_unblock_activate_from (GtkAction *action, 
1021                                   GtkWidget *proxy)
1022 {
1023   g_return_if_fail (GTK_IS_ACTION (action));
1024
1025   g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1026                                      action);
1027 }
1028
1029 static void
1030 closure_accel_activate (GClosure     *closure,
1031                         GValue       *return_value,
1032                         guint         n_param_values,
1033                         const GValue *param_values,
1034                         gpointer      invocation_hint,
1035                         gpointer      marshal_data)
1036 {
1037   if (GTK_ACTION (closure->data)->private_data->sensitive)
1038     g_signal_emit (closure->data, action_signals[ACTIVATE], 0);
1039
1040   /* we handled the accelerator */
1041   g_value_set_boolean (return_value, TRUE);
1042 }
1043
1044 /**
1045  * gtk_action_set_accel_path:
1046  * @action: the action object
1047  * @accel_path: the accelerator path
1048  *
1049  * Sets the accel path for this action.  All proxy widgets associated
1050  * with the action will have this accel path, so that their
1051  * accelerators are consistent.
1052  *
1053  * Since: 2.4
1054  */
1055 void
1056 gtk_action_set_accel_path (GtkAction   *action, 
1057                            const gchar *accel_path)
1058 {
1059   g_return_if_fail (GTK_IS_ACTION (action));
1060
1061   action->private_data->accel_quark = g_quark_from_string (accel_path);
1062 }
1063
1064 /**
1065  * gtk_action_set_accel_group:
1066  * @action: the action object
1067  * @accel_group: a #GtkAccelGroup or %NULL
1068  * 
1069  * Sets the #GtkAccelGroup in which the accelerator for this action
1070  * will be installed.
1071  *
1072  * Since: 2.4
1073  **/
1074 void
1075 gtk_action_set_accel_group (GtkAction     *action,
1076                             GtkAccelGroup *accel_group)
1077 {
1078   g_return_if_fail (GTK_IS_ACTION (action));
1079   g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1080   
1081   if (accel_group)
1082     g_object_ref (accel_group);
1083   if (action->private_data->accel_group)
1084     g_object_unref (action->private_data->accel_group);
1085
1086   action->private_data->accel_group = accel_group;
1087 }
1088
1089 /**
1090  * gtk_action_connect_accelerator:
1091  * @action: a #GtkAction
1092  * 
1093  * Installs the accelerator for @action if @action has an
1094  * accel path and group. See gtk_action_set_accel_path() and 
1095  * gtk_action_set_accel_group()
1096  *
1097  * Since multiple proxies may independently trigger the installation
1098  * of the accelerator, the @action counts the number of times this
1099  * function has been called and doesn't remove the accelerator until
1100  * gtk_action_disconnect_accelerator() has been called as many times.
1101  *
1102  * Since: 2.4
1103  **/
1104 void 
1105 gtk_action_connect_accelerator (GtkAction *action)
1106 {
1107   g_return_if_fail (GTK_IS_ACTION (action));
1108
1109   if (!action->private_data->accel_quark ||
1110       !action->private_data->accel_group)
1111     return;
1112
1113   if (action->private_data->accel_count == 0)
1114     {
1115       const gchar *accel_path = 
1116         g_quark_to_string (action->private_data->accel_quark);
1117       
1118       gtk_accel_group_connect_by_path (action->private_data->accel_group,
1119                                        accel_path,
1120                                        action->private_data->accel_closure);
1121     }
1122
1123   action->private_data->accel_count++;
1124 }
1125
1126 /**
1127  * gtk_action_disconnect_accelerator:
1128  * @action: a #GtkAction
1129  * 
1130  * Undoes the effect of one call to gtk_action_connect_accelerator().
1131  *
1132  * Since: 2.4
1133  **/
1134 void 
1135 gtk_action_disconnect_accelerator (GtkAction *action)
1136 {
1137   g_return_if_fail (GTK_IS_ACTION (action));
1138
1139   if (!action->private_data->accel_quark ||
1140       !action->private_data->accel_group)
1141     return;
1142
1143   action->private_data->accel_count--;
1144
1145   if (action->private_data->accel_count == 0)
1146     gtk_accel_group_disconnect (action->private_data->accel_group,
1147                                 action->private_data->accel_closure);
1148 }