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