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