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;
47 GtkRoundedBox clip_box;
50 } GtkThemingBackgroundLayer;
53 _gtk_theming_background_layer_apply_origin (GtkThemingBackground *bg,
54 GtkThemingBackgroundLayer *layer)
56 cairo_rectangle_t image_rect;
57 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
58 GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
60 /* The default size of the background image depends on the
61 background-origin value as this affects the top left
62 and the bottom right corners. */
64 case GTK_CSS_AREA_BORDER_BOX:
67 image_rect.width = bg->paint_area.width;
68 image_rect.height = bg->paint_area.height;
70 case GTK_CSS_AREA_CONTENT_BOX:
71 image_rect.x = bg->border.left + bg->padding.left;
72 image_rect.y = bg->border.top + bg->padding.top;
73 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
74 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
76 case GTK_CSS_AREA_PADDING_BOX:
78 image_rect.x = bg->border.left;
79 image_rect.y = bg->border.top;
80 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
81 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
85 /* XXX: image_rect might have negative width/height here.
86 * Do we need to do something about it? */
87 layer->image_rect = image_rect;
91 _gtk_theming_background_apply_clip (GtkThemingBackground *bg,
95 if (clip == GTK_CSS_AREA_PADDING_BOX)
97 _gtk_rounded_box_shrink (box,
98 bg->border.top, bg->border.right,
99 bg->border.bottom, bg->border.left);
101 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
103 _gtk_rounded_box_shrink (box,
104 bg->border.top + bg->padding.top,
105 bg->border.right + bg->padding.right,
106 bg->border.bottom + bg->padding.bottom,
107 bg->border.left + bg->padding.left);
112 _gtk_theming_background_layer_apply_clip (GtkThemingBackground *bg,
113 GtkThemingBackgroundLayer *layer)
115 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP);
116 GtkCssArea clip = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
118 _gtk_theming_background_apply_clip (bg, &layer->clip_box, clip);
122 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
124 GtkCssValue *background_image)
126 GtkRoundedBox clip_box;
127 gint n_values = _gtk_css_array_value_get_n_values (background_image);
128 GtkCssArea clip = _gtk_css_area_value_get
129 (_gtk_css_array_value_get_nth
130 (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
133 clip_box = bg->border_box;
134 _gtk_theming_background_apply_clip (bg, &clip_box, clip);
137 _gtk_rounded_box_path (&clip_box, cr);
140 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
147 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
148 GtkThemingBackgroundLayer *layer,
151 GtkCssRepeatStyle hrepeat, vrepeat;
152 const GtkCssValue *pos, *repeat;
154 double image_width, image_height;
155 double width, height;
157 pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), layer->idx);
158 repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), layer->idx);
159 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
160 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
161 image = _gtk_css_image_value_get_image (
162 _gtk_css_array_value_get_nth (
163 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
165 width = layer->image_rect.width;
166 height = layer->image_rect.height;
168 if (image == NULL || width <= 0 || height <= 0)
171 _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),
178 if (image_width <= 0 || image_height <= 0)
182 if (image_width == width)
183 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
184 if (image_height == height)
185 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
190 _gtk_rounded_box_path (&layer->clip_box, cr);
194 cairo_translate (cr, layer->image_rect.x, layer->image_rect.y);
196 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
199 _gtk_css_position_value_get_x (pos, width - image_width),
200 _gtk_css_position_value_get_y (pos, height - image_height));
201 /* shortcut for normal case */
202 _gtk_css_image_draw (image, cr, image_width, image_height);
206 int surface_width, surface_height;
207 cairo_rectangle_t fill_rect;
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_REPEAT_STYLE_ROUND)
229 double n = round (width / image_width);
233 if (vrepeat != GTK_CSS_REPEAT_STYLE_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_REPEAT_STYLE_ROUND)
240 double n = round (height / image_height);
244 if (hrepeat != GTK_CSS_REPEAT_STYLE_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_REPEAT_STYLE_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_REPEAT_STYLE_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 (image, cr2, image_width, image_height);
278 cairo_set_source_surface (cr, surface,
279 _gtk_css_position_value_get_x (pos, width - image_width),
280 _gtk_css_position_value_get_y (pos, height - image_height));
281 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
282 cairo_surface_destroy (surface);
284 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
286 fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
287 fill_rect.width = image_width;
292 fill_rect.width = width;
295 if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
297 fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
298 fill_rect.height = image_height;
303 fill_rect.height = height;
306 cairo_rectangle (cr, fill_rect.x, fill_rect.y,
307 fill_rect.width, fill_rect.height);
316 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
319 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
325 _gtk_theming_background_init_layer (GtkThemingBackground *bg,
326 GtkThemingBackgroundLayer *layer,
330 layer->clip_box = bg->border_box;
332 _gtk_theming_background_layer_apply_clip (bg, layer);
333 _gtk_theming_background_layer_apply_origin (bg, layer);
337 _gtk_theming_background_init_context (GtkThemingBackground *bg)
339 GtkStateFlags flags = gtk_style_context_get_state (bg->context);
341 gtk_style_context_get_border (bg->context, flags, &bg->border);
342 gtk_style_context_get_padding (bg->context, flags, &bg->padding);
343 gtk_style_context_get_background_color (bg->context, flags, &bg->bg_color);
345 /* In the CSS box model, by default the background positioning area is
346 * the padding-box, i.e. all the border-box minus the borders themselves,
347 * which determines also its default size, see
348 * http://dev.w3.org/csswg/css3-background/#background-origin
350 * In the future we might want to support different origins or clips, but
351 * right now we just shrink to the default.
353 _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
354 _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
356 bg->padding_box = bg->border_box;
357 _gtk_rounded_box_shrink (&bg->padding_box,
358 bg->border.top, bg->border.right,
359 bg->border.bottom, bg->border.left);
363 _gtk_theming_background_init (GtkThemingBackground *bg,
364 GtkThemingEngine *engine,
369 GtkJunctionSides junction)
371 GtkStyleContext *context;
373 g_assert (bg != NULL);
375 context = _gtk_theming_engine_get_context (engine);
376 _gtk_theming_background_init_from_context (bg, context,
382 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
383 GtkStyleContext *context,
388 GtkJunctionSides junction)
390 g_assert (bg != NULL);
392 bg->context = context;
394 bg->paint_area.x = x;
395 bg->paint_area.y = y;
396 bg->paint_area.width = width;
397 bg->paint_area.height = height;
399 bg->junction = junction;
401 _gtk_theming_background_init_context (bg);
405 _gtk_theming_background_render (GtkThemingBackground *bg,
409 GtkThemingBackgroundLayer layer;
410 GtkCssValue *background_image;
412 background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
415 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
417 _gtk_theming_background_paint_color (bg, cr, background_image);
419 for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
421 _gtk_theming_background_init_layer (bg, &layer, idx);
422 _gtk_theming_background_paint_layer (bg, &layer, cr);
425 _gtk_theming_background_apply_shadow (bg, cr);
431 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
434 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
436 if (_gtk_css_array_value_get_n_values (value) == 0)
439 image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
440 return (image != NULL);