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 /* reversed_1 */ NULL,
64 /* reversed_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 radio_button->group = g_slist_prepend (NULL, radio_button);
105 gtk_radio_button_set_arg (GtkObject *object,
109 GtkRadioButton *radio_button;
111 radio_button = GTK_RADIO_BUTTON (object);
118 if (GTK_VALUE_OBJECT (*arg))
119 slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
122 gtk_radio_button_set_group (radio_button, slist);
130 gtk_radio_button_get_arg (GtkObject *object,
134 GtkRadioButton *radio_button;
136 radio_button = GTK_RADIO_BUTTON (object);
141 arg->type = GTK_TYPE_INVALID;
147 gtk_radio_button_set_group (GtkRadioButton *radio_button,
150 g_return_if_fail (radio_button != NULL);
151 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
152 g_return_if_fail (!g_slist_find (group, radio_button));
154 if (radio_button->group)
158 radio_button->group = g_slist_remove (radio_button->group, radio_button);
160 for (slist = radio_button->group; slist; slist = slist->next)
162 GtkRadioButton *tmp_button;
164 tmp_button = slist->data;
166 tmp_button->group = radio_button->group;
170 radio_button->group = g_slist_prepend (group, radio_button);
176 for (slist = group; slist; slist = slist->next)
178 GtkRadioButton *tmp_button;
180 tmp_button = slist->data;
182 tmp_button->group = radio_button->group;
187 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
188 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
193 gtk_radio_button_new (GSList *group)
195 GtkRadioButton *radio_button;
197 radio_button = gtk_type_new (gtk_radio_button_get_type ());
199 gtk_radio_button_set_group (radio_button, group);
201 return GTK_WIDGET (radio_button);
205 gtk_radio_button_new_with_label (GSList *group,
208 GtkWidget *radio_button;
209 GtkWidget *label_widget;
211 radio_button = gtk_radio_button_new (group);
212 label_widget = gtk_label_new (label);
213 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
215 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
216 gtk_widget_show (label_widget);
222 gtk_radio_button_new_from_widget (GtkRadioButton *group)
226 l = gtk_radio_button_group (group);
227 return gtk_radio_button_new (l);
232 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
237 l = gtk_radio_button_group (group);
238 return gtk_radio_button_new_with_label (l, label);
242 gtk_radio_button_group (GtkRadioButton *radio_button)
244 g_return_val_if_fail (radio_button != NULL, NULL);
245 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
247 return radio_button->group;
252 gtk_radio_button_destroy (GtkObject *object)
254 GtkRadioButton *radio_button;
255 GtkRadioButton *tmp_button;
258 g_return_if_fail (object != NULL);
259 g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
261 radio_button = GTK_RADIO_BUTTON (object);
263 radio_button->group = g_slist_remove (radio_button->group, radio_button);
264 tmp_list = radio_button->group;
268 tmp_button = tmp_list->data;
269 tmp_list = tmp_list->next;
271 tmp_button->group = radio_button->group;
274 if (GTK_OBJECT_CLASS (parent_class)->destroy)
275 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
279 gtk_radio_button_clicked (GtkButton *button)
281 GtkToggleButton *toggle_button;
282 GtkRadioButton *radio_button;
283 GtkToggleButton *tmp_button;
284 GtkStateType new_state;
288 g_return_if_fail (button != NULL);
289 g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
291 radio_button = GTK_RADIO_BUTTON (button);
292 toggle_button = GTK_TOGGLE_BUTTON (button);
295 if (toggle_button->active)
298 tmp_list = radio_button->group;
302 tmp_button = tmp_list->data;
303 tmp_list = tmp_list->next;
305 if (tmp_button->active && (tmp_button != toggle_button))
313 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
318 toggle_button->active = !toggle_button->active;
319 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
325 toggle_button->active = !toggle_button->active;
327 tmp_list = radio_button->group;
330 tmp_button = tmp_list->data;
331 tmp_list = tmp_list->next;
333 if (tmp_button->active && (tmp_button != toggle_button))
335 gtk_button_clicked (GTK_BUTTON (tmp_button));
340 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
343 if (GTK_WIDGET_STATE (button) != new_state)
344 gtk_widget_set_state (GTK_WIDGET (button), new_state);
346 gtk_toggle_button_toggled (toggle_button);
347 gtk_widget_queue_draw (GTK_WIDGET (button));
351 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
356 GtkToggleButton *toggle_button;
357 GtkStateType state_type;
358 GtkShadowType shadow_type;
359 GdkRectangle restrict_area;
360 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 = GTK_CONTAINER (widget)->border_width;
380 restrict_area.y = restrict_area.x;
381 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
382 restrict_area.height = widget->allocation.height - restrict_area.x * 2;
384 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
386 gtk_style_set_background (widget->style, widget->window, state_type);
387 gdk_window_clear_area (widget->window, new_area.x, new_area.y,
388 new_area.width, new_area.height);
391 x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
392 y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
393 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
394 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
396 if (GTK_TOGGLE_BUTTON (widget)->active)
397 shadow_type = GTK_SHADOW_IN;
399 shadow_type = GTK_SHADOW_OUT;
401 pts[0].x = x + width / 2;
403 pts[1].x = x + width;
404 pts[1].y = y + height / 2;
406 pts[2].y = y + height;
410 gdk_draw_polygon (widget->window,
411 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
413 gtk_draw_diamond (widget->style, widget->window,
414 GTK_WIDGET_STATE (widget), shadow_type,
415 x, y, width, height);