]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
take value_spacing into account for GTK_POS_LEFT.
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtklabel.h"
20 #include "gtkradiobutton.h"
21 #include "gtksignal.h"
22
23
24 enum {
25   ARG_0,
26   ARG_GROUP
27 };
28
29 #define CHECK_BUTTON_CLASS(w)  GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
30
31
32 static void gtk_radio_button_class_init     (GtkRadioButtonClass  *klass);
33 static void gtk_radio_button_init           (GtkRadioButton       *radio_button);
34 static void gtk_radio_button_destroy        (GtkObject            *object);
35 static void gtk_radio_button_clicked        (GtkButton            *button);
36 static void gtk_radio_button_draw_indicator (GtkCheckButton       *check_button,
37                                              GdkRectangle         *area);
38 static void gtk_radio_button_set_arg        (GtkObject            *object,
39                                              GtkArg               *arg,
40                                              guint                 arg_id);
41 static void gtk_radio_button_get_arg        (GtkObject            *object,
42                                              GtkArg               *arg,
43                                              guint                 arg_id);
44
45
46 static GtkCheckButtonClass *parent_class = NULL;
47
48
49 GtkType
50 gtk_radio_button_get_type (void)
51 {
52   static GtkType radio_button_type = 0;
53
54   if (!radio_button_type)
55     {
56       static const GtkTypeInfo radio_button_info =
57       {
58         "GtkRadioButton",
59         sizeof (GtkRadioButton),
60         sizeof (GtkRadioButtonClass),
61         (GtkClassInitFunc) gtk_radio_button_class_init,
62         (GtkObjectInitFunc) gtk_radio_button_init,
63         /* reserved_1 */ NULL,
64         /* reserved_2 */ NULL,
65         (GtkClassInitFunc) NULL,
66       };
67
68       radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
69     }
70
71   return radio_button_type;
72 }
73
74 static void
75 gtk_radio_button_class_init (GtkRadioButtonClass *class)
76 {
77   GtkObjectClass *object_class;
78   GtkButtonClass *button_class;
79   GtkCheckButtonClass *check_button_class;
80
81   object_class = (GtkObjectClass*) class;
82   button_class = (GtkButtonClass*) class;
83   check_button_class = (GtkCheckButtonClass*) class;
84
85   parent_class = gtk_type_class (gtk_check_button_get_type ());
86
87   gtk_object_add_arg_type ("GtkRadioButton::group", GTK_TYPE_RADIO_BUTTON, GTK_ARG_WRITABLE, ARG_GROUP);
88
89   object_class->set_arg = gtk_radio_button_set_arg;
90   object_class->get_arg = gtk_radio_button_get_arg;
91   object_class->destroy = gtk_radio_button_destroy;
92
93   button_class->clicked = gtk_radio_button_clicked;
94
95   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
96 }
97
98 static void
99 gtk_radio_button_init (GtkRadioButton *radio_button)
100 {
101   GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
102
103   GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
104
105   radio_button->group = g_slist_prepend (NULL, radio_button);
106
107   gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
108 }
109
110 static void
111 gtk_radio_button_set_arg (GtkObject      *object,
112                           GtkArg         *arg,
113                           guint           arg_id)
114 {
115   GtkRadioButton *radio_button;
116
117   radio_button = GTK_RADIO_BUTTON (object);
118
119   switch (arg_id)
120     {
121       GSList *slist;
122
123     case ARG_GROUP:
124       if (GTK_VALUE_OBJECT (*arg))
125         slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
126       else
127         slist = NULL;
128       gtk_radio_button_set_group (radio_button, slist);
129       break;
130     default:
131       break;
132     }
133 }
134
135 static void
136 gtk_radio_button_get_arg (GtkObject      *object,
137                           GtkArg         *arg,
138                           guint           arg_id)
139 {
140   GtkRadioButton *radio_button;
141
142   radio_button = GTK_RADIO_BUTTON (object);
143
144   switch (arg_id)
145     {
146     default:
147       arg->type = GTK_TYPE_INVALID;
148       break;
149     }
150 }
151
152 void
153 gtk_radio_button_set_group (GtkRadioButton *radio_button,
154                             GSList         *group)
155 {
156   g_return_if_fail (radio_button != NULL);
157   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
158   g_return_if_fail (!g_slist_find (group, radio_button));
159
160   if (radio_button->group)
161     {
162       GSList *slist;
163       
164       radio_button->group = g_slist_remove (radio_button->group, radio_button);
165       
166       for (slist = radio_button->group; slist; slist = slist->next)
167         {
168           GtkRadioButton *tmp_button;
169           
170           tmp_button = slist->data;
171           
172           tmp_button->group = radio_button->group;
173         }
174     }
175   
176   radio_button->group = g_slist_prepend (group, radio_button);
177   
178   if (group)
179     {
180       GSList *slist;
181       
182       for (slist = group; slist; slist = slist->next)
183         {
184           GtkRadioButton *tmp_button;
185           
186           tmp_button = slist->data;
187           
188           tmp_button->group = radio_button->group;
189         }
190     }
191
192   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
193 }
194
195 GtkWidget*
196 gtk_radio_button_new (GSList *group)
197 {
198   GtkRadioButton *radio_button;
199
200   radio_button = gtk_type_new (gtk_radio_button_get_type ());
201
202   if (group)
203     gtk_radio_button_set_group (radio_button, group);
204
205   return GTK_WIDGET (radio_button);
206 }
207
208 GtkWidget*
209 gtk_radio_button_new_with_label (GSList      *group,
210                                  const gchar *label)
211 {
212   GtkWidget *radio_button;
213   GtkWidget *label_widget;
214
215   radio_button = gtk_radio_button_new (group);
216   label_widget = gtk_label_new (label);
217   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
218
219   gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
220   gtk_widget_show (label_widget);
221
222   return radio_button;
223 }
224
225 GtkWidget*
226 gtk_radio_button_new_from_widget (GtkRadioButton *group)
227 {
228   GSList *l = NULL;
229   if (group)
230     l = gtk_radio_button_group (group);
231   return gtk_radio_button_new (l);
232 }
233
234
235 GtkWidget*
236 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
237                                              const gchar    *label)
238 {
239   GSList *l = NULL;
240   if (group)
241     l = gtk_radio_button_group (group);
242   return gtk_radio_button_new_with_label (l, label);
243 }
244
245 GSList*
246 gtk_radio_button_group (GtkRadioButton *radio_button)
247 {
248   g_return_val_if_fail (radio_button != NULL, NULL);
249   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
250
251   return radio_button->group;
252 }
253
254
255 static void
256 gtk_radio_button_destroy (GtkObject *object)
257 {
258   GtkRadioButton *radio_button;
259   GtkRadioButton *tmp_button;
260   GSList *tmp_list;
261
262   g_return_if_fail (object != NULL);
263   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
264
265   radio_button = GTK_RADIO_BUTTON (object);
266
267   radio_button->group = g_slist_remove (radio_button->group, radio_button);
268   tmp_list = radio_button->group;
269
270   while (tmp_list)
271     {
272       tmp_button = tmp_list->data;
273       tmp_list = tmp_list->next;
274
275       tmp_button->group = radio_button->group;
276     }
277
278   if (GTK_OBJECT_CLASS (parent_class)->destroy)
279     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
280 }
281
282 static void
283 gtk_radio_button_clicked (GtkButton *button)
284 {
285   GtkToggleButton *toggle_button;
286   GtkRadioButton *radio_button;
287   GtkToggleButton *tmp_button;
288   GtkStateType new_state;
289   GSList *tmp_list;
290   gint toggled;
291
292   g_return_if_fail (button != NULL);
293   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
294
295   radio_button = GTK_RADIO_BUTTON (button);
296   toggle_button = GTK_TOGGLE_BUTTON (button);
297   toggled = FALSE;
298
299   gtk_widget_ref (GTK_WIDGET (button));
300
301   if (toggle_button->active)
302     {
303       tmp_button = NULL;
304       tmp_list = radio_button->group;
305
306       while (tmp_list)
307         {
308           tmp_button = tmp_list->data;
309           tmp_list = tmp_list->next;
310
311           if (tmp_button->active && tmp_button != toggle_button)
312             break;
313
314           tmp_button = NULL;
315         }
316
317       if (!tmp_button)
318         {
319           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
320         }
321       else
322         {
323           toggled = TRUE;
324           toggle_button->active = !toggle_button->active;
325           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
326         }
327     }
328   else
329     {
330       toggled = TRUE;
331       toggle_button->active = !toggle_button->active;
332
333       tmp_list = radio_button->group;
334       while (tmp_list)
335         {
336           tmp_button = tmp_list->data;
337           tmp_list = tmp_list->next;
338
339           if (tmp_button->active && (tmp_button != toggle_button))
340             {
341               gtk_button_clicked (GTK_BUTTON (tmp_button));
342               break;
343             }
344         }
345
346       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
347     }
348
349   if (GTK_WIDGET_STATE (button) != new_state)
350     gtk_widget_set_state (GTK_WIDGET (button), new_state);
351
352   if (toggled)
353     gtk_toggle_button_toggled (toggle_button);
354
355   gtk_widget_queue_draw (GTK_WIDGET (button));
356
357   gtk_widget_unref (GTK_WIDGET (button));
358 }
359
360 static void
361 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
362                                  GdkRectangle   *area)
363 {
364   GtkWidget *widget;
365   GtkButton *button;
366   GtkToggleButton *toggle_button;
367   GtkStateType state_type;
368   GtkShadowType shadow_type;
369   GdkRectangle restrict_area;
370   GdkRectangle new_area;
371   gint width, height;
372   gint x, y;
373
374   g_return_if_fail (check_button != NULL);
375   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
376
377   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
378     {
379       widget = GTK_WIDGET (check_button);
380       button = GTK_BUTTON (check_button);
381       toggle_button = GTK_TOGGLE_BUTTON (check_button);
382
383       state_type = GTK_WIDGET_STATE (widget);
384       if ((state_type != GTK_STATE_NORMAL) &&
385           (state_type != GTK_STATE_PRELIGHT))
386         state_type = GTK_STATE_NORMAL;
387
388       restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
389       restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
390       restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
391       restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
392
393       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
394         {
395            if (state_type != GTK_STATE_NORMAL)
396              gtk_paint_flat_box(widget->style, widget->window, state_type, 
397                                 GTK_SHADOW_ETCHED_OUT,
398                                 area, widget, "radiobutton",
399                                 new_area.x, new_area.y,
400                                 new_area.width, new_area.height);
401         }
402       
403       x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
404       y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
405       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
406       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
407       
408       if (GTK_TOGGLE_BUTTON (widget)->active)
409         shadow_type = GTK_SHADOW_IN;
410       else
411         shadow_type = GTK_SHADOW_OUT;
412       
413       gtk_paint_option (widget->style, widget->window,
414                         GTK_WIDGET_STATE (widget), shadow_type,
415                         area, widget, "radiobutton",
416                         x, y, width, height);
417     }
418 }