]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkthemingbackground.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2011 Red Hat, Inc.
4  * 
5  * Authors: Carlos Garnacho <carlosg@gnome.org>
6  *          Cosimo Cecchi <cosimoc@gnome.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "config.h"
23
24 #include "gtkthemingbackgroundprivate.h"
25
26 #include "gtkcssarrayvalueprivate.h"
27 #include "gtkcssbgsizevalueprivate.h"
28 #include "gtkcssenumvalueprivate.h"
29 #include "gtkcssimagevalueprivate.h"
30 #include "gtkcssshadowsvalueprivate.h"
31 #include "gtkcsspositionvalueprivate.h"
32 #include "gtkcssrepeatvalueprivate.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkthemingengineprivate.h"
35
36 #include <math.h>
37
38 #include <gdk/gdk.h>
39
40 /* this is in case round() is not provided by the compiler, 
41  * such as in the case of C89 compilers, like MSVC
42  */
43 #include "fallback-c89.c"
44
45 static const GtkRoundedBox *
46 gtk_theming_background_get_box (GtkThemingBackground *bg,
47                                 GtkCssArea            area)
48 {
49   switch (area)
50     {
51     case GTK_CSS_AREA_BORDER_BOX:
52       return &bg->border_box;
53     case GTK_CSS_AREA_PADDING_BOX:
54       return &bg->padding_box;
55     case GTK_CSS_AREA_CONTENT_BOX:
56       return &bg->content_box;
57     default:
58       g_return_val_if_reached (&bg->border_box);
59   }
60 }
61
62 static void
63 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
64                                      cairo_t              *cr,
65                                      GtkCssValue          *background_image)
66 {
67   gint n_values = _gtk_css_array_value_get_n_values (background_image);
68   GtkCssArea clip = _gtk_css_area_value_get 
69     (_gtk_css_array_value_get_nth 
70      (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP), 
71       n_values - 1));
72
73   cairo_save (cr);
74   _gtk_rounded_box_path (gtk_theming_background_get_box (bg, clip), cr);
75   cairo_clip (cr);
76
77   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
78   cairo_paint (cr);
79
80   cairo_restore (cr);
81 }
82
83 static void
84 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
85                                      guint                 idx,
86                                      cairo_t              *cr)
87 {
88   GtkCssRepeatStyle hrepeat, vrepeat;
89   const GtkCssValue *pos, *repeat;
90   GtkCssImage *image;
91   const GtkRoundedBox *origin;
92   double image_width, image_height;
93   double width, height;
94
95   pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), idx);
96   repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), idx);
97   hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
98   vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
99   image = _gtk_css_image_value_get_image (
100               _gtk_css_array_value_get_nth (
101                   _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
102                   idx));
103   origin = gtk_theming_background_get_box (
104                bg,
105                _gtk_css_area_value_get (
106                    _gtk_css_array_value_get_nth (
107                        _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN),
108                        idx)));
109   width = origin->box.width;
110   height = origin->box.height;
111
112   if (image == NULL || width <= 0 || height <= 0)
113     return;
114
115   _gtk_css_bg_size_value_compute_size (_gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE), idx),
116                                        image,
117                                        width,
118                                        height,
119                                        &image_width,
120                                        &image_height);
121
122   if (image_width <= 0 || image_height <= 0)
123     return;
124
125   /* optimization */
126   if (image_width == width)
127     hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
128   if (image_height == height)
129     vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
130
131
132   cairo_save (cr);
133
134   _gtk_rounded_box_path (
135       gtk_theming_background_get_box (
136           bg,
137           _gtk_css_area_value_get (
138               _gtk_css_array_value_get_nth (
139                   _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
140                   idx))),
141       cr);
142   cairo_clip (cr);
143
144
145   cairo_translate (cr, origin->box.x, origin->box.y);
146
147   if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
148     {
149       cairo_translate (cr,
150                        _gtk_css_position_value_get_x (pos, width - image_width),
151                        _gtk_css_position_value_get_y (pos, height - image_height));
152       /* shortcut for normal case */
153       _gtk_css_image_draw (image, cr, image_width, image_height);
154     }
155   else
156     {
157       int surface_width, surface_height;
158       cairo_rectangle_t fill_rect;
159       cairo_surface_t *surface;
160       cairo_t *cr2;
161
162       /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
163        * there is a second step. The UA must scale the image in that
164        * dimension (or both dimensions) so that it fits a whole number of
165        * times in the background positioning area. In the case of the width
166        * (height is analogous):
167        *
168        * If X ≠ 0 is the width of the image after step one and W is the width
169        * of the background positioning area, then the rounded width
170        * X' = W / round(W / X) where round() is a function that returns the
171        * nearest natural number (integer greater than zero). 
172        *
173        * If ‘background-repeat’ is ‘round’ for one dimension only and if
174        * ‘background-size’ is ‘auto’ for the other dimension, then there is
175        * a third step: that other dimension is scaled so that the original
176        * aspect ratio is restored. 
177        */
178       if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
179         {
180           double n = round (width / image_width);
181
182           n = MAX (1, n);
183
184           if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
185               /* && vsize == auto (it is by default) */)
186             image_height *= width / (image_width * n);
187           image_width = width / n;
188         }
189       if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
190         {
191           double n = round (height / image_height);
192
193           n = MAX (1, n);
194
195           if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
196               /* && hsize == auto (it is by default) */)
197             image_width *= height / (image_height * n);
198           image_height = height / n;
199         }
200
201       /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
202        * to store the extra space. */
203       if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
204         {
205           double n = floor (width / image_width);
206           surface_width = n ? round (width / n) : 0;
207         }
208       else
209         surface_width = round (image_width);
210
211       if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
212         {
213           double n = floor (height / image_height);
214           surface_height = n ? round (height / n) : 0;
215         }
216       else
217         surface_height = round (image_height);
218
219       surface = cairo_surface_create_similar (cairo_get_target (cr),
220                                               CAIRO_CONTENT_COLOR_ALPHA,
221                                               surface_width, surface_height);
222       cr2 = cairo_create (surface);
223       cairo_translate (cr2,
224                        0.5 * (surface_width - image_width),
225                        0.5 * (surface_height - image_height));
226       _gtk_css_image_draw (image, cr2, image_width, image_height);
227       cairo_destroy (cr2);
228
229       cairo_set_source_surface (cr, surface,
230                                 _gtk_css_position_value_get_x (pos, width - image_width),
231                                 _gtk_css_position_value_get_y (pos, height - image_height));
232       cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
233       cairo_surface_destroy (surface);
234
235       if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
236         {
237           fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
238           fill_rect.width = image_width;
239         }
240       else
241         {
242           fill_rect.x = 0;
243           fill_rect.width = width;
244         }
245
246       if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
247         {
248           fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
249           fill_rect.height = image_height;
250         }
251       else
252         {
253           fill_rect.y = 0;
254           fill_rect.height = height;
255         }
256
257       cairo_rectangle (cr, fill_rect.x, fill_rect.y,
258                        fill_rect.width, fill_rect.height);
259       cairo_fill (cr);
260     }
261
262
263   cairo_restore (cr);
264 }
265
266 static void
267 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
268                                       cairo_t              *cr)
269 {
270   _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
271                                     cr,
272                                     &bg->padding_box);
273 }
274
275 static void
276 _gtk_theming_background_init_context (GtkThemingBackground *bg)
277 {
278   GtkStateFlags flags = gtk_style_context_get_state (bg->context);
279   GtkBorder border, padding;
280
281   gtk_style_context_get_border (bg->context, flags, &border);
282   gtk_style_context_get_padding (bg->context, flags, &padding);
283   gtk_style_context_get_background_color (bg->context, flags, &bg->bg_color);
284
285   /* In the CSS box model, by default the background positioning area is
286    * the padding-box, i.e. all the border-box minus the borders themselves,
287    * which determines also its default size, see
288    * http://dev.w3.org/csswg/css3-background/#background-origin
289    *
290    * In the future we might want to support different origins or clips, but
291    * right now we just shrink to the default.
292    */
293   _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
294   _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
295
296   bg->padding_box = bg->border_box;
297   _gtk_rounded_box_shrink (&bg->padding_box,
298                            border.top, border.right,
299                            border.bottom, border.left);
300
301   bg->content_box = bg->padding_box;
302   _gtk_rounded_box_shrink (&bg->content_box,
303                            padding.top, padding.right,
304                            padding.bottom, padding.left);
305 }
306
307 void
308 _gtk_theming_background_init (GtkThemingBackground *bg,
309                               GtkThemingEngine     *engine,
310                               gdouble               x,
311                               gdouble               y,
312                               gdouble               width,
313                               gdouble               height,
314                               GtkJunctionSides      junction)
315 {
316   GtkStyleContext *context;
317
318   g_assert (bg != NULL);
319
320   context = _gtk_theming_engine_get_context (engine);
321   _gtk_theming_background_init_from_context (bg, context,
322                                              x, y, width, height,
323                                              junction);
324 }
325
326 void
327 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
328                                            GtkStyleContext      *context,
329                                            gdouble               x,
330                                            gdouble               y,
331                                            gdouble               width,
332                                            gdouble               height,
333                                            GtkJunctionSides      junction)
334 {
335   g_assert (bg != NULL);
336
337   bg->context = context;
338
339   bg->paint_area.x = x;
340   bg->paint_area.y = y;
341   bg->paint_area.width = width;
342   bg->paint_area.height = height;
343
344   bg->junction = junction;
345
346   _gtk_theming_background_init_context (bg);
347 }
348
349 void
350 _gtk_theming_background_render (GtkThemingBackground *bg,
351                                 cairo_t              *cr)
352 {
353   gint idx;
354   GtkCssValue *background_image;
355
356   background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
357
358   cairo_save (cr);
359   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
360
361   _gtk_theming_background_paint_color (bg, cr, background_image);
362
363   for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
364     {
365       _gtk_theming_background_paint_layer (bg, idx, cr);
366     }
367
368   _gtk_theming_background_apply_shadow (bg, cr);
369
370   cairo_restore (cr);
371 }
372
373 gboolean
374 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
375 {
376   GtkCssImage *image;
377   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
378
379   if (_gtk_css_array_value_get_n_values (value) == 0)
380     return FALSE;
381
382   image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
383   return (image != NULL);
384 }