]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / gtk / gtkcheckbutton.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 "gtkcheckbutton.h"
29 #include "gtkintl.h"
30 #include "gtklabel.h"
31
32
33 #define INDICATOR_SIZE     13
34 #define INDICATOR_SPACING  2
35
36
37 static void gtk_check_button_class_init          (GtkCheckButtonClass *klass);
38 static void gtk_check_button_init                (GtkCheckButton      *check_button);
39 static void gtk_check_button_size_request        (GtkWidget           *widget,
40                                                   GtkRequisition      *requisition);
41 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
42                                                   GtkAllocation       *allocation);
43 static gint gtk_check_button_expose              (GtkWidget           *widget,
44                                                   GdkEventExpose      *event);
45 static void gtk_check_button_paint               (GtkWidget           *widget,
46                                                   GdkRectangle        *area);
47 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
48                                                   GdkRectangle        *area);
49 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
50                                                   GdkRectangle        *area);
51
52 static GtkToggleButtonClass *parent_class = NULL;
53
54
55 GType
56 gtk_check_button_get_type (void)
57 {
58   static GType check_button_type = 0;
59   
60   if (!check_button_type)
61     {
62       static const GTypeInfo check_button_info =
63       {
64         sizeof (GtkCheckButtonClass),
65         NULL,           /* base_init */
66         NULL,           /* base_finalize */
67         (GClassInitFunc) gtk_check_button_class_init,
68         NULL,           /* class_finalize */
69         NULL,           /* class_data */
70         sizeof (GtkCheckButton),
71         0,              /* n_preallocs */
72         (GInstanceInitFunc) gtk_check_button_init,
73       };
74       
75       check_button_type =
76         g_type_register_static (GTK_TYPE_TOGGLE_BUTTON, "GtkCheckButton",
77                                 &check_button_info, 0);
78     }
79   
80   return check_button_type;
81 }
82
83 static void
84 gtk_check_button_class_init (GtkCheckButtonClass *class)
85 {
86   GtkWidgetClass *widget_class;
87   
88   widget_class = (GtkWidgetClass*) class;
89   parent_class = g_type_class_peek_parent (class);
90   
91   widget_class->size_request = gtk_check_button_size_request;
92   widget_class->size_allocate = gtk_check_button_size_allocate;
93   widget_class->expose_event = gtk_check_button_expose;
94
95   class->draw_indicator = gtk_real_check_button_draw_indicator;
96
97   gtk_widget_class_install_style_property (widget_class,
98                                            g_param_spec_int ("indicator_size",
99                                                              P_("Indicator Size"),
100                                                              P_("Size of check or radio indicator"),
101                                                              0,
102                                                              G_MAXINT,
103                                                              INDICATOR_SIZE,
104                                                              G_PARAM_READABLE));
105   gtk_widget_class_install_style_property (widget_class,
106                                            g_param_spec_int ("indicator_spacing",
107                                                              P_("Indicator Spacing"),
108                                                              P_("Spacing around check or radio indicator"),
109                                                              0,
110                                                              G_MAXINT,
111                                                              INDICATOR_SPACING,
112                                                              G_PARAM_READABLE));
113 }
114
115 static void
116 gtk_check_button_init (GtkCheckButton *check_button)
117 {
118   GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
119   GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
120   GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
121   GTK_BUTTON (check_button)->depress_on_activate = FALSE;
122 }
123
124 GtkWidget*
125 gtk_check_button_new (void)
126 {
127   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
128 }
129
130
131 GtkWidget*
132 gtk_check_button_new_with_label (const gchar *label)
133 {
134   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
135 }
136
137 /**
138  * gtk_check_button_new_with_mnemonic:
139  * @label: The text of the button, with an underscore in front of the
140  *         mnemonic character
141  * @returns: a new #GtkCheckButton
142  *
143  * Creates a new #GtkCheckButton containing a label. The label
144  * will be created using gtk_label_new_with_mnemonic(), so underscores
145  * in @label indicate the mnemonic for the check button.
146  **/
147 GtkWidget*
148 gtk_check_button_new_with_mnemonic (const gchar *label)
149 {
150   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, "use_underline", TRUE, NULL);
151 }
152
153
154 /* This should only be called when toggle_button->draw_indicator
155  * is true.
156  */
157 static void
158 gtk_check_button_paint (GtkWidget    *widget,
159                         GdkRectangle *area)
160 {
161   GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
162   
163   if (GTK_WIDGET_DRAWABLE (widget))
164     {
165       gint border_width;
166       gint interior_focus;
167       gint focus_width;
168       gint focus_pad;
169           
170       gtk_widget_style_get (widget,
171                             "interior-focus", &interior_focus,
172                             "focus-line-width", &focus_width,
173                             "focus-padding", &focus_pad,
174                             NULL);
175
176       gtk_check_button_draw_indicator (check_button, area);
177       
178       border_width = GTK_CONTAINER (widget)->border_width;
179       if (GTK_WIDGET_HAS_FOCUS (widget))
180         {
181           GtkWidget *child = GTK_BIN (widget)->child;
182           
183           if (interior_focus && child && GTK_WIDGET_VISIBLE (child))
184             gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
185                              NULL, widget, "checkbutton",
186                              child->allocation.x - focus_width - focus_pad,
187                              child->allocation.y - focus_width - focus_pad,
188                              child->allocation.width + 2 * (focus_width + focus_pad),
189                              child->allocation.height + 2 * (focus_width + focus_pad));
190           else
191             gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
192                              NULL, widget, "checkbutton",
193                              border_width + widget->allocation.x,
194                              border_width + widget->allocation.y,
195                              widget->allocation.width - 2 * border_width,
196                              widget->allocation.height - 2 * border_width);
197         }
198     }
199 }
200
201 void
202 _gtk_check_button_get_props (GtkCheckButton *check_button,
203                              gint           *indicator_size,
204                              gint           *indicator_spacing)
205 {
206   GtkWidget *widget =  GTK_WIDGET (check_button);
207
208   if (indicator_size)
209     gtk_widget_style_get (widget, "indicator_size", indicator_size, NULL);
210
211   if (indicator_spacing)
212     gtk_widget_style_get (widget, "indicator_spacing", indicator_spacing, NULL);
213 }
214
215 static void
216 gtk_check_button_size_request (GtkWidget      *widget,
217                                GtkRequisition *requisition)
218 {
219   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
220   
221   if (toggle_button->draw_indicator)
222     {
223       GtkWidget *child;
224       gint temp;
225       gint indicator_size;
226       gint indicator_spacing;
227       gint border_width = GTK_CONTAINER (widget)->border_width;
228       gint focus_width;
229       gint focus_pad;
230
231       gtk_widget_style_get (GTK_WIDGET (widget),
232                             "focus-line-width", &focus_width,
233                             "focus-padding", &focus_pad,
234                             NULL);
235  
236       requisition->width = border_width * 2;
237       requisition->height = border_width * 2;
238
239       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
240                                    &indicator_size, &indicator_spacing);
241       
242       child = GTK_BIN (widget)->child;
243       if (child && GTK_WIDGET_VISIBLE (child))
244         {
245           GtkRequisition child_requisition;
246           
247           gtk_widget_size_request (child, &child_requisition);
248
249           requisition->width += child_requisition.width + indicator_spacing;
250           requisition->height += child_requisition.height;
251         }
252       
253       requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
254       
255       temp = indicator_size + indicator_spacing * 2;
256       requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
257     }
258   else
259     (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
260 }
261
262 static void
263 gtk_check_button_size_allocate (GtkWidget     *widget,
264                                 GtkAllocation *allocation)
265 {
266   GtkCheckButton *check_button;
267   GtkToggleButton *toggle_button;
268   GtkButton *button;
269   GtkAllocation child_allocation;
270
271   button = GTK_BUTTON (widget);
272   check_button = GTK_CHECK_BUTTON (widget);
273   toggle_button = GTK_TOGGLE_BUTTON (widget);
274
275   if (toggle_button->draw_indicator)
276     {
277       gint indicator_size;
278       gint indicator_spacing;
279       gint focus_width;
280       gint focus_pad;
281       
282       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
283       gtk_widget_style_get (widget,
284                             "focus-line-width", &focus_width,
285                             "focus-padding", &focus_pad,
286                             NULL);
287                                                     
288       widget->allocation = *allocation;
289       if (GTK_WIDGET_REALIZED (widget))
290         gdk_window_move_resize (button->event_window,
291                                 allocation->x, allocation->y,
292                                 allocation->width, allocation->height);
293       
294       if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
295         {
296           GtkRequisition child_requisition;
297           gint border_width = GTK_CONTAINER (widget)->border_width;
298
299           gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
300  
301           child_allocation.width = MIN (child_requisition.width,
302                                         allocation->width -
303                                         ((border_width + focus_width + focus_pad) * 2
304                                          + indicator_size + indicator_spacing * 3));
305           child_allocation.width = MAX (child_allocation.width, 1);
306
307           child_allocation.height = MIN (child_requisition.height,
308                                          allocation->height - (border_width + focus_width + focus_pad) * 2);
309           child_allocation.height = MAX (child_allocation.height, 1);
310           
311           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
312                                 widget->allocation.x + focus_width + focus_pad);
313           child_allocation.y = widget->allocation.y +
314                   (allocation->height - child_allocation.height) / 2;
315
316           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
317             child_allocation.x = allocation->x + allocation->width
318               - (child_allocation.x - allocation->x + child_allocation.width);
319           
320           gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
321         }
322     }
323   else
324     (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
325 }
326
327 static gint
328 gtk_check_button_expose (GtkWidget      *widget,
329                          GdkEventExpose *event)
330 {
331   GtkCheckButton *check_button;
332   GtkToggleButton *toggle_button;
333   GtkBin *bin;
334   
335   check_button = GTK_CHECK_BUTTON (widget);
336   toggle_button = GTK_TOGGLE_BUTTON (widget);
337   bin = GTK_BIN (widget);
338   
339   if (GTK_WIDGET_DRAWABLE (widget))
340     {
341       if (toggle_button->draw_indicator)
342         {
343           gtk_check_button_paint (widget, &event->area);
344           
345           if (bin->child)
346             gtk_container_propagate_expose (GTK_CONTAINER (widget),
347                                             bin->child,
348                                             event);
349         }
350       else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
351         (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
352     }
353   
354   return FALSE;
355 }
356
357
358 static void
359 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
360                                  GdkRectangle   *area)
361 {
362   GtkCheckButtonClass *class;
363   
364   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
365   
366   class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
367   
368   if (class->draw_indicator)
369     (* class->draw_indicator) (check_button, area);
370 }
371
372 static void
373 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
374                                       GdkRectangle   *area)
375 {
376   GtkWidget *widget;
377   GtkWidget *child;
378   GtkButton *button;
379   GtkToggleButton *toggle_button;
380   GtkStateType state_type;
381   GtkShadowType shadow_type;
382   gint x, y;
383   gint indicator_size;
384   gint indicator_spacing;
385   gint focus_width;
386   gint focus_pad;
387   gboolean interior_focus;
388   
389   if (GTK_WIDGET_DRAWABLE (check_button))
390     {
391       widget = GTK_WIDGET (check_button);
392       button = GTK_BUTTON (check_button);
393       toggle_button = GTK_TOGGLE_BUTTON (check_button);
394   
395       gtk_widget_style_get (widget, "interior_focus", &interior_focus,
396                             "focus-line-width", &focus_width, 
397                             "focus-padding", &focus_pad, NULL);
398
399       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
400
401       x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
402       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
403
404       child = GTK_BIN (check_button)->child;
405       if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
406         x += focus_width + focus_pad;      
407
408       if (toggle_button->inconsistent)
409         shadow_type = GTK_SHADOW_ETCHED_IN;
410       else if (toggle_button->active)
411         shadow_type = GTK_SHADOW_IN;
412       else
413         shadow_type = GTK_SHADOW_OUT;
414
415       if (button->activate_timeout || (button->button_down && button->in_button))
416         state_type = GTK_STATE_ACTIVE;
417       else if (button->in_button)
418         state_type = GTK_STATE_PRELIGHT;
419       else if (!GTK_WIDGET_IS_SENSITIVE (widget))
420         state_type = GTK_STATE_INSENSITIVE;
421       else
422         state_type = GTK_STATE_NORMAL;
423       
424       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
425         x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
426
427       if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
428         {
429           GdkRectangle restrict_area;
430           GdkRectangle new_area;
431               
432           restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
433           restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
434           restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
435           restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
436           
437           if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
438             {
439               gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
440                                   GTK_SHADOW_ETCHED_OUT, 
441                                   area, widget, "checkbutton",
442                                   new_area.x, new_area.y,
443                                   new_area.width, new_area.height);
444             }
445         }
446
447       gtk_paint_check (widget->style, widget->window,
448                        state_type, shadow_type,
449                        area, widget, "checkbutton",
450                        x, y, indicator_size, indicator_size);
451     }
452 }