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 "gtkcsstypesprivate.h"
25 #include "gtkthemingbackgroundprivate.h"
26 #include "gtkthemingengineprivate.h"
32 /* this is in case round() is not provided by the compiler,
33 * such as in the case of C89 compilers, like MSVC
35 #include "fallback-c89.c"
38 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
41 if (gtk_style_context_has_class (bg->context, "background"))
43 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
44 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
50 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
53 cairo_rectangle_t image_rect;
55 gtk_style_context_get (bg->context, bg->flags,
56 "background-origin", &origin,
59 /* The default size of the background image depends on the
60 background-origin value as this affects the top left
61 and the bottom right corners. */
63 case GTK_CSS_AREA_BORDER_BOX:
66 image_rect.width = bg->paint_area.width;
67 image_rect.height = bg->paint_area.height;
69 case GTK_CSS_AREA_CONTENT_BOX:
70 image_rect.x = bg->border.left + bg->padding.left;
71 image_rect.y = bg->border.top + bg->padding.top;
72 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
73 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
75 case GTK_CSS_AREA_PADDING_BOX:
77 image_rect.x = bg->border.left;
78 image_rect.y = bg->border.top;
79 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
80 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
84 /* XXX: image_rect might have negative width/height here.
85 * Do we need to do something about it? */
86 bg->image_rect = image_rect;
90 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
94 gtk_style_context_get (bg->context, bg->flags,
95 "background-clip", &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 GtkCssBackgroundSize *size;
165 double image_width, image_height;
166 double width, height;
168 size = g_value_get_boxed (_gtk_style_context_peek_property (bg->context, "background-size"));
169 gtk_style_context_get (bg->context, bg->flags,
170 "background-repeat", &hrepeat,
172 vrepeat = GTK_CSS_BACKGROUND_VERTICAL (hrepeat);
173 hrepeat = GTK_CSS_BACKGROUND_HORIZONTAL (hrepeat);
174 width = bg->image_rect.width;
175 height = bg->image_rect.height;
177 if (size->contain || size->cover)
178 _gtk_theming_background_get_cover_contain (bg->image,
185 _gtk_css_image_get_concrete_size (bg->image,
186 /* note: 0 does the right thing here for 'auto' */
187 _gtk_css_number_get (&size->width, width),
188 _gtk_css_number_get (&size->height, height),
190 &image_width, &image_height);
193 if (image_width == width)
194 hrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
195 if (image_height == height)
196 vrepeat = GTK_CSS_BACKGROUND_NO_REPEAT;
198 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
200 if (hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT && vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT)
202 /* shortcut for normal case */
203 _gtk_css_image_draw (bg->image, cr, image_width, image_height);
207 int surface_width, surface_height;
208 cairo_surface_t *surface;
211 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
212 * there is a second step. The UA must scale the image in that
213 * dimension (or both dimensions) so that it fits a whole number of
214 * times in the background positioning area. In the case of the width
215 * (height is analogous):
217 * If X ≠ 0 is the width of the image after step one and W is the width
218 * of the background positioning area, then the rounded width
219 * X' = W / round(W / X) where round() is a function that returns the
220 * nearest natural number (integer greater than zero).
222 * If ‘background-repeat’ is ‘round’ for one dimension only and if
223 * ‘background-size’ is ‘auto’ for the other dimension, then there is
224 * a third step: that other dimension is scaled so that the original
225 * aspect ratio is restored.
227 if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
229 double n = round (width / image_width);
233 if (vrepeat != GTK_CSS_BACKGROUND_ROUND
234 /* && vsize == auto (it is by default) */)
235 image_height *= width / (image_width * n);
236 image_width = width / n;
238 if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
240 double n = round (height / image_height);
244 if (hrepeat != GTK_CSS_BACKGROUND_ROUND
245 /* && hsize == auto (it is by default) */)
246 image_width *= height / (image_height * n);
247 image_height = height / n;
250 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
251 * to store the extra space. */
252 if (hrepeat == GTK_CSS_BACKGROUND_SPACE)
254 double n = floor (width / image_width);
255 surface_width = n ? round (width / n) : 0;
258 surface_width = round (image_width);
260 if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
262 double n = floor (height / image_height);
263 surface_height = n ? round (height / n) : 0;
266 surface_height = round (image_height);
268 surface = cairo_surface_create_similar (cairo_get_target (cr),
269 CAIRO_CONTENT_COLOR_ALPHA,
270 surface_width, surface_height);
271 cr2 = cairo_create (surface);
272 cairo_translate (cr2,
273 0.5 * (surface_width - image_width),
274 0.5 * (surface_height - image_height));
275 _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
278 cairo_set_source_surface (cr, surface,
279 /* background-position goes here */
281 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
282 cairo_surface_destroy (surface);
286 hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
287 vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
296 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
299 GtkShadow *box_shadow;
301 gtk_style_context_get (bg->context, bg->flags,
302 "box-shadow", &box_shadow,
305 if (box_shadow != NULL)
307 _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
308 _gtk_shadow_unref (box_shadow);
313 _gtk_theming_background_init_context (GtkThemingBackground *bg)
315 bg->flags = gtk_style_context_get_state (bg->context);
317 gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
318 gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
319 gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
321 /* In the CSS box model, by default the background positioning area is
322 * the padding-box, i.e. all the border-box minus the borders themselves,
323 * which determines also its default size, see
324 * http://dev.w3.org/csswg/css3-background/#background-origin
326 * In the future we might want to support different origins or clips, but
327 * right now we just shrink to the default.
329 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
331 _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->flags, bg->junction);
333 bg->clip_box = bg->padding_box;
334 _gtk_rounded_box_shrink (&bg->padding_box,
335 bg->border.top, bg->border.right,
336 bg->border.bottom, bg->border.left);
338 _gtk_theming_background_apply_clip (bg);
339 _gtk_theming_background_apply_origin (bg);
341 bg->image = g_value_get_object (_gtk_style_context_peek_property (bg->context, "background-image"));
345 _gtk_theming_background_init (GtkThemingBackground *bg,
346 GtkThemingEngine *engine,
351 GtkJunctionSides junction)
353 GtkStyleContext *context;
355 g_assert (bg != NULL);
357 context = _gtk_theming_engine_get_context (engine);
358 _gtk_theming_background_init_from_context (bg, context,
364 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
365 GtkStyleContext *context,
370 GtkJunctionSides junction)
372 g_assert (bg != NULL);
374 bg->context = context;
376 bg->paint_area.x = x;
377 bg->paint_area.y = y;
378 bg->paint_area.width = width;
379 bg->paint_area.height = height;
382 bg->junction = junction;
384 _gtk_theming_background_init_context (bg);
388 _gtk_theming_background_render (GtkThemingBackground *bg,
392 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
394 _gtk_theming_background_apply_window_background (bg, cr);
395 _gtk_theming_background_paint (bg, cr);
396 _gtk_theming_background_apply_shadow (bg, cr);
402 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
404 return (bg->image != NULL) ? TRUE : FALSE;