]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
cssvalue: Add a custom value for repeats
[~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 "gtkcssrepeatvalueprivate.h"
32 #include "gtkcsstypesprivate.h"
33 #include "gtkthemingengineprivate.h"
34
35 #include <math.h>
36
37 #include <gdk/gdk.h>
38
39 /* this is in case round() is not provided by the compiler, 
40  * such as in the case of C89 compilers, like MSVC
41  */
42 #include "fallback-c89.c"
43
44 static void
45 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
46                                                  cairo_t              *cr)
47 {
48   if (gtk_style_context_has_class (bg->context, "background"))
49     {
50       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
51       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
52       cairo_paint (cr);
53     }
54 }
55
56 static void
57 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
58 {
59   GtkCssArea origin;
60   cairo_rectangle_t image_rect;
61
62   origin = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
63
64   /* The default size of the background image depends on the
65      background-origin value as this affects the top left
66      and the bottom right corners. */
67   switch (origin) {
68   case GTK_CSS_AREA_BORDER_BOX:
69     image_rect.x = 0;
70     image_rect.y = 0;
71     image_rect.width = bg->paint_area.width;
72     image_rect.height = bg->paint_area.height;
73     break;
74   case GTK_CSS_AREA_CONTENT_BOX:
75     image_rect.x = bg->border.left + bg->padding.left;
76     image_rect.y = bg->border.top + bg->padding.top;
77     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
78     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
79     break;
80   case GTK_CSS_AREA_PADDING_BOX:
81   default:
82     image_rect.x = bg->border.left;
83     image_rect.y = bg->border.top;
84     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
85     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
86     break;
87   }
88
89   /* XXX: image_rect might have negative width/height here.
90    * Do we need to do something about it? */
91   bg->image_rect = image_rect;
92 }
93
94 static void
95 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
96 {
97   GtkCssArea clip = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
98
99   if (clip == GTK_CSS_AREA_PADDING_BOX)
100     {
101       _gtk_rounded_box_shrink (&bg->clip_box,
102                                bg->border.top, bg->border.right,
103                                bg->border.bottom, bg->border.left);
104     }
105   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
106     {
107       _gtk_rounded_box_shrink (&bg->clip_box,
108                                bg->border.top + bg->padding.top,
109                                bg->border.right + bg->padding.right,
110                                bg->border.bottom + bg->padding.bottom,
111                                bg->border.left + bg->padding.left);
112     }
113 }
114
115 static void
116 _gtk_theming_background_get_cover_contain (GtkCssImage *image,
117                                            gboolean     cover,
118                                            double       width,
119                                            double       height,
120                                            double      *concrete_width,
121                                            double      *concrete_height)
122 {
123   double aspect, image_aspect;
124   
125   image_aspect = _gtk_css_image_get_aspect_ratio (image);
126   if (image_aspect == 0.0)
127     {
128       *concrete_width = width;
129       *concrete_height = height;
130       return;
131     }
132
133   aspect = width / height;
134
135   if ((aspect >= image_aspect && cover) ||
136       (aspect < image_aspect && !cover))
137     {
138       *concrete_width = width;
139       *concrete_height = width / image_aspect;
140     }
141   else
142     {
143       *concrete_height = height;
144       *concrete_width = height * image_aspect;
145     }
146 }
147
148 static void
149 _gtk_theming_background_paint (GtkThemingBackground *bg,
150                                cairo_t              *cr)
151 {
152   cairo_save (cr);
153
154   _gtk_rounded_box_path (&bg->clip_box, cr);
155   cairo_clip (cr);
156
157   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
158   cairo_paint (cr);
159
160   if (bg->image
161       && bg->image_rect.width > 0
162       && bg->image_rect.height > 0)
163     {
164       const GtkCssBackgroundSize *size;
165       const GtkCssValue *pos, *repeat;
166       double image_width, image_height;
167       double width, height;
168       GtkCssRepeatStyle hrepeat, vrepeat;
169
170       size = _gtk_css_value_get_background_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE));
171       pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION);
172       repeat = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT);
173       hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
174       vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
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_REPEAT_STYLE_NO_REPEAT;
196       if (image_height == height)
197         vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
198
199       cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
200
201       if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
202         {
203           cairo_translate (cr,
204                            _gtk_css_position_value_get_x (pos, width - image_width),
205                            _gtk_css_position_value_get_y (pos, 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_REPEAT_STYLE_ROUND)
232             {
233               double n = round (width / image_width);
234
235               n = MAX (1, n);
236
237               if (vrepeat != GTK_CSS_REPEAT_STYLE_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_REPEAT_STYLE_ROUND)
243             {
244               double n = round (height / image_height);
245
246               n = MAX (1, n);
247
248               if (hrepeat != GTK_CSS_REPEAT_STYLE_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_REPEAT_STYLE_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_REPEAT_STYLE_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_position_value_get_x (pos, width - image_width),
284                                     _gtk_css_position_value_get_y (pos, 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_REPEAT_STYLE_NO_REPEAT ? image_width : width,
291                            vrepeat == GTK_CSS_REPEAT_STYLE_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->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 }