]> Pileus Git - ~andy/gtk/blob - gtk/gtkactiongroup.c
|___* demos/gtk-demo/demo.ui: |___* gtk/gtk-builder-convert: |___*
[~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                                                        (GDestroyNotify) g_free,
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   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
747   g_return_if_fail (GTK_IS_ACTION (action));
748   g_return_if_fail (gtk_action_get_name (action) != NULL);
749   
750   if (!check_unique_action (action_group, gtk_action_get_name (action)))
751     return;
752
753   g_hash_table_insert (action_group->private_data->actions, 
754                        g_strdup (gtk_action_get_name (action)),
755                        g_object_ref (action));
756   g_object_set (action, I_("action-group"), action_group, NULL);
757 }
758
759 /**
760  * gtk_action_group_add_action_with_accel:
761  * @action_group: the action group 
762  * @action: the action to add 
763  * @accelerator: the accelerator for the action, in
764  *   the format understood by gtk_accelerator_parse(), or "" for no accelerator, or 
765  *   %NULL to use the stock accelerator 
766  *
767  * Adds an action object to the action group and sets up the accelerator.
768  *
769  * If @accelerator is %NULL, attempts to use the accelerator associated 
770  * with the stock_id of the action. 
771  *
772  * Accel paths are set to
773  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
774  *
775  * Since: 2.4
776  */
777 void
778 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
779                                         GtkAction      *action,
780                                         const gchar    *accelerator)
781 {
782   gchar *accel_path;
783   guint  accel_key = 0;
784   GdkModifierType accel_mods;
785   GtkStockItem stock_item;
786   gchar *name;
787   gchar *stock_id;
788
789   if (!check_unique_action (action_group, gtk_action_get_name (action)))
790     return;
791   
792   g_object_get (action, "name", &name, "stock-id", &stock_id, NULL);
793
794   accel_path = g_strconcat ("<Actions>/",
795                             action_group->private_data->name, "/", name, NULL);
796
797   if (accelerator)
798     {
799       if (accelerator[0] == 0) 
800         accel_key = 0;
801       else
802         {
803           gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
804           if (accel_key == 0)
805             g_warning ("Unable to parse accelerator '%s' for action '%s'",
806                        accelerator, name);
807         }
808     }
809   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
810     {
811       accel_key = stock_item.keyval;
812       accel_mods = stock_item.modifier;
813     }
814
815   if (accel_key)
816     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
817
818   gtk_action_set_accel_path (action, accel_path);
819   gtk_action_group_add_action (action_group, action);
820
821   g_free (accel_path);
822   g_free (stock_id);
823   g_free (name);
824 }
825
826 /**
827  * gtk_action_group_remove_action:
828  * @action_group: the action group
829  * @action: an action
830  *
831  * Removes an action object from the action group.
832  *
833  * Since: 2.4
834  */
835 void
836 gtk_action_group_remove_action (GtkActionGroup *action_group,
837                                 GtkAction      *action)
838 {
839   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
840   g_return_if_fail (GTK_IS_ACTION (action));
841   g_return_if_fail (gtk_action_get_name (action) != NULL);
842
843   /* extra protection to make sure action->name is valid */
844   g_object_ref (action);
845   g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
846   g_object_unref (action);
847 }
848
849 static void
850 add_single_action (gpointer key, 
851                    gpointer value, 
852                    gpointer user_data)
853 {
854   GList **list = user_data;
855
856   *list = g_list_prepend (*list, value);
857 }
858
859 /**
860  * gtk_action_group_list_actions:
861  * @action_group: the action group
862  *
863  * Lists the actions in the action group.
864  *
865  * Returns: an allocated list of the action objects in the action group
866  * 
867  * Since: 2.4
868  */
869 GList *
870 gtk_action_group_list_actions (GtkActionGroup *action_group)
871 {
872   GList *actions = NULL;
873   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
874   
875   g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
876
877   return g_list_reverse (actions);
878 }
879
880
881 /**
882  * gtk_action_group_add_actions:
883  * @action_group: the action group
884  * @entries: an array of action descriptions
885  * @n_entries: the number of entries
886  * @user_data: data to pass to the action callbacks
887  *
888  * This is a convenience function to create a number of actions and add them 
889  * to the action group.
890  *
891  * The "activate" signals of the actions are connected to the callbacks and 
892  * their accel paths are set to 
893  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
894  * 
895  * Since: 2.4
896  */
897 void
898 gtk_action_group_add_actions (GtkActionGroup       *action_group,
899                               const GtkActionEntry *entries,
900                               guint                 n_entries,
901                               gpointer              user_data)
902 {
903   gtk_action_group_add_actions_full (action_group, 
904                                      entries, n_entries, 
905                                      user_data, NULL);
906 }
907
908 typedef struct _SharedData  SharedData;
909
910 struct _SharedData {
911   guint          ref_count;
912   gpointer       data;
913   GDestroyNotify destroy;
914 };
915
916 static void
917 shared_data_unref (gpointer data)
918 {
919   SharedData *shared_data = (SharedData *)data;
920
921   shared_data->ref_count--;
922   if (shared_data->ref_count == 0)
923     {
924       if (shared_data->destroy) 
925         (*shared_data->destroy) (shared_data->data);
926       
927       g_slice_free (SharedData, shared_data);
928     }
929 }
930
931
932 /**
933  * gtk_action_group_add_actions_full:
934  * @action_group: the action group
935  * @entries: an array of action descriptions
936  * @n_entries: the number of entries
937  * @user_data: data to pass to the action callbacks
938  * @destroy: destroy notification callback for @user_data
939  *
940  * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
941  * callback for @user_data. 
942  * 
943  * Since: 2.4
944  */
945 void
946 gtk_action_group_add_actions_full (GtkActionGroup       *action_group,
947                                    const GtkActionEntry *entries,
948                                    guint                 n_entries,
949                                    gpointer              user_data,
950                                    GDestroyNotify        destroy)
951 {
952
953   /* Keep this in sync with the other 
954    * gtk_action_group_add_..._actions_full() functions.
955    */
956   guint i;
957   SharedData *shared_data;
958
959   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
960
961   shared_data = g_slice_new0 (SharedData);
962   shared_data->ref_count = 1;
963   shared_data->data = user_data;
964   shared_data->destroy = destroy;
965
966   for (i = 0; i < n_entries; i++)
967     {
968       GtkAction *action;
969       const gchar *label;
970       const gchar *tooltip;
971
972       if (!check_unique_action (action_group, entries[i].name))
973         continue;
974
975       label = gtk_action_group_translate_string (action_group, entries[i].label);
976       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
977
978       action = gtk_action_new (entries[i].name,
979                                label,
980                                tooltip,
981                                NULL);
982
983       if (entries[i].stock_id) 
984         {
985           g_object_set (action, "stock-id", entries[i].stock_id, NULL);
986           if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), 
987                                        entries[i].stock_id))
988             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
989         }
990           
991       if (entries[i].callback)
992         {
993           GClosure *closure;
994
995           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
996           g_closure_add_finalize_notifier (closure, shared_data, 
997                                            (GClosureNotify)shared_data_unref);
998           shared_data->ref_count++;
999
1000           g_signal_connect_closure (action, "activate", closure, FALSE);
1001         }
1002           
1003       gtk_action_group_add_action_with_accel (action_group, 
1004                                               action,
1005                                               entries[i].accelerator);
1006       g_object_unref (action);
1007     }
1008
1009   shared_data_unref (shared_data);
1010 }
1011
1012 /**
1013  * gtk_action_group_add_toggle_actions:
1014  * @action_group: the action group
1015  * @entries: an array of toggle action descriptions
1016  * @n_entries: the number of entries
1017  * @user_data: data to pass to the action callbacks
1018  *
1019  * This is a convenience function to create a number of toggle actions and add them 
1020  * to the action group.
1021  *
1022  * The "activate" signals of the actions are connected to the callbacks and 
1023  * their accel paths are set to 
1024  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
1025  * 
1026  * Since: 2.4
1027  */
1028 void
1029 gtk_action_group_add_toggle_actions (GtkActionGroup             *action_group,
1030                                      const GtkToggleActionEntry *entries,
1031                                      guint                       n_entries,
1032                                      gpointer                    user_data)
1033 {
1034   gtk_action_group_add_toggle_actions_full (action_group, 
1035                                             entries, n_entries, 
1036                                             user_data, NULL);
1037 }
1038
1039
1040 /**
1041  * gtk_action_group_add_toggle_actions_full:
1042  * @action_group: the action group
1043  * @entries: an array of toggle action descriptions
1044  * @n_entries: the number of entries
1045  * @user_data: data to pass to the action callbacks
1046  * @destroy: destroy notification callback for @user_data
1047  *
1048  * This variant of gtk_action_group_add_toggle_actions() adds a 
1049  * #GDestroyNotify callback for @user_data. 
1050  * 
1051  * Since: 2.4
1052  */
1053 void
1054 gtk_action_group_add_toggle_actions_full (GtkActionGroup             *action_group,
1055                                           const GtkToggleActionEntry *entries,
1056                                           guint                       n_entries,
1057                                           gpointer                    user_data,
1058                                           GDestroyNotify              destroy)
1059 {
1060   /* Keep this in sync with the other 
1061    * gtk_action_group_add_..._actions_full() functions.
1062    */
1063   guint i;
1064   SharedData *shared_data;
1065
1066   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1067
1068   shared_data = g_slice_new0 (SharedData);
1069   shared_data->ref_count = 1;
1070   shared_data->data = user_data;
1071   shared_data->destroy = destroy;
1072
1073   for (i = 0; i < n_entries; i++)
1074     {
1075       GtkToggleAction *action;
1076       const gchar *label;
1077       const gchar *tooltip;
1078
1079       if (!check_unique_action (action_group, entries[i].name))
1080         continue;
1081
1082       label = gtk_action_group_translate_string (action_group, entries[i].label);
1083       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1084
1085       action = gtk_toggle_action_new (entries[i].name,
1086                                       label,
1087                                       tooltip,
1088                                       NULL);
1089
1090       if (entries[i].stock_id) 
1091         {
1092           if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1093             g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1094           else
1095             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1096         }
1097
1098       gtk_toggle_action_set_active (action, entries[i].is_active);
1099
1100       if (entries[i].callback)
1101         {
1102           GClosure *closure;
1103
1104           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1105           g_closure_add_finalize_notifier (closure, shared_data, 
1106                                            (GClosureNotify)shared_data_unref);
1107           shared_data->ref_count++;
1108
1109           g_signal_connect_closure (action, "activate", closure, FALSE);
1110         }
1111           
1112       gtk_action_group_add_action_with_accel (action_group, 
1113                                               GTK_ACTION (action),
1114                                               entries[i].accelerator);
1115       g_object_unref (action);
1116     }
1117
1118   shared_data_unref (shared_data);
1119 }
1120
1121 /**
1122  * gtk_action_group_add_radio_actions:
1123  * @action_group: the action group
1124  * @entries: an array of radio action descriptions
1125  * @n_entries: the number of entries
1126  * @value: the value of the action to activate initially, or -1 if
1127  *   no action should be activated
1128  * @on_change: the callback to connect to the changed signal
1129  * @user_data: data to pass to the action callbacks
1130  * 
1131  * This is a convenience routine to create a group of radio actions and
1132  * add them to the action group. 
1133  *
1134  * The "changed" signal of the first radio action is connected to the 
1135  * @on_change callback and the accel paths of the actions are set to 
1136  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
1137  * 
1138  * Since: 2.4
1139  **/
1140 void            
1141 gtk_action_group_add_radio_actions (GtkActionGroup            *action_group,
1142                                     const GtkRadioActionEntry *entries,
1143                                     guint                      n_entries,
1144                                     gint                       value,
1145                                     GCallback                  on_change,
1146                                     gpointer                   user_data)
1147 {
1148   gtk_action_group_add_radio_actions_full (action_group, 
1149                                            entries, n_entries, 
1150                                            value,
1151                                            on_change, user_data, NULL);
1152 }
1153
1154 /**
1155  * gtk_action_group_add_radio_actions_full:
1156  * @action_group: the action group
1157  * @entries: an array of radio action descriptions
1158  * @n_entries: the number of entries
1159  * @value: the value of the action to activate initially, or -1 if
1160  *   no action should be activated
1161  * @on_change: the callback to connect to the changed signal
1162  * @user_data: data to pass to the action callbacks
1163  * @destroy: destroy notification callback for @user_data
1164  *
1165  * This variant of gtk_action_group_add_radio_actions() adds a 
1166  * #GDestroyNotify callback for @user_data. 
1167  * 
1168  * Since: 2.4
1169  **/
1170 void            
1171 gtk_action_group_add_radio_actions_full (GtkActionGroup            *action_group,
1172                                          const GtkRadioActionEntry *entries,
1173                                          guint                      n_entries,
1174                                          gint                       value,
1175                                          GCallback                  on_change,
1176                                          gpointer                   user_data,
1177                                          GDestroyNotify             destroy)
1178 {
1179   /* Keep this in sync with the other 
1180    * gtk_action_group_add_..._actions_full() functions.
1181    */
1182   guint i;
1183   GSList *group = NULL;
1184   GtkRadioAction *first_action = NULL;
1185
1186   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1187
1188   for (i = 0; i < n_entries; i++)
1189     {
1190       GtkRadioAction *action;
1191       const gchar *label;
1192       const gchar *tooltip; 
1193
1194       if (!check_unique_action (action_group, entries[i].name))
1195         continue;
1196
1197       label = gtk_action_group_translate_string (action_group, entries[i].label);
1198       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1199
1200       action = gtk_radio_action_new (entries[i].name,
1201                                      label,
1202                                      tooltip,
1203                                      NULL,
1204                                      entries[i].value);
1205
1206       if (entries[i].stock_id) 
1207         {
1208           if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1209             g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1210           else
1211             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1212         }
1213
1214       if (i == 0) 
1215         first_action = action;
1216
1217       gtk_radio_action_set_group (action, group);
1218       group = gtk_radio_action_get_group (action);
1219
1220       if (value == entries[i].value)
1221         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1222
1223       gtk_action_group_add_action_with_accel (action_group, 
1224                                               GTK_ACTION (action),
1225                                               entries[i].accelerator);
1226       g_object_unref (action);
1227     }
1228
1229   if (on_change && first_action)
1230     g_signal_connect_data (first_action, "changed",
1231                            on_change, user_data, 
1232                            (GClosureNotify)destroy, 0);
1233 }
1234
1235 /**
1236  * gtk_action_group_set_translate_func:
1237  * @action_group: a #GtkActionGroup
1238  * @func: a #GtkTranslateFunc
1239  * @data: data to be passed to @func and @notify
1240  * @notify: a #GtkDestroyNotify function to be called when @action_group is 
1241  *   destroyed and when the translation function is changed again
1242  * 
1243  * Sets a function to be used for translating the @label and @tooltip of 
1244  * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1245  *
1246  * If you're using gettext(), it is enough to set the translation domain
1247  * with gtk_action_group_set_translation_domain().
1248  *
1249  * Since: 2.4 
1250  **/
1251 void
1252 gtk_action_group_set_translate_func (GtkActionGroup   *action_group,
1253                                      GtkTranslateFunc  func,
1254                                      gpointer          data,
1255                                      GtkDestroyNotify  notify)
1256 {
1257   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1258   
1259   if (action_group->private_data->translate_notify)
1260     action_group->private_data->translate_notify (action_group->private_data->translate_data);
1261       
1262   action_group->private_data->translate_func = func;
1263   action_group->private_data->translate_data = data;
1264   action_group->private_data->translate_notify = notify;
1265 }
1266
1267 static gchar *
1268 dgettext_swapped (const gchar *msgid, 
1269                   const gchar *domainname)
1270 {
1271   /* Pass through dgettext if and only if msgid is nonempty. */
1272   if (msgid && *msgid) 
1273     return dgettext (domainname, msgid); 
1274   else
1275     return (gchar*) msgid;
1276 }
1277
1278 /**
1279  * gtk_action_group_set_translation_domain:
1280  * @action_group: a #GtkActionGroup
1281  * @domain: the translation domain to use for dgettext() calls
1282  * 
1283  * Sets the translation domain and uses dgettext() for translating the 
1284  * @label and @tooltip of #GtkActionEntry<!-- -->s added by 
1285  * gtk_action_group_add_actions().
1286  *
1287  * If you're not using gettext() for localization, see 
1288  * gtk_action_group_set_translate_func().
1289  *
1290  * Since: 2.4
1291  **/
1292 void 
1293 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1294                                          const gchar    *domain)
1295 {
1296   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1297
1298   gtk_action_group_set_translate_func (action_group, 
1299                                        (GtkTranslateFunc)dgettext_swapped,
1300                                        g_strdup (domain),
1301                                        g_free);
1302
1303
1304
1305 /**
1306  * gtk_action_group_translate_string:
1307  * @action_group: a #GtkActionGroup
1308  * @string: a string
1309  *
1310  * Translates a string using the specified translate_func(). This
1311  * is mainly intended for language bindings.
1312  *
1313  * Returns: the translation of @string
1314  *
1315  * Since: 2.6
1316  **/
1317 G_CONST_RETURN gchar *
1318 gtk_action_group_translate_string (GtkActionGroup *action_group,
1319                                    const gchar    *string)
1320 {
1321   GtkTranslateFunc translate_func;
1322   gpointer translate_data;
1323   
1324   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1325   
1326   if (string == NULL)
1327     return NULL;
1328
1329   translate_func = action_group->private_data->translate_func;
1330   translate_data = action_group->private_data->translate_data;
1331   
1332   if (translate_func)
1333     return translate_func (string, translate_data);
1334   else
1335     return string;
1336 }
1337
1338 /* Protected for use by GtkAction */
1339 void
1340 _gtk_action_group_emit_connect_proxy  (GtkActionGroup *action_group,
1341                                        GtkAction      *action,
1342                                        GtkWidget      *proxy)
1343 {
1344   g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0, 
1345                  action, proxy);
1346 }
1347
1348 void
1349 _gtk_action_group_emit_disconnect_proxy  (GtkActionGroup *action_group,
1350                                           GtkAction      *action,
1351                                           GtkWidget      *proxy)
1352 {
1353   g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0, 
1354                  action, proxy);
1355 }
1356
1357 void
1358 _gtk_action_group_emit_pre_activate  (GtkActionGroup *action_group,
1359                                       GtkAction      *action)
1360 {
1361   g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1362 }
1363
1364 void
1365 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1366                                       GtkAction      *action)
1367 {
1368   g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);
1369 }
1370
1371 #define __GTK_ACTION_GROUP_C__
1372 #include "gtkaliasdef.c"