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