]> Pileus Git - ~andy/gtk/blob - gtk/gtkaction.c
gdk/gdkkeyuni.c gdk/gdkpixbuf-drawable.c gdk/gdkrgb.c gdk/x11/gdkdnd-x11.c
[~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 "gtkalias.h"
34 #include "gtkaction.h"
35 #include "gtkactiongroup.h"
36 #include "gtkaccellabel.h"
37 #include "gtkbutton.h"
38 #include "gtkimage.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkintl.h"
41 #include "gtklabel.h"
42 #include "gtkmarshalers.h"
43 #include "gtkmenuitem.h"
44 #include "gtkstock.h"
45 #include "gtktearoffmenuitem.h"
46 #include "gtktoolbutton.h"
47 #include "gtktoolbar.h"
48
49
50 #define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
51
52 struct _GtkActionPrivate 
53 {
54   gchar *name;
55   gchar *label;
56   gchar *short_label;
57   gchar *tooltip;
58   gchar *stock_id; /* icon */
59
60   guint sensitive          : 1;
61   guint visible            : 1;
62   guint label_set          : 1; /* these two used so we can set label */
63   guint short_label_set    : 1; /* based on stock id */
64   guint visible_horizontal : 1;
65   guint visible_vertical   : 1;
66   guint is_important       : 1;
67   guint hide_if_empty      : 1;
68   guint visible_overflown  : 1;
69
70   /* accelerator */
71   guint          accel_count;
72   GtkAccelGroup *accel_group;
73   GClosure      *accel_closure;
74   GQuark         accel_quark;
75
76   GtkActionGroup *action_group;
77
78   /* list of proxy widgets */
79   GSList *proxies;
80 };
81
82 enum 
83 {
84   ACTIVATE,
85   LAST_SIGNAL
86 };
87
88 enum 
89 {
90   PROP_0,
91   PROP_NAME,
92   PROP_LABEL,
93   PROP_SHORT_LABEL,
94   PROP_TOOLTIP,
95   PROP_STOCK_ID,
96   PROP_VISIBLE_HORIZONTAL,
97   PROP_VISIBLE_VERTICAL,
98   PROP_VISIBLE_OVERFLOWN,
99   PROP_IS_IMPORTANT,
100   PROP_HIDE_IF_EMPTY,
101   PROP_SENSITIVE,
102   PROP_VISIBLE,
103   PROP_ACTION_GROUP
104 };
105
106 static void gtk_action_init       (GtkAction *action);
107 static void gtk_action_class_init (GtkActionClass *class);
108
109 static GQuark       accel_path_id  = 0;
110 static const gchar accel_path_key[] = "GtkAction::accel_path";
111
112 GType
113 gtk_action_get_type (void)
114 {
115   static GtkType type = 0;
116
117   if (!type)
118     {
119       static const GTypeInfo type_info =
120       {
121         sizeof (GtkActionClass),
122         (GBaseInitFunc) NULL,
123         (GBaseFinalizeFunc) NULL,
124         (GClassInitFunc) gtk_action_class_init,
125         (GClassFinalizeFunc) NULL,
126         NULL,
127         
128         sizeof (GtkAction),
129         0, /* n_preallocs */
130         (GInstanceInitFunc) gtk_action_init,
131       };
132
133       type = g_type_register_static (G_TYPE_OBJECT,
134                                      "GtkAction",
135                                      &type_info, 0);
136     }
137   return type;
138 }
139
140 static void gtk_action_finalize     (GObject *object);
141 static void gtk_action_set_property (GObject         *object,
142                                      guint            prop_id,
143                                      const GValue    *value,
144                                      GParamSpec      *pspec);
145 static void gtk_action_get_property (GObject         *object,
146                                      guint            prop_id,
147                                      GValue          *value,
148                                      GParamSpec      *pspec);
149 static void gtk_action_set_action_group (GtkAction          *action,
150                                          GtkActionGroup *action_group);
151
152 static GtkWidget *create_menu_item    (GtkAction *action);
153 static GtkWidget *create_tool_item    (GtkAction *action);
154 static void       connect_proxy       (GtkAction     *action,
155                                        GtkWidget     *proxy);
156 static void       disconnect_proxy    (GtkAction *action,
157                                        GtkWidget *proxy);
158 static void       closure_accel_activate (GClosure     *closure,
159                                           GValue       *return_value,
160                                           guint         n_param_values,
161                                           const GValue *param_values,
162                                           gpointer      invocation_hint,
163                                           gpointer      marshal_data);
164
165 static GObjectClass *parent_class = NULL;
166 static guint         action_signals[LAST_SIGNAL] = { 0 };
167
168
169 static void
170 gtk_action_class_init (GtkActionClass *klass)
171 {
172   GObjectClass *gobject_class;
173
174   accel_path_id = g_quark_from_static_string (accel_path_key);
175
176   parent_class = g_type_class_peek_parent (klass);
177   gobject_class = G_OBJECT_CLASS (klass);
178
179   gobject_class->finalize     = gtk_action_finalize;
180   gobject_class->set_property = gtk_action_set_property;
181   gobject_class->get_property = gtk_action_get_property;
182
183   klass->activate = NULL;
184
185   klass->create_menu_item = create_menu_item;
186   klass->create_tool_item = create_tool_item;
187   klass->connect_proxy = connect_proxy;
188   klass->disconnect_proxy = disconnect_proxy;
189
190   klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
191   klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
192
193   g_object_class_install_property (gobject_class,
194                                    PROP_NAME,
195                                    g_param_spec_string ("name",
196                                                         P_("Name"),
197                                                         P_("A unique name for the action."),
198                                                         NULL,
199                                                         G_PARAM_READWRITE |
200                                                         G_PARAM_CONSTRUCT_ONLY));
201   g_object_class_install_property (gobject_class,
202                                    PROP_LABEL,
203                                    g_param_spec_string ("label",
204                                                         P_("Label"),
205                                                         P_("The label used for menu items and buttons "
206                                                            "that activate this action."),
207                                                         NULL,
208                                                         G_PARAM_READWRITE));
209   g_object_class_install_property (gobject_class,
210                                    PROP_SHORT_LABEL,
211                                    g_param_spec_string ("short_label",
212                                                         P_("Short label"),
213                                                         P_("A shorter label that may be used on toolbar buttons."),
214                                                         NULL,
215                                                         G_PARAM_READWRITE));
216   g_object_class_install_property (gobject_class,
217                                    PROP_TOOLTIP,
218                                    g_param_spec_string ("tooltip",
219                                                         P_("Tooltip"),
220                                                         P_("A tooltip for this action."),
221                                                         NULL,
222                                                         G_PARAM_READWRITE));
223   g_object_class_install_property (gobject_class,
224                                    PROP_STOCK_ID,
225                                    g_param_spec_string ("stock_id",
226                                                         P_("Stock Icon"),
227                                                         P_("The stock icon displayed in widgets representing "
228                                                            "this action."),
229                                                         NULL,
230                                                         G_PARAM_READWRITE));
231   g_object_class_install_property (gobject_class,
232                                    PROP_VISIBLE_HORIZONTAL,
233                                    g_param_spec_boolean ("visible_horizontal",
234                                                          P_("Visible when horizontal"),
235                                                          P_("Whether the toolbar item is visible when the toolbar "
236                                                             "is in a horizontal orientation."),
237                                                          TRUE,
238                                                          G_PARAM_READWRITE));
239   /**
240    * GtkAction:visible-overflown:
241    *
242    * When %TRUE, toolitem proxies for this action are represented in the 
243    * toolbar overflow menu.
244    *
245    * Since: 2.6
246    */
247   g_object_class_install_property (gobject_class,
248                                    PROP_VISIBLE_OVERFLOWN,
249                                    g_param_spec_boolean ("visible_overflown",
250                                                          P_("Visible when overflown"),
251                                                          P_("When TRUE, toolitem proxies for this action "
252                                                             "are represented in the toolbar overflow menu."),
253                                                          TRUE,
254                                                          G_PARAM_READWRITE));
255   g_object_class_install_property (gobject_class,
256                                    PROP_VISIBLE_VERTICAL,
257                                    g_param_spec_boolean ("visible_vertical",
258                                                          P_("Visible when vertical"),
259                                                          P_("Whether the toolbar item is visible when the toolbar "
260                                                             "is in a vertical orientation."),
261                                                          TRUE,
262                                                          G_PARAM_READWRITE));
263   g_object_class_install_property (gobject_class,
264                                    PROP_IS_IMPORTANT,
265                                    g_param_spec_boolean ("is_important",
266                                                          P_("Is important"),
267                                                          P_("Whether the action is considered important. "
268                                                             "When TRUE, toolitem proxies for this action "
269                                                             "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
270                                                          FALSE,
271                                                          G_PARAM_READWRITE));
272   g_object_class_install_property (gobject_class,
273                                    PROP_HIDE_IF_EMPTY,
274                                    g_param_spec_boolean ("hide_if_empty",
275                                                          P_("Hide if empty"),
276                                                          P_("When TRUE, empty menu proxies for this action are hidden."),
277                                                          TRUE,
278                                                          G_PARAM_READWRITE));
279   g_object_class_install_property (gobject_class,
280                                    PROP_SENSITIVE,
281                                    g_param_spec_boolean ("sensitive",
282                                                          P_("Sensitive"),
283                                                          P_("Whether the action is enabled."),
284                                                          TRUE,
285                                                          G_PARAM_READWRITE));
286   g_object_class_install_property (gobject_class,
287                                    PROP_VISIBLE,
288                                    g_param_spec_boolean ("visible",
289                                                          P_("Visible"),
290                                                          P_("Whether the action is visible."),
291                                                          TRUE,
292                                                          G_PARAM_READWRITE));
293   g_object_class_install_property (gobject_class,
294                                    PROP_ACTION_GROUP,
295                                    g_param_spec_object ("action_group",
296                                                          P_("Action Group"),
297                                                          P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
298                                                          GTK_TYPE_ACTION_GROUP,
299                                                          G_PARAM_READWRITE));
300
301   /**
302    * GtkAction::activate:
303    * @action: the #GtkAction
304    *
305    * The "activate" signal is emitted when the action is activated.
306    *
307    * Since: 2.4
308    */
309   action_signals[ACTIVATE] =
310     g_signal_new ("activate",
311                   G_OBJECT_CLASS_TYPE (klass),
312                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
313                   G_STRUCT_OFFSET (GtkActionClass, activate),  NULL, NULL,
314                   g_cclosure_marshal_VOID__VOID,
315                   G_TYPE_NONE, 0);
316
317   g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
318 }
319
320
321 static void
322 gtk_action_init (GtkAction *action)
323 {
324   action->private_data = GTK_ACTION_GET_PRIVATE (action);
325
326   action->private_data->name = NULL;
327   action->private_data->label = NULL;
328   action->private_data->short_label = NULL;
329   action->private_data->tooltip = NULL;
330   action->private_data->stock_id = NULL;
331   action->private_data->visible_horizontal = TRUE;
332   action->private_data->visible_vertical   = TRUE;
333   action->private_data->visible_overflown  = TRUE;
334   action->private_data->is_important = FALSE;
335   action->private_data->hide_if_empty = TRUE;
336
337   action->private_data->sensitive = TRUE;
338   action->private_data->visible = TRUE;
339
340   action->private_data->label_set = FALSE;
341   action->private_data->short_label_set = FALSE;
342
343   action->private_data->accel_count = 0;
344   action->private_data->accel_group = NULL;
345   action->private_data->accel_quark = 0;
346   action->private_data->accel_closure = 
347     g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
348   g_closure_set_marshal (action->private_data->accel_closure, 
349                          closure_accel_activate);
350   g_closure_ref (action->private_data->accel_closure);
351   g_closure_sink (action->private_data->accel_closure);
352
353   action->private_data->action_group = NULL;
354
355   action->private_data->proxies = NULL;
356 }
357
358 /**
359  * gtk_action_new:
360  * @name: A unique name for the action
361  * @label: the label displayed in menu items and on buttons
362  * @tooltip: a tooltip for the action
363  * @stock_id: the stock icon to display in widgets representing the action
364  *
365  * Creates a new #GtkAction object. To add the action to a
366  * #GtkActionGroup and set the accelerator for the action,
367  * call gtk_action_group_add_action_with_accel().
368  * See <xref linkend="XML-UI"/> for information on allowed action
369  * names.
370  *
371  * Return value: a new #GtkAction
372  *
373  * Since: 2.4
374  */
375 GtkAction *
376 gtk_action_new (const gchar *name,
377                 const gchar *label,
378                 const gchar *tooltip,
379                 const gchar *stock_id)
380 {
381   GtkAction *action;
382
383   action = g_object_new (GTK_TYPE_ACTION,
384                          "name", name,
385                          "label", label,
386                          "tooltip", tooltip,
387                          "stock_id", stock_id,
388                          NULL);
389
390   return action;
391 }
392
393 static void
394 gtk_action_finalize (GObject *object)
395 {
396   GtkAction *action;
397   action = GTK_ACTION (object);
398
399   g_free (action->private_data->name);
400   g_free (action->private_data->label);
401   g_free (action->private_data->short_label);
402   g_free (action->private_data->tooltip);
403   g_free (action->private_data->stock_id);
404
405   g_closure_unref (action->private_data->accel_closure);
406   if (action->private_data->accel_group)
407     g_object_unref (action->private_data->accel_group);
408
409   G_OBJECT_CLASS (parent_class)->finalize (object);  
410 }
411
412 static void
413 gtk_action_set_property (GObject         *object,
414                          guint            prop_id,
415                          const GValue    *value,
416                          GParamSpec      *pspec)
417 {
418   GtkAction *action;
419   gchar *tmp;
420   
421   action = GTK_ACTION (object);
422
423   switch (prop_id)
424     {
425     case PROP_NAME:
426       tmp = action->private_data->name;
427       action->private_data->name = g_value_dup_string (value);
428       g_free (tmp);
429       break;
430     case PROP_LABEL:
431       tmp = action->private_data->label;
432       action->private_data->label = g_value_dup_string (value);
433       g_free (tmp);
434       action->private_data->label_set = (action->private_data->label != NULL);
435       /* if label is unset, then use the label from the stock item */
436       if (!action->private_data->label_set && action->private_data->stock_id)
437         {
438           GtkStockItem stock_item;
439
440           if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
441             action->private_data->label = g_strdup (stock_item.label);
442         }
443       /* if short_label is unset, set short_label=label */
444       if (!action->private_data->short_label_set)
445         {
446           tmp = action->private_data->short_label;
447           action->private_data->short_label = g_strdup (action->private_data->label);
448           g_free (tmp);
449           g_object_notify (object, "short_label");
450         }
451       break;
452     case PROP_SHORT_LABEL:
453       tmp = action->private_data->short_label;
454       action->private_data->short_label = g_value_dup_string (value);
455       g_free (tmp);
456       action->private_data->short_label_set = (action->private_data->short_label != NULL);
457       /* if short_label is unset, then use the value of label */
458       if (!action->private_data->short_label_set)
459         {
460           action->private_data->short_label = g_strdup (action->private_data->label);
461         }
462       break;
463     case PROP_TOOLTIP:
464       tmp = action->private_data->tooltip;
465       action->private_data->tooltip = g_value_dup_string (value);
466       g_free (tmp);
467       break;
468     case PROP_STOCK_ID:
469       tmp = action->private_data->stock_id;
470       action->private_data->stock_id = g_value_dup_string (value);
471       g_free (tmp);
472       /* update label and short_label if appropriate */
473       if (!action->private_data->label_set)
474         {
475           GtkStockItem stock_item;
476
477           g_free (action->private_data->label);
478           if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
479             action->private_data->label = g_strdup (stock_item.label);
480           else
481             action->private_data->label = NULL;
482           g_object_notify (object, "label");
483         }
484       if (!action->private_data->short_label_set)
485         {
486           tmp = action->private_data->short_label;
487           action->private_data->short_label = g_strdup (action->private_data->label);
488           g_free (tmp);
489           g_object_notify (object, "short_label");
490         }
491       break;
492     case PROP_VISIBLE_HORIZONTAL:
493       action->private_data->visible_horizontal = g_value_get_boolean (value);
494       break;
495     case PROP_VISIBLE_VERTICAL:
496       action->private_data->visible_vertical = g_value_get_boolean (value);
497       break;
498     case PROP_VISIBLE_OVERFLOWN:
499       action->private_data->visible_overflown = g_value_get_boolean (value);
500       break;
501     case PROP_IS_IMPORTANT:
502       action->private_data->is_important = g_value_get_boolean (value);
503       break;
504     case PROP_HIDE_IF_EMPTY:
505       action->private_data->hide_if_empty = g_value_get_boolean (value);
506       break;
507     case PROP_SENSITIVE:
508       action->private_data->sensitive = g_value_get_boolean (value);
509       break;
510     case PROP_VISIBLE:
511       action->private_data->visible = g_value_get_boolean (value);
512       break;
513     case PROP_ACTION_GROUP:
514       gtk_action_set_action_group (action, g_value_get_object (value));
515       break;
516     default:
517       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
518       break;
519     }
520 }
521
522 static void
523 gtk_action_get_property (GObject    *object,
524                          guint       prop_id,
525                          GValue     *value,
526                          GParamSpec *pspec)
527 {
528   GtkAction *action;
529
530   action = GTK_ACTION (object);
531
532   switch (prop_id)
533     {
534     case PROP_NAME:
535       g_value_set_string (value, action->private_data->name);
536       break;
537     case PROP_LABEL:
538       g_value_set_string (value, action->private_data->label);
539       break;
540     case PROP_SHORT_LABEL:
541       g_value_set_string (value, action->private_data->short_label);
542       break;
543     case PROP_TOOLTIP:
544       g_value_set_string (value, action->private_data->tooltip);
545       break;
546     case PROP_STOCK_ID:
547       g_value_set_string (value, action->private_data->stock_id);
548       break;
549     case PROP_VISIBLE_HORIZONTAL:
550       g_value_set_boolean (value, action->private_data->visible_horizontal);
551       break;
552     case PROP_VISIBLE_VERTICAL:
553       g_value_set_boolean (value, action->private_data->visible_vertical);
554       break;
555     case PROP_VISIBLE_OVERFLOWN:
556       g_value_set_boolean (value, action->private_data->visible_overflown);
557       break;
558     case PROP_IS_IMPORTANT:
559       g_value_set_boolean (value, action->private_data->is_important);
560       break;
561     case PROP_HIDE_IF_EMPTY:
562       g_value_set_boolean (value, action->private_data->hide_if_empty);
563       break;
564     case PROP_SENSITIVE:
565       g_value_set_boolean (value, action->private_data->sensitive);
566       break;
567     case PROP_VISIBLE:
568       g_value_set_boolean (value, action->private_data->visible);
569       break;
570     case PROP_ACTION_GROUP:
571       g_value_set_object (value, action->private_data->action_group);
572       break;
573     default:
574       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
575       break;
576     }
577 }
578
579 static GtkWidget *
580 create_menu_item (GtkAction *action)
581 {
582   GType menu_item_type;
583
584   menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
585
586   return g_object_new (menu_item_type, NULL);
587 }
588
589 static GtkWidget *
590 create_tool_item (GtkAction *action)
591 {
592   GType toolbar_item_type;
593
594   toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
595
596   return g_object_new (toolbar_item_type, NULL);
597 }
598
599 static void
600 remove_proxy (GtkWidget *proxy, 
601               GtkAction *action)
602 {
603   if (GTK_IS_MENU_ITEM (proxy))
604     gtk_action_disconnect_accelerator (action);
605
606   action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
607 }
608
609 static void
610 gtk_action_sync_sensitivity (GtkAction  *action, 
611                              GParamSpec *pspec,
612                              GtkWidget  *proxy)
613 {
614   gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
615 }
616
617 static void
618 gtk_action_sync_property (GtkAction  *action, 
619                           GParamSpec *pspec,
620                           GtkWidget  *proxy)
621 {
622   const gchar *property;
623   GValue value = { 0, };
624
625   property = g_param_spec_get_name (pspec);
626
627   g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
628   g_object_get_property (G_OBJECT (action), property, &value);
629
630   g_object_set_property (G_OBJECT (proxy), property, &value);
631   g_value_unset (&value);
632 }
633
634 /**
635  * _gtk_action_sync_menu_visible:
636  * @action: a #GtkAction, or %NULL to determine the action from @proxy
637  * @proxy: a proxy menu item
638  * @empty: whether the submenu attached to @proxy is empty
639  * 
640  * Updates the visibility of @proxy from the visibility of @action
641  * according to the following rules:
642  * <itemizedlist>
643  * <listitem><para>if @action is invisible, @proxy is too
644  * </para></listitem>
645  * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty" 
646  *   property of @action indicates otherwise
647  * </para></listitem>
648  * </itemizedlist>
649  * 
650  * This function is used in the implementation of #GtkUIManager.
651  **/
652 void
653 _gtk_action_sync_menu_visible (GtkAction *action,
654                                GtkWidget *proxy,
655                                gboolean   empty)
656 {
657   gboolean visible, hide_if_empty;
658
659   g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
660   g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
661   
662   if (action == NULL)
663     action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
664
665   visible = gtk_action_is_visible (action);
666   hide_if_empty = action->private_data->hide_if_empty;
667
668   if (visible && !(empty && hide_if_empty))
669     gtk_widget_show (proxy);
670   else
671     gtk_widget_hide (proxy);
672 }
673
674 gboolean _gtk_menu_is_empty (GtkWidget *menu);
675
676 static void
677 gtk_action_sync_visible (GtkAction  *action, 
678                          GParamSpec *pspec,
679                          GtkWidget  *proxy)
680 {
681   if (GTK_IS_MENU_ITEM (proxy))
682     {
683       GtkWidget *menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
684
685       _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
686     }
687   else
688     {
689       if (gtk_action_is_visible (action))
690         gtk_widget_show (proxy);
691       else
692         gtk_widget_hide (proxy);
693     }
694 }
695
696 static void
697 gtk_action_sync_label (GtkAction  *action, 
698                        GParamSpec *pspec, 
699                        GtkWidget  *proxy)
700 {
701   GtkWidget *label = NULL;
702
703   g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
704   label = GTK_BIN (proxy)->child;
705
706   if (GTK_IS_LABEL (label))
707     gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
708 }
709
710 static void
711 gtk_action_sync_short_label (GtkAction  *action, 
712                              GParamSpec *pspec,
713                              GtkWidget  *proxy)
714 {
715   GValue value = { 0, };
716
717   g_value_init (&value, G_TYPE_STRING);
718   g_object_get_property (G_OBJECT (action), "short_label", &value);
719
720   g_object_set_property (G_OBJECT (proxy), "label", &value);
721   g_value_unset (&value);
722 }
723
724 static void
725 gtk_action_sync_stock_id (GtkAction  *action, 
726                           GParamSpec *pspec,
727                           GtkWidget  *proxy)
728 {
729   GtkWidget *image = NULL;
730
731   if (GTK_IS_IMAGE_MENU_ITEM (proxy))
732     {
733       image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
734
735       if (GTK_IS_IMAGE (image))
736         gtk_image_set_from_stock (GTK_IMAGE (image),
737                                   action->private_data->stock_id, GTK_ICON_SIZE_MENU);
738     }
739 }
740
741 static void
742 gtk_action_sync_button_stock_id (GtkAction  *action, 
743                                  GParamSpec *pspec,
744                                  GtkWidget  *proxy)
745 {
746   g_object_set (G_OBJECT (proxy),
747                 "stock-id",
748                 action->private_data->stock_id,
749                 NULL);
750 }
751
752 static void
753 gtk_action_sync_tooltip (GtkAction  *action, 
754                          GParamSpec *pspec, 
755                          GtkWidget  *proxy)
756 {
757   g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
758
759   if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
760     {
761       GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
762       
763       gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy), 
764                                  toolbar->tooltips,
765                                  action->private_data->tooltip,
766                                  NULL);
767     }
768 }
769
770
771 static gboolean
772 gtk_action_create_menu_proxy (GtkToolItem *tool_item, 
773                               GtkAction   *action)
774 {
775   GtkWidget *menu_item;
776   
777   if (action->private_data->visible_overflown)
778     {
779       menu_item = gtk_action_create_menu_item (action);
780
781       g_object_ref (menu_item);
782       gtk_object_sink (GTK_OBJECT (menu_item));
783       
784       gtk_tool_item_set_proxy_menu_item (tool_item, 
785                                          "gtk-action-menu-item", menu_item);
786       g_object_unref (menu_item);
787     }
788   else
789     gtk_tool_item_set_proxy_menu_item (tool_item, 
790                                        "gtk-action-menu-item", NULL);
791
792   return TRUE;
793 }
794
795 static void
796 connect_proxy (GtkAction     *action, 
797                GtkWidget     *proxy)
798 {
799   g_object_ref (action);
800   g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
801                           g_object_unref);
802
803   /* add this widget to the list of proxies */
804   action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
805   g_signal_connect (proxy, "destroy",
806                     G_CALLBACK (remove_proxy), action);
807
808   g_signal_connect_object (action, "notify::sensitive",
809                            G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
810   gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
811
812   g_signal_connect_object (action, "notify::visible",
813                            G_CALLBACK (gtk_action_sync_visible), proxy, 0);
814   if (gtk_action_is_visible (action))
815     gtk_widget_show (proxy);
816   else
817     gtk_widget_hide (proxy);
818   gtk_widget_set_no_show_all (proxy, TRUE);
819
820   if (GTK_IS_MENU_ITEM (proxy))
821     {
822       GtkWidget *label;
823       /* menu item specific synchronisers ... */
824       
825       if (action->private_data->accel_quark)
826         {
827           gtk_action_connect_accelerator (action);
828           gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
829                                         g_quark_to_string (action->private_data->accel_quark));
830         }
831       
832       label = GTK_BIN (proxy)->child;
833
834       /* make sure label is a label */
835       if (label && !GTK_IS_LABEL (label))
836         {
837           gtk_container_remove (GTK_CONTAINER (proxy), label);
838           label = NULL;
839         }
840
841       if (!label)
842         label = g_object_new (GTK_TYPE_ACCEL_LABEL,
843                               "use_underline", TRUE,
844                               "xalign", 0.0,
845                               "visible", TRUE,
846                               "parent", proxy,
847                               NULL);
848       
849       if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
850         g_object_set (label,
851                       "accel_closure", action->private_data->accel_closure,
852                       NULL);
853
854       gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
855       g_signal_connect_object (action, "notify::label",
856                                G_CALLBACK (gtk_action_sync_label), proxy, 0);
857
858       if (GTK_IS_IMAGE_MENU_ITEM (proxy))
859         {
860           GtkWidget *image;
861
862           image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
863           if (image && !GTK_IS_IMAGE (image))
864             {
865               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
866               image = NULL;
867             }
868           if (!image)
869             {
870               image = gtk_image_new_from_stock (NULL,
871                                                 GTK_ICON_SIZE_MENU);
872               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
873                                              image);
874               gtk_widget_show (image);
875             }
876           gtk_image_set_from_stock (GTK_IMAGE (image),
877                                     action->private_data->stock_id, GTK_ICON_SIZE_MENU);
878           g_signal_connect_object (action, "notify::stock-id",
879                                    G_CALLBACK (gtk_action_sync_stock_id),
880                                    proxy, 0);
881         }
882
883       if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
884         g_signal_connect_object (proxy, "activate",
885                                  G_CALLBACK (gtk_action_activate), action,
886                                  G_CONNECT_SWAPPED);
887
888     }
889   else if (GTK_IS_TOOL_ITEM (proxy))
890     {
891       /* toolbar item specific synchronisers ... */
892
893       g_object_set (proxy,
894                     "visible_horizontal", action->private_data->visible_horizontal,
895                     "visible_vertical",   action->private_data->visible_vertical,
896                     "is_important", action->private_data->is_important,
897                     NULL);
898       /* FIXME: we should set the tooltip here, but the current api
899        * doesn't allow it before the item is added to a toolbar. 
900        */
901       g_signal_connect_object (action, "notify::visible-horizontal",
902                                G_CALLBACK (gtk_action_sync_property), 
903                                proxy, 0);
904       g_signal_connect_object (action, "notify::visible-vertical",
905                                G_CALLBACK (gtk_action_sync_property), 
906                                proxy, 0);
907       g_signal_connect_object (action, "notify::is-important",
908                                G_CALLBACK (gtk_action_sync_property), 
909                                proxy, 0);
910       g_signal_connect_object (action, "notify::tooltip",
911                                G_CALLBACK (gtk_action_sync_tooltip), 
912                                proxy, 0);
913
914       g_signal_connect_object (proxy, "create_menu_proxy",
915                                G_CALLBACK (gtk_action_create_menu_proxy),
916                                action, 0);
917
918       gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
919
920       /* toolbar button specific synchronisers ... */
921       if (GTK_IS_TOOL_BUTTON (proxy))
922         {
923           g_object_set (proxy,
924                         "label", action->private_data->short_label,
925                         "use_underline", TRUE,
926                         "stock_id", action->private_data->stock_id,
927                         NULL);
928           /* FIXME: we should set the tooltip here, but the current api
929            * doesn't allow it before the item is added to a toolbar. 
930            */
931           g_signal_connect_object (action, "notify::short-label",
932                                    G_CALLBACK (gtk_action_sync_short_label),
933                                    proxy, 0);      
934           g_signal_connect_object (action, "notify::stock-id",
935                                    G_CALLBACK (gtk_action_sync_property), 
936                                    proxy, 0);
937           g_signal_connect_object (proxy, "clicked",
938                                    G_CALLBACK (gtk_action_activate), action,
939                                    G_CONNECT_SWAPPED);
940         }
941     }
942   else if (GTK_IS_BUTTON (proxy))
943     {
944       /* button specific synchronisers ... */
945       if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
946         {
947           /* synchronise stock-id */
948           g_object_set (proxy,
949                         "stock-id", action->private_data->stock_id,
950                         NULL);
951           g_signal_connect_object (action, "notify::stock-id",
952                                    G_CALLBACK (gtk_action_sync_button_stock_id),
953                                    proxy, 0);
954         }
955       else if (GTK_IS_LABEL(GTK_BIN(proxy)->child))
956         {
957           /* synchronise the label */
958           g_object_set (proxy,
959                         "label", action->private_data->short_label,
960                         "use_underline", TRUE,
961                         NULL);
962           g_signal_connect_object (action, "notify::short-label",
963                                    G_CALLBACK (gtk_action_sync_short_label),
964                                    proxy, 0);
965           
966         }
967       
968       /* we leave the button alone if there is a custom child */
969       g_signal_connect_object (proxy, "clicked",
970                                G_CALLBACK (gtk_action_activate), action,
971                                G_CONNECT_SWAPPED);
972     }
973
974   if (action->private_data->action_group)
975     _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
976 }
977
978 static void
979 disconnect_proxy (GtkAction *action, 
980                   GtkWidget *proxy)
981 {
982   g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
983
984   /* remove proxy from list of proxies */
985   g_signal_handlers_disconnect_by_func (proxy,
986                                         G_CALLBACK (remove_proxy),
987                                         action);
988   remove_proxy (proxy, action);
989
990   /* disconnect the activate handler */
991   g_signal_handlers_disconnect_by_func (proxy,
992                                         G_CALLBACK (gtk_action_activate),
993                                         action);
994
995   /* disconnect handlers for notify::* signals */
996   g_signal_handlers_disconnect_by_func (action,
997                                         G_CALLBACK (gtk_action_sync_sensitivity),
998                                         proxy);
999   g_signal_handlers_disconnect_by_func (action,
1000                                         G_CALLBACK (gtk_action_sync_property),
1001                                         proxy);
1002
1003   g_signal_handlers_disconnect_by_func (action,
1004                                 G_CALLBACK (gtk_action_sync_stock_id), proxy);
1005
1006   /* menu item specific synchronisers ... */
1007   g_signal_handlers_disconnect_by_func (action,
1008                                         G_CALLBACK (gtk_action_sync_label),
1009                                         proxy);
1010   
1011   /* toolbar button specific synchronisers ... */
1012   g_signal_handlers_disconnect_by_func (action,
1013                                         G_CALLBACK (gtk_action_sync_short_label),
1014                                         proxy);
1015
1016   g_signal_handlers_disconnect_by_func (proxy,
1017                                         G_CALLBACK (gtk_action_create_menu_proxy),
1018                                         action);
1019
1020   if (action->private_data->action_group)
1021     _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
1022 }
1023
1024 void
1025 _gtk_action_emit_activate (GtkAction *action)
1026 {
1027   GtkActionGroup *group = action->private_data->action_group;
1028
1029   if (group != NULL) 
1030     {
1031       g_object_ref (group);
1032       _gtk_action_group_emit_pre_activate (group, action);
1033     }
1034
1035     g_signal_emit (action, action_signals[ACTIVATE], 0);
1036
1037   if (group != NULL) 
1038     {
1039       _gtk_action_group_emit_post_activate (group, action);
1040       g_object_unref (group);
1041     }
1042 }
1043
1044 /**
1045  * gtk_action_activate:
1046  * @action: the action object
1047  *
1048  * Emits the "activate" signal on the specified action, if it isn't 
1049  * insensitive. This gets called by the proxy widgets when they get 
1050  * activated.
1051  *
1052  * It can also be used to manually activate an action.
1053  *
1054  * Since: 2.4
1055  */
1056 void
1057 gtk_action_activate (GtkAction *action)
1058 {
1059   g_return_if_fail (GTK_IS_ACTION (action));
1060   
1061   if (gtk_action_is_sensitive (action))
1062     _gtk_action_emit_activate (action);
1063 }
1064
1065 /**
1066  * gtk_action_create_icon:
1067  * @action: the action object
1068  * @icon_size: the size of the icon that should be created.
1069  *
1070  * This function is intended for use by action implementations to
1071  * create icons displayed in the proxy widgets.
1072  *
1073  * Returns: a widget that displays the icon for this action.
1074  *
1075  * Since: 2.4
1076  */
1077 GtkWidget *
1078 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1079 {
1080   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1081
1082   if (action->private_data->stock_id)
1083     return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1084   else
1085     return NULL;
1086 }
1087
1088 /**
1089  * gtk_action_create_menu_item:
1090  * @action: the action object
1091  *
1092  * Creates a menu item widget that proxies for the given action.
1093  *
1094  * Returns: a menu item connected to the action.
1095  *
1096  * Since: 2.4
1097  */
1098 GtkWidget *
1099 gtk_action_create_menu_item (GtkAction *action)
1100 {
1101   GtkWidget *menu_item;
1102
1103   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1104
1105   menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1106
1107   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1108
1109   return menu_item;
1110 }
1111
1112 /**
1113  * gtk_action_create_tool_item:
1114  * @action: the action object
1115  *
1116  * Creates a toolbar item widget that proxies for the given action.
1117  *
1118  * Returns: a toolbar item connected to the action.
1119  *
1120  * Since: 2.4
1121  */
1122 GtkWidget *
1123 gtk_action_create_tool_item (GtkAction *action)
1124 {
1125   GtkWidget *button;
1126
1127   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1128
1129   button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1130
1131   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1132
1133   return button;
1134 }
1135
1136 /**
1137  * gtk_action_connect_proxy:
1138  * @action: the action object
1139  * @proxy: the proxy widget
1140  *
1141  * Connects a widget to an action object as a proxy.  Synchronises 
1142  * various properties of the action with the widget (such as label 
1143  * text, icon, tooltip, etc), and attaches a callback so that the 
1144  * action gets activated when the proxy widget does.
1145  *
1146  * If the widget is already connected to an action, it is disconnected
1147  * first.
1148  *
1149  * Since: 2.4
1150  */
1151 void
1152 gtk_action_connect_proxy (GtkAction *action,
1153                           GtkWidget *proxy)
1154 {
1155   GtkAction *prev_action;
1156
1157   g_return_if_fail (GTK_IS_ACTION (action));
1158   g_return_if_fail (GTK_IS_WIDGET (proxy));
1159
1160   prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1161
1162   if (prev_action)
1163     (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);  
1164
1165   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1166 }
1167
1168 /**
1169  * gtk_action_disconnect_proxy:
1170  * @action: the action object
1171  * @proxy: the proxy widget
1172  *
1173  * Disconnects a proxy widget from an action.  
1174  * Does <emphasis>not</emphasis> destroy the widget, however.
1175  *
1176  * Since: 2.4
1177  */
1178 void
1179 gtk_action_disconnect_proxy (GtkAction *action,
1180                              GtkWidget *proxy)
1181 {
1182   g_return_if_fail (GTK_IS_ACTION (action));
1183   g_return_if_fail (GTK_IS_WIDGET (proxy));
1184
1185   g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1186
1187   (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);  
1188 }
1189
1190 /**
1191  * gtk_action_get_proxies:
1192  * @action: the action object
1193  * 
1194  * Returns the proxy widgets for an action.
1195  * 
1196  * Return value: a #GSList of proxy widgets. The list is owned by the action and
1197  * must not be modified.
1198  *
1199  * Since: 2.4
1200  **/
1201 GSList*
1202 gtk_action_get_proxies (GtkAction *action)
1203 {
1204   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1205
1206   return action->private_data->proxies;
1207 }
1208
1209
1210 /**
1211  * gtk_action_get_name:
1212  * @action: the action object
1213  * 
1214  * Returns the name of the action.
1215  * 
1216  * Return value: the name of the action. The string belongs to GTK+ and should not
1217  *   be freed.
1218  *
1219  * Since: 2.4
1220  **/
1221 G_CONST_RETURN gchar *
1222 gtk_action_get_name (GtkAction *action)
1223 {
1224   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1225
1226   return action->private_data->name;
1227 }
1228
1229 /**
1230  * gtk_action_is_sensitive:
1231  * @action: the action object
1232  * 
1233  * Returns whether the action is effectively sensitive.
1234  *
1235  * Return value: %TRUE if the action and its associated action group 
1236  * are both sensitive.
1237  *
1238  * Since: 2.4
1239  **/
1240 gboolean
1241 gtk_action_is_sensitive (GtkAction *action)
1242 {
1243   GtkActionPrivate *priv;
1244   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1245
1246   priv = action->private_data;
1247   return priv->sensitive &&
1248     (priv->action_group == NULL ||
1249      gtk_action_group_get_sensitive (priv->action_group));
1250 }
1251
1252 /**
1253  * gtk_action_get_sensitive:
1254  * @action: the action object
1255  * 
1256  * Returns whether the action itself is sensitive. Note that this doesn't 
1257  * necessarily mean effective sensitivity. See gtk_action_is_sensitive() 
1258  * for that.
1259  *
1260  * Return value: %TRUE if the action itself is sensitive.
1261  *
1262  * Since: 2.4
1263  **/
1264 gboolean
1265 gtk_action_get_sensitive (GtkAction *action)
1266 {
1267   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1268
1269   return action->private_data->sensitive;
1270 }
1271
1272 /**
1273  * gtk_action_set_sensitive:
1274  * @action: the action object
1275  * @sensitive: %TRUE to make the action sensitive
1276  * 
1277  * Sets the ::sensitive property of the action to @sensitive. Note that 
1278  * this doesn't necessarily mean effective sensitivity. See 
1279  * gtk_action_is_sensitive() 
1280  * for that.
1281  *
1282  * Since: 2.6
1283  **/
1284 void
1285 gtk_action_set_sensitive (GtkAction *action,
1286                           gboolean   sensitive)
1287 {
1288   g_return_if_fail (GTK_IS_ACTION (action));
1289
1290   sensitive = sensitive != FALSE;
1291   
1292   if (action->private_data->sensitive != sensitive)
1293     {
1294       action->private_data->sensitive = sensitive;
1295
1296       g_object_notify (G_OBJECT (action), "sensitive");
1297     }
1298 }
1299
1300 /**
1301  * gtk_action_is_visible:
1302  * @action: the action object
1303  * 
1304  * Returns whether the action is effectively visible.
1305  *
1306  * Return value: %TRUE if the action and its associated action group 
1307  * are both visible.
1308  *
1309  * Since: 2.4
1310  **/
1311 gboolean
1312 gtk_action_is_visible (GtkAction *action)
1313 {
1314   GtkActionPrivate *priv;
1315   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1316
1317   priv = action->private_data;
1318   return priv->visible &&
1319     (priv->action_group == NULL ||
1320      gtk_action_group_get_visible (priv->action_group));
1321 }
1322
1323 /**
1324  * gtk_action_get_visible:
1325  * @action: the action object
1326  * 
1327  * Returns whether the action itself is visible. Note that this doesn't 
1328  * necessarily mean effective visibility. See gtk_action_is_sensitive() 
1329  * for that.
1330  *
1331  * Return value: %TRUE if the action itself is visible.
1332  *
1333  * Since: 2.4
1334  **/
1335 gboolean
1336 gtk_action_get_visible (GtkAction *action)
1337 {
1338   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1339
1340   return action->private_data->visible;
1341 }
1342
1343 /**
1344  * gtk_action_set_visible:
1345  * @action: the action object
1346  * @visible: %TRUE to make the action visible
1347  * 
1348  * Sets the ::visible property of the action to @visible. Note that 
1349  * this doesn't necessarily mean effective visibility. See 
1350  * gtk_action_is_visible() 
1351  * for that.
1352  *
1353  * Since: 2.6
1354  **/
1355 void
1356 gtk_action_set_visible (GtkAction *action,
1357                         gboolean   visible)
1358 {
1359   g_return_if_fail (GTK_IS_ACTION (action));
1360
1361   visible = visible != FALSE;
1362   
1363   if (action->private_data->visible != visible)
1364     {
1365       action->private_data->visible = visible;
1366
1367       g_object_notify (G_OBJECT (action), "visible");
1368     }
1369 }
1370
1371 /**
1372  * gtk_action_block_activate_from:
1373  * @action: the action object
1374  * @proxy: a proxy widget
1375  *
1376  * Disables calls to the gtk_action_activate()
1377  * function by signals on the given proxy widget.  This is used to
1378  * break notification loops for things like check or radio actions.
1379  *
1380  * This function is intended for use by action implementations.
1381  * 
1382  * Since: 2.4
1383  */
1384 void
1385 gtk_action_block_activate_from (GtkAction *action, 
1386                                 GtkWidget *proxy)
1387 {
1388   g_return_if_fail (GTK_IS_ACTION (action));
1389   
1390   g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1391                                    action);
1392 }
1393
1394 /**
1395  * gtk_action_unblock_activate_from:
1396  * @action: the action object
1397  * @proxy: a proxy widget
1398  *
1399  * Re-enables calls to the gtk_action_activate()
1400  * function by signals on the given proxy widget.  This undoes the
1401  * blocking done by gtk_action_block_activate_from().
1402  *
1403  * This function is intended for use by action implementations.
1404  * 
1405  * Since: 2.4
1406  */
1407 void
1408 gtk_action_unblock_activate_from (GtkAction *action, 
1409                                   GtkWidget *proxy)
1410 {
1411   g_return_if_fail (GTK_IS_ACTION (action));
1412
1413   g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1414                                      action);
1415 }
1416
1417 static void
1418 closure_accel_activate (GClosure     *closure,
1419                         GValue       *return_value,
1420                         guint         n_param_values,
1421                         const GValue *param_values,
1422                         gpointer      invocation_hint,
1423                         gpointer      marshal_data)
1424 {
1425   if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1426     {
1427       _gtk_action_emit_activate (GTK_ACTION (closure->data));
1428       
1429       /* we handled the accelerator */
1430       g_value_set_boolean (return_value, TRUE);
1431     }
1432 }
1433
1434 static void
1435 gtk_action_set_action_group (GtkAction      *action,
1436                              GtkActionGroup *action_group)
1437 {
1438   g_return_if_fail (GTK_IS_ACTION (action));
1439
1440   if (action->private_data->action_group == NULL)
1441     g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1442   else
1443     g_return_if_fail (action_group == NULL);
1444
1445   action->private_data->action_group = action_group;
1446 }
1447
1448 /**
1449  * gtk_action_set_accel_path:
1450  * @action: the action object
1451  * @accel_path: the accelerator path
1452  *
1453  * Sets the accel path for this action.  All proxy widgets associated
1454  * with the action will have this accel path, so that their
1455  * accelerators are consistent.
1456  *
1457  * Since: 2.4
1458  */
1459 void
1460 gtk_action_set_accel_path (GtkAction   *action, 
1461                            const gchar *accel_path)
1462 {
1463   g_return_if_fail (GTK_IS_ACTION (action));
1464
1465   action->private_data->accel_quark = g_quark_from_string (accel_path);
1466 }
1467
1468 /**
1469  * gtk_action_get_accel_path:
1470  * @action: the action object
1471  *
1472  * Returns the accel path for this action.  
1473  *
1474  * Since: 2.6
1475  *
1476  * Returns: the accel path for this action, or %NULL
1477  *   if none is set. The returned string is owned by GTK+
1478  *   and must not be freed or modified.
1479  */
1480 G_CONST_RETURN gchar *
1481 gtk_action_get_accel_path (GtkAction *action)
1482 {
1483   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1484
1485   if (action->private_data->accel_quark)
1486     return g_quark_to_string (action->private_data->accel_quark);
1487   else
1488     return NULL;
1489 }
1490
1491
1492 /**
1493  * gtk_action_set_accel_group:
1494  * @action: the action object
1495  * @accel_group: a #GtkAccelGroup or %NULL
1496  * 
1497  * Sets the #GtkAccelGroup in which the accelerator for this action
1498  * will be installed.
1499  *
1500  * Since: 2.4
1501  **/
1502 void
1503 gtk_action_set_accel_group (GtkAction     *action,
1504                             GtkAccelGroup *accel_group)
1505 {
1506   g_return_if_fail (GTK_IS_ACTION (action));
1507   g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1508   
1509   if (accel_group)
1510     g_object_ref (accel_group);
1511   if (action->private_data->accel_group)
1512     g_object_unref (action->private_data->accel_group);
1513
1514   action->private_data->accel_group = accel_group;
1515 }
1516
1517 /**
1518  * gtk_action_connect_accelerator:
1519  * @action: a #GtkAction
1520  * 
1521  * Installs the accelerator for @action if @action has an
1522  * accel path and group. See gtk_action_set_accel_path() and 
1523  * gtk_action_set_accel_group()
1524  *
1525  * Since multiple proxies may independently trigger the installation
1526  * of the accelerator, the @action counts the number of times this
1527  * function has been called and doesn't remove the accelerator until
1528  * gtk_action_disconnect_accelerator() has been called as many times.
1529  *
1530  * Since: 2.4
1531  **/
1532 void 
1533 gtk_action_connect_accelerator (GtkAction *action)
1534 {
1535   g_return_if_fail (GTK_IS_ACTION (action));
1536
1537   if (!action->private_data->accel_quark ||
1538       !action->private_data->accel_group)
1539     return;
1540
1541   if (action->private_data->accel_count == 0)
1542     {
1543       const gchar *accel_path = 
1544         g_quark_to_string (action->private_data->accel_quark);
1545       
1546       gtk_accel_group_connect_by_path (action->private_data->accel_group,
1547                                        accel_path,
1548                                        action->private_data->accel_closure);
1549     }
1550
1551   action->private_data->accel_count++;
1552 }
1553
1554 /**
1555  * gtk_action_disconnect_accelerator:
1556  * @action: a #GtkAction
1557  * 
1558  * Undoes the effect of one call to gtk_action_connect_accelerator().
1559  *
1560  * Since: 2.4
1561  **/
1562 void 
1563 gtk_action_disconnect_accelerator (GtkAction *action)
1564 {
1565   g_return_if_fail (GTK_IS_ACTION (action));
1566
1567   if (!action->private_data->accel_quark ||
1568       !action->private_data->accel_group)
1569     return;
1570
1571   action->private_data->accel_count--;
1572
1573   if (action->private_data->accel_count == 0)
1574     gtk_accel_group_disconnect (action->private_data->accel_group,
1575                                 action->private_data->accel_closure);
1576 }