]> Pileus Git - ~andy/gtk/blob - gtk/gtkactiongroup.c
Fix actiongroup-action interaction that relied on actions connecting to
[~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 "gtkprivate.h"
40 #include "gtkintl.h"
41 #include "gtkalias.h"
42
43 #define GTK_ACTION_GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION_GROUP, GtkActionGroupPrivate))
44
45 struct _GtkActionGroupPrivate 
46 {
47   gchar           *name;
48   gboolean         sensitive;
49   gboolean         visible;
50   GHashTable      *actions;
51
52   GtkTranslateFunc translate_func;
53   gpointer         translate_data;
54   GtkDestroyNotify translate_notify;   
55 };
56
57 enum 
58 {
59   CONNECT_PROXY,
60   DISCONNECT_PROXY,
61   PRE_ACTIVATE,
62   POST_ACTIVATE,
63   LAST_SIGNAL
64 };
65
66 enum 
67 {
68   PROP_0,
69   PROP_NAME,
70   PROP_SENSITIVE,
71   PROP_VISIBLE
72 };
73
74 static void       gtk_action_group_init            (GtkActionGroup      *self);
75 static void       gtk_action_group_class_init      (GtkActionGroupClass *class);
76 static void       gtk_action_group_finalize        (GObject             *object);
77 static void       gtk_action_group_set_property    (GObject             *object,
78                                                     guint                prop_id,
79                                                     const GValue        *value,
80                                                     GParamSpec          *pspec);
81 static void       gtk_action_group_get_property    (GObject             *object,
82                                                     guint                prop_id,
83                                                     GValue              *value,
84                                                     GParamSpec          *pspec);
85 static GtkAction *gtk_action_group_real_get_action (GtkActionGroup      *self,
86                                                     const gchar         *name);
87
88
89 GType
90 gtk_action_group_get_type (void)
91 {
92   static GType type = 0;
93
94   if (!type)
95     {
96       static const GTypeInfo type_info =
97       {
98         sizeof (GtkActionGroupClass),
99         NULL,           /* base_init */
100         NULL,           /* base_finalize */
101         (GClassInitFunc) gtk_action_group_class_init,
102         NULL,           /* class_finalize */
103         NULL,           /* class_data */
104         sizeof (GtkActionGroup),
105         0, /* n_preallocs */
106         (GInstanceInitFunc) gtk_action_group_init,
107       };
108
109       type = g_type_register_static (G_TYPE_OBJECT, I_("GtkActionGroup"),
110                                      &type_info, 0);
111     }
112
113   return type;
114 }
115
116 static GObjectClass *parent_class = NULL;
117 static guint         action_group_signals[LAST_SIGNAL] = { 0 };
118
119 static void
120 gtk_action_group_class_init (GtkActionGroupClass *klass)
121 {
122   GObjectClass *gobject_class;
123
124   gobject_class = G_OBJECT_CLASS (klass);
125   parent_class = g_type_class_peek_parent (klass);
126
127   gobject_class->finalize = gtk_action_group_finalize;
128   gobject_class->set_property = gtk_action_group_set_property;
129   gobject_class->get_property = gtk_action_group_get_property;
130   klass->get_action = gtk_action_group_real_get_action;
131
132   g_object_class_install_property (gobject_class,
133                                    PROP_NAME,
134                                    g_param_spec_string ("name",
135                                                         P_("Name"),
136                                                         P_("A name for the action group."),
137                                                         NULL,
138                                                         GTK_PARAM_READWRITE | 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                                                          GTK_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                                                          GTK_PARAM_READWRITE));
153
154   /**
155    * GtkActionGroup::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 (I_("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    * GtkActionGroup::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 (I_("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 (I_("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 (I_("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, I_("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 G_CONST_RETURN 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, 
421                            GtkAction   *action)
422 {
423   /* Minor optimization, the action_groups state only affects actions 
424    * that are themselves sensitive */
425   if (gtk_action_get_sensitive (action))
426     _gtk_action_sync_sensitive (action);
427 }
428
429 /**
430  * gtk_action_group_set_sensitive:
431  * @action_group: the action group
432  * @sensitive: new sensitivity
433  *
434  * Changes the sensitivity of @action_group
435  * 
436  * Since: 2.4
437  */
438 void
439 gtk_action_group_set_sensitive (GtkActionGroup *action_group, 
440                                 gboolean        sensitive)
441 {
442   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
443
444   sensitive = sensitive != FALSE;
445
446   if (action_group->private_data->sensitive != sensitive)
447     {
448       action_group->private_data->sensitive = sensitive;
449       g_hash_table_foreach (action_group->private_data->actions, 
450                             (GHFunc) cb_set_action_sensitivity, NULL);
451
452       g_object_notify (G_OBJECT (action_group), "sensitive");
453     }
454 }
455
456 /**
457  * gtk_action_group_get_visible:
458  * @action_group: the action group
459  *
460  * Returns %TRUE if the group is visible.  The constituent actions
461  * can only be logically visible (see gtk_action_is_visible()) if
462  * they are visible (see gtk_action_get_visible()) and their group
463  * is visible.
464  * 
465  * Return value: %TRUE if the group is visible.
466  * 
467  * Since: 2.4
468  */
469 gboolean
470 gtk_action_group_get_visible (GtkActionGroup *action_group)
471 {
472   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
473
474   return action_group->private_data->visible;
475 }
476
477 static void
478 cb_set_action_visiblity (const gchar *name, 
479                          GtkAction   *action)
480 {
481   /* Minor optimization, the action_groups state only affects actions 
482    * that are themselves visible */
483   if (gtk_action_get_visible (action))
484     _gtk_action_sync_visible (action);
485 }
486
487 /**
488  * gtk_action_group_set_visible:
489  * @action_group: the action group
490  * @visible: new visiblity
491  *
492  * Changes the visible of @action_group.
493  * 
494  * Since: 2.4
495  */
496 void
497 gtk_action_group_set_visible (GtkActionGroup *action_group, 
498                               gboolean        visible)
499 {
500   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
501
502   visible = visible != FALSE;
503
504   if (action_group->private_data->visible != visible)
505     {
506       action_group->private_data->visible = visible;
507       g_hash_table_foreach (action_group->private_data->actions, 
508                             (GHFunc) cb_set_action_visiblity, NULL);
509
510       g_object_notify (G_OBJECT (action_group), "visible");
511     }
512 }
513
514 /**
515  * gtk_action_group_get_action:
516  * @action_group: the action group
517  * @action_name: the name of the action
518  *
519  * Looks up an action in the action group by name.
520  *
521  * Returns: the action, or %NULL if no action by that name exists
522  *
523  * Since: 2.4
524  */
525 GtkAction *
526 gtk_action_group_get_action (GtkActionGroup *action_group,
527                              const gchar    *action_name)
528 {
529   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
530   g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
531
532   return (* GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action)
533     (action_group, action_name);
534 }
535
536 /**
537  * gtk_action_group_add_action:
538  * @action_group: the action group
539  * @action: an action
540  *
541  * Adds an action object to the action group. Note that this function
542  * does not set up the accel path of the action, which can lead to problems
543  * if a user tries to modify the accelerator of a menuitem associated with
544  * the action. Therefore you must either set the accel path yourself with
545  * gtk_action_set_accel_path(), or use 
546  * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
547  *
548  * Since: 2.4
549  */
550 void
551 gtk_action_group_add_action (GtkActionGroup *action_group,
552                              GtkAction      *action)
553 {
554   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
555   g_return_if_fail (GTK_IS_ACTION (action));
556   g_return_if_fail (gtk_action_get_name (action) != NULL);
557
558   g_hash_table_insert (action_group->private_data->actions, 
559                        g_strdup (gtk_action_get_name (action)),
560                        g_object_ref (action));
561   g_object_set (action, I_("action-group"), action_group, NULL);
562 }
563
564 /**
565  * gtk_action_group_add_action_with_accel:
566  * @action_group: the action group 
567  * @action: the action to add 
568  * @accelerator: the accelerator for the action, in
569  *   the format understood by gtk_accelerator_parse(), or "" for no accelerator, or 
570  *   %NULL to use the stock accelerator 
571  *
572  * Adds an action object to the action group and sets up the accelerator.
573  *
574  * If @accelerator is %NULL, attempts to use the accelerator associated 
575  * with the stock_id of the action. 
576  *
577  * Accel paths are set to
578  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
579  *
580  * Since: 2.4
581  */
582 void
583 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
584                                         GtkAction      *action,
585                                         const gchar    *accelerator)
586 {
587   gchar *accel_path;
588   guint  accel_key = 0;
589   GdkModifierType accel_mods;
590   GtkStockItem stock_item;
591   gchar *name;
592   gchar *stock_id;
593   
594   g_object_get (action, "name", &name, "stock-id", &stock_id, NULL);
595
596   accel_path = g_strconcat ("<Actions>/",
597                             action_group->private_data->name, "/", name, NULL);
598
599   if (accelerator)
600     {
601       if (accelerator[0] == 0) 
602         accel_key = 0;
603       else
604         {
605           gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
606           if (accel_key == 0)
607             g_warning ("Unable to parse accelerator '%s' for action '%s'",
608                        accelerator, name);
609         }
610     }
611   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
612     {
613       accel_key = stock_item.keyval;
614       accel_mods = stock_item.modifier;
615     }
616
617   if (accel_key)
618     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
619
620   gtk_action_set_accel_path (action, accel_path);
621   gtk_action_group_add_action (action_group, action);
622
623   g_free (accel_path);
624   g_free (stock_id);
625   g_free (name);
626 }
627
628 /**
629  * gtk_action_group_remove_action:
630  * @action_group: the action group
631  * @action: an action
632  *
633  * Removes an action object from the action group.
634  *
635  * Since: 2.4
636  */
637 void
638 gtk_action_group_remove_action (GtkActionGroup *action_group,
639                                 GtkAction      *action)
640 {
641   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
642   g_return_if_fail (GTK_IS_ACTION (action));
643   g_return_if_fail (gtk_action_get_name (action) != NULL);
644
645   /* extra protection to make sure action->name is valid */
646   g_object_ref (action);
647   g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
648   g_object_unref (action);
649 }
650
651 static void
652 add_single_action (gpointer key, 
653                    gpointer value, 
654                    gpointer user_data)
655 {
656   GList **list = user_data;
657
658   *list = g_list_prepend (*list, value);
659 }
660
661 /**
662  * gtk_action_group_list_actions:
663  * @action_group: the action group
664  *
665  * Lists the actions in the action group.
666  *
667  * Returns: an allocated list of the action objects in the action group
668  * 
669  * Since: 2.4
670  */
671 GList *
672 gtk_action_group_list_actions (GtkActionGroup *action_group)
673 {
674   GList *actions = NULL;
675   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
676   
677   g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
678
679   return g_list_reverse (actions);
680 }
681
682
683 /**
684  * gtk_action_group_add_actions:
685  * @action_group: the action group
686  * @entries: an array of action descriptions
687  * @n_entries: the number of entries
688  * @user_data: data to pass to the action callbacks
689  *
690  * This is a convenience function to create a number of actions and add them 
691  * to the action group.
692  *
693  * The "activate" signals of the actions are connected to the callbacks and 
694  * their accel paths are set to 
695  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
696  * 
697  * Since: 2.4
698  */
699 void
700 gtk_action_group_add_actions (GtkActionGroup       *action_group,
701                               const GtkActionEntry *entries,
702                               guint                 n_entries,
703                               gpointer              user_data)
704 {
705   gtk_action_group_add_actions_full (action_group, 
706                                      entries, n_entries, 
707                                      user_data, NULL);
708 }
709
710 typedef struct _SharedData  SharedData;
711
712 struct _SharedData {
713   guint          ref_count;
714   gpointer       data;
715   GDestroyNotify destroy;
716 };
717
718 static void
719 shared_data_unref (gpointer data)
720 {
721   SharedData *shared_data = (SharedData *)data;
722
723   shared_data->ref_count--;
724   if (shared_data->ref_count == 0)
725     {
726       if (shared_data->destroy) 
727         (*shared_data->destroy) (shared_data->data);
728       
729       g_slice_free (SharedData, shared_data);
730     }
731 }
732
733
734 /**
735  * gtk_action_group_add_actions_full:
736  * @action_group: the action group
737  * @entries: an array of action descriptions
738  * @n_entries: the number of entries
739  * @user_data: data to pass to the action callbacks
740  * @destroy: destroy notification callback for @user_data
741  *
742  * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
743  * callback for @user_data. 
744  * 
745  * Since: 2.4
746  */
747 void
748 gtk_action_group_add_actions_full (GtkActionGroup       *action_group,
749                                    const GtkActionEntry *entries,
750                                    guint                 n_entries,
751                                    gpointer              user_data,
752                                    GDestroyNotify        destroy)
753 {
754
755   /* Keep this in sync with the other 
756    * gtk_action_group_add_..._actions_full() functions.
757    */
758   guint i;
759   SharedData *shared_data;
760
761   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
762
763   shared_data = g_slice_new0 (SharedData);
764   shared_data->ref_count = 1;
765   shared_data->data = user_data;
766   shared_data->destroy = destroy;
767
768   for (i = 0; i < n_entries; i++)
769     {
770       GtkAction *action;
771       const gchar *label;
772       const gchar *tooltip;
773
774       label = gtk_action_group_translate_string (action_group, entries[i].label);
775       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
776
777       action = gtk_action_new (entries[i].name,
778                                label,
779                                tooltip,
780                                entries[i].stock_id);
781
782       if (entries[i].callback)
783         {
784           GClosure *closure;
785
786           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
787           g_closure_add_finalize_notifier (closure, shared_data, 
788                                            (GClosureNotify)shared_data_unref);
789           shared_data->ref_count++;
790
791           g_signal_connect_closure (action, "activate", closure, FALSE);
792         }
793           
794       gtk_action_group_add_action_with_accel (action_group, 
795                                               action,
796                                               entries[i].accelerator);
797       g_object_unref (action);
798     }
799
800   shared_data_unref (shared_data);
801 }
802
803 /**
804  * gtk_action_group_add_toggle_actions:
805  * @action_group: the action group
806  * @entries: an array of toggle action descriptions
807  * @n_entries: the number of entries
808  * @user_data: data to pass to the action callbacks
809  *
810  * This is a convenience function to create a number of toggle actions and add them 
811  * to the action group.
812  *
813  * The "activate" signals of the actions are connected to the callbacks and 
814  * their accel paths are set to 
815  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
816  * 
817  * Since: 2.4
818  */
819 void
820 gtk_action_group_add_toggle_actions (GtkActionGroup             *action_group,
821                                      const GtkToggleActionEntry *entries,
822                                      guint                       n_entries,
823                                      gpointer                    user_data)
824 {
825   gtk_action_group_add_toggle_actions_full (action_group, 
826                                             entries, n_entries, 
827                                             user_data, NULL);
828 }
829
830
831 /**
832  * gtk_action_group_add_toggle_actions_full:
833  * @action_group: the action group
834  * @entries: an array of toggle action descriptions
835  * @n_entries: the number of entries
836  * @user_data: data to pass to the action callbacks
837  * @destroy: destroy notification callback for @user_data
838  *
839  * This variant of gtk_action_group_add_toggle_actions() adds a 
840  * #GDestroyNotify callback for @user_data. 
841  * 
842  * Since: 2.4
843  */
844 void
845 gtk_action_group_add_toggle_actions_full (GtkActionGroup             *action_group,
846                                           const GtkToggleActionEntry *entries,
847                                           guint                       n_entries,
848                                           gpointer                    user_data,
849                                           GDestroyNotify              destroy)
850 {
851   /* Keep this in sync with the other 
852    * gtk_action_group_add_..._actions_full() functions.
853    */
854   guint i;
855   SharedData *shared_data;
856
857   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
858
859   shared_data = g_slice_new0 (SharedData);
860   shared_data->ref_count = 1;
861   shared_data->data = user_data;
862   shared_data->destroy = destroy;
863
864   for (i = 0; i < n_entries; i++)
865     {
866       GtkToggleAction *action;
867       const gchar *label;
868       const gchar *tooltip;
869
870       label = gtk_action_group_translate_string (action_group, entries[i].label);
871       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
872
873       action = gtk_toggle_action_new (entries[i].name,
874                                       label,
875                                       tooltip,
876                                       entries[i].stock_id);
877
878       gtk_toggle_action_set_active (action, entries[i].is_active);
879
880       if (entries[i].callback)
881         {
882           GClosure *closure;
883
884           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
885           g_closure_add_finalize_notifier (closure, shared_data, 
886                                            (GClosureNotify)shared_data_unref);
887           shared_data->ref_count++;
888
889           g_signal_connect_closure (action, "activate", closure, FALSE);
890         }
891           
892       gtk_action_group_add_action_with_accel (action_group, 
893                                               GTK_ACTION (action),
894                                               entries[i].accelerator);
895       g_object_unref (action);
896     }
897
898     shared_data_unref (shared_data);
899 }
900
901 /**
902  * gtk_action_group_add_radio_actions:
903  * @action_group: the action group
904  * @entries: an array of radio action descriptions
905  * @n_entries: the number of entries
906  * @value: the value of the action to activate initially, or -1 if
907  *   no action should be activated
908  * @on_change: the callback to connect to the changed signal
909  * @user_data: data to pass to the action callbacks
910  * 
911  * This is a convenience routine to create a group of radio actions and
912  * add them to the action group. 
913  *
914  * The "changed" signal of the first radio action is connected to the 
915  * @on_change callback and the accel paths of the actions are set to 
916  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
917  * 
918  * Since: 2.4
919  **/
920 void            
921 gtk_action_group_add_radio_actions (GtkActionGroup            *action_group,
922                                     const GtkRadioActionEntry *entries,
923                                     guint                      n_entries,
924                                     gint                       value,
925                                     GCallback                  on_change,
926                                     gpointer                   user_data)
927 {
928   gtk_action_group_add_radio_actions_full (action_group, 
929                                            entries, n_entries, 
930                                            value,
931                                            on_change, user_data, NULL);
932 }
933
934 /**
935  * gtk_action_group_add_radio_actions_full:
936  * @action_group: the action group
937  * @entries: an array of radio action descriptions
938  * @n_entries: the number of entries
939  * @value: the value of the action to activate initially, or -1 if
940  *   no action should be activated
941  * @on_change: the callback to connect to the changed signal
942  * @user_data: data to pass to the action callbacks
943  * @destroy: destroy notification callback for @user_data
944  *
945  * This variant of gtk_action_group_add_radio_actions() adds a 
946  * #GDestroyNotify callback for @user_data. 
947  * 
948  * Since: 2.4
949  **/
950 void            
951 gtk_action_group_add_radio_actions_full (GtkActionGroup            *action_group,
952                                          const GtkRadioActionEntry *entries,
953                                          guint                      n_entries,
954                                          gint                       value,
955                                          GCallback                  on_change,
956                                          gpointer                   user_data,
957                                          GDestroyNotify             destroy)
958 {
959   /* Keep this in sync with the other 
960    * gtk_action_group_add_..._actions_full() functions.
961    */
962   guint i;
963   GSList *group = NULL;
964   GtkRadioAction *first_action = NULL;
965
966   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
967
968   for (i = 0; i < n_entries; i++)
969     {
970       GtkRadioAction *action;
971       const gchar *label;
972       const gchar *tooltip; 
973
974       label = gtk_action_group_translate_string (action_group, entries[i].label);
975       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
976
977       action = gtk_radio_action_new (entries[i].name,
978                                      label,
979                                      tooltip,
980                                      entries[i].stock_id,
981                                      entries[i].value);
982
983       if (i == 0) 
984         first_action = action;
985
986       gtk_radio_action_set_group (action, group);
987       group = gtk_radio_action_get_group (action);
988
989       if (value == entries[i].value)
990         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
991
992       gtk_action_group_add_action_with_accel (action_group, 
993                                               GTK_ACTION (action),
994                                               entries[i].accelerator);
995       g_object_unref (action);
996     }
997
998   if (on_change && first_action)
999     g_signal_connect_data (first_action, "changed",
1000                            on_change, user_data, 
1001                            (GClosureNotify)destroy, 0);
1002 }
1003
1004 /**
1005  * gtk_action_group_set_translate_func:
1006  * @action_group: a #GtkActionGroup
1007  * @func: a #GtkTranslateFunc
1008  * @data: data to be passed to @func and @notify
1009  * @notify: a #GtkDestroyNotify function to be called when @action_group is 
1010  *   destroyed and when the translation function is changed again
1011  * 
1012  * Sets a function to be used for translating the @label and @tooltip of 
1013  * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1014  *
1015  * If you're using gettext(), it is enough to set the translation domain
1016  * with gtk_action_group_set_translation_domain().
1017  *
1018  * Since: 2.4 
1019  **/
1020 void
1021 gtk_action_group_set_translate_func (GtkActionGroup   *action_group,
1022                                      GtkTranslateFunc  func,
1023                                      gpointer          data,
1024                                      GtkDestroyNotify  notify)
1025 {
1026   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1027   
1028   if (action_group->private_data->translate_notify)
1029     action_group->private_data->translate_notify (action_group->private_data->translate_data);
1030       
1031   action_group->private_data->translate_func = func;
1032   action_group->private_data->translate_data = data;
1033   action_group->private_data->translate_notify = notify;
1034 }
1035
1036 static gchar *
1037 dgettext_swapped (const gchar *msgid, 
1038                   const gchar *domainname)
1039 {
1040   return dgettext (domainname, msgid);
1041 }
1042
1043 /**
1044  * gtk_action_group_set_translation_domain:
1045  * @action_group: a #GtkActionGroup
1046  * @domain: the translation domain to use for dgettext() calls
1047  * 
1048  * Sets the translation domain and uses dgettext() for translating the 
1049  * @label and @tooltip of #GtkActionEntry<!-- -->s added by 
1050  * gtk_action_group_add_actions().
1051  *
1052  * If you're not using gettext() for localization, see 
1053  * gtk_action_group_set_translate_func().
1054  *
1055  * Since: 2.4
1056  **/
1057 void 
1058 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1059                                          const gchar    *domain)
1060 {
1061   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1062
1063   gtk_action_group_set_translate_func (action_group, 
1064                                        (GtkTranslateFunc)dgettext_swapped,
1065                                        g_strdup (domain),
1066                                        g_free);
1067
1068
1069
1070 /**
1071  * gtk_action_group_translate_string:
1072  * @action_group: a #GtkActionGroup
1073  * @string: a string
1074  *
1075  * Translates a string using the specified translate_func(). This
1076  * is mainly intended for language bindings.
1077  *
1078  * Returns: the translation of @string
1079  *
1080  * Since: 2.6
1081  **/
1082 G_CONST_RETURN gchar *
1083 gtk_action_group_translate_string (GtkActionGroup *action_group,
1084                                    const gchar    *string)
1085 {
1086   GtkTranslateFunc translate_func;
1087   gpointer translate_data;
1088   
1089   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1090   
1091   if (string == NULL)
1092     return NULL;
1093
1094   translate_func = action_group->private_data->translate_func;
1095   translate_data = action_group->private_data->translate_data;
1096   
1097   if (translate_func)
1098     return translate_func (string, translate_data);
1099   else
1100     return string;
1101 }
1102
1103 /* Protected for use by GtkAction */
1104 void
1105 _gtk_action_group_emit_connect_proxy  (GtkActionGroup *action_group,
1106                                        GtkAction      *action,
1107                                        GtkWidget      *proxy)
1108 {
1109   g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0, 
1110                  action, proxy);
1111 }
1112
1113 void
1114 _gtk_action_group_emit_disconnect_proxy  (GtkActionGroup *action_group,
1115                                           GtkAction      *action,
1116                                           GtkWidget      *proxy)
1117 {
1118   g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0, 
1119                  action, proxy);
1120 }
1121
1122 void
1123 _gtk_action_group_emit_pre_activate  (GtkActionGroup *action_group,
1124                                       GtkAction      *action)
1125 {
1126   g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1127 }
1128
1129 void
1130 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1131                                       GtkAction      *action)
1132 {
1133   g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);
1134 }
1135
1136 #define __GTK_ACTION_GROUP_C__
1137 #include "gtkaliasdef.c"