]> Pileus Git - ~andy/gtk/blob - gtk/gtkactiongroup.c
26243f31975917fcbe646e2a0b500715b3a4a85c
[~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       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 static gboolean
539 check_unique_action (GtkActionGroup *action_group,
540                      const gchar    *action_name)
541 {
542   if (gtk_action_group_get_action (action_group, action_name) != NULL)
543     {
544       g_warning ("Refusing to add non-unique action '%s' to action group '%s'",
545                  action_name,
546                  action_group->private_data->name);
547       return FALSE;
548     }
549
550   return TRUE;
551 }
552
553 /**
554  * gtk_action_group_add_action:
555  * @action_group: the action group
556  * @action: an action
557  *
558  * Adds an action object to the action group. Note that this function
559  * does not set up the accel path of the action, which can lead to problems
560  * if a user tries to modify the accelerator of a menuitem associated with
561  * the action. Therefore you must either set the accel path yourself with
562  * gtk_action_set_accel_path(), or use 
563  * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
564  *
565  * Since: 2.4
566  */
567 void
568 gtk_action_group_add_action (GtkActionGroup *action_group,
569                              GtkAction      *action)
570 {
571   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
572   g_return_if_fail (GTK_IS_ACTION (action));
573   g_return_if_fail (gtk_action_get_name (action) != NULL);
574   
575   if (!check_unique_action (action_group, gtk_action_get_name (action)))
576     return;
577
578   g_hash_table_insert (action_group->private_data->actions, 
579                        g_strdup (gtk_action_get_name (action)),
580                        g_object_ref (action));
581   g_object_set (action, I_("action-group"), action_group, NULL);
582 }
583
584 /**
585  * gtk_action_group_add_action_with_accel:
586  * @action_group: the action group 
587  * @action: the action to add 
588  * @accelerator: the accelerator for the action, in
589  *   the format understood by gtk_accelerator_parse(), or "" for no accelerator, or 
590  *   %NULL to use the stock accelerator 
591  *
592  * Adds an action object to the action group and sets up the accelerator.
593  *
594  * If @accelerator is %NULL, attempts to use the accelerator associated 
595  * with the stock_id of the action. 
596  *
597  * Accel paths are set to
598  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
599  *
600  * Since: 2.4
601  */
602 void
603 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
604                                         GtkAction      *action,
605                                         const gchar    *accelerator)
606 {
607   gchar *accel_path;
608   guint  accel_key = 0;
609   GdkModifierType accel_mods;
610   GtkStockItem stock_item;
611   gchar *name;
612   gchar *stock_id;
613
614   if (!check_unique_action (action_group, gtk_action_get_name (action)))
615     return;
616   
617   g_object_get (action, "name", &name, "stock-id", &stock_id, NULL);
618
619   accel_path = g_strconcat ("<Actions>/",
620                             action_group->private_data->name, "/", name, NULL);
621
622   if (accelerator)
623     {
624       if (accelerator[0] == 0) 
625         accel_key = 0;
626       else
627         {
628           gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
629           if (accel_key == 0)
630             g_warning ("Unable to parse accelerator '%s' for action '%s'",
631                        accelerator, name);
632         }
633     }
634   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
635     {
636       accel_key = stock_item.keyval;
637       accel_mods = stock_item.modifier;
638     }
639
640   if (accel_key)
641     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
642
643   gtk_action_set_accel_path (action, accel_path);
644   gtk_action_group_add_action (action_group, action);
645
646   g_free (accel_path);
647   g_free (stock_id);
648   g_free (name);
649 }
650
651 /**
652  * gtk_action_group_remove_action:
653  * @action_group: the action group
654  * @action: an action
655  *
656  * Removes an action object from the action group.
657  *
658  * Since: 2.4
659  */
660 void
661 gtk_action_group_remove_action (GtkActionGroup *action_group,
662                                 GtkAction      *action)
663 {
664   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
665   g_return_if_fail (GTK_IS_ACTION (action));
666   g_return_if_fail (gtk_action_get_name (action) != NULL);
667
668   /* extra protection to make sure action->name is valid */
669   g_object_ref (action);
670   g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
671   g_object_unref (action);
672 }
673
674 static void
675 add_single_action (gpointer key, 
676                    gpointer value, 
677                    gpointer user_data)
678 {
679   GList **list = user_data;
680
681   *list = g_list_prepend (*list, value);
682 }
683
684 /**
685  * gtk_action_group_list_actions:
686  * @action_group: the action group
687  *
688  * Lists the actions in the action group.
689  *
690  * Returns: an allocated list of the action objects in the action group
691  * 
692  * Since: 2.4
693  */
694 GList *
695 gtk_action_group_list_actions (GtkActionGroup *action_group)
696 {
697   GList *actions = NULL;
698   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
699   
700   g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
701
702   return g_list_reverse (actions);
703 }
704
705
706 /**
707  * gtk_action_group_add_actions:
708  * @action_group: the action group
709  * @entries: an array of action descriptions
710  * @n_entries: the number of entries
711  * @user_data: data to pass to the action callbacks
712  *
713  * This is a convenience function to create a number of actions and add them 
714  * to the action group.
715  *
716  * The "activate" signals of the actions are connected to the callbacks and 
717  * their accel paths are set to 
718  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
719  * 
720  * Since: 2.4
721  */
722 void
723 gtk_action_group_add_actions (GtkActionGroup       *action_group,
724                               const GtkActionEntry *entries,
725                               guint                 n_entries,
726                               gpointer              user_data)
727 {
728   gtk_action_group_add_actions_full (action_group, 
729                                      entries, n_entries, 
730                                      user_data, NULL);
731 }
732
733 typedef struct _SharedData  SharedData;
734
735 struct _SharedData {
736   guint          ref_count;
737   gpointer       data;
738   GDestroyNotify destroy;
739 };
740
741 static void
742 shared_data_unref (gpointer data)
743 {
744   SharedData *shared_data = (SharedData *)data;
745
746   shared_data->ref_count--;
747   if (shared_data->ref_count == 0)
748     {
749       if (shared_data->destroy) 
750         (*shared_data->destroy) (shared_data->data);
751       
752       g_slice_free (SharedData, shared_data);
753     }
754 }
755
756
757 /**
758  * gtk_action_group_add_actions_full:
759  * @action_group: the action group
760  * @entries: an array of action descriptions
761  * @n_entries: the number of entries
762  * @user_data: data to pass to the action callbacks
763  * @destroy: destroy notification callback for @user_data
764  *
765  * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
766  * callback for @user_data. 
767  * 
768  * Since: 2.4
769  */
770 void
771 gtk_action_group_add_actions_full (GtkActionGroup       *action_group,
772                                    const GtkActionEntry *entries,
773                                    guint                 n_entries,
774                                    gpointer              user_data,
775                                    GDestroyNotify        destroy)
776 {
777
778   /* Keep this in sync with the other 
779    * gtk_action_group_add_..._actions_full() functions.
780    */
781   guint i;
782   SharedData *shared_data;
783
784   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
785
786   shared_data = g_slice_new0 (SharedData);
787   shared_data->ref_count = 1;
788   shared_data->data = user_data;
789   shared_data->destroy = destroy;
790
791   for (i = 0; i < n_entries; i++)
792     {
793       GtkAction *action;
794       const gchar *label;
795       const gchar *tooltip;
796
797       if (!check_unique_action (action_group, entries[i].name))
798         continue;
799
800       label = gtk_action_group_translate_string (action_group, entries[i].label);
801       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
802
803       action = gtk_action_new (entries[i].name,
804                                label,
805                                tooltip,
806                                NULL);
807
808       if (entries[i].stock_id) 
809         {
810           g_object_set (action, "stock-id", entries[i].stock_id, NULL);
811           if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), 
812                                        entries[i].stock_id))
813             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
814         }
815           
816       if (entries[i].callback)
817         {
818           GClosure *closure;
819
820           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
821           g_closure_add_finalize_notifier (closure, shared_data, 
822                                            (GClosureNotify)shared_data_unref);
823           shared_data->ref_count++;
824
825           g_signal_connect_closure (action, "activate", closure, FALSE);
826         }
827           
828       gtk_action_group_add_action_with_accel (action_group, 
829                                               action,
830                                               entries[i].accelerator);
831       g_object_unref (action);
832     }
833
834   shared_data_unref (shared_data);
835 }
836
837 /**
838  * gtk_action_group_add_toggle_actions:
839  * @action_group: the action group
840  * @entries: an array of toggle action descriptions
841  * @n_entries: the number of entries
842  * @user_data: data to pass to the action callbacks
843  *
844  * This is a convenience function to create a number of toggle actions and add them 
845  * to the action group.
846  *
847  * The "activate" signals of the actions are connected to the callbacks and 
848  * their accel paths are set to 
849  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
850  * 
851  * Since: 2.4
852  */
853 void
854 gtk_action_group_add_toggle_actions (GtkActionGroup             *action_group,
855                                      const GtkToggleActionEntry *entries,
856                                      guint                       n_entries,
857                                      gpointer                    user_data)
858 {
859   gtk_action_group_add_toggle_actions_full (action_group, 
860                                             entries, n_entries, 
861                                             user_data, NULL);
862 }
863
864
865 /**
866  * gtk_action_group_add_toggle_actions_full:
867  * @action_group: the action group
868  * @entries: an array of toggle action descriptions
869  * @n_entries: the number of entries
870  * @user_data: data to pass to the action callbacks
871  * @destroy: destroy notification callback for @user_data
872  *
873  * This variant of gtk_action_group_add_toggle_actions() adds a 
874  * #GDestroyNotify callback for @user_data. 
875  * 
876  * Since: 2.4
877  */
878 void
879 gtk_action_group_add_toggle_actions_full (GtkActionGroup             *action_group,
880                                           const GtkToggleActionEntry *entries,
881                                           guint                       n_entries,
882                                           gpointer                    user_data,
883                                           GDestroyNotify              destroy)
884 {
885   /* Keep this in sync with the other 
886    * gtk_action_group_add_..._actions_full() functions.
887    */
888   guint i;
889   SharedData *shared_data;
890
891   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
892
893   shared_data = g_slice_new0 (SharedData);
894   shared_data->ref_count = 1;
895   shared_data->data = user_data;
896   shared_data->destroy = destroy;
897
898   for (i = 0; i < n_entries; i++)
899     {
900       GtkToggleAction *action;
901       const gchar *label;
902       const gchar *tooltip;
903
904       if (!check_unique_action (action_group, entries[i].name))
905         continue;
906
907       label = gtk_action_group_translate_string (action_group, entries[i].label);
908       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
909
910       action = gtk_toggle_action_new (entries[i].name,
911                                       label,
912                                       tooltip,
913                                       NULL);
914
915       if (entries[i].stock_id) 
916         {
917           if (gtk_icon_factory_lookup_default (entries[i].stock_id))
918             g_object_set (action, "stock-id", entries[i].stock_id, NULL);
919           else
920             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
921         }
922
923       gtk_toggle_action_set_active (action, entries[i].is_active);
924
925       if (entries[i].callback)
926         {
927           GClosure *closure;
928
929           closure = g_cclosure_new (entries[i].callback, user_data, NULL);
930           g_closure_add_finalize_notifier (closure, shared_data, 
931                                            (GClosureNotify)shared_data_unref);
932           shared_data->ref_count++;
933
934           g_signal_connect_closure (action, "activate", closure, FALSE);
935         }
936           
937       gtk_action_group_add_action_with_accel (action_group, 
938                                               GTK_ACTION (action),
939                                               entries[i].accelerator);
940       g_object_unref (action);
941     }
942
943   shared_data_unref (shared_data);
944 }
945
946 /**
947  * gtk_action_group_add_radio_actions:
948  * @action_group: the action group
949  * @entries: an array of radio action descriptions
950  * @n_entries: the number of entries
951  * @value: the value of the action to activate initially, or -1 if
952  *   no action should be activated
953  * @on_change: the callback to connect to the changed signal
954  * @user_data: data to pass to the action callbacks
955  * 
956  * This is a convenience routine to create a group of radio actions and
957  * add them to the action group. 
958  *
959  * The "changed" signal of the first radio action is connected to the 
960  * @on_change callback and the accel paths of the actions are set to 
961  * <literal>&lt;Actions&gt;/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.  
962  * 
963  * Since: 2.4
964  **/
965 void            
966 gtk_action_group_add_radio_actions (GtkActionGroup            *action_group,
967                                     const GtkRadioActionEntry *entries,
968                                     guint                      n_entries,
969                                     gint                       value,
970                                     GCallback                  on_change,
971                                     gpointer                   user_data)
972 {
973   gtk_action_group_add_radio_actions_full (action_group, 
974                                            entries, n_entries, 
975                                            value,
976                                            on_change, user_data, NULL);
977 }
978
979 /**
980  * gtk_action_group_add_radio_actions_full:
981  * @action_group: the action group
982  * @entries: an array of radio action descriptions
983  * @n_entries: the number of entries
984  * @value: the value of the action to activate initially, or -1 if
985  *   no action should be activated
986  * @on_change: the callback to connect to the changed signal
987  * @user_data: data to pass to the action callbacks
988  * @destroy: destroy notification callback for @user_data
989  *
990  * This variant of gtk_action_group_add_radio_actions() adds a 
991  * #GDestroyNotify callback for @user_data. 
992  * 
993  * Since: 2.4
994  **/
995 void            
996 gtk_action_group_add_radio_actions_full (GtkActionGroup            *action_group,
997                                          const GtkRadioActionEntry *entries,
998                                          guint                      n_entries,
999                                          gint                       value,
1000                                          GCallback                  on_change,
1001                                          gpointer                   user_data,
1002                                          GDestroyNotify             destroy)
1003 {
1004   /* Keep this in sync with the other 
1005    * gtk_action_group_add_..._actions_full() functions.
1006    */
1007   guint i;
1008   GSList *group = NULL;
1009   GtkRadioAction *first_action = NULL;
1010
1011   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1012
1013   for (i = 0; i < n_entries; i++)
1014     {
1015       GtkRadioAction *action;
1016       const gchar *label;
1017       const gchar *tooltip; 
1018
1019       if (!check_unique_action (action_group, entries[i].name))
1020         continue;
1021
1022       label = gtk_action_group_translate_string (action_group, entries[i].label);
1023       tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1024
1025       action = gtk_radio_action_new (entries[i].name,
1026                                      label,
1027                                      tooltip,
1028                                      NULL,
1029                                      entries[i].value);
1030
1031       if (entries[i].stock_id) 
1032         {
1033           if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1034             g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1035           else
1036             g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1037         }
1038
1039       if (i == 0) 
1040         first_action = action;
1041
1042       gtk_radio_action_set_group (action, group);
1043       group = gtk_radio_action_get_group (action);
1044
1045       if (value == entries[i].value)
1046         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1047
1048       gtk_action_group_add_action_with_accel (action_group, 
1049                                               GTK_ACTION (action),
1050                                               entries[i].accelerator);
1051       g_object_unref (action);
1052     }
1053
1054   if (on_change && first_action)
1055     g_signal_connect_data (first_action, "changed",
1056                            on_change, user_data, 
1057                            (GClosureNotify)destroy, 0);
1058 }
1059
1060 /**
1061  * gtk_action_group_set_translate_func:
1062  * @action_group: a #GtkActionGroup
1063  * @func: a #GtkTranslateFunc
1064  * @data: data to be passed to @func and @notify
1065  * @notify: a #GtkDestroyNotify function to be called when @action_group is 
1066  *   destroyed and when the translation function is changed again
1067  * 
1068  * Sets a function to be used for translating the @label and @tooltip of 
1069  * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1070  *
1071  * If you're using gettext(), it is enough to set the translation domain
1072  * with gtk_action_group_set_translation_domain().
1073  *
1074  * Since: 2.4 
1075  **/
1076 void
1077 gtk_action_group_set_translate_func (GtkActionGroup   *action_group,
1078                                      GtkTranslateFunc  func,
1079                                      gpointer          data,
1080                                      GtkDestroyNotify  notify)
1081 {
1082   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1083   
1084   if (action_group->private_data->translate_notify)
1085     action_group->private_data->translate_notify (action_group->private_data->translate_data);
1086       
1087   action_group->private_data->translate_func = func;
1088   action_group->private_data->translate_data = data;
1089   action_group->private_data->translate_notify = notify;
1090 }
1091
1092 static gchar *
1093 dgettext_swapped (const gchar *msgid, 
1094                   const gchar *domainname)
1095 {
1096   /* Pass through dgettext if and only if msgid is nonempty. */
1097   if (msgid && *msgid) 
1098     return dgettext (domainname, msgid); 
1099   else
1100     return (gchar*) msgid;
1101 }
1102
1103 /**
1104  * gtk_action_group_set_translation_domain:
1105  * @action_group: a #GtkActionGroup
1106  * @domain: the translation domain to use for dgettext() calls
1107  * 
1108  * Sets the translation domain and uses dgettext() for translating the 
1109  * @label and @tooltip of #GtkActionEntry<!-- -->s added by 
1110  * gtk_action_group_add_actions().
1111  *
1112  * If you're not using gettext() for localization, see 
1113  * gtk_action_group_set_translate_func().
1114  *
1115  * Since: 2.4
1116  **/
1117 void 
1118 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1119                                          const gchar    *domain)
1120 {
1121   g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1122
1123   gtk_action_group_set_translate_func (action_group, 
1124                                        (GtkTranslateFunc)dgettext_swapped,
1125                                        g_strdup (domain),
1126                                        g_free);
1127
1128
1129
1130 /**
1131  * gtk_action_group_translate_string:
1132  * @action_group: a #GtkActionGroup
1133  * @string: a string
1134  *
1135  * Translates a string using the specified translate_func(). This
1136  * is mainly intended for language bindings.
1137  *
1138  * Returns: the translation of @string
1139  *
1140  * Since: 2.6
1141  **/
1142 G_CONST_RETURN gchar *
1143 gtk_action_group_translate_string (GtkActionGroup *action_group,
1144                                    const gchar    *string)
1145 {
1146   GtkTranslateFunc translate_func;
1147   gpointer translate_data;
1148   
1149   g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1150   
1151   if (string == NULL)
1152     return NULL;
1153
1154   translate_func = action_group->private_data->translate_func;
1155   translate_data = action_group->private_data->translate_data;
1156   
1157   if (translate_func)
1158     return translate_func (string, translate_data);
1159   else
1160     return string;
1161 }
1162
1163 /* Protected for use by GtkAction */
1164 void
1165 _gtk_action_group_emit_connect_proxy  (GtkActionGroup *action_group,
1166                                        GtkAction      *action,
1167                                        GtkWidget      *proxy)
1168 {
1169   g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0, 
1170                  action, proxy);
1171 }
1172
1173 void
1174 _gtk_action_group_emit_disconnect_proxy  (GtkActionGroup *action_group,
1175                                           GtkAction      *action,
1176                                           GtkWidget      *proxy)
1177 {
1178   g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0, 
1179                  action, proxy);
1180 }
1181
1182 void
1183 _gtk_action_group_emit_pre_activate  (GtkActionGroup *action_group,
1184                                       GtkAction      *action)
1185 {
1186   g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1187 }
1188
1189 void
1190 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1191                                       GtkAction      *action)
1192 {
1193   g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);
1194 }
1195
1196 #define __GTK_ACTION_GROUP_C__
1197 #include "gtkaliasdef.c"