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"
46 cairo_rectangle_t image_rect;
48 GtkRoundedBox clip_box;
51 } GtkThemingBackgroundLayer;
54 _gtk_theming_background_layer_apply_origin (GtkThemingBackground *bg,
55 GtkThemingBackgroundLayer *layer)
57 cairo_rectangle_t image_rect;
58 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
59 GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
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 layer->image_rect = image_rect;
92 _gtk_theming_background_apply_clip (GtkThemingBackground *bg,
96 if (clip == GTK_CSS_AREA_PADDING_BOX)
98 _gtk_rounded_box_shrink (box,
99 bg->border.top, bg->border.right,
100 bg->border.bottom, bg->border.left);
102 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
104 _gtk_rounded_box_shrink (box,
105 bg->border.top + bg->padding.top,
106 bg->border.right + bg->padding.right,
107 bg->border.bottom + bg->padding.bottom,
108 bg->border.left + bg->padding.left);
113 _gtk_theming_background_layer_apply_clip (GtkThemingBackground *bg,
114 GtkThemingBackgroundLayer *layer)
116 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP);
117 GtkCssArea clip = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
119 _gtk_theming_background_apply_clip (bg, &layer->clip_box, clip);
123 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
125 GtkCssValue *background_image)
127 GtkRoundedBox clip_box;
128 gint n_values = _gtk_css_array_value_get_n_values (background_image);
129 GtkCssArea clip = _gtk_css_area_value_get
130 (_gtk_css_array_value_get_nth
131 (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
134 clip_box = bg->border_box;
135 _gtk_theming_background_apply_clip (bg, &clip_box, clip);
138 _gtk_rounded_box_path (&clip_box, cr);
141 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
148 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
149 GtkThemingBackgroundLayer *layer,
154 _gtk_rounded_box_path (&layer->clip_box, cr);
158 && layer->image_rect.width > 0
159 && layer->image_rect.height > 0)
161 const GtkCssValue *pos, *repeat;
162 double image_width, image_height;
163 double width, height;
164 GtkCssRepeatStyle hrepeat, vrepeat;
166 pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), layer->idx);
167 repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), layer->idx);
168 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
169 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
170 width = layer->image_rect.width;
171 height = layer->image_rect.height;
173 _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), layer->idx),
181 if (image_width == width)
182 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
183 if (image_height == height)
184 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
186 cairo_translate (cr, layer->image_rect.x, layer->image_rect.y);
188 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
191 _gtk_css_position_value_get_x (pos, width - image_width),
192 _gtk_css_position_value_get_y (pos, height - image_height));
193 /* shortcut for normal case */
194 _gtk_css_image_draw (layer->image, cr, image_width, image_height);
198 int surface_width, surface_height;
199 cairo_surface_t *surface;
202 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
203 * there is a second step. The UA must scale the image in that
204 * dimension (or both dimensions) so that it fits a whole number of
205 * times in the background positioning area. In the case of the width
206 * (height is analogous):
208 * If X ≠ 0 is the width of the image after step one and W is the width
209 * of the background positioning area, then the rounded width
210 * X' = W / round(W / X) where round() is a function that returns the
211 * nearest natural number (integer greater than zero).
213 * If ‘background-repeat’ is ‘round’ for one dimension only and if
214 * ‘background-size’ is ‘auto’ for the other dimension, then there is
215 * a third step: that other dimension is scaled so that the original
216 * aspect ratio is restored.
218 if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
220 double n = round (width / image_width);
224 if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
225 /* && vsize == auto (it is by default) */)
226 image_height *= width / (image_width * n);
227 image_width = width / n;
229 if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
231 double n = round (height / image_height);
235 if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
236 /* && hsize == auto (it is by default) */)
237 image_width *= height / (image_height * n);
238 image_height = height / n;
241 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
242 * to store the extra space. */
243 if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
245 double n = floor (width / image_width);
246 surface_width = n ? round (width / n) : 0;
249 surface_width = round (image_width);
251 if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
253 double n = floor (height / image_height);
254 surface_height = n ? round (height / n) : 0;
257 surface_height = round (image_height);
259 surface = cairo_surface_create_similar (cairo_get_target (cr),
260 CAIRO_CONTENT_COLOR_ALPHA,
261 surface_width, surface_height);
262 cr2 = cairo_create (surface);
263 cairo_translate (cr2,
264 0.5 * (surface_width - image_width),
265 0.5 * (surface_height - image_height));
266 _gtk_css_image_draw (layer->image, cr2, image_width, image_height);
269 cairo_set_source_surface (cr, surface,
270 _gtk_css_position_value_get_x (pos, width - image_width),
271 _gtk_css_position_value_get_y (pos, height - image_height));
272 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
273 cairo_surface_destroy (surface);
277 hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_width : width,
278 vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_height : height);
287 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
290 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
296 _gtk_theming_background_init_layer (GtkThemingBackground *bg,
297 GtkThemingBackgroundLayer *layer,
298 GtkCssValue *background_image,
302 layer->clip_box = bg->border_box;
304 _gtk_theming_background_layer_apply_clip (bg, layer);
305 _gtk_theming_background_layer_apply_origin (bg, layer);
307 layer->image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (background_image, layer->idx));
311 _gtk_theming_background_init_context (GtkThemingBackground *bg)
313 bg->flags = gtk_style_context_get_state (bg->context);
315 gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
316 gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
317 gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
319 /* In the CSS box model, by default the background positioning area is
320 * the padding-box, i.e. all the border-box minus the borders themselves,
321 * which determines also its default size, see
322 * http://dev.w3.org/csswg/css3-background/#background-origin
324 * In the future we might want to support different origins or clips, but
325 * right now we just shrink to the default.
327 _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
328 _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
330 bg->padding_box = bg->border_box;
331 _gtk_rounded_box_shrink (&bg->padding_box,
332 bg->border.top, bg->border.right,
333 bg->border.bottom, bg->border.left);
337 _gtk_theming_background_init (GtkThemingBackground *bg,
338 GtkThemingEngine *engine,
343 GtkJunctionSides junction)
345 GtkStyleContext *context;
347 g_assert (bg != NULL);
349 context = _gtk_theming_engine_get_context (engine);
350 _gtk_theming_background_init_from_context (bg, context,
356 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
357 GtkStyleContext *context,
362 GtkJunctionSides junction)
364 g_assert (bg != NULL);
366 bg->context = context;
368 bg->paint_area.x = x;
369 bg->paint_area.y = y;
370 bg->paint_area.width = width;
371 bg->paint_area.height = height;
373 bg->junction = junction;
375 _gtk_theming_background_init_context (bg);
379 _gtk_theming_background_render (GtkThemingBackground *bg,
383 GtkThemingBackgroundLayer layer;
384 GtkCssValue *background_image;
386 background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
389 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
391 _gtk_theming_background_paint_color (bg, cr, background_image);
393 for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
395 _gtk_theming_background_init_layer (bg, &layer, background_image, idx);
396 _gtk_theming_background_paint_layer (bg, &layer, cr);
399 _gtk_theming_background_apply_shadow (bg, cr);
405 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
408 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
410 if (_gtk_css_array_value_get_n_values (value) == 0)
413 image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
414 return (image != NULL);