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"
24 #define CHECK_BUTTON_CLASS(w) GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
27 static void gtk_radio_button_class_init (GtkRadioButtonClass *klass);
28 static void gtk_radio_button_init (GtkRadioButton *radio_button);
29 static void gtk_radio_button_destroy (GtkObject *object);
30 static void gtk_radio_button_clicked (GtkButton *button);
31 static void gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
35 static GtkCheckButtonClass *parent_class = NULL;
39 gtk_radio_button_get_type ()
41 static guint radio_button_type = 0;
43 if (!radio_button_type)
45 GtkTypeInfo radio_button_info =
48 sizeof (GtkRadioButton),
49 sizeof (GtkRadioButtonClass),
50 (GtkClassInitFunc) gtk_radio_button_class_init,
51 (GtkObjectInitFunc) gtk_radio_button_init,
56 radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
59 return radio_button_type;
63 gtk_radio_button_class_init (GtkRadioButtonClass *class)
65 GtkObjectClass *object_class;
66 GtkButtonClass *button_class;
67 GtkCheckButtonClass *check_button_class;
69 object_class = (GtkObjectClass*) class;
70 button_class = (GtkButtonClass*) class;
71 check_button_class = (GtkCheckButtonClass*) class;
73 parent_class = gtk_type_class (gtk_check_button_get_type ());
75 object_class->destroy = gtk_radio_button_destroy;
77 button_class->clicked = gtk_radio_button_clicked;
79 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
83 gtk_radio_button_init (GtkRadioButton *radio_button)
85 radio_button->group = NULL;
89 gtk_radio_button_set_group (GtkRadioButton *radio_button,
93 GtkRadioButton *tmp_button;
95 if (radio_button->group)
97 radio_button->group = g_slist_remove (radio_button->group, radio_button);
99 tmp_list = radio_button->group;
102 tmp_button = tmp_list->data;
103 tmp_list = tmp_list->next;
105 tmp_button->group = radio_button->group;
109 radio_button->group = g_slist_prepend (group, radio_button);
116 tmp_button = tmp_list->data;
117 tmp_list = tmp_list->next;
119 tmp_button->group = radio_button->group;
124 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
125 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
130 gtk_radio_button_new (GSList *group)
132 GtkRadioButton *radio_button;
134 radio_button = gtk_type_new (gtk_radio_button_get_type ());
136 gtk_radio_button_set_group (radio_button, group);
138 return GTK_WIDGET (radio_button);
142 gtk_radio_button_new_with_label (GSList *group,
145 GtkWidget *radio_button;
146 GtkWidget *label_widget;
148 radio_button = gtk_radio_button_new (group);
149 label_widget = gtk_label_new (label);
150 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
152 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
153 gtk_widget_show (label_widget);
159 gtk_radio_button_new_from_widget (GtkRadioButton *group)
163 l = gtk_radio_button_group (group);
164 return gtk_radio_button_new (l);
169 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
174 l = gtk_radio_button_group (group);
175 return gtk_radio_button_new_with_label (l, label);
179 gtk_radio_button_group (GtkRadioButton *radio_button)
181 g_return_val_if_fail (radio_button != NULL, NULL);
182 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
184 return radio_button->group;
189 gtk_radio_button_destroy (GtkObject *object)
191 GtkRadioButton *radio_button;
192 GtkRadioButton *tmp_button;
195 g_return_if_fail (object != NULL);
196 g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
198 radio_button = GTK_RADIO_BUTTON (object);
200 radio_button->group = g_slist_remove (radio_button->group, radio_button);
201 tmp_list = radio_button->group;
205 tmp_button = tmp_list->data;
206 tmp_list = tmp_list->next;
208 tmp_button->group = radio_button->group;
211 if (GTK_OBJECT_CLASS (parent_class)->destroy)
212 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
216 gtk_radio_button_clicked (GtkButton *button)
218 GtkToggleButton *toggle_button;
219 GtkRadioButton *radio_button;
220 GtkToggleButton *tmp_button;
221 GtkStateType new_state;
225 g_return_if_fail (button != NULL);
226 g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
228 radio_button = GTK_RADIO_BUTTON (button);
229 toggle_button = GTK_TOGGLE_BUTTON (button);
232 if (toggle_button->active)
235 tmp_list = radio_button->group;
239 tmp_button = tmp_list->data;
240 tmp_list = tmp_list->next;
242 if (tmp_button->active && (tmp_button != toggle_button))
250 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
255 toggle_button->active = !toggle_button->active;
256 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
262 toggle_button->active = !toggle_button->active;
264 tmp_list = radio_button->group;
267 tmp_button = tmp_list->data;
268 tmp_list = tmp_list->next;
270 if (tmp_button->active && (tmp_button != toggle_button))
272 gtk_button_clicked (GTK_BUTTON (tmp_button));
277 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
280 if (GTK_WIDGET_STATE (button) != new_state)
281 gtk_widget_set_state (GTK_WIDGET (button), new_state);
283 gtk_toggle_button_toggled (toggle_button);
284 gtk_widget_queue_draw (GTK_WIDGET (button));
288 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
293 GtkToggleButton *toggle_button;
294 GtkStateType state_type;
295 GtkShadowType shadow_type;
296 GdkRectangle restrict_area;
297 GdkRectangle new_area;
302 g_return_if_fail (check_button != NULL);
303 g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
305 if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
307 widget = GTK_WIDGET (check_button);
308 button = GTK_BUTTON (check_button);
309 toggle_button = GTK_TOGGLE_BUTTON (check_button);
311 state_type = GTK_WIDGET_STATE (widget);
312 if ((state_type != GTK_STATE_NORMAL) &&
313 (state_type != GTK_STATE_PRELIGHT))
314 state_type = GTK_STATE_NORMAL;
316 restrict_area.x = GTK_CONTAINER (widget)->border_width;
317 restrict_area.y = restrict_area.x;
318 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
319 restrict_area.height = widget->allocation.height - restrict_area.x * 2;
321 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
323 gtk_style_set_background (widget->style, widget->window, state_type);
324 gdk_window_clear_area (widget->window, new_area.x, new_area.y,
325 new_area.width, new_area.height);
328 x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
329 y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
330 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
331 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
333 if (GTK_TOGGLE_BUTTON (widget)->active)
334 shadow_type = GTK_SHADOW_IN;
336 shadow_type = GTK_SHADOW_OUT;
338 pts[0].x = x + width / 2;
340 pts[1].x = x + width;
341 pts[1].y = y + height / 2;
343 pts[2].y = y + height;
347 gdk_draw_polygon (widget->window,
348 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
350 gtk_draw_diamond (widget->style, widget->window,
351 GTK_WIDGET_STATE (widget), shadow_type,
352 x, y, width, height);