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