]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
2a00228a7f96fe3b41f8cde22c93c8a1813f7bc8
[~andy/gtk] / gtk / gtkthemingbackground.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2011 Red Hat, Inc.
4  * 
5  * Authors: Carlos Garnacho <carlosg@gnome.org>
6  *          Cosimo Cecchi <cosimoc@gnome.org>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include "gtkcsstypesprivate.h"
25 #include "gtkthemingbackgroundprivate.h"
26 #include "gtkthemingengineprivate.h"
27
28 #include <gdk/gdk.h>
29
30 static void
31 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
32                                                  cairo_t              *cr)
33 {
34   if (gtk_theming_engine_has_class (bg->engine, "background"))
35     {
36       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
37       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
38       cairo_paint (cr);
39     }
40 }
41
42 static void
43 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
44 {
45   GtkCssArea origin;
46   cairo_rectangle_t image_rect;
47
48   gtk_theming_engine_get (bg->engine, bg->flags,
49                           "background-origin", &origin,
50                           NULL);
51
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. */
55   switch (origin) {
56   case GTK_CSS_AREA_BORDER_BOX:
57     image_rect.x = 0;
58     image_rect.y = 0;
59     image_rect.width = bg->paint_area.width;
60     image_rect.height = bg->paint_area.height;
61     break;
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;
67     break;
68   case GTK_CSS_AREA_PADDING_BOX:
69   default:
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;
74     break;
75   }
76
77   bg->image_rect = image_rect;
78 }
79
80 static void
81 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
82 {
83   GtkCssArea clip;
84
85   gtk_theming_engine_get (bg->engine, bg->flags,
86                           "background-clip", &clip,
87                           NULL);
88
89   if (clip == GTK_CSS_AREA_PADDING_BOX)
90     {
91       _gtk_rounded_box_shrink (&bg->clip_box,
92                                bg->border.top, bg->border.right,
93                                bg->border.bottom, bg->border.left);
94     }
95   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
96     {
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);
102     }
103 }
104
105 static void
106 _gtk_theming_background_paint (GtkThemingBackground *bg,
107                                cairo_t              *cr)
108 {
109   _gtk_rounded_box_path (&bg->clip_box, cr);
110   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
111
112   if (bg->pattern)
113     {
114       GtkCssBackgroundRepeat *repeat;
115       cairo_surface_t *surface;
116       int scale_width, scale_height;
117
118       gtk_theming_engine_get (bg->engine, bg->flags,
119                               "background-repeat", &repeat,
120                               NULL);
121
122       if (cairo_pattern_get_surface (bg->pattern, &surface) != CAIRO_STATUS_SUCCESS)
123         surface = NULL;
124
125       if (surface && repeat)
126         {
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);
133         }
134       else
135         {
136           cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_PAD);
137           scale_width = bg->image_rect.width;
138           scale_height = bg->image_rect.height;
139         }
140
141       if (scale_width && scale_height)
142         {
143           /* Fill background color first */
144           cairo_fill_preserve (cr);
145
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);
151
152           g_free (repeat);
153
154           cairo_pattern_destroy (bg->pattern);
155           bg->pattern = NULL;
156         }
157     }
158
159   cairo_fill (cr);
160 }
161
162 static void
163 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
164                                       cairo_t              *cr)
165 {
166   GtkShadow *box_shadow;
167
168   gtk_theming_engine_get (bg->engine, bg->flags,
169                           "box-shadow", &box_shadow,
170                           NULL);
171
172   if (box_shadow != NULL)
173     {
174       _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
175       _gtk_shadow_unref (box_shadow);
176     }
177 }
178
179 static void
180 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
181 {
182   bg->flags = gtk_theming_engine_get_state (bg->engine);
183
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);
187
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
192    *
193    * In the future we might want to support different origins or clips, but
194    * right now we just shrink to the default.
195    */
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);
198
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);
203
204   _gtk_theming_background_apply_clip (bg);
205   _gtk_theming_background_apply_origin (bg);
206
207   gtk_theming_engine_get (bg->engine, bg->flags,
208                           "background-image", &bg->pattern,
209                           NULL);
210 }
211
212 void
213 _gtk_theming_background_init (GtkThemingBackground *bg,
214                               GtkThemingEngine     *engine,
215                               gdouble               x,
216                               gdouble               y,
217                               gdouble               width,
218                               gdouble               height,
219                               GtkJunctionSides      junction)
220 {
221   g_assert (bg != NULL);
222
223   bg->engine = engine;
224
225   bg->paint_area.x = x;
226   bg->paint_area.y = y;
227   bg->paint_area.width = width;
228   bg->paint_area.height = height;
229
230   bg->pattern = NULL;
231   bg->junction = junction;
232
233   _gtk_theming_background_init_engine (bg);
234 }
235
236 void
237 _gtk_theming_background_render (GtkThemingBackground *bg,
238                                 cairo_t              *cr)
239 {
240   cairo_save (cr);
241   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
242
243   _gtk_theming_background_apply_window_background (bg, cr);
244   _gtk_theming_background_paint (bg, cr);
245   _gtk_theming_background_apply_shadow (bg, cr);
246
247   cairo_restore (cr);
248 }
249
250 gboolean
251 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
252 {
253   return (bg->pattern != NULL) ? TRUE : FALSE;
254 }