]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~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_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 (radio_button != NULL);
164   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
165   g_return_if_fail (!g_slist_find (group, radio_button));
166
167   if (radio_button->group)
168     {
169       GSList *slist;
170       
171       radio_button->group = g_slist_remove (radio_button->group, radio_button);
172       
173       for (slist = radio_button->group; slist; slist = slist->next)
174         {
175           GtkRadioButton *tmp_button;
176           
177           tmp_button = slist->data;
178           
179           tmp_button->group = radio_button->group;
180         }
181     }
182   
183   radio_button->group = g_slist_prepend (group, radio_button);
184   
185   if (group)
186     {
187       GSList *slist;
188       
189       for (slist = group; slist; slist = slist->next)
190         {
191           GtkRadioButton *tmp_button;
192           
193           tmp_button = slist->data;
194           
195           tmp_button->group = radio_button->group;
196         }
197     }
198
199   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
200 }
201
202 GtkWidget*
203 gtk_radio_button_new (GSList *group)
204 {
205   GtkRadioButton *radio_button;
206
207   radio_button = gtk_type_new (gtk_radio_button_get_type ());
208
209   if (group)
210     gtk_radio_button_set_group (radio_button, group);
211
212   return GTK_WIDGET (radio_button);
213 }
214
215 GtkWidget*
216 gtk_radio_button_new_with_label (GSList      *group,
217                                  const gchar *label)
218 {
219   GtkWidget *radio_button;
220   GtkWidget *label_widget;
221
222   radio_button = gtk_radio_button_new (group);
223   label_widget = gtk_label_new (label);
224   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
225
226   gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
227   gtk_widget_show (label_widget);
228
229   return radio_button;
230 }
231
232 GtkWidget*
233 gtk_radio_button_new_from_widget (GtkRadioButton *group)
234 {
235   GSList *l = NULL;
236   if (group)
237     l = gtk_radio_button_group (group);
238   return gtk_radio_button_new (l);
239 }
240
241
242 GtkWidget*
243 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
244                                              const gchar    *label)
245 {
246   GSList *l = NULL;
247   if (group)
248     l = gtk_radio_button_group (group);
249   return gtk_radio_button_new_with_label (l, label);
250 }
251
252 GSList*
253 gtk_radio_button_group (GtkRadioButton *radio_button)
254 {
255   g_return_val_if_fail (radio_button != NULL, NULL);
256   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
257
258   return radio_button->group;
259 }
260
261
262 static void
263 gtk_radio_button_destroy (GtkObject *object)
264 {
265   GtkRadioButton *radio_button;
266   GtkRadioButton *tmp_button;
267   GSList *tmp_list;
268
269   g_return_if_fail (object != NULL);
270   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
271
272   radio_button = GTK_RADIO_BUTTON (object);
273
274   radio_button->group = g_slist_remove (radio_button->group, radio_button);
275   tmp_list = radio_button->group;
276
277   while (tmp_list)
278     {
279       tmp_button = tmp_list->data;
280       tmp_list = tmp_list->next;
281
282       tmp_button->group = radio_button->group;
283     }
284
285   if (GTK_OBJECT_CLASS (parent_class)->destroy)
286     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
287 }
288
289 static void
290 gtk_radio_button_clicked (GtkButton *button)
291 {
292   GtkToggleButton *toggle_button;
293   GtkRadioButton *radio_button;
294   GtkToggleButton *tmp_button;
295   GtkStateType new_state;
296   GSList *tmp_list;
297   gint toggled;
298
299   g_return_if_fail (button != NULL);
300   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
301
302   radio_button = GTK_RADIO_BUTTON (button);
303   toggle_button = GTK_TOGGLE_BUTTON (button);
304   toggled = FALSE;
305
306   gtk_widget_ref (GTK_WIDGET (button));
307
308   if (toggle_button->active)
309     {
310       tmp_button = NULL;
311       tmp_list = radio_button->group;
312
313       while (tmp_list)
314         {
315           tmp_button = tmp_list->data;
316           tmp_list = tmp_list->next;
317
318           if (tmp_button->active && tmp_button != toggle_button)
319             break;
320
321           tmp_button = NULL;
322         }
323
324       if (!tmp_button)
325         {
326           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
327         }
328       else
329         {
330           toggled = TRUE;
331           toggle_button->active = !toggle_button->active;
332           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
333         }
334     }
335   else
336     {
337       toggled = TRUE;
338       toggle_button->active = !toggle_button->active;
339
340       tmp_list = radio_button->group;
341       while (tmp_list)
342         {
343           tmp_button = tmp_list->data;
344           tmp_list = tmp_list->next;
345
346           if (tmp_button->active && (tmp_button != toggle_button))
347             {
348               gtk_button_clicked (GTK_BUTTON (tmp_button));
349               break;
350             }
351         }
352
353       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
354     }
355
356   if (GTK_WIDGET_STATE (button) != new_state)
357     gtk_widget_set_state (GTK_WIDGET (button), new_state);
358
359   if (toggled)
360     gtk_toggle_button_toggled (toggle_button);
361
362   gtk_widget_queue_draw (GTK_WIDGET (button));
363
364   gtk_widget_unref (GTK_WIDGET (button));
365 }
366
367 static void
368 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
369                                  GdkRectangle   *area)
370 {
371   GtkWidget *widget;
372   GtkButton *button;
373   GtkToggleButton *toggle_button;
374   GtkStateType state_type;
375   GtkShadowType shadow_type;
376   GdkRectangle restrict_area;
377   GdkRectangle new_area;
378   gint width, height;
379   gint x, y;
380
381   g_return_if_fail (check_button != NULL);
382   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
383
384   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
385     {
386       widget = GTK_WIDGET (check_button);
387       button = GTK_BUTTON (check_button);
388       toggle_button = GTK_TOGGLE_BUTTON (check_button);
389
390       state_type = GTK_WIDGET_STATE (widget);
391       if ((state_type != GTK_STATE_NORMAL) &&
392           (state_type != GTK_STATE_PRELIGHT))
393         state_type = GTK_STATE_NORMAL;
394
395       restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
396       restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
397       restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
398       restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
399
400       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
401         {
402            if (state_type != GTK_STATE_NORMAL)
403              gtk_paint_flat_box(widget->style, widget->window, state_type, 
404                                 GTK_SHADOW_ETCHED_OUT,
405                                 area, widget, "radiobutton",
406                                 new_area.x, new_area.y,
407                                 new_area.width, new_area.height);
408         }
409       
410       x = widget->allocation.x + GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
411       y = widget->allocation.y + (widget->allocation.height - GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size) / 2;
412       width = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
413       height = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
414       
415       if (GTK_TOGGLE_BUTTON (widget)->active)
416         shadow_type = GTK_SHADOW_IN;
417       else
418         shadow_type = GTK_SHADOW_OUT;
419       
420       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
421         x = widget->allocation.x + widget->allocation.width - (width + x - widget->allocation.x);
422
423       gtk_paint_option (widget->style, widget->window,
424                         GTK_WIDGET_STATE (widget), shadow_type,
425                         area, widget, "radiobutton",
426                         x, y, width, height);
427     }
428 }