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 "gtkcssrepeatvalueprivate.h"
32 #include "gtkcsstypesprivate.h"
33 #include "gtkthemingengineprivate.h"
39 /* this is in case round() is not provided by the compiler,
40 * such as in the case of C89 compilers, like MSVC
42 #include "fallback-c89.c"
45 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
48 if (gtk_style_context_has_class (bg->context, "background"))
50 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
51 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
57 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
60 cairo_rectangle_t image_rect;
62 origin = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
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. */
68 case GTK_CSS_AREA_BORDER_BOX:
71 image_rect.width = bg->paint_area.width;
72 image_rect.height = bg->paint_area.height;
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;
80 case GTK_CSS_AREA_PADDING_BOX:
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;
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;
95 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
97 GtkCssArea clip = _gtk_css_area_value_get (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP));
99 if (clip == GTK_CSS_AREA_PADDING_BOX)
101 _gtk_rounded_box_shrink (&bg->clip_box,
102 bg->border.top, bg->border.right,
103 bg->border.bottom, bg->border.left);
105 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
107 _gtk_rounded_box_shrink (&bg->clip_box,
108 bg->border.top + bg->padding.top,
109 bg->border.right + bg->padding.right,
110 bg->border.bottom + bg->padding.bottom,
111 bg->border.left + bg->padding.left);
116 _gtk_theming_background_get_cover_contain (GtkCssImage *image,
120 double *concrete_width,
121 double *concrete_height)
123 double aspect, image_aspect;
125 image_aspect = _gtk_css_image_get_aspect_ratio (image);
126 if (image_aspect == 0.0)
128 *concrete_width = width;
129 *concrete_height = height;
133 aspect = width / height;
135 if ((aspect >= image_aspect && cover) ||
136 (aspect < image_aspect && !cover))
138 *concrete_width = width;
139 *concrete_height = width / image_aspect;
143 *concrete_height = height;
144 *concrete_width = height * image_aspect;
149 _gtk_theming_background_paint (GtkThemingBackground *bg,
154 _gtk_rounded_box_path (&bg->clip_box, cr);
157 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
161 && bg->image_rect.width > 0
162 && bg->image_rect.height > 0)
164 const GtkCssBackgroundSize *size;
165 const GtkCssValue *pos, *repeat;
166 double image_width, image_height;
167 double width, height;
168 GtkCssRepeatStyle hrepeat, vrepeat;
170 size = _gtk_css_value_get_background_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE));
171 pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION);
172 repeat = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT);
173 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
174 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
175 width = bg->image_rect.width;
176 height = bg->image_rect.height;
178 if (size->contain || size->cover)
179 _gtk_theming_background_get_cover_contain (bg->image,
186 _gtk_css_image_get_concrete_size (bg->image,
187 /* note: 0 does the right thing here for 'auto' */
188 _gtk_css_number_get (&size->width, width),
189 _gtk_css_number_get (&size->height, height),
191 &image_width, &image_height);
194 if (image_width == width)
195 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
196 if (image_height == height)
197 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
199 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
201 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
204 _gtk_css_position_value_get_x (pos, width - image_width),
205 _gtk_css_position_value_get_y (pos, height - image_height));
206 /* shortcut for normal case */
207 _gtk_css_image_draw (bg->image, cr, image_width, image_height);
211 int surface_width, surface_height;
212 cairo_surface_t *surface;
215 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
216 * there is a second step. The UA must scale the image in that
217 * dimension (or both dimensions) so that it fits a whole number of
218 * times in the background positioning area. In the case of the width
219 * (height is analogous):
221 * If X ≠ 0 is the width of the image after step one and W is the width
222 * of the background positioning area, then the rounded width
223 * X' = W / round(W / X) where round() is a function that returns the
224 * nearest natural number (integer greater than zero).
226 * If ‘background-repeat’ is ‘round’ for one dimension only and if
227 * ‘background-size’ is ‘auto’ for the other dimension, then there is
228 * a third step: that other dimension is scaled so that the original
229 * aspect ratio is restored.
231 if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
233 double n = round (width / image_width);
237 if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
238 /* && vsize == auto (it is by default) */)
239 image_height *= width / (image_width * n);
240 image_width = width / n;
242 if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
244 double n = round (height / image_height);
248 if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
249 /* && hsize == auto (it is by default) */)
250 image_width *= height / (image_height * n);
251 image_height = height / n;
254 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
255 * to store the extra space. */
256 if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
258 double n = floor (width / image_width);
259 surface_width = n ? round (width / n) : 0;
262 surface_width = round (image_width);
264 if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
266 double n = floor (height / image_height);
267 surface_height = n ? round (height / n) : 0;
270 surface_height = round (image_height);
272 surface = cairo_surface_create_similar (cairo_get_target (cr),
273 CAIRO_CONTENT_COLOR_ALPHA,
274 surface_width, surface_height);
275 cr2 = cairo_create (surface);
276 cairo_translate (cr2,
277 0.5 * (surface_width - image_width),
278 0.5 * (surface_height - image_height));
279 _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
282 cairo_set_source_surface (cr, surface,
283 _gtk_css_position_value_get_x (pos, width - image_width),
284 _gtk_css_position_value_get_y (pos, height - image_height));
285 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
286 cairo_surface_destroy (surface);
290 hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_width : width,
291 vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_height : height);
300 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
303 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
309 _gtk_theming_background_init_context (GtkThemingBackground *bg)
311 bg->flags = gtk_style_context_get_state (bg->context);
313 gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
314 gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
315 gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
317 /* In the CSS box model, by default the background positioning area is
318 * the padding-box, i.e. all the border-box minus the borders themselves,
319 * which determines also its default size, see
320 * http://dev.w3.org/csswg/css3-background/#background-origin
322 * In the future we might want to support different origins or clips, but
323 * right now we just shrink to the default.
325 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
327 _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->junction);
329 bg->clip_box = bg->padding_box;
330 _gtk_rounded_box_shrink (&bg->padding_box,
331 bg->border.top, bg->border.right,
332 bg->border.bottom, bg->border.left);
334 _gtk_theming_background_apply_clip (bg);
335 _gtk_theming_background_apply_origin (bg);
337 bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
341 _gtk_theming_background_init (GtkThemingBackground *bg,
342 GtkThemingEngine *engine,
347 GtkJunctionSides junction)
349 GtkStyleContext *context;
351 g_assert (bg != NULL);
353 context = _gtk_theming_engine_get_context (engine);
354 _gtk_theming_background_init_from_context (bg, context,
360 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
361 GtkStyleContext *context,
366 GtkJunctionSides junction)
368 g_assert (bg != NULL);
370 bg->context = context;
372 bg->paint_area.x = x;
373 bg->paint_area.y = y;
374 bg->paint_area.width = width;
375 bg->paint_area.height = height;
378 bg->junction = junction;
380 _gtk_theming_background_init_context (bg);
384 _gtk_theming_background_render (GtkThemingBackground *bg,
388 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
390 _gtk_theming_background_apply_window_background (bg, cr);
391 _gtk_theming_background_paint (bg, cr);
392 _gtk_theming_background_apply_shadow (bg, cr);
398 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
400 return (bg->image != NULL) ? TRUE : FALSE;