2011-05-26 19:53:28 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Authors: Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
* Cosimo Cecchi <cosimoc@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-26 19:53:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <cairo-gobject.h>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "gtkborderimageprivate.h"
|
2012-04-04 14:15:41 +00:00
|
|
|
#include "gtkcssbordervalueprivate.h"
|
2012-03-29 00:58:32 +00:00
|
|
|
#include "gtkcssimagevalueprivate.h"
|
2012-04-04 14:15:41 +00:00
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2012-04-04 09:44:57 +00:00
|
|
|
#include "gtkcssrepeatvalueprivate.h"
|
2014-06-16 02:18:51 +00:00
|
|
|
#include "gtkstylepropertyprivate.h"
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-08-19 06:26:52 +00:00
|
|
|
/* this is in case round() is not provided by the compiler,
|
|
|
|
* such as in the case of C89 compilers, like MSVC
|
|
|
|
*/
|
|
|
|
#include "fallback-c89.c"
|
|
|
|
|
2012-01-04 03:17:38 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_border_image_init (GtkBorderImage *image,
|
2014-08-16 17:00:03 +00:00
|
|
|
GtkStyleContext *context)
|
2012-01-04 03:00:58 +00:00
|
|
|
{
|
2014-08-16 17:00:03 +00:00
|
|
|
image->source = _gtk_css_image_value_get_image (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_IMAGE_SOURCE));
|
2012-01-04 03:17:38 +00:00
|
|
|
if (image->source == NULL)
|
|
|
|
return FALSE;
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2014-08-16 17:00:03 +00:00
|
|
|
image->slice = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_IMAGE_SLICE);
|
|
|
|
image->width = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_IMAGE_WIDTH);
|
|
|
|
image->repeat = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_IMAGE_REPEAT);
|
2011-06-09 23:59:14 +00:00
|
|
|
|
2012-01-04 03:17:38 +00:00
|
|
|
return TRUE;
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
|
|
|
|
struct _GtkBorderImageSliceSize {
|
|
|
|
double offset;
|
|
|
|
double size;
|
|
|
|
};
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
static void
|
2012-04-04 15:42:45 +00:00
|
|
|
gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
|
|
|
|
double offset,
|
|
|
|
double area_size,
|
2012-10-27 01:16:35 +00:00
|
|
|
double start_border_width,
|
|
|
|
double end_border_width,
|
2012-04-04 15:42:45 +00:00
|
|
|
const GtkCssValue *start_border,
|
|
|
|
const GtkCssValue *end_border)
|
2011-05-26 19:53:28 +00:00
|
|
|
{
|
2012-04-04 15:42:45 +00:00
|
|
|
double start, end;
|
|
|
|
|
|
|
|
if (_gtk_css_number_value_get_unit (start_border) == GTK_CSS_NUMBER)
|
|
|
|
start = start_border_width * _gtk_css_number_value_get (start_border, 100);
|
|
|
|
else
|
|
|
|
start = _gtk_css_number_value_get (start_border, area_size);
|
|
|
|
if (_gtk_css_number_value_get_unit (end_border) == GTK_CSS_NUMBER)
|
|
|
|
end = end_border_width * _gtk_css_number_value_get (end_border, 100);
|
|
|
|
else
|
|
|
|
end = _gtk_css_number_value_get (end_border, area_size);
|
|
|
|
|
|
|
|
/* XXX: reduce vertical and horizontal by the same factor */
|
|
|
|
if (start + end > area_size)
|
|
|
|
{
|
|
|
|
start = start * area_size / (start + end);
|
|
|
|
end = end * area_size / (start + end);
|
|
|
|
}
|
2011-06-08 23:41:18 +00:00
|
|
|
|
|
|
|
sizes[0].offset = offset;
|
2012-04-04 15:42:45 +00:00
|
|
|
sizes[0].size = start;
|
|
|
|
sizes[1].offset = offset + start;
|
|
|
|
sizes[1].size = area_size - start - end;
|
|
|
|
sizes[2].offset = offset + area_size - end;
|
|
|
|
sizes[2].size = end;
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-08 23:41:18 +00:00
|
|
|
gtk_border_image_render_slice (cairo_t *cr,
|
|
|
|
cairo_surface_t *slice,
|
|
|
|
double slice_width,
|
|
|
|
double slice_height,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
double width,
|
|
|
|
double height,
|
2012-04-04 09:44:57 +00:00
|
|
|
GtkCssRepeatStyle hrepeat,
|
|
|
|
GtkCssRepeatStyle vrepeat)
|
2011-05-26 19:53:28 +00:00
|
|
|
{
|
2011-06-08 23:41:18 +00:00
|
|
|
double hscale, vscale;
|
|
|
|
double xstep, ystep;
|
|
|
|
cairo_extend_t extend = CAIRO_EXTEND_PAD;
|
|
|
|
cairo_matrix_t matrix;
|
2011-05-26 19:53:28 +00:00
|
|
|
cairo_pattern_t *pattern;
|
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
/* We can't draw center tiles yet */
|
2011-11-25 14:30:10 +00:00
|
|
|
g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
hscale = width / slice_width;
|
|
|
|
vscale = height / slice_height;
|
|
|
|
xstep = width;
|
|
|
|
ystep = height;
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
switch (hrepeat)
|
2011-05-26 19:53:28 +00:00
|
|
|
{
|
2011-06-08 23:41:18 +00:00
|
|
|
case GTK_CSS_REPEAT_STYLE_REPEAT:
|
|
|
|
extend = CAIRO_EXTEND_REPEAT;
|
|
|
|
hscale = vscale;
|
|
|
|
break;
|
|
|
|
case GTK_CSS_REPEAT_STYLE_SPACE:
|
|
|
|
{
|
|
|
|
double space, n;
|
|
|
|
|
|
|
|
extend = CAIRO_EXTEND_NONE;
|
|
|
|
hscale = vscale;
|
|
|
|
|
|
|
|
xstep = hscale * slice_width;
|
|
|
|
n = floor (width / xstep);
|
|
|
|
space = (width - n * xstep) / (n + 1);
|
|
|
|
xstep += space;
|
|
|
|
x += space;
|
|
|
|
width -= 2 * space;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-25 14:30:10 +00:00
|
|
|
case GTK_CSS_REPEAT_STYLE_STRETCH:
|
2011-06-08 23:41:18 +00:00
|
|
|
break;
|
|
|
|
case GTK_CSS_REPEAT_STYLE_ROUND:
|
|
|
|
extend = CAIRO_EXTEND_REPEAT;
|
|
|
|
hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
switch (vrepeat)
|
2011-05-26 19:53:28 +00:00
|
|
|
{
|
2011-06-08 23:41:18 +00:00
|
|
|
case GTK_CSS_REPEAT_STYLE_REPEAT:
|
|
|
|
extend = CAIRO_EXTEND_REPEAT;
|
|
|
|
vscale = hscale;
|
|
|
|
break;
|
|
|
|
case GTK_CSS_REPEAT_STYLE_SPACE:
|
|
|
|
{
|
|
|
|
double space, n;
|
|
|
|
|
|
|
|
extend = CAIRO_EXTEND_NONE;
|
|
|
|
vscale = hscale;
|
|
|
|
|
|
|
|
ystep = vscale * slice_height;
|
|
|
|
n = floor (height / ystep);
|
|
|
|
space = (height - n * ystep) / (n + 1);
|
|
|
|
ystep += space;
|
|
|
|
y += space;
|
|
|
|
height -= 2 * space;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-25 14:30:10 +00:00
|
|
|
case GTK_CSS_REPEAT_STYLE_STRETCH:
|
2011-06-08 23:41:18 +00:00
|
|
|
break;
|
|
|
|
case GTK_CSS_REPEAT_STYLE_ROUND:
|
|
|
|
extend = CAIRO_EXTEND_REPEAT;
|
|
|
|
vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
pattern = cairo_pattern_create_for_surface (slice);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_matrix_init_translate (&matrix,
|
|
|
|
hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
|
|
|
|
vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
|
|
|
|
cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
|
|
|
|
cairo_matrix_translate (&matrix,
|
|
|
|
hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
|
|
|
|
vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_pattern_set_matrix (pattern, &matrix);
|
|
|
|
cairo_pattern_set_extend (pattern, extend);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
cairo_translate (cr, x, y);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
for (y = 0; y < height; y += ystep)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x += xstep)
|
2011-05-26 19:53:28 +00:00
|
|
|
{
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
cairo_translate (cr, x, y);
|
|
|
|
cairo_set_source (cr, pattern);
|
|
|
|
cairo_rectangle (cr, 0, 0, xstep, ystep);
|
|
|
|
cairo_fill (cr);
|
|
|
|
cairo_restore (cr);
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_restore (cr);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
cairo_pattern_destroy (pattern);
|
|
|
|
}
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
static void
|
|
|
|
gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
|
|
|
|
int surface_size,
|
|
|
|
int start_size,
|
|
|
|
int end_size)
|
|
|
|
{
|
|
|
|
sizes[0].size = MIN (start_size, surface_size);
|
|
|
|
sizes[0].offset = 0;
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
sizes[2].size = MIN (end_size, surface_size);
|
|
|
|
sizes[2].offset = surface_size - sizes[2].size;
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
|
|
|
|
sizes[1].offset = sizes[0].size;
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_border_image_render (GtkBorderImage *image,
|
2012-10-27 01:16:35 +00:00
|
|
|
const double border_width[4],
|
2011-05-26 19:53:28 +00:00
|
|
|
cairo_t *cr,
|
|
|
|
gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gdouble width,
|
|
|
|
gdouble height)
|
|
|
|
{
|
2011-06-01 14:23:36 +00:00
|
|
|
cairo_surface_t *surface, *slice;
|
2011-06-08 23:41:18 +00:00
|
|
|
GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
|
|
|
|
GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
|
2011-12-21 16:43:31 +00:00
|
|
|
double source_width, source_height;
|
2011-06-08 23:41:18 +00:00
|
|
|
int h, v;
|
2011-06-01 14:23:36 +00:00
|
|
|
|
2011-12-21 16:43:31 +00:00
|
|
|
_gtk_css_image_get_concrete_size (image->source,
|
|
|
|
0, 0,
|
|
|
|
width, height,
|
|
|
|
&source_width, &source_height);
|
2011-06-01 14:23:36 +00:00
|
|
|
|
2011-12-21 16:43:31 +00:00
|
|
|
/* XXX: Optimize for (source_width == width && source_height == height) */
|
2011-06-01 14:23:36 +00:00
|
|
|
|
2011-12-21 16:43:31 +00:00
|
|
|
surface = _gtk_css_image_get_surface (image->source,
|
|
|
|
cairo_get_target (cr),
|
|
|
|
source_width, source_height);
|
2011-05-26 19:53:28 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
gtk_border_image_compute_slice_size (horizontal_slice,
|
2011-12-21 16:43:31 +00:00
|
|
|
source_width,
|
2012-04-04 14:15:41 +00:00
|
|
|
_gtk_css_number_value_get (_gtk_css_border_value_get_left (image->slice), source_width),
|
|
|
|
_gtk_css_number_value_get (_gtk_css_border_value_get_right (image->slice), source_width));
|
2011-06-08 23:41:18 +00:00
|
|
|
gtk_border_image_compute_slice_size (vertical_slice,
|
2011-12-21 16:43:31 +00:00
|
|
|
source_height,
|
2012-04-04 14:15:41 +00:00
|
|
|
_gtk_css_number_value_get (_gtk_css_border_value_get_top (image->slice), source_height),
|
|
|
|
_gtk_css_number_value_get (_gtk_css_border_value_get_bottom (image->slice), source_height));
|
2011-06-08 23:41:18 +00:00
|
|
|
gtk_border_image_compute_border_size (horizontal_border,
|
|
|
|
x,
|
|
|
|
width,
|
2012-10-27 01:16:35 +00:00
|
|
|
border_width[GTK_CSS_LEFT],
|
|
|
|
border_width[GTK_CSS_RIGHT],
|
2012-04-04 15:42:45 +00:00
|
|
|
_gtk_css_border_value_get_left (image->width),
|
|
|
|
_gtk_css_border_value_get_right (image->width));
|
2011-06-08 23:41:18 +00:00
|
|
|
gtk_border_image_compute_border_size (vertical_border,
|
|
|
|
y,
|
|
|
|
height,
|
2012-10-27 01:16:35 +00:00
|
|
|
border_width[GTK_CSS_TOP],
|
|
|
|
border_width[GTK_CSS_BOTTOM],
|
2012-04-04 15:42:45 +00:00
|
|
|
_gtk_css_border_value_get_top (image->width),
|
|
|
|
_gtk_css_border_value_get_bottom(image->width));
|
2011-06-08 23:41:18 +00:00
|
|
|
|
|
|
|
for (v = 0; v < 3; v++)
|
2011-06-01 14:23:36 +00:00
|
|
|
{
|
2011-06-08 23:41:18 +00:00
|
|
|
if (vertical_slice[v].size == 0 ||
|
|
|
|
vertical_border[v].size == 0)
|
|
|
|
continue;
|
2011-06-01 14:23:36 +00:00
|
|
|
|
2011-06-08 23:41:18 +00:00
|
|
|
for (h = 0; h < 3; h++)
|
|
|
|
{
|
|
|
|
if (horizontal_slice[h].size == 0 ||
|
2011-06-09 21:05:27 +00:00
|
|
|
horizontal_border[h].size == 0)
|
2011-06-08 23:41:18 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (h == 1 && v == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
slice = cairo_surface_create_for_rectangle (surface,
|
|
|
|
horizontal_slice[h].offset,
|
|
|
|
vertical_slice[v].offset,
|
|
|
|
horizontal_slice[h].size,
|
|
|
|
vertical_slice[v].size);
|
|
|
|
|
|
|
|
gtk_border_image_render_slice (cr,
|
|
|
|
slice,
|
|
|
|
horizontal_slice[h].size,
|
|
|
|
vertical_slice[v].size,
|
|
|
|
horizontal_border[h].offset,
|
|
|
|
vertical_border[v].offset,
|
|
|
|
horizontal_border[h].size,
|
|
|
|
vertical_border[v].size,
|
2012-04-04 09:44:57 +00:00
|
|
|
h == 1 ? _gtk_css_border_repeat_value_get_x (image->repeat) : GTK_CSS_REPEAT_STYLE_STRETCH,
|
|
|
|
v == 1 ? _gtk_css_border_repeat_value_get_y (image->repeat) : GTK_CSS_REPEAT_STYLE_STRETCH);
|
2011-06-08 23:41:18 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (slice);
|
|
|
|
}
|
2011-06-01 14:23:36 +00:00
|
|
|
}
|
2011-06-15 11:45:34 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
2011-05-26 19:53:28 +00:00
|
|
|
}
|