]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
cssvalue: Make GtkCssShadowValue only hold one shadow
[~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 "gtkcssenumvalueprivate.h"
28 #include "gtkcssimagevalueprivate.h"
29 #include "gtkcsstypesprivate.h"
30 #include "gtkthemingengineprivate.h"
31
32 #include <math.h>
33
34 #include <gdk/gdk.h>
35
36 /* this is in case round() is not provided by the compiler, 
37  * such as in the case of C89 compilers, like MSVC
38  */
39 #include "fallback-c89.c"
40
41 static void
42 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
43                                                  cairo_t              *cr)
44 {
45   if (gtk_style_context_has_class (bg->context, "background"))
46     {
47       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
48       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
49       cairo_paint (cr);
50     }
51 }
52
53 static void
54 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
55 {
56   GtkCssArea origin;
57   cairo_rectangle_t image_rect;
58
59   origin = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
60
61   /* The default size of the background image depends on the
62      background-origin value as this affects the top left
63      and the bottom right corners. */
64   switch (origin) {
65   case GTK_CSS_AREA_BORDER_BOX:
66     image_rect.x = 0;
67     image_rect.y = 0;
68     image_rect.width = bg->paint_area.width;
69     image_rect.height = bg->paint_area.height;
70     break;
71   case GTK_CSS_AREA_CONTENT_BOX:
72     image_rect.x = bg->border.left + bg->padding.left;
73     image_rect.y = bg->border.top + bg->padding.top;
74     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
75     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
76     break;
77   case GTK_CSS_AREA_PADDING_BOX:
78   default:
79     image_rect.x = bg->border.left;
80     image_rect.y = bg->border.top;
81     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
82     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
83     break;
84   }
85
86   /* XXX: image_rect might have negative width/height here.
87    * Do we need to do something about it? */
88   bg->image_rect = image_rect;
89 }
90
91 static void
92 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
93 {
94   GtkCssArea clip = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
95
96   if (clip == GTK_CSS_AREA_PADDING_BOX)
97     {
98       _gtk_rounded_box_shrink (&bg->clip_box,
99                                bg->border.top, bg->border.right,
100                                bg->border.bottom, bg->border.left);
101     }
102   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
103     {
104       _gtk_rounded_box_shrink (&bg->clip_box,
105                                bg->border.top + bg->padding.top,
106                                bg->border.right + bg->padding.right,
107                                bg->border.bottom + bg->padding.bottom,
108                                bg->border.left + bg->padding.left);
109     }
110 }
111
112 static void
113 _gtk_theming_background_get_cover_contain (GtkCssImage *image,
114                                            gboolean     cover,
115                                            double       width,
116                                            double       height,
117                                            double      *concrete_width,
118                                            double      *concrete_height)
119 {
120   double aspect, image_aspect;
121   
122   image_aspect = _gtk_css_image_get_aspect_ratio (image);
123   if (image_aspect == 0.0)
124     {
125       *concrete_width = width;
126       *concrete_height = height;
127       return;
128     }
129
130   aspect = width / height;
131
132   if ((aspect >= image_aspect && cover) ||
133       (aspect < image_aspect && !cover))
134     {
135       *concrete_width = width;
136       *concrete_height = width / image_aspect;
137     }
138   else
139     {
140       *concrete_height = height;
141       *concrete_width = height * image_aspect;
142     }
143 }
144
145 static void
146 _gtk_theming_background_paint (GtkThemingBackground *bg,
147                                cairo_t              *cr)
148 {
149   cairo_save (cr);
150
151   _gtk_rounded_box_path (&bg->clip_box, cr);
152   cairo_clip (cr);
153
154   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
155   cairo_paint (cr);
156
157   if (bg->image
158       && bg->image_rect.width > 0
159       && bg->image_rect.height > 0)
160     {
161       GtkCssBackgroundRepeat hrepeat, vrepeat;
162       const GtkCssBackgroundSize *size;
163       const GtkCssBackgroundPosition *pos;
164       double image_width, image_height;
165       double width, height;
166
167       size = _gtk_css_value_get_background_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE));
168       pos = _gtk_css_value_get_background_position (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION));
169       gtk_style_context_get (bg->context, bg->flags,
170                              "background-repeat", &hrepeat,
171                              NULL);
172       vrepeat = GTK_CSS_BACKGROUND_VERTICAL (hrepeat);
173       hrepeat = GTK_CSS_BACKGROUND_HORIZONTAL (hrepeat);
174       width = bg->image_rect.width;
175       height = bg->image_rect.height;
176
177       if (size->contain || size->cover)
178         _gtk_theming_background_get_cover_contain (bg->image,
179                                                    size->cover,
180                                                    width,
181                                                    height,
182                                                    &image_width,
183                                                    &image_height);
184       else
185         _gtk_css_image_get_concrete_size (bg->image,
186                                           /* note: 0 does the right thing here for 'auto' */
187                                           _gtk_css_number_get (&size->width, width),
188                                           _gtk_css_number_get (&size->height, height),
189                                           width, height,
190                                           &image_width, &image_height);
191
192       /* optimization */
193       if (image_width == width)
194         hrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
195       if (image_height == height)
196         vrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
197
198       cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
199
200       if (hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT && vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT)
201         {
202           cairo_translate (cr,
203                            _gtk_css_number_get (&pos->x, bg->image_rect.width - image_width),
204                            _gtk_css_number_get (&pos->y, bg->image_rect.height - image_height));
205           /* shortcut for normal case */
206           _gtk_css_image_draw (bg->image, cr, image_width, image_height);
207         }
208       else
209         {
210           int surface_width, surface_height;
211           cairo_surface_t *surface;
212           cairo_t *cr2;
213
214           /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
215            * there is a second step. The UA must scale the image in that
216            * dimension (or both dimensions) so that it fits a whole number of
217            * times in the background positioning area. In the case of the width
218            * (height is analogous):
219            *
220            * If X ≠ 0 is the width of the image after step one and W is the width
221            * of the background positioning area, then the rounded width
222            * X' = W / round(W / X) where round() is a function that returns the
223            * nearest natural number (integer greater than zero). 
224            *
225            * If ‘background-repeat’ is ‘round’ for one dimension only and if
226            * ‘background-size’ is ‘auto’ for the other dimension, then there is
227            * a third step: that other dimension is scaled so that the original
228            * aspect ratio is restored. 
229            */
230           if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
231             {
232               double n = round (width / image_width);
233
234               n = MAX (1, n);
235
236               if (vrepeat != GTK_CSS_BACKGROUND_ROUND
237                   /* && vsize == auto (it is by default) */)
238                 image_height *= width / (image_width * n);
239               image_width = width / n;
240             }
241           if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
242             {
243               double n = round (height / image_height);
244
245               n = MAX (1, n);
246
247               if (hrepeat != GTK_CSS_BACKGROUND_ROUND
248                   /* && hsize == auto (it is by default) */)
249                 image_width *= height / (image_height * n);
250               image_height = height / n;
251             }
252
253           /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
254            * to store the extra space. */
255           if (hrepeat == GTK_CSS_BACKGROUND_SPACE)
256             {
257               double n = floor (width / image_width);
258               surface_width = n ? round (width / n) : 0;
259             }
260           else
261             surface_width = round (image_width);
262
263           if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
264             {
265               double n = floor (height / image_height);
266               surface_height = n ? round (height / n) : 0;
267             }
268           else
269             surface_height = round (image_height);
270
271           surface = cairo_surface_create_similar (cairo_get_target (cr),
272                                                   CAIRO_CONTENT_COLOR_ALPHA,
273                                                   surface_width, surface_height);
274           cr2 = cairo_create (surface);
275           cairo_translate (cr2,
276                            0.5 * (surface_width - image_width),
277                            0.5 * (surface_height - image_height));
278           _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
279           cairo_destroy (cr2);
280
281           cairo_set_source_surface (cr, surface,
282                                     _gtk_css_number_get (&pos->x, bg->image_rect.width - image_width),
283                                     _gtk_css_number_get (&pos->y, bg->image_rect.height - image_height));
284           cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
285           cairo_surface_destroy (surface);
286
287           cairo_rectangle (cr,
288                            0, 0,
289                            hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
290                            vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
291           cairo_fill (cr);
292         }
293     }
294
295   cairo_restore (cr);
296 }
297
298 static void
299 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
300                                       cairo_t              *cr)
301 {
302   GtkCssValue *shadows;
303   guint i;
304
305   shadows = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW);
306
307   for (i = 0; i < _gtk_css_array_value_get_n_values (shadows); i++)
308     {
309       _gtk_css_shadow_value_paint_box (_gtk_css_array_value_get_nth (shadows, i),
310                                        cr,
311                                        &bg->padding_box);
312     }
313 }
314
315 static void
316 _gtk_theming_background_init_context (GtkThemingBackground *bg)
317 {
318   bg->flags = gtk_style_context_get_state (bg->context);
319
320   gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
321   gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
322   gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
323
324   /* In the CSS box model, by default the background positioning area is
325    * the padding-box, i.e. all the border-box minus the borders themselves,
326    * which determines also its default size, see
327    * http://dev.w3.org/csswg/css3-background/#background-origin
328    *
329    * In the future we might want to support different origins or clips, but
330    * right now we just shrink to the default.
331    */
332   _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
333
334   _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->flags, bg->junction);
335
336   bg->clip_box = bg->padding_box;
337   _gtk_rounded_box_shrink (&bg->padding_box,
338                            bg->border.top, bg->border.right,
339                            bg->border.bottom, bg->border.left);
340
341   _gtk_theming_background_apply_clip (bg);
342   _gtk_theming_background_apply_origin (bg);
343
344   bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
345 }
346
347 void
348 _gtk_theming_background_init (GtkThemingBackground *bg,
349                               GtkThemingEngine     *engine,
350                               gdouble               x,
351                               gdouble               y,
352                               gdouble               width,
353                               gdouble               height,
354                               GtkJunctionSides      junction)
355 {
356   GtkStyleContext *context;
357
358   g_assert (bg != NULL);
359
360   context = _gtk_theming_engine_get_context (engine);
361   _gtk_theming_background_init_from_context (bg, context,
362                                              x, y, width, height,
363                                              junction);
364 }
365
366 void
367 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
368                                            GtkStyleContext      *context,
369                                            gdouble               x,
370                                            gdouble               y,
371                                            gdouble               width,
372                                            gdouble               height,
373                                            GtkJunctionSides      junction)
374 {
375   g_assert (bg != NULL);
376
377   bg->context = context;
378
379   bg->paint_area.x = x;
380   bg->paint_area.y = y;
381   bg->paint_area.width = width;
382   bg->paint_area.height = height;
383
384   bg->image = NULL;
385   bg->junction = junction;
386
387   _gtk_theming_background_init_context (bg);
388 }
389
390 void
391 _gtk_theming_background_render (GtkThemingBackground *bg,
392                                 cairo_t              *cr)
393 {
394   cairo_save (cr);
395   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
396
397   _gtk_theming_background_apply_window_background (bg, cr);
398   _gtk_theming_background_paint (bg, cr);
399   _gtk_theming_background_apply_shadow (bg, cr);
400
401   cairo_restore (cr);
402 }
403
404 gboolean
405 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
406 {
407   return (bg->image != NULL) ? TRUE : FALSE;
408 }