]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
main part for GtkArgSetFunc/GtkArgGetFunc implementation.
[~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 GtkWidget*
88 gtk_radio_button_new (GSList *group)
89 {
90   GtkRadioButton *radio_button;
91   GtkRadioButton *tmp_button;
92   GSList *tmp_list;
93
94   radio_button = gtk_type_new (gtk_radio_button_get_type ());
95
96   tmp_list = group;
97   radio_button->group = g_slist_prepend (group, radio_button);
98
99   if (tmp_list)
100     {
101       while (tmp_list)
102         {
103           tmp_button = tmp_list->data;
104           tmp_list = tmp_list->next;
105
106           tmp_button->group = radio_button->group;
107         }
108     }
109   else
110     {
111       GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
112       gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
113     }
114
115   return GTK_WIDGET (radio_button);
116 }
117
118 GtkWidget*
119 gtk_radio_button_new_with_label (GSList      *group,
120                                  const gchar *label)
121 {
122   GtkWidget *radio_button;
123   GtkWidget *label_widget;
124
125   radio_button = gtk_radio_button_new (group);
126   label_widget = gtk_label_new (label);
127   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
128
129   gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
130   gtk_widget_show (label_widget);
131
132   return radio_button;
133 }
134
135 GtkWidget*
136 gtk_radio_button_new_from_widget (GtkRadioButton *group)
137 {
138   GSList *l = NULL;
139   if (group)
140     l = gtk_radio_button_group (group);
141   return gtk_radio_button_new (l);
142 }
143
144
145 GtkWidget*
146 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
147                                              const gchar    *label)
148 {
149   GSList *l = NULL;
150   if (group)
151     l = gtk_radio_button_group (group);
152   return gtk_radio_button_new_with_label (l, label);
153 }
154
155 GSList*
156 gtk_radio_button_group (GtkRadioButton *radio_button)
157 {
158   g_return_val_if_fail (radio_button != NULL, NULL);
159   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
160
161   return radio_button->group;
162 }
163
164
165 static void
166 gtk_radio_button_destroy (GtkObject *object)
167 {
168   GtkRadioButton *radio_button;
169   GtkRadioButton *tmp_button;
170   GSList *tmp_list;
171
172   g_return_if_fail (object != NULL);
173   g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
174
175   radio_button = GTK_RADIO_BUTTON (object);
176
177   radio_button->group = g_slist_remove (radio_button->group, radio_button);
178   tmp_list = radio_button->group;
179
180   while (tmp_list)
181     {
182       tmp_button = tmp_list->data;
183       tmp_list = tmp_list->next;
184
185       tmp_button->group = radio_button->group;
186     }
187
188   if (GTK_OBJECT_CLASS (parent_class)->destroy)
189     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
190 }
191
192 static void
193 gtk_radio_button_clicked (GtkButton *button)
194 {
195   GtkToggleButton *toggle_button;
196   GtkRadioButton *radio_button;
197   GtkToggleButton *tmp_button;
198   GtkStateType new_state;
199   GSList *tmp_list;
200   gint toggled;
201
202   g_return_if_fail (button != NULL);
203   g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
204
205   radio_button = GTK_RADIO_BUTTON (button);
206   toggle_button = GTK_TOGGLE_BUTTON (button);
207   toggled = FALSE;
208
209   if (toggle_button->active)
210     {
211       tmp_button = NULL;
212       tmp_list = radio_button->group;
213
214       while (tmp_list)
215         {
216           tmp_button = tmp_list->data;
217           tmp_list = tmp_list->next;
218
219           if (tmp_button->active && (tmp_button != toggle_button))
220             break;
221
222           tmp_button = NULL;
223         }
224
225       if (!tmp_button)
226         {
227           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
228         }
229       else
230         {
231           toggled = TRUE;
232           toggle_button->active = !toggle_button->active;
233           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
234         }
235     }
236   else
237     {
238       toggled = TRUE;
239       toggle_button->active = !toggle_button->active;
240
241       tmp_list = radio_button->group;
242       while (tmp_list)
243         {
244           tmp_button = tmp_list->data;
245           tmp_list = tmp_list->next;
246
247           if (tmp_button->active && (tmp_button != toggle_button))
248             {
249               gtk_button_clicked (GTK_BUTTON (tmp_button));
250               break;
251             }
252         }
253
254       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
255     }
256
257   if (GTK_WIDGET_STATE (button) != new_state)
258     gtk_widget_set_state (GTK_WIDGET (button), new_state);
259   if (toggled)
260     gtk_toggle_button_toggled (toggle_button);
261   gtk_widget_queue_draw (GTK_WIDGET (button));
262 }
263
264 static void
265 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
266                                  GdkRectangle   *area)
267 {
268   GtkWidget *widget;
269   GtkButton *button;
270   GtkToggleButton *toggle_button;
271   GtkStateType state_type;
272   GtkShadowType shadow_type;
273   GdkPoint pts[4];
274   gint width, height;
275   gint x, y;
276
277   g_return_if_fail (check_button != NULL);
278   g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
279
280   if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
281     {
282       widget = GTK_WIDGET (check_button);
283       button = GTK_BUTTON (check_button);
284       toggle_button = GTK_TOGGLE_BUTTON (check_button);
285
286       state_type = GTK_WIDGET_STATE (widget);
287       if ((state_type != GTK_STATE_NORMAL) &&
288           (state_type != GTK_STATE_PRELIGHT))
289         state_type = GTK_STATE_NORMAL;
290
291       gtk_style_set_background (widget->style, widget->window, state_type);
292       gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
293
294       x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
295       y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
296       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
297       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
298
299       if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
300         shadow_type = GTK_SHADOW_IN;
301       else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT) && toggle_button->active)
302         shadow_type = GTK_SHADOW_IN;
303       else
304         shadow_type = GTK_SHADOW_OUT;
305
306       pts[0].x = x + width / 2;
307       pts[0].y = y;
308       pts[1].x = x + width;
309       pts[1].y = y + height / 2;
310       pts[2].x = pts[0].x;
311       pts[2].y = y + height;
312       pts[3].x = x;
313       pts[3].y = pts[1].y;
314
315       gdk_draw_polygon (widget->window,
316                         widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
317                         TRUE, pts, 4);
318       gtk_draw_diamond (widget->style, widget->window,
319                         GTK_WIDGET_STATE (widget), shadow_type,
320                         x, y, width, height);
321     }
322 }