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