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