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