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_destroy (GtkObject *object);
43 static gboolean gtk_radio_button_focus (GtkWidget *widget,
44 GtkDirectionType direction);
45 static void gtk_radio_button_clicked (GtkButton *button);
46 static void gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
48 static void gtk_radio_button_set_property (GObject *object,
52 static void gtk_radio_button_get_property (GObject *object,
57 G_DEFINE_TYPE (GtkRadioButton, gtk_radio_button, GTK_TYPE_CHECK_BUTTON)
59 static guint group_changed_signal = 0;
62 gtk_radio_button_class_init (GtkRadioButtonClass *class)
64 GObjectClass *gobject_class;
65 GtkObjectClass *object_class;
66 GtkButtonClass *button_class;
67 GtkCheckButtonClass *check_button_class;
68 GtkWidgetClass *widget_class;
70 gobject_class = G_OBJECT_CLASS (class);
71 object_class = (GtkObjectClass*) class;
72 widget_class = (GtkWidgetClass*) class;
73 button_class = (GtkButtonClass*) class;
74 check_button_class = (GtkCheckButtonClass*) class;
76 gobject_class->set_property = gtk_radio_button_set_property;
77 gobject_class->get_property = gtk_radio_button_get_property;
79 g_object_class_install_property (gobject_class,
81 g_param_spec_object ("group",
83 P_("The radio button whose group this widget belongs to."),
84 GTK_TYPE_RADIO_BUTTON,
86 object_class->destroy = gtk_radio_button_destroy;
88 widget_class->focus = gtk_radio_button_focus;
90 button_class->clicked = gtk_radio_button_clicked;
92 check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
94 class->group_changed = NULL;
97 * GtkRadioButton::group-changed:
98 * @style: the object which received the signal
100 * Emitted when the group of radio buttons that a radio button belongs
101 * to changes. This is emitted when a radio button switches from
102 * being alone to being part of a group of 2 or more buttons, or
103 * vice-versa, and when a button is moved from one group of 2 or
104 * more buttons to a different one, but not when the composition
105 * of the group that a button belongs to changes.
109 group_changed_signal = g_signal_new (I_("group-changed"),
110 G_OBJECT_CLASS_TYPE (object_class),
112 G_STRUCT_OFFSET (GtkRadioButtonClass, group_changed),
114 _gtk_marshal_VOID__VOID,
119 gtk_radio_button_init (GtkRadioButton *radio_button)
121 GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
122 gtk_widget_set_receives_default (GTK_WIDGET (radio_button), FALSE);
124 GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
126 GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
128 radio_button->group = g_slist_prepend (NULL, radio_button);
130 _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
131 gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
135 gtk_radio_button_set_property (GObject *object,
140 GtkRadioButton *radio_button;
142 radio_button = GTK_RADIO_BUTTON (object);
147 GtkRadioButton *button;
150 button = g_value_get_object (value);
153 slist = gtk_radio_button_get_group (button);
156 gtk_radio_button_set_group (radio_button, slist);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gtk_radio_button_get_property (GObject *object,
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gtk_radio_button_set_group (GtkRadioButton *radio_button,
182 GtkWidget *old_group_singleton = NULL;
183 GtkWidget *new_group_singleton = NULL;
185 g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
186 g_return_if_fail (!g_slist_find (group, radio_button));
188 if (radio_button->group)
192 radio_button->group = g_slist_remove (radio_button->group, radio_button);
194 if (radio_button->group && !radio_button->group->next)
195 old_group_singleton = g_object_ref (radio_button->group->data);
197 for (slist = radio_button->group; slist; slist = slist->next)
199 GtkRadioButton *tmp_button;
201 tmp_button = slist->data;
203 tmp_button->group = radio_button->group;
207 if (group && !group->next)
208 new_group_singleton = g_object_ref (group->data);
210 radio_button->group = g_slist_prepend (group, radio_button);
216 for (slist = group; slist; slist = slist->next)
218 GtkRadioButton *tmp_button;
220 tmp_button = slist->data;
222 tmp_button->group = radio_button->group;
226 g_object_ref (radio_button);
228 g_object_notify (G_OBJECT (radio_button), "group");
229 g_signal_emit (radio_button, group_changed_signal, 0);
230 if (old_group_singleton)
232 g_signal_emit (old_group_singleton, group_changed_signal, 0);
233 g_object_unref (old_group_singleton);
235 if (new_group_singleton)
237 g_signal_emit (new_group_singleton, group_changed_signal, 0);
238 g_object_unref (new_group_singleton);
241 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
243 g_object_unref (radio_button);
247 gtk_radio_button_new (GSList *group)
249 GtkRadioButton *radio_button;
251 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
254 gtk_radio_button_set_group (radio_button, group);
256 return GTK_WIDGET (radio_button);
260 gtk_radio_button_new_with_label (GSList *group,
263 GtkWidget *radio_button;
265 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
268 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
275 * gtk_radio_button_new_with_mnemonic:
276 * @group: the radio button group
277 * @label: the text of the button, with an underscore in front of the
279 * @returns: a new #GtkRadioButton
281 * Creates a new #GtkRadioButton containing a label, adding it to the same
282 * group as @group. The label will be created using
283 * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
284 * mnemonic for the button.
287 gtk_radio_button_new_with_mnemonic (GSList *group,
290 GtkWidget *radio_button;
292 radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON,
294 "use-underline", TRUE,
298 gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
304 gtk_radio_button_new_from_widget (GtkRadioButton *radio_group_member)
307 if (radio_group_member)
308 l = gtk_radio_button_get_group (radio_group_member);
309 return gtk_radio_button_new (l);
314 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *radio_group_member,
318 if (radio_group_member)
319 l = gtk_radio_button_get_group (radio_group_member);
320 return gtk_radio_button_new_with_label (l, label);
324 * gtk_radio_button_new_with_mnemonic_from_widget:
325 * @radio_group_member: (allow-none): widget to get radio group from or %NULL
326 * @label: the text of the button, with an underscore in front of the
328 * @returns: a new #GtkRadioButton
330 * Creates a new #GtkRadioButton containing a label. The label
331 * will be created using gtk_label_new_with_mnemonic(), so underscores
332 * in @label indicate the mnemonic for the button.
335 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *radio_group_member,
339 if (radio_group_member)
340 l = gtk_radio_button_get_group (radio_group_member);
341 return gtk_radio_button_new_with_mnemonic (l, label);
346 * gtk_radio_button_get_group:
347 * @radio_button: a #GtkRadioButton.
349 * Retrieves the group assigned to a radio button.
351 * Return value: (element-type GtkRadioButton) (transfer none): a linked list
352 * containing all the radio buttons in the same group
353 * as @radio_button. The returned list is owned by the radio button
354 * and must not be modified or freed.
357 gtk_radio_button_get_group (GtkRadioButton *radio_button)
359 g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
361 return radio_button->group;
366 gtk_radio_button_destroy (GtkObject *object)
368 GtkWidget *old_group_singleton = NULL;
369 GtkRadioButton *radio_button;
370 GtkRadioButton *tmp_button;
372 gboolean was_in_group;
374 radio_button = GTK_RADIO_BUTTON (object);
376 was_in_group = radio_button->group && radio_button->group->next;
378 radio_button->group = g_slist_remove (radio_button->group, radio_button);
379 if (radio_button->group && !radio_button->group->next)
380 old_group_singleton = radio_button->group->data;
382 tmp_list = radio_button->group;
386 tmp_button = tmp_list->data;
387 tmp_list = tmp_list->next;
389 tmp_button->group = radio_button->group;
392 /* this button is no longer in the group */
393 radio_button->group = NULL;
395 if (old_group_singleton)
396 g_signal_emit (old_group_singleton, group_changed_signal, 0);
398 g_signal_emit (radio_button, group_changed_signal, 0);
400 GTK_OBJECT_CLASS (gtk_radio_button_parent_class)->destroy (object);
404 get_coordinates (GtkWidget *widget,
405 GtkWidget *reference,
409 *x = widget->allocation.x + widget->allocation.width / 2;
410 *y = widget->allocation.y + widget->allocation.height / 2;
412 gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
416 left_right_compare (gconstpointer a,
422 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
423 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
426 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
428 return (y1 < y2) ? -1 : 1;
432 up_down_compare (gconstpointer a,
438 get_coordinates ((GtkWidget *)a, data, &x1, &y1);
439 get_coordinates ((GtkWidget *)b, data, &x2, &y2);
442 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
444 return (x1 < x2) ? -1 : 1;
448 gtk_radio_button_focus (GtkWidget *widget,
449 GtkDirectionType direction)
451 GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
454 /* Radio buttons with draw_indicator unset focus "normally", since
455 * they look like buttons to the user.
457 if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
458 return GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->focus (widget, direction);
460 if (gtk_widget_is_focus (widget))
462 GtkSettings *settings = gtk_widget_get_settings (widget);
463 GSList *focus_list, *tmp_list;
464 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
465 GtkWidget *new_focus = NULL;
466 gboolean cursor_only;
467 gboolean wrap_around;
473 focus_list = g_slist_copy (radio_button->group);
474 focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
478 focus_list = g_slist_copy (radio_button->group);
479 focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
481 case GTK_DIR_TAB_FORWARD:
482 case GTK_DIR_TAB_BACKWARD:
488 if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
489 focus_list = g_slist_reverse (focus_list);
491 tmp_list = g_slist_find (focus_list, widget);
495 tmp_list = tmp_list->next;
499 GtkWidget *child = tmp_list->data;
501 if (GTK_WIDGET_MAPPED (child) && gtk_widget_is_sensitive (child))
507 tmp_list = tmp_list->next;
511 g_object_get (settings,
512 "gtk-keynav-cursor-only", &cursor_only,
513 "gtk-keynav-wrap-around", &wrap_around,
520 g_slist_free (focus_list);
526 g_slist_free (focus_list);
527 gtk_widget_error_bell (widget);
531 tmp_list = focus_list;
535 GtkWidget *child = tmp_list->data;
537 if (GTK_WIDGET_MAPPED (child) && gtk_widget_is_sensitive (child))
543 tmp_list = tmp_list->next;
547 g_slist_free (focus_list);
551 gtk_widget_grab_focus (new_focus);
554 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
561 GtkRadioButton *selected_button = NULL;
563 /* We accept the focus if, we don't have the focus and
564 * - we are the currently active button in the group
565 * - there is no currently active radio button.
568 tmp_slist = radio_button->group;
571 if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
572 selected_button = tmp_slist->data;
573 tmp_slist = tmp_slist->next;
576 if (selected_button && selected_button != radio_button)
579 gtk_widget_grab_focus (widget);
585 gtk_radio_button_clicked (GtkButton *button)
587 GtkToggleButton *toggle_button;
588 GtkRadioButton *radio_button;
589 GtkToggleButton *tmp_button;
590 GtkStateType new_state;
595 radio_button = GTK_RADIO_BUTTON (button);
596 toggle_button = GTK_TOGGLE_BUTTON (button);
599 g_object_ref (GTK_WIDGET (button));
601 if (toggle_button->active)
604 tmp_list = radio_button->group;
608 tmp_button = tmp_list->data;
609 tmp_list = tmp_list->next;
611 if (tmp_button->active && tmp_button != toggle_button)
619 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
624 toggle_button->active = !toggle_button->active;
625 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
631 toggle_button->active = !toggle_button->active;
633 tmp_list = radio_button->group;
636 tmp_button = tmp_list->data;
637 tmp_list = tmp_list->next;
639 if (tmp_button->active && (tmp_button != toggle_button))
641 gtk_button_clicked (GTK_BUTTON (tmp_button));
646 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
649 if (toggle_button->inconsistent)
651 else if (button->in_button && button->button_down)
652 depressed = !toggle_button->active;
654 depressed = toggle_button->active;
656 if (GTK_WIDGET_STATE (button) != new_state)
657 gtk_widget_set_state (GTK_WIDGET (button), new_state);
661 gtk_toggle_button_toggled (toggle_button);
663 g_object_notify (G_OBJECT (toggle_button), "active");
666 _gtk_button_set_depressed (button, depressed);
668 gtk_widget_queue_draw (GTK_WIDGET (button));
670 g_object_unref (button);
674 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
680 GtkToggleButton *toggle_button;
681 GtkStateType state_type;
682 GtkShadowType shadow_type;
684 gint indicator_size, indicator_spacing;
687 gboolean interior_focus;
689 widget = GTK_WIDGET (check_button);
691 if (gtk_widget_is_drawable (widget))
693 button = GTK_BUTTON (check_button);
694 toggle_button = GTK_TOGGLE_BUTTON (check_button);
696 gtk_widget_style_get (widget,
697 "interior-focus", &interior_focus,
698 "focus-line-width", &focus_width,
699 "focus-padding", &focus_pad,
702 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
704 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
705 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
707 child = GTK_BIN (check_button)->child;
708 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
709 x += focus_width + focus_pad;
711 if (toggle_button->inconsistent)
712 shadow_type = GTK_SHADOW_ETCHED_IN;
713 else if (toggle_button->active)
714 shadow_type = GTK_SHADOW_IN;
716 shadow_type = GTK_SHADOW_OUT;
718 if (button->activate_timeout || (button->button_down && button->in_button))
719 state_type = GTK_STATE_ACTIVE;
720 else if (button->in_button)
721 state_type = GTK_STATE_PRELIGHT;
722 else if (!gtk_widget_is_sensitive (widget))
723 state_type = GTK_STATE_INSENSITIVE;
725 state_type = GTK_STATE_NORMAL;
727 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
728 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
730 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
732 GdkRectangle restrict_area;
733 GdkRectangle new_area;
735 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
736 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
737 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
738 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
740 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
742 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
743 GTK_SHADOW_ETCHED_OUT,
744 area, widget, "checkbutton",
745 new_area.x, new_area.y,
746 new_area.width, new_area.height);
750 gtk_paint_option (widget->style, widget->window,
751 state_type, shadow_type,
752 area, widget, "radiobutton",
753 x, y, indicator_size, indicator_size);
757 #define __GTK_RADIO_BUTTON_C__
758 #include "gtkaliasdef.c"