]> Pileus Git - ~andy/gtk/blob - gtk/gtkradioaction.c
Intern some more strings.
[~andy/gtk] / gtk / gtkradioaction.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 "gtkradioaction.h"
34 #include "gtkradiomenuitem.h"
35 #include "gtktoggleactionprivate.h"
36 #include "gtktoggletoolbutton.h"
37 #include "gtkintl.h"
38 #include "gtkprivate.h"
39 #include "gtkalias.h"
40
41 #define GTK_RADIO_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RADIO_ACTION, GtkRadioActionPrivate))
42
43 struct _GtkRadioActionPrivate 
44 {
45   GSList *group;
46   gint    value;
47 };
48
49 enum 
50 {
51   CHANGED,
52   LAST_SIGNAL
53 };
54
55 enum 
56 {
57   PROP_0,
58   PROP_VALUE,
59   PROP_GROUP
60 };
61
62 static void gtk_radio_action_init         (GtkRadioAction *action);
63 static void gtk_radio_action_class_init   (GtkRadioActionClass *class);
64 static void gtk_radio_action_finalize     (GObject *object);
65 static void gtk_radio_action_set_property (GObject         *object,
66                                            guint            prop_id,
67                                            const GValue    *value,
68                                            GParamSpec      *pspec);
69 static void gtk_radio_action_get_property (GObject         *object,
70                                            guint            prop_id,
71                                            GValue          *value,
72                                            GParamSpec      *pspec);
73 static void gtk_radio_action_activate     (GtkAction *action);
74 static GtkWidget *create_menu_item        (GtkAction *action);
75
76
77 GType
78 gtk_radio_action_get_type (void)
79 {
80   static GtkType type = 0;
81
82   if (!type)
83     {
84       static const GTypeInfo type_info =
85       {
86         sizeof (GtkRadioActionClass),
87         (GBaseInitFunc) NULL,
88         (GBaseFinalizeFunc) NULL,
89         (GClassInitFunc) gtk_radio_action_class_init,
90         (GClassFinalizeFunc) NULL,
91         NULL,
92         
93         sizeof (GtkRadioAction),
94         0, /* n_preallocs */
95         (GInstanceInitFunc) gtk_radio_action_init,
96       };
97
98       type = g_type_register_static (GTK_TYPE_TOGGLE_ACTION,
99                                      I_("GtkRadioAction"),
100                                      &type_info, 0);
101     }
102   return type;
103 }
104
105 static GObjectClass *parent_class = NULL;
106 static guint         radio_action_signals[LAST_SIGNAL] = { 0 };
107
108 static void
109 gtk_radio_action_class_init (GtkRadioActionClass *klass)
110 {
111   GObjectClass *gobject_class;
112   GtkActionClass *action_class;
113
114   parent_class = g_type_class_peek_parent (klass);
115   gobject_class = G_OBJECT_CLASS (klass);
116   action_class = GTK_ACTION_CLASS (klass);
117
118   gobject_class->finalize = gtk_radio_action_finalize;
119   gobject_class->set_property = gtk_radio_action_set_property;
120   gobject_class->get_property = gtk_radio_action_get_property;
121
122   action_class->activate = gtk_radio_action_activate;
123
124   action_class->create_menu_item = create_menu_item;
125
126   /**
127    * GtkRadioAction:value:
128    *
129    * The value is an arbitrary integer which can be used as a
130    * convenient way to determine which action in the group is 
131    * currently active in an ::activate or ::changed signal handler.
132    * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
133    * for convenient ways to get and set this property.
134    *
135    * Since: 2.4
136    */
137   g_object_class_install_property (gobject_class,
138                                    PROP_VALUE,
139                                    g_param_spec_int ("value",
140                                                      P_("The value"),
141                                                      P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
142                                                      G_MININT,
143                                                      G_MAXINT,
144                                                      0,
145                                                      GTK_PARAM_READWRITE));
146
147   /**
148    * GtkRadioAction:group:
149    *
150    * Sets a new group for a radio action.
151    *
152    * Since: 2.4
153    */
154   g_object_class_install_property (gobject_class,
155                                    PROP_GROUP,
156                                    g_param_spec_object ("group",
157                                                         P_("Group"),
158                                                         P_("The radio action whose group this action belongs to."),
159                                                         GTK_TYPE_RADIO_ACTION,
160                                                         GTK_PARAM_WRITABLE));
161
162   /**
163    * GtkRadioAction::changed:
164    * @action: the action on which the signal is emitted
165    * @current: the member of @action<!-- -->s group which has just been activated
166    *
167    * The ::changed signal is emitted on every member of a radio group when the
168    * active member is changed. The signal gets emitted after the ::activate signals
169    * for the previous and current active members.
170    *
171    * Since: 2.4
172    */
173   radio_action_signals[CHANGED] =
174     g_signal_new (I_("changed"),
175                   G_OBJECT_CLASS_TYPE (klass),
176                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
177                   G_STRUCT_OFFSET (GtkRadioActionClass, changed),  NULL, NULL,
178                   g_cclosure_marshal_VOID__OBJECT,
179                   G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
180
181   g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
182 }
183
184 static void
185 gtk_radio_action_init (GtkRadioAction *action)
186 {
187   action->private_data = GTK_RADIO_ACTION_GET_PRIVATE (action);
188   action->private_data->group = g_slist_prepend (NULL, action);
189   action->private_data->value = 0;
190 }
191
192 /**
193  * gtk_radio_action_new:
194  * @name: A unique name for the action
195  * @label: The label displayed in menu items and on buttons
196  * @tooltip: A tooltip for this action
197  * @stock_id: The stock icon to display in widgets representing this action
198  * @value: The value which gtk_radio_action_get_current_value() should return
199  *    if this action is selected.
200  *
201  * Creates a new #GtkRadioAction object. To add the action to
202  * a #GtkActionGroup and set the accelerator for the action,
203  * call gtk_action_group_add_action_with_accel().
204  *
205  * Return value: a new #GtkRadioAction
206  *
207  * Since: 2.4
208  */
209 GtkRadioAction *
210 gtk_radio_action_new (const gchar *name,
211                       const gchar *label,
212                       const gchar *tooltip,
213                       const gchar *stock_id,
214                       gint value)
215 {
216   GtkRadioAction *action;
217
218   action = g_object_new (GTK_TYPE_RADIO_ACTION,
219                          "name", name,
220                          "label", label,
221                          "tooltip", tooltip,
222                          "stock-id", stock_id,
223                          "value", value,
224                          NULL);
225
226   return action;
227 }
228
229 static void
230 gtk_radio_action_finalize (GObject *object)
231 {
232   GtkRadioAction *action;
233   GSList *tmp_list;
234
235   action = GTK_RADIO_ACTION (object);
236
237   action->private_data->group = g_slist_remove (action->private_data->group, action);
238
239   tmp_list = action->private_data->group;
240
241   while (tmp_list)
242     {
243       GtkRadioAction *tmp_action = tmp_list->data;
244
245       tmp_list = tmp_list->next;
246       tmp_action->private_data->group = action->private_data->group;
247     }
248
249   (* parent_class->finalize) (object);
250 }
251
252 static void
253 gtk_radio_action_set_property (GObject         *object,
254                                guint            prop_id,
255                                const GValue    *value,
256                                GParamSpec      *pspec)
257 {
258   GtkRadioAction *radio_action;
259   
260   radio_action = GTK_RADIO_ACTION (object);
261
262   switch (prop_id)
263     {
264     case PROP_VALUE:
265       radio_action->private_data->value = g_value_get_int (value);
266       break;
267     case PROP_GROUP: 
268       {
269         GtkRadioAction *arg;
270         GSList *slist = NULL;
271         
272         if (G_VALUE_HOLDS_OBJECT (value)) 
273           {
274             arg = GTK_RADIO_ACTION (g_value_get_object (value));
275             if (arg)
276               slist = gtk_radio_action_get_group (arg);
277             gtk_radio_action_set_group (radio_action, slist);
278           }
279       }
280       break;
281     default:
282       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283       break;
284     }
285 }
286
287 static void
288 gtk_radio_action_get_property (GObject    *object,
289                                guint       prop_id,
290                                GValue     *value,
291                                GParamSpec *pspec)
292 {
293   GtkRadioAction *radio_action;
294
295   radio_action = GTK_RADIO_ACTION (object);
296
297   switch (prop_id)
298     {
299     case PROP_VALUE:
300       g_value_set_int (value, radio_action->private_data->value);
301       break;
302     default:
303       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304       break;
305     }
306 }
307
308 static void
309 gtk_radio_action_activate (GtkAction *action)
310 {
311   GtkRadioAction *radio_action;
312   GtkToggleAction *toggle_action;
313   GtkToggleAction *tmp_action;
314   GSList *tmp_list;
315
316   radio_action = GTK_RADIO_ACTION (action);
317   toggle_action = GTK_TOGGLE_ACTION (action);
318
319   if (toggle_action->private_data->active)
320     {
321       tmp_list = radio_action->private_data->group;
322
323       while (tmp_list)
324         {
325           tmp_action = tmp_list->data;
326           tmp_list = tmp_list->next;
327
328           if (tmp_action->private_data->active && (tmp_action != toggle_action)) 
329             {
330               toggle_action->private_data->active = !toggle_action->private_data->active;
331               break;
332             }
333         }
334     }
335   else
336     {
337       toggle_action->private_data->active = !toggle_action->private_data->active;
338
339       tmp_list = radio_action->private_data->group;
340       while (tmp_list)
341         {
342           tmp_action = tmp_list->data;
343           tmp_list = tmp_list->next;
344
345           if (tmp_action->private_data->active && (tmp_action != toggle_action))
346             {
347               _gtk_action_emit_activate (GTK_ACTION (tmp_action));
348               break;
349             }
350         }
351
352       tmp_list = radio_action->private_data->group;
353       while (tmp_list)
354         {
355           tmp_action = tmp_list->data;
356           tmp_list = tmp_list->next;
357           
358           g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
359         }
360     }
361
362   gtk_toggle_action_toggled (toggle_action);
363 }
364
365 static GtkWidget *
366 create_menu_item (GtkAction *action)
367 {
368   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
369                        "draw-as-radio", TRUE,
370                        NULL);
371 }
372
373 /**
374  * gtk_radio_action_get_group:
375  * @action: the action object
376  *
377  * Returns the list representing the radio group for this object.
378  * Note that the returned list is only valid until the next change
379  * to the group. 
380  *
381  * A common way to set up a group of radio group is the following:
382  * <informalexample><programlisting>
383  *   GSList *group = NULL;
384  *   GtkRadioAction *action;
385  *  
386  *   while (/<!-- -->* more actions to add *<!-- -->/)
387  *     {
388  *        action = gtk_radio_action_new (...);
389  *        
390  *        gtk_radio_action_set_group (action, group);
391  *        group = gtk_radio_action_get_group (action);
392  *     }
393  * </programlisting></informalexample>
394  *
395  * Returns: the list representing the radio group for this object
396  *
397  * Since: 2.4
398  */
399 GSList *
400 gtk_radio_action_get_group (GtkRadioAction *action)
401 {
402   g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
403
404   return action->private_data->group;
405 }
406
407 /**
408  * gtk_radio_action_set_group:
409  * @action: the action object
410  * @group: a list representing a radio group
411  *
412  * Sets the radio group for the radio action object.
413  *
414  * Since: 2.4
415  */
416 void
417 gtk_radio_action_set_group (GtkRadioAction *action, 
418                             GSList         *group)
419 {
420   g_return_if_fail (GTK_IS_RADIO_ACTION (action));
421   g_return_if_fail (!g_slist_find (group, action));
422
423   if (action->private_data->group)
424     {
425       GSList *slist;
426
427       action->private_data->group = g_slist_remove (action->private_data->group, action);
428
429       for (slist = action->private_data->group; slist; slist = slist->next)
430         {
431           GtkRadioAction *tmp_action = slist->data;
432
433           tmp_action->private_data->group = action->private_data->group;
434         }
435     }
436
437   action->private_data->group = g_slist_prepend (group, action);
438
439   if (group)
440     {
441       GSList *slist;
442
443       for (slist = action->private_data->group; slist; slist = slist->next)
444         {
445           GtkRadioAction *tmp_action = slist->data;
446
447           tmp_action->private_data->group = action->private_data->group;
448         }
449     }
450   else
451     {
452       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
453     }
454 }
455
456 /**
457  * gtk_radio_action_get_current_value:
458  * @action: a #GtkRadioAction
459  * 
460  * Obtains the value property of the currently active member of 
461  * the group to which @action belongs.
462  * 
463  * Return value: The value of the currently active group member
464  *
465  * Since: 2.4
466  **/
467 gint
468 gtk_radio_action_get_current_value (GtkRadioAction *action)
469 {
470   GSList *slist;
471
472   g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
473
474   if (action->private_data->group)
475     {
476       for (slist = action->private_data->group; slist; slist = slist->next)
477         {
478           GtkToggleAction *toggle_action = slist->data;
479
480           if (toggle_action->private_data->active)
481             return GTK_RADIO_ACTION (toggle_action)->private_data->value;
482         }
483     }
484
485   return action->private_data->value;
486 }
487
488 #define __GTK_RADIO_ACTION_C__
489 #include "gtkaliasdef.c"