]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
Merge branch 'master' into treeview-refactor
[~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
37 /**
38  * SECTION:gtkradiobutton
39  * @Short_description: A choice from multiple check buttons
40  * @Title: GtkRadioButton
41  * @See_also: #GtkComboBox
42  *
43  * A single radio button performs the same basic function as a #GtkCheckButton,
44  * as its position in the object hierarchy reflects. It is only when multiple
45  * radio buttons are grouped together that they become a different user
46  * interface component in their own right.
47  *
48  * Every radio button is a member of some group of radio buttons. When one is
49  * selected, all other radio buttons in the same group are deselected. A
50  * #GtkRadioButton is one way of giving the user a choice from many options.
51  *
52  * Radio button widgets are created with gtk_radio_button_new(), passing %NULL
53  * as the argument if this is the first radio button in a group. In subsequent
54  * calls, the group you wish to add this button to should be passed as an
55  * argument. Optionally, gtk_radio_button_new_with_label() can be used if you
56  * want a text label on the radio button.
57  *
58  * Alternatively, when adding widgets to an existing group of radio buttons,
59  * use gtk_radio_button_new_from_widget() with a #GtkRadioButton that already
60  * has a group assigned to it. The convenience function
61  * gtk_radio_button_new_with_label_from_widget() is also provided.
62  *
63  * To retrieve the group a #GtkRadioButton is assigned to, use
64  * gtk_radio_button_get_group().
65  *
66  * To remove a #GtkRadioButton from one group and make it part of a new one,
67  * use gtk_radio_button_set_group().
68  *
69  * The group list does not need to be freed, as each #GtkRadioButton will remove
70  * itself and its list item when it is destroyed.
71  *
72  * <example>
73  * <title>How to create a group of two radio buttons.</title>
74  * <programlisting>
75  * void create_radio_buttons (void) {
76  *
77  *    GtkWidget *window, *radio1, *radio2, *box, *entry;
78  *    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
79  *    box = gtk_box_new (GTK_ORIENTATION_VERTICAL, TRUE, 2);
80  *
81  *    /&ast; Create a radio button with a GtkEntry widget &ast;/
82  *    radio1 = gtk_radio_button_new (NULL);
83  *    entry = gtk_entry_new (<!-- -->);
84  *    gtk_container_add (GTK_CONTAINER (radio1), entry);
85  *
86  *
87  *    /&ast; Create a radio button with a label &ast;/
88  *    radio2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio1),
89  *                                                          "I'm the second radio button.");
90  *
91  *    /&ast; Pack them into a box, then show all the widgets &ast;/
92  *    gtk_box_pack_start (GTK_BOX (box), radio1, TRUE, TRUE, 2);
93  *    gtk_box_pack_start (GTK_BOX (box), radio2, TRUE, TRUE, 2);
94  *    gtk_container_add (GTK_CONTAINER (window), box);
95  *    gtk_widget_show_all (window);
96  *    return;
97  * }
98  * </programlisting>
99  * </example>
100  *
101  * When an unselected button in the group is clicked the clicked button
102  * receives the #GtkToggleButton::toggled signal, as does the previously
103  * selected button.
104  * Inside the #GtkToggleButton::toggled handler, gtk_toggle_button_get_active()
105  * can be used to determine if the button has been selected or deselected.
106  */
107
108
109 struct _GtkRadioButtonPrivate
110 {
111   GSList *group;
112 };
113
114 enum {
115   PROP_0,
116   PROP_GROUP
117 };
118
119
120 static void     gtk_radio_button_destroy        (GtkWidget           *widget);
121 static gboolean gtk_radio_button_focus          (GtkWidget           *widget,
122                                                  GtkDirectionType     direction);
123 static void     gtk_radio_button_clicked        (GtkButton           *button);
124 static void     gtk_radio_button_draw_indicator (GtkCheckButton      *check_button,
125                                                  cairo_t             *cr);
126 static void     gtk_radio_button_set_property   (GObject             *object,
127                                                  guint                prop_id,
128                                                  const GValue        *value,
129                                                  GParamSpec          *pspec);
130 static void     gtk_radio_button_get_property   (GObject             *object,
131                                                  guint                prop_id,
132                                                  GValue              *value,
133                                                  GParamSpec          *pspec);
134
135 G_DEFINE_TYPE (GtkRadioButton, gtk_radio_button, GTK_TYPE_CHECK_BUTTON)
136
137 static guint group_changed_signal = 0;
138
139 static void
140 gtk_radio_button_class_init (GtkRadioButtonClass *class)
141 {
142   GObjectClass *gobject_class;
143   GtkButtonClass *button_class;
144   GtkCheckButtonClass *check_button_class;
145   GtkWidgetClass *widget_class;
146
147   gobject_class = G_OBJECT_CLASS (class);
148   widget_class = (GtkWidgetClass*) class;
149   button_class = (GtkButtonClass*) class;
150   check_button_class = (GtkCheckButtonClass*) class;
151
152   gobject_class->set_property = gtk_radio_button_set_property;
153   gobject_class->get_property = gtk_radio_button_get_property;
154
155   /**
156    * GtkRadioButton:group:
157    *
158    * Sets a new group for a radio button.
159    */
160   g_object_class_install_property (gobject_class,
161                                    PROP_GROUP,
162                                    g_param_spec_object ("group",
163                                                         P_("Group"),
164                                                         P_("The radio button whose group this widget belongs to."),
165                                                         GTK_TYPE_RADIO_BUTTON,
166                                                         GTK_PARAM_WRITABLE));
167   widget_class->destroy = gtk_radio_button_destroy;
168   widget_class->focus = gtk_radio_button_focus;
169
170   button_class->clicked = gtk_radio_button_clicked;
171
172   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
173
174   class->group_changed = NULL;
175
176   /**
177    * GtkRadioButton::group-changed:
178    * @style: the object which received the signal
179    *
180    * Emitted when the group of radio buttons that a radio button belongs
181    * to changes. This is emitted when a radio button switches from
182    * being alone to being part of a group of 2 or more buttons, or
183    * vice-versa, and when a button is moved from one group of 2 or
184    * more buttons to a different one, but not when the composition
185    * of the group that a button belongs to changes.
186    *
187    * Since: 2.4
188    */
189   group_changed_signal = g_signal_new (I_("group-changed"),
190                                        G_OBJECT_CLASS_TYPE (gobject_class),
191                                        G_SIGNAL_RUN_FIRST,
192                                        G_STRUCT_OFFSET (GtkRadioButtonClass, group_changed),
193                                        NULL, NULL,
194                                        _gtk_marshal_VOID__VOID,
195                                        G_TYPE_NONE, 0);
196
197   g_type_class_add_private (class, sizeof (GtkRadioButtonPrivate));
198 }
199
200 static void
201 gtk_radio_button_init (GtkRadioButton *radio_button)
202 {
203   GtkRadioButtonPrivate *priv;
204
205   radio_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_button,
206                                                     GTK_TYPE_RADIO_BUTTON,
207                                                     GtkRadioButtonPrivate);
208   priv = radio_button->priv;
209
210   gtk_widget_set_has_window (GTK_WIDGET (radio_button), FALSE);
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 (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
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: (allow-none): an existing radio button group, or %NULL if you are
407  *  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: (allow-none): an existing radio button group, or %NULL if you are
430  *  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: (transfer full): 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: (allow-none): the radio button group
455  * @label: the text of the button, with an underscore in front of the
456  *         mnemonic character
457  *
458  * Creates a new #GtkRadioButton containing a label, adding it to the same
459  * group as @group. The label will be created using
460  * gtk_label_new_with_mnemonic(), so underscores in @label indicate the
461  * mnemonic for the button.
462  *
463  * Returns: (transfer full): a new #GtkRadioButton
464  */
465 GtkWidget*
466 gtk_radio_button_new_with_mnemonic (GSList      *group,
467                                     const gchar *label)
468 {
469   GtkWidget *radio_button;
470
471   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, 
472                                "label", label, 
473                                "use-underline", TRUE, 
474                                NULL);
475
476   if (group)
477     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
478
479   return radio_button;
480 }
481
482 /**
483  * gtk_radio_button_new_from_widget:
484  * @radio_group_member: (allow-none): an existing #GtkRadioButton.
485  *
486  * Creates a new #GtkRadioButton, adding it to the same group as
487  * @radio_group_member. As with gtk_radio_button_new(), a widget
488  * should be packed into the radio button.
489  *
490  * Returns: (transfer full): a new radio button.
491  */
492 GtkWidget*
493 gtk_radio_button_new_from_widget (GtkRadioButton *radio_group_member)
494 {
495   GSList *l = NULL;
496   if (radio_group_member)
497     l = gtk_radio_button_get_group (radio_group_member);
498   return gtk_radio_button_new (l);
499 }
500
501 /**
502  * gtk_radio_button_new_with_label_from_widget:
503  * @radio_group_member: (allow-none): widget to get radio group from or %NULL
504  * @label: a text string to display next to the radio button.
505  *
506  * Creates a new #GtkRadioButton with a text label, adding it to
507  * the same group as @radio_group_member.
508  *
509  * Returns: (transfer none): a new radio button.
510  */
511 GtkWidget*
512 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *radio_group_member,
513                                              const gchar    *label)
514 {
515   GSList *l = NULL;
516   if (radio_group_member)
517     l = gtk_radio_button_get_group (radio_group_member);
518   return gtk_radio_button_new_with_label (l, label);
519 }
520
521 /**
522  * gtk_radio_button_new_with_mnemonic_from_widget:
523  * @radio_group_member: (allow-none): widget to get radio group from or %NULL
524  * @label: the text of the button, with an underscore in front of the
525  *         mnemonic character
526  *
527  * Creates a new #GtkRadioButton containing a label. The label
528  * will be created using gtk_label_new_with_mnemonic(), so underscores
529  * in @label indicate the mnemonic for the button.
530  *
531  * Returns: (transfer full): a new #GtkRadioButton
532  **/
533 GtkWidget*
534 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *radio_group_member,
535                                                 const gchar    *label)
536 {
537   GSList *l = NULL;
538   if (radio_group_member)
539     l = gtk_radio_button_get_group (radio_group_member);
540   return gtk_radio_button_new_with_mnemonic (l, label);
541 }
542
543
544 /**
545  * gtk_radio_button_get_group:
546  * @radio_button: a #GtkRadioButton.
547  *
548  * Retrieves the group assigned to a radio button.
549  *
550  * Return value: (element-type GtkRadioButton) (transfer none): a linked list
551  * containing all the radio buttons in the same group
552  * as @radio_button. The returned list is owned by the radio button
553  * and must not be modified or freed.
554  */
555 GSList*
556 gtk_radio_button_get_group (GtkRadioButton *radio_button)
557 {
558   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
559
560   return radio_button->priv->group;
561 }
562
563
564 static void
565 gtk_radio_button_destroy (GtkWidget *widget)
566 {
567   GtkWidget *old_group_singleton = NULL;
568   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
569   GtkRadioButtonPrivate *priv = radio_button->priv;
570   GtkRadioButton *tmp_button;
571   GSList *tmp_list;
572   gboolean was_in_group;
573
574   was_in_group = priv->group && priv->group->next;
575
576   priv->group = g_slist_remove (priv->group, radio_button);
577   if (priv->group && !priv->group->next)
578     old_group_singleton = priv->group->data;
579
580   tmp_list = priv->group;
581
582   while (tmp_list)
583     {
584       tmp_button = tmp_list->data;
585       tmp_list = tmp_list->next;
586
587       tmp_button->priv->group = priv->group;
588     }
589
590   /* this button is no longer in the group */
591   priv->group = NULL;
592
593   if (old_group_singleton)
594     g_signal_emit (old_group_singleton, group_changed_signal, 0);
595   if (was_in_group)
596     g_signal_emit (radio_button, group_changed_signal, 0);
597
598   GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->destroy (widget);
599 }
600
601 static void
602 get_coordinates (GtkWidget    *widget,
603                  GtkWidget    *reference,
604                  gint         *x,
605                  gint         *y)
606 {
607   GtkAllocation allocation;
608
609   gtk_widget_get_allocation (widget, &allocation);
610   *x = allocation.x + allocation.width / 2;
611   *y = allocation.y + allocation.height / 2;
612
613   gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
614 }
615
616 static gint
617 left_right_compare (gconstpointer a,
618                     gconstpointer b,
619                     gpointer      data)
620 {
621   gint x1, y1, x2, y2;
622
623   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
624   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
625
626   if (y1 == y2)
627     return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
628   else
629     return (y1 < y2) ? -1 : 1;
630 }
631
632 static gint
633 up_down_compare (gconstpointer a,
634                  gconstpointer b,
635                  gpointer      data)
636 {
637   gint x1, y1, x2, y2;
638   
639   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
640   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
641   
642   if (x1 == x2)
643     return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
644   else
645     return (x1 < x2) ? -1 : 1;
646 }
647
648 static gboolean
649 gtk_radio_button_focus (GtkWidget         *widget,
650                         GtkDirectionType   direction)
651 {
652   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
653   GtkRadioButtonPrivate *priv = radio_button->priv;
654   GSList *tmp_slist;
655
656   /* Radio buttons with draw_indicator unset focus "normally", since
657    * they look like buttons to the user.
658    */
659   if (!gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget)))
660     return GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->focus (widget, direction);
661   
662   if (gtk_widget_is_focus (widget))
663     {
664       GtkSettings *settings = gtk_widget_get_settings (widget);
665       GSList *focus_list, *tmp_list;
666       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
667       GtkWidget *new_focus = NULL;
668       gboolean cursor_only;
669       gboolean wrap_around;
670
671       switch (direction)
672         {
673         case GTK_DIR_LEFT:
674         case GTK_DIR_RIGHT:
675           focus_list = g_slist_copy (priv->group);
676           focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
677           break;
678         case GTK_DIR_UP:
679         case GTK_DIR_DOWN:
680           focus_list = g_slist_copy (priv->group);
681           focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
682           break;
683         case GTK_DIR_TAB_FORWARD:
684         case GTK_DIR_TAB_BACKWARD:
685           /* fall through */
686         default:
687           return FALSE;
688         }
689
690       if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
691         focus_list = g_slist_reverse (focus_list);
692
693       tmp_list = g_slist_find (focus_list, widget);
694
695       if (tmp_list)
696         {
697           tmp_list = tmp_list->next;
698           
699           while (tmp_list)
700             {
701               GtkWidget *child = tmp_list->data;
702               
703               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
704                 {
705                   new_focus = child;
706                   break;
707                 }
708
709               tmp_list = tmp_list->next;
710             }
711         }
712
713       g_object_get (settings,
714                     "gtk-keynav-cursor-only", &cursor_only,
715                     "gtk-keynav-wrap-around", &wrap_around,
716                     NULL);
717
718       if (!new_focus)
719         {
720           if (cursor_only)
721             {
722               g_slist_free (focus_list);
723               return FALSE;
724             }
725
726           if (!wrap_around)
727             {
728               g_slist_free (focus_list);
729               gtk_widget_error_bell (widget);
730               return TRUE;
731             }
732
733           tmp_list = focus_list;
734
735           while (tmp_list)
736             {
737               GtkWidget *child = tmp_list->data;
738               
739               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
740                 {
741                   new_focus = child;
742                   break;
743                 }
744               
745               tmp_list = tmp_list->next;
746             }
747         }
748       
749       g_slist_free (focus_list);
750
751       if (new_focus)
752         {
753           gtk_widget_grab_focus (new_focus);
754
755           if (!cursor_only)
756             gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
757         }
758
759       return TRUE;
760     }
761   else
762     {
763       GtkRadioButton *selected_button = NULL;
764       
765       /* We accept the focus if, we don't have the focus and
766        *  - we are the currently active button in the group
767        *  - there is no currently active radio button.
768        */
769       
770       tmp_slist = priv->group;
771       while (tmp_slist)
772         {
773           if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (tmp_slist->data)))
774             selected_button = tmp_slist->data;
775           tmp_slist = tmp_slist->next;
776         }
777       
778       if (selected_button && selected_button != radio_button)
779         return FALSE;
780
781       gtk_widget_grab_focus (widget);
782       return TRUE;
783     }
784 }
785
786 static void
787 gtk_radio_button_clicked (GtkButton *button)
788 {
789   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (button);
790   GtkRadioButtonPrivate *priv = radio_button->priv;
791   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
792   GtkToggleButton *tmp_button;
793   GtkStateType new_state;
794   GSList *tmp_list;
795   gint toggled;
796   gboolean depressed;
797
798   toggled = FALSE;
799
800   g_object_ref (GTK_WIDGET (button));
801
802   if (gtk_toggle_button_get_active (toggle_button))
803     {
804       tmp_button = NULL;
805       tmp_list = priv->group;
806
807       while (tmp_list)
808         {
809           tmp_button = tmp_list->data;
810           tmp_list = tmp_list->next;
811
812           if (tmp_button != toggle_button &&
813               gtk_toggle_button_get_active (tmp_button))
814             break;
815
816           tmp_button = NULL;
817         }
818
819       if (!tmp_button)
820         {
821           new_state = (button->priv->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
822         }
823       else
824         {
825           toggled = TRUE;
826           _gtk_toggle_button_set_active (toggle_button,
827                                          !gtk_toggle_button_get_active (toggle_button));
828           new_state = (button->priv->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
829         }
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       tmp_list = priv->group;
838       while (tmp_list)
839         {
840           tmp_button = tmp_list->data;
841           tmp_list = tmp_list->next;
842
843           if (gtk_toggle_button_get_active (tmp_button) && (tmp_button != toggle_button))
844             {
845               gtk_button_clicked (GTK_BUTTON (tmp_button));
846               break;
847             }
848         }
849
850       new_state = (button->priv->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
851     }
852
853   if (gtk_toggle_button_get_inconsistent (toggle_button))
854     depressed = FALSE;
855   else if (button->priv->in_button && button->priv->button_down)
856     depressed = !gtk_toggle_button_get_active (toggle_button);
857   else
858     depressed = gtk_toggle_button_get_active (toggle_button);
859
860   if (gtk_widget_get_state (GTK_WIDGET (button)) != new_state)
861     gtk_widget_set_state (GTK_WIDGET (button), new_state);
862
863   if (toggled)
864     {
865       gtk_toggle_button_toggled (toggle_button);
866
867       g_object_notify (G_OBJECT (toggle_button), "active");
868     }
869
870   _gtk_button_set_depressed (button, depressed);
871
872   gtk_widget_queue_draw (GTK_WIDGET (button));
873
874   g_object_unref (button);
875 }
876
877 static void
878 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
879                                  cairo_t        *cr)
880 {
881   GtkAllocation allocation;
882   GtkWidget *widget;
883   GtkWidget *child;
884   GtkButton *button;
885   GtkToggleButton *toggle_button;
886   GtkStateType state_type;
887   GtkShadowType shadow_type;
888   GtkStyle *style;
889   GdkWindow *window;
890   gint x, y;
891   gint indicator_size, indicator_spacing;
892   gint focus_width;
893   gint focus_pad;
894   guint border_width;
895   gboolean interior_focus;
896
897   widget = GTK_WIDGET (check_button);
898   button = GTK_BUTTON (check_button);
899   toggle_button = GTK_TOGGLE_BUTTON (check_button);
900
901   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
902
903   style = gtk_widget_get_style (widget);
904   gtk_widget_style_get (widget,
905                         "interior-focus", &interior_focus,
906                         "focus-line-width", &focus_width,
907                         "focus-padding", &focus_pad,
908                         NULL);
909
910   window = gtk_widget_get_window (widget);
911
912   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
913
914   gtk_widget_get_allocation (widget, &allocation);
915
916   x = indicator_spacing + border_width;
917   y = (allocation.height - indicator_size) / 2;
918
919   child = gtk_bin_get_child (GTK_BIN (check_button));
920   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
921     x += focus_width + focus_pad;      
922
923   if (gtk_toggle_button_get_inconsistent (toggle_button))
924     shadow_type = GTK_SHADOW_ETCHED_IN;
925   else if (gtk_toggle_button_get_active (toggle_button))
926     shadow_type = GTK_SHADOW_IN;
927   else
928     shadow_type = GTK_SHADOW_OUT;
929
930   if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
931     state_type = GTK_STATE_ACTIVE;
932   else if (button->priv->in_button)
933     state_type = GTK_STATE_PRELIGHT;
934   else if (!gtk_widget_is_sensitive (widget))
935     state_type = GTK_STATE_INSENSITIVE;
936   else
937     state_type = GTK_STATE_NORMAL;
938
939   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
940     x = allocation.width - (indicator_size + x);
941
942   if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
943     {
944       gtk_paint_flat_box (style, cr,
945                           GTK_STATE_PRELIGHT,
946                           GTK_SHADOW_ETCHED_OUT, 
947                           widget, "checkbutton",
948                           border_width, border_width,
949                           allocation.width - (2 * border_width),
950                           allocation.height - (2 * border_width));
951     }
952
953   gtk_paint_option (style, cr,
954                     state_type, shadow_type,
955                     widget, "radiobutton",
956                     x, y, indicator_size, indicator_size);
957 }