]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / gtk / gtkradiobutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28 #include "gtklabel.h"
29 #include "gtkmarshalers.h"
30 #include "gtkradiobutton.h"
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33
34 /**
35  * SECTION:gtkradiobutton
36  * @Short_description: A choice from multiple check buttons
37  * @Title: GtkRadioButton
38  * @See_also: #GtkComboBox
39  *
40  * A single radio button performs the same basic function as a #GtkCheckButton,
41  * as its position in the object hierarchy reflects. It is only when multiple
42  * radio buttons are grouped together that they become a different user
43  * interface component in their own right.
44  *
45  * Every radio button is a member of some group of radio buttons. When one is
46  * selected, all other radio buttons in the same group are deselected. A
47  * #GtkRadioButton is one way of giving the user a choice from many options.
48  *
49  * Radio button widgets are created with gtk_radio_button_new(), passing %NULL
50  * as the argument if this is the first radio button in a group. In subsequent
51  * calls, the group you wish to add this button to should be passed as an
52  * argument. Optionally, gtk_radio_button_new_with_label() can be used if you
53  * want a text label on the radio button.
54  *
55  * Alternatively, when adding widgets to an existing group of radio buttons,
56  * use gtk_radio_button_new_from_widget() with a #GtkRadioButton that already
57  * has a group assigned to it. The convenience function
58  * gtk_radio_button_new_with_label_from_widget() is also provided.
59  *
60  * To retrieve the group a #GtkRadioButton is assigned to, use
61  * gtk_radio_button_get_group().
62  *
63  * To remove a #GtkRadioButton from one group and make it part of a new one,
64  * use gtk_radio_button_set_group().
65  *
66  * The group list does not need to be freed, as each #GtkRadioButton will remove
67  * itself and its list item when it is destroyed.
68  *
69  * <example>
70  * <title>How to create a group of two radio buttons.</title>
71  * <programlisting>
72  * void create_radio_buttons (void) {
73  *
74  *    GtkWidget *window, *radio1, *radio2, *box, *entry;
75  *    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76  *    box = gtk_box_new (GTK_ORIENTATION_VERTICAL, TRUE, 2);
77  *
78  *    /&ast; Create a radio button with a GtkEntry widget &ast;/
79  *    radio1 = gtk_radio_button_new (NULL);
80  *    entry = gtk_entry_new (<!-- -->);
81  *    gtk_container_add (GTK_CONTAINER (radio1), entry);
82  *
83  *
84  *    /&ast; Create a radio button with a label &ast;/
85  *    radio2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio1),
86  *                                                          "I'm the second radio button.");
87  *
88  *    /&ast; Pack them into a box, then show all the widgets &ast;/
89  *    gtk_box_pack_start (GTK_BOX (box), radio1, TRUE, TRUE, 2);
90  *    gtk_box_pack_start (GTK_BOX (box), radio2, TRUE, TRUE, 2);
91  *    gtk_container_add (GTK_CONTAINER (window), box);
92  *    gtk_widget_show_all (window);
93  *    return;
94  * }
95  * </programlisting>
96  * </example>
97  *
98  * When an unselected button in the group is clicked the clicked button
99  * receives the #GtkToggleButton::toggled signal, as does the previously
100  * selected button.
101  * Inside the #GtkToggleButton::toggled handler, gtk_toggle_button_get_active()
102  * can be used to determine if the button has been selected or deselected.
103  */
104
105
106 struct _GtkRadioButtonPrivate
107 {
108   GSList *group;
109 };
110
111 enum {
112   PROP_0,
113   PROP_GROUP
114 };
115
116
117 static void     gtk_radio_button_destroy        (GtkWidget           *widget);
118 static gboolean gtk_radio_button_focus          (GtkWidget           *widget,
119                                                  GtkDirectionType     direction);
120 static void     gtk_radio_button_clicked        (GtkButton           *button);
121 static void     gtk_radio_button_draw_indicator (GtkCheckButton      *check_button,
122                                                  cairo_t             *cr);
123 static void     gtk_radio_button_set_property   (GObject             *object,
124                                                  guint                prop_id,
125                                                  const GValue        *value,
126                                                  GParamSpec          *pspec);
127 static void     gtk_radio_button_get_property   (GObject             *object,
128                                                  guint                prop_id,
129                                                  GValue              *value,
130                                                  GParamSpec          *pspec);
131
132 G_DEFINE_TYPE (GtkRadioButton, gtk_radio_button, GTK_TYPE_CHECK_BUTTON)
133
134 static guint group_changed_signal = 0;
135
136 static void
137 gtk_radio_button_class_init (GtkRadioButtonClass *class)
138 {
139   GObjectClass *gobject_class;
140   GtkButtonClass *button_class;
141   GtkCheckButtonClass *check_button_class;
142   GtkWidgetClass *widget_class;
143
144   gobject_class = G_OBJECT_CLASS (class);
145   widget_class = (GtkWidgetClass*) class;
146   button_class = (GtkButtonClass*) class;
147   check_button_class = (GtkCheckButtonClass*) class;
148
149   gobject_class->set_property = gtk_radio_button_set_property;
150   gobject_class->get_property = gtk_radio_button_get_property;
151
152   /**
153    * GtkRadioButton:group:
154    *
155    * Sets a new group for a radio button.
156    */
157   g_object_class_install_property (gobject_class,
158                                    PROP_GROUP,
159                                    g_param_spec_object ("group",
160                                                         P_("Group"),
161                                                         P_("The radio button whose group this widget belongs to."),
162                                                         GTK_TYPE_RADIO_BUTTON,
163                                                         GTK_PARAM_WRITABLE));
164   widget_class->destroy = gtk_radio_button_destroy;
165   widget_class->focus = gtk_radio_button_focus;
166
167   button_class->clicked = gtk_radio_button_clicked;
168
169   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
170
171   class->group_changed = NULL;
172
173   /**
174    * GtkRadioButton::group-changed:
175    * @style: the object which received the signal
176    *
177    * Emitted when the group of radio buttons that a radio button belongs
178    * to changes. This is emitted when a radio button switches from
179    * being alone to being part of a group of 2 or more buttons, or
180    * vice-versa, and when a button is moved from one group of 2 or
181    * more buttons to a different one, but not when the composition
182    * of the group that a button belongs to changes.
183    *
184    * Since: 2.4
185    */
186   group_changed_signal = g_signal_new (I_("group-changed"),
187                                        G_OBJECT_CLASS_TYPE (gobject_class),
188                                        G_SIGNAL_RUN_FIRST,
189                                        G_STRUCT_OFFSET (GtkRadioButtonClass, group_changed),
190                                        NULL, NULL,
191                                        _gtk_marshal_VOID__VOID,
192                                        G_TYPE_NONE, 0);
193
194   g_type_class_add_private (class, sizeof (GtkRadioButtonPrivate));
195 }
196
197 static void
198 gtk_radio_button_init (GtkRadioButton *radio_button)
199 {
200   GtkRadioButtonPrivate *priv;
201
202   radio_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_button,
203                                                     GTK_TYPE_RADIO_BUTTON,
204                                                     GtkRadioButtonPrivate);
205   priv = radio_button->priv;
206
207   gtk_widget_set_has_window (GTK_WIDGET (radio_button), FALSE);
208   gtk_widget_set_receives_default (GTK_WIDGET (radio_button), FALSE);
209
210   _gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), TRUE);
211
212   GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
213
214   priv->group = g_slist_prepend (NULL, radio_button);
215
216   _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
217   gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
218 }
219
220 static void
221 gtk_radio_button_set_property (GObject      *object,
222                                guint         prop_id,
223                                const GValue *value,
224                                GParamSpec   *pspec)
225 {
226   GtkRadioButton *radio_button;
227
228   radio_button = GTK_RADIO_BUTTON (object);
229
230   switch (prop_id)
231     {
232       GSList *slist;
233       GtkRadioButton *button;
234
235     case PROP_GROUP:
236         button = g_value_get_object (value);
237
238       if (button)
239         slist = gtk_radio_button_get_group (button);
240       else
241         slist = NULL;
242       gtk_radio_button_set_group (radio_button, slist);
243       break;
244     default:
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246       break;
247     }
248 }
249
250 static void
251 gtk_radio_button_get_property (GObject    *object,
252                                guint       prop_id,
253                                GValue     *value,
254                                GParamSpec *pspec)
255 {
256   switch (prop_id)
257     {
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261     }
262 }
263
264 /**
265  * gtk_radio_button_set_group:
266  * @radio_button: a #GtkRadioButton.
267  * @group: (transfer none) (element-type GtkRadioButton): an existing radio
268  *     button group, such as one returned from gtk_radio_button_get_group().
269  *
270  * Sets a #GtkRadioButton's group. It should be noted that this does not change
271  * the layout of your interface in any way, so if you are changing the group,
272  * it is likely you will need to re-arrange the user interface to reflect these
273  * changes.
274  */
275 void
276 gtk_radio_button_set_group (GtkRadioButton *radio_button,
277                             GSList         *group)
278 {
279   GtkRadioButtonPrivate *priv;
280   GtkWidget *old_group_singleton = NULL;
281   GtkWidget *new_group_singleton = NULL;
282
283   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
284   g_return_if_fail (!g_slist_find (group, radio_button));
285
286   priv = radio_button->priv;
287
288   if (priv->group)
289     {
290       GSList *slist;
291
292       priv->group = g_slist_remove (priv->group, radio_button);
293
294       if (priv->group && !priv->group->next)
295         old_group_singleton = g_object_ref (priv->group->data);
296
297       for (slist = priv->group; slist; slist = slist->next)
298         {
299           GtkRadioButton *tmp_button;
300           
301           tmp_button = slist->data;
302
303           tmp_button->priv->group = priv->group;
304         }
305     }
306   
307   if (group && !group->next)
308     new_group_singleton = g_object_ref (group->data);
309
310   priv->group = g_slist_prepend (group, radio_button);
311
312   if (group)
313     {
314       GSList *slist;
315       
316       for (slist = group; slist; slist = slist->next)
317         {
318           GtkRadioButton *tmp_button;
319           
320           tmp_button = slist->data;
321
322           tmp_button->priv->group = priv->group;
323         }
324     }
325
326   g_object_ref (radio_button);
327   
328   g_object_notify (G_OBJECT (radio_button), "group");
329   g_signal_emit (radio_button, group_changed_signal, 0);
330   if (old_group_singleton)
331     {
332       g_signal_emit (old_group_singleton, group_changed_signal, 0);
333       g_object_unref (old_group_singleton);
334     }
335   if (new_group_singleton)
336     {
337       g_signal_emit (new_group_singleton, group_changed_signal, 0);
338       g_object_unref (new_group_singleton);
339     }
340
341   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
342
343   g_object_unref (radio_button);
344 }
345
346 /**
347  * gtk_radio_button_join_group:
348  * @radio_button: the #GtkRadioButton object
349  * @group_source: (allow-none): a radio button object whos group we are 
350  *   joining, or %NULL to remove the radio button from its group
351  *
352  * Joins a #GtkRadioButton object to the group of another #GtkRadioButton object
353  *
354  * Use this in language bindings instead of the gtk_radio_button_get_group() 
355  * and gtk_radio_button_set_group() methods
356  *
357  * A common way to set up a group of radio buttons is the following:
358  * |[
359  *   GtkRadioButton *radio_button;
360  *   GtkRadioButton *last_button;
361  *
362  *   while (/&ast; more buttons to add &ast;/)
363  *     {
364  *        radio_button = gtk_radio_button_new (...);
365  *
366  *        gtk_radio_button_join_group (radio_button, last_button);
367  *        last_button = radio_button;
368  *     }
369  * ]|
370  *
371  * Since: 3.0
372  */
373 void
374 gtk_radio_button_join_group (GtkRadioButton *radio_button, 
375                              GtkRadioButton *group_source)
376 {
377   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
378   g_return_if_fail (group_source == NULL || GTK_IS_RADIO_BUTTON (group_source));
379
380   if (group_source)
381     {
382       GSList *group;
383       group = gtk_radio_button_get_group (group_source);
384
385       if (!group)
386         {
387           /* if we are not already part of a group we need to set up a new one
388              and then get the newly created group */
389           gtk_radio_button_set_group (group_source, NULL);
390           group = gtk_radio_button_get_group (group_source);
391         }
392
393       gtk_radio_button_set_group (radio_button, group);
394     }
395   else
396     {
397       gtk_radio_button_set_group (radio_button, NULL);
398     }
399 }
400
401 /**
402  * gtk_radio_button_new:
403  * @group: an existing radio button group, or %NULL if you are creating a new group.
404  *
405  * Creates a new #GtkRadioButton. To be of any practical value, a widget should
406  * then be packed into the radio button.
407  *
408  * Returns: a new radio button
409  */
410 GtkWidget*
411 gtk_radio_button_new (GSList *group)
412 {
413   GtkRadioButton *radio_button;
414
415   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
416
417   if (group)
418     gtk_radio_button_set_group (radio_button, group);
419
420   return GTK_WIDGET (radio_button);
421 }
422
423 /**
424  * gtk_radio_button_new_with_label:
425  * @group: an existing radio button group, or %NULL if you are creating a new
426  *  group.
427  * @label: the text label to display next to the radio button.
428  *
429  * Creates a new #GtkRadioButton with a text label.
430  *
431  * Returns: (transfer full): a new radio button.
432  */
433 GtkWidget*
434 gtk_radio_button_new_with_label (GSList      *group,
435                                  const gchar *label)
436 {
437   GtkWidget *radio_button;
438
439   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
440
441   if (group)
442     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
443
444   return radio_button;
445 }
446
447
448 /**
449  * gtk_radio_button_new_with_mnemonic:
450  * @group: the radio button group
451  * @label: the text of the button, with an underscore in front of the
452  *         mnemonic character
453  *
454  * Creates a new #GtkRadioButton containing a label, adding it to the same
455  * group as @group. The label will be created using
456  * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
457  * mnemonic for the button.
458  *
459  * Returns: (transfer full): a new #GtkRadioButton
460  */
461 GtkWidget*
462 gtk_radio_button_new_with_mnemonic (GSList      *group,
463                                     const gchar *label)
464 {
465   GtkWidget *radio_button;
466
467   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, 
468                                "label", label, 
469                                "use-underline", TRUE, 
470                                NULL);
471
472   if (group)
473     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
474
475   return radio_button;
476 }
477
478 /**
479  * gtk_radio_button_new_from_widget:
480  * @radio_group_member: an existing #GtkRadioButton.
481  *
482  * Creates a new #GtkRadioButton, adding it to the same group as
483  * @radio_group_member. As with gtk_radio_button_new(), a widget
484  * should be packed into the radio button.
485  *
486  * Returns: (transfer full): a new radio button.
487  */
488 GtkWidget*
489 gtk_radio_button_new_from_widget (GtkRadioButton *radio_group_member)
490 {
491   GSList *l = NULL;
492   if (radio_group_member)
493     l = gtk_radio_button_get_group (radio_group_member);
494   return gtk_radio_button_new (l);
495 }
496
497 /**
498  * gtk_radio_button_new_with_label_from_widget:
499  * @radio_group_member: widget to get radio group from or %NULL
500  * @label: a text string to display next to the radio button.
501  *
502  * Creates a new #GtkRadioButton with a text label, adding it to
503  * the same group as @radio_group_member.
504  *
505  * Returns: (transfer none): a new radio button.
506  */
507 GtkWidget*
508 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *radio_group_member,
509                                              const gchar    *label)
510 {
511   GSList *l = NULL;
512   if (radio_group_member)
513     l = gtk_radio_button_get_group (radio_group_member);
514   return gtk_radio_button_new_with_label (l, label);
515 }
516
517 /**
518  * gtk_radio_button_new_with_mnemonic_from_widget:
519  * @radio_group_member: (allow-none): widget to get radio group from or %NULL
520  * @label: the text of the button, with an underscore in front of the
521  *         mnemonic character
522  *
523  * Creates a new #GtkRadioButton containing a label. The label
524  * will be created using gtk_label_new_with_mnemonic(), so underscores
525  * in @label indicate the mnemonic for the button.
526  *
527  * Returns: (transfer full): a new #GtkRadioButton
528  **/
529 GtkWidget*
530 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *radio_group_member,
531                                                 const gchar    *label)
532 {
533   GSList *l = NULL;
534   if (radio_group_member)
535     l = gtk_radio_button_get_group (radio_group_member);
536   return gtk_radio_button_new_with_mnemonic (l, label);
537 }
538
539
540 /**
541  * gtk_radio_button_get_group:
542  * @radio_button: a #GtkRadioButton.
543  *
544  * Retrieves the group assigned to a radio button.
545  *
546  * Return value: (element-type GtkRadioButton) (transfer none): a linked list
547  * containing all the radio buttons in the same group
548  * as @radio_button. The returned list is owned by the radio button
549  * and must not be modified or freed.
550  */
551 GSList*
552 gtk_radio_button_get_group (GtkRadioButton *radio_button)
553 {
554   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
555
556   return radio_button->priv->group;
557 }
558
559
560 static void
561 gtk_radio_button_destroy (GtkWidget *widget)
562 {
563   GtkWidget *old_group_singleton = NULL;
564   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
565   GtkRadioButtonPrivate *priv = radio_button->priv;
566   GtkRadioButton *tmp_button;
567   GSList *tmp_list;
568   gboolean was_in_group;
569
570   was_in_group = priv->group && priv->group->next;
571
572   priv->group = g_slist_remove (priv->group, radio_button);
573   if (priv->group && !priv->group->next)
574     old_group_singleton = priv->group->data;
575
576   tmp_list = priv->group;
577
578   while (tmp_list)
579     {
580       tmp_button = tmp_list->data;
581       tmp_list = tmp_list->next;
582
583       tmp_button->priv->group = priv->group;
584     }
585
586   /* this button is no longer in the group */
587   priv->group = NULL;
588
589   if (old_group_singleton)
590     g_signal_emit (old_group_singleton, group_changed_signal, 0);
591   if (was_in_group)
592     g_signal_emit (radio_button, group_changed_signal, 0);
593
594   GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->destroy (widget);
595 }
596
597 static void
598 get_coordinates (GtkWidget    *widget,
599                  GtkWidget    *reference,
600                  gint         *x,
601                  gint         *y)
602 {
603   GtkAllocation allocation;
604
605   gtk_widget_get_allocation (widget, &allocation);
606   *x = allocation.x + allocation.width / 2;
607   *y = allocation.y + allocation.height / 2;
608
609   gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
610 }
611
612 static gint
613 left_right_compare (gconstpointer a,
614                     gconstpointer b,
615                     gpointer      data)
616 {
617   gint x1, y1, x2, y2;
618
619   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
620   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
621
622   if (y1 == y2)
623     return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
624   else
625     return (y1 < y2) ? -1 : 1;
626 }
627
628 static gint
629 up_down_compare (gconstpointer a,
630                  gconstpointer b,
631                  gpointer      data)
632 {
633   gint x1, y1, x2, y2;
634   
635   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
636   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
637   
638   if (x1 == x2)
639     return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
640   else
641     return (x1 < x2) ? -1 : 1;
642 }
643
644 static gboolean
645 gtk_radio_button_focus (GtkWidget         *widget,
646                         GtkDirectionType   direction)
647 {
648   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
649   GtkRadioButtonPrivate *priv = radio_button->priv;
650   GSList *tmp_slist;
651
652   /* Radio buttons with draw_indicator unset focus "normally", since
653    * they look like buttons to the user.
654    */
655   if (!gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget)))
656     return GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->focus (widget, direction);
657   
658   if (gtk_widget_is_focus (widget))
659     {
660       GtkSettings *settings = gtk_widget_get_settings (widget);
661       GSList *focus_list, *tmp_list;
662       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
663       GtkWidget *new_focus = NULL;
664       gboolean cursor_only;
665       gboolean wrap_around;
666
667       switch (direction)
668         {
669         case GTK_DIR_LEFT:
670         case GTK_DIR_RIGHT:
671           focus_list = g_slist_copy (priv->group);
672           focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
673           break;
674         case GTK_DIR_UP:
675         case GTK_DIR_DOWN:
676           focus_list = g_slist_copy (priv->group);
677           focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
678           break;
679         case GTK_DIR_TAB_FORWARD:
680         case GTK_DIR_TAB_BACKWARD:
681           /* fall through */
682         default:
683           return FALSE;
684         }
685
686       if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
687         focus_list = g_slist_reverse (focus_list);
688
689       tmp_list = g_slist_find (focus_list, widget);
690
691       if (tmp_list)
692         {
693           tmp_list = tmp_list->next;
694           
695           while (tmp_list)
696             {
697               GtkWidget *child = tmp_list->data;
698               
699               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
700                 {
701                   new_focus = child;
702                   break;
703                 }
704
705               tmp_list = tmp_list->next;
706             }
707         }
708
709       g_object_get (settings,
710                     "gtk-keynav-cursor-only", &cursor_only,
711                     "gtk-keynav-wrap-around", &wrap_around,
712                     NULL);
713
714       if (!new_focus)
715         {
716           if (cursor_only)
717             {
718               g_slist_free (focus_list);
719               return FALSE;
720             }
721
722           if (!wrap_around)
723             {
724               g_slist_free (focus_list);
725               gtk_widget_error_bell (widget);
726               return TRUE;
727             }
728
729           tmp_list = focus_list;
730
731           while (tmp_list)
732             {
733               GtkWidget *child = tmp_list->data;
734               
735               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
736                 {
737                   new_focus = child;
738                   break;
739                 }
740               
741               tmp_list = tmp_list->next;
742             }
743         }
744       
745       g_slist_free (focus_list);
746
747       if (new_focus)
748         {
749           gtk_widget_grab_focus (new_focus);
750
751           if (!cursor_only)
752             gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
753         }
754
755       return TRUE;
756     }
757   else
758     {
759       GtkRadioButton *selected_button = NULL;
760       
761       /* We accept the focus if, we don't have the focus and
762        *  - we are the currently active button in the group
763        *  - there is no currently active radio button.
764        */
765       
766       tmp_slist = priv->group;
767       while (tmp_slist)
768         {
769           if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (tmp_slist->data)))
770             selected_button = tmp_slist->data;
771           tmp_slist = tmp_slist->next;
772         }
773       
774       if (selected_button && selected_button != radio_button)
775         return FALSE;
776
777       gtk_widget_grab_focus (widget);
778       return TRUE;
779     }
780 }
781
782 static void
783 gtk_radio_button_clicked (GtkButton *button)
784 {
785   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (button);
786   GtkRadioButtonPrivate *priv = radio_button->priv;
787   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
788   GtkToggleButton *tmp_button;
789   GtkStateType new_state;
790   GSList *tmp_list;
791   gint toggled;
792   gboolean depressed;
793
794   toggled = FALSE;
795
796   g_object_ref (GTK_WIDGET (button));
797
798   if (gtk_toggle_button_get_active (toggle_button))
799     {
800       tmp_button = NULL;
801       tmp_list = priv->group;
802
803       while (tmp_list)
804         {
805           tmp_button = tmp_list->data;
806           tmp_list = tmp_list->next;
807
808           if (tmp_button != toggle_button &&
809               gtk_toggle_button_get_active (tmp_button))
810             break;
811
812           tmp_button = NULL;
813         }
814
815       if (!tmp_button)
816         {
817           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
818         }
819       else
820         {
821           toggled = TRUE;
822           _gtk_toggle_button_set_active (toggle_button,
823                                          !gtk_toggle_button_get_active (toggle_button));
824           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
825         }
826     }
827   else
828     {
829       toggled = TRUE;
830       _gtk_toggle_button_set_active (toggle_button,
831                                      !gtk_toggle_button_get_active (toggle_button));
832
833       tmp_list = priv->group;
834       while (tmp_list)
835         {
836           tmp_button = tmp_list->data;
837           tmp_list = tmp_list->next;
838
839           if (gtk_toggle_button_get_active (tmp_button) && (tmp_button != toggle_button))
840             {
841               gtk_button_clicked (GTK_BUTTON (tmp_button));
842               break;
843             }
844         }
845
846       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
847     }
848
849   if (gtk_toggle_button_get_inconsistent (toggle_button))
850     depressed = FALSE;
851   else if (button->in_button && button->button_down)
852     depressed = !gtk_toggle_button_get_active (toggle_button);
853   else
854     depressed = gtk_toggle_button_get_active (toggle_button);
855
856   if (gtk_widget_get_state (GTK_WIDGET (button)) != new_state)
857     gtk_widget_set_state (GTK_WIDGET (button), new_state);
858
859   if (toggled)
860     {
861       gtk_toggle_button_toggled (toggle_button);
862
863       g_object_notify (G_OBJECT (toggle_button), "active");
864     }
865
866   _gtk_button_set_depressed (button, depressed);
867
868   gtk_widget_queue_draw (GTK_WIDGET (button));
869
870   g_object_unref (button);
871 }
872
873 static void
874 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
875                                  cairo_t        *cr)
876 {
877   GtkAllocation allocation;
878   GtkWidget *widget;
879   GtkWidget *child;
880   GtkButton *button;
881   GtkToggleButton *toggle_button;
882   GtkStateType state_type;
883   GtkShadowType shadow_type;
884   GtkStyle *style;
885   GdkWindow *window;
886   gint x, y;
887   gint indicator_size, indicator_spacing;
888   gint focus_width;
889   gint focus_pad;
890   guint border_width;
891   gboolean interior_focus;
892
893   widget = GTK_WIDGET (check_button);
894   button = GTK_BUTTON (check_button);
895   toggle_button = GTK_TOGGLE_BUTTON (check_button);
896
897   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
898
899   style = gtk_widget_get_style (widget);
900   gtk_widget_style_get (widget,
901                         "interior-focus", &interior_focus,
902                         "focus-line-width", &focus_width,
903                         "focus-padding", &focus_pad,
904                         NULL);
905
906   window = gtk_widget_get_window (widget);
907
908   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
909
910   gtk_widget_get_allocation (widget, &allocation);
911
912   x = indicator_spacing + border_width;
913   y = (allocation.height - indicator_size) / 2;
914
915   child = gtk_bin_get_child (GTK_BIN (check_button));
916   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
917     x += focus_width + focus_pad;      
918
919   if (gtk_toggle_button_get_inconsistent (toggle_button))
920     shadow_type = GTK_SHADOW_ETCHED_IN;
921   else if (gtk_toggle_button_get_active (toggle_button))
922     shadow_type = GTK_SHADOW_IN;
923   else
924     shadow_type = GTK_SHADOW_OUT;
925
926   if (button->activate_timeout || (button->button_down && button->in_button))
927     state_type = GTK_STATE_ACTIVE;
928   else if (button->in_button)
929     state_type = GTK_STATE_PRELIGHT;
930   else if (!gtk_widget_is_sensitive (widget))
931     state_type = GTK_STATE_INSENSITIVE;
932   else
933     state_type = GTK_STATE_NORMAL;
934
935   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
936     x = allocation.width - (indicator_size + x);
937
938   if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
939     {
940       gtk_paint_flat_box (style, cr,
941                           GTK_STATE_PRELIGHT,
942                           GTK_SHADOW_ETCHED_OUT, 
943                           widget, "checkbutton",
944                           border_width, border_width,
945                           allocation.width - (2 * border_width),
946                           allocation.height - (2 * border_width));
947     }
948
949   gtk_paint_option (style, cr,
950                     state_type, shadow_type,
951                     widget, "radiobutton",
952                     x, y, indicator_size, indicator_size);
953 }