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