]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
fix silly cut'n'paste error, with_label and with_mnemonic should create
[~andy/gtk] / gtk / gtkradiobutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtklabel.h"
28 #include "gtkradiobutton.h"
29 #include "gtksignal.h"
30
31
32 enum {
33   ARG_0,
34   ARG_GROUP
35 };
36
37
38 static void gtk_radio_button_class_init     (GtkRadioButtonClass  *klass);
39 static void gtk_radio_button_init           (GtkRadioButton       *radio_button);
40 static void gtk_radio_button_destroy        (GtkObject            *object);
41 static void gtk_radio_button_clicked        (GtkButton            *button);
42 static void gtk_radio_button_draw_indicator (GtkCheckButton       *check_button,
43                                              GdkRectangle         *area);
44 static void gtk_radio_button_set_arg        (GtkObject            *object,
45                                              GtkArg               *arg,
46                                              guint                 arg_id);
47 static void gtk_radio_button_get_arg        (GtkObject            *object,
48                                              GtkArg               *arg,
49                                              guint                 arg_id);
50
51
52 static GtkCheckButtonClass *parent_class = NULL;
53
54
55 GtkType
56 gtk_radio_button_get_type (void)
57 {
58   static GtkType radio_button_type = 0;
59
60   if (!radio_button_type)
61     {
62       static const GtkTypeInfo radio_button_info =
63       {
64         "GtkRadioButton",
65         sizeof (GtkRadioButton),
66         sizeof (GtkRadioButtonClass),
67         (GtkClassInitFunc) gtk_radio_button_class_init,
68         (GtkObjectInitFunc) gtk_radio_button_init,
69         /* reserved_1 */ NULL,
70         /* reserved_2 */ NULL,
71         (GtkClassInitFunc) NULL,
72       };
73
74       radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
75     }
76
77   return radio_button_type;
78 }
79
80 static void
81 gtk_radio_button_class_init (GtkRadioButtonClass *class)
82 {
83   GtkObjectClass *object_class;
84   GtkButtonClass *button_class;
85   GtkCheckButtonClass *check_button_class;
86
87   object_class = (GtkObjectClass*) class;
88   button_class = (GtkButtonClass*) class;
89   check_button_class = (GtkCheckButtonClass*) class;
90
91   parent_class = gtk_type_class (gtk_check_button_get_type ());
92
93   gtk_object_add_arg_type ("GtkRadioButton::group", GTK_TYPE_RADIO_BUTTON, GTK_ARG_WRITABLE, ARG_GROUP);
94
95   object_class->set_arg = gtk_radio_button_set_arg;
96   object_class->get_arg = gtk_radio_button_get_arg;
97   object_class->destroy = gtk_radio_button_destroy;
98
99   button_class->clicked = gtk_radio_button_clicked;
100
101   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
102 }
103
104 static void
105 gtk_radio_button_init (GtkRadioButton *radio_button)
106 {
107   GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
108   GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
109
110   GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
111
112   radio_button->group = g_slist_prepend (NULL, radio_button);
113
114   gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
115 }
116
117 static void
118 gtk_radio_button_set_arg (GtkObject      *object,
119                           GtkArg         *arg,
120                           guint           arg_id)
121 {
122   GtkRadioButton *radio_button;
123
124   radio_button = GTK_RADIO_BUTTON (object);
125
126   switch (arg_id)
127     {
128       GSList *slist;
129
130     case ARG_GROUP:
131       if (GTK_VALUE_OBJECT (*arg))
132         slist = gtk_radio_button_get_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
133       else
134         slist = NULL;
135       gtk_radio_button_set_group (radio_button, slist);
136       break;
137     default:
138       break;
139     }
140 }
141
142 static void
143 gtk_radio_button_get_arg (GtkObject      *object,
144                           GtkArg         *arg,
145                           guint           arg_id)
146 {
147   GtkRadioButton *radio_button;
148
149   radio_button = GTK_RADIO_BUTTON (object);
150
151   switch (arg_id)
152     {
153     default:
154       arg->type = GTK_TYPE_INVALID;
155       break;
156     }
157 }
158
159 void
160 gtk_radio_button_set_group (GtkRadioButton *radio_button,
161                             GSList         *group)
162 {
163   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
164   g_return_if_fail (!g_slist_find (group, radio_button));
165
166   if (radio_button->group)
167     {
168       GSList *slist;
169       
170       radio_button->group = g_slist_remove (radio_button->group, radio_button);
171       
172       for (slist = radio_button->group; slist; slist = slist->next)
173         {
174           GtkRadioButton *tmp_button;
175           
176           tmp_button = slist->data;
177           
178           tmp_button->group = radio_button->group;
179         }
180     }
181   
182   radio_button->group = g_slist_prepend (group, radio_button);
183   
184   if (group)
185     {
186       GSList *slist;
187       
188       for (slist = group; slist; slist = slist->next)
189         {
190           GtkRadioButton *tmp_button;
191           
192           tmp_button = slist->data;
193           
194           tmp_button->group = radio_button->group;
195         }
196     }
197
198   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
199 }
200
201 GtkWidget*
202 gtk_radio_button_new (GSList *group)
203 {
204   GtkRadioButton *radio_button;
205
206   radio_button = gtk_type_new (gtk_radio_button_get_type ());
207
208   if (group)
209     gtk_radio_button_set_group (radio_button, group);
210
211   return GTK_WIDGET (radio_button);
212 }
213
214 GtkWidget*
215 gtk_radio_button_new_with_label (GSList      *group,
216                                  const gchar *label)
217 {
218   GtkWidget *radio_button;
219
220   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
221
222   if (group)
223     gtk_radio_button_set_group (radio_button, group);
224
225   return radio_button;
226 }
227
228
229 /**
230  * gtk_radio_button_new_with_mnemonic:
231  * @group: the radio button group
232  * @label: the text of the button, with an underscore in front of the
233  *         mnemonic character
234  * @returns: a new #GtkRadioButton
235  *
236  * Creates a new #GtkRadioButton containing a label. The label
237  * will be created using gtk_label_new_with_mnemonic(), so underscores
238  * in @label indicate the mnemonic for the button.
239  **/
240 GtkWidget*
241 gtk_radio_button_new_with_mnemonic (GSList      *group,
242                                     const gchar *label)
243 {
244   GtkWidget *radio_button;
245
246   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, "use_underline", TRUE, NULL);
247
248   if (group)
249     gtk_radio_button_set_group (radio_button, group);
250
251   return radio_button;
252 }
253
254 GtkWidget*
255 gtk_radio_button_new_from_widget (GtkRadioButton *group)
256 {
257   GSList *l = NULL;
258   if (group)
259     l = gtk_radio_button_get_group (group);
260   return gtk_radio_button_new (l);
261 }
262
263
264 GtkWidget*
265 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
266                                              const gchar    *label)
267 {
268   GSList *l = NULL;
269   if (group)
270     l = gtk_radio_button_get_group (group);
271   return gtk_radio_button_new_with_label (l, label);
272 }
273
274 /**
275  * gtk_radio_button_new_with_mnemonic_from_widget:
276  * @group: widget to get radio group from
277  * @label: the text of the button, with an underscore in front of the
278  *         mnemonic character
279  * @returns: a new #GtkRadioButton
280  *
281  * Creates a new #GtkRadioButton containing a label. The label
282  * will be created using gtk_label_new_with_mnemonic(), so underscores
283  * in @label indicate the mnemonic for the button.
284  **/
285 GtkWidget*
286 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
287                                                 const gchar    *label)
288 {
289   GSList *l = NULL;
290   if (group)
291     l = gtk_radio_button_get_group (group);
292   return gtk_radio_button_new_with_mnemonic (l, label);
293 }
294
295 GSList*
296 gtk_radio_button_get_group (GtkRadioButton *radio_button)
297 {
298   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
299
300   return radio_button->group;
301 }
302
303
304 static void
305 gtk_radio_button_destroy (GtkObject *object)
306 {
307   GtkRadioButton *radio_button;
308   GtkRadioButton *tmp_button;
309   GSList *tmp_list;
310
311   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
312
313   radio_button = GTK_RADIO_BUTTON (object);
314
315   radio_button->group = g_slist_remove (radio_button->group, radio_button);
316   tmp_list = radio_button->group;
317
318   while (tmp_list)
319     {
320       tmp_button = tmp_list->data;
321       tmp_list = tmp_list->next;
322
323       tmp_button->group = radio_button->group;
324     }
325
326   if (GTK_OBJECT_CLASS (parent_class)->destroy)
327     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
328 }
329
330 static void
331 gtk_radio_button_clicked (GtkButton *button)
332 {
333   GtkToggleButton *toggle_button;
334   GtkRadioButton *radio_button;
335   GtkToggleButton *tmp_button;
336   GtkStateType new_state;
337   GSList *tmp_list;
338   gint toggled;
339
340   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
341
342   radio_button = GTK_RADIO_BUTTON (button);
343   toggle_button = GTK_TOGGLE_BUTTON (button);
344   toggled = FALSE;
345
346   gtk_widget_ref (GTK_WIDGET (button));
347
348   if (toggle_button->active)
349     {
350       tmp_button = NULL;
351       tmp_list = radio_button->group;
352
353       while (tmp_list)
354         {
355           tmp_button = tmp_list->data;
356           tmp_list = tmp_list->next;
357
358           if (tmp_button->active && tmp_button != toggle_button)
359             break;
360
361           tmp_button = NULL;
362         }
363
364       if (!tmp_button)
365         {
366           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
367         }
368       else
369         {
370           toggled = TRUE;
371           toggle_button->active = !toggle_button->active;
372           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
373         }
374     }
375   else
376     {
377       toggled = TRUE;
378       toggle_button->active = !toggle_button->active;
379
380       tmp_list = radio_button->group;
381       while (tmp_list)
382         {
383           tmp_button = tmp_list->data;
384           tmp_list = tmp_list->next;
385
386           if (tmp_button->active && (tmp_button != toggle_button))
387             {
388               gtk_button_clicked (GTK_BUTTON (tmp_button));
389               break;
390             }
391         }
392
393       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
394     }
395
396   if (GTK_WIDGET_STATE (button) != new_state)
397     gtk_widget_set_state (GTK_WIDGET (button), new_state);
398
399   if (toggled)
400     gtk_toggle_button_toggled (toggle_button);
401
402   gtk_widget_queue_draw (GTK_WIDGET (button));
403
404   gtk_widget_unref (GTK_WIDGET (button));
405 }
406
407 static void
408 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
409                                  GdkRectangle   *area)
410 {
411   GtkWidget *widget;
412   GtkButton *button;
413   GtkToggleButton *toggle_button;
414   GtkStateType state_type;
415   GtkShadowType shadow_type;
416   GdkRectangle restrict_area;
417   GdkRectangle new_area;
418   gint x, y;
419   gint indicator_size, indicator_spacing;
420
421   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
422
423   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
424     {
425       widget = GTK_WIDGET (check_button);
426       button = GTK_BUTTON (check_button);
427       toggle_button = GTK_TOGGLE_BUTTON (check_button);
428
429       state_type = GTK_WIDGET_STATE (widget);
430       if ((state_type != GTK_STATE_NORMAL) &&
431           (state_type != GTK_STATE_PRELIGHT))
432         state_type = GTK_STATE_NORMAL;
433
434       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
435
436       restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
437       restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
438       restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
439       restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
440
441       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
442         {
443            if (state_type != GTK_STATE_NORMAL)
444              gtk_paint_flat_box(widget->style, widget->window, state_type, 
445                                 GTK_SHADOW_ETCHED_OUT,
446                                 area, widget, "radiobutton",
447                                 new_area.x, new_area.y,
448                                 new_area.width, new_area.height);
449         }
450       
451       x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
452       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
453       
454       if (GTK_TOGGLE_BUTTON (widget)->active)
455         shadow_type = GTK_SHADOW_IN;
456       else
457         shadow_type = GTK_SHADOW_OUT;
458
459       if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
460         shadow_type = GTK_SHADOW_ETCHED_IN;
461       
462       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
463         x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
464
465       gtk_paint_option (widget->style, widget->window,
466                         GTK_WIDGET_STATE (widget), shadow_type,
467                         area, widget, "radiobutton",
468                         x, y, indicator_size, indicator_size);
469     }
470 }