]> Pileus Git - ~andy/gtk/blob - gtk/gtkradioaction.c
Include "config.h" instead of <config.h> Command used: find -name
[~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, or %NULL
185  * @tooltip: A tooltip for this action, or %NULL
186  * @stock_id: The stock icon to display in widgets representing this
187  *   action, or %NULL
188  * @value: The value which gtk_radio_action_get_current_value() should
189  *   return if this action is selected.
190  *
191  * Creates a new #GtkRadioAction object. To add the action to
192  * a #GtkActionGroup and set the accelerator for the action,
193  * call gtk_action_group_add_action_with_accel().
194  *
195  * Return value: a new #GtkRadioAction
196  *
197  * Since: 2.4
198  */
199 GtkRadioAction *
200 gtk_radio_action_new (const gchar *name,
201                       const gchar *label,
202                       const gchar *tooltip,
203                       const gchar *stock_id,
204                       gint value)
205 {
206   g_return_val_if_fail (name != NULL, NULL);
207
208   return g_object_new (GTK_TYPE_RADIO_ACTION,
209                        "name", name,
210                        "label", label,
211                        "tooltip", tooltip,
212                        "stock-id", stock_id,
213                        "value", value,
214                        NULL);
215 }
216
217 static void
218 gtk_radio_action_finalize (GObject *object)
219 {
220   GtkRadioAction *action;
221   GSList *tmp_list;
222
223   action = GTK_RADIO_ACTION (object);
224
225   action->private_data->group = g_slist_remove (action->private_data->group, action);
226
227   tmp_list = action->private_data->group;
228
229   while (tmp_list)
230     {
231       GtkRadioAction *tmp_action = tmp_list->data;
232
233       tmp_list = tmp_list->next;
234       tmp_action->private_data->group = action->private_data->group;
235     }
236
237   G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
238 }
239
240 static void
241 gtk_radio_action_set_property (GObject         *object,
242                                guint            prop_id,
243                                const GValue    *value,
244                                GParamSpec      *pspec)
245 {
246   GtkRadioAction *radio_action;
247   
248   radio_action = GTK_RADIO_ACTION (object);
249
250   switch (prop_id)
251     {
252     case PROP_VALUE:
253       radio_action->private_data->value = g_value_get_int (value);
254       break;
255     case PROP_GROUP: 
256       {
257         GtkRadioAction *arg;
258         GSList *slist = NULL;
259         
260         if (G_VALUE_HOLDS_OBJECT (value)) 
261           {
262             arg = GTK_RADIO_ACTION (g_value_get_object (value));
263             if (arg)
264               slist = gtk_radio_action_get_group (arg);
265             gtk_radio_action_set_group (radio_action, slist);
266           }
267       }
268       break;
269     case PROP_CURRENT_VALUE:
270       gtk_radio_action_set_current_value (radio_action,
271                                           g_value_get_int (value));
272       break;
273     default:
274       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275       break;
276     }
277 }
278
279 static void
280 gtk_radio_action_get_property (GObject    *object,
281                                guint       prop_id,
282                                GValue     *value,
283                                GParamSpec *pspec)
284 {
285   GtkRadioAction *radio_action;
286
287   radio_action = GTK_RADIO_ACTION (object);
288
289   switch (prop_id)
290     {
291     case PROP_VALUE:
292       g_value_set_int (value, radio_action->private_data->value);
293       break;
294     case PROP_CURRENT_VALUE:
295       g_value_set_int (value,
296                        gtk_radio_action_get_current_value (radio_action));
297       break;
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300       break;
301     }
302 }
303
304 static void
305 gtk_radio_action_activate (GtkAction *action)
306 {
307   GtkRadioAction *radio_action;
308   GtkToggleAction *toggle_action;
309   GtkToggleAction *tmp_action;
310   GSList *tmp_list;
311
312   radio_action = GTK_RADIO_ACTION (action);
313   toggle_action = GTK_TOGGLE_ACTION (action);
314
315   if (toggle_action->private_data->active)
316     {
317       tmp_list = radio_action->private_data->group;
318
319       while (tmp_list)
320         {
321           tmp_action = tmp_list->data;
322           tmp_list = tmp_list->next;
323
324           if (tmp_action->private_data->active && (tmp_action != toggle_action)) 
325             {
326               toggle_action->private_data->active = !toggle_action->private_data->active;
327               break;
328             }
329         }
330     }
331   else
332     {
333       toggle_action->private_data->active = !toggle_action->private_data->active;
334
335       tmp_list = radio_action->private_data->group;
336       while (tmp_list)
337         {
338           tmp_action = tmp_list->data;
339           tmp_list = tmp_list->next;
340
341           if (tmp_action->private_data->active && (tmp_action != toggle_action))
342             {
343               _gtk_action_emit_activate (GTK_ACTION (tmp_action));
344               break;
345             }
346         }
347
348       tmp_list = radio_action->private_data->group;
349       while (tmp_list)
350         {
351           tmp_action = tmp_list->data;
352           tmp_list = tmp_list->next;
353           
354           g_object_notify (G_OBJECT (tmp_action), "current-value");
355
356           g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
357         }
358     }
359
360   gtk_toggle_action_toggled (toggle_action);
361 }
362
363 static GtkWidget *
364 create_menu_item (GtkAction *action)
365 {
366   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
367                        "draw-as-radio", TRUE,
368                        NULL);
369 }
370
371 /**
372  * gtk_radio_action_get_group:
373  * @action: the action object
374  *
375  * Returns the list representing the radio group for this object.
376  * Note that the returned list is only valid until the next change
377  * to the group. 
378  *
379  * A common way to set up a group of radio group is the following:
380  * |[
381  *   GSList *group = NULL;
382  *   GtkRadioAction *action;
383  *  
384  *   while (/&ast; more actions to add &ast;/)
385  *     {
386  *        action = gtk_radio_action_new (...);
387  *        
388  *        gtk_radio_action_set_group (action, group);
389  *        group = gtk_radio_action_get_group (action);
390  *     }
391  * ]|
392  *
393  * Returns: the list representing the radio group for this object
394  *
395  * Since: 2.4
396  */
397 GSList *
398 gtk_radio_action_get_group (GtkRadioAction *action)
399 {
400   g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
401
402   return action->private_data->group;
403 }
404
405 /**
406  * gtk_radio_action_set_group:
407  * @action: the action object
408  * @group: a list representing a radio group
409  *
410  * Sets the radio group for the radio action object.
411  *
412  * Since: 2.4
413  */
414 void
415 gtk_radio_action_set_group (GtkRadioAction *action, 
416                             GSList         *group)
417 {
418   g_return_if_fail (GTK_IS_RADIO_ACTION (action));
419   g_return_if_fail (!g_slist_find (group, action));
420
421   if (action->private_data->group)
422     {
423       GSList *slist;
424
425       action->private_data->group = g_slist_remove (action->private_data->group, action);
426
427       for (slist = action->private_data->group; slist; slist = slist->next)
428         {
429           GtkRadioAction *tmp_action = slist->data;
430
431           tmp_action->private_data->group = action->private_data->group;
432         }
433     }
434
435   action->private_data->group = g_slist_prepend (group, action);
436
437   if (group)
438     {
439       GSList *slist;
440
441       for (slist = action->private_data->group; slist; slist = slist->next)
442         {
443           GtkRadioAction *tmp_action = slist->data;
444
445           tmp_action->private_data->group = action->private_data->group;
446         }
447     }
448   else
449     {
450       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
451     }
452 }
453
454 /**
455  * gtk_radio_action_get_current_value:
456  * @action: a #GtkRadioAction
457  * 
458  * Obtains the value property of the currently active member of 
459  * the group to which @action belongs.
460  * 
461  * Return value: The value of the currently active group member
462  *
463  * Since: 2.4
464  **/
465 gint
466 gtk_radio_action_get_current_value (GtkRadioAction *action)
467 {
468   GSList *slist;
469
470   g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
471
472   if (action->private_data->group)
473     {
474       for (slist = action->private_data->group; slist; slist = slist->next)
475         {
476           GtkToggleAction *toggle_action = slist->data;
477
478           if (toggle_action->private_data->active)
479             return GTK_RADIO_ACTION (toggle_action)->private_data->value;
480         }
481     }
482
483   return action->private_data->value;
484 }
485
486 /**
487  * gtk_radio_action_set_current_value:
488  * @action: a #GtkRadioAction
489  * @current_value: the new value
490  * 
491  * Sets the currently active group member to the member with value
492  * property @current_value.
493  *
494  * Since: 2.10
495  **/
496 void
497 gtk_radio_action_set_current_value (GtkRadioAction *action,
498                                     gint            current_value)
499 {
500   GSList *slist;
501
502   g_return_if_fail (GTK_IS_RADIO_ACTION (action));
503
504   if (action->private_data->group)
505     {
506       for (slist = action->private_data->group; slist; slist = slist->next)
507         {
508           GtkRadioAction *radio_action = slist->data;
509
510           if (radio_action->private_data->value == current_value)
511             {
512               gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
513                                             TRUE);
514               return;
515             }
516         }
517     }
518
519   if (action->private_data->value == current_value)
520     gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
521   else
522     g_warning ("Radio group does not contain an action with value '%d'",
523                current_value);
524 }
525
526 #define __GTK_RADIO_ACTION_C__
527 #include "gtkaliasdef.c"