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