]> Pileus Git - ~andy/gtk/blob - gtk/gtkactiongroup.c
The first part of the fix for #114351 (see also gdk-pixbuf/ChangeLog and
[~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 static void
250 gtk_action_group_init (GtkActionGroup *self)
251 {
252   self->private_data = GTK_ACTION_GROUP_GET_PRIVATE (self);
253   self->private_data->name = NULL;
254   self->private_data->sensitive = TRUE;
255   self->private_data->visible = TRUE;
256   self->private_data->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
257                                                        (GDestroyNotify) g_free,
258                                                        (GDestroyNotify) g_object_unref);
259   self->private_data->translate_func = NULL;
260   self->private_data->translate_data = NULL;
261   self->private_data->translate_notify = NULL;
262 }
263
264 /**
265  * gtk_action_group_new:
266  * @name: the name of the action group.
267  *
268  * Creates a new #GtkActionGroup object. The name of the action group
269  * is used when associating <link linkend="Action-Accel">keybindings</link> 
270  * with the actions.
271  *
272  * Returns: the new #GtkActionGroup
273  *
274  * Since: 2.4
275  */
276 GtkActionGroup *
277 gtk_action_group_new (const gchar *name)
278 {
279   GtkActionGroup *self;
280
281   self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
282   self->private_data->name = g_strdup (name);
283
284   return self;
285 }
286
287 static void
288 gtk_action_group_finalize (GObject *object)
289 {
290   GtkActionGroup *self;
291
292   self = GTK_ACTION_GROUP (object);
293
294   g_free (self->private_data->name);
295   self->private_data->name = NULL;
296
297   g_hash_table_destroy (self->private_data->actions);
298   self->private_data->actions = NULL;
299
300   if (self->private_data->translate_notify)
301     self->private_data->translate_notify (self->private_data->translate_data);
302
303   if (parent_class->finalize)
304     (* parent_class->finalize) (object);
305 }
306
307 static void
308 gtk_action_group_set_property (GObject         *object,
309                                guint            prop_id,
310                                const GValue    *value,
311                                GParamSpec      *pspec)
312 {
313   GtkActionGroup *self;
314   gchar *tmp;
315   
316   self = GTK_ACTION_GROUP (object);
317
318   switch (prop_id)
319     {
320     case PROP_NAME:
321       tmp = self->private_data->name;
322       self->private_data->name = g_value_dup_string (value);
323       g_free (tmp);
324       break;
325     case PROP_SENSITIVE:
326       gtk_action_group_set_sensitive (self, g_value_get_boolean (value));
327       break;
328     case PROP_VISIBLE:
329       gtk_action_group_set_visible (self, g_value_get_boolean (value));
330       break;
331     default:
332       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333       break;
334     }
335 }
336
337 static void
338 gtk_action_group_get_property (GObject    *object,
339                                guint       prop_id,
340                                GValue     *value,
341                                GParamSpec *pspec)
342 {
343   GtkActionGroup *self;
344   
345   self = GTK_ACTION_GROUP (object);
346
347   switch (prop_id)
348     {
349     case PROP_NAME:
350       g_value_set_string (value, self->private_data->name);
351       break;
352     case PROP_SENSITIVE:
353       g_value_set_boolean (value, self->private_data->sensitive);
354       break;
355     case PROP_VISIBLE:
356       g_value_set_boolean (value, self->private_data->visible);
357       break;
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361     }
362 }
363
364 static GtkAction *
365 gtk_action_group_real_get_action (GtkActionGroup *self,
366                                   const gchar    *action_name)
367 {
368   return g_hash_table_lookup (self->private_data->actions, action_name);
369 }
370
371 /**
372  * gtk_action_group_get_name:
373  * @action_group: the action group
374  *
375  * Gets the name of the action group.
376  *
377  * Returns: the name of the action group.
378  * 
379  * Since: 2.4
380  */
381 const gchar *
382 gtk_action_group_get_name (GtkActionGroup *action_group)
383 {
384   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
385
386   return action_group->private_data->name;
387 }
388
389 /**
390  * gtk_action_group_get_sensitive:
391  * @action_group: the action group
392  *
393  * Returns %TRUE if the group is sensitive.  The constituent actions
394  * can only be logically sensitive (see gtk_action_is_sensitive()) if
395  * they are sensitive (see gtk_action_get_sensitive()) and their group
396  * is sensitive.
397  * 
398  * Return value: %TRUE if the group is sensitive.
399  *
400  * Since: 2.4
401  */
402 gboolean
403 gtk_action_group_get_sensitive (GtkActionGroup *action_group)
404 {
405   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
406
407   return action_group->private_data->sensitive;
408 }
409
410 static void
411 cb_set_action_sensitivity (const gchar *name, GtkAction *action)
412 {
413   /* Minor optimization, the action_groups state only effects actions that are
414    * themselves sensitive */
415   if (gtk_action_get_sensitive (action))
416     g_object_notify (G_OBJECT (action), "sensitive");
417 }
418
419 /**
420  * gtk_action_group_set_sensitive:
421  * @action_group: the action group
422  * @sensitive: new sensitivity
423  *
424  * Changes the sensitivity of @action_group
425  * 
426  * Since: 2.4
427  */
428 void
429 gtk_action_group_set_sensitive (GtkActionGroup *action_group, gboolean sensitive)
430 {
431   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
432
433   if (action_group->private_data->sensitive ^ sensitive)
434     {
435       action_group->private_data->sensitive = sensitive;
436       g_hash_table_foreach (action_group->private_data->actions, 
437                             (GHFunc) cb_set_action_sensitivity, NULL);
438     }
439 }
440
441 /**
442  * gtk_action_group_get_visible:
443  * @action_group: the action group
444  *
445  * Returns %TRUE if the group is visible.  The constituent actions
446  * can only be logically visible (see gtk_action_is_visible()) if
447  * they are visible (see gtk_action_get_visible()) and their group
448  * is visible.
449  * 
450  * Return value: %TRUE if the group is sensitive.
451  * 
452  * Since: 2.4
453  */
454 gboolean
455 gtk_action_group_get_visible (GtkActionGroup *action_group)
456 {
457   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
458
459   return action_group->private_data->visible;
460 }
461
462 static void
463 cb_set_action_visiblity (const gchar *name, GtkAction *action)
464 {
465   /* Minor optimization, the action_groups state only effects actions that are
466    * themselves sensitive */
467   if (gtk_action_get_visible (action))
468     g_object_notify (G_OBJECT (action), "visible");
469 }
470
471 /**
472  * gtk_action_group_set_visible:
473  * @action_group: the action group
474  * @visible: new visiblity
475  *
476  * Changes the visible of @action_group.
477  * 
478  * Since: 2.4
479  */
480 void
481 gtk_action_group_set_visible (GtkActionGroup *action_group, gboolean visible)
482 {
483   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
484
485   if (action_group->private_data->visible ^ visible)
486     {
487       action_group->private_data->visible = visible;
488       g_hash_table_foreach (action_group->private_data->actions, 
489                             (GHFunc) cb_set_action_visiblity, NULL);
490     }
491 }
492
493 /**
494  * gtk_action_group_get_action:
495  * @action_group: the action group
496  * @action_name: the name of the action
497  *
498  * Looks up an action in the action group by name.
499  *
500  * Returns: the action, or %NULL if no action by that name exists
501  *
502  * Since: 2.4
503  */
504 GtkAction *
505 gtk_action_group_get_action (GtkActionGroup *action_group,
506                              const gchar    *action_name)
507 {
508   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
509   g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
510
511   return (* GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action)
512     (action_group, action_name);
513 }
514
515 /**
516  * gtk_action_group_add_action:
517  * @action_group: the action group
518  * @action: an action
519  *
520  * Adds an action object to the action group. 
521  *
522  * Since: 2.4
523  */
524 void
525 gtk_action_group_add_action (GtkActionGroup *action_group,
526                              GtkAction      *action)
527 {
528   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
529   g_return_if_fail (GTK_IS_ACTION (action));
530   g_return_if_fail (gtk_action_get_name (action) != NULL);
531
532   g_hash_table_insert (action_group->private_data->actions, 
533                        g_strdup (gtk_action_get_name (action)),
534                        g_object_ref (action));
535   g_object_set (G_OBJECT (action), "action_group", action_group, NULL);
536 }
537
538 /**
539  * gtk_action_group_add_action_with_accel:
540  * @action_group: the action group 
541  * @action: the action to add 
542  * @accelerator: the accelerator for the action, in
543  *   the format understood by gtk_accelerator_parse(), or %NULL to use the
544  *   stock accelerator 
545  *
546  * Adds an action object to the action group and sets up the accelerator.
547  *
548  * If @accelerator is %NULL, attempts to use the accelerator associated 
549  * with the stock_id of the action.
550  *
551  * Accel paths are set to
552  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
553  *
554  * Since: 2.4
555  */
556 void
557 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
558                                         GtkAction *action,
559                                         const gchar *accelerator)
560 {
561   gchar *accel_path;
562   guint  accel_key = 0;
563   GdkModifierType accel_mods;
564   GtkStockItem stock_item;
565   gchar *name;
566   gchar *stock_id;
567   
568   g_object_get (action, "name", &name, "stock_id", &stock_id, NULL);
569
570   accel_path = g_strconcat ("<Actions>/",
571                             action_group->private_data->name, "/", name, NULL);
572
573   if (accelerator)
574     gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
575   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
576     {
577       accel_key = stock_item.keyval;
578       accel_mods = stock_item.modifier;
579     }
580
581   if (accel_key)
582     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
583
584   gtk_action_set_accel_path (action, accel_path);
585   gtk_action_group_add_action (action_group, action);
586
587   g_free (accel_path);
588   g_free (stock_id);
589   g_free (name);
590 }
591
592 /**
593  * gtk_action_group_remove_action:
594  * @action_group: the action group
595  * @action: an action
596  *
597  * Removes an action object from the action group.
598  *
599  * Since: 2.4
600  */
601 void
602 gtk_action_group_remove_action (GtkActionGroup *action_group,
603                                 GtkAction      *action)
604 {
605   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
606   g_return_if_fail (GTK_IS_ACTION (action));
607   g_return_if_fail (gtk_action_get_name (action) != NULL);
608
609   /* extra protection to make sure action->name is valid */
610   g_object_ref (action);
611   g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
612   g_object_set (G_OBJECT (action), "action_group", NULL, NULL);
613   g_object_unref (action);
614 }
615
616 static void
617 add_single_action (gpointer key, 
618                    gpointer value, 
619                    gpointer user_data)
620 {
621   GList **list = user_data;
622
623   *list = g_list_prepend (*list, value);
624 }
625
626 /**
627  * gtk_action_group_list_actions:
628  * @action_group: the action group
629  *
630  * Lists the actions in the action group.
631  *
632  * Returns: an allocated list of the action objects in the action group
633  * 
634  * Since: 2.4
635  */
636 GList *
637 gtk_action_group_list_actions (GtkActionGroup *action_group)
638 {
639   GList *actions = NULL;
640   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
641   
642   g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
643
644   return g_list_reverse (actions);
645 }
646
647
648 /**
649  * gtk_action_group_add_actions:
650  * @action_group: the action group
651  * @entries: an array of action descriptions
652  * @n_entries: the number of entries
653  * @user_data: data to pass to the action callbacks
654  *
655  * This is a convenience function to create a number of actions and add them 
656  * to the action group.
657  *
658  * The "activate" signals of the actions are connected to the callbacks and 
659  * their accel paths are set to 
660  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
661  * 
662  * Since: 2.4
663  */
664 void
665 gtk_action_group_add_actions (GtkActionGroup *action_group,
666                               GtkActionEntry *entries,
667                               guint           n_entries,
668                               gpointer        user_data)
669 {
670   gtk_action_group_add_actions_full (action_group, 
671                                      entries, n_entries, 
672                                      user_data, NULL);
673 }
674
675
676 /**
677  * gtk_action_group_add_actions_full:
678  * @action_group: the action group
679  * @entries: an array of action descriptions
680  * @n_entries: the number of entries
681  * @user_data: data to pass to the action callbacks
682  * @destroy: destroy notification callback for @user_data
683  *
684  * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
685  * callback for @user_data. 
686  * 
687  * Since: 2.4
688  */
689 void
690 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
691                                    GtkActionEntry *entries,
692                                    guint           n_entries,
693                                    gpointer        user_data,
694                                    GDestroyNotify  destroy)
695 {
696
697   /* Keep this in sync with the other 
698    * gtk_action_group_add_..._actions_full() functions.
699    */
700   guint i;
701   GtkTranslateFunc translate_func;
702   gpointer translate_data;
703
704   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
705
706   translate_func = action_group->private_data->translate_func;
707   translate_data = action_group->private_data->translate_data;
708
709   for (i = 0; i < n_entries; i++)
710     {
711       GtkAction *action;
712       const gchar *label;
713       const gchar *tooltip;
714
715       if (translate_func)
716         {
717           label = translate_func (entries[i].label, translate_data);
718           tooltip = translate_func (entries[i].tooltip, translate_data);
719         }
720       else
721         {
722           label = entries[i].label;
723           tooltip = entries[i].tooltip;
724         }
725
726       action = gtk_action_new (entries[i].name,
727                                label,
728                                tooltip,
729                                entries[i].stock_id);
730
731       if (entries[i].callback)
732         g_signal_connect_data (action, "activate",
733                                entries[i].callback, 
734                                user_data, (GClosureNotify)destroy, 0);
735
736       gtk_action_group_add_action_with_accel (action_group, 
737                                               action,
738                                               entries[i].accelerator);
739       g_object_unref (action);
740     }
741 }
742
743 /**
744  * gtk_action_group_add_toggle_actions:
745  * @action_group: the action group
746  * @entries: an array of toggle action descriptions
747  * @n_entries: the number of entries
748  * @user_data: data to pass to the action callbacks
749  *
750  * This is a convenience function to create a number of toggle actions and add them 
751  * to the action group.
752  *
753  * The "activate" signals of the actions are connected to the callbacks and 
754  * their accel paths are set to 
755  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
756  * 
757  * Since: 2.4
758  */
759 void
760 gtk_action_group_add_toggle_actions (GtkActionGroup       *action_group,
761                                      GtkToggleActionEntry *entries,
762                                      guint                 n_entries,
763                                      gpointer              user_data)
764 {
765   gtk_action_group_add_toggle_actions_full (action_group, 
766                                             entries, n_entries, 
767                                             user_data, NULL);
768 }
769
770
771 /**
772  * gtk_action_group_add_toggle_actions_full:
773  * @action_group: the action group
774  * @entries: an array of toggle action descriptions
775  * @n_entries: the number of entries
776  * @user_data: data to pass to the action callbacks
777  * @destroy: destroy notification callback for @user_data
778  *
779  * This variant of gtk_action_group_add_toggle_actions() adds a 
780  * #GDestroyNotify callback for @user_data. 
781  * 
782  * Since: 2.4
783  */
784 void
785 gtk_action_group_add_toggle_actions_full (GtkActionGroup       *action_group,
786                                           GtkToggleActionEntry *entries,
787                                           guint                 n_entries,
788                                           gpointer              user_data,
789                                           GDestroyNotify        destroy)
790 {
791   /* Keep this in sync with the other 
792    * gtk_action_group_add_..._actions_full() functions.
793    */
794   guint i;
795   GtkTranslateFunc translate_func;
796   gpointer translate_data;
797
798   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
799
800   translate_func = action_group->private_data->translate_func;
801   translate_data = action_group->private_data->translate_data;
802
803   for (i = 0; i < n_entries; i++)
804     {
805       GtkToggleAction *action;
806       const gchar *label;
807       const gchar *tooltip;
808
809       if (translate_func)
810         {
811           label = translate_func (entries[i].label, translate_data);
812           tooltip = translate_func (entries[i].tooltip, translate_data);
813         }
814       else
815         {
816           label = entries[i].label;
817           tooltip = entries[i].tooltip;
818         }
819
820       action = gtk_toggle_action_new (entries[i].name,
821                                       label,
822                                       tooltip,
823                                       entries[i].stock_id);
824
825       gtk_toggle_action_set_active (action, entries[i].is_active);
826
827       if (entries[i].callback)
828         g_signal_connect_data (action, "activate",
829                                entries[i].callback, 
830                                user_data, (GClosureNotify)destroy, 0);
831
832       gtk_action_group_add_action_with_accel (action_group, 
833                                               GTK_ACTION (action),
834                                               entries[i].accelerator);
835       g_object_unref (action);
836     }
837 }
838
839 /**
840  * gtk_action_group_add_radio_actions:
841  * @action_group: the action group
842  * @entries: an array of radio action descriptions
843  * @n_entries: the number of entries
844  * @value: the value of the action to activate initially, or -1 if
845  *   no action should be activated
846  * @on_change: the callback to connect to the changed signal
847  * @user_data: data to pass to the action callbacks
848  * 
849  * This is a convenience routine to create a group of radio actions and
850  * add them to the action group. 
851  *
852  * The "changed" signal of the first radio action is connected to the 
853  * @on_change callback and the accel paths of the actions are set to 
854  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
855  * 
856  * Since: 2.4
857  **/
858 void            
859 gtk_action_group_add_radio_actions (GtkActionGroup      *action_group,
860                                     GtkRadioActionEntry *entries,
861                                     guint                n_entries,
862                                     gint                 value,
863                                     GCallback            on_change,
864                                     gpointer             user_data)
865 {
866   gtk_action_group_add_radio_actions_full (action_group, 
867                                            entries, n_entries, 
868                                            value,
869                                            on_change, user_data, NULL);
870 }
871
872 /**
873  * gtk_action_group_add_radio_actions_full:
874  * @action_group: the action group
875  * @entries: an array of radio action descriptions
876  * @n_entries: the number of entries
877  * @value: the value of the action to activate initially, or -1 if
878  *   no action should be activated
879  * @on_change: the callback to connect to the changed signal
880  * @user_data: data to pass to the action callbacks
881  * @destroy: destroy notification callback for @user_data
882  *
883  * This variant of gtk_action_group_add_radio_actions() adds a 
884  * #GDestroyNotify callback for @user_data. 
885  * 
886  * Since: 2.4
887  **/
888 void            
889 gtk_action_group_add_radio_actions_full (GtkActionGroup      *action_group,
890                                          GtkRadioActionEntry *entries,
891                                          guint                n_entries,
892                                          gint                 value,
893                                          GCallback            on_change,
894                                          gpointer             user_data,
895                                          GDestroyNotify       destroy)
896 {
897   /* Keep this in sync with the other 
898    * gtk_action_group_add_..._actions_full() functions.
899    */
900   guint i;
901   GtkTranslateFunc translate_func;
902   gpointer translate_data;
903   GSList *group = NULL;
904   GtkRadioAction *first_action = NULL;
905
906   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
907
908   translate_func = action_group->private_data->translate_func;
909   translate_data = action_group->private_data->translate_data;
910
911   for (i = 0; i < n_entries; i++)
912     {
913       GtkRadioAction *action;
914       const gchar *label;
915       const gchar *tooltip; 
916
917       if (translate_func)
918         {
919           label = translate_func (entries[i].label, translate_data);
920           tooltip = translate_func (entries[i].tooltip, translate_data);
921         }
922       else
923         {
924           label = entries[i].label;
925           tooltip = entries[i].tooltip;
926         }
927
928       action = gtk_radio_action_new (entries[i].name,
929                                      label,
930                                      tooltip,
931                                      entries[i].stock_id,
932                                      value);
933
934       if (i == 0) 
935         first_action = action;
936
937       gtk_radio_action_set_group (action, group);
938       group = gtk_radio_action_get_group (action);
939
940       if (value == entries[i].value)
941         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
942
943       gtk_action_group_add_action_with_accel (action_group, 
944                                               GTK_ACTION (action),
945                                               entries[i].accelerator);
946       g_object_unref (action);
947     }
948
949   if (on_change && first_action)
950     g_signal_connect_data (first_action, "changed",
951                            on_change, user_data, 
952                            (GClosureNotify)destroy, 0);
953 }
954
955 /**
956  * gtk_action_group_set_translate_func:
957  * @action_group: a #GtkActionGroup
958  * @func: a #GtkTranslateFunc
959  * @data: data to be passed to @func and @notify
960  * @notify: a #GtkDestroyNotify function to be called when @action_group is 
961  *   destroyed and when the translation function is changed again
962  * 
963  * Sets a function to be used for translating the @label and @tooltip of 
964  * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
965  *
966  * If you're using gettext(), it is enough to set the translation domain
967  * with gtk_action_group_set_translation_domain().
968  *
969  * Since: 2.4 
970  **/
971 void
972 gtk_action_group_set_translate_func (GtkActionGroup      *action_group,
973                                      GtkTranslateFunc     func,
974                                      gpointer             data,
975                                      GtkDestroyNotify     notify)
976 {
977   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
978   
979   if (action_group->private_data->translate_notify)
980     action_group->private_data->translate_notify (action_group->private_data->translate_data);
981       
982   action_group->private_data->translate_func = func;
983   action_group->private_data->translate_data = data;
984   action_group->private_data->translate_notify = notify;
985 }
986
987 static gchar *
988 dgettext_swapped (const gchar *msgid, 
989                   const gchar *domainname)
990 {
991   return dgettext (domainname, msgid);
992 }
993
994 /**
995  * gtk_action_group_set_translation_domain:
996  * @action_group: a #GtkActionGroup
997  * @domain: the translation domain to use for dgettext() calls
998  * 
999  * Sets the translation domain and uses dgettext() for translating the 
1000  * @label and @tooltip of #GtkActionEntry<!-- -->s added by 
1001  * gtk_action_group_add_actions().
1002  *
1003  * If you're not using gettext() for localization, see 
1004  * gtk_action_group_set_translate_func().
1005  *
1006  * Since: 2.4
1007  **/
1008 void 
1009 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1010                                          const gchar    *domain)
1011 {
1012   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1013
1014   gtk_action_group_set_translate_func (action_group, 
1015                                        (GtkTranslateFunc)dgettext_swapped,
1016                                        g_strdup (domain),
1017                                        g_free);
1018
1019
1020 /* Protected for use by GtkAction */
1021 void
1022 _gtk_action_group_emit_connect_proxy  (GtkActionGroup *action_group,
1023                                        GtkAction      *action,
1024                                        GtkWidget      *proxy)
1025 {
1026   g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0, 
1027                  action, proxy);
1028 }
1029
1030 void
1031 _gtk_action_group_emit_disconnect_proxy  (GtkActionGroup *action_group,
1032                                           GtkAction      *action,
1033                                           GtkWidget      *proxy)
1034 {
1035   g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0, 
1036                  action, proxy);
1037 }
1038
1039 void
1040 _gtk_action_group_emit_pre_activate  (GtkActionGroup *action_group,
1041                                       GtkAction      *action)
1042 {
1043   g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1044 }
1045
1046 void
1047 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1048                                       GtkAction *action)
1049 {
1050   g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);
1051 }