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 "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"
40 /* this is in case round() is not provided by the compiler,
41 * such as in the case of C89 compilers, like MSVC
43 #include "fallback-c89.c"
45 static const GtkRoundedBox *
46 gtk_theming_background_get_box (GtkThemingBackground *bg,
51 case GTK_CSS_AREA_BORDER_BOX:
52 return &bg->border_box;
53 case GTK_CSS_AREA_PADDING_BOX:
54 return &bg->padding_box;
55 case GTK_CSS_AREA_CONTENT_BOX:
56 return &bg->content_box;
58 g_return_val_if_reached (&bg->border_box);
63 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
65 GtkCssValue *background_image)
67 gint n_values = _gtk_css_array_value_get_n_values (background_image);
68 GtkCssArea clip = _gtk_css_area_value_get
69 (_gtk_css_array_value_get_nth
70 (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
74 _gtk_rounded_box_path (gtk_theming_background_get_box (bg, clip), cr);
77 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
84 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
88 GtkCssRepeatStyle hrepeat, vrepeat;
89 const GtkCssValue *pos, *repeat;
91 const GtkRoundedBox *origin;
92 double image_width, image_height;
95 pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), idx);
96 repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), idx);
97 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
98 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
99 image = _gtk_css_image_value_get_image (
100 _gtk_css_array_value_get_nth (
101 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
103 origin = gtk_theming_background_get_box (
105 _gtk_css_area_value_get (
106 _gtk_css_array_value_get_nth (
107 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN),
109 width = origin->box.width;
110 height = origin->box.height;
112 if (image == NULL || width <= 0 || height <= 0)
115 _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), idx),
122 if (image_width <= 0 || image_height <= 0)
126 if (image_width == width)
127 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
128 if (image_height == height)
129 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
134 _gtk_rounded_box_path (
135 gtk_theming_background_get_box (
137 _gtk_css_area_value_get (
138 _gtk_css_array_value_get_nth (
139 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
145 cairo_translate (cr, origin->box.x, origin->box.y);
147 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
150 _gtk_css_position_value_get_x (pos, width - image_width),
151 _gtk_css_position_value_get_y (pos, height - image_height));
152 /* shortcut for normal case */
153 _gtk_css_image_draw (image, cr, image_width, image_height);
157 int surface_width, surface_height;
158 cairo_rectangle_t fill_rect;
159 cairo_surface_t *surface;
162 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
163 * there is a second step. The UA must scale the image in that
164 * dimension (or both dimensions) so that it fits a whole number of
165 * times in the background positioning area. In the case of the width
166 * (height is analogous):
168 * If X ≠ 0 is the width of the image after step one and W is the width
169 * of the background positioning area, then the rounded width
170 * X' = W / round(W / X) where round() is a function that returns the
171 * nearest natural number (integer greater than zero).
173 * If ‘background-repeat’ is ‘round’ for one dimension only and if
174 * ‘background-size’ is ‘auto’ for the other dimension, then there is
175 * a third step: that other dimension is scaled so that the original
176 * aspect ratio is restored.
178 if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
180 double n = round (width / image_width);
184 if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
185 /* && vsize == auto (it is by default) */)
186 image_height *= width / (image_width * n);
187 image_width = width / n;
189 if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
191 double n = round (height / image_height);
195 if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
196 /* && hsize == auto (it is by default) */)
197 image_width *= height / (image_height * n);
198 image_height = height / n;
201 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
202 * to store the extra space. */
203 if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
205 double n = floor (width / image_width);
206 surface_width = n ? round (width / n) : 0;
209 surface_width = round (image_width);
211 if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
213 double n = floor (height / image_height);
214 surface_height = n ? round (height / n) : 0;
217 surface_height = round (image_height);
219 surface = cairo_surface_create_similar (cairo_get_target (cr),
220 CAIRO_CONTENT_COLOR_ALPHA,
221 surface_width, surface_height);
222 cr2 = cairo_create (surface);
223 cairo_translate (cr2,
224 0.5 * (surface_width - image_width),
225 0.5 * (surface_height - image_height));
226 _gtk_css_image_draw (image, cr2, image_width, image_height);
229 cairo_set_source_surface (cr, surface,
230 _gtk_css_position_value_get_x (pos, width - image_width),
231 _gtk_css_position_value_get_y (pos, height - image_height));
232 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
233 cairo_surface_destroy (surface);
235 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
237 fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
238 fill_rect.width = image_width;
243 fill_rect.width = width;
246 if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
248 fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
249 fill_rect.height = image_height;
254 fill_rect.height = height;
257 cairo_rectangle (cr, fill_rect.x, fill_rect.y,
258 fill_rect.width, fill_rect.height);
267 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
270 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
276 _gtk_theming_background_init_context (GtkThemingBackground *bg)
278 GtkStateFlags flags = gtk_style_context_get_state (bg->context);
279 GtkBorder border, padding;
281 gtk_style_context_get_border (bg->context, flags, &border);
282 gtk_style_context_get_padding (bg->context, flags, &padding);
283 gtk_style_context_get_background_color (bg->context, flags, &bg->bg_color);
285 /* In the CSS box model, by default the background positioning area is
286 * the padding-box, i.e. all the border-box minus the borders themselves,
287 * which determines also its default size, see
288 * http://dev.w3.org/csswg/css3-background/#background-origin
290 * In the future we might want to support different origins or clips, but
291 * right now we just shrink to the default.
293 _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
294 _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
296 bg->padding_box = bg->border_box;
297 _gtk_rounded_box_shrink (&bg->padding_box,
298 border.top, border.right,
299 border.bottom, border.left);
301 bg->content_box = bg->padding_box;
302 _gtk_rounded_box_shrink (&bg->content_box,
303 padding.top, padding.right,
304 padding.bottom, padding.left);
308 _gtk_theming_background_init (GtkThemingBackground *bg,
309 GtkThemingEngine *engine,
314 GtkJunctionSides junction)
316 GtkStyleContext *context;
318 g_assert (bg != NULL);
320 context = _gtk_theming_engine_get_context (engine);
321 _gtk_theming_background_init_from_context (bg, context,
327 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
328 GtkStyleContext *context,
333 GtkJunctionSides junction)
335 g_assert (bg != NULL);
337 bg->context = context;
339 bg->paint_area.x = x;
340 bg->paint_area.y = y;
341 bg->paint_area.width = width;
342 bg->paint_area.height = height;
344 bg->junction = junction;
346 _gtk_theming_background_init_context (bg);
350 _gtk_theming_background_render (GtkThemingBackground *bg,
354 GtkCssValue *background_image;
356 background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
359 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
361 _gtk_theming_background_paint_color (bg, cr, background_image);
363 for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
365 _gtk_theming_background_paint_layer (bg, idx, cr);
368 _gtk_theming_background_apply_shadow (bg, cr);
374 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
377 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
379 if (_gtk_css_array_value_get_n_values (value) == 0)
382 image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
383 return (image != NULL);