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