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 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);
102 radio_button->group = g_slist_prepend (NULL, radio_button);
106 gtk_radio_button_set_arg (GtkObject *object,
110 GtkRadioButton *radio_button;
112 radio_button = GTK_RADIO_BUTTON (object);
119 if (GTK_VALUE_OBJECT (*arg))
120 slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
123 gtk_radio_button_set_group (radio_button, slist);
131 gtk_radio_button_get_arg (GtkObject *object,
135 GtkRadioButton *radio_button;
137 radio_button = GTK_RADIO_BUTTON (object);
142 arg->type = GTK_TYPE_INVALID;
148 gtk_radio_button_set_group (GtkRadioButton *radio_button,
151 g_return_if_fail (radio_button != NULL);
152 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
153 g_return_if_fail (!g_slist_find (group, radio_button));
155 if (radio_button->group)
159 radio_button->group = g_slist_remove (radio_button->group, radio_button);
161 for (slist = radio_button->group; slist; slist = slist->next)
163 GtkRadioButton *tmp_button;
165 tmp_button = slist->data;
167 tmp_button->group = radio_button->group;
171 radio_button->group = g_slist_prepend (group, radio_button);
177 for (slist = group; slist; slist = slist->next)
179 GtkRadioButton *tmp_button;
181 tmp_button = slist->data;
183 tmp_button->group = radio_button->group;
188 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
189 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
194 gtk_radio_button_new (GSList *group)
196 GtkRadioButton *radio_button;
198 radio_button = gtk_type_new (gtk_radio_button_get_type ());
200 gtk_radio_button_set_group (radio_button, group);
202 return GTK_WIDGET (radio_button);
206 gtk_radio_button_new_with_label (GSList *group,
209 GtkWidget *radio_button;
210 GtkWidget *label_widget;
212 radio_button = gtk_radio_button_new (group);
213 label_widget = gtk_label_new (label);
214 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
216 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
217 gtk_widget_show (label_widget);
223 gtk_radio_button_new_from_widget (GtkRadioButton *group)
227 l = gtk_radio_button_group (group);
228 return gtk_radio_button_new (l);
233 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
238 l = gtk_radio_button_group (group);
239 return gtk_radio_button_new_with_label (l, label);
243 gtk_radio_button_group (GtkRadioButton *radio_button)
245 g_return_val_if_fail (radio_button != NULL, NULL);
246 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
248 return radio_button->group;
253 gtk_radio_button_destroy (GtkObject *object)
255 GtkRadioButton *radio_button;
256 GtkRadioButton *tmp_button;
259 g_return_if_fail (object != NULL);
260 g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
262 radio_button = GTK_RADIO_BUTTON (object);
264 radio_button->group = g_slist_remove (radio_button->group, radio_button);
265 tmp_list = radio_button->group;
269 tmp_button = tmp_list->data;
270 tmp_list = tmp_list->next;
272 tmp_button->group = radio_button->group;
275 if (GTK_OBJECT_CLASS (parent_class)->destroy)
276 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
280 gtk_radio_button_clicked (GtkButton *button)
282 GtkToggleButton *toggle_button;
283 GtkRadioButton *radio_button;
284 GtkToggleButton *tmp_button;
285 GtkStateType new_state;
289 g_return_if_fail (button != NULL);
290 g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
292 radio_button = GTK_RADIO_BUTTON (button);
293 toggle_button = GTK_TOGGLE_BUTTON (button);
296 if (toggle_button->active)
299 tmp_list = radio_button->group;
303 tmp_button = tmp_list->data;
304 tmp_list = tmp_list->next;
306 if (tmp_button->active && (tmp_button != toggle_button))
314 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
319 toggle_button->active = !toggle_button->active;
320 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
326 toggle_button->active = !toggle_button->active;
328 tmp_list = radio_button->group;
331 tmp_button = tmp_list->data;
332 tmp_list = tmp_list->next;
334 if (tmp_button->active && (tmp_button != toggle_button))
336 gtk_button_clicked (GTK_BUTTON (tmp_button));
341 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
344 if (GTK_WIDGET_STATE (button) != new_state)
345 gtk_widget_set_state (GTK_WIDGET (button), new_state);
347 gtk_toggle_button_toggled (toggle_button);
348 gtk_widget_queue_draw (GTK_WIDGET (button));
352 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
357 GtkToggleButton *toggle_button;
358 GtkStateType state_type;
359 GtkShadowType shadow_type;
360 GdkRectangle restrict_area;
361 GdkRectangle new_area;
365 g_return_if_fail (check_button != NULL);
366 g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
368 if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
370 widget = GTK_WIDGET (check_button);
371 button = GTK_BUTTON (check_button);
372 toggle_button = GTK_TOGGLE_BUTTON (check_button);
374 state_type = GTK_WIDGET_STATE (widget);
375 if ((state_type != GTK_STATE_NORMAL) &&
376 (state_type != GTK_STATE_PRELIGHT))
377 state_type = GTK_STATE_NORMAL;
379 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
380 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
381 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
382 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
384 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
386 if (state_type != GTK_STATE_NORMAL)
387 gtk_paint_flat_box(widget->style, widget->window, state_type,
388 GTK_SHADOW_ETCHED_OUT,
389 area, widget, "radiobutton",
390 new_area.x, new_area.y,
391 new_area.width, new_area.height);
394 x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
395 y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
396 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
397 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
399 if (GTK_TOGGLE_BUTTON (widget)->active)
400 shadow_type = GTK_SHADOW_IN;
402 shadow_type = GTK_SHADOW_OUT;
404 gtk_paint_option (widget->style, widget->window,
405 GTK_WIDGET_STATE (widget), shadow_type,
406 area, widget, "radiobutton",
407 x, y, width, height);