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