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"
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 gboolean gtk_radio_button_focus (GtkWidget *widget,
42 GtkDirectionType direction);
43 static void gtk_radio_button_clicked (GtkButton *button);
44 static void gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
46 static void gtk_radio_button_set_property (GObject *object,
50 static void gtk_radio_button_get_property (GObject *object,
55 static GtkCheckButtonClass *parent_class = NULL;
59 gtk_radio_button_get_type (void)
61 static GType radio_button_type = 0;
63 if (!radio_button_type)
65 static const GTypeInfo radio_button_info =
67 sizeof (GtkRadioButtonClass),
69 NULL, /* base_finalize */
70 (GClassInitFunc) gtk_radio_button_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
73 sizeof (GtkRadioButton),
75 (GInstanceInitFunc) gtk_radio_button_init,
79 g_type_register_static (GTK_TYPE_CHECK_BUTTON, "GtkRadioButton",
80 &radio_button_info, 0);
83 return radio_button_type;
87 gtk_radio_button_class_init (GtkRadioButtonClass *class)
89 GObjectClass *gobject_class;
90 GtkObjectClass *object_class;
91 GtkButtonClass *button_class;
92 GtkCheckButtonClass *check_button_class;
93 GtkWidgetClass *widget_class;
95 gobject_class = G_OBJECT_CLASS (class);
96 object_class = (GtkObjectClass*) class;
97 widget_class = (GtkWidgetClass*) class;
98 button_class = (GtkButtonClass*) class;
99 check_button_class = (GtkCheckButtonClass*) class;
101 parent_class = g_type_class_peek_parent (class);
103 gobject_class->set_property = gtk_radio_button_set_property;
104 gobject_class->get_property = gtk_radio_button_get_property;
106 g_object_class_install_property (gobject_class,
108 g_param_spec_object ("group",
110 _("The radio button whose group this widget belongs."),
111 GTK_TYPE_RADIO_BUTTON,
113 object_class->destroy = gtk_radio_button_destroy;
115 widget_class->focus = gtk_radio_button_focus;
117 button_class->clicked = gtk_radio_button_clicked;
119 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
123 gtk_radio_button_init (GtkRadioButton *radio_button)
125 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
126 GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
128 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
130 GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
132 radio_button->group = g_slist_prepend (NULL, radio_button);
134 _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
135 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
139 gtk_radio_button_set_property (GObject *object,
144 GtkRadioButton *radio_button;
146 radio_button = GTK_RADIO_BUTTON (object);
153 if (G_VALUE_HOLDS_OBJECT (value))
154 slist = gtk_radio_button_get_group ((GtkRadioButton*) g_value_get_object (value));
157 gtk_radio_button_set_group (radio_button, slist);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 gtk_radio_button_get_property (GObject *object,
171 GtkRadioButton *radio_button;
173 radio_button = GTK_RADIO_BUTTON (object);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 gtk_radio_button_set_group (GtkRadioButton *radio_button,
187 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
188 g_return_if_fail (!g_slist_find (group, radio_button));
190 if (radio_button->group)
194 radio_button->group = g_slist_remove (radio_button->group, radio_button);
196 for (slist = radio_button->group; slist; slist = slist->next)
198 GtkRadioButton *tmp_button;
200 tmp_button = slist->data;
202 tmp_button->group = radio_button->group;
206 radio_button->group = g_slist_prepend (group, radio_button);
212 for (slist = group; slist; slist = slist->next)
214 GtkRadioButton *tmp_button;
216 tmp_button = slist->data;
218 tmp_button->group = radio_button->group;
222 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
226 gtk_radio_button_new (GSList *group)
228 GtkRadioButton *radio_button;
230 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
233 gtk_radio_button_set_group (radio_button, group);
235 return GTK_WIDGET (radio_button);
239 gtk_radio_button_new_with_label (GSList *group,
242 GtkWidget *radio_button;
244 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
247 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
254 * gtk_radio_button_new_with_mnemonic:
255 * @group: the radio button group
256 * @label: the text of the button, with an underscore in front of the
258 * @returns: a new #GtkRadioButton
260 * Creates a new #GtkRadioButton containing a label, adding it to the same
261 * group as @group. The label will be created using
262 * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
263 * mnemonic for the button.
266 gtk_radio_button_new_with_mnemonic (GSList *group,
269 GtkWidget *radio_button;
271 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, "use_underline", TRUE, NULL);
274 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
280 gtk_radio_button_new_from_widget (GtkRadioButton *group)
284 l = gtk_radio_button_get_group (group);
285 return gtk_radio_button_new (l);
290 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
295 l = gtk_radio_button_get_group (group);
296 return gtk_radio_button_new_with_label (l, label);
300 * gtk_radio_button_new_with_mnemonic_from_widget:
301 * @group: widget to get radio group from
302 * @label: the text of the button, with an underscore in front of the
304 * @returns: a new #GtkRadioButton
306 * Creates a new #GtkRadioButton containing a label. The label
307 * will be created using gtk_label_new_with_mnemonic(), so underscores
308 * in @label indicate the mnemonic for the button.
311 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
316 l = gtk_radio_button_get_group (group);
317 return gtk_radio_button_new_with_mnemonic (l, label);
321 gtk_radio_button_get_group (GtkRadioButton *radio_button)
323 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
325 return radio_button->group;
330 gtk_radio_button_destroy (GtkObject *object)
332 GtkRadioButton *radio_button;
333 GtkRadioButton *tmp_button;
336 radio_button = GTK_RADIO_BUTTON (object);
338 radio_button->group = g_slist_remove (radio_button->group, radio_button);
339 tmp_list = radio_button->group;
343 tmp_button = tmp_list->data;
344 tmp_list = tmp_list->next;
346 tmp_button->group = radio_button->group;
349 /* this button is no longer in the group */
350 radio_button->group = NULL;
352 if (GTK_OBJECT_CLASS (parent_class)->destroy)
353 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
357 get_coordinates (GtkWidget *widget,
358 GtkWidget *reference,
362 *x = widget->allocation.x + widget->allocation.width / 2;
363 *y = widget->allocation.y + widget->allocation.height / 2;
365 gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
369 left_right_compare (gconstpointer a,
375 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
376 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
379 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
381 return (y1 < y2) ? -1 : 1;
385 up_down_compare (gconstpointer a,
391 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
392 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
395 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
397 return (x1 < x2) ? -1 : 1;
401 gtk_radio_button_focus (GtkWidget *widget,
402 GtkDirectionType direction)
404 GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
407 /* Radio buttons with draw_indicator unset focus "normally", since
408 * they look like buttons to the user.
410 if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
411 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
413 if (gtk_widget_is_focus (widget))
415 GSList *focus_list, *tmp_list;
416 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
417 GtkWidget *new_focus = NULL;
419 focus_list = g_slist_copy (radio_button->group);
423 case GTK_DIR_TAB_FORWARD:
424 case GTK_DIR_TAB_BACKWARD:
428 focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
432 focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
436 if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
437 focus_list = g_slist_reverse (focus_list);
439 tmp_list = g_slist_find (focus_list, widget);
443 tmp_list = tmp_list->next;
447 GtkWidget *child = tmp_list->data;
449 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
455 tmp_list = tmp_list->next;
461 tmp_list = focus_list;
465 GtkWidget *child = tmp_list->data;
467 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
473 tmp_list = tmp_list->next;
477 g_slist_free (focus_list);
481 gtk_widget_grab_focus (new_focus);
482 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
489 GtkRadioButton *selected_button = NULL;
491 /* We accept the focus if, we don't have the focus and
492 * - we are the currently active button in the group
493 * - there is no currently active radio button.
496 tmp_slist = radio_button->group;
499 if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
500 selected_button = tmp_slist->data;
501 tmp_slist = tmp_slist->next;
504 if (selected_button && selected_button != radio_button)
507 gtk_widget_grab_focus (widget);
513 gtk_radio_button_clicked (GtkButton *button)
515 GtkToggleButton *toggle_button;
516 GtkRadioButton *radio_button;
517 GtkToggleButton *tmp_button;
518 GtkStateType new_state;
523 radio_button = GTK_RADIO_BUTTON (button);
524 toggle_button = GTK_TOGGLE_BUTTON (button);
527 g_object_ref (GTK_WIDGET (button));
529 if (toggle_button->active)
532 tmp_list = radio_button->group;
536 tmp_button = tmp_list->data;
537 tmp_list = tmp_list->next;
539 if (tmp_button->active && tmp_button != toggle_button)
547 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
552 toggle_button->active = !toggle_button->active;
553 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
559 toggle_button->active = !toggle_button->active;
561 tmp_list = radio_button->group;
564 tmp_button = tmp_list->data;
565 tmp_list = tmp_list->next;
567 if (tmp_button->active && (tmp_button != toggle_button))
569 gtk_button_clicked (GTK_BUTTON (tmp_button));
574 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
577 if (toggle_button->inconsistent)
579 else if (button->in_button && button->button_down)
580 depressed = !toggle_button->active;
582 depressed = toggle_button->active;
584 if (GTK_WIDGET_STATE (button) != new_state)
585 gtk_widget_set_state (GTK_WIDGET (button), new_state);
588 gtk_toggle_button_toggled (toggle_button);
590 _gtk_button_set_depressed (button, depressed);
592 gtk_widget_queue_draw (GTK_WIDGET (button));
594 g_object_unref (button);
598 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
604 GtkToggleButton *toggle_button;
605 GtkStateType state_type;
606 GtkShadowType shadow_type;
608 gint indicator_size, indicator_spacing;
611 gboolean interior_focus;
613 if (GTK_WIDGET_DRAWABLE (check_button))
615 widget = GTK_WIDGET (check_button);
616 button = GTK_BUTTON (check_button);
617 toggle_button = GTK_TOGGLE_BUTTON (check_button);
619 gtk_widget_style_get (widget,
620 "interior_focus", &interior_focus,
621 "focus-line-width", &focus_width,
622 "focus-padding", &focus_pad,
625 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
627 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
628 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
630 child = GTK_BIN (check_button)->child;
631 if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
632 x += focus_width + focus_pad;
634 if (toggle_button->inconsistent)
635 shadow_type = GTK_SHADOW_ETCHED_IN;
636 else if (toggle_button->active)
637 shadow_type = GTK_SHADOW_IN;
639 shadow_type = GTK_SHADOW_OUT;
641 if (button->activate_timeout || (button->button_down && button->in_button))
642 state_type = GTK_STATE_ACTIVE;
643 else if (button->in_button)
644 state_type = GTK_STATE_PRELIGHT;
646 state_type = GTK_STATE_NORMAL;
648 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
649 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
651 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
653 GdkRectangle restrict_area;
654 GdkRectangle new_area;
656 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
657 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
658 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
659 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
661 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
663 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
664 GTK_SHADOW_ETCHED_OUT,
665 area, widget, "checkbutton",
666 new_area.x, new_area.y,
667 new_area.width, new_area.height);
671 gtk_paint_option (widget->style, widget->window,
672 state_type, shadow_type,
673 area, widget, "radiobutton",
674 x, y, indicator_size, indicator_size);