adg: split AdgParamDress from AdgDress
[adg.git] / src / adg / adg-hatch.c
blob76158baf4d44c8f24002f0cfcc738e53aaefcfd7
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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-hatch
23 * @short_description: A hatched region
25 * The #AdgHatch object is used to fill a closed #AdgTrail model
26 * with some sort of pattern.
28 * Since: 1.0
29 **/
31 /**
32 * AdgHatch:
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
37 * Since: 1.0
38 **/
41 #include "adg-internal.h"
42 #include "adg-model.h"
43 #include "adg-trail.h"
44 #include "adg-stroke.h"
45 #include "adg-style.h"
46 #include "adg-fill-style.h"
47 #include "adg-dress.h"
48 #include "adg-param-dress.h"
50 #include "adg-hatch.h"
51 #include "adg-hatch-private.h"
54 G_DEFINE_TYPE(AdgHatch, adg_hatch, ADG_TYPE_STROKE)
56 enum {
57 PROP_0,
58 PROP_FILL_DRESS
62 static void _adg_get_property (GObject *object,
63 guint param_id,
64 GValue *value,
65 GParamSpec *pspec);
66 static void _adg_set_property (GObject *object,
67 guint param_id,
68 const GValue *value,
69 GParamSpec *pspec);
70 static void _adg_render (AdgEntity *entity,
71 cairo_t *cr);
74 static void
75 adg_hatch_class_init(AdgHatchClass *klass)
77 GObjectClass *gobject_class;
78 AdgEntityClass *entity_class;
79 GParamSpec *param;
81 gobject_class = (GObjectClass *) klass;
82 entity_class = (AdgEntityClass *) klass;
84 g_type_class_add_private(klass, sizeof(AdgHatchPrivate));
86 gobject_class->get_property = _adg_get_property;
87 gobject_class->set_property = _adg_set_property;
89 entity_class->render = _adg_render;
91 param = adg_param_spec_dress("fill-dress",
92 P_("Fill Dress"),
93 P_("The dress to use for filling this entity"),
94 ADG_DRESS_FILL_HATCH,
95 G_PARAM_READWRITE);
96 g_object_class_install_property(gobject_class, PROP_FILL_DRESS, param);
99 static void
100 adg_hatch_init(AdgHatch *hatch)
102 AdgHatchPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(hatch, ADG_TYPE_HATCH,
103 AdgHatchPrivate);
105 data->fill_dress = ADG_DRESS_FILL_HATCH;
107 hatch->data = data;
110 static void
111 _adg_get_property(GObject *object, guint prop_id,
112 GValue *value, GParamSpec *pspec)
114 AdgHatchPrivate *data = ((AdgHatch *) object)->data;
116 switch (prop_id) {
117 case PROP_FILL_DRESS:
118 g_value_set_enum(value, data->fill_dress);
119 break;
120 default:
121 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
122 break;
126 static void
127 _adg_set_property(GObject *object, guint prop_id,
128 const GValue *value, GParamSpec *pspec)
130 AdgHatch *hatch;
131 AdgHatchPrivate *data;
133 hatch = (AdgHatch *) object;
134 data = hatch->data;
136 switch (prop_id) {
137 case PROP_FILL_DRESS:
138 data->fill_dress = g_value_get_enum(value);
139 break;
140 default:
141 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
142 break;
148 * adg_hatch_new:
149 * @trail: the #AdgTrail to hatch
151 * Creates a new hatch entity.
152 * @trail can be %NULL, in which case an empty hatch is created.
154 * Returns: the newly created hatch entity
156 * Since: 1.0
158 AdgHatch *
159 adg_hatch_new(AdgTrail *trail)
161 return g_object_new(ADG_TYPE_HATCH, "trail", trail, NULL);
165 * adg_hatch_set_fill_dress:
166 * @hatch: an #AdgHatch
167 * @dress: the new #AdgDress to use
169 * Sets a new line dress for rendering @hatch. The new dress
170 * must be related to the original dress for this property:
171 * you cannot set a dress used for line styles to a dress
172 * managing fonts.
174 * The check is done by calling adg_dress_are_related() with
175 * @dress and the previous dress as arguments. Check out its
176 * documentation for details on what is a related dress.
178 * Since: 1.0
180 void
181 adg_hatch_set_fill_dress(AdgHatch *hatch, AdgDress dress)
183 g_return_if_fail(ADG_IS_HATCH(hatch));
184 g_object_set(hatch, "fill-dress", dress, NULL);
188 * adg_hatch_get_fill_dress:
189 * @hatch: an #AdgHatch
191 * Gets the line dress to be used in rendering @hatch.
193 * Returns: (transfer none): the current line dress.
195 * Since: 1.0
197 AdgDress
198 adg_hatch_get_fill_dress(AdgHatch *hatch)
200 AdgHatchPrivate *data;
202 g_return_val_if_fail(ADG_IS_HATCH(hatch), ADG_DRESS_UNDEFINED);
204 data = hatch->data;
206 return data->fill_dress;
210 static void
211 _adg_render(AdgEntity *entity, cairo_t *cr)
213 AdgHatch *hatch;
214 AdgStroke *stroke;
215 AdgHatchPrivate *data;
216 const cairo_path_t *cairo_path;
218 hatch = (AdgHatch *) entity;
219 stroke = (AdgStroke *) entity;
220 data = hatch->data;
221 cairo_path = adg_trail_get_cairo_path(adg_stroke_get_trail(stroke));
223 if (cairo_path != NULL) {
224 AdgFillStyle *fill_style = (AdgFillStyle *)
225 adg_entity_style(entity, data->fill_dress);
227 adg_fill_style_set_extents(fill_style, adg_entity_get_extents(entity));
229 cairo_save(cr);
230 cairo_transform(cr, adg_entity_get_global_matrix(entity));
231 cairo_transform(cr, adg_entity_get_local_matrix(entity));
232 cairo_append_path(cr, cairo_path);
233 cairo_restore(cr);
235 adg_style_apply((AdgStyle *) fill_style, entity, cr);
236 cairo_fill(cr);