1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
20 #include "gtkradiobutton.h"
21 #include "gtksignal.h"
29 #define CHECK_BUTTON_CLASS(w) GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
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,
38 static void gtk_radio_button_set_arg (GtkObject *object,
41 static void gtk_radio_button_get_arg (GtkObject *object,
46 static GtkCheckButtonClass *parent_class = NULL;
50 gtk_radio_button_get_type (void)
52 static GtkType radio_button_type = 0;
54 if (!radio_button_type)
56 static const GtkTypeInfo radio_button_info =
59 sizeof (GtkRadioButton),
60 sizeof (GtkRadioButtonClass),
61 (GtkClassInitFunc) gtk_radio_button_class_init,
62 (GtkObjectInitFunc) gtk_radio_button_init,
63 /* reserved_1 */ NULL,
64 /* reserved_2 */ NULL,
65 (GtkClassInitFunc) NULL,
68 radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
71 return radio_button_type;
75 gtk_radio_button_class_init (GtkRadioButtonClass *class)
77 GtkObjectClass *object_class;
78 GtkButtonClass *button_class;
79 GtkCheckButtonClass *check_button_class;
81 object_class = (GtkObjectClass*) class;
82 button_class = (GtkButtonClass*) class;
83 check_button_class = (GtkCheckButtonClass*) class;
85 parent_class = gtk_type_class (gtk_check_button_get_type ());
87 gtk_object_add_arg_type ("GtkRadioButton::group", GTK_TYPE_RADIO_BUTTON, GTK_ARG_WRITABLE, ARG_GROUP);
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;
93 button_class->clicked = gtk_radio_button_clicked;
95 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
99 gtk_radio_button_init (GtkRadioButton *radio_button)
101 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
103 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
105 radio_button->group = g_slist_prepend (NULL, radio_button);
107 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
111 gtk_radio_button_set_arg (GtkObject *object,
115 GtkRadioButton *radio_button;
117 radio_button = GTK_RADIO_BUTTON (object);
124 if (GTK_VALUE_OBJECT (*arg))
125 slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
128 gtk_radio_button_set_group (radio_button, slist);
136 gtk_radio_button_get_arg (GtkObject *object,
140 GtkRadioButton *radio_button;
142 radio_button = GTK_RADIO_BUTTON (object);
147 arg->type = GTK_TYPE_INVALID;
153 gtk_radio_button_set_group (GtkRadioButton *radio_button,
156 g_return_if_fail (radio_button != NULL);
157 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
158 g_return_if_fail (!g_slist_find (group, radio_button));
160 if (radio_button->group)
164 radio_button->group = g_slist_remove (radio_button->group, radio_button);
166 for (slist = radio_button->group; slist; slist = slist->next)
168 GtkRadioButton *tmp_button;
170 tmp_button = slist->data;
172 tmp_button->group = radio_button->group;
176 radio_button->group = g_slist_prepend (group, radio_button);
182 for (slist = group; slist; slist = slist->next)
184 GtkRadioButton *tmp_button;
186 tmp_button = slist->data;
188 tmp_button->group = radio_button->group;
192 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
196 gtk_radio_button_new (GSList *group)
198 GtkRadioButton *radio_button;
200 radio_button = gtk_type_new (gtk_radio_button_get_type ());
203 gtk_radio_button_set_group (radio_button, group);
205 return GTK_WIDGET (radio_button);
209 gtk_radio_button_new_with_label (GSList *group,
212 GtkWidget *radio_button;
213 GtkWidget *label_widget;
215 radio_button = gtk_radio_button_new (group);
216 label_widget = gtk_label_new (label);
217 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
219 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
220 gtk_widget_show (label_widget);
226 gtk_radio_button_new_from_widget (GtkRadioButton *group)
230 l = gtk_radio_button_group (group);
231 return gtk_radio_button_new (l);
236 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
241 l = gtk_radio_button_group (group);
242 return gtk_radio_button_new_with_label (l, label);
246 gtk_radio_button_group (GtkRadioButton *radio_button)
248 g_return_val_if_fail (radio_button != NULL, NULL);
249 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
251 return radio_button->group;
256 gtk_radio_button_destroy (GtkObject *object)
258 GtkRadioButton *radio_button;
259 GtkRadioButton *tmp_button;
262 g_return_if_fail (object != NULL);
263 g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
265 radio_button = GTK_RADIO_BUTTON (object);
267 radio_button->group = g_slist_remove (radio_button->group, radio_button);
268 tmp_list = radio_button->group;
272 tmp_button = tmp_list->data;
273 tmp_list = tmp_list->next;
275 tmp_button->group = radio_button->group;
278 if (GTK_OBJECT_CLASS (parent_class)->destroy)
279 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
283 gtk_radio_button_clicked (GtkButton *button)
285 GtkToggleButton *toggle_button;
286 GtkRadioButton *radio_button;
287 GtkToggleButton *tmp_button;
288 GtkStateType new_state;
292 g_return_if_fail (button != NULL);
293 g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
295 radio_button = GTK_RADIO_BUTTON (button);
296 toggle_button = GTK_TOGGLE_BUTTON (button);
299 gtk_widget_ref (GTK_WIDGET (button));
301 if (toggle_button->active)
304 tmp_list = radio_button->group;
308 tmp_button = tmp_list->data;
309 tmp_list = tmp_list->next;
311 if (tmp_button->active && tmp_button != toggle_button)
319 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
324 toggle_button->active = !toggle_button->active;
325 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
331 toggle_button->active = !toggle_button->active;
333 tmp_list = radio_button->group;
336 tmp_button = tmp_list->data;
337 tmp_list = tmp_list->next;
339 if (tmp_button->active && (tmp_button != toggle_button))
341 gtk_button_clicked (GTK_BUTTON (tmp_button));
346 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
349 if (GTK_WIDGET_STATE (button) != new_state)
350 gtk_widget_set_state (GTK_WIDGET (button), new_state);
353 gtk_toggle_button_toggled (toggle_button);
355 gtk_widget_queue_draw (GTK_WIDGET (button));
357 gtk_widget_unref (GTK_WIDGET (button));
361 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
366 GtkToggleButton *toggle_button;
367 GtkStateType state_type;
368 GtkShadowType shadow_type;
369 GdkRectangle restrict_area;
370 GdkRectangle new_area;
374 g_return_if_fail (check_button != NULL);
375 g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
377 if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
379 widget = GTK_WIDGET (check_button);
380 button = GTK_BUTTON (check_button);
381 toggle_button = GTK_TOGGLE_BUTTON (check_button);
383 state_type = GTK_WIDGET_STATE (widget);
384 if ((state_type != GTK_STATE_NORMAL) &&
385 (state_type != GTK_STATE_PRELIGHT))
386 state_type = GTK_STATE_NORMAL;
388 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
389 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
390 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
391 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
393 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
395 if (state_type != GTK_STATE_NORMAL)
396 gtk_paint_flat_box(widget->style, widget->window, state_type,
397 GTK_SHADOW_ETCHED_OUT,
398 area, widget, "radiobutton",
399 new_area.x, new_area.y,
400 new_area.width, new_area.height);
403 x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
404 y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
405 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
406 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
408 if (GTK_TOGGLE_BUTTON (widget)->active)
409 shadow_type = GTK_SHADOW_IN;
411 shadow_type = GTK_SHADOW_OUT;
413 gtk_paint_option (widget->style, widget->window,
414 GTK_WIDGET_STATE (widget), shadow_type,
415 area, widget, "radiobutton",
416 x, y, width, height);