]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
Merge from themes-2. See the ChangeLog for a somewhat detailed
[~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       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   radio_button->group = g_slist_prepend (NULL, radio_button);
103 }
104
105 static void
106 gtk_radio_button_set_arg (GtkObject      *object,
107                           GtkArg         *arg,
108                           guint           arg_id)
109 {
110   GtkRadioButton *radio_button;
111
112   radio_button = GTK_RADIO_BUTTON (object);
113
114   switch (arg_id)
115     {
116       GSList *slist;
117
118     case ARG_GROUP:
119       if (GTK_VALUE_OBJECT (*arg))
120         slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
121       else
122         slist = NULL;
123       gtk_radio_button_set_group (radio_button, slist);
124       break;
125     default:
126       break;
127     }
128 }
129
130 static void
131 gtk_radio_button_get_arg (GtkObject      *object,
132                           GtkArg         *arg,
133                           guint           arg_id)
134 {
135   GtkRadioButton *radio_button;
136
137   radio_button = GTK_RADIO_BUTTON (object);
138
139   switch (arg_id)
140     {
141     default:
142       arg->type = GTK_TYPE_INVALID;
143       break;
144     }
145 }
146
147 void
148 gtk_radio_button_set_group (GtkRadioButton *radio_button,
149                             GSList *group)
150 {
151   g_return_if_fail (radio_button != NULL);
152   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
153   g_return_if_fail (!g_slist_find (group, radio_button));
154   
155   if (radio_button->group)
156     {
157       GSList *slist;
158       
159       radio_button->group = g_slist_remove (radio_button->group, radio_button);
160       
161       for (slist = radio_button->group; slist; slist = slist->next)
162         {
163           GtkRadioButton *tmp_button;
164           
165           tmp_button = slist->data;
166           
167           tmp_button->group = radio_button->group;
168         }
169     }
170   
171   radio_button->group = g_slist_prepend (group, radio_button);
172   
173   if (group)
174     {
175       GSList *slist;
176       
177       for (slist = group; slist; slist = slist->next)
178         {
179           GtkRadioButton *tmp_button;
180           
181           tmp_button = slist->data;
182           
183           tmp_button->group = radio_button->group;
184         }
185     }
186   else
187     {
188       GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
189       gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
190     }
191 }
192
193 GtkWidget*
194 gtk_radio_button_new (GSList *group)
195 {
196   GtkRadioButton *radio_button;
197
198   radio_button = gtk_type_new (gtk_radio_button_get_type ());
199
200   gtk_radio_button_set_group (radio_button, group);
201
202   return GTK_WIDGET (radio_button);
203 }
204
205 GtkWidget*
206 gtk_radio_button_new_with_label (GSList      *group,
207                                  const gchar *label)
208 {
209   GtkWidget *radio_button;
210   GtkWidget *label_widget;
211
212   radio_button = gtk_radio_button_new (group);
213   label_widget = gtk_label_new (label);
214   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
215
216   gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
217   gtk_widget_show (label_widget);
218
219   return radio_button;
220 }
221
222 GtkWidget*
223 gtk_radio_button_new_from_widget (GtkRadioButton *group)
224 {
225   GSList *l = NULL;
226   if (group)
227     l = gtk_radio_button_group (group);
228   return gtk_radio_button_new (l);
229 }
230
231
232 GtkWidget*
233 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
234                                              const gchar    *label)
235 {
236   GSList *l = NULL;
237   if (group)
238     l = gtk_radio_button_group (group);
239   return gtk_radio_button_new_with_label (l, label);
240 }
241
242 GSList*
243 gtk_radio_button_group (GtkRadioButton *radio_button)
244 {
245   g_return_val_if_fail (radio_button != NULL, NULL);
246   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
247
248   return radio_button->group;
249 }
250
251
252 static void
253 gtk_radio_button_destroy (GtkObject *object)
254 {
255   GtkRadioButton *radio_button;
256   GtkRadioButton *tmp_button;
257   GSList *tmp_list;
258
259   g_return_if_fail (object != NULL);
260   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
261
262   radio_button = GTK_RADIO_BUTTON (object);
263
264   radio_button->group = g_slist_remove (radio_button->group, radio_button);
265   tmp_list = radio_button->group;
266
267   while (tmp_list)
268     {
269       tmp_button = tmp_list->data;
270       tmp_list = tmp_list->next;
271
272       tmp_button->group = radio_button->group;
273     }
274
275   if (GTK_OBJECT_CLASS (parent_class)->destroy)
276     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
277 }
278
279 static void
280 gtk_radio_button_clicked (GtkButton *button)
281 {
282   GtkToggleButton *toggle_button;
283   GtkRadioButton *radio_button;
284   GtkToggleButton *tmp_button;
285   GtkStateType new_state;
286   GSList *tmp_list;
287   gint toggled;
288
289   g_return_if_fail (button != NULL);
290   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
291
292   radio_button = GTK_RADIO_BUTTON (button);
293   toggle_button = GTK_TOGGLE_BUTTON (button);
294   toggled = FALSE;
295
296   if (toggle_button->active)
297     {
298       tmp_button = NULL;
299       tmp_list = radio_button->group;
300
301       while (tmp_list)
302         {
303           tmp_button = tmp_list->data;
304           tmp_list = tmp_list->next;
305
306           if (tmp_button->active && (tmp_button != toggle_button))
307             break;
308
309           tmp_button = NULL;
310         }
311
312       if (!tmp_button)
313         {
314           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
315         }
316       else
317         {
318           toggled = TRUE;
319           toggle_button->active = !toggle_button->active;
320           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
321         }
322     }
323   else
324     {
325       toggled = TRUE;
326       toggle_button->active = !toggle_button->active;
327
328       tmp_list = radio_button->group;
329       while (tmp_list)
330         {
331           tmp_button = tmp_list->data;
332           tmp_list = tmp_list->next;
333
334           if (tmp_button->active && (tmp_button != toggle_button))
335             {
336               gtk_button_clicked (GTK_BUTTON (tmp_button));
337               break;
338             }
339         }
340
341       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
342     }
343
344   if (GTK_WIDGET_STATE (button) != new_state)
345     gtk_widget_set_state (GTK_WIDGET (button), new_state);
346   if (toggled)
347     gtk_toggle_button_toggled (toggle_button);
348   gtk_widget_queue_draw (GTK_WIDGET (button));
349 }
350
351 static void
352 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
353                                  GdkRectangle   *area)
354 {
355   GtkWidget *widget;
356   GtkButton *button;
357   GtkToggleButton *toggle_button;
358   GtkStateType state_type;
359   GtkShadowType shadow_type;
360   GdkRectangle restrict_area;
361   GdkRectangle new_area;
362   gint width, height;
363   gint x, y;
364
365   g_return_if_fail (check_button != NULL);
366   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
367
368   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
369     {
370       widget = GTK_WIDGET (check_button);
371       button = GTK_BUTTON (check_button);
372       toggle_button = GTK_TOGGLE_BUTTON (check_button);
373
374       state_type = GTK_WIDGET_STATE (widget);
375       if ((state_type != GTK_STATE_NORMAL) &&
376           (state_type != GTK_STATE_PRELIGHT))
377         state_type = GTK_STATE_NORMAL;
378
379       restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
380       restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
381       restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
382       restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
383
384       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
385         {
386            if (state_type != GTK_STATE_NORMAL)
387              gtk_paint_flat_box(widget->style, widget->window, state_type, 
388                                 GTK_SHADOW_ETCHED_OUT,
389                                 area, widget, "radiobutton",
390                                 new_area.x, new_area.y,
391                                 new_area.width, new_area.height);
392         }
393       
394       x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
395       y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
396       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
397       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
398       
399       if (GTK_TOGGLE_BUTTON (widget)->active)
400         shadow_type = GTK_SHADOW_IN;
401       else
402         shadow_type = GTK_SHADOW_OUT;
403       
404       gtk_paint_option (widget->style, widget->window,
405                         GTK_WIDGET_STATE (widget), shadow_type,
406                         area, widget, "radiobutton",
407                         x, y, width, height);
408     }
409 }