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