]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
always display the correct active state.
[~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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtklabel.h"
19 #include "gtkradiobutton.h"
20 #include "gtksignal.h"
21
22
23 #define CHECK_BUTTON_CLASS(w)  GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
24
25
26 static void gtk_radio_button_class_init     (GtkRadioButtonClass  *klass);
27 static void gtk_radio_button_init           (GtkRadioButton       *radio_button);
28 static void gtk_radio_button_destroy        (GtkObject            *object);
29 static void gtk_radio_button_clicked        (GtkButton            *button);
30 static void gtk_radio_button_draw_indicator (GtkCheckButton       *check_button,
31                                              GdkRectangle         *area);
32
33
34 static GtkCheckButtonClass *parent_class = NULL;
35
36
37 guint
38 gtk_radio_button_get_type ()
39 {
40   static guint radio_button_type = 0;
41
42   if (!radio_button_type)
43     {
44       GtkTypeInfo radio_button_info =
45       {
46         "GtkRadioButton",
47         sizeof (GtkRadioButton),
48         sizeof (GtkRadioButtonClass),
49         (GtkClassInitFunc) gtk_radio_button_class_init,
50         (GtkObjectInitFunc) gtk_radio_button_init,
51         (GtkArgSetFunc) NULL,
52         (GtkArgGetFunc) NULL,
53       };
54
55       radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
56     }
57
58   return radio_button_type;
59 }
60
61 static void
62 gtk_radio_button_class_init (GtkRadioButtonClass *class)
63 {
64   GtkObjectClass *object_class;
65   GtkButtonClass *button_class;
66   GtkCheckButtonClass *check_button_class;
67
68   object_class = (GtkObjectClass*) class;
69   button_class = (GtkButtonClass*) class;
70   check_button_class = (GtkCheckButtonClass*) class;
71
72   parent_class = gtk_type_class (gtk_check_button_get_type ());
73
74   object_class->destroy = gtk_radio_button_destroy;
75
76   button_class->clicked = gtk_radio_button_clicked;
77
78   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
79 }
80
81 static void
82 gtk_radio_button_init (GtkRadioButton *radio_button)
83 {
84   radio_button->group = NULL;
85 }
86
87 void
88 gtk_radio_button_set_group (GtkRadioButton *radio_button,
89                             GSList *group)
90 {
91   GSList *tmp_list;
92   GtkRadioButton *tmp_button;
93
94   if (radio_button->group)
95     {
96       radio_button->group = g_slist_remove (radio_button->group, radio_button);
97       
98       tmp_list = radio_button->group;
99       while (tmp_list)
100         {
101           tmp_button = tmp_list->data;
102           tmp_list = tmp_list->next;
103           
104           tmp_button->group = radio_button->group;
105         }
106     }
107
108   radio_button->group = g_slist_prepend (group, radio_button);
109   tmp_list = group;
110
111   if (tmp_list)
112     {
113       while (tmp_list)
114         {
115           tmp_button = tmp_list->data;
116           tmp_list = tmp_list->next;
117
118           tmp_button->group = radio_button->group;
119         }
120     }
121   else
122     {
123       GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
124       gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
125     }
126 }
127
128 GtkWidget*
129 gtk_radio_button_new (GSList *group)
130 {
131   GtkRadioButton *radio_button;
132
133   radio_button = gtk_type_new (gtk_radio_button_get_type ());
134
135   gtk_radio_button_set_group (radio_button, group);
136
137   return GTK_WIDGET (radio_button);
138 }
139
140 GtkWidget*
141 gtk_radio_button_new_with_label (GSList      *group,
142                                  const gchar *label)
143 {
144   GtkWidget *radio_button;
145   GtkWidget *label_widget;
146
147   radio_button = gtk_radio_button_new (group);
148   label_widget = gtk_label_new (label);
149   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
150
151   gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
152   gtk_widget_show (label_widget);
153
154   return radio_button;
155 }
156
157 GtkWidget*
158 gtk_radio_button_new_from_widget (GtkRadioButton *group)
159 {
160   GSList *l = NULL;
161   if (group)
162     l = gtk_radio_button_group (group);
163   return gtk_radio_button_new (l);
164 }
165
166
167 GtkWidget*
168 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
169                                              const gchar    *label)
170 {
171   GSList *l = NULL;
172   if (group)
173     l = gtk_radio_button_group (group);
174   return gtk_radio_button_new_with_label (l, label);
175 }
176
177 GSList*
178 gtk_radio_button_group (GtkRadioButton *radio_button)
179 {
180   g_return_val_if_fail (radio_button != NULL, NULL);
181   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
182
183   return radio_button->group;
184 }
185
186
187 static void
188 gtk_radio_button_destroy (GtkObject *object)
189 {
190   GtkRadioButton *radio_button;
191   GtkRadioButton *tmp_button;
192   GSList *tmp_list;
193
194   g_return_if_fail (object != NULL);
195   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
196
197   radio_button = GTK_RADIO_BUTTON (object);
198
199   radio_button->group = g_slist_remove (radio_button->group, radio_button);
200   tmp_list = radio_button->group;
201
202   while (tmp_list)
203     {
204       tmp_button = tmp_list->data;
205       tmp_list = tmp_list->next;
206
207       tmp_button->group = radio_button->group;
208     }
209
210   if (GTK_OBJECT_CLASS (parent_class)->destroy)
211     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
212 }
213
214 static void
215 gtk_radio_button_clicked (GtkButton *button)
216 {
217   GtkToggleButton *toggle_button;
218   GtkRadioButton *radio_button;
219   GtkToggleButton *tmp_button;
220   GtkStateType new_state;
221   GSList *tmp_list;
222   gint toggled;
223
224   g_return_if_fail (button != NULL);
225   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
226
227   radio_button = GTK_RADIO_BUTTON (button);
228   toggle_button = GTK_TOGGLE_BUTTON (button);
229   toggled = FALSE;
230
231   if (toggle_button->active)
232     {
233       tmp_button = NULL;
234       tmp_list = radio_button->group;
235
236       while (tmp_list)
237         {
238           tmp_button = tmp_list->data;
239           tmp_list = tmp_list->next;
240
241           if (tmp_button->active && (tmp_button != toggle_button))
242             break;
243
244           tmp_button = NULL;
245         }
246
247       if (!tmp_button)
248         {
249           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
250         }
251       else
252         {
253           toggled = TRUE;
254           toggle_button->active = !toggle_button->active;
255           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
256         }
257     }
258   else
259     {
260       toggled = TRUE;
261       toggle_button->active = !toggle_button->active;
262
263       tmp_list = radio_button->group;
264       while (tmp_list)
265         {
266           tmp_button = tmp_list->data;
267           tmp_list = tmp_list->next;
268
269           if (tmp_button->active && (tmp_button != toggle_button))
270             {
271               gtk_button_clicked (GTK_BUTTON (tmp_button));
272               break;
273             }
274         }
275
276       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
277     }
278
279   if (GTK_WIDGET_STATE (button) != new_state)
280     gtk_widget_set_state (GTK_WIDGET (button), new_state);
281   if (toggled)
282     gtk_toggle_button_toggled (toggle_button);
283   gtk_widget_queue_draw (GTK_WIDGET (button));
284 }
285
286 static void
287 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
288                                  GdkRectangle   *area)
289 {
290   GtkWidget *widget;
291   GtkButton *button;
292   GtkToggleButton *toggle_button;
293   GtkStateType state_type;
294   GtkShadowType shadow_type;
295   GdkRectangle restrict_area;
296   GdkRectangle new_area;
297   GdkPoint pts[4];
298   gint width, height;
299   gint x, y;
300
301   g_return_if_fail (check_button != NULL);
302   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
303
304   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
305     {
306       widget = GTK_WIDGET (check_button);
307       button = GTK_BUTTON (check_button);
308       toggle_button = GTK_TOGGLE_BUTTON (check_button);
309
310       state_type = GTK_WIDGET_STATE (widget);
311       if ((state_type != GTK_STATE_NORMAL) &&
312           (state_type != GTK_STATE_PRELIGHT))
313         state_type = GTK_STATE_NORMAL;
314
315       restrict_area.x = GTK_CONTAINER (widget)->border_width;
316       restrict_area.y = restrict_area.x;
317       restrict_area.width = widget->allocation.width - restrict_area.x * 2;
318       restrict_area.height = widget->allocation.height - restrict_area.x * 2;
319
320       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
321         {
322           gtk_style_set_background (widget->style, widget->window, state_type);
323           gdk_window_clear_area (widget->window, new_area.x, new_area.y,
324                                  new_area.width, new_area.height);
325         }
326       
327       x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
328       y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
329       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
330       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
331
332       if (GTK_TOGGLE_BUTTON (widget)->active)
333         shadow_type = GTK_SHADOW_IN;
334       else
335         shadow_type = GTK_SHADOW_OUT;
336
337       pts[0].x = x + width / 2;
338       pts[0].y = y;
339       pts[1].x = x + width;
340       pts[1].y = y + height / 2;
341       pts[2].x = pts[0].x;
342       pts[2].y = y + height;
343       pts[3].x = x;
344       pts[3].y = pts[1].y;
345
346       gdk_draw_polygon (widget->window,
347                         widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
348                         TRUE, pts, 4);
349       gtk_draw_diamond (widget->style, widget->window,
350                         GTK_WIDGET_STATE (widget), shadow_type,
351                         x, y, width, height);
352     }
353 }