]> Pileus Git - ~andy/gtk/blob - gtk/gtkactiongroup.c
Document possible accelerator gotcha when using this function. (#139641,
[~andy/gtk] / gtk / gtkactiongroup.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 "gtkactiongroup.h"
34 #include "gtkstock.h"
35 #include "gtktoggleaction.h"
36 #include "gtkradioaction.h"
37 #include "gtkaccelmap.h"
38 #include "gtkmarshalers.h"
39 #include "gtkintl.h"
40
41 #define GTK_ACTION_GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION_GROUP, GtkActionGroupPrivate))
42
43 struct _GtkActionGroupPrivate 
44 {
45   gchar           *name;
46   gboolean         sensitive;
47   gboolean         visible;
48   GHashTable      *actions;
49
50   GtkTranslateFunc translate_func;
51   gpointer         translate_data;
52   GtkDestroyNotify translate_notify;   
53 };
54
55 enum 
56 {
57   CONNECT_PROXY,
58   DISCONNECT_PROXY,
59   PRE_ACTIVATE,
60   POST_ACTIVATE,
61   LAST_SIGNAL
62 };
63
64 enum 
65 {
66   PROP_0,
67   PROP_NAME,
68   PROP_SENSITIVE,
69   PROP_VISIBLE
70 };
71
72 static void       gtk_action_group_init            (GtkActionGroup      *self);
73 static void       gtk_action_group_class_init      (GtkActionGroupClass *class);
74 static void       gtk_action_group_finalize        (GObject             *object);
75 static void       gtk_action_group_set_property    (GObject             *object,
76                                                     guint                prop_id,
77                                                     const GValue        *value,
78                                                     GParamSpec          *pspec);
79 static void       gtk_action_group_get_property    (GObject             *object,
80                                                     guint                prop_id,
81                                                     GValue              *value,
82                                                     GParamSpec          *pspec);
83 static GtkAction *gtk_action_group_real_get_action (GtkActionGroup      *self,
84                                                     const gchar         *name);
85
86
87 GType
88 gtk_action_group_get_type (void)
89 {
90   static GType type = 0;
91
92   if (!type)
93     {
94       static const GTypeInfo type_info =
95       {
96         sizeof (GtkActionGroupClass),
97         NULL,           /* base_init */
98         NULL,           /* base_finalize */
99         (GClassInitFunc) gtk_action_group_class_init,
100         NULL,           /* class_finalize */
101         NULL,           /* class_data */
102         sizeof (GtkActionGroup),
103         0, /* n_preallocs */
104         (GInstanceInitFunc) gtk_action_group_init,
105       };
106
107       type = g_type_register_static (G_TYPE_OBJECT, "GtkActionGroup",
108                                      &type_info, 0);
109     }
110
111   return type;
112 }
113
114 static GObjectClass *parent_class = NULL;
115 static guint         action_group_signals[LAST_SIGNAL] = { 0 };
116
117 static void
118 gtk_action_group_class_init (GtkActionGroupClass *klass)
119 {
120   GObjectClass *gobject_class;
121
122   gobject_class = G_OBJECT_CLASS (klass);
123   parent_class = g_type_class_peek_parent (klass);
124
125   gobject_class->finalize = gtk_action_group_finalize;
126   gobject_class->set_property = gtk_action_group_set_property;
127   gobject_class->get_property = gtk_action_group_get_property;
128   klass->get_action = gtk_action_group_real_get_action;
129
130   g_object_class_install_property (gobject_class,
131                                    PROP_NAME,
132                                    g_param_spec_string ("name",
133                                                         P_("Name"),
134                                                         P_("A name for the action group."),
135                                                         NULL,
136                                                         G_PARAM_READWRITE |
137                                                         G_PARAM_CONSTRUCT_ONLY));
138   g_object_class_install_property (gobject_class,
139                                    PROP_SENSITIVE,
140                                    g_param_spec_boolean ("sensitive",
141                                                          P_("Sensitive"),
142                                                          P_("Whether the action group is enabled."),
143                                                          TRUE,
144                                                          G_PARAM_READWRITE));
145   g_object_class_install_property (gobject_class,
146                                    PROP_VISIBLE,
147                                    g_param_spec_boolean ("visible",
148                                                          P_("Visible"),
149                                                          P_("Whether the action group is visible."),
150                                                          TRUE,
151                                                          G_PARAM_READWRITE));
152
153   /**
154    * GtkGroupAction::connect-proxy:
155    * @action_group: the group
156    * @action: the action
157    * @proxy: the proxy
158    *
159    * The connect_proxy signal is emitted after connecting a proxy to 
160    * an action in the group. Note that the proxy may have been connected 
161    * to a different action before.
162    *
163    * This is intended for simple customizations for which a custom action
164    * class would be too clumsy, e.g. showing tooltips for menuitems in the
165    * statusbar.
166    *
167    * #GtkUIManager proxies the signal and provides global notification 
168    * just before any action is connected to a proxy, which is probably more
169    * convenient to use.
170    *
171    * Since: 2.4
172    */
173   action_group_signals[CONNECT_PROXY] =
174     g_signal_new ("connect_proxy",
175                   G_OBJECT_CLASS_TYPE (klass),
176                   0, 0, NULL, NULL,
177                   _gtk_marshal_VOID__OBJECT_OBJECT,
178                   G_TYPE_NONE, 2,
179                   GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
180
181   /**
182    * GtkAction::disconnect-proxy:
183    * @action_group: the group
184    * @action: the action
185    * @proxy: the proxy
186    *
187    * The disconnect_proxy signal is emitted after disconnecting a proxy 
188    * from an action in the group. 
189    *
190    * #GtkUIManager proxies the signal and provides global notification 
191    * just before any action is connected to a proxy, which is probably more
192    * convenient to use.
193    *
194    * Since: 2.4
195    */
196   action_group_signals[DISCONNECT_PROXY] =
197     g_signal_new ("disconnect_proxy",
198                   G_OBJECT_CLASS_TYPE (klass),
199                   0, 0, NULL, NULL,
200                   _gtk_marshal_VOID__OBJECT_OBJECT,
201                   G_TYPE_NONE, 2, 
202                   GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
203
204   /**
205    * GtkActionGroup::pre_activate:
206    * @action_group: the group
207    * @action: the action
208    *
209    * The pre_activate signal is emitted just before the @action in the
210    * @action_group is activated
211    *
212    * This is intended for #GtkUIManager to proxy the signal and provide global
213    * notification just before any action is activated.
214    *
215    * Since: 2.4
216    */
217   action_group_signals[PRE_ACTIVATE] =
218     g_signal_new ("pre_activate",
219                   G_OBJECT_CLASS_TYPE (klass),
220                   0, 0, NULL, NULL,
221                   _gtk_marshal_VOID__OBJECT,
222                   G_TYPE_NONE, 1, 
223                   GTK_TYPE_ACTION);
224
225   /**
226    * GtkActionGroup::post_activate:
227    * @action_group: the group
228    * @action: the action
229    *
230    * The post_activate signal is emitted just after the @action in the
231    * @action_group is activated
232    *
233    * This is intended for #GtkUIManager to proxy the signal and provide global
234    * notification just after any action is activated.
235    *
236    * Since: 2.4
237    */
238   action_group_signals[POST_ACTIVATE] =
239     g_signal_new ("post_activate",
240                   G_OBJECT_CLASS_TYPE (klass),
241                   0, 0, NULL, NULL,
242                   _gtk_marshal_VOID__OBJECT,
243                   G_TYPE_NONE, 1, 
244                   GTK_TYPE_ACTION);
245
246   g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
247 }
248
249
250 static void 
251 remove_action (GtkAction *action) 
252 {
253   g_object_set (action, "action_group", NULL, NULL);
254   g_object_unref (action);
255 }
256
257 static void
258 gtk_action_group_init (GtkActionGroup *self)
259 {
260   self->private_data = GTK_ACTION_GROUP_GET_PRIVATE (self);
261   self->private_data->name = NULL;
262   self->private_data->sensitive = TRUE;
263   self->private_data->visible = TRUE;
264   self->private_data->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
265                                                        (GDestroyNotify) g_free,
266                                                        (GDestroyNotify) remove_action);
267   self->private_data->translate_func = NULL;
268   self->private_data->translate_data = NULL;
269   self->private_data->translate_notify = NULL;
270 }
271
272 /**
273  * gtk_action_group_new:
274  * @name: the name of the action group.
275  *
276  * Creates a new #GtkActionGroup object. The name of the action group
277  * is used when associating <link linkend="Action-Accel">keybindings</link> 
278  * with the actions.
279  *
280  * Returns: the new #GtkActionGroup
281  *
282  * Since: 2.4
283  */
284 GtkActionGroup *
285 gtk_action_group_new (const gchar *name)
286 {
287   GtkActionGroup *self;
288
289   self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
290   self->private_data->name = g_strdup (name);
291
292   return self;
293 }
294
295 static void
296 gtk_action_group_finalize (GObject *object)
297 {
298   GtkActionGroup *self;
299
300   self = GTK_ACTION_GROUP (object);
301
302   g_free (self->private_data->name);
303   self->private_data->name = NULL;
304
305   g_hash_table_destroy (self->private_data->actions);
306   self->private_data->actions = NULL;
307
308   if (self->private_data->translate_notify)
309     self->private_data->translate_notify (self->private_data->translate_data);
310
311   if (parent_class->finalize)
312     (* parent_class->finalize) (object);
313 }
314
315 static void
316 gtk_action_group_set_property (GObject         *object,
317                                guint            prop_id,
318                                const GValue    *value,
319                                GParamSpec      *pspec)
320 {
321   GtkActionGroup *self;
322   gchar *tmp;
323   
324   self = GTK_ACTION_GROUP (object);
325
326   switch (prop_id)
327     {
328     case PROP_NAME:
329       tmp = self->private_data->name;
330       self->private_data->name = g_value_dup_string (value);
331       g_free (tmp);
332       break;
333     case PROP_SENSITIVE:
334       gtk_action_group_set_sensitive (self, g_value_get_boolean (value));
335       break;
336     case PROP_VISIBLE:
337       gtk_action_group_set_visible (self, g_value_get_boolean (value));
338       break;
339     default:
340       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341       break;
342     }
343 }
344
345 static void
346 gtk_action_group_get_property (GObject    *object,
347                                guint       prop_id,
348                                GValue     *value,
349                                GParamSpec *pspec)
350 {
351   GtkActionGroup *self;
352   
353   self = GTK_ACTION_GROUP (object);
354
355   switch (prop_id)
356     {
357     case PROP_NAME:
358       g_value_set_string (value, self->private_data->name);
359       break;
360     case PROP_SENSITIVE:
361       g_value_set_boolean (value, self->private_data->sensitive);
362       break;
363     case PROP_VISIBLE:
364       g_value_set_boolean (value, self->private_data->visible);
365       break;
366     default:
367       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368       break;
369     }
370 }
371
372 static GtkAction *
373 gtk_action_group_real_get_action (GtkActionGroup *self,
374                                   const gchar    *action_name)
375 {
376   return g_hash_table_lookup (self->private_data->actions, action_name);
377 }
378
379 /**
380  * gtk_action_group_get_name:
381  * @action_group: the action group
382  *
383  * Gets the name of the action group.
384  *
385  * Returns: the name of the action group.
386  * 
387  * Since: 2.4
388  */
389 const gchar *
390 gtk_action_group_get_name (GtkActionGroup *action_group)
391 {
392   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
393
394   return action_group->private_data->name;
395 }
396
397 /**
398  * gtk_action_group_get_sensitive:
399  * @action_group: the action group
400  *
401  * Returns %TRUE if the group is sensitive.  The constituent actions
402  * can only be logically sensitive (see gtk_action_is_sensitive()) if
403  * they are sensitive (see gtk_action_get_sensitive()) and their group
404  * is sensitive.
405  * 
406  * Return value: %TRUE if the group is sensitive.
407  *
408  * Since: 2.4
409  */
410 gboolean
411 gtk_action_group_get_sensitive (GtkActionGroup *action_group)
412 {
413   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
414
415   return action_group->private_data->sensitive;
416 }
417
418 static void
419 cb_set_action_sensitivity (const gchar *name, GtkAction *action)
420 {
421   /* Minor optimization, the action_groups state only effects actions that are
422    * themselves sensitive */
423   if (gtk_action_get_sensitive (action))
424     g_object_notify (G_OBJECT (action), "sensitive");
425 }
426
427 /**
428  * gtk_action_group_set_sensitive:
429  * @action_group: the action group
430  * @sensitive: new sensitivity
431  *
432  * Changes the sensitivity of @action_group
433  * 
434  * Since: 2.4
435  */
436 void
437 gtk_action_group_set_sensitive (GtkActionGroup *action_group, gboolean sensitive)
438 {
439   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
440
441   if (action_group->private_data->sensitive ^ sensitive)
442     {
443       action_group->private_data->sensitive = sensitive;
444       g_hash_table_foreach (action_group->private_data->actions, 
445                             (GHFunc) cb_set_action_sensitivity, NULL);
446     }
447 }
448
449 /**
450  * gtk_action_group_get_visible:
451  * @action_group: the action group
452  *
453  * Returns %TRUE if the group is visible.  The constituent actions
454  * can only be logically visible (see gtk_action_is_visible()) if
455  * they are visible (see gtk_action_get_visible()) and their group
456  * is visible.
457  * 
458  * Return value: %TRUE if the group is sensitive.
459  * 
460  * Since: 2.4
461  */
462 gboolean
463 gtk_action_group_get_visible (GtkActionGroup *action_group)
464 {
465   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
466
467   return action_group->private_data->visible;
468 }
469
470 static void
471 cb_set_action_visiblity (const gchar *name, GtkAction *action)
472 {
473   /* Minor optimization, the action_groups state only effects actions that are
474    * themselves sensitive */
475   if (gtk_action_get_visible (action))
476     g_object_notify (G_OBJECT (action), "visible");
477 }
478
479 /**
480  * gtk_action_group_set_visible:
481  * @action_group: the action group
482  * @visible: new visiblity
483  *
484  * Changes the visible of @action_group.
485  * 
486  * Since: 2.4
487  */
488 void
489 gtk_action_group_set_visible (GtkActionGroup *action_group, gboolean visible)
490 {
491   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
492
493   if (action_group->private_data->visible ^ visible)
494     {
495       action_group->private_data->visible = visible;
496       g_hash_table_foreach (action_group->private_data->actions, 
497                             (GHFunc) cb_set_action_visiblity, NULL);
498     }
499 }
500
501 /**
502  * gtk_action_group_get_action:
503  * @action_group: the action group
504  * @action_name: the name of the action
505  *
506  * Looks up an action in the action group by name.
507  *
508  * Returns: the action, or %NULL if no action by that name exists
509  *
510  * Since: 2.4
511  */
512 GtkAction *
513 gtk_action_group_get_action (GtkActionGroup *action_group,
514                              const gchar    *action_name)
515 {
516   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
517   g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
518
519   return (* GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action)
520     (action_group, action_name);
521 }
522
523 /**
524  * gtk_action_group_add_action:
525  * @action_group: the action group
526  * @action: an action
527  *
528  * Adds an action object to the action group. Note that this function
529  * does not set up the accel path of the action, which can lead to problems
530  * if a user tries to modify the accelerator of a menuitem associated with
531  * the action. Therefore you must either set the accel path yourself with
532  * gtk_action_set_accel_path(), or use 
533  * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
534  *
535  * Since: 2.4
536  */
537 void
538 gtk_action_group_add_action (GtkActionGroup *action_group,
539                              GtkAction      *action)
540 {
541   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
542   g_return_if_fail (GTK_IS_ACTION (action));
543   g_return_if_fail (gtk_action_get_name (action) != NULL);
544
545   g_hash_table_insert (action_group->private_data->actions, 
546                        g_strdup (gtk_action_get_name (action)),
547                        g_object_ref (action));
548   g_object_set (G_OBJECT (action), "action_group", action_group, NULL);
549 }
550
551 /**
552  * gtk_action_group_add_action_with_accel:
553  * @action_group: the action group 
554  * @action: the action to add 
555  * @accelerator: the accelerator for the action, in
556  *   the format understood by gtk_accelerator_parse(), or %NULL to use the
557  *   stock accelerator 
558  *
559  * Adds an action object to the action group and sets up the accelerator.
560  *
561  * If @accelerator is %NULL, attempts to use the accelerator associated 
562  * with the stock_id of the action.
563  *
564  * Accel paths are set to
565  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
566  *
567  * Since: 2.4
568  */
569 void
570 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
571                                         GtkAction *action,
572                                         const gchar *accelerator)
573 {
574   gchar *accel_path;
575   guint  accel_key = 0;
576   GdkModifierType accel_mods;
577   GtkStockItem stock_item;
578   gchar *name;
579   gchar *stock_id;
580   
581   g_object_get (action, "name", &name, "stock_id", &stock_id, NULL);
582
583   accel_path = g_strconcat ("<Actions>/",
584                             action_group->private_data->name, "/", name, NULL);
585
586   if (accelerator)
587     gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
588   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
589     {
590       accel_key = stock_item.keyval;
591       accel_mods = stock_item.modifier;
592     }
593
594   if (accel_key)
595     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
596
597   gtk_action_set_accel_path (action, accel_path);
598   gtk_action_group_add_action (action_group, action);
599
600   g_free (accel_path);
601   g_free (stock_id);
602   g_free (name);
603 }
604
605 /**
606  * gtk_action_group_remove_action:
607  * @action_group: the action group
608  * @action: an action
609  *
610  * Removes an action object from the action group.
611  *
612  * Since: 2.4
613  */
614 void
615 gtk_action_group_remove_action (GtkActionGroup *action_group,
616                                 GtkAction      *action)
617 {
618   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
619   g_return_if_fail (GTK_IS_ACTION (action));
620   g_return_if_fail (gtk_action_get_name (action) != NULL);
621
622   /* extra protection to make sure action->name is valid */
623   g_object_ref (action);
624   g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
625   g_object_unref (action);
626 }
627
628 static void
629 add_single_action (gpointer key, 
630                    gpointer value, 
631                    gpointer user_data)
632 {
633   GList **list = user_data;
634
635   *list = g_list_prepend (*list, value);
636 }
637
638 /**
639  * gtk_action_group_list_actions:
640  * @action_group: the action group
641  *
642  * Lists the actions in the action group.
643  *
644  * Returns: an allocated list of the action objects in the action group
645  * 
646  * Since: 2.4
647  */
648 GList *
649 gtk_action_group_list_actions (GtkActionGroup *action_group)
650 {
651   GList *actions = NULL;
652   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
653   
654   g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
655
656   return g_list_reverse (actions);
657 }
658
659
660 /**
661  * gtk_action_group_add_actions:
662  * @action_group: the action group
663  * @entries: an array of action descriptions
664  * @n_entries: the number of entries
665  * @user_data: data to pass to the action callbacks
666  *
667  * This is a convenience function to create a number of actions and add them 
668  * to the action group.
669  *
670  * The "activate" signals of the actions are connected to the callbacks and 
671  * their accel paths are set to 
672  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
673  * 
674  * Since: 2.4
675  */
676 void
677 gtk_action_group_add_actions (GtkActionGroup *action_group,
678                               GtkActionEntry *entries,
679                               guint           n_entries,
680                               gpointer        user_data)
681 {
682   gtk_action_group_add_actions_full (action_group, 
683                                      entries, n_entries, 
684                                      user_data, NULL);
685 }
686
687 typedef struct _SharedData  SharedData;
688
689 struct _SharedData {
690   guint          ref_count;
691   gpointer       data;
692   GDestroyNotify destroy;
693 };
694
695 static void
696 shared_data_unref (gpointer data)
697 {
698   SharedData *shared_data = (SharedData *)data;
699
700   shared_data->ref_count--;
701   if (shared_data->ref_count == 0)
702     {
703       if (shared_data->destroy) 
704         (*shared_data->destroy) (shared_data->data);
705       
706       g_free (shared_data);
707     }
708 }
709
710
711 /**
712  * gtk_action_group_add_actions_full:
713  * @action_group: the action group
714  * @entries: an array of action descriptions
715  * @n_entries: the number of entries
716  * @user_data: data to pass to the action callbacks
717  * @destroy: destroy notification callback for @user_data
718  *
719  * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
720  * callback for @user_data. 
721  * 
722  * Since: 2.4
723  */
724 void
725 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
726                                    GtkActionEntry *entries,
727                                    guint           n_entries,
728                                    gpointer        user_data,
729                                    GDestroyNotify  destroy)
730 {
731
732   /* Keep this in sync with the other 
733    * gtk_action_group_add_..._actions_full() functions.
734    */
735   guint i;
736   GtkTranslateFunc translate_func;
737   gpointer translate_data;
738   SharedData *shared_data;
739
740   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
741
742   translate_func = action_group->private_data->translate_func;
743   translate_data = action_group->private_data->translate_data;
744
745   shared_data = g_new0 (SharedData, 1);
746   shared_data->ref_count = 1;
747   shared_data->data = user_data;
748   shared_data->destroy = destroy;
749
750   for (i = 0; i < n_entries; i++)
751     {
752       GtkAction *action;
753       const gchar *label;
754       const gchar *tooltip;
755
756       if (translate_func)
757         {
758           label = translate_func (entries[i].label, translate_data);
759           tooltip = translate_func (entries[i].tooltip, translate_data);
760         }
761       else
762         {
763           label = entries[i].label;
764           tooltip = entries[i].tooltip;
765         }
766
767       action = gtk_action_new (entries[i].name,
768                                label,
769                                tooltip,
770                                entries[i].stock_id);
771
772       if (entries[i].callback)
773         {
774           GClosure *closure;
775
776           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
777           g_closure_add_finalize_notifier (closure, shared_data, 
778                                            (GClosureNotify)shared_data_unref);
779           shared_data->ref_count++;
780
781           g_signal_connect_closure (action, "activate", closure, FALSE);
782         }
783           
784       gtk_action_group_add_action_with_accel (action_group, 
785                                               action,
786                                               entries[i].accelerator);
787       g_object_unref (action);
788     }
789
790   shared_data_unref (shared_data);
791 }
792
793 /**
794  * gtk_action_group_add_toggle_actions:
795  * @action_group: the action group
796  * @entries: an array of toggle action descriptions
797  * @n_entries: the number of entries
798  * @user_data: data to pass to the action callbacks
799  *
800  * This is a convenience function to create a number of toggle actions and add them 
801  * to the action group.
802  *
803  * The "activate" signals of the actions are connected to the callbacks and 
804  * their accel paths are set to 
805  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
806  * 
807  * Since: 2.4
808  */
809 void
810 gtk_action_group_add_toggle_actions (GtkActionGroup       *action_group,
811                                      GtkToggleActionEntry *entries,
812                                      guint                 n_entries,
813                                      gpointer              user_data)
814 {
815   gtk_action_group_add_toggle_actions_full (action_group, 
816                                             entries, n_entries, 
817                                             user_data, NULL);
818 }
819
820
821 /**
822  * gtk_action_group_add_toggle_actions_full:
823  * @action_group: the action group
824  * @entries: an array of toggle action descriptions
825  * @n_entries: the number of entries
826  * @user_data: data to pass to the action callbacks
827  * @destroy: destroy notification callback for @user_data
828  *
829  * This variant of gtk_action_group_add_toggle_actions() adds a 
830  * #GDestroyNotify callback for @user_data. 
831  * 
832  * Since: 2.4
833  */
834 void
835 gtk_action_group_add_toggle_actions_full (GtkActionGroup       *action_group,
836                                           GtkToggleActionEntry *entries,
837                                           guint                 n_entries,
838                                           gpointer              user_data,
839                                           GDestroyNotify        destroy)
840 {
841   /* Keep this in sync with the other 
842    * gtk_action_group_add_..._actions_full() functions.
843    */
844   guint i;
845   GtkTranslateFunc translate_func;
846   gpointer translate_data;
847   SharedData *shared_data;
848
849   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
850
851   translate_func = action_group->private_data->translate_func;
852   translate_data = action_group->private_data->translate_data;
853
854   shared_data = g_new0 (SharedData, 1);
855   shared_data->ref_count = 1;
856   shared_data->data = user_data;
857   shared_data->destroy = destroy;
858
859   for (i = 0; i < n_entries; i++)
860     {
861       GtkToggleAction *action;
862       const gchar *label;
863       const gchar *tooltip;
864
865       if (translate_func)
866         {
867           label = translate_func (entries[i].label, translate_data);
868           tooltip = translate_func (entries[i].tooltip, translate_data);
869         }
870       else
871         {
872           label = entries[i].label;
873           tooltip = entries[i].tooltip;
874         }
875
876       action = gtk_toggle_action_new (entries[i].name,
877                                       label,
878                                       tooltip,
879                                       entries[i].stock_id);
880
881       gtk_toggle_action_set_active (action, entries[i].is_active);
882
883       if (entries[i].callback)
884         {
885           GClosure *closure;
886
887           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
888           g_closure_add_finalize_notifier (closure, shared_data, 
889                                            (GClosureNotify)shared_data_unref);
890           shared_data->ref_count++;
891
892           g_signal_connect_closure (action, "activate", closure, FALSE);
893         }
894           
895       gtk_action_group_add_action_with_accel (action_group, 
896                                               GTK_ACTION (action),
897                                               entries[i].accelerator);
898       g_object_unref (action);
899     }
900
901     shared_data_unref (shared_data);
902 }
903
904 /**
905  * gtk_action_group_add_radio_actions:
906  * @action_group: the action group
907  * @entries: an array of radio action descriptions
908  * @n_entries: the number of entries
909  * @value: the value of the action to activate initially, or -1 if
910  *   no action should be activated
911  * @on_change: the callback to connect to the changed signal
912  * @user_data: data to pass to the action callbacks
913  * 
914  * This is a convenience routine to create a group of radio actions and
915  * add them to the action group. 
916  *
917  * The "changed" signal of the first radio action is connected to the 
918  * @on_change callback and the accel paths of the actions are set to 
919  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
920  * 
921  * Since: 2.4
922  **/
923 void            
924 gtk_action_group_add_radio_actions (GtkActionGroup      *action_group,
925                                     GtkRadioActionEntry *entries,
926                                     guint                n_entries,
927                                     gint                 value,
928                                     GCallback            on_change,
929                                     gpointer             user_data)
930 {
931   gtk_action_group_add_radio_actions_full (action_group, 
932                                            entries, n_entries, 
933                                            value,
934                                            on_change, user_data, NULL);
935 }
936
937 /**
938  * gtk_action_group_add_radio_actions_full:
939  * @action_group: the action group
940  * @entries: an array of radio action descriptions
941  * @n_entries: the number of entries
942  * @value: the value of the action to activate initially, or -1 if
943  *   no action should be activated
944  * @on_change: the callback to connect to the changed signal
945  * @user_data: data to pass to the action callbacks
946  * @destroy: destroy notification callback for @user_data
947  *
948  * This variant of gtk_action_group_add_radio_actions() adds a 
949  * #GDestroyNotify callback for @user_data. 
950  * 
951  * Since: 2.4
952  **/
953 void            
954 gtk_action_group_add_radio_actions_full (GtkActionGroup      *action_group,
955                                          GtkRadioActionEntry *entries,
956                                          guint                n_entries,
957                                          gint                 value,
958                                          GCallback            on_change,
959                                          gpointer             user_data,
960                                          GDestroyNotify       destroy)
961 {
962   /* Keep this in sync with the other 
963    * gtk_action_group_add_..._actions_full() functions.
964    */
965   guint i;
966   GtkTranslateFunc translate_func;
967   gpointer translate_data;
968   GSList *group = NULL;
969   GtkRadioAction *first_action = NULL;
970
971   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
972
973   translate_func = action_group->private_data->translate_func;
974   translate_data = action_group->private_data->translate_data;
975
976   for (i = 0; i < n_entries; i++)
977     {
978       GtkRadioAction *action;
979       const gchar *label;
980       const gchar *tooltip; 
981
982       if (translate_func)
983         {
984           label = translate_func (entries[i].label, translate_data);
985           tooltip = translate_func (entries[i].tooltip, translate_data);
986         }
987       else
988         {
989           label = entries[i].label;
990           tooltip = entries[i].tooltip;
991         }
992
993       action = gtk_radio_action_new (entries[i].name,
994                                      label,
995                                      tooltip,
996                                      entries[i].stock_id,
997                                      entries[i].value);
998
999       if (i == 0) 
1000         first_action = action;
1001
1002       gtk_radio_action_set_group (action, group);
1003       group = gtk_radio_action_get_group (action);
1004
1005       if (value == entries[i].value)
1006         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1007
1008       gtk_action_group_add_action_with_accel (action_group, 
1009                                               GTK_ACTION (action),
1010                                               entries[i].accelerator);
1011       g_object_unref (action);
1012     }
1013
1014   if (on_change && first_action)
1015     g_signal_connect_data (first_action, "changed",
1016                            on_change, user_data, 
1017                            (GClosureNotify)destroy, 0);
1018 }
1019
1020 /**
1021  * gtk_action_group_set_translate_func:
1022  * @action_group: a #GtkActionGroup
1023  * @func: a #GtkTranslateFunc
1024  * @data: data to be passed to @func and @notify
1025  * @notify: a #GtkDestroyNotify function to be called when @action_group is 
1026  *   destroyed and when the translation function is changed again
1027  * 
1028  * Sets a function to be used for translating the @label and @tooltip of 
1029  * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1030  *
1031  * If you're using gettext(), it is enough to set the translation domain
1032  * with gtk_action_group_set_translation_domain().
1033  *
1034  * Since: 2.4 
1035  **/
1036 void
1037 gtk_action_group_set_translate_func (GtkActionGroup      *action_group,
1038                                      GtkTranslateFunc     func,
1039                                      gpointer             data,
1040                                      GtkDestroyNotify     notify)
1041 {
1042   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1043   
1044   if (action_group->private_data->translate_notify)
1045     action_group->private_data->translate_notify (action_group->private_data->translate_data);
1046       
1047   action_group->private_data->translate_func = func;
1048   action_group->private_data->translate_data = data;
1049   action_group->private_data->translate_notify = notify;
1050 }
1051
1052 static gchar *
1053 dgettext_swapped (const gchar *msgid, 
1054                   const gchar *domainname)
1055 {
1056   return dgettext (domainname, msgid);
1057 }
1058
1059 /**
1060  * gtk_action_group_set_translation_domain:
1061  * @action_group: a #GtkActionGroup
1062  * @domain: the translation domain to use for dgettext() calls
1063  * 
1064  * Sets the translation domain and uses dgettext() for translating the 
1065  * @label and @tooltip of #GtkActionEntry<!-- -->s added by 
1066  * gtk_action_group_add_actions().
1067  *
1068  * If you're not using gettext() for localization, see 
1069  * gtk_action_group_set_translate_func().
1070  *
1071  * Since: 2.4
1072  **/
1073 void 
1074 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1075                                          const gchar    *domain)
1076 {
1077   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1078
1079   gtk_action_group_set_translate_func (action_group, 
1080                                        (GtkTranslateFunc)dgettext_swapped,
1081                                        g_strdup (domain),
1082                                        g_free);
1083
1084
1085 /* Protected for use by GtkAction */
1086 void
1087 _gtk_action_group_emit_connect_proxy  (GtkActionGroup *action_group,
1088                                        GtkAction      *action,
1089                                        GtkWidget      *proxy)
1090 {
1091   g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0, 
1092                  action, proxy);
1093 }
1094
1095 void
1096 _gtk_action_group_emit_disconnect_proxy  (GtkActionGroup *action_group,
1097                                           GtkAction      *action,
1098                                           GtkWidget      *proxy)
1099 {
1100   g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0, 
1101                  action, proxy);
1102 }
1103
1104 void
1105 _gtk_action_group_emit_pre_activate  (GtkActionGroup *action_group,
1106                                       GtkAction      *action)
1107 {
1108   g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1109 }
1110
1111 void
1112 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1113                                       GtkAction *action)
1114 {
1115   g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);
1116 }