]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
Change FSF Address
[~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       GtkCssBackgroundSize *size;
165       double image_width, image_height;
166       double width, height;
167
168       size = g_value_get_boxed (_gtk_style_context_peek_property (bg->context, "background-size"));
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           /* shortcut for normal case */
203           _gtk_css_image_draw (bg->image, cr, image_width, image_height);
204         }
205       else
206         {
207           int surface_width, surface_height;
208           cairo_surface_t *surface;
209           cairo_t *cr2;
210
211           /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
212            * there is a second step. The UA must scale the image in that
213            * dimension (or both dimensions) so that it fits a whole number of
214            * times in the background positioning area. In the case of the width
215            * (height is analogous):
216            *
217            * If X ≠ 0 is the width of the image after step one and W is the width
218            * of the background positioning area, then the rounded width
219            * X' = W / round(W / X) where round() is a function that returns the
220            * nearest natural number (integer greater than zero). 
221            *
222            * If ‘background-repeat’ is ‘round’ for one dimension only and if
223            * ‘background-size’ is ‘auto’ for the other dimension, then there is
224            * a third step: that other dimension is scaled so that the original
225            * aspect ratio is restored. 
226            */
227           if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
228             {
229               double n = round (width / image_width);
230
231               n = MAX (1, n);
232
233               if (vrepeat != GTK_CSS_BACKGROUND_ROUND
234                   /* && vsize == auto (it is by default) */)
235                 image_height *= width / (image_width * n);
236               image_width = width / n;
237             }
238           if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
239             {
240               double n = round (height / image_height);
241
242               n = MAX (1, n);
243
244               if (hrepeat != GTK_CSS_BACKGROUND_ROUND
245                   /* && hsize == auto (it is by default) */)
246                 image_width *= height / (image_height * n);
247               image_height = height / n;
248             }
249
250           /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
251            * to store the extra space. */
252           if (hrepeat == GTK_CSS_BACKGROUND_SPACE)
253             {
254               double n = floor (width / image_width);
255               surface_width = n ? round (width / n) : 0;
256             }
257           else
258             surface_width = round (image_width);
259
260           if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
261             {
262               double n = floor (height / image_height);
263               surface_height = n ? round (height / n) : 0;
264             }
265           else
266             surface_height = round (image_height);
267
268           surface = cairo_surface_create_similar (cairo_get_target (cr),
269                                                   CAIRO_CONTENT_COLOR_ALPHA,
270                                                   surface_width, surface_height);
271           cr2 = cairo_create (surface);
272           cairo_translate (cr2,
273                            0.5 * (surface_width - image_width),
274                            0.5 * (surface_height - image_height));
275           _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
276           cairo_destroy (cr2);
277
278           cairo_set_source_surface (cr, surface,
279                                     /* background-position goes here */
280                                     0, 0);
281           cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
282           cairo_surface_destroy (surface);
283
284           cairo_rectangle (cr,
285                            0, 0,
286                            hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
287                            vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
288           cairo_fill (cr);
289         }
290     }
291
292   cairo_restore (cr);
293 }
294
295 static void
296 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
297                                       cairo_t              *cr)
298 {
299   GtkShadow *box_shadow;
300
301   gtk_style_context_get (bg->context, bg->flags,
302                          "box-shadow", &box_shadow,
303                          NULL);
304
305   if (box_shadow != NULL)
306     {
307       _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
308       _gtk_shadow_unref (box_shadow);
309     }
310 }
311
312 static void
313 _gtk_theming_background_init_context (GtkThemingBackground *bg)
314 {
315   bg->flags = gtk_style_context_get_state (bg->context);
316
317   gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
318   gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
319   gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
320
321   /* In the CSS box model, by default the background positioning area is
322    * the padding-box, i.e. all the border-box minus the borders themselves,
323    * which determines also its default size, see
324    * http://dev.w3.org/csswg/css3-background/#background-origin
325    *
326    * In the future we might want to support different origins or clips, but
327    * right now we just shrink to the default.
328    */
329   _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
330
331   _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->flags, bg->junction);
332
333   bg->clip_box = bg->padding_box;
334   _gtk_rounded_box_shrink (&bg->padding_box,
335                            bg->border.top, bg->border.right,
336                            bg->border.bottom, bg->border.left);
337
338   _gtk_theming_background_apply_clip (bg);
339   _gtk_theming_background_apply_origin (bg);
340
341   bg->image = g_value_get_object (_gtk_style_context_peek_property (bg->context, "background-image"));
342 }
343
344 void
345 _gtk_theming_background_init (GtkThemingBackground *bg,
346                               GtkThemingEngine     *engine,
347                               gdouble               x,
348                               gdouble               y,
349                               gdouble               width,
350                               gdouble               height,
351                               GtkJunctionSides      junction)
352 {
353   GtkStyleContext *context;
354
355   g_assert (bg != NULL);
356
357   context = _gtk_theming_engine_get_context (engine);
358   _gtk_theming_background_init_from_context (bg, context,
359                                              x, y, width, height,
360                                              junction);
361 }
362
363 void
364 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
365                                            GtkStyleContext      *context,
366                                            gdouble               x,
367                                            gdouble               y,
368                                            gdouble               width,
369                                            gdouble               height,
370                                            GtkJunctionSides      junction)
371 {
372   g_assert (bg != NULL);
373
374   bg->context = context;
375
376   bg->paint_area.x = x;
377   bg->paint_area.y = y;
378   bg->paint_area.width = width;
379   bg->paint_area.height = height;
380
381   bg->image = NULL;
382   bg->junction = junction;
383
384   _gtk_theming_background_init_context (bg);
385 }
386
387 void
388 _gtk_theming_background_render (GtkThemingBackground *bg,
389                                 cairo_t              *cr)
390 {
391   cairo_save (cr);
392   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
393
394   _gtk_theming_background_apply_window_background (bg, cr);
395   _gtk_theming_background_paint (bg, cr);
396   _gtk_theming_background_apply_shadow (bg, cr);
397
398   cairo_restore (cr);
399 }
400
401 gboolean
402 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
403 {
404   return (bg->image != NULL) ? TRUE : FALSE;
405 }