]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
GtkCheckButton: Use GtkStyleContext for rendering.
[~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
29 #include "gtkcheckbutton.h"
30
31 #include "gtkbuttonprivate.h"
32 #include "gtklabel.h"
33
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36
37
38 #define INDICATOR_SIZE     13
39 #define INDICATOR_SPACING  2
40
41
42 static void gtk_check_button_get_preferred_width  (GtkWidget          *widget,
43                                                    gint               *minimum,
44                                                    gint               *natural);
45 static void gtk_check_button_get_preferred_height (GtkWidget          *widget,
46                                                    gint               *minimum,
47                                                    gint               *natural);
48 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
49                                                   GtkAllocation       *allocation);
50 static gboolean gtk_check_button_draw            (GtkWidget           *widget,
51                                                   cairo_t             *cr);
52 static void gtk_check_button_paint               (GtkWidget           *widget,
53                                                   cairo_t             *cr);
54 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
55                                                   cairo_t             *cr);
56 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
57                                                   cairo_t             *cr);
58
59 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
60
61 static void
62 gtk_check_button_class_init (GtkCheckButtonClass *class)
63 {
64   GtkWidgetClass *widget_class;
65
66   widget_class = (GtkWidgetClass*) class;
67
68   widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
69   widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
70   widget_class->size_allocate = gtk_check_button_size_allocate;
71   widget_class->draw = gtk_check_button_draw;
72
73   class->draw_indicator = gtk_real_check_button_draw_indicator;
74
75   gtk_widget_class_install_style_property (widget_class,
76                                            g_param_spec_int ("indicator-size",
77                                                              P_("Indicator Size"),
78                                                              P_("Size of check or radio indicator"),
79                                                              0,
80                                                              G_MAXINT,
81                                                              INDICATOR_SIZE,
82                                                              GTK_PARAM_READABLE));
83   gtk_widget_class_install_style_property (widget_class,
84                                            g_param_spec_int ("indicator-spacing",
85                                                              P_("Indicator Spacing"),
86                                                              P_("Spacing around check or radio indicator"),
87                                                              0,
88                                                              G_MAXINT,
89                                                              INDICATOR_SPACING,
90                                                              GTK_PARAM_READABLE));
91 }
92
93 static void
94 gtk_check_button_init (GtkCheckButton *check_button)
95 {
96   GtkStyleContext *context;
97
98   gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
99   gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
100   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
101
102   /* Remove the "button" class added in GtkButton constructor,
103    * since this widget implementation doesn't look like a button
104    * at all.
105    */
106   context = gtk_widget_get_style_context (GTK_WIDGET (check_button));
107   gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
108 }
109
110 GtkWidget*
111 gtk_check_button_new (void)
112 {
113   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
114 }
115
116
117 GtkWidget*
118 gtk_check_button_new_with_label (const gchar *label)
119 {
120   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
121 }
122
123 /**
124  * gtk_check_button_new_with_mnemonic:
125  * @label: The text of the button, with an underscore in front of the
126  *         mnemonic character
127  * @returns: a new #GtkCheckButton
128  *
129  * Creates a new #GtkCheckButton containing a label. The label
130  * will be created using gtk_label_new_with_mnemonic(), so underscores
131  * in @label indicate the mnemonic for the check button.
132  **/
133 GtkWidget*
134 gtk_check_button_new_with_mnemonic (const gchar *label)
135 {
136   return g_object_new (GTK_TYPE_CHECK_BUTTON, 
137                        "label", label, 
138                        "use-underline", TRUE, 
139                        NULL);
140 }
141
142
143 /* This should only be called when toggle_button->draw_indicator
144  * is true.
145  */
146 static void
147 gtk_check_button_paint (GtkWidget    *widget,
148                         cairo_t      *cr)
149 {
150   GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
151   gint border_width;
152   gint interior_focus;
153   gint focus_width;
154   gint focus_pad;
155       
156   gtk_widget_style_get (widget,
157                         "interior-focus", &interior_focus,
158                         "focus-line-width", &focus_width,
159                         "focus-padding", &focus_pad,
160                         NULL);
161
162   gtk_check_button_draw_indicator (check_button, cr);
163
164   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
165   if (gtk_widget_has_focus (widget))
166     {
167       GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
168       GtkStyleContext *context;
169       GtkStateFlags state;
170       GtkAllocation allocation;
171
172       gtk_widget_get_allocation (widget, &allocation);
173       context = gtk_widget_get_style_context (widget);
174       state = gtk_widget_get_state_flags (widget);
175
176       gtk_style_context_set_state (context, state);
177
178       if (interior_focus && child && gtk_widget_get_visible (child))
179         {
180           GtkAllocation child_allocation;
181
182           gtk_widget_get_allocation (child, &child_allocation);
183           gtk_render_focus (context, cr,
184                             child_allocation.x - allocation.x - focus_width - focus_pad,
185                             child_allocation.y - allocation.y - focus_width - focus_pad,
186                             child_allocation.width + 2 * (focus_width + focus_pad),
187                             child_allocation.height + 2 * (focus_width + focus_pad));
188         }
189       else
190         gtk_render_focus (context, cr,
191                           border_width, border_width,
192                           allocation.width - 2 * border_width,
193                           allocation.height - 2 * border_width);
194     }
195 }
196
197 void
198 _gtk_check_button_get_props (GtkCheckButton *check_button,
199                              gint           *indicator_size,
200                              gint           *indicator_spacing)
201 {
202   GtkWidget *widget =  GTK_WIDGET (check_button);
203
204   if (indicator_size)
205     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
206
207   if (indicator_spacing)
208     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
209 }
210
211 static void
212 gtk_check_button_get_preferred_width (GtkWidget *widget,
213                                       gint      *minimum,
214                                       gint      *natural)
215 {
216   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
217   
218   if (gtk_toggle_button_get_mode (toggle_button))
219     {
220       GtkWidget *child;
221       gint indicator_size;
222       gint indicator_spacing;
223       gint focus_width;
224       gint focus_pad;
225       guint border_width;
226
227       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
228
229       gtk_widget_style_get (GTK_WIDGET (widget),
230                             "focus-line-width", &focus_width,
231                             "focus-padding", &focus_pad,
232                             NULL);
233       *minimum = 2 * border_width;
234       *natural = 2 * border_width;
235
236       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
237                                    &indicator_size, &indicator_spacing);
238
239       child = gtk_bin_get_child (GTK_BIN (widget));
240       if (child && gtk_widget_get_visible (child))
241         {
242           gint child_min, child_nat;
243
244           gtk_widget_get_preferred_width (child, &child_min, &child_nat);
245
246           *minimum += child_min + indicator_spacing;
247           *natural += child_nat + indicator_spacing;
248         }
249
250       *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
251       *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
252     }
253   else
254     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
255 }
256
257 static void
258 gtk_check_button_get_preferred_height (GtkWidget *widget,
259                                        gint      *minimum,
260                                        gint      *natural)
261 {
262   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
263
264   if (gtk_toggle_button_get_mode (toggle_button))
265     {
266       GtkWidget *child;
267       gint temp;
268       gint indicator_size;
269       gint indicator_spacing;
270       gint focus_width;
271       gint focus_pad;
272       guint border_width;
273
274       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
275
276       gtk_widget_style_get (GTK_WIDGET (widget),
277                             "focus-line-width", &focus_width,
278                             "focus-padding", &focus_pad,
279                             NULL);
280
281       *minimum = border_width * 2;
282       *natural = border_width * 2;
283
284       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
285                                    &indicator_size, &indicator_spacing);
286
287       child = gtk_bin_get_child (GTK_BIN (widget));
288       if (child && gtk_widget_get_visible (child))
289         {
290           gint child_min, child_nat;
291
292           gtk_widget_get_preferred_height (child, &child_min, &child_nat);
293
294           *minimum += child_min;
295           *natural += child_nat;
296         }
297
298       temp = indicator_size + indicator_spacing * 2;
299       *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
300       *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
301     }
302   else
303     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
304 }
305
306 static void
307 gtk_check_button_size_allocate (GtkWidget     *widget,
308                                 GtkAllocation *allocation)
309 {
310   GtkCheckButton *check_button;
311   GtkToggleButton *toggle_button;
312   GtkButton *button;
313   GtkAllocation child_allocation;
314
315   button = GTK_BUTTON (widget);
316   check_button = GTK_CHECK_BUTTON (widget);
317   toggle_button = GTK_TOGGLE_BUTTON (widget);
318
319   if (gtk_toggle_button_get_mode (toggle_button))
320     {
321       GtkWidget *child;
322       gint indicator_size;
323       gint indicator_spacing;
324       gint focus_width;
325       gint focus_pad;
326       
327       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
328       gtk_widget_style_get (widget,
329                             "focus-line-width", &focus_width,
330                             "focus-padding", &focus_pad,
331                             NULL);
332
333       gtk_widget_set_allocation (widget, allocation);
334
335       if (gtk_widget_get_realized (widget))
336         gdk_window_move_resize (gtk_button_get_event_window (button),
337                                 allocation->x, allocation->y,
338                                 allocation->width, allocation->height);
339
340       child = gtk_bin_get_child (GTK_BIN (button));
341       if (child && gtk_widget_get_visible (child))
342         {
343           GtkRequisition child_requisition;
344           guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
345
346           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
347
348           child_allocation.width = MIN (child_requisition.width,
349                                         allocation->width -
350                                         ((border_width + focus_width + focus_pad) * 2
351                                          + indicator_size + indicator_spacing * 3));
352           child_allocation.width = MAX (child_allocation.width, 1);
353
354           child_allocation.height = MIN (child_requisition.height,
355                                          allocation->height - (border_width + focus_width + focus_pad) * 2);
356           child_allocation.height = MAX (child_allocation.height, 1);
357           
358           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
359                                 allocation->x + focus_width + focus_pad);
360           child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
361
362           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
363             child_allocation.x = allocation->x + allocation->width
364               - (child_allocation.x - allocation->x + child_allocation.width);
365           
366           gtk_widget_size_allocate (child, &child_allocation);
367         }
368     }
369   else
370     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
371 }
372
373 static gint
374 gtk_check_button_draw (GtkWidget *widget,
375                        cairo_t   *cr)
376 {
377   GtkToggleButton *toggle_button;
378   GtkBin *bin;
379   GtkWidget *child;
380   
381   toggle_button = GTK_TOGGLE_BUTTON (widget);
382   bin = GTK_BIN (widget);
383
384   if (gtk_toggle_button_get_mode (toggle_button))
385     {
386       gtk_check_button_paint (widget, cr);
387
388       child = gtk_bin_get_child (bin);
389       if (child)
390         gtk_container_propagate_draw (GTK_CONTAINER (widget),
391                                       child,
392                                       cr);
393     }
394   else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
395     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
396
397   return FALSE;
398 }
399
400
401 static void
402 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
403                                  cairo_t        *cr)
404 {
405   GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
406
407   if (class->draw_indicator)
408     class->draw_indicator (check_button, cr);
409 }
410
411 static void
412 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
413                                       cairo_t        *cr)
414 {
415   GtkWidget *widget;
416   GtkWidget *child;
417   GtkButton *button;
418   GtkToggleButton *toggle_button;
419   GtkStateFlags state = 0;
420   gint x, y;
421   gint indicator_size;
422   gint indicator_spacing;
423   gint focus_width;
424   gint focus_pad;
425   guint border_width;
426   gboolean interior_focus;
427   GtkAllocation allocation;
428   GtkStyleContext *context;
429
430   widget = GTK_WIDGET (check_button);
431   button = GTK_BUTTON (check_button);
432   toggle_button = GTK_TOGGLE_BUTTON (check_button);
433
434   gtk_widget_get_allocation (widget, &allocation);
435   context = gtk_widget_get_style_context (widget);
436
437   gtk_widget_style_get (widget, 
438                         "interior-focus", &interior_focus,
439                         "focus-line-width", &focus_width, 
440                         "focus-padding", &focus_pad, 
441                         NULL);
442
443   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
444
445   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
446
447   x = indicator_spacing + border_width;
448   y = (allocation.height - indicator_size) / 2;
449
450   child = gtk_bin_get_child (GTK_BIN (check_button));
451   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
452     x += focus_width + focus_pad;      
453
454   if (gtk_toggle_button_get_inconsistent (toggle_button))
455     state |= GTK_STATE_FLAG_INCONSISTENT;
456   else if (gtk_toggle_button_get_active (toggle_button))
457     state |= GTK_STATE_FLAG_ACTIVE;
458
459   if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
460     state |= GTK_STATE_FLAG_SELECTED;
461
462   if (button->priv->in_button)
463     state |= GTK_STATE_FLAG_PRELIGHT;
464   else if (!gtk_widget_is_sensitive (widget))
465     state |= GTK_STATE_FLAG_INSENSITIVE;
466
467   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
468     x = allocation.width - (indicator_size + x);
469
470   gtk_style_context_set_state (context, state);
471
472   if (state & GTK_STATE_FLAG_PRELIGHT)
473     gtk_render_background (context, cr,
474                            border_width, border_width,
475                            allocation.width - (2 * border_width),
476                            allocation.height - (2 * border_width));
477
478   gtk_style_context_save (context);
479   gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
480
481   gtk_render_check (context, cr,
482                     x, y, indicator_size, indicator_size);
483
484   gtk_style_context_restore (context);
485 }