]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
Use GtkFooPrivate instead GtkFooPriv
[~andy/gtk] / gtk / gtkradiobutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include "gtklabel.h"
29 #include "gtkmarshalers.h"
30 #include "gtkradiobutton.h"
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33
34 /**
35  * SECTION:gtkradiobutton
36  * @Short_description: A choice from multiple check buttons
37  * @Title: GtkRadioButton
38  * @See_also: #GtkComboBox
39  *
40  * A single radio button performs the same basic function as a #GtkCheckButton,
41  * as its position in the object hierarchy reflects. It is only when multiple
42  * radio buttons are grouped together that they become a different user
43  * interface component in their own right.
44  *
45  * Every radio button is a member of some group of radio buttons. When one is
46  * selected, all other radio buttons in the same group are deselected. A
47  * #GtkRadioButton is one way of giving the user a choice from many options.
48  *
49  * Radio button widgets are created with gtk_radio_button_new(), passing %NULL
50  * as the argument if this is the first radio button in a group. In subsequent
51  * calls, the group you wish to add this button to should be passed as an
52  * argument. Optionally, gtk_radio_button_new_with_label() can be used if you
53  * want a text label on the radio button.
54  *
55  * Alternatively, when adding widgets to an existing group of radio buttons,
56  * use gtk_radio_button_new_from_widget() with a #GtkRadioButton that already
57  * has a group assigned to it. The convenience function
58  * gtk_radio_button_new_with_label_from_widget() is also provided.
59  *
60  * To retrieve the group a #GtkRadioButton is assigned to, use
61  * gtk_radio_button_get_group().
62  *
63  * To remove a #GtkRadioButton from one group and make it part of a new one,
64  * use gtk_radio_button_set_group().
65  *
66  * The group list does not need to be freed, as each #GtkRadioButton will remove
67  * itself and its list item when it is destroyed.
68  *
69  * <example>
70  * <title>How to create a group of two radio buttons.</title>
71  * <programlisting>
72  * void create_radio_buttons (void) {
73  *
74  *    GtkWidget *window, *radio1, *radio2, *box, *entry;
75  *    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76  *    box = gtk_vbox_new (TRUE, 2);
77  *
78  *    /&ast; Create a radio button with a GtkEntry widget &ast;/
79  *    radio1 = gtk_radio_button_new (NULL);
80  *    entry = gtk_entry_new (<!-- -->);
81  *    gtk_container_add (GTK_CONTAINER (radio1), entry);
82  *
83  *
84  *    /&ast; Create a radio button with a label &ast;/
85  *    radio2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio1),
86  *                                                          "I'm the second radio button.");
87  *
88  *    /&ast; Pack them into a box, then show all the widgets &ast;/
89  *    gtk_box_pack_start (GTK_BOX (box), radio1, TRUE, TRUE, 2);
90  *    gtk_box_pack_start (GTK_BOX (box), radio2, TRUE, TRUE, 2);
91  *    gtk_container_add (GTK_CONTAINER (window), box);
92  *    gtk_widget_show_all (window);
93  *    return;
94  * }
95  * </programlisting>
96  * </example>
97  *
98  * When an unselected button in the group is clicked the clicked button
99  * receives the #GtkToggleButton::toggled signal, as does the previously
100  * selected button.
101  * Inside the #GtkToggleButton::toggled handler, gtk_toggle_button_get_active()
102  * can be used to determine if the button has been selected or deselected.
103  */
104
105
106 struct _GtkRadioButtonPrivate
107 {
108   GSList *group;
109 };
110
111 enum {
112   PROP_0,
113   PROP_GROUP
114 };
115
116
117 static void     gtk_radio_button_destroy        (GtkObject           *object);
118 static gboolean gtk_radio_button_focus          (GtkWidget           *widget,
119                                                  GtkDirectionType     direction);
120 static void     gtk_radio_button_clicked        (GtkButton           *button);
121 static void     gtk_radio_button_draw_indicator (GtkCheckButton      *check_button,
122                                                  GdkRectangle        *area);
123 static void     gtk_radio_button_set_property   (GObject             *object,
124                                                  guint                prop_id,
125                                                  const GValue        *value,
126                                                  GParamSpec          *pspec);
127 static void     gtk_radio_button_get_property   (GObject             *object,
128                                                  guint                prop_id,
129                                                  GValue              *value,
130                                                  GParamSpec          *pspec);
131
132 G_DEFINE_TYPE (GtkRadioButton, gtk_radio_button, GTK_TYPE_CHECK_BUTTON)
133
134 static guint group_changed_signal = 0;
135
136 static void
137 gtk_radio_button_class_init (GtkRadioButtonClass *class)
138 {
139   GObjectClass *gobject_class;
140   GtkObjectClass *object_class;
141   GtkButtonClass *button_class;
142   GtkCheckButtonClass *check_button_class;
143   GtkWidgetClass *widget_class;
144
145   gobject_class = G_OBJECT_CLASS (class);
146   object_class = (GtkObjectClass*) 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   object_class->destroy = gtk_radio_button_destroy;
167
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 (object_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 (radio_button)->active = TRUE;
214
215   GTK_BUTTON (radio_button)->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: an existing radio button group, such as one returned from
271  *  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_new:
351  * @group: an existing radio button group, or %NULL if you are creating a new group.
352  *
353  * Creates a new #GtkRadioButton. To be of any practical value, a widget should
354  * then be packed into the radio button.
355  *
356  * Returns: a new radio button.
357  */
358 GtkWidget*
359 gtk_radio_button_new (GSList *group)
360 {
361   GtkRadioButton *radio_button;
362
363   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
364
365   if (group)
366     gtk_radio_button_set_group (radio_button, group);
367
368   return GTK_WIDGET (radio_button);
369 }
370
371 /**
372  * gtk_radio_button_new_with_label:
373  * @group: an existing radio button group, or %NULL if you are creating a new
374  *  group.
375  * @label: the text label to display next to the radio button.
376  *
377  * Creates a new #GtkRadioButton with a text label.
378  *
379  * Returns: a new radio button.
380  */
381 GtkWidget*
382 gtk_radio_button_new_with_label (GSList      *group,
383                                  const gchar *label)
384 {
385   GtkWidget *radio_button;
386
387   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
388
389   if (group)
390     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
391
392   return radio_button;
393 }
394
395
396 /**
397  * gtk_radio_button_new_with_mnemonic:
398  * @group: the radio button group
399  * @label: the text of the button, with an underscore in front of the
400  *         mnemonic character
401  * @returns: a new #GtkRadioButton
402  *
403  * Creates a new #GtkRadioButton containing a label, adding it to the same 
404  * group as @group. The label will be created using 
405  * gtk_label_new_with_mnemonic(), so underscores in @label indicate the 
406  * mnemonic for the button.
407  **/
408 GtkWidget*
409 gtk_radio_button_new_with_mnemonic (GSList      *group,
410                                     const gchar *label)
411 {
412   GtkWidget *radio_button;
413
414   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, 
415                                "label", label, 
416                                "use-underline", TRUE, 
417                                NULL);
418
419   if (group)
420     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
421
422   return radio_button;
423 }
424
425 /**
426  * gtk_radio_button_new_from_widget:
427  * @radio_group_member: an existing #GtkRadioButton.
428  *
429  * Creates a new #GtkRadioButton, adding it to the same group as @radio_group_member.
430  * As with gtk_radio_button_new(), a widget should be packed into the radio button.
431  *
432  * Returns: a new radio button.
433  */
434 GtkWidget*
435 gtk_radio_button_new_from_widget (GtkRadioButton *radio_group_member)
436 {
437   GSList *l = NULL;
438   if (radio_group_member)
439     l = gtk_radio_button_get_group (radio_group_member);
440   return gtk_radio_button_new (l);
441 }
442
443 /**
444  * gtk_radio_button_new_with_label_from_widget:
445  * @radio_group_member: widget to get radio group from or %NULL
446  * @label: a text string to display next to the radio button.
447  *
448  * Creates a new #GtkRadioButton with a text label, adding it to the same group
449  * as @radio_group_member.
450  *
451  * Returns: a new radio button.
452  */
453 GtkWidget*
454 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *radio_group_member,
455                                              const gchar    *label)
456 {
457   GSList *l = NULL;
458   if (radio_group_member)
459     l = gtk_radio_button_get_group (radio_group_member);
460   return gtk_radio_button_new_with_label (l, label);
461 }
462
463 /**
464  * gtk_radio_button_new_with_mnemonic_from_widget:
465  * @radio_group_member: (allow-none): widget to get radio group from or %NULL
466  * @label: the text of the button, with an underscore in front of the
467  *         mnemonic character
468  * @returns: a new #GtkRadioButton
469  *
470  * Creates a new #GtkRadioButton containing a label. The label
471  * will be created using gtk_label_new_with_mnemonic(), so underscores
472  * in @label indicate the mnemonic for the button.
473  **/
474 GtkWidget*
475 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *radio_group_member,
476                                                 const gchar    *label)
477 {
478   GSList *l = NULL;
479   if (radio_group_member)
480     l = gtk_radio_button_get_group (radio_group_member);
481   return gtk_radio_button_new_with_mnemonic (l, label);
482 }
483
484
485 /**
486  * gtk_radio_button_get_group:
487  * @radio_button: a #GtkRadioButton.
488  *
489  * Retrieves the group assigned to a radio button.
490  *
491  * Return value: (element-type GtkRadioButton) (transfer none): a linked list
492  * containing all the radio buttons in the same group
493  * as @radio_button. The returned list is owned by the radio button
494  * and must not be modified or freed.
495  */
496 GSList*
497 gtk_radio_button_get_group (GtkRadioButton *radio_button)
498 {
499   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
500
501   return radio_button->priv->group;
502 }
503
504
505 static void
506 gtk_radio_button_destroy (GtkObject *object)
507 {
508   GtkWidget *old_group_singleton = NULL;
509   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (object);
510   GtkRadioButtonPrivate *priv = radio_button->priv;
511   GtkRadioButton *tmp_button;
512   GSList *tmp_list;
513   gboolean was_in_group;
514
515   was_in_group = priv->group && priv->group->next;
516
517   priv->group = g_slist_remove (priv->group, radio_button);
518   if (priv->group && !priv->group->next)
519     old_group_singleton = priv->group->data;
520
521   tmp_list = priv->group;
522
523   while (tmp_list)
524     {
525       tmp_button = tmp_list->data;
526       tmp_list = tmp_list->next;
527
528       tmp_button->priv->group = priv->group;
529     }
530
531   /* this button is no longer in the group */
532   priv->group = NULL;
533
534   if (old_group_singleton)
535     g_signal_emit (old_group_singleton, group_changed_signal, 0);
536   if (was_in_group)
537     g_signal_emit (radio_button, group_changed_signal, 0);
538
539   GTK_OBJECT_CLASS (gtk_radio_button_parent_class)->destroy (object);
540 }
541
542 static void
543 get_coordinates (GtkWidget    *widget,
544                  GtkWidget    *reference,
545                  gint         *x,
546                  gint         *y)
547 {
548   GtkAllocation allocation;
549
550   gtk_widget_get_allocation (widget, &allocation);
551   *x = allocation.x + allocation.width / 2;
552   *y = allocation.y + allocation.height / 2;
553
554   gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
555 }
556
557 static gint
558 left_right_compare (gconstpointer a,
559                     gconstpointer b,
560                     gpointer      data)
561 {
562   gint x1, y1, x2, y2;
563
564   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
565   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
566
567   if (y1 == y2)
568     return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
569   else
570     return (y1 < y2) ? -1 : 1;
571 }
572
573 static gint
574 up_down_compare (gconstpointer a,
575                  gconstpointer b,
576                  gpointer      data)
577 {
578   gint x1, y1, x2, y2;
579   
580   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
581   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
582   
583   if (x1 == x2)
584     return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
585   else
586     return (x1 < x2) ? -1 : 1;
587 }
588
589 static gboolean
590 gtk_radio_button_focus (GtkWidget         *widget,
591                         GtkDirectionType   direction)
592 {
593   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
594   GtkRadioButtonPrivate *priv = radio_button->priv;
595   GSList *tmp_slist;
596
597   /* Radio buttons with draw_indicator unset focus "normally", since
598    * they look like buttons to the user.
599    */
600   if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
601     return GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->focus (widget, direction);
602   
603   if (gtk_widget_is_focus (widget))
604     {
605       GtkSettings *settings = gtk_widget_get_settings (widget);
606       GSList *focus_list, *tmp_list;
607       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
608       GtkWidget *new_focus = NULL;
609       gboolean cursor_only;
610       gboolean wrap_around;
611
612       switch (direction)
613         {
614         case GTK_DIR_LEFT:
615         case GTK_DIR_RIGHT:
616           focus_list = g_slist_copy (priv->group);
617           focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
618           break;
619         case GTK_DIR_UP:
620         case GTK_DIR_DOWN:
621           focus_list = g_slist_copy (priv->group);
622           focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
623           break;
624         case GTK_DIR_TAB_FORWARD:
625         case GTK_DIR_TAB_BACKWARD:
626           /* fall through */
627         default:
628           return FALSE;
629         }
630
631       if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
632         focus_list = g_slist_reverse (focus_list);
633
634       tmp_list = g_slist_find (focus_list, widget);
635
636       if (tmp_list)
637         {
638           tmp_list = tmp_list->next;
639           
640           while (tmp_list)
641             {
642               GtkWidget *child = tmp_list->data;
643               
644               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
645                 {
646                   new_focus = child;
647                   break;
648                 }
649
650               tmp_list = tmp_list->next;
651             }
652         }
653
654       g_object_get (settings,
655                     "gtk-keynav-cursor-only", &cursor_only,
656                     "gtk-keynav-wrap-around", &wrap_around,
657                     NULL);
658
659       if (!new_focus)
660         {
661           if (cursor_only)
662             {
663               g_slist_free (focus_list);
664               return FALSE;
665             }
666
667           if (!wrap_around)
668             {
669               g_slist_free (focus_list);
670               gtk_widget_error_bell (widget);
671               return TRUE;
672             }
673
674           tmp_list = focus_list;
675
676           while (tmp_list)
677             {
678               GtkWidget *child = tmp_list->data;
679               
680               if (gtk_widget_get_mapped (child) && gtk_widget_is_sensitive (child))
681                 {
682                   new_focus = child;
683                   break;
684                 }
685               
686               tmp_list = tmp_list->next;
687             }
688         }
689       
690       g_slist_free (focus_list);
691
692       if (new_focus)
693         {
694           gtk_widget_grab_focus (new_focus);
695
696           if (!cursor_only)
697             gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
698         }
699
700       return TRUE;
701     }
702   else
703     {
704       GtkRadioButton *selected_button = NULL;
705       
706       /* We accept the focus if, we don't have the focus and
707        *  - we are the currently active button in the group
708        *  - there is no currently active radio button.
709        */
710       
711       tmp_slist = priv->group;
712       while (tmp_slist)
713         {
714           if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
715             selected_button = tmp_slist->data;
716           tmp_slist = tmp_slist->next;
717         }
718       
719       if (selected_button && selected_button != radio_button)
720         return FALSE;
721
722       gtk_widget_grab_focus (widget);
723       return TRUE;
724     }
725 }
726
727 static void
728 gtk_radio_button_clicked (GtkButton *button)
729 {
730   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (button);
731   GtkRadioButtonPrivate *priv = radio_button->priv;
732   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
733   GtkToggleButton *tmp_button;
734   GtkStateType new_state;
735   GSList *tmp_list;
736   gint toggled;
737   gboolean depressed;
738
739   toggled = FALSE;
740
741   g_object_ref (GTK_WIDGET (button));
742
743   if (toggle_button->active)
744     {
745       tmp_button = NULL;
746       tmp_list = priv->group;
747
748       while (tmp_list)
749         {
750           tmp_button = tmp_list->data;
751           tmp_list = tmp_list->next;
752
753           if (tmp_button->active && tmp_button != toggle_button)
754             break;
755
756           tmp_button = NULL;
757         }
758
759       if (!tmp_button)
760         {
761           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
762         }
763       else
764         {
765           toggled = TRUE;
766           toggle_button->active = !toggle_button->active;
767           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
768         }
769     }
770   else
771     {
772       toggled = TRUE;
773       toggle_button->active = !toggle_button->active;
774
775       tmp_list = priv->group;
776       while (tmp_list)
777         {
778           tmp_button = tmp_list->data;
779           tmp_list = tmp_list->next;
780
781           if (tmp_button->active && (tmp_button != toggle_button))
782             {
783               gtk_button_clicked (GTK_BUTTON (tmp_button));
784               break;
785             }
786         }
787
788       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
789     }
790
791   if (toggle_button->inconsistent)
792     depressed = FALSE;
793   else if (button->in_button && button->button_down)
794     depressed = !toggle_button->active;
795   else
796     depressed = toggle_button->active;
797
798   if (gtk_widget_get_state (GTK_WIDGET (button)) != new_state)
799     gtk_widget_set_state (GTK_WIDGET (button), new_state);
800
801   if (toggled)
802     {
803       gtk_toggle_button_toggled (toggle_button);
804
805       g_object_notify (G_OBJECT (toggle_button), "active");
806     }
807
808   _gtk_button_set_depressed (button, depressed);
809
810   gtk_widget_queue_draw (GTK_WIDGET (button));
811
812   g_object_unref (button);
813 }
814
815 static void
816 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
817                                  GdkRectangle   *area)
818 {
819   GtkAllocation allocation;
820   GtkWidget *widget;
821   GtkWidget *child;
822   GtkButton *button;
823   GtkToggleButton *toggle_button;
824   GtkStateType state_type;
825   GtkShadowType shadow_type;
826   GtkStyle *style;
827   GdkWindow *window;
828   gint x, y;
829   gint indicator_size, indicator_spacing;
830   gint focus_width;
831   gint focus_pad;
832   guint border_width;
833   gboolean interior_focus;
834
835   widget = GTK_WIDGET (check_button);
836
837   if (gtk_widget_is_drawable (widget))
838     {
839       button = GTK_BUTTON (check_button);
840       toggle_button = GTK_TOGGLE_BUTTON (check_button);
841
842       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
843
844       style = gtk_widget_get_style (widget);
845       gtk_widget_style_get (widget,
846                             "interior-focus", &interior_focus,
847                             "focus-line-width", &focus_width,
848                             "focus-padding", &focus_pad,
849                             NULL);
850
851       window = gtk_widget_get_window (widget);
852
853       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
854
855       gtk_widget_get_allocation (widget, &allocation);
856
857       x = allocation.x + indicator_spacing + border_width;
858       y = allocation.y + (allocation.height - indicator_size) / 2;
859
860       child = gtk_bin_get_child (GTK_BIN (check_button));
861       if (!interior_focus || !(child && gtk_widget_get_visible (child)))
862         x += focus_width + focus_pad;      
863
864       if (toggle_button->inconsistent)
865         shadow_type = GTK_SHADOW_ETCHED_IN;
866       else if (toggle_button->active)
867         shadow_type = GTK_SHADOW_IN;
868       else
869         shadow_type = GTK_SHADOW_OUT;
870
871       if (button->activate_timeout || (button->button_down && button->in_button))
872         state_type = GTK_STATE_ACTIVE;
873       else if (button->in_button)
874         state_type = GTK_STATE_PRELIGHT;
875       else if (!gtk_widget_is_sensitive (widget))
876         state_type = GTK_STATE_INSENSITIVE;
877       else
878         state_type = GTK_STATE_NORMAL;
879
880       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
881         x = allocation.x + allocation.width - (indicator_size + x - allocation.x);
882
883       if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
884         {
885           GdkRectangle restrict_area;
886           GdkRectangle new_area;
887
888           restrict_area.x = allocation.x + border_width;
889           restrict_area.y = allocation.y + border_width;
890           restrict_area.width = allocation.width - (2 * border_width);
891           restrict_area.height = allocation.height - (2 * border_width);
892
893           if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
894             {
895               gtk_paint_flat_box (style, window,
896                                   GTK_STATE_PRELIGHT,
897                                   GTK_SHADOW_ETCHED_OUT, 
898                                   area, widget, "checkbutton",
899                                   new_area.x, new_area.y,
900                                   new_area.width, new_area.height);
901             }
902         }
903
904       gtk_paint_option (style, window,
905                         state_type, shadow_type,
906                         area, widget, "radiobutton",
907                         x, y, indicator_size, indicator_size);
908     }
909 }