adg: removed AdgPattern
[adg.git] / src / adg / adg-fill-style.c
blobc277db1b067f2badeb369ac8f7c5eba485c837d7
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-fill-style
23 * @short_description: Generic pattern fill
25 * A style defining a generic fill based on cairo_pattern_t.
27 * Since: 1.0
28 **/
30 /**
31 * AdgFillStyle:
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
36 * Since: 1.0
37 **/
39 /**
40 * AdgFillStyleClass:
41 * @set_extents: virtual method that specifies where a specific fill style
42 * must be applied. It is called by #AdgHatch in the rendering
43 * phase passing with its boundary box as argument.
45 * The default @set_extents@ implementation simply sets the extents owned by
46 * the fill style instance to the one provided, so the last call has
47 * precedence. Any derived class can override it to customize this behavior,
48 * for example to keep the greatest boundary box instead of the last one.
50 * Since: 1.0
51 **/
54 #include "adg-internal.h"
55 #include "adg-style.h"
57 #include "adg-fill-style.h"
58 #include "adg-fill-style-private.h"
61 G_DEFINE_ABSTRACT_TYPE(AdgFillStyle, adg_fill_style, ADG_TYPE_STYLE)
63 enum {
64 PROP_0,
65 PROP_PATTERN
69 static void _adg_finalize (GObject *object);
70 static void _adg_get_property (GObject *object,
71 guint prop_id,
72 GValue *value,
73 GParamSpec *pspec);
74 static void _adg_set_property (GObject *object,
75 guint prop_id,
76 const GValue *value,
77 GParamSpec *pspec);
78 static void _adg_apply (AdgStyle *style,
79 AdgEntity *entity,
80 cairo_t *cr);
81 static void _adg_set_extents (AdgFillStyle *fill_style,
82 const CpmlExtents *extents);
85 static void
86 adg_fill_style_class_init(AdgFillStyleClass *klass)
88 GObjectClass *gobject_class;
89 AdgStyleClass *style_class;
90 GParamSpec *param;
92 gobject_class = (GObjectClass *) klass;
93 style_class = (AdgStyleClass *) klass;
95 g_type_class_add_private(klass, sizeof(AdgFillStylePrivate));
97 gobject_class->finalize = _adg_finalize;
98 gobject_class->get_property = _adg_get_property;
99 gobject_class->set_property = _adg_set_property;
101 style_class->apply = _adg_apply;
103 klass->set_extents = _adg_set_extents;
105 param = g_param_spec_boxed("pattern",
106 P_("Pattern"),
107 P_("The cairo pattern set for this entity"),
108 CAIRO_GOBJECT_TYPE_PATTERN,
109 G_PARAM_READWRITE);
110 g_object_class_install_property(gobject_class, PROP_PATTERN, param);
113 static void
114 adg_fill_style_init(AdgFillStyle *fill_style)
116 AdgFillStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(fill_style,
117 ADG_TYPE_FILL_STYLE,
118 AdgFillStylePrivate);
120 data->pattern = NULL;
122 fill_style->data = data;
125 static void
126 _adg_finalize(GObject *object)
128 AdgFillStylePrivate *data = ((AdgFillStyle *) object)->data;
130 if (data->pattern != NULL) {
131 cairo_pattern_destroy(data->pattern);
132 data->pattern = NULL;
136 static void
137 _adg_get_property(GObject *object, guint prop_id,
138 GValue *value, GParamSpec *pspec)
140 AdgFillStylePrivate *data = ((AdgFillStyle *) object)->data;
142 switch (prop_id) {
143 case PROP_PATTERN:
144 g_value_set_boxed(value, data->pattern);
145 break;
146 default:
147 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
148 break;
152 static void
153 _adg_set_property(GObject *object, guint prop_id,
154 const GValue *value, GParamSpec *pspec)
156 AdgFillStylePrivate *data = ((AdgFillStyle *) object)->data;
157 cairo_pattern_t *old_pattern;
159 switch (prop_id) {
160 case PROP_PATTERN:
161 old_pattern = data->pattern;
162 data->pattern = g_value_get_boxed(value);
164 if (data->pattern)
165 cairo_pattern_reference(data->pattern);
166 if (old_pattern)
167 cairo_pattern_destroy(old_pattern);
168 break;
169 default:
170 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
171 break;
177 * adg_fill_style_set_pattern:
178 * @fill_style: an #AdgFillStyle
179 * @pattern: the new pattern
181 * <note><para>
182 * This function is only useful in new fill implementations.
183 * </para></note>
185 * Sets a new pattern on @fill_style. A new reference is added to
186 * @pattern with cairo_pattern_reference() and the old pattern
187 * (if any) is unreferenced with cairo_pattern_destroy().
189 * Since: 1.0
191 void
192 adg_fill_style_set_pattern(AdgFillStyle *fill_style,
193 cairo_pattern_t *pattern)
195 g_return_if_fail(ADG_IS_FILL_STYLE(fill_style));
196 g_object_set(fill_style, "pattern", pattern, NULL);
200 * adg_fill_style_get_pattern:
201 * @fill_style: an #AdgFillStyle
203 * Gets the current pattern binded to @fill_style.
205 * Returns: (transfer none): the current pattern
207 * Since: 1.0
209 cairo_pattern_t *
210 adg_fill_style_get_pattern(AdgFillStyle *fill_style)
212 AdgFillStylePrivate *data;
214 g_return_val_if_fail(ADG_IS_FILL_STYLE(fill_style), NULL);
216 data = fill_style->data;
218 return data->pattern;
222 * adg_fill_style_set_extents:
223 * @fill_style: an #AdgFillStyle
224 * @extents: the new extents
226 * <note><para>
227 * This function is only useful in new fill style implementations.
228 * </para></note>
230 * Forcibly sets new extents on @fill_style. Any fill style class
231 * that want to make some kind of customization can override the
232 * set_extents() virtual method to intercept any extents change.
234 * Sets new extents on @fill_style. These extents are usually set
235 * by the arrange() method of the entity using this filling style.
237 * Since: 1.0
239 void
240 adg_fill_style_set_extents(AdgFillStyle *fill_style,
241 const CpmlExtents *extents)
243 AdgFillStyleClass *klass;
245 g_return_if_fail(ADG_IS_FILL_STYLE(fill_style));
246 g_return_if_fail(extents != NULL);
248 klass = ADG_FILL_STYLE_GET_CLASS(fill_style);
250 if (klass->set_extents)
251 klass->set_extents(fill_style, extents);
255 * adg_fill_style_get_extents:
256 * @fill_style: an #AdgFillStyle
258 * Stores a copy of the extents of @fill_style in @extents.
259 * This struct specifies the maximum portion (in global space)
260 * this fill style should be applied: it will clamped by the
261 * entities as needed.
263 * Returns: (transfer none): the extents of @fill_style or %NULL on errors.
265 * Since: 1.0
267 const CpmlExtents *
268 adg_fill_style_get_extents(AdgFillStyle *fill_style)
270 AdgFillStylePrivate *data;
272 g_return_val_if_fail(ADG_IS_FILL_STYLE(fill_style), NULL);
274 data = fill_style->data;
276 return &data->extents;
280 static void
281 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
283 AdgFillStylePrivate *data = ((AdgFillStyle *) style)->data;
285 if (data->pattern == NULL)
286 g_warning(_("%s: pattern undefined for type `%s'"),
287 G_STRLOC, g_type_name(G_OBJECT_TYPE(style)));
288 else
289 cairo_set_source(cr, data->pattern);
292 static void
293 _adg_set_extents(AdgFillStyle *fill_style, const CpmlExtents *extents)
295 AdgFillStylePrivate *data = fill_style->data;
297 cpml_extents_copy(&data->extents, extents);