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_origin (GtkThemingBackground *bg)
46 cairo_rectangle_t image_rect;
48 gtk_theming_engine_get (bg->engine, bg->flags,
49 "background-origin", &origin,
52 /* The default size of the background image depends on the
53 background-origin value as this affects the top left
54 and the bottom right corners. */
56 case GTK_CSS_AREA_BORDER_BOX:
59 image_rect.width = bg->paint_area.width;
60 image_rect.height = bg->paint_area.height;
62 case GTK_CSS_AREA_CONTENT_BOX:
63 image_rect.x = bg->border.left + bg->padding.left;
64 image_rect.y = bg->border.top + bg->padding.top;
65 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
66 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
68 case GTK_CSS_AREA_PADDING_BOX:
70 image_rect.x = bg->border.left;
71 image_rect.y = bg->border.top;
72 image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
73 image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
77 bg->image_rect = image_rect;
81 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
85 gtk_theming_engine_get (bg->engine, bg->flags,
86 "background-clip", &clip,
89 if (clip == GTK_CSS_AREA_PADDING_BOX)
91 _gtk_rounded_box_shrink (&bg->clip_box,
92 bg->border.top, bg->border.right,
93 bg->border.bottom, bg->border.left);
95 else if (clip == GTK_CSS_AREA_CONTENT_BOX)
97 _gtk_rounded_box_shrink (&bg->clip_box,
98 bg->border.top + bg->padding.top,
99 bg->border.right + bg->padding.right,
100 bg->border.bottom + bg->padding.bottom,
101 bg->border.left + bg->padding.left);
106 _gtk_theming_background_paint (GtkThemingBackground *bg,
109 _gtk_rounded_box_path (&bg->clip_box, cr);
110 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
114 GtkCssBackgroundRepeat *repeat;
115 cairo_surface_t *surface;
116 int scale_width, scale_height;
118 gtk_theming_engine_get (bg->engine, bg->flags,
119 "background-repeat", &repeat,
122 if (cairo_pattern_get_surface (bg->pattern, &surface) != CAIRO_STATUS_SUCCESS)
125 if (surface && repeat)
127 scale_width = cairo_image_surface_get_width (surface);
128 scale_height = cairo_image_surface_get_height (surface);
129 if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT)
130 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_REPEAT);
131 else if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT)
132 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_NONE);
136 cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_PAD);
137 scale_width = bg->image_rect.width;
138 scale_height = bg->image_rect.height;
141 if (scale_width && scale_height)
143 /* Fill background color first */
144 cairo_fill_preserve (cr);
146 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
147 cairo_scale (cr, scale_width, scale_height);
148 cairo_set_source (cr, bg->pattern);
149 cairo_scale (cr, 1.0 / scale_width, 1.0 / scale_height);
150 cairo_translate (cr, -bg->image_rect.x, -bg->image_rect.y);
154 cairo_pattern_destroy (bg->pattern);
163 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
166 GtkShadow *box_shadow;
168 gtk_theming_engine_get (bg->engine, bg->flags,
169 "box-shadow", &box_shadow,
172 if (box_shadow != NULL)
174 _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
175 _gtk_shadow_unref (box_shadow);
180 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
182 bg->flags = gtk_theming_engine_get_state (bg->engine);
184 gtk_theming_engine_get_border (bg->engine, bg->flags, &bg->border);
185 gtk_theming_engine_get_padding (bg->engine, bg->flags, &bg->padding);
186 gtk_theming_engine_get_background_color (bg->engine, bg->flags, &bg->bg_color);
188 /* In the CSS box model, by default the background positioning area is
189 * the padding-box, i.e. all the border-box minus the borders themselves,
190 * which determines also its default size, see
191 * http://dev.w3.org/csswg/css3-background/#background-origin
193 * In the future we might want to support different origins or clips, but
194 * right now we just shrink to the default.
196 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
197 _gtk_rounded_box_apply_border_radius (&bg->padding_box, bg->engine, bg->flags, bg->junction);
199 bg->clip_box = bg->padding_box;
200 _gtk_rounded_box_shrink (&bg->padding_box,
201 bg->border.top, bg->border.right,
202 bg->border.bottom, bg->border.left);
204 _gtk_theming_background_apply_clip (bg);
205 _gtk_theming_background_apply_origin (bg);
207 gtk_theming_engine_get (bg->engine, bg->flags,
208 "background-image", &bg->pattern,
213 _gtk_theming_background_init (GtkThemingBackground *bg,
214 GtkThemingEngine *engine,
219 GtkJunctionSides junction)
221 g_assert (bg != NULL);
225 bg->paint_area.x = x;
226 bg->paint_area.y = y;
227 bg->paint_area.width = width;
228 bg->paint_area.height = height;
231 bg->junction = junction;
233 _gtk_theming_background_init_engine (bg);
237 _gtk_theming_background_render (GtkThemingBackground *bg,
241 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
243 _gtk_theming_background_apply_window_background (bg, cr);
244 _gtk_theming_background_paint (bg, cr);
245 _gtk_theming_background_apply_shadow (bg, cr);
251 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
253 return (bg->pattern != NULL) ? TRUE : FALSE;