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,
111 _gtk_rounded_box_path (&bg->clip_box, cr);
114 gdk_cairo_set_source_rgba (cr, &bg->bg_color);
119 GtkCssBackgroundRepeat *repeat;
120 double image_width, image_height;
122 gtk_theming_engine_get (bg->engine, bg->flags,
123 "background-repeat", &repeat,
126 _gtk_css_image_get_concrete_size (bg->image,
127 0, 0, /* XXX: needs background-size support */
128 bg->image_rect.width, bg->image_rect.height,
129 &image_width, &image_height);
131 cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
132 /* XXX: repeat flags */
133 _gtk_css_image_draw (bg->image, cr, image_width, image_height);
140 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
143 GtkShadow *box_shadow;
145 gtk_theming_engine_get (bg->engine, bg->flags,
146 "box-shadow", &box_shadow,
149 if (box_shadow != NULL)
151 _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
152 _gtk_shadow_unref (box_shadow);
157 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
159 bg->flags = gtk_theming_engine_get_state (bg->engine);
161 gtk_theming_engine_get_border (bg->engine, bg->flags, &bg->border);
162 gtk_theming_engine_get_padding (bg->engine, bg->flags, &bg->padding);
163 gtk_theming_engine_get_background_color (bg->engine, bg->flags, &bg->bg_color);
165 /* In the CSS box model, by default the background positioning area is
166 * the padding-box, i.e. all the border-box minus the borders themselves,
167 * which determines also its default size, see
168 * http://dev.w3.org/csswg/css3-background/#background-origin
170 * In the future we might want to support different origins or clips, but
171 * right now we just shrink to the default.
173 _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
174 _gtk_rounded_box_apply_border_radius (&bg->padding_box, bg->engine, bg->flags, bg->junction);
176 bg->clip_box = bg->padding_box;
177 _gtk_rounded_box_shrink (&bg->padding_box,
178 bg->border.top, bg->border.right,
179 bg->border.bottom, bg->border.left);
181 _gtk_theming_background_apply_clip (bg);
182 _gtk_theming_background_apply_origin (bg);
184 bg->image = g_value_get_object (_gtk_theming_engine_peek_property (bg->engine, "background-image"));
188 _gtk_theming_background_init (GtkThemingBackground *bg,
189 GtkThemingEngine *engine,
194 GtkJunctionSides junction)
196 g_assert (bg != NULL);
200 bg->paint_area.x = x;
201 bg->paint_area.y = y;
202 bg->paint_area.width = width;
203 bg->paint_area.height = height;
206 bg->junction = junction;
208 _gtk_theming_background_init_engine (bg);
212 _gtk_theming_background_render (GtkThemingBackground *bg,
216 cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
218 _gtk_theming_background_apply_window_background (bg, cr);
219 _gtk_theming_background_paint (bg, cr);
220 _gtk_theming_background_apply_shadow (bg, cr);
226 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
228 return (bg->image != NULL) ? TRUE : FALSE;