]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
themingbackground: Convert to GtkCssImage
[~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   cairo_save (cr);
110
111   _gtk_rounded_box_path (&bg->clip_box, cr);
112   cairo_clip (cr);
113
114   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
115   cairo_paint (cr);
116
117   if (bg->image)
118     {
119       GtkCssBackgroundRepeat *repeat;
120       double image_width, image_height;
121
122       gtk_theming_engine_get (bg->engine, bg->flags,
123                               "background-repeat", &repeat,
124                               NULL);
125
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);
130
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);
134     }
135
136   cairo_restore (cr);
137 }
138
139 static void
140 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
141                                       cairo_t              *cr)
142 {
143   GtkShadow *box_shadow;
144
145   gtk_theming_engine_get (bg->engine, bg->flags,
146                           "box-shadow", &box_shadow,
147                           NULL);
148
149   if (box_shadow != NULL)
150     {
151       _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
152       _gtk_shadow_unref (box_shadow);
153     }
154 }
155
156 static void
157 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
158 {
159   bg->flags = gtk_theming_engine_get_state (bg->engine);
160
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);
164
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
169    *
170    * In the future we might want to support different origins or clips, but
171    * right now we just shrink to the default.
172    */
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);
175
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);
180
181   _gtk_theming_background_apply_clip (bg);
182   _gtk_theming_background_apply_origin (bg);
183
184   bg->image = g_value_get_object (_gtk_theming_engine_peek_property (bg->engine, "background-image"));
185 }
186
187 void
188 _gtk_theming_background_init (GtkThemingBackground *bg,
189                               GtkThemingEngine     *engine,
190                               gdouble               x,
191                               gdouble               y,
192                               gdouble               width,
193                               gdouble               height,
194                               GtkJunctionSides      junction)
195 {
196   g_assert (bg != NULL);
197
198   bg->engine = engine;
199
200   bg->paint_area.x = x;
201   bg->paint_area.y = y;
202   bg->paint_area.width = width;
203   bg->paint_area.height = height;
204
205   bg->image = NULL;
206   bg->junction = junction;
207
208   _gtk_theming_background_init_engine (bg);
209 }
210
211 void
212 _gtk_theming_background_render (GtkThemingBackground *bg,
213                                 cairo_t              *cr)
214 {
215   cairo_save (cr);
216   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
217
218   _gtk_theming_background_apply_window_background (bg, cr);
219   _gtk_theming_background_paint (bg, cr);
220   _gtk_theming_background_apply_shadow (bg, cr);
221
222   cairo_restore (cr);
223 }
224
225 gboolean
226 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
227 {
228   return (bg->image != NULL) ? TRUE : FALSE;
229 }