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, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "gtkcsstypesprivate.h"
25 #include "gtkthemingbackgroundprivate.h"
26 #include "gtkthemingengineprivate.h"
31 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
34 if (gtk_theming_engine_has_class (bg->engine, "background"))
36 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
37 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
43 _gtk_theming_background_apply_running_transformation (GtkThemingBackground *bg,
48 cairo_pattern_t *other_pattern;
49 GtkStateFlags other_flags;
51 cairo_pattern_t *new_pattern = NULL;
53 running = gtk_theming_engine_state_is_running (bg->engine, GTK_STATE_PRELIGHT, &progress);
58 if (bg->flags & GTK_STATE_FLAG_PRELIGHT)
60 other_flags = bg->flags & ~(GTK_STATE_FLAG_PRELIGHT);
61 progress = 1 - progress;
64 other_flags = bg->flags | GTK_STATE_FLAG_PRELIGHT;
66 gtk_theming_engine_get_background_color (bg->engine, other_flags, &other_bg);
67 gtk_theming_engine_get (bg->engine, other_flags,
68 "background-image", &other_pattern,
71 if (bg->pattern && other_pattern)
73 cairo_pattern_type_t type, other_type;
76 cairo_pattern_get_color_stop_count (bg->pattern, &n0);
77 cairo_pattern_get_color_stop_count (other_pattern, &n1);
78 type = cairo_pattern_get_type (bg->pattern);
79 other_type = cairo_pattern_get_type (other_pattern);
81 if (type == other_type && n0 == n1)
83 gdouble offset0, red0, green0, blue0, alpha0;
84 gdouble offset1, red1, green1, blue1, alpha1;
85 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
86 gdouble r00, r01, r10, r11;
89 if (type == CAIRO_PATTERN_TYPE_LINEAR)
91 cairo_pattern_get_linear_points (bg->pattern, &x00, &y00, &x01, &y01);
92 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
94 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
95 y00 + (y10 - y00) * progress,
96 x01 + (x11 - x01) * progress,
97 y01 + (y11 - y01) * progress);
101 cairo_pattern_get_radial_circles (bg->pattern, &x00, &y00, &r00, &x01, &y01, &r01);
102 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
104 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
105 y00 + (y10 - y00) * progress,
106 r00 + (r10 - r00) * progress,
107 x01 + (x11 - x01) * progress,
108 y01 + (y11 - y01) * progress,
109 r01 + (r11 - r01) * progress);
112 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
115 /* Blend both gradients into one */
116 while (i < n0 && i < n1)
118 cairo_pattern_get_color_stop_rgba (bg->pattern, i,
120 &red0, &green0, &blue0,
122 cairo_pattern_get_color_stop_rgba (other_pattern, i,
124 &red1, &green1, &blue1,
127 cairo_pattern_add_color_stop_rgba (new_pattern,
128 offset0 + ((offset1 - offset0) * progress),
129 red0 + ((red1 - red0) * progress),
130 green0 + ((green1 - green0) * progress),
131 blue0 + ((blue1 - blue0) * progress),
132 alpha0 + ((alpha1 - alpha0) * progress));
140 cairo_rectangle (cr, 0, 0, bg->paint_area.width, bg->paint_area.height);
143 cairo_push_group (cr);
145 cairo_scale (cr, bg->paint_area.width, bg->paint_area.height);
146 cairo_set_source (cr, other_pattern);
147 cairo_paint_with_alpha (cr, progress);
148 cairo_set_source (cr, bg->pattern);
149 cairo_paint_with_alpha (cr, 1.0 - progress);
151 new_pattern = cairo_pop_group (cr);
156 else if (bg->pattern || other_pattern)
160 gdouble x0, y0, x1, y1, r0, r1;
163 /* Blend a pattern with a color */
168 progress = 1 - progress;
176 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
178 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
179 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
183 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
184 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
187 cairo_pattern_get_color_stop_count (p, &n);
189 for (i = 0; i < n; i++)
191 gdouble red1, green1, blue1, alpha1;
194 cairo_pattern_get_color_stop_rgba (p, i,
196 &red1, &green1, &blue1,
198 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
199 c->red + ((red1 - c->red) * progress),
200 c->green + ((green1 - c->green) * progress),
201 c->blue + ((blue1 - c->blue) * progress),
202 c->alpha + ((alpha1 - c->alpha) * progress));
207 /* Merge just colors */
208 new_pattern = cairo_pattern_create_rgba (CLAMP (bg->bg_color.red + ((other_bg.red - bg->bg_color.red) * progress), 0, 1),
209 CLAMP (bg->bg_color.green + ((other_bg.green - bg->bg_color.green) * progress), 0, 1),
210 CLAMP (bg->bg_color.blue + ((other_bg.blue - bg->bg_color.blue) * progress), 0, 1),
211 CLAMP (bg->bg_color.alpha + ((other_bg.alpha - bg->bg_color.alpha) * progress), 0, 1));
216 /* Replace pattern to use */
217 cairo_pattern_destroy (bg->pattern);
218 bg->pattern = new_pattern;
222 cairo_pattern_destroy (other_pattern);
226 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
229 cairo_rectangle_t image_rect;
231 gtk_theming_engine_get (bg->engine, bg->flags,
232 "background-origin", &origin,
235 /* The default size of the background image depends on the
236 background-origin value as this affects the top left
237 and the bottom right corners. */
239 case GTK_CSS_AREA_BORDER_BOX:
242 image_rect.width = bg->paint_area.width;
243 image_rect.height = bg->paint_area.height;
245 case GTK_CSS_AREA_CONTENT_BOX:
246 image_rect.x = bg->border.left + bg->padding.left;
247 image_rect.y = bg->border.top + bg->padding.top;
248 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
249 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
251 case GTK_CSS_AREA_PADDING_BOX:
253 image_rect.x = bg->border.left;
254 image_rect.y = bg->border.top;
255 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
256 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
260 bg->image_rect = image_rect;
264 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
268 gtk_theming_engine_get (bg->engine, bg->flags,
269 "background-clip", &clip,
272 if (clip == GTK_CSS_AREA_PADDING_BOX)
274 _gtk_rounded_box_shrink (&bg->clip_box,
275 bg->border.top, bg->border.right,
276 bg->border.bottom, bg->border.left);
278 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
280 _gtk_rounded_box_shrink (&bg->clip_box,
281 bg->border.top + bg->padding.top,
282 bg->border.right + bg->padding.right,
283 bg->border.bottom + bg->padding.bottom,
284 bg->border.left + bg->padding.left);
289 _gtk_theming_background_paint (GtkThemingBackground *bg,
292 _gtk_rounded_box_path (&bg->clip_box, cr);
293 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
297 GtkCssBackgroundRepeat *repeat;
298 cairo_surface_t *surface;
299 int scale_width, scale_height;
301 gtk_theming_engine_get (bg->engine, bg->flags,
302 "background-repeat", &repeat,
305 if (cairo_pattern_get_surface (bg->pattern, &surface) != CAIRO_STATUS_SUCCESS)
308 if (surface && repeat)
310 scale_width = cairo_image_surface_get_width (surface);
311 scale_height = cairo_image_surface_get_height (surface);
312 if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT)
313 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_REPEAT);
314 else if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT)
315 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_NONE);
319 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_PAD);
320 scale_width = bg->image_rect.width;
321 scale_height = bg->image_rect.height;
324 if (scale_width && scale_height)
326 /* Fill background color first */
327 cairo_fill_preserve (cr);
329 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
330 cairo_scale (cr, scale_width, scale_height);
331 cairo_set_source (cr, bg->pattern);
332 cairo_scale (cr, 1.0 / scale_width, 1.0 / scale_height);
333 cairo_translate (cr, -bg->image_rect.x, -bg->image_rect.y);
337 cairo_pattern_destroy (bg->pattern);
346 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
349 GtkShadow *box_shadow;
351 gtk_theming_engine_get (bg->engine, bg->flags,
352 "box-shadow", &box_shadow,
355 if (box_shadow != NULL)
357 _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
358 _gtk_shadow_unref (box_shadow);
363 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
365 bg->flags = gtk_theming_engine_get_state (bg->engine);
367 gtk_theming_engine_get_border (bg->engine, bg->flags, &bg->border);
368 gtk_theming_engine_get_padding (bg->engine, bg->flags, &bg->padding);
369 gtk_theming_engine_get_background_color (bg->engine, bg->flags, &bg->bg_color);
371 /* In the CSS box model, by default the background positioning area is
372 * the padding-box, i.e. all the border-box minus the borders themselves,
373 * which determines also its default size, see
374 * http://dev.w3.org/csswg/css3-background/#background-origin
376 * In the future we might want to support different origins or clips, but
377 * right now we just shrink to the default.
379 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
380 _gtk_rounded_box_apply_border_radius (&bg->padding_box, bg->engine, bg->flags, bg->junction);
382 bg->clip_box = bg->padding_box;
383 _gtk_rounded_box_shrink (&bg->padding_box,
384 bg->border.top, bg->border.right,
385 bg->border.bottom, bg->border.left);
387 _gtk_theming_background_apply_clip (bg);
388 _gtk_theming_background_apply_origin (bg);
390 gtk_theming_engine_get (bg->engine, bg->flags,
391 "background-image", &bg->pattern,
396 _gtk_theming_background_init (GtkThemingBackground *bg,
397 GtkThemingEngine *engine,
402 GtkJunctionSides junction)
404 g_assert (bg != NULL);
408 bg->paint_area.x = x;
409 bg->paint_area.y = y;
410 bg->paint_area.width = width;
411 bg->paint_area.height = height;
414 bg->junction = junction;
416 _gtk_theming_background_init_engine (bg);
420 _gtk_theming_background_render (GtkThemingBackground *bg,
424 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
426 _gtk_theming_background_apply_window_background (bg, cr);
427 _gtk_theming_background_apply_running_transformation (bg, cr);
428 _gtk_theming_background_paint (bg, cr);
429 _gtk_theming_background_apply_shadow (bg, cr);
435 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
437 return (bg->pattern != NULL) ? TRUE : FALSE;