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;
49 } GtkThemingBackgroundLayer;
52 _gtk_theming_background_layer_apply_origin (GtkThemingBackground *bg,
53 GtkThemingBackgroundLayer *layer)
55 cairo_rectangle_t image_rect;
56 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
57 GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
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 layer->image_rect = image_rect;
89 static const GtkRoundedBox *
90 gtk_theming_background_get_box (GtkThemingBackground *bg,
95 case GTK_CSS_AREA_BORDER_BOX:
96 return &bg->border_box;
97 case GTK_CSS_AREA_PADDING_BOX:
98 return &bg->padding_box;
99 case GTK_CSS_AREA_CONTENT_BOX:
100 return &bg->content_box;
102 g_return_val_if_reached (&bg->border_box);
107 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
109 GtkCssValue *background_image)
111 gint n_values = _gtk_css_array_value_get_n_values (background_image);
112 GtkCssArea clip = _gtk_css_area_value_get
113 (_gtk_css_array_value_get_nth
114 (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
118 _gtk_rounded_box_path (gtk_theming_background_get_box (bg, clip), cr);
121 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
128 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
129 GtkThemingBackgroundLayer *layer,
132 GtkCssRepeatStyle hrepeat, vrepeat;
133 const GtkCssValue *pos, *repeat;
135 double image_width, image_height;
136 double width, height;
138 pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), layer->idx);
139 repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), layer->idx);
140 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
141 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
142 image = _gtk_css_image_value_get_image (
143 _gtk_css_array_value_get_nth (
144 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
146 width = layer->image_rect.width;
147 height = layer->image_rect.height;
149 if (image == NULL || width <= 0 || height <= 0)
152 _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),
159 if (image_width <= 0 || image_height <= 0)
163 if (image_width == width)
164 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
165 if (image_height == height)
166 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
171 _gtk_rounded_box_path (
172 gtk_theming_background_get_box (
174 _gtk_css_area_value_get (
175 _gtk_css_array_value_get_nth (
176 _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
182 cairo_translate (cr, layer->image_rect.x, layer->image_rect.y);
184 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
187 _gtk_css_position_value_get_x (pos, width - image_width),
188 _gtk_css_position_value_get_y (pos, height - image_height));
189 /* shortcut for normal case */
190 _gtk_css_image_draw (image, cr, image_width, image_height);
194 int surface_width, surface_height;
195 cairo_rectangle_t fill_rect;
196 cairo_surface_t *surface;
199 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
200 * there is a second step. The UA must scale the image in that
201 * dimension (or both dimensions) so that it fits a whole number of
202 * times in the background positioning area. In the case of the width
203 * (height is analogous):
205 * If X ≠ 0 is the width of the image after step one and W is the width
206 * of the background positioning area, then the rounded width
207 * X' = W / round(W / X) where round() is a function that returns the
208 * nearest natural number (integer greater than zero).
210 * If ‘background-repeat’ is ‘round’ for one dimension only and if
211 * ‘background-size’ is ‘auto’ for the other dimension, then there is
212 * a third step: that other dimension is scaled so that the original
213 * aspect ratio is restored.
215 if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
217 double n = round (width / image_width);
221 if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
222 /* && vsize == auto (it is by default) */)
223 image_height *= width / (image_width * n);
224 image_width = width / n;
226 if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
228 double n = round (height / image_height);
232 if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
233 /* && hsize == auto (it is by default) */)
234 image_width *= height / (image_height * n);
235 image_height = height / n;
238 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
239 * to store the extra space. */
240 if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
242 double n = floor (width / image_width);
243 surface_width = n ? round (width / n) : 0;
246 surface_width = round (image_width);
248 if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
250 double n = floor (height / image_height);
251 surface_height = n ? round (height / n) : 0;
254 surface_height = round (image_height);
256 surface = cairo_surface_create_similar (cairo_get_target (cr),
257 CAIRO_CONTENT_COLOR_ALPHA,
258 surface_width, surface_height);
259 cr2 = cairo_create (surface);
260 cairo_translate (cr2,
261 0.5 * (surface_width - image_width),
262 0.5 * (surface_height - image_height));
263 _gtk_css_image_draw (image, cr2, image_width, image_height);
266 cairo_set_source_surface (cr, surface,
267 _gtk_css_position_value_get_x (pos, width - image_width),
268 _gtk_css_position_value_get_y (pos, height - image_height));
269 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
270 cairo_surface_destroy (surface);
272 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
274 fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
275 fill_rect.width = image_width;
280 fill_rect.width = width;
283 if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
285 fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
286 fill_rect.height = image_height;
291 fill_rect.height = height;
294 cairo_rectangle (cr, fill_rect.x, fill_rect.y,
295 fill_rect.width, fill_rect.height);
304 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
307 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
313 _gtk_theming_background_init_layer (GtkThemingBackground *bg,
314 GtkThemingBackgroundLayer *layer,
319 _gtk_theming_background_layer_apply_origin (bg, layer);
323 _gtk_theming_background_init_context (GtkThemingBackground *bg)
325 GtkStateFlags flags = gtk_style_context_get_state (bg->context);
327 gtk_style_context_get_border (bg->context, flags, &bg->border);
328 gtk_style_context_get_padding (bg->context, flags, &bg->padding);
329 gtk_style_context_get_background_color (bg->context, flags, &bg->bg_color);
331 /* In the CSS box model, by default the background positioning area is
332 * the padding-box, i.e. all the border-box minus the borders themselves,
333 * which determines also its default size, see
334 * http://dev.w3.org/csswg/css3-background/#background-origin
336 * In the future we might want to support different origins or clips, but
337 * right now we just shrink to the default.
339 _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
340 _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
342 bg->padding_box = bg->border_box;
343 _gtk_rounded_box_shrink (&bg->padding_box,
344 bg->border.top, bg->border.right,
345 bg->border.bottom, bg->border.left);
347 bg->content_box = bg->padding_box;
348 _gtk_rounded_box_shrink (&bg->content_box,
349 bg->padding.top, bg->padding.right,
350 bg->padding.bottom, bg->padding.left);
354 _gtk_theming_background_init (GtkThemingBackground *bg,
355 GtkThemingEngine *engine,
360 GtkJunctionSides junction)
362 GtkStyleContext *context;
364 g_assert (bg != NULL);
366 context = _gtk_theming_engine_get_context (engine);
367 _gtk_theming_background_init_from_context (bg, context,
373 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
374 GtkStyleContext *context,
379 GtkJunctionSides junction)
381 g_assert (bg != NULL);
383 bg->context = context;
385 bg->paint_area.x = x;
386 bg->paint_area.y = y;
387 bg->paint_area.width = width;
388 bg->paint_area.height = height;
390 bg->junction = junction;
392 _gtk_theming_background_init_context (bg);
396 _gtk_theming_background_render (GtkThemingBackground *bg,
400 GtkThemingBackgroundLayer layer;
401 GtkCssValue *background_image;
403 background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
406 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
408 _gtk_theming_background_paint_color (bg, cr, background_image);
410 for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
412 _gtk_theming_background_init_layer (bg, &layer, idx);
413 _gtk_theming_background_paint_layer (bg, &layer, cr);
416 _gtk_theming_background_apply_shadow (bg, cr);
422 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
425 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
427 if (_gtk_css_array_value_get_n_values (value) == 0)
430 image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
431 return (image != NULL);