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, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "gtkcsstypesprivate.h"
27 #include "gtkthemingbackgroundprivate.h"
28 #include "gtkthemingengineprivate.h"
34 /* this is in case round() is not provided by the compiler,
35 * such as in the case of C89 compilers, like MSVC
37 #include "fallback-c89.c"
40 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
43 if (gtk_style_context_has_class (bg->context, "background"))
45 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
46 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
52 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
55 cairo_rectangle_t image_rect;
57 gtk_style_context_get (bg->context, bg->flags,
58 "background-origin", &origin,
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. */
65 case GTK_CSS_AREA_BORDER_BOX:
68 image_rect.width = bg->paint_area.width;
69 image_rect.height = bg->paint_area.height;
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;
77 case GTK_CSS_AREA_PADDING_BOX:
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;
86 /* XXX: image_rect might have negative width/height here.
87 * Do we need to do something about it? */
88 bg->image_rect = image_rect;
92 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
96 gtk_style_context_get (bg->context, bg->flags,
97 "background-clip", &clip,
100 if (clip == GTK_CSS_AREA_PADDING_BOX)
102 _gtk_rounded_box_shrink (&bg->clip_box,
103 bg->border.top, bg->border.right,
104 bg->border.bottom, bg->border.left);
106 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
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);
117 _gtk_theming_background_get_cover_contain (GtkCssImage *image,
121 double *concrete_width,
122 double *concrete_height)
124 double aspect, image_aspect;
126 image_aspect = _gtk_css_image_get_aspect_ratio (image);
127 if (image_aspect == 0.0)
129 *concrete_width = width;
130 *concrete_height = height;
134 aspect = width / height;
136 if ((aspect >= image_aspect && cover) ||
137 (aspect < image_aspect && !cover))
139 *concrete_width = width;
140 *concrete_height = width / image_aspect;
144 *concrete_height = height;
145 *concrete_width = height * image_aspect;
150 _gtk_theming_background_paint (GtkThemingBackground *bg,
155 _gtk_rounded_box_path (&bg->clip_box, cr);
158 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
162 && bg->image_rect.width > 0
163 && bg->image_rect.height > 0)
165 GtkCssBackgroundRepeat hrepeat, vrepeat;
166 GtkCssBackgroundSize *size;
167 double image_width, image_height;
168 double width, height;
170 size = g_value_get_boxed (_gtk_style_context_peek_property (bg->context, "background-size"));
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)
204 /* shortcut for normal case */
205 _gtk_css_image_draw (bg->image, cr, image_width, image_height);
209 int surface_width, surface_height;
210 cairo_surface_t *surface;
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):
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).
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.
229 if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
231 double n = round (width / image_width);
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;
240 if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
242 double n = round (height / image_height);
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;
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)
256 double n = floor (width / image_width);
257 surface_width = n ? round (width / n) : 0;
260 surface_width = round (image_width);
262 if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
264 double n = floor (height / image_height);
265 surface_height = n ? round (height / n) : 0;
268 surface_height = round (image_height);
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);
280 cairo_set_source_surface (cr, surface,
281 /* background-position goes here */
283 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
284 cairo_surface_destroy (surface);
288 hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
289 vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
298 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
301 GtkShadow *box_shadow;
303 gtk_style_context_get (bg->context, bg->flags,
304 "box-shadow", &box_shadow,
307 if (box_shadow != NULL)
309 _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
310 _gtk_shadow_unref (box_shadow);
315 _gtk_theming_background_init_context (GtkThemingBackground *bg)
317 bg->flags = gtk_style_context_get_state (bg->context);
319 gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
320 gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
321 gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
323 /* In the CSS box model, by default the background positioning area is
324 * the padding-box, i.e. all the border-box minus the borders themselves,
325 * which determines also its default size, see
326 * http://dev.w3.org/csswg/css3-background/#background-origin
328 * In the future we might want to support different origins or clips, but
329 * right now we just shrink to the default.
331 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
333 _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->flags, bg->junction);
335 bg->clip_box = bg->padding_box;
336 _gtk_rounded_box_shrink (&bg->padding_box,
337 bg->border.top, bg->border.right,
338 bg->border.bottom, bg->border.left);
340 _gtk_theming_background_apply_clip (bg);
341 _gtk_theming_background_apply_origin (bg);
343 bg->image = g_value_get_object (_gtk_style_context_peek_property (bg->context, "background-image"));
347 _gtk_theming_background_init (GtkThemingBackground *bg,
348 GtkThemingEngine *engine,
353 GtkJunctionSides junction)
355 GtkStyleContext *context;
357 g_assert (bg != NULL);
359 context = _gtk_theming_engine_get_context (engine);
360 _gtk_theming_background_init_from_context (bg, context,
366 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
367 GtkStyleContext *context,
372 GtkJunctionSides junction)
374 g_assert (bg != NULL);
376 bg->context = context;
378 bg->paint_area.x = x;
379 bg->paint_area.y = y;
380 bg->paint_area.width = width;
381 bg->paint_area.height = height;
384 bg->junction = junction;
386 _gtk_theming_background_init_context (bg);
390 _gtk_theming_background_render (GtkThemingBackground *bg,
394 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
396 _gtk_theming_background_apply_window_background (bg, cr);
397 _gtk_theming_background_paint (bg, cr);
398 _gtk_theming_background_apply_shadow (bg, cr);
404 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
406 return (bg->image != NULL) ? TRUE : FALSE;