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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gtkradiobutton.h"
29 #include "gtksignal.h"
38 static void gtk_radio_button_class_init (GtkRadioButtonClass *klass);
39 static void gtk_radio_button_init (GtkRadioButton *radio_button);
40 static void gtk_radio_button_destroy (GtkObject *object);
41 static void gtk_radio_button_clicked (GtkButton *button);
42 static void gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
44 static void gtk_radio_button_set_arg (GtkObject *object,
47 static void gtk_radio_button_get_arg (GtkObject *object,
52 static GtkCheckButtonClass *parent_class = NULL;
56 gtk_radio_button_get_type (void)
58 static GtkType radio_button_type = 0;
60 if (!radio_button_type)
62 static const GtkTypeInfo radio_button_info =
65 sizeof (GtkRadioButton),
66 sizeof (GtkRadioButtonClass),
67 (GtkClassInitFunc) gtk_radio_button_class_init,
68 (GtkObjectInitFunc) gtk_radio_button_init,
69 /* reserved_1 */ NULL,
70 /* reserved_2 */ NULL,
71 (GtkClassInitFunc) NULL,
74 radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
77 return radio_button_type;
81 gtk_radio_button_class_init (GtkRadioButtonClass *class)
83 GtkObjectClass *object_class;
84 GtkButtonClass *button_class;
85 GtkCheckButtonClass *check_button_class;
87 object_class = (GtkObjectClass*) class;
88 button_class = (GtkButtonClass*) class;
89 check_button_class = (GtkCheckButtonClass*) class;
91 parent_class = gtk_type_class (gtk_check_button_get_type ());
93 gtk_object_add_arg_type ("GtkRadioButton::group", GTK_TYPE_RADIO_BUTTON, GTK_ARG_WRITABLE, ARG_GROUP);
95 object_class->set_arg = gtk_radio_button_set_arg;
96 object_class->get_arg = gtk_radio_button_get_arg;
97 object_class->destroy = gtk_radio_button_destroy;
99 button_class->clicked = gtk_radio_button_clicked;
101 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
105 gtk_radio_button_init (GtkRadioButton *radio_button)
107 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
108 GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
110 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
112 radio_button->group = g_slist_prepend (NULL, radio_button);
114 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
118 gtk_radio_button_set_arg (GtkObject *object,
122 GtkRadioButton *radio_button;
124 radio_button = GTK_RADIO_BUTTON (object);
131 if (GTK_VALUE_OBJECT (*arg))
132 slist = gtk_radio_button_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
135 gtk_radio_button_set_group (radio_button, slist);
143 gtk_radio_button_get_arg (GtkObject *object,
147 GtkRadioButton *radio_button;
149 radio_button = GTK_RADIO_BUTTON (object);
154 arg->type = GTK_TYPE_INVALID;
160 gtk_radio_button_set_group (GtkRadioButton *radio_button,
163 g_return_if_fail (radio_button != NULL);
164 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
165 g_return_if_fail (!g_slist_find (group, radio_button));
167 if (radio_button->group)
171 radio_button->group = g_slist_remove (radio_button->group, radio_button);
173 for (slist = radio_button->group; slist; slist = slist->next)
175 GtkRadioButton *tmp_button;
177 tmp_button = slist->data;
179 tmp_button->group = radio_button->group;
183 radio_button->group = g_slist_prepend (group, radio_button);
189 for (slist = group; slist; slist = slist->next)
191 GtkRadioButton *tmp_button;
193 tmp_button = slist->data;
195 tmp_button->group = radio_button->group;
199 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
203 gtk_radio_button_new (GSList *group)
205 GtkRadioButton *radio_button;
207 radio_button = gtk_type_new (gtk_radio_button_get_type ());
210 gtk_radio_button_set_group (radio_button, group);
212 return GTK_WIDGET (radio_button);
216 gtk_radio_button_new_with_label (GSList *group,
219 GtkWidget *radio_button;
220 GtkWidget *label_widget;
222 radio_button = gtk_radio_button_new (group);
223 label_widget = gtk_label_new (label);
224 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
226 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
227 gtk_widget_show (label_widget);
234 * gtk_radio_button_new_with_mnemonic:
235 * @group: the radio button group
236 * @label: the text of the button, with an underscore in front of the
238 * @returns: a new #GtkRadioButton
240 * Creates a new #GtkRadioButton containing a label. The label
241 * will be created using gtk_label_new_with_mnemonic(), so underscores
242 * in @label indicate the mnemonic for the button.
245 gtk_radio_button_new_with_mnemonic (GSList *group,
248 GtkWidget *radio_button;
249 GtkWidget *label_widget;
251 radio_button = gtk_radio_button_new (group);
252 label_widget = gtk_label_new_with_mnemonic (label);
253 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
255 gtk_container_add (GTK_CONTAINER (radio_button), label_widget);
256 gtk_widget_show (label_widget);
262 gtk_radio_button_new_from_widget (GtkRadioButton *group)
266 l = gtk_radio_button_group (group);
267 return gtk_radio_button_new (l);
272 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
277 l = gtk_radio_button_group (group);
278 return gtk_radio_button_new_with_label (l, label);
282 * gtk_radio_button_new_with_mnemonic_from_widget:
283 * @group: widget to get radio group from
284 * @label: the text of the button, with an underscore in front of the
286 * @returns: a new #GtkRadioButton
288 * Creates a new #GtkRadioButton containing a label. The label
289 * will be created using gtk_label_new_with_mnemonic(), so underscores
290 * in @label indicate the mnemonic for the button.
293 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
298 l = gtk_radio_button_group (group);
299 return gtk_radio_button_new_with_mnemonic (l, label);
303 gtk_radio_button_group (GtkRadioButton *radio_button)
305 g_return_val_if_fail (radio_button != NULL, NULL);
306 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
308 return radio_button->group;
313 gtk_radio_button_destroy (GtkObject *object)
315 GtkRadioButton *radio_button;
316 GtkRadioButton *tmp_button;
319 g_return_if_fail (object != NULL);
320 g_return_if_fail (GTK_IS_RADIO_BUTTON (object));
322 radio_button = GTK_RADIO_BUTTON (object);
324 radio_button->group = g_slist_remove (radio_button->group, radio_button);
325 tmp_list = radio_button->group;
329 tmp_button = tmp_list->data;
330 tmp_list = tmp_list->next;
332 tmp_button->group = radio_button->group;
335 if (GTK_OBJECT_CLASS (parent_class)->destroy)
336 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
340 gtk_radio_button_clicked (GtkButton *button)
342 GtkToggleButton *toggle_button;
343 GtkRadioButton *radio_button;
344 GtkToggleButton *tmp_button;
345 GtkStateType new_state;
349 g_return_if_fail (button != NULL);
350 g_return_if_fail (GTK_IS_RADIO_BUTTON (button));
352 radio_button = GTK_RADIO_BUTTON (button);
353 toggle_button = GTK_TOGGLE_BUTTON (button);
356 gtk_widget_ref (GTK_WIDGET (button));
358 if (toggle_button->active)
361 tmp_list = radio_button->group;
365 tmp_button = tmp_list->data;
366 tmp_list = tmp_list->next;
368 if (tmp_button->active && tmp_button != toggle_button)
376 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
381 toggle_button->active = !toggle_button->active;
382 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
388 toggle_button->active = !toggle_button->active;
390 tmp_list = radio_button->group;
393 tmp_button = tmp_list->data;
394 tmp_list = tmp_list->next;
396 if (tmp_button->active && (tmp_button != toggle_button))
398 gtk_button_clicked (GTK_BUTTON (tmp_button));
403 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
406 if (GTK_WIDGET_STATE (button) != new_state)
407 gtk_widget_set_state (GTK_WIDGET (button), new_state);
410 gtk_toggle_button_toggled (toggle_button);
412 gtk_widget_queue_draw (GTK_WIDGET (button));
414 gtk_widget_unref (GTK_WIDGET (button));
418 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
423 GtkToggleButton *toggle_button;
424 GtkStateType state_type;
425 GtkShadowType shadow_type;
426 GdkRectangle restrict_area;
427 GdkRectangle new_area;
429 gint indicator_size, indicator_spacing;
431 g_return_if_fail (check_button != NULL);
432 g_return_if_fail (GTK_IS_RADIO_BUTTON (check_button));
434 if (GTK_WIDGET_VISIBLE (check_button) && GTK_WIDGET_MAPPED (check_button))
436 widget = GTK_WIDGET (check_button);
437 button = GTK_BUTTON (check_button);
438 toggle_button = GTK_TOGGLE_BUTTON (check_button);
440 state_type = GTK_WIDGET_STATE (widget);
441 if ((state_type != GTK_STATE_NORMAL) &&
442 (state_type != GTK_STATE_PRELIGHT))
443 state_type = GTK_STATE_NORMAL;
445 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
447 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
448 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
449 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
450 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
452 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
454 if (state_type != GTK_STATE_NORMAL)
455 gtk_paint_flat_box(widget->style, widget->window, state_type,
456 GTK_SHADOW_ETCHED_OUT,
457 area, widget, "radiobutton",
458 new_area.x, new_area.y,
459 new_area.width, new_area.height);
462 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
463 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
465 if (GTK_TOGGLE_BUTTON (widget)->active)
466 shadow_type = GTK_SHADOW_IN;
468 shadow_type = GTK_SHADOW_OUT;
470 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
471 shadow_type = GTK_SHADOW_ETCHED_IN;
473 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
474 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
476 gtk_paint_option (widget->style, widget->window,
477 GTK_WIDGET_STATE (widget), shadow_type,
478 area, widget, "radiobutton",
479 x, y, indicator_size, indicator_size);