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