]> Pileus Git - ~andy/gtk/blob - gtk/gtkborderimage.c
css: Use GtkCssValues instead of GValue in the css machinery
[~andy/gtk] / gtk / gtkborderimage.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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <cairo-gobject.h>
24
25 #include <math.h>
26
27 #include "gtkborderimageprivate.h"
28 #include "gtkstylepropertiesprivate.h"
29 #include "gtkthemingengineprivate.h"
30
31 /* this is in case round() is not provided by the compiler, 
32  * such as in the case of C89 compilers, like MSVC
33  */
34 #include "fallback-c89.c"
35
36 gboolean
37 _gtk_border_image_init (GtkBorderImage   *image,
38                         GtkThemingEngine *engine)
39 {
40   GtkBorder *width;
41
42   image->source = _gtk_css_value_get_object (_gtk_theming_engine_peek_property (engine, "border-image-source"));
43   if (image->source == NULL)
44     return FALSE;
45
46   image->slice = *(GtkBorder *) _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-slice"));
47   width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-width"));
48   if (width)
49     {
50       image->width = *width;
51       image->has_width = TRUE;
52     }
53   else
54     image->has_width = FALSE;
55
56   image->repeat = *_gtk_css_value_get_border_image_repeat (_gtk_theming_engine_peek_property (engine, "border-image-repeat"));
57
58   return TRUE;
59 }
60
61 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
62 struct _GtkBorderImageSliceSize {
63   double offset;
64   double size;
65 };
66
67 static void
68 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
69                                       double                  offset,
70                                       double                  area_size,
71                                       int                     start_border,
72                                       int                     end_border)
73 {
74   /* This code assumes area_size >= start_border + end_border */
75
76   sizes[0].offset = offset;
77   sizes[0].size = start_border;
78   sizes[1].offset = offset + start_border;
79   sizes[1].size = area_size - start_border - end_border;
80   sizes[2].offset = offset + area_size - end_border;
81   sizes[2].size = end_border;
82 }
83
84 static void
85 gtk_border_image_render_slice (cairo_t           *cr,
86                                cairo_surface_t   *slice,
87                                double             slice_width,
88                                double             slice_height,
89                                double             x,
90                                double             y,
91                                double             width,
92                                double             height,
93                                GtkCssBorderRepeatStyle  hrepeat,
94                                GtkCssBorderRepeatStyle  vrepeat)
95 {
96   double hscale, vscale;
97   double xstep, ystep;
98   cairo_extend_t extend = CAIRO_EXTEND_PAD;
99   cairo_matrix_t matrix;
100   cairo_pattern_t *pattern;
101
102   /* We can't draw center tiles yet */
103   g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
104
105   hscale = width / slice_width;
106   vscale = height / slice_height;
107   xstep = width;
108   ystep = height;
109
110   switch (hrepeat)
111     {
112     case GTK_CSS_REPEAT_STYLE_REPEAT:
113       extend = CAIRO_EXTEND_REPEAT;
114       hscale = vscale;
115       break;
116     case GTK_CSS_REPEAT_STYLE_SPACE:
117       {
118         double space, n;
119
120         extend = CAIRO_EXTEND_NONE;
121         hscale = vscale;
122
123         xstep = hscale * slice_width;
124         n = floor (width / xstep);
125         space = (width - n * xstep) / (n + 1);
126         xstep += space;
127         x += space;
128         width -= 2 * space;
129       }
130       break;
131     case GTK_CSS_REPEAT_STYLE_STRETCH:
132       break;
133     case GTK_CSS_REPEAT_STYLE_ROUND:
134       extend = CAIRO_EXTEND_REPEAT;
135       hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
136       break;
137     default:
138       g_assert_not_reached ();
139       break;
140     }
141
142   switch (vrepeat)
143     {
144     case GTK_CSS_REPEAT_STYLE_REPEAT:
145       extend = CAIRO_EXTEND_REPEAT;
146       vscale = hscale;
147       break;
148     case GTK_CSS_REPEAT_STYLE_SPACE:
149       {
150         double space, n;
151
152         extend = CAIRO_EXTEND_NONE;
153         vscale = hscale;
154
155         ystep = vscale * slice_height;
156         n = floor (height / ystep);
157         space = (height - n * ystep) / (n + 1);
158         ystep += space;
159         y += space;
160         height -= 2 * space;
161       }
162       break;
163     case GTK_CSS_REPEAT_STYLE_STRETCH:
164       break;
165     case GTK_CSS_REPEAT_STYLE_ROUND:
166       extend = CAIRO_EXTEND_REPEAT;
167       vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
168       break;
169     default:
170       g_assert_not_reached ();
171       break;
172     }
173
174   pattern = cairo_pattern_create_for_surface (slice);
175
176   cairo_matrix_init_translate (&matrix,
177                                hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
178                                vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
179   cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
180   cairo_matrix_translate (&matrix,
181                           hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
182                           vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
183
184   cairo_pattern_set_matrix (pattern, &matrix);
185   cairo_pattern_set_extend (pattern, extend);
186
187   cairo_save (cr);
188   cairo_translate (cr, x, y);
189
190   for (y = 0; y < height; y += ystep)
191     {
192       for (x = 0; x < width; x += xstep)
193         {
194           cairo_save (cr);
195           cairo_translate (cr, x, y);
196           cairo_set_source (cr, pattern);
197           cairo_rectangle (cr, 0, 0, xstep, ystep);
198           cairo_fill (cr);
199           cairo_restore (cr);
200         }
201     }
202
203   cairo_restore (cr);
204
205   cairo_pattern_destroy (pattern);
206 }
207
208 static void
209 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
210                                      int                     surface_size,
211                                      int                     start_size,
212                                      int                     end_size)
213 {
214   sizes[0].size = MIN (start_size, surface_size);
215   sizes[0].offset = 0;
216
217   sizes[2].size = MIN (end_size, surface_size);
218   sizes[2].offset = surface_size - sizes[2].size;
219
220   sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
221   sizes[1].offset = sizes[0].size;
222 }
223
224 void
225 _gtk_border_image_render (GtkBorderImage   *image,
226                           GtkBorder        *border_width,
227                           cairo_t          *cr,
228                           gdouble           x,
229                           gdouble           y,
230                           gdouble           width,
231                           gdouble           height)
232 {
233   cairo_surface_t *surface, *slice;
234   GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
235   GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
236   double source_width, source_height;
237   int h, v;
238
239   if (image->has_width)
240     border_width = &image->width;
241
242   _gtk_css_image_get_concrete_size (image->source,
243                                     0, 0,
244                                     width, height,
245                                     &source_width, &source_height);
246
247   /* XXX: Optimize for (source_width == width && source_height == height) */
248
249   surface = _gtk_css_image_get_surface (image->source,
250                                         cairo_get_target (cr),
251                                         source_width, source_height);
252
253   gtk_border_image_compute_slice_size (horizontal_slice,
254                                        source_width, 
255                                        image->slice.left,
256                                        image->slice.right);
257   gtk_border_image_compute_slice_size (vertical_slice,
258                                        source_height, 
259                                        image->slice.top,
260                                        image->slice.bottom);
261   gtk_border_image_compute_border_size (horizontal_border,
262                                         x,
263                                         width,
264                                         border_width->left,
265                                         border_width->right);
266   gtk_border_image_compute_border_size (vertical_border,
267                                         y,
268                                         height,
269                                         border_width->top,
270                                         border_width->bottom);
271   
272   for (v = 0; v < 3; v++)
273     {
274       if (vertical_slice[v].size == 0 ||
275           vertical_border[v].size == 0)
276         continue;
277
278       for (h = 0; h < 3; h++)
279         {
280           if (horizontal_slice[h].size == 0 ||
281               horizontal_border[h].size == 0)
282             continue;
283
284           if (h == 1 && v == 1)
285             continue;
286
287           slice = cairo_surface_create_for_rectangle (surface,
288                                                       horizontal_slice[h].offset,
289                                                       vertical_slice[v].offset,
290                                                       horizontal_slice[h].size,
291                                                       vertical_slice[v].size);
292
293           gtk_border_image_render_slice (cr,
294                                          slice,
295                                          horizontal_slice[h].size,
296                                          vertical_slice[v].size,
297                                          horizontal_border[h].offset,
298                                          vertical_border[v].offset,
299                                          horizontal_border[h].size,
300                                          vertical_border[v].size,
301                                          h == 1 ? image->repeat.hrepeat : GTK_CSS_REPEAT_STYLE_STRETCH,
302                                          v == 1 ? image->repeat.vrepeat : GTK_CSS_REPEAT_STYLE_STRETCH);
303
304           cairo_surface_destroy (slice);
305         }
306     }
307
308   cairo_surface_destroy (surface);
309 }