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