]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
styleproperty: make background-repeat an array property
[~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 "gtkcssbgsizevalueprivate.h"
28 #include "gtkcssenumvalueprivate.h"
29 #include "gtkcssimagevalueprivate.h"
30 #include "gtkcssshadowsvalueprivate.h"
31 #include "gtkcsspositionvalueprivate.h"
32 #include "gtkcssrepeatvalueprivate.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkthemingengineprivate.h"
35
36 #include <math.h>
37
38 #include <gdk/gdk.h>
39
40 /* this is in case round() is not provided by the compiler, 
41  * such as in the case of C89 compilers, like MSVC
42  */
43 #include "fallback-c89.c"
44
45 static void
46 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
47                                                  cairo_t              *cr)
48 {
49   if (gtk_style_context_has_class (bg->context, "background"))
50     {
51       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
52       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
53       cairo_paint (cr);
54     }
55 }
56
57 static void
58 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
59 {
60   cairo_rectangle_t image_rect;
61   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
62   GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, 0));
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   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP);
98   GtkCssArea clip = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, 0));
99
100   if (clip == GTK_CSS_AREA_PADDING_BOX)
101     {
102       _gtk_rounded_box_shrink (&bg->clip_box,
103                                bg->border.top, bg->border.right,
104                                bg->border.bottom, bg->border.left);
105     }
106   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
107     {
108       _gtk_rounded_box_shrink (&bg->clip_box,
109                                bg->border.top + bg->padding.top,
110                                bg->border.right + bg->padding.right,
111                                bg->border.bottom + bg->padding.bottom,
112                                bg->border.left + bg->padding.left);
113     }
114 }
115
116 static void
117 _gtk_theming_background_paint (GtkThemingBackground *bg,
118                                cairo_t              *cr)
119 {
120   cairo_save (cr);
121
122   _gtk_rounded_box_path (&bg->clip_box, cr);
123   cairo_clip (cr);
124
125   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
126   cairo_paint (cr);
127
128   if (bg->image
129       && bg->image_rect.width > 0
130       && bg->image_rect.height > 0)
131     {
132       const GtkCssValue *pos, *repeat;
133       double image_width, image_height;
134       double width, height;
135       GtkCssRepeatStyle hrepeat, vrepeat;
136
137       pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION);
138       repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), 0);
139       hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
140       vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
141       width = bg->image_rect.width;
142       height = bg->image_rect.height;
143
144       _gtk_css_bg_size_value_compute_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE),
145                                            bg->image,
146                                            width,
147                                            height,
148                                            &image_width,
149                                            &image_height);
150
151       /* optimization */
152       if (image_width == width)
153         hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
154       if (image_height == height)
155         vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
156
157       cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
158
159       if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
160         {
161           cairo_translate (cr,
162                            _gtk_css_position_value_get_x (pos, width - image_width),
163                            _gtk_css_position_value_get_y (pos, height - image_height));
164           /* shortcut for normal case */
165           _gtk_css_image_draw (bg->image, cr, image_width, image_height);
166         }
167       else
168         {
169           int surface_width, surface_height;
170           cairo_surface_t *surface;
171           cairo_t *cr2;
172
173           /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
174            * there is a second step. The UA must scale the image in that
175            * dimension (or both dimensions) so that it fits a whole number of
176            * times in the background positioning area. In the case of the width
177            * (height is analogous):
178            *
179            * If X ≠ 0 is the width of the image after step one and W is the width
180            * of the background positioning area, then the rounded width
181            * X' = W / round(W / X) where round() is a function that returns the
182            * nearest natural number (integer greater than zero). 
183            *
184            * If ‘background-repeat’ is ‘round’ for one dimension only and if
185            * ‘background-size’ is ‘auto’ for the other dimension, then there is
186            * a third step: that other dimension is scaled so that the original
187            * aspect ratio is restored. 
188            */
189           if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
190             {
191               double n = round (width / image_width);
192
193               n = MAX (1, n);
194
195               if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
196                   /* && vsize == auto (it is by default) */)
197                 image_height *= width / (image_width * n);
198               image_width = width / n;
199             }
200           if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
201             {
202               double n = round (height / image_height);
203
204               n = MAX (1, n);
205
206               if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
207                   /* && hsize == auto (it is by default) */)
208                 image_width *= height / (image_height * n);
209               image_height = height / n;
210             }
211
212           /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
213            * to store the extra space. */
214           if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
215             {
216               double n = floor (width / image_width);
217               surface_width = n ? round (width / n) : 0;
218             }
219           else
220             surface_width = round (image_width);
221
222           if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
223             {
224               double n = floor (height / image_height);
225               surface_height = n ? round (height / n) : 0;
226             }
227           else
228             surface_height = round (image_height);
229
230           surface = cairo_surface_create_similar (cairo_get_target (cr),
231                                                   CAIRO_CONTENT_COLOR_ALPHA,
232                                                   surface_width, surface_height);
233           cr2 = cairo_create (surface);
234           cairo_translate (cr2,
235                            0.5 * (surface_width - image_width),
236                            0.5 * (surface_height - image_height));
237           _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
238           cairo_destroy (cr2);
239
240           cairo_set_source_surface (cr, surface,
241                                     _gtk_css_position_value_get_x (pos, width - image_width),
242                                     _gtk_css_position_value_get_y (pos, height - image_height));
243           cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
244           cairo_surface_destroy (surface);
245
246           cairo_rectangle (cr,
247                            0, 0,
248                            hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_width : width,
249                            vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_height : height);
250           cairo_fill (cr);
251         }
252     }
253
254   cairo_restore (cr);
255 }
256
257 static void
258 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
259                                       cairo_t              *cr)
260 {
261   _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
262                                     cr,
263                                     &bg->padding_box);
264 }
265
266 static void
267 _gtk_theming_background_init_context (GtkThemingBackground *bg)
268 {
269   bg->flags = gtk_style_context_get_state (bg->context);
270
271   gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
272   gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
273   gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
274
275   /* In the CSS box model, by default the background positioning area is
276    * the padding-box, i.e. all the border-box minus the borders themselves,
277    * which determines also its default size, see
278    * http://dev.w3.org/csswg/css3-background/#background-origin
279    *
280    * In the future we might want to support different origins or clips, but
281    * right now we just shrink to the default.
282    */
283   _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
284
285   _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->junction);
286
287   bg->clip_box = bg->padding_box;
288   _gtk_rounded_box_shrink (&bg->padding_box,
289                            bg->border.top, bg->border.right,
290                            bg->border.bottom, bg->border.left);
291
292   _gtk_theming_background_apply_clip (bg);
293   _gtk_theming_background_apply_origin (bg);
294
295   bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
296 }
297
298 void
299 _gtk_theming_background_init (GtkThemingBackground *bg,
300                               GtkThemingEngine     *engine,
301                               gdouble               x,
302                               gdouble               y,
303                               gdouble               width,
304                               gdouble               height,
305                               GtkJunctionSides      junction)
306 {
307   GtkStyleContext *context;
308
309   g_assert (bg != NULL);
310
311   context = _gtk_theming_engine_get_context (engine);
312   _gtk_theming_background_init_from_context (bg, context,
313                                              x, y, width, height,
314                                              junction);
315 }
316
317 void
318 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
319                                            GtkStyleContext      *context,
320                                            gdouble               x,
321                                            gdouble               y,
322                                            gdouble               width,
323                                            gdouble               height,
324                                            GtkJunctionSides      junction)
325 {
326   g_assert (bg != NULL);
327
328   bg->context = context;
329
330   bg->paint_area.x = x;
331   bg->paint_area.y = y;
332   bg->paint_area.width = width;
333   bg->paint_area.height = height;
334
335   bg->image = NULL;
336   bg->junction = junction;
337
338   _gtk_theming_background_init_context (bg);
339 }
340
341 void
342 _gtk_theming_background_render (GtkThemingBackground *bg,
343                                 cairo_t              *cr)
344 {
345   cairo_save (cr);
346   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
347
348   _gtk_theming_background_apply_window_background (bg, cr);
349   _gtk_theming_background_paint (bg, cr);
350   _gtk_theming_background_apply_shadow (bg, cr);
351
352   cairo_restore (cr);
353 }
354
355 gboolean
356 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
357 {
358   return (bg->image != NULL) ? TRUE : FALSE;
359 }