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 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
49 if (gtk_style_context_has_class (bg->context, "background"))
51 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
52 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
58 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
60 cairo_rectangle_t image_rect;
61 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
62 GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, 0));
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 GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP);
98 GtkCssArea clip = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, 0));
100 if (clip == GTK_CSS_AREA_PADDING_BOX)
102 _gtk_rounded_box_shrink (&bg->clip_box,
103 bg->border.top, bg->border.right,
104 bg->border.bottom, bg->border.left);
106 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
108 _gtk_rounded_box_shrink (&bg->clip_box,
109 bg->border.top + bg->padding.top,
110 bg->border.right + bg->padding.right,
111 bg->border.bottom + bg->padding.bottom,
112 bg->border.left + bg->padding.left);
117 _gtk_theming_background_paint (GtkThemingBackground *bg,
122 _gtk_rounded_box_path (&bg->clip_box, cr);
125 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
129 && bg->image_rect.width > 0
130 && bg->image_rect.height > 0)
132 const GtkCssValue *pos, *repeat;
133 double image_width, image_height;
134 double width, height;
135 GtkCssRepeatStyle hrepeat, vrepeat;
137 pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION);
138 repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), 0);
139 hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
140 vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
141 width = bg->image_rect.width;
142 height = bg->image_rect.height;
144 _gtk_css_bg_size_value_compute_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE),
152 if (image_width == width)
153 hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
154 if (image_height == height)
155 vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
157 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
159 if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
162 _gtk_css_position_value_get_x (pos, width - image_width),
163 _gtk_css_position_value_get_y (pos, height - image_height));
164 /* shortcut for normal case */
165 _gtk_css_image_draw (bg->image, cr, image_width, image_height);
169 int surface_width, surface_height;
170 cairo_surface_t *surface;
173 /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
174 * there is a second step. The UA must scale the image in that
175 * dimension (or both dimensions) so that it fits a whole number of
176 * times in the background positioning area. In the case of the width
177 * (height is analogous):
179 * If X ≠ 0 is the width of the image after step one and W is the width
180 * of the background positioning area, then the rounded width
181 * X' = W / round(W / X) where round() is a function that returns the
182 * nearest natural number (integer greater than zero).
184 * If ‘background-repeat’ is ‘round’ for one dimension only and if
185 * ‘background-size’ is ‘auto’ for the other dimension, then there is
186 * a third step: that other dimension is scaled so that the original
187 * aspect ratio is restored.
189 if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
191 double n = round (width / image_width);
195 if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
196 /* && vsize == auto (it is by default) */)
197 image_height *= width / (image_width * n);
198 image_width = width / n;
200 if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
202 double n = round (height / image_height);
206 if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
207 /* && hsize == auto (it is by default) */)
208 image_width *= height / (image_height * n);
209 image_height = height / n;
212 /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
213 * to store the extra space. */
214 if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
216 double n = floor (width / image_width);
217 surface_width = n ? round (width / n) : 0;
220 surface_width = round (image_width);
222 if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
224 double n = floor (height / image_height);
225 surface_height = n ? round (height / n) : 0;
228 surface_height = round (image_height);
230 surface = cairo_surface_create_similar (cairo_get_target (cr),
231 CAIRO_CONTENT_COLOR_ALPHA,
232 surface_width, surface_height);
233 cr2 = cairo_create (surface);
234 cairo_translate (cr2,
235 0.5 * (surface_width - image_width),
236 0.5 * (surface_height - image_height));
237 _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
240 cairo_set_source_surface (cr, surface,
241 _gtk_css_position_value_get_x (pos, width - image_width),
242 _gtk_css_position_value_get_y (pos, height - image_height));
243 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
244 cairo_surface_destroy (surface);
248 hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_width : width,
249 vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT ? image_height : height);
258 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
261 _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
267 _gtk_theming_background_init_context (GtkThemingBackground *bg)
269 bg->flags = gtk_style_context_get_state (bg->context);
271 gtk_style_context_get_border (bg->context, bg->flags, &bg->border);
272 gtk_style_context_get_padding (bg->context, bg->flags, &bg->padding);
273 gtk_style_context_get_background_color (bg->context, bg->flags, &bg->bg_color);
275 /* In the CSS box model, by default the background positioning area is
276 * the padding-box, i.e. all the border-box minus the borders themselves,
277 * which determines also its default size, see
278 * http://dev.w3.org/csswg/css3-background/#background-origin
280 * In the future we might want to support different origins or clips, but
281 * right now we just shrink to the default.
283 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
285 _gtk_rounded_box_apply_border_radius_for_context (&bg->padding_box, bg->context, bg->junction);
287 bg->clip_box = bg->padding_box;
288 _gtk_rounded_box_shrink (&bg->padding_box,
289 bg->border.top, bg->border.right,
290 bg->border.bottom, bg->border.left);
292 _gtk_theming_background_apply_clip (bg);
293 _gtk_theming_background_apply_origin (bg);
295 bg->image = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE));
299 _gtk_theming_background_init (GtkThemingBackground *bg,
300 GtkThemingEngine *engine,
305 GtkJunctionSides junction)
307 GtkStyleContext *context;
309 g_assert (bg != NULL);
311 context = _gtk_theming_engine_get_context (engine);
312 _gtk_theming_background_init_from_context (bg, context,
318 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
319 GtkStyleContext *context,
324 GtkJunctionSides junction)
326 g_assert (bg != NULL);
328 bg->context = context;
330 bg->paint_area.x = x;
331 bg->paint_area.y = y;
332 bg->paint_area.width = width;
333 bg->paint_area.height = height;
336 bg->junction = junction;
338 _gtk_theming_background_init_context (bg);
342 _gtk_theming_background_render (GtkThemingBackground *bg,
346 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
348 _gtk_theming_background_apply_window_background (bg, cr);
349 _gtk_theming_background_paint (bg, cr);
350 _gtk_theming_background_apply_shadow (bg, cr);
356 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
358 return (bg->image != NULL) ? TRUE : FALSE;