]> Pileus Git - ~andy/gtk/blob - gtk/gtkaction.c
gdk/linux-fb/gdkgc-fb.c gdk/win32/gdkgc-win32.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_tooltip (GtkAction  *action, 
743                          GParamSpec *pspec, 
744                          GtkWidget  *proxy)
745 {
746   g_return_if_fail (GTK_IS_TOOL_ITEM (proxy));
747
748   if (GTK_IS_TOOLBAR (gtk_widget_get_parent (proxy)))
749     {
750       GtkToolbar *toolbar = GTK_TOOLBAR (gtk_widget_get_parent (proxy));
751       
752       gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (proxy), 
753                                  toolbar->tooltips,
754                                  action->private_data->tooltip,
755                                  NULL);
756     }
757 }
758
759
760 static gboolean
761 gtk_action_create_menu_proxy (GtkToolItem *tool_item, 
762                               GtkAction   *action)
763 {
764   GtkWidget *menu_item;
765   
766   if (action->private_data->visible_overflown)
767     {
768       menu_item = gtk_action_create_menu_item (action);
769
770       g_object_ref (menu_item);
771       gtk_object_sink (GTK_OBJECT (menu_item));
772       
773       gtk_tool_item_set_proxy_menu_item (tool_item, 
774                                          "gtk-action-menu-item", menu_item);
775       g_object_unref (menu_item);
776     }
777   else
778     gtk_tool_item_set_proxy_menu_item (tool_item, 
779                                        "gtk-action-menu-item", NULL);
780
781   return TRUE;
782 }
783
784 static void
785 connect_proxy (GtkAction     *action, 
786                GtkWidget     *proxy)
787 {
788   g_object_ref (action);
789   g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
790                           g_object_unref);
791
792   /* add this widget to the list of proxies */
793   action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
794   g_signal_connect (proxy, "destroy",
795                     G_CALLBACK (remove_proxy), action);
796
797   g_signal_connect_object (action, "notify::sensitive",
798                            G_CALLBACK (gtk_action_sync_sensitivity), proxy, 0);
799   gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
800
801   g_signal_connect_object (action, "notify::visible",
802                            G_CALLBACK (gtk_action_sync_visible), proxy, 0);
803   if (gtk_action_is_visible (action))
804     gtk_widget_show (proxy);
805   else
806     gtk_widget_hide (proxy);
807   gtk_widget_set_no_show_all (proxy, TRUE);
808
809   if (GTK_IS_MENU_ITEM (proxy))
810     {
811       GtkWidget *label;
812       /* menu item specific synchronisers ... */
813       
814       if (action->private_data->accel_quark)
815         {
816           gtk_action_connect_accelerator (action);
817           gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
818                                         g_quark_to_string (action->private_data->accel_quark));
819         }
820       
821       label = GTK_BIN (proxy)->child;
822
823       /* make sure label is a label */
824       if (label && !GTK_IS_LABEL (label))
825         {
826           gtk_container_remove (GTK_CONTAINER (proxy), label);
827           label = NULL;
828         }
829
830       if (!label)
831         label = g_object_new (GTK_TYPE_ACCEL_LABEL,
832                               "use_underline", TRUE,
833                               "xalign", 0.0,
834                               "visible", TRUE,
835                               "parent", proxy,
836                               NULL);
837       
838       if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
839         g_object_set (label,
840                       "accel_closure", action->private_data->accel_closure,
841                       NULL);
842
843       gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
844       g_signal_connect_object (action, "notify::label",
845                                G_CALLBACK (gtk_action_sync_label), proxy, 0);
846
847       if (GTK_IS_IMAGE_MENU_ITEM (proxy))
848         {
849           GtkWidget *image;
850
851           image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
852           if (image && !GTK_IS_IMAGE (image))
853             {
854               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
855               image = NULL;
856             }
857           if (!image)
858             {
859               image = gtk_image_new_from_stock (NULL,
860                                                 GTK_ICON_SIZE_MENU);
861               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
862                                              image);
863               gtk_widget_show (image);
864             }
865           gtk_image_set_from_stock (GTK_IMAGE (image),
866                                     action->private_data->stock_id, GTK_ICON_SIZE_MENU);
867           g_signal_connect_object (action, "notify::stock-id",
868                                    G_CALLBACK (gtk_action_sync_stock_id),
869                                    proxy, 0);
870         }
871
872       if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
873         g_signal_connect_object (proxy, "activate",
874                                  G_CALLBACK (gtk_action_activate), action,
875                                  G_CONNECT_SWAPPED);
876
877     }
878   else if (GTK_IS_TOOL_ITEM (proxy))
879     {
880       /* toolbar item specific synchronisers ... */
881
882       g_object_set (proxy,
883                     "visible_horizontal", action->private_data->visible_horizontal,
884                     "visible_vertical",   action->private_data->visible_vertical,
885                     "is_important", action->private_data->is_important,
886                     NULL);
887       /* FIXME: we should set the tooltip here, but the current api
888        * doesn't allow it before the item is added to a toolbar. 
889        */
890       g_signal_connect_object (action, "notify::visible-horizontal",
891                                G_CALLBACK (gtk_action_sync_property), 
892                                proxy, 0);
893       g_signal_connect_object (action, "notify::visible-vertical",
894                                G_CALLBACK (gtk_action_sync_property), 
895                                proxy, 0);
896       g_signal_connect_object (action, "notify::is-important",
897                                G_CALLBACK (gtk_action_sync_property), 
898                                proxy, 0);
899       g_signal_connect_object (action, "notify::tooltip",
900                                G_CALLBACK (gtk_action_sync_tooltip), 
901                                proxy, 0);
902
903       g_signal_connect_object (proxy, "create_menu_proxy",
904                                G_CALLBACK (gtk_action_create_menu_proxy),
905                                action, 0);
906
907       gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));
908
909       /* toolbar button specific synchronisers ... */
910       if (GTK_IS_TOOL_BUTTON (proxy))
911         {
912           g_object_set (proxy,
913                         "label", action->private_data->short_label,
914                         "use_underline", TRUE,
915                         "stock_id", action->private_data->stock_id,
916                         NULL);
917           /* FIXME: we should set the tooltip here, but the current api
918            * doesn't allow it before the item is added to a toolbar. 
919            */
920           g_signal_connect_object (action, "notify::short-label",
921                                    G_CALLBACK (gtk_action_sync_short_label),
922                                    proxy, 0);      
923           g_signal_connect_object (action, "notify::stock-id",
924                                    G_CALLBACK (gtk_action_sync_property), 
925                                    proxy, 0);
926           g_signal_connect_object (proxy, "clicked",
927                                    G_CALLBACK (gtk_action_activate), action,
928                                    G_CONNECT_SWAPPED);
929         }
930     }
931   else if (GTK_IS_BUTTON (proxy))
932     {
933       /* button specific synchronisers ... */
934
935       /* synchronise the label */
936       g_object_set (proxy,
937                     "label", action->private_data->short_label,
938                     "use_underline", TRUE,
939                     NULL);
940       g_signal_connect_object (action, "notify::short-label",
941                                G_CALLBACK (gtk_action_sync_short_label),
942                                proxy, 0);
943       
944       g_signal_connect_object (proxy, "clicked",
945                                G_CALLBACK (gtk_action_activate), action,
946                                G_CONNECT_SWAPPED);
947     }
948
949   if (action->private_data->action_group)
950     _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
951 }
952
953 static void
954 disconnect_proxy (GtkAction *action, 
955                   GtkWidget *proxy)
956 {
957   g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
958
959   /* remove proxy from list of proxies */
960   g_signal_handlers_disconnect_by_func (proxy,
961                                         G_CALLBACK (remove_proxy),
962                                         action);
963   remove_proxy (proxy, action);
964
965   /* disconnect the activate handler */
966   g_signal_handlers_disconnect_by_func (proxy,
967                                         G_CALLBACK (gtk_action_activate),
968                                         action);
969
970   /* disconnect handlers for notify::* signals */
971   g_signal_handlers_disconnect_by_func (action,
972                                         G_CALLBACK (gtk_action_sync_sensitivity),
973                                         proxy);
974   g_signal_handlers_disconnect_by_func (action,
975                                         G_CALLBACK (gtk_action_sync_property),
976                                         proxy);
977
978   g_signal_handlers_disconnect_by_func (action,
979                                 G_CALLBACK (gtk_action_sync_stock_id), proxy);
980
981   /* menu item specific synchronisers ... */
982   g_signal_handlers_disconnect_by_func (action,
983                                         G_CALLBACK (gtk_action_sync_label),
984                                         proxy);
985   
986   /* toolbar button specific synchronisers ... */
987   g_signal_handlers_disconnect_by_func (action,
988                                         G_CALLBACK (gtk_action_sync_short_label),
989                                         proxy);
990
991   g_signal_handlers_disconnect_by_func (proxy,
992                                         G_CALLBACK (gtk_action_create_menu_proxy),
993                                         action);
994
995   if (action->private_data->action_group)
996     _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
997 }
998
999 void
1000 _gtk_action_emit_activate (GtkAction *action)
1001 {
1002   GtkActionGroup *group = action->private_data->action_group;
1003
1004   if (group != NULL) 
1005     {
1006       g_object_ref (group);
1007       _gtk_action_group_emit_pre_activate (group, action);
1008     }
1009
1010     g_signal_emit (action, action_signals[ACTIVATE], 0);
1011
1012   if (group != NULL) 
1013     {
1014       _gtk_action_group_emit_post_activate (group, action);
1015       g_object_unref (group);
1016     }
1017 }
1018
1019 /**
1020  * gtk_action_activate:
1021  * @action: the action object
1022  *
1023  * Emits the "activate" signal on the specified action, if it isn't 
1024  * insensitive. This gets called by the proxy widgets when they get 
1025  * activated.
1026  *
1027  * It can also be used to manually activate an action.
1028  *
1029  * Since: 2.4
1030  */
1031 void
1032 gtk_action_activate (GtkAction *action)
1033 {
1034   g_return_if_fail (GTK_IS_ACTION (action));
1035   
1036   if (gtk_action_is_sensitive (action))
1037     _gtk_action_emit_activate (action);
1038 }
1039
1040 /**
1041  * gtk_action_create_icon:
1042  * @action: the action object
1043  * @icon_size: the size of the icon that should be created.
1044  *
1045  * This function is intended for use by action implementations to
1046  * create icons displayed in the proxy widgets.
1047  *
1048  * Returns: a widget that displays the icon for this action.
1049  *
1050  * Since: 2.4
1051  */
1052 GtkWidget *
1053 gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
1054 {
1055   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1056
1057   if (action->private_data->stock_id)
1058     return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
1059   else
1060     return NULL;
1061 }
1062
1063 /**
1064  * gtk_action_create_menu_item:
1065  * @action: the action object
1066  *
1067  * Creates a menu item widget that proxies for the given action.
1068  *
1069  * Returns: a menu item connected to the action.
1070  *
1071  * Since: 2.4
1072  */
1073 GtkWidget *
1074 gtk_action_create_menu_item (GtkAction *action)
1075 {
1076   GtkWidget *menu_item;
1077
1078   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1079
1080   menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
1081
1082   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
1083
1084   return menu_item;
1085 }
1086
1087 /**
1088  * gtk_action_create_tool_item:
1089  * @action: the action object
1090  *
1091  * Creates a toolbar item widget that proxies for the given action.
1092  *
1093  * Returns: a toolbar item connected to the action.
1094  *
1095  * Since: 2.4
1096  */
1097 GtkWidget *
1098 gtk_action_create_tool_item (GtkAction *action)
1099 {
1100   GtkWidget *button;
1101
1102   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1103
1104   button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
1105
1106   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
1107
1108   return button;
1109 }
1110
1111 /**
1112  * gtk_action_connect_proxy:
1113  * @action: the action object
1114  * @proxy: the proxy widget
1115  *
1116  * Connects a widget to an action object as a proxy.  Synchronises 
1117  * various properties of the action with the widget (such as label 
1118  * text, icon, tooltip, etc), and attaches a callback so that the 
1119  * action gets activated when the proxy widget does.
1120  *
1121  * If the widget is already connected to an action, it is disconnected
1122  * first.
1123  *
1124  * Since: 2.4
1125  */
1126 void
1127 gtk_action_connect_proxy (GtkAction *action,
1128                           GtkWidget *proxy)
1129 {
1130   GtkAction *prev_action;
1131
1132   g_return_if_fail (GTK_IS_ACTION (action));
1133   g_return_if_fail (GTK_IS_WIDGET (proxy));
1134
1135   prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
1136
1137   if (prev_action)
1138     (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);  
1139
1140   (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
1141 }
1142
1143 /**
1144  * gtk_action_disconnect_proxy:
1145  * @action: the action object
1146  * @proxy: the proxy widget
1147  *
1148  * Disconnects a proxy widget from an action.  
1149  * Does <emphasis>not</emphasis> destroy the widget, however.
1150  *
1151  * Since: 2.4
1152  */
1153 void
1154 gtk_action_disconnect_proxy (GtkAction *action,
1155                              GtkWidget *proxy)
1156 {
1157   g_return_if_fail (GTK_IS_ACTION (action));
1158   g_return_if_fail (GTK_IS_WIDGET (proxy));
1159
1160   g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") == action);
1161
1162   (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);  
1163 }
1164
1165 /**
1166  * gtk_action_get_proxies:
1167  * @action: the action object
1168  * 
1169  * Returns the proxy widgets for an action.
1170  * 
1171  * Return value: a #GSList of proxy widgets. The list is owned by the action and
1172  * must not be modified.
1173  *
1174  * Since: 2.4
1175  **/
1176 GSList*
1177 gtk_action_get_proxies (GtkAction *action)
1178 {
1179   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1180
1181   return action->private_data->proxies;
1182 }
1183
1184
1185 /**
1186  * gtk_action_get_name:
1187  * @action: the action object
1188  * 
1189  * Returns the name of the action.
1190  * 
1191  * Return value: the name of the action. The string belongs to GTK+ and should not
1192  *   be freed.
1193  *
1194  * Since: 2.4
1195  **/
1196 G_CONST_RETURN gchar *
1197 gtk_action_get_name (GtkAction *action)
1198 {
1199   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1200
1201   return action->private_data->name;
1202 }
1203
1204 /**
1205  * gtk_action_is_sensitive:
1206  * @action: the action object
1207  * 
1208  * Returns whether the action is effectively sensitive.
1209  *
1210  * Return value: %TRUE if the action and its associated action group 
1211  * are both sensitive.
1212  *
1213  * Since: 2.4
1214  **/
1215 gboolean
1216 gtk_action_is_sensitive (GtkAction *action)
1217 {
1218   GtkActionPrivate *priv;
1219   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1220
1221   priv = action->private_data;
1222   return priv->sensitive &&
1223     (priv->action_group == NULL ||
1224      gtk_action_group_get_sensitive (priv->action_group));
1225 }
1226
1227 /**
1228  * gtk_action_get_sensitive:
1229  * @action: the action object
1230  * 
1231  * Returns whether the action itself is sensitive. Note that this doesn't 
1232  * necessarily mean effective sensitivity. See gtk_action_is_sensitive() 
1233  * for that.
1234  *
1235  * Return value: %TRUE if the action itself is sensitive.
1236  *
1237  * Since: 2.4
1238  **/
1239 gboolean
1240 gtk_action_get_sensitive (GtkAction *action)
1241 {
1242   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1243
1244   return action->private_data->sensitive;
1245 }
1246
1247 /**
1248  * gtk_action_set_sensitive:
1249  * @action: the action object
1250  * @sensitive: %TRUE to make the action sensitive
1251  * 
1252  * Sets the ::sensitive property of the action to @sensitive. Note that 
1253  * this doesn't necessarily mean effective sensitivity. See 
1254  * gtk_action_is_sensitive() 
1255  * for that.
1256  *
1257  * Since: 2.6
1258  **/
1259 void
1260 gtk_action_set_sensitive (GtkAction *action,
1261                           gboolean   sensitive)
1262 {
1263   g_return_if_fail (GTK_IS_ACTION (action));
1264
1265   sensitive = sensitive != FALSE;
1266   
1267   if (action->private_data->sensitive != sensitive)
1268     {
1269       action->private_data->sensitive = sensitive;
1270
1271       g_object_notify (G_OBJECT (action), "sensitive");
1272     }
1273 }
1274
1275 /**
1276  * gtk_action_is_visible:
1277  * @action: the action object
1278  * 
1279  * Returns whether the action is effectively visible.
1280  *
1281  * Return value: %TRUE if the action and its associated action group 
1282  * are both visible.
1283  *
1284  * Since: 2.4
1285  **/
1286 gboolean
1287 gtk_action_is_visible (GtkAction *action)
1288 {
1289   GtkActionPrivate *priv;
1290   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1291
1292   priv = action->private_data;
1293   return priv->visible &&
1294     (priv->action_group == NULL ||
1295      gtk_action_group_get_visible (priv->action_group));
1296 }
1297
1298 /**
1299  * gtk_action_get_visible:
1300  * @action: the action object
1301  * 
1302  * Returns whether the action itself is visible. Note that this doesn't 
1303  * necessarily mean effective visibility. See gtk_action_is_sensitive() 
1304  * for that.
1305  *
1306  * Return value: %TRUE if the action itself is visible.
1307  *
1308  * Since: 2.4
1309  **/
1310 gboolean
1311 gtk_action_get_visible (GtkAction *action)
1312 {
1313   g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);
1314
1315   return action->private_data->visible;
1316 }
1317
1318 /**
1319  * gtk_action_set_visible:
1320  * @action: the action object
1321  * @visible: %TRUE to make the action visible
1322  * 
1323  * Sets the ::visible property of the action to @visible. Note that 
1324  * this doesn't necessarily mean effective visibility. See 
1325  * gtk_action_is_visible() 
1326  * for that.
1327  *
1328  * Since: 2.6
1329  **/
1330 void
1331 gtk_action_set_visible (GtkAction *action,
1332                         gboolean   visible)
1333 {
1334   g_return_if_fail (GTK_IS_ACTION (action));
1335
1336   visible = visible != FALSE;
1337   
1338   if (action->private_data->visible != visible)
1339     {
1340       action->private_data->visible = visible;
1341
1342       g_object_notify (G_OBJECT (action), "visible");
1343     }
1344 }
1345
1346 /**
1347  * gtk_action_block_activate_from:
1348  * @action: the action object
1349  * @proxy: a proxy widget
1350  *
1351  * Disables calls to the gtk_action_activate()
1352  * function by signals on the given proxy widget.  This is used to
1353  * break notification loops for things like check or radio actions.
1354  *
1355  * This function is intended for use by action implementations.
1356  * 
1357  * Since: 2.4
1358  */
1359 void
1360 gtk_action_block_activate_from (GtkAction *action, 
1361                                 GtkWidget *proxy)
1362 {
1363   g_return_if_fail (GTK_IS_ACTION (action));
1364   
1365   g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
1366                                    action);
1367 }
1368
1369 /**
1370  * gtk_action_unblock_activate_from:
1371  * @action: the action object
1372  * @proxy: a proxy widget
1373  *
1374  * Re-enables calls to the gtk_action_activate()
1375  * function by signals on the given proxy widget.  This undoes the
1376  * blocking done by gtk_action_block_activate_from().
1377  *
1378  * This function is intended for use by action implementations.
1379  * 
1380  * Since: 2.4
1381  */
1382 void
1383 gtk_action_unblock_activate_from (GtkAction *action, 
1384                                   GtkWidget *proxy)
1385 {
1386   g_return_if_fail (GTK_IS_ACTION (action));
1387
1388   g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
1389                                      action);
1390 }
1391
1392 static void
1393 closure_accel_activate (GClosure     *closure,
1394                         GValue       *return_value,
1395                         guint         n_param_values,
1396                         const GValue *param_values,
1397                         gpointer      invocation_hint,
1398                         gpointer      marshal_data)
1399 {
1400   if (gtk_action_is_sensitive (GTK_ACTION (closure->data)))
1401     _gtk_action_emit_activate (GTK_ACTION (closure->data));
1402
1403   /* we handled the accelerator */
1404   g_value_set_boolean (return_value, TRUE);
1405 }
1406
1407 static void
1408 gtk_action_set_action_group (GtkAction      *action,
1409                              GtkActionGroup *action_group)
1410 {
1411   g_return_if_fail (GTK_IS_ACTION (action));
1412
1413   if (action->private_data->action_group == NULL)
1414     g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1415   else
1416     g_return_if_fail (action_group == NULL);
1417
1418   action->private_data->action_group = action_group;
1419 }
1420
1421 /**
1422  * gtk_action_set_accel_path:
1423  * @action: the action object
1424  * @accel_path: the accelerator path
1425  *
1426  * Sets the accel path for this action.  All proxy widgets associated
1427  * with the action will have this accel path, so that their
1428  * accelerators are consistent.
1429  *
1430  * Since: 2.4
1431  */
1432 void
1433 gtk_action_set_accel_path (GtkAction   *action, 
1434                            const gchar *accel_path)
1435 {
1436   g_return_if_fail (GTK_IS_ACTION (action));
1437
1438   action->private_data->accel_quark = g_quark_from_string (accel_path);
1439 }
1440
1441 /**
1442  * gtk_action_get_accel_path:
1443  * @action: the action object
1444  *
1445  * Returns the accel path for this action.  
1446  *
1447  * Since: 2.6
1448  *
1449  * Returns: the accel path for this action, or %NULL
1450  *   if none is set. The returned string is owned by GTK+
1451  *   and must not be freed or modified.
1452  */
1453 G_CONST_RETURN gchar *
1454 gtk_action_get_accel_path (GtkAction *action)
1455 {
1456   g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
1457
1458   if (action->private_data->accel_quark)
1459     return g_quark_to_string (action->private_data->accel_quark);
1460   else
1461     return NULL;
1462 }
1463
1464
1465 /**
1466  * gtk_action_set_accel_group:
1467  * @action: the action object
1468  * @accel_group: a #GtkAccelGroup or %NULL
1469  * 
1470  * Sets the #GtkAccelGroup in which the accelerator for this action
1471  * will be installed.
1472  *
1473  * Since: 2.4
1474  **/
1475 void
1476 gtk_action_set_accel_group (GtkAction     *action,
1477                             GtkAccelGroup *accel_group)
1478 {
1479   g_return_if_fail (GTK_IS_ACTION (action));
1480   g_return_if_fail (accel_group == NULL || GTK_IS_ACCEL_GROUP (accel_group));
1481   
1482   if (accel_group)
1483     g_object_ref (accel_group);
1484   if (action->private_data->accel_group)
1485     g_object_unref (action->private_data->accel_group);
1486
1487   action->private_data->accel_group = accel_group;
1488 }
1489
1490 /**
1491  * gtk_action_connect_accelerator:
1492  * @action: a #GtkAction
1493  * 
1494  * Installs the accelerator for @action if @action has an
1495  * accel path and group. See gtk_action_set_accel_path() and 
1496  * gtk_action_set_accel_group()
1497  *
1498  * Since multiple proxies may independently trigger the installation
1499  * of the accelerator, the @action counts the number of times this
1500  * function has been called and doesn't remove the accelerator until
1501  * gtk_action_disconnect_accelerator() has been called as many times.
1502  *
1503  * Since: 2.4
1504  **/
1505 void 
1506 gtk_action_connect_accelerator (GtkAction *action)
1507 {
1508   g_return_if_fail (GTK_IS_ACTION (action));
1509
1510   if (!action->private_data->accel_quark ||
1511       !action->private_data->accel_group)
1512     return;
1513
1514   if (action->private_data->accel_count == 0)
1515     {
1516       const gchar *accel_path = 
1517         g_quark_to_string (action->private_data->accel_quark);
1518       
1519       gtk_accel_group_connect_by_path (action->private_data->accel_group,
1520                                        accel_path,
1521                                        action->private_data->accel_closure);
1522     }
1523
1524   action->private_data->accel_count++;
1525 }
1526
1527 /**
1528  * gtk_action_disconnect_accelerator:
1529  * @action: a #GtkAction
1530  * 
1531  * Undoes the effect of one call to gtk_action_connect_accelerator().
1532  *
1533  * Since: 2.4
1534  **/
1535 void 
1536 gtk_action_disconnect_accelerator (GtkAction *action)
1537 {
1538   g_return_if_fail (GTK_IS_ACTION (action));
1539
1540   if (!action->private_data->accel_quark ||
1541       !action->private_data->accel_group)
1542     return;
1543
1544   action->private_data->accel_count--;
1545
1546   if (action->private_data->accel_count == 0)
1547     gtk_accel_group_disconnect (action->private_data->accel_group,
1548                                 action->private_data->accel_closure);
1549 }