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 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_arg (GtkObject *object,
49 static void gtk_radio_button_get_arg (GtkObject *object,
53 static GtkCheckButtonClass *parent_class = NULL;
57 gtk_radio_button_get_type (void)
59 static GtkType radio_button_type = 0;
61 if (!radio_button_type)
63 static const GtkTypeInfo radio_button_info =
66 sizeof (GtkRadioButton),
67 sizeof (GtkRadioButtonClass),
68 (GtkClassInitFunc) gtk_radio_button_class_init,
69 (GtkObjectInitFunc) gtk_radio_button_init,
70 /* reserved_1 */ NULL,
71 /* reserved_2 */ NULL,
72 (GtkClassInitFunc) NULL,
75 radio_button_type = gtk_type_unique (gtk_check_button_get_type (), &radio_button_info);
78 return radio_button_type;
82 gtk_radio_button_class_init (GtkRadioButtonClass *class)
84 GtkObjectClass *object_class;
85 GtkButtonClass *button_class;
86 GtkCheckButtonClass *check_button_class;
87 GtkWidgetClass *widget_class;
89 object_class = (GtkObjectClass*) class;
90 widget_class = (GtkWidgetClass*) class;
91 button_class = (GtkButtonClass*) class;
92 check_button_class = (GtkCheckButtonClass*) class;
94 parent_class = gtk_type_class (gtk_check_button_get_type ());
96 gtk_object_add_arg_type ("GtkRadioButton::group", GTK_TYPE_RADIO_BUTTON, GTK_ARG_WRITABLE, ARG_GROUP);
98 object_class->set_arg = gtk_radio_button_set_arg;
99 object_class->get_arg = gtk_radio_button_get_arg;
100 object_class->destroy = gtk_radio_button_destroy;
102 widget_class->focus = gtk_radio_button_focus;
104 button_class->clicked = gtk_radio_button_clicked;
106 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
110 gtk_radio_button_init (GtkRadioButton *radio_button)
112 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
113 GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
115 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
117 GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
119 radio_button->group = g_slist_prepend (NULL, radio_button);
121 _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
122 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
126 gtk_radio_button_set_arg (GtkObject *object,
130 GtkRadioButton *radio_button;
132 radio_button = GTK_RADIO_BUTTON (object);
139 if (GTK_VALUE_OBJECT (*arg))
140 slist = gtk_radio_button_get_group ((GtkRadioButton*) GTK_VALUE_OBJECT (*arg));
143 gtk_radio_button_set_group (radio_button, slist);
151 gtk_radio_button_get_arg (GtkObject *object,
155 GtkRadioButton *radio_button;
157 radio_button = GTK_RADIO_BUTTON (object);
162 arg->type = GTK_TYPE_INVALID;
168 gtk_radio_button_set_group (GtkRadioButton *radio_button,
171 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
172 g_return_if_fail (!g_slist_find (group, radio_button));
174 if (radio_button->group)
178 radio_button->group = g_slist_remove (radio_button->group, radio_button);
180 for (slist = radio_button->group; slist; slist = slist->next)
182 GtkRadioButton *tmp_button;
184 tmp_button = slist->data;
186 tmp_button->group = radio_button->group;
190 radio_button->group = g_slist_prepend (group, radio_button);
196 for (slist = group; slist; slist = slist->next)
198 GtkRadioButton *tmp_button;
200 tmp_button = slist->data;
202 tmp_button->group = radio_button->group;
206 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
210 gtk_radio_button_new (GSList *group)
212 GtkRadioButton *radio_button;
214 radio_button = gtk_type_new (gtk_radio_button_get_type ());
217 gtk_radio_button_set_group (radio_button, group);
219 return GTK_WIDGET (radio_button);
223 gtk_radio_button_new_with_label (GSList *group,
226 GtkWidget *radio_button;
228 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
231 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
238 * gtk_radio_button_new_with_mnemonic:
239 * @group: the radio button group
240 * @label: the text of the button, with an underscore in front of the
242 * @returns: a new #GtkRadioButton
244 * Creates a new #GtkRadioButton containing a label, adding it to the same
245 * group as @group. The label will be created using
246 * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
247 * mnemonic for the button.
250 gtk_radio_button_new_with_mnemonic (GSList *group,
253 GtkWidget *radio_button;
255 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, "use_underline", TRUE, NULL);
258 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
264 gtk_radio_button_new_from_widget (GtkRadioButton *group)
268 l = gtk_radio_button_get_group (group);
269 return gtk_radio_button_new (l);
274 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
279 l = gtk_radio_button_get_group (group);
280 return gtk_radio_button_new_with_label (l, label);
284 * gtk_radio_button_new_with_mnemonic_from_widget:
285 * @group: widget to get radio group from
286 * @label: the text of the button, with an underscore in front of the
288 * @returns: a new #GtkRadioButton
290 * Creates a new #GtkRadioButton containing a label. The label
291 * will be created using gtk_label_new_with_mnemonic(), so underscores
292 * in @label indicate the mnemonic for the button.
295 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
300 l = gtk_radio_button_get_group (group);
301 return gtk_radio_button_new_with_mnemonic (l, label);
305 gtk_radio_button_get_group (GtkRadioButton *radio_button)
307 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
309 return radio_button->group;
314 gtk_radio_button_destroy (GtkObject *object)
316 GtkRadioButton *radio_button;
317 GtkRadioButton *tmp_button;
320 radio_button = GTK_RADIO_BUTTON (object);
322 radio_button->group = g_slist_remove (radio_button->group, radio_button);
323 tmp_list = radio_button->group;
327 tmp_button = tmp_list->data;
328 tmp_list = tmp_list->next;
330 tmp_button->group = radio_button->group;
333 /* this button is no longer in the group */
334 radio_button->group = NULL;
336 if (GTK_OBJECT_CLASS (parent_class)->destroy)
337 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
341 get_coordinates (GtkWidget *widget,
342 GtkWidget *reference,
346 *x = widget->allocation.x + widget->allocation.width / 2;
347 *y = widget->allocation.y + widget->allocation.height / 2;
349 gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
353 left_right_compare (gconstpointer a,
359 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
360 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
363 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
365 return (y1 < y2) ? -1 : 1;
369 up_down_compare (gconstpointer a,
375 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
376 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
379 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
381 return (x1 < x2) ? -1 : 1;
385 gtk_radio_button_focus (GtkWidget *widget,
386 GtkDirectionType direction)
388 GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
391 /* Radio buttons with draw_indicator unset focus "normally", since
392 * they look like buttons to the user.
394 if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
395 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
397 if (gtk_widget_is_focus (widget))
399 GSList *focus_list, *tmp_list;
400 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
401 GtkWidget *new_focus = NULL;
403 focus_list = g_slist_copy (radio_button->group);
407 case GTK_DIR_TAB_FORWARD:
408 case GTK_DIR_TAB_BACKWARD:
412 focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
416 focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
420 if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
421 focus_list = g_slist_reverse (focus_list);
423 tmp_list = g_slist_find (focus_list, widget);
427 tmp_list = tmp_list->next;
431 GtkWidget *child = tmp_list->data;
433 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
439 tmp_list = tmp_list->next;
445 tmp_list = focus_list;
449 GtkWidget *child = tmp_list->data;
451 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
457 tmp_list = tmp_list->next;
461 g_slist_free (focus_list);
465 gtk_widget_grab_focus (new_focus);
466 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
473 GtkRadioButton *selected_button = NULL;
475 /* We accept the focus if, we don't have the focus and
476 * - we are the currently active button in the group
477 * - there is no currently active radio button.
480 tmp_slist = radio_button->group;
483 if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
484 selected_button = tmp_slist->data;
485 tmp_slist = tmp_slist->next;
488 if (selected_button && selected_button != radio_button)
491 gtk_widget_grab_focus (widget);
497 gtk_radio_button_clicked (GtkButton *button)
499 GtkToggleButton *toggle_button;
500 GtkRadioButton *radio_button;
501 GtkToggleButton *tmp_button;
502 GtkStateType new_state;
507 radio_button = GTK_RADIO_BUTTON (button);
508 toggle_button = GTK_TOGGLE_BUTTON (button);
511 gtk_widget_ref (GTK_WIDGET (button));
513 if (toggle_button->active)
516 tmp_list = radio_button->group;
520 tmp_button = tmp_list->data;
521 tmp_list = tmp_list->next;
523 if (tmp_button->active && tmp_button != toggle_button)
531 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
536 toggle_button->active = !toggle_button->active;
537 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
543 toggle_button->active = !toggle_button->active;
545 tmp_list = radio_button->group;
548 tmp_button = tmp_list->data;
549 tmp_list = tmp_list->next;
551 if (tmp_button->active && (tmp_button != toggle_button))
553 gtk_button_clicked (GTK_BUTTON (tmp_button));
558 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
561 if (toggle_button->inconsistent)
563 else if (button->in_button && button->button_down)
564 depressed = !toggle_button->active;
566 depressed = toggle_button->active;
568 if (GTK_WIDGET_STATE (button) != new_state)
569 gtk_widget_set_state (GTK_WIDGET (button), new_state);
572 gtk_toggle_button_toggled (toggle_button);
574 _gtk_button_set_depressed (button, depressed);
576 gtk_widget_queue_draw (GTK_WIDGET (button));
578 gtk_widget_unref (GTK_WIDGET (button));
582 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
587 GtkToggleButton *toggle_button;
588 GtkStateType state_type;
589 GtkShadowType shadow_type;
591 gint indicator_size, indicator_spacing;
594 gboolean interior_focus;
596 if (GTK_WIDGET_DRAWABLE (check_button))
598 widget = GTK_WIDGET (check_button);
599 button = GTK_BUTTON (check_button);
600 toggle_button = GTK_TOGGLE_BUTTON (check_button);
602 gtk_widget_style_get (widget,
603 "interior_focus", &interior_focus,
604 "focus-line-width", &focus_width,
605 "focus-padding", &focus_pad,
608 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
610 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
611 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
614 x += focus_width + focus_pad;
616 if (toggle_button->inconsistent)
617 shadow_type = GTK_SHADOW_ETCHED_IN;
618 else if (toggle_button->active)
619 shadow_type = GTK_SHADOW_IN;
621 shadow_type = GTK_SHADOW_OUT;
623 if (button->activate_timeout || (button->button_down && button->in_button))
624 state_type = GTK_STATE_ACTIVE;
625 else if (button->in_button)
626 state_type = GTK_STATE_PRELIGHT;
628 state_type = GTK_STATE_NORMAL;
630 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
631 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
633 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
635 GdkRectangle restrict_area;
636 GdkRectangle new_area;
638 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
639 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
640 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
641 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
643 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
645 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
646 GTK_SHADOW_ETCHED_OUT,
647 area, widget, "checkbutton",
648 new_area.x, new_area.y,
649 new_area.width, new_area.height);
653 gtk_paint_option (widget->style, widget->window,
654 state_type, shadow_type,
655 area, widget, "radiobutton",
656 x, y, indicator_size, indicator_size);