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