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