]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiobutton.c
Make the widgets work reasonably when they don't have children -- draw the
[~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 "gtklabel.h"
28 #include "gtkradiobutton.h"
29 #include "gtkintl.h"
30
31
32 enum {
33   PROP_0,
34   PROP_GROUP
35 };
36
37
38 static void     gtk_radio_button_class_init     (GtkRadioButtonClass *klass);
39 static void     gtk_radio_button_init           (GtkRadioButton      *radio_button);
40 static void     gtk_radio_button_destroy        (GtkObject           *object);
41 static gboolean gtk_radio_button_focus          (GtkWidget           *widget,
42                                                  GtkDirectionType     direction);
43 static void     gtk_radio_button_clicked        (GtkButton           *button);
44 static void     gtk_radio_button_draw_indicator (GtkCheckButton      *check_button,
45                                                  GdkRectangle        *area);
46 static void     gtk_radio_button_set_property   (GObject             *object,
47                                                  guint                prop_id,
48                                                  const GValue        *value,
49                                                  GParamSpec          *pspec);
50 static void     gtk_radio_button_get_property   (GObject             *object,
51                                                  guint                prop_id,
52                                                  GValue              *value,
53                                                  GParamSpec          *pspec);
54
55 static GtkCheckButtonClass *parent_class = NULL;
56
57
58 GType
59 gtk_radio_button_get_type (void)
60 {
61   static GType radio_button_type = 0;
62
63   if (!radio_button_type)
64     {
65       static const GTypeInfo radio_button_info =
66       {
67         sizeof (GtkRadioButtonClass),
68         NULL,           /* base_init */
69         NULL,           /* base_finalize */
70         (GClassInitFunc) gtk_radio_button_class_init,
71         NULL,           /* class_finalize */
72         NULL,           /* class_data */
73         sizeof (GtkRadioButton),
74         0,              /* n_preallocs */
75         (GInstanceInitFunc) gtk_radio_button_init,
76       };
77
78       radio_button_type =
79         g_type_register_static (GTK_TYPE_CHECK_BUTTON, "GtkRadioButton",
80                                 &radio_button_info, 0);
81     }
82
83   return radio_button_type;
84 }
85
86 static void
87 gtk_radio_button_class_init (GtkRadioButtonClass *class)
88 {
89   GObjectClass *gobject_class;
90   GtkObjectClass *object_class;
91   GtkButtonClass *button_class;
92   GtkCheckButtonClass *check_button_class;
93   GtkWidgetClass *widget_class;
94
95   gobject_class = G_OBJECT_CLASS (class);
96   object_class = (GtkObjectClass*) class;
97   widget_class = (GtkWidgetClass*) class;
98   button_class = (GtkButtonClass*) class;
99   check_button_class = (GtkCheckButtonClass*) class;
100
101   parent_class = g_type_class_peek_parent (class);
102
103   gobject_class->set_property = gtk_radio_button_set_property;
104   gobject_class->get_property = gtk_radio_button_get_property;
105
106   g_object_class_install_property (gobject_class,
107                                    PROP_GROUP,
108                                    g_param_spec_object ("group",
109                                                         _("Group"),
110                                                         _("The radio button whose group this widget belongs."),
111                                                         GTK_TYPE_RADIO_BUTTON,
112                                                         G_PARAM_WRITABLE));
113   object_class->destroy = gtk_radio_button_destroy;
114
115   widget_class->focus = gtk_radio_button_focus;
116
117   button_class->clicked = gtk_radio_button_clicked;
118
119   check_button_class->draw_indicator = gtk_radio_button_draw_indicator;
120 }
121
122 static void
123 gtk_radio_button_init (GtkRadioButton *radio_button)
124 {
125   GTK_WIDGET_SET_FLAGS (radio_button, GTK_NO_WINDOW);
126   GTK_WIDGET_UNSET_FLAGS (radio_button, GTK_RECEIVES_DEFAULT);
127
128   GTK_TOGGLE_BUTTON (radio_button)->active = TRUE;
129
130   GTK_BUTTON (radio_button)->depress_on_activate = FALSE;
131
132   radio_button->group = g_slist_prepend (NULL, radio_button);
133
134   _gtk_button_set_depressed (GTK_BUTTON (radio_button), TRUE);
135   gtk_widget_set_state (GTK_WIDGET (radio_button), GTK_STATE_ACTIVE);
136 }
137
138 static void
139 gtk_radio_button_set_property (GObject      *object,
140                                guint         prop_id,
141                                const GValue *value,
142                                GParamSpec   *pspec)
143 {
144   GtkRadioButton *radio_button;
145
146   radio_button = GTK_RADIO_BUTTON (object);
147
148   switch (prop_id)
149     {
150       GSList *slist;
151
152     case PROP_GROUP:
153       if (G_VALUE_HOLDS_OBJECT (value))
154         slist = gtk_radio_button_get_group ((GtkRadioButton*) g_value_get_object (value));
155       else
156         slist = NULL;
157       gtk_radio_button_set_group (radio_button, slist);
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162     }
163 }
164
165 static void
166 gtk_radio_button_get_property (GObject    *object,
167                                guint       prop_id,
168                                GValue     *value,
169                                GParamSpec *pspec)
170 {
171   GtkRadioButton *radio_button;
172
173   radio_button = GTK_RADIO_BUTTON (object);
174
175   switch (prop_id)
176     {
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179       break;
180     }
181 }
182
183 void
184 gtk_radio_button_set_group (GtkRadioButton *radio_button,
185                             GSList         *group)
186 {
187   g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
188   g_return_if_fail (!g_slist_find (group, radio_button));
189
190   if (radio_button->group)
191     {
192       GSList *slist;
193       
194       radio_button->group = g_slist_remove (radio_button->group, radio_button);
195       
196       for (slist = radio_button->group; slist; slist = slist->next)
197         {
198           GtkRadioButton *tmp_button;
199           
200           tmp_button = slist->data;
201           
202           tmp_button->group = radio_button->group;
203         }
204     }
205   
206   radio_button->group = g_slist_prepend (group, radio_button);
207   
208   if (group)
209     {
210       GSList *slist;
211       
212       for (slist = group; slist; slist = slist->next)
213         {
214           GtkRadioButton *tmp_button;
215           
216           tmp_button = slist->data;
217           
218           tmp_button->group = radio_button->group;
219         }
220     }
221
222   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button), group == NULL);
223 }
224
225 GtkWidget*
226 gtk_radio_button_new (GSList *group)
227 {
228   GtkRadioButton *radio_button;
229
230   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, NULL);
231
232   if (group)
233     gtk_radio_button_set_group (radio_button, group);
234
235   return GTK_WIDGET (radio_button);
236 }
237
238 GtkWidget*
239 gtk_radio_button_new_with_label (GSList      *group,
240                                  const gchar *label)
241 {
242   GtkWidget *radio_button;
243
244   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, NULL) ;
245
246   if (group)
247     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
248
249   return radio_button;
250 }
251
252
253 /**
254  * gtk_radio_button_new_with_mnemonic:
255  * @group: the radio button group
256  * @label: the text of the button, with an underscore in front of the
257  *         mnemonic character
258  * @returns: a new #GtkRadioButton
259  *
260  * Creates a new #GtkRadioButton containing a label, adding it to the same 
261  * group as @group. The label will be created using 
262  * gtk_label_new_with_mnemonic(), so underscores in @label indicate the 
263  * mnemonic for the button.
264  **/
265 GtkWidget*
266 gtk_radio_button_new_with_mnemonic (GSList      *group,
267                                     const gchar *label)
268 {
269   GtkWidget *radio_button;
270
271   radio_button = g_object_new (GTK_TYPE_RADIO_BUTTON, "label", label, "use_underline", TRUE, NULL);
272
273   if (group)
274     gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button), group);
275
276   return radio_button;
277 }
278
279 GtkWidget*
280 gtk_radio_button_new_from_widget (GtkRadioButton *group)
281 {
282   GSList *l = NULL;
283   if (group)
284     l = gtk_radio_button_get_group (group);
285   return gtk_radio_button_new (l);
286 }
287
288
289 GtkWidget*
290 gtk_radio_button_new_with_label_from_widget (GtkRadioButton *group,
291                                              const gchar    *label)
292 {
293   GSList *l = NULL;
294   if (group)
295     l = gtk_radio_button_get_group (group);
296   return gtk_radio_button_new_with_label (l, label);
297 }
298
299 /**
300  * gtk_radio_button_new_with_mnemonic_from_widget:
301  * @group: widget to get radio group from
302  * @label: the text of the button, with an underscore in front of the
303  *         mnemonic character
304  * @returns: a new #GtkRadioButton
305  *
306  * Creates a new #GtkRadioButton containing a label. The label
307  * will be created using gtk_label_new_with_mnemonic(), so underscores
308  * in @label indicate the mnemonic for the button.
309  **/
310 GtkWidget*
311 gtk_radio_button_new_with_mnemonic_from_widget (GtkRadioButton *group,
312                                                 const gchar    *label)
313 {
314   GSList *l = NULL;
315   if (group)
316     l = gtk_radio_button_get_group (group);
317   return gtk_radio_button_new_with_mnemonic (l, label);
318 }
319
320 GSList*
321 gtk_radio_button_get_group (GtkRadioButton *radio_button)
322 {
323   g_return_val_if_fail (GTK_IS_RADIO_BUTTON (radio_button), NULL);
324
325   return radio_button->group;
326 }
327
328
329 static void
330 gtk_radio_button_destroy (GtkObject *object)
331 {
332   GtkRadioButton *radio_button;
333   GtkRadioButton *tmp_button;
334   GSList *tmp_list;
335
336   radio_button = GTK_RADIO_BUTTON (object);
337
338   radio_button->group = g_slist_remove (radio_button->group, radio_button);
339   tmp_list = radio_button->group;
340
341   while (tmp_list)
342     {
343       tmp_button = tmp_list->data;
344       tmp_list = tmp_list->next;
345
346       tmp_button->group = radio_button->group;
347     }
348
349   /* this button is no longer in the group */
350   radio_button->group = NULL;
351   
352   if (GTK_OBJECT_CLASS (parent_class)->destroy)
353     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
354 }
355
356 static void
357 get_coordinates (GtkWidget    *widget,
358                  GtkWidget    *reference,
359                  gint         *x,
360                  gint         *y)
361 {
362   *x = widget->allocation.x + widget->allocation.width / 2;
363   *y = widget->allocation.y + widget->allocation.height / 2;
364   
365   gtk_widget_translate_coordinates (widget, reference, *x, *y, x, y);
366 }
367
368 static gint
369 left_right_compare (gconstpointer a,
370                     gconstpointer b,
371                     gpointer      data)
372 {
373   gint x1, y1, x2, y2;
374
375   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
376   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
377
378   if (y1 == y2)
379     return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
380   else
381     return (y1 < y2) ? -1 : 1;
382 }
383
384 static gint
385 up_down_compare (gconstpointer a,
386                  gconstpointer b,
387                  gpointer      data)
388 {
389   gint x1, y1, x2, y2;
390   
391   get_coordinates ((GtkWidget *)a, data, &x1, &y1);
392   get_coordinates ((GtkWidget *)b, data, &x2, &y2);
393   
394   if (x1 == x2)
395     return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
396   else
397     return (x1 < x2) ? -1 : 1;
398 }
399
400 static gboolean
401 gtk_radio_button_focus (GtkWidget         *widget,
402                         GtkDirectionType   direction)
403 {
404   GtkRadioButton *radio_button = GTK_RADIO_BUTTON (widget);
405   GSList *tmp_slist;
406
407   /* Radio buttons with draw_indicator unset focus "normally", since
408    * they look like buttons to the user.
409    */
410   if (!GTK_TOGGLE_BUTTON (widget)->draw_indicator)
411     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
412   
413   if (gtk_widget_is_focus (widget))
414     {
415       GSList *focus_list, *tmp_list;
416       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
417       GtkWidget *new_focus = NULL;
418
419       focus_list = g_slist_copy (radio_button->group);
420       
421       switch (direction)
422         {
423         case GTK_DIR_TAB_FORWARD:
424         case GTK_DIR_TAB_BACKWARD:
425           return FALSE;
426         case GTK_DIR_LEFT:
427         case GTK_DIR_RIGHT:
428           focus_list = g_slist_sort_with_data (focus_list, left_right_compare, toplevel);
429           break;
430         case GTK_DIR_UP:
431         case GTK_DIR_DOWN:
432           focus_list = g_slist_sort_with_data (focus_list, up_down_compare, toplevel);
433           break;
434         }
435
436       if (direction == GTK_DIR_LEFT || direction == GTK_DIR_UP)
437         focus_list = g_slist_reverse (focus_list);
438
439       tmp_list = g_slist_find (focus_list, widget);
440
441       if (tmp_list)
442         {
443           tmp_list = tmp_list->next;
444           
445           while (tmp_list)
446             {
447               GtkWidget *child = tmp_list->data;
448               
449               if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
450                 {
451                   new_focus = child;
452                   break;
453                 }
454
455               tmp_list = tmp_list->next;
456             }
457         }
458
459       if (!new_focus)
460         {
461           tmp_list = focus_list;
462
463           while (tmp_list)
464             {
465               GtkWidget *child = tmp_list->data;
466               
467               if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_IS_SENSITIVE (child))
468                 {
469                   new_focus = child;
470                   break;
471                 }
472               
473               tmp_list = tmp_list->next;
474             }
475         }
476       
477       g_slist_free (focus_list);
478
479       if (new_focus)
480         {
481           gtk_widget_grab_focus (new_focus);
482           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (new_focus), TRUE);
483         }
484
485       return TRUE;
486     }
487   else
488     {
489       GtkRadioButton *selected_button = NULL;
490       
491       /* We accept the focus if, we don't have the focus and
492        *  - we are the currently active button in the group
493        *  - there is no currently active radio button.
494        */
495       
496       tmp_slist = radio_button->group;
497       while (tmp_slist)
498         {
499           if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
500             selected_button = tmp_slist->data;
501           tmp_slist = tmp_slist->next;
502         }
503       
504       if (selected_button && selected_button != radio_button)
505         return FALSE;
506
507       gtk_widget_grab_focus (widget);
508       return TRUE;
509     }
510 }
511
512 static void
513 gtk_radio_button_clicked (GtkButton *button)
514 {
515   GtkToggleButton *toggle_button;
516   GtkRadioButton *radio_button;
517   GtkToggleButton *tmp_button;
518   GtkStateType new_state;
519   GSList *tmp_list;
520   gint toggled;
521   gboolean depressed;
522
523   radio_button = GTK_RADIO_BUTTON (button);
524   toggle_button = GTK_TOGGLE_BUTTON (button);
525   toggled = FALSE;
526
527   g_object_ref (GTK_WIDGET (button));
528
529   if (toggle_button->active)
530     {
531       tmp_button = NULL;
532       tmp_list = radio_button->group;
533
534       while (tmp_list)
535         {
536           tmp_button = tmp_list->data;
537           tmp_list = tmp_list->next;
538
539           if (tmp_button->active && tmp_button != toggle_button)
540             break;
541
542           tmp_button = NULL;
543         }
544
545       if (!tmp_button)
546         {
547           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
548         }
549       else
550         {
551           toggled = TRUE;
552           toggle_button->active = !toggle_button->active;
553           new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
554         }
555     }
556   else
557     {
558       toggled = TRUE;
559       toggle_button->active = !toggle_button->active;
560
561       tmp_list = radio_button->group;
562       while (tmp_list)
563         {
564           tmp_button = tmp_list->data;
565           tmp_list = tmp_list->next;
566
567           if (tmp_button->active && (tmp_button != toggle_button))
568             {
569               gtk_button_clicked (GTK_BUTTON (tmp_button));
570               break;
571             }
572         }
573
574       new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
575     }
576
577   if (toggle_button->inconsistent)
578     depressed = FALSE;
579   else if (button->in_button && button->button_down)
580     depressed = !toggle_button->active;
581   else
582     depressed = toggle_button->active;
583
584   if (GTK_WIDGET_STATE (button) != new_state)
585     gtk_widget_set_state (GTK_WIDGET (button), new_state);
586
587   if (toggled)
588     gtk_toggle_button_toggled (toggle_button);
589
590   _gtk_button_set_depressed (button, depressed);
591
592   gtk_widget_queue_draw (GTK_WIDGET (button));
593
594   g_object_unref (button);
595 }
596
597 static void
598 gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
599                                  GdkRectangle   *area)
600 {
601   GtkWidget *widget;
602   GtkWidget *child;
603   GtkButton *button;
604   GtkToggleButton *toggle_button;
605   GtkStateType state_type;
606   GtkShadowType shadow_type;
607   gint x, y;
608   gint indicator_size, indicator_spacing;
609   gint focus_width;
610   gint focus_pad;
611   gboolean interior_focus;
612
613   if (GTK_WIDGET_DRAWABLE (check_button))
614     {
615       widget = GTK_WIDGET (check_button);
616       button = GTK_BUTTON (check_button);
617       toggle_button = GTK_TOGGLE_BUTTON (check_button);
618
619       gtk_widget_style_get (widget,
620                             "interior_focus", &interior_focus,
621                             "focus-line-width", &focus_width,
622                             "focus-padding", &focus_pad,
623                             NULL);
624
625       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
626
627       x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
628       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
629
630       child = GTK_BIN (check_button)->child;
631       if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
632         x += focus_width + focus_pad;      
633
634       if (toggle_button->inconsistent)
635         shadow_type = GTK_SHADOW_ETCHED_IN;
636       else if (toggle_button->active)
637         shadow_type = GTK_SHADOW_IN;
638       else
639         shadow_type = GTK_SHADOW_OUT;
640
641       if (button->activate_timeout || (button->button_down && button->in_button))
642         state_type = GTK_STATE_ACTIVE;
643       else if (button->in_button)
644         state_type = GTK_STATE_PRELIGHT;
645       else
646         state_type = GTK_STATE_NORMAL;
647
648       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
649         x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
650
651       if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
652         {
653           GdkRectangle restrict_area;
654           GdkRectangle new_area;
655               
656           restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
657           restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
658           restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
659           restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
660           
661           if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
662             {
663               gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
664                                   GTK_SHADOW_ETCHED_OUT, 
665                                   area, widget, "checkbutton",
666                                   new_area.x, new_area.y,
667                                   new_area.width, new_area.height);
668             }
669         }
670
671       gtk_paint_option (widget->style, widget->window,
672                         state_type, shadow_type,
673                         area, widget, "radiobutton",
674                         x, y, indicator_size, indicator_size);
675     }
676 }