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