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.
25 #include <cairo-gobject.h>
29 #include "gtkborderimageprivate.h"
30 #include "gtkstylepropertiesprivate.h"
31 #include "gtkthemingengineprivate.h"
33 /* this is in case round() is not provided by the compiler,
34 * such as in the case of C89 compilers, like MSVC
36 #include "fallback-c89.c"
39 _gtk_border_image_init (GtkBorderImage *image,
40 GtkThemingEngine *engine)
44 image->source = g_value_get_object (_gtk_theming_engine_peek_property (engine, "border-image-source"));
45 if (image->source == NULL)
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"));
52 image->width = *width;
53 image->has_width = TRUE;
56 image->has_width = FALSE;
58 image->repeat = *(GtkCssBorderImageRepeat *) g_value_get_boxed (
59 _gtk_theming_engine_peek_property (engine, "border-image-repeat"));
64 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
65 struct _GtkBorderImageSliceSize {
71 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
77 /* This code assumes area_size >= start_border + end_border */
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;
88 gtk_border_image_render_slice (cairo_t *cr,
89 cairo_surface_t *slice,
96 GtkCssBorderRepeatStyle hrepeat,
97 GtkCssBorderRepeatStyle vrepeat)
99 double hscale, vscale;
101 cairo_extend_t extend = CAIRO_EXTEND_PAD;
102 cairo_matrix_t matrix;
103 cairo_pattern_t *pattern;
105 /* We can't draw center tiles yet */
106 g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
108 hscale = width / slice_width;
109 vscale = height / slice_height;
115 case GTK_CSS_REPEAT_STYLE_REPEAT:
116 extend = CAIRO_EXTEND_REPEAT;
119 case GTK_CSS_REPEAT_STYLE_SPACE:
123 extend = CAIRO_EXTEND_NONE;
126 xstep = hscale * slice_width;
127 n = floor (width / xstep);
128 space = (width - n * xstep) / (n + 1);
134 case GTK_CSS_REPEAT_STYLE_STRETCH:
136 case GTK_CSS_REPEAT_STYLE_ROUND:
137 extend = CAIRO_EXTEND_REPEAT;
138 hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
141 g_assert_not_reached ();
147 case GTK_CSS_REPEAT_STYLE_REPEAT:
148 extend = CAIRO_EXTEND_REPEAT;
151 case GTK_CSS_REPEAT_STYLE_SPACE:
155 extend = CAIRO_EXTEND_NONE;
158 ystep = vscale * slice_height;
159 n = floor (height / ystep);
160 space = (height - n * ystep) / (n + 1);
166 case GTK_CSS_REPEAT_STYLE_STRETCH:
168 case GTK_CSS_REPEAT_STYLE_ROUND:
169 extend = CAIRO_EXTEND_REPEAT;
170 vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
173 g_assert_not_reached ();
177 pattern = cairo_pattern_create_for_surface (slice);
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);
187 cairo_pattern_set_matrix (pattern, &matrix);
188 cairo_pattern_set_extend (pattern, extend);
191 cairo_translate (cr, x, y);
193 for (y = 0; y < height; y += ystep)
195 for (x = 0; x < width; x += xstep)
198 cairo_translate (cr, x, y);
199 cairo_set_source (cr, pattern);
200 cairo_rectangle (cr, 0, 0, xstep, ystep);
208 cairo_pattern_destroy (pattern);
212 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
217 sizes[0].size = MIN (start_size, surface_size);
220 sizes[2].size = MIN (end_size, surface_size);
221 sizes[2].offset = surface_size - sizes[2].size;
223 sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
224 sizes[1].offset = sizes[0].size;
228 _gtk_border_image_render (GtkBorderImage *image,
229 GtkBorder *border_width,
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;
242 if (image->has_width)
243 border_width = &image->width;
245 _gtk_css_image_get_concrete_size (image->source,
248 &source_width, &source_height);
250 /* XXX: Optimize for (source_width == width && source_height == height) */
252 surface = _gtk_css_image_get_surface (image->source,
253 cairo_get_target (cr),
254 source_width, source_height);
256 gtk_border_image_compute_slice_size (horizontal_slice,
260 gtk_border_image_compute_slice_size (vertical_slice,
263 image->slice.bottom);
264 gtk_border_image_compute_border_size (horizontal_border,
268 border_width->right);
269 gtk_border_image_compute_border_size (vertical_border,
273 border_width->bottom);
275 for (v = 0; v < 3; v++)
277 if (vertical_slice[v].size == 0 ||
278 vertical_border[v].size == 0)
281 for (h = 0; h < 3; h++)
283 if (horizontal_slice[h].size == 0 ||
284 horizontal_border[h].size == 0)
287 if (h == 1 && v == 1)
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);
296 gtk_border_image_render_slice (cr,
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);
307 cairo_surface_destroy (slice);
311 cairo_surface_destroy (surface);