]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
cssvalue: Add a value for background-position
[~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 "gtkcsspositionvalueprivate.h"
31 #include "gtkcsstypesprivate.h"
32 #include "gtkthemingengineprivate.h"
33
34 #include <math.h>
35
36 #include <gdk/gdk.h>
37
38 /* this is in case round() is not provided by the compiler, 
39  * such as in the case of C89 compilers, like MSVC
40  */
41 #include "fallback-c89.c"
42
43 static void
44 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
45                                                  cairo_t              *cr)
46 {
47   if (gtk_style_context_has_class (bg->context, "background"))
48     {
49       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
50       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
51       cairo_paint (cr);
52     }
53 }
54
55 static void
56 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
57 {
58   GtkCssArea origin;
59   cairo_rectangle_t image_rect;
60
61   origin = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
62
63   /* The default size of the background image depends on the
64      background-origin value as this affects the top left
65      and the bottom right corners. */
66   switch (origin) {
67   case GTK_CSS_AREA_BORDER_BOX:
68     image_rect.x = 0;
69     image_rect.y = 0;
70     image_rect.width = bg->paint_area.width;
71     image_rect.height = bg->paint_area.height;
72     break;
73   case GTK_CSS_AREA_CONTENT_BOX:
74     image_rect.x = bg->border.left + bg->padding.left;
75     image_rect.y = bg->border.top + bg->padding.top;
76     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
77     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
78     break;
79   case GTK_CSS_AREA_PADDING_BOX:
80   default:
81     image_rect.x = bg->border.left;
82     image_rect.y = bg->border.top;
83     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
84     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
85     break;
86   }
87
88   /* XXX: image_rect might have negative width/height here.
89    * Do we need to do something about it? */
90   bg->image_rect = image_rect;
91 }
92
93 static void
94 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
95 {
96   GtkCssArea clip = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
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 GtkCssValue *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, GTK_CSS_PROPERTY_BACKGROUND_SIZE));
170       pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_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_position_value_get_x (pos, width - image_width),
206                            _gtk_css_position_value_get_y (pos, 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_position_value_get_x (pos, width - image_width),
285                                     _gtk_css_position_value_get_y (pos, 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   _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
305                                     cr,
306                                     &bg->padding_box);
307 }
308
309 static void
310 _gtk_theming_background_init_context (GtkThemingBackground *bg)
311 {
312   bg->flags = gtk_style_context_get_state (bg->context);
313
314   gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
315   gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
316   gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
317
318   /* In the CSS box model, by default the background positioning area is
319    * the padding-box, i.e. all the border-box minus the borders themselves,
320    * which determines also its default size, see
321    * http://dev.w3.org/csswg/css3-background/#background-origin
322    *
323    * In the future we might want to support different origins or clips, but
324    * right now we just shrink to the default.
325    */
326   _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
327
328   _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->junction);
329
330   bg->clip_box = bg->padding_box;
331   _gtk_rounded_box_shrink (&bg->padding_box,
332                            bg->border.top, bg->border.right,
333                            bg->border.bottom, bg->border.left);
334
335   _gtk_theming_background_apply_clip (bg);
336   _gtk_theming_background_apply_origin (bg);
337
338   bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
339 }
340
341 void
342 _gtk_theming_background_init (GtkThemingBackground *bg,
343                               GtkThemingEngine     *engine,
344                               gdouble               x,
345                               gdouble               y,
346                               gdouble               width,
347                               gdouble               height,
348                               GtkJunctionSides      junction)
349 {
350   GtkStyleContext *context;
351
352   g_assert (bg != NULL);
353
354   context = _gtk_theming_engine_get_context (engine);
355   _gtk_theming_background_init_from_context (bg, context,
356                                              x, y, width, height,
357                                              junction);
358 }
359
360 void
361 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
362                                            GtkStyleContext      *context,
363                                            gdouble               x,
364                                            gdouble               y,
365                                            gdouble               width,
366                                            gdouble               height,
367                                            GtkJunctionSides      junction)
368 {
369   g_assert (bg != NULL);
370
371   bg->context = context;
372
373   bg->paint_area.x = x;
374   bg->paint_area.y = y;
375   bg->paint_area.width = width;
376   bg->paint_area.height = height;
377
378   bg->image = NULL;
379   bg->junction = junction;
380
381   _gtk_theming_background_init_context (bg);
382 }
383
384 void
385 _gtk_theming_background_render (GtkThemingBackground *bg,
386                                 cairo_t              *cr)
387 {
388   cairo_save (cr);
389   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
390
391   _gtk_theming_background_apply_window_background (bg, cr);
392   _gtk_theming_background_paint (bg, cr);
393   _gtk_theming_background_apply_shadow (bg, cr);
394
395   cairo_restore (cr);
396 }
397
398 gboolean
399 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
400 {
401   return (bg->image != NULL) ? TRUE : FALSE;
402 }