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