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/.
29 #include "gtkmarshalers.h"
30 #include "gtkradiobutton.h"
31 #include "gtkprivate.h"
42 static void gtk_radio_button_class_init (GtkRadioButtonClass *klass);
43 static void gtk_radio_button_init (GtkRadioButton *radio_button);
44 static void gtk_radio_button_destroy (GtkObject *object);
45 static gboolean gtk_radio_button_focus (GtkWidget *widget,
46 GtkDirectionType direction);
47 static void gtk_radio_button_clicked (GtkButton *button);
48 static void gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
50 static void gtk_radio_button_set_property (GObject *object,
54 static void gtk_radio_button_get_property (GObject *object,
59 static GtkCheckButtonClass *parent_class = NULL;
61 static guint group_changed_signal = 0;
64 gtk_radio_button_get_type (void)
66 static GType radio_button_type = 0;
68 if (!radio_button_type)
70 static const GTypeInfo radio_button_info =
72 sizeof (GtkRadioButtonClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) gtk_radio_button_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof (GtkRadioButton),
80 (GInstanceInitFunc) gtk_radio_button_init,
84 g_type_register_static (GTK_TYPE_CHECK_BUTTON, I_("GtkRadioButton"),
85 &radio_button_info, 0);
88 return radio_button_type;
92 gtk_radio_button_class_init (GtkRadioButtonClass *class)
94 GObjectClass *gobject_class;
95 GtkObjectClass *object_class;
96 GtkButtonClass *button_class;
97 GtkCheckButtonClass *check_button_class;
98 GtkWidgetClass *widget_class;
100 gobject_class = G_OBJECT_CLASS (class);
101 object_class = (GtkObjectClass*) class;
102 widget_class = (GtkWidgetClass*) class;
103 button_class = (GtkButtonClass*) class;
104 check_button_class = (GtkCheckButtonClass*) class;
106 parent_class = g_type_class_peek_parent (class);
108 gobject_class->set_property = gtk_radio_button_set_property;
109 gobject_class->get_property = gtk_radio_button_get_property;
111 g_object_class_install_property (gobject_class,
113 g_param_spec_object ("group",
115 P_("The radio button whose group this widget belongs to."),
116 GTK_TYPE_RADIO_BUTTON,
117 GTK_PARAM_WRITABLE));
118 object_class->destroy = gtk_radio_button_destroy;
120 widget_class->focus = gtk_radio_button_focus;
122 button_class->clicked = gtk_radio_button_clicked;
124 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
126 class->group_changed = NULL;
129 * GtkRadioButton::group-changed:
130 * @style: the object which received the signal
132 * Emitted when the group of radio buttons that a radio button belongs
133 * to changes. This is emitted when a radio button switches from
134 * being alone to being part of a group of 2 or more buttons, or
135 * vice-versa, and when a buttton is moved from one group of 2 or
136 * more buttons to a different one, but not when the composition
137 * of the group that a button belongs to changes.
141 group_changed_signal = g_signal_new (I_("group-changed"),
142 G_OBJECT_CLASS_TYPE (object_class),
144 G_STRUCT_OFFSET (GtkRadioButtonClass, group_changed),
146 _gtk_marshal_VOID__VOID,
151 gtk_radio_button_init (GtkRadioButton *radio_button)
153 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
154 GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
156 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
158 GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
160 radio_button->group = g_slist_prepend (NULL, radio_button);
162 _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
163 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
167 gtk_radio_button_set_property (GObject *object,
172 GtkRadioButton *radio_button;
174 radio_button = GTK_RADIO_BUTTON (object);
181 if (G_VALUE_HOLDS_OBJECT (value))
182 slist = gtk_radio_button_get_group ((GtkRadioButton*) g_value_get_object (value));
185 gtk_radio_button_set_group (radio_button, slist);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 gtk_radio_button_get_property (GObject *object,
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 gtk_radio_button_set_group (GtkRadioButton *radio_button,
211 GtkWidget *old_group_singleton = NULL;
212 GtkWidget *new_group_singleton = NULL;
214 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
215 g_return_if_fail (!g_slist_find (group, radio_button));
217 if (radio_button->group)
221 radio_button->group = g_slist_remove (radio_button->group, radio_button);
223 if (radio_button->group && !radio_button->group->next)
224 old_group_singleton = g_object_ref (radio_button->group->data);
226 for (slist = radio_button->group; slist; slist = slist->next)
228 GtkRadioButton *tmp_button;
230 tmp_button = slist->data;
232 tmp_button->group = radio_button->group;
236 if (group && !group->next)
237 new_group_singleton = g_object_ref (group->data);
239 radio_button->group = g_slist_prepend (group, radio_button);
245 for (slist = group; slist; slist = slist->next)
247 GtkRadioButton *tmp_button;
249 tmp_button = slist->data;
251 tmp_button->group = radio_button->group;
255 g_object_ref (radio_button);
257 g_object_notify (G_OBJECT (radio_button), "group");
258 g_signal_emit (radio_button, group_changed_signal, 0);
259 if (old_group_singleton)
261 g_signal_emit (old_group_singleton, group_changed_signal, 0);
262 g_object_unref (old_group_singleton);
264 if (new_group_singleton)
266 g_signal_emit (new_group_singleton, group_changed_signal, 0);
267 g_object_unref (new_group_singleton);
270 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
272 g_object_unref (radio_button);
276 gtk_radio_button_new (GSList *group)
278 GtkRadioButton *radio_button;
280 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
283 gtk_radio_button_set_group (radio_button, group);
285 return GTK_WIDGET (radio_button);
289 gtk_radio_button_new_with_label (GSList *group,
292 GtkWidget *radio_button;
294 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
297 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
304 * gtk_radio_button_new_with_mnemonic:
305 * @group: the radio button group
306 * @label: the text of the button, with an underscore in front of the
308 * @returns: a new #GtkRadioButton
310 * Creates a new #GtkRadioButton containing a label, adding it to the same
311 * group as @group. The label will be created using
312 * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
313 * mnemonic for the button.
316 gtk_radio_button_new_with_mnemonic (GSList *group,
319 GtkWidget *radio_button;
321 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON,
323 "use-underline", TRUE,
327 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
333 gtk_radio_button_new_from_widget (GtkRadioButton *group)
337 l = gtk_radio_button_get_group (group);
338 return gtk_radio_button_new (l);
343 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
348 l = gtk_radio_button_get_group (group);
349 return gtk_radio_button_new_with_label (l, label);
353 * gtk_radio_button_new_with_mnemonic_from_widget:
354 * @group: widget to get radio group from
355 * @label: the text of the button, with an underscore in front of the
357 * @returns: a new #GtkRadioButton
359 * Creates a new #GtkRadioButton containing a label. The label
360 * will be created using gtk_label_new_with_mnemonic(), so underscores
361 * in @label indicate the mnemonic for the button.
364 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
369 l = gtk_radio_button_get_group (group);
370 return gtk_radio_button_new_with_mnemonic (l, label);
374 gtk_radio_button_get_group (GtkRadioButton *radio_button)
376 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
378 return radio_button->group;
383 gtk_radio_button_destroy (GtkObject *object)
385 GtkWidget *old_group_singleton = NULL;
386 GtkRadioButton *radio_button;
387 GtkRadioButton *tmp_button;
389 gboolean was_in_group;
391 radio_button = GTK_RADIO_BUTTON (object);
393 was_in_group = radio_button->group && radio_button->group->next;
395 radio_button->group = g_slist_remove (radio_button->group, radio_button);
396 if (radio_button->group && !radio_button->group->next)
397 old_group_singleton = radio_button->group->data;
399 tmp_list = radio_button->group;
403 tmp_button = tmp_list->data;
404 tmp_list = tmp_list->next;
406 tmp_button->group = radio_button->group;
409 /* this button is no longer in the group */
410 radio_button->group = NULL;
412 if (old_group_singleton)
413 g_signal_emit (old_group_singleton, group_changed_signal, 0);
415 g_signal_emit (radio_button, group_changed_signal, 0);
417 if (GTK_OBJECT_CLASS (parent_class)->destroy)
418 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
422 get_coordinates (GtkWidget *widget,
423 GtkWidget *reference,
427 *x = widget->allocation.x + widget->allocation.width / 2;
428 *y = widget->allocation.y + widget->allocation.height / 2;
430 gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
434 left_right_compare (gconstpointer a,
440 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
441 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
444 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
446 return (y1 < y2) ? -1 : 1;
450 up_down_compare (gconstpointer a,
456 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
457 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
460 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
462 return (x1 < x2) ? -1 : 1;
466 gtk_radio_button_focus (GtkWidget *widget,
467 GtkDirectionType direction)
469 GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
472 /* Radio buttons with draw_indicator unset focus "normally", since
473 * they look like buttons to the user.
475 if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
476 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
478 if (gtk_widget_is_focus (widget))
480 GSList *focus_list, *tmp_list;
481 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
482 GtkWidget *new_focus = NULL;
484 focus_list = g_slist_copy (radio_button->group);
488 case GTK_DIR_TAB_FORWARD:
489 case GTK_DIR_TAB_BACKWARD:
493 focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
497 focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
501 if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
502 focus_list = g_slist_reverse (focus_list);
504 tmp_list = g_slist_find (focus_list, widget);
508 tmp_list = tmp_list->next;
512 GtkWidget *child = tmp_list->data;
514 if (GTK_WIDGET_REALIZED (child) && GTK_WIDGET_IS_SENSITIVE (child))
520 tmp_list = tmp_list->next;
526 tmp_list = focus_list;
530 GtkWidget *child = tmp_list->data;
532 if (GTK_WIDGET_REALIZED (child) && GTK_WIDGET_IS_SENSITIVE (child))
538 tmp_list = tmp_list->next;
542 g_slist_free (focus_list);
546 gtk_widget_grab_focus (new_focus);
547 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
554 GtkRadioButton *selected_button = NULL;
556 /* We accept the focus if, we don't have the focus and
557 * - we are the currently active button in the group
558 * - there is no currently active radio button.
561 tmp_slist = radio_button->group;
564 if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
565 selected_button = tmp_slist->data;
566 tmp_slist = tmp_slist->next;
569 if (selected_button && selected_button != radio_button)
572 gtk_widget_grab_focus (widget);
578 gtk_radio_button_clicked (GtkButton *button)
580 GtkToggleButton *toggle_button;
581 GtkRadioButton *radio_button;
582 GtkToggleButton *tmp_button;
583 GtkStateType new_state;
588 radio_button = GTK_RADIO_BUTTON (button);
589 toggle_button = GTK_TOGGLE_BUTTON (button);
592 g_object_ref (GTK_WIDGET (button));
594 if (toggle_button->active)
597 tmp_list = radio_button->group;
601 tmp_button = tmp_list->data;
602 tmp_list = tmp_list->next;
604 if (tmp_button->active && tmp_button != toggle_button)
612 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
617 toggle_button->active = !toggle_button->active;
618 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
624 toggle_button->active = !toggle_button->active;
626 tmp_list = radio_button->group;
629 tmp_button = tmp_list->data;
630 tmp_list = tmp_list->next;
632 if (tmp_button->active && (tmp_button != toggle_button))
634 gtk_button_clicked (GTK_BUTTON (tmp_button));
639 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
642 if (toggle_button->inconsistent)
644 else if (button->in_button && button->button_down)
645 depressed = !toggle_button->active;
647 depressed = toggle_button->active;
649 if (GTK_WIDGET_STATE (button) != new_state)
650 gtk_widget_set_state (GTK_WIDGET (button), new_state);
653 gtk_toggle_button_toggled (toggle_button);
655 _gtk_button_set_depressed (button, depressed);
657 gtk_widget_queue_draw (GTK_WIDGET (button));
659 g_object_unref (button);
663 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
669 GtkToggleButton *toggle_button;
670 GtkStateType state_type;
671 GtkShadowType shadow_type;
673 gint indicator_size, indicator_spacing;
676 gboolean interior_focus;
678 if (GTK_WIDGET_DRAWABLE (check_button))
680 widget = GTK_WIDGET (check_button);
681 button = GTK_BUTTON (check_button);
682 toggle_button = GTK_TOGGLE_BUTTON (check_button);
684 gtk_widget_style_get (widget,
685 "interior-focus", &interior_focus,
686 "focus-line-width", &focus_width,
687 "focus-padding", &focus_pad,
690 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
692 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
693 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
695 child = GTK_BIN (check_button)->child;
696 if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
697 x += focus_width + focus_pad;
699 if (toggle_button->inconsistent)
700 shadow_type = GTK_SHADOW_ETCHED_IN;
701 else if (toggle_button->active)
702 shadow_type = GTK_SHADOW_IN;
704 shadow_type = GTK_SHADOW_OUT;
706 if (button->activate_timeout || (button->button_down && button->in_button))
707 state_type = GTK_STATE_ACTIVE;
708 else if (button->in_button)
709 state_type = GTK_STATE_PRELIGHT;
710 else if (!GTK_WIDGET_IS_SENSITIVE (widget))
711 state_type = GTK_STATE_INSENSITIVE;
713 state_type = GTK_STATE_NORMAL;
715 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
716 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
718 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
720 GdkRectangle restrict_area;
721 GdkRectangle new_area;
723 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
724 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
725 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
726 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
728 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
730 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
731 GTK_SHADOW_ETCHED_OUT,
732 area, widget, "checkbutton",
733 new_area.x, new_area.y,
734 new_area.width, new_area.height);
738 gtk_paint_option (widget->style, widget->window,
739 state_type, shadow_type,
740 area, widget, "radiobutton",
741 x, y, indicator_size, indicator_size);
745 #define __GTK_RADIO_BUTTON_C__
746 #include "gtkaliasdef.c"