1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3 * Copyright (C) 2011 Red Hat, Inc.
5 * Authors: Carlos Garnacho <carlosg@gnome.org>
6 * Cosimo Cecchi <cosimoc@gnome.org>
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.
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.
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/>.
24 #include "gtkthemingbackgroundprivate.h"
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"
38 /* this is in case round() is not provided by the compiler,
39 * such as in the case of C89 compilers, like MSVC
41 #include "fallback-c89.c"
44 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
47 if (gtk_style_context_has_class (bg->context, "background"))
49 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
50 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
56 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
59 cairo_rectangle_t image_rect;
61 origin = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
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. */
67 case GTK_CSS_AREA_BORDER_BOX:
70 image_rect.width = bg->paint_area.width;
71 image_rect.height = bg->paint_area.height;
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;
79 case GTK_CSS_AREA_PADDING_BOX:
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;
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;
94 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
96 GtkCssArea clip = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
98 if (clip == GTK_CSS_AREA_PADDING_BOX)
100 _gtk_rounded_box_shrink (&bg->clip_box,
101 bg->border.top, bg->border.right,
102 bg->border.bottom, bg->border.left);
104 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
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);
115 _gtk_theming_background_get_cover_contain (GtkCssImage *image,
119 double *concrete_width,
120 double *concrete_height)
122 double aspect, image_aspect;
124 image_aspect = _gtk_css_image_get_aspect_ratio (image);
125 if (image_aspect == 0.0)
127 *concrete_width = width;
128 *concrete_height = height;
132 aspect = width / height;
134 if ((aspect >= image_aspect && cover) ||
135 (aspect < image_aspect && !cover))
137 *concrete_width = width;
138 *concrete_height = width / image_aspect;
142 *concrete_height = height;
143 *concrete_width = height * image_aspect;
148 _gtk_theming_background_paint (GtkThemingBackground *bg,
153 _gtk_rounded_box_path (&bg->clip_box, cr);
156 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
160 && bg->image_rect.width > 0
161 && bg->image_rect.height > 0)
163 GtkCssBackgroundRepeat hrepeat, vrepeat;
164 const GtkCssBackgroundSize *size;
165 const GtkCssValue *pos;
166 double image_width, image_height;
167 double width, height;
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,
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;
179 if (size->contain || size->cover)
180 _gtk_theming_background_get_cover_contain (bg->image,
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),
192 &image_width, &image_height);
195 if (image_width == width)
196 hrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
197 if (image_height == height)
198 vrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
200 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
202 if (hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT && vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT)
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);
212 int surface_width, surface_height;
213 cairo_surface_t *surface;
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):
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).
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.
232 if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
234 double n = round (width / image_width);
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;
243 if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
245 double n = round (height / image_height);
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;
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)
259 double n = floor (width / image_width);
260 surface_width = n ? round (width / n) : 0;
263 surface_width = round (image_width);
265 if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
267 double n = floor (height / image_height);
268 surface_height = n ? round (height / n) : 0;
271 surface_height = round (image_height);
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);
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);
291 hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
292 vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
301 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
304 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
310 _gtk_theming_background_init_context (GtkThemingBackground *bg)
312 bg->flags = gtk_style_context_get_state (bg->context);
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);
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
323 * In the future we might want to support different origins or clips, but
324 * right now we just shrink to the default.
326 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
328 _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->junction);
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);
335 _gtk_theming_background_apply_clip (bg);
336 _gtk_theming_background_apply_origin (bg);
338 bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
342 _gtk_theming_background_init (GtkThemingBackground *bg,
343 GtkThemingEngine *engine,
348 GtkJunctionSides junction)
350 GtkStyleContext *context;
352 g_assert (bg != NULL);
354 context = _gtk_theming_engine_get_context (engine);
355 _gtk_theming_background_init_from_context (bg, context,
361 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
362 GtkStyleContext *context,
367 GtkJunctionSides junction)
369 g_assert (bg != NULL);
371 bg->context = context;
373 bg->paint_area.x = x;
374 bg->paint_area.y = y;
375 bg->paint_area.width = width;
376 bg->paint_area.height = height;
379 bg->junction = junction;
381 _gtk_theming_background_init_context (bg);
385 _gtk_theming_background_render (GtkThemingBackground *bg,
389 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
391 _gtk_theming_background_apply_window_background (bg, cr);
392 _gtk_theming_background_paint (bg, cr);
393 _gtk_theming_background_apply_shadow (bg, cr);
399 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
401 return (bg->image != NULL) ? TRUE : FALSE;