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