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