[docs] Updated TODO.xml
[adg.git] / adg / adg-stroke.c
blobbd82891726b137ce14fab579b0083f4e6ff817fe
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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-stroke
23 * @short_description: A stroked entity
25 * The #AdgStroke object is a stroked representation of an #AdgTrail model.
26 **/
28 /**
29 * AdgStroke:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-stroke.h"
37 #include "adg-stroke-private.h"
38 #include "adg-dress-builtins.h"
39 #include "adg-line-style.h"
40 #include "adg-intl.h"
42 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_stroke_parent_class)
43 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_stroke_parent_class)
46 enum {
47 PROP_0,
48 PROP_LINE_DRESS,
49 PROP_TRAIL
52 static void dispose (GObject *object);
53 static void get_property (GObject *object,
54 guint param_id,
55 GValue *value,
56 GParamSpec *pspec);
57 static void set_property (GObject *object,
58 guint param_id,
59 const GValue *value,
60 GParamSpec *pspec);
61 static void global_changed (AdgEntity *entity);
62 static void local_changed (AdgEntity *entity);
63 static void arrange (AdgEntity *entity);
64 static void render (AdgEntity *entity,
65 cairo_t *cr);
66 static gboolean set_trail (AdgStroke *stroke,
67 AdgTrail *trail);
68 static void unset_trail (AdgStroke *stroke);
71 G_DEFINE_TYPE(AdgStroke, adg_stroke, ADG_TYPE_ENTITY);
74 static void
75 adg_stroke_class_init(AdgStrokeClass *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(AdgStrokePrivate));
86 gobject_class->dispose = dispose;
87 gobject_class->get_property = get_property;
88 gobject_class->set_property = set_property;
90 entity_class->global_changed = global_changed;
91 entity_class->local_changed = local_changed;
92 entity_class->arrange = arrange;
93 entity_class->render = render;
95 param = adg_param_spec_dress("line-dress",
96 P_("Line Dress"),
97 P_("The dress to use for stroking this entity"),
98 ADG_DRESS_LINE_MEDIUM,
99 G_PARAM_READWRITE);
100 g_object_class_install_property(gobject_class, PROP_LINE_DRESS, param);
102 param = g_param_spec_object("trail",
103 P_("Trail"),
104 P_("The trail to be stroked"),
105 ADG_TYPE_TRAIL,
106 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
107 g_object_class_install_property(gobject_class, PROP_TRAIL, param);
110 static void
111 adg_stroke_init(AdgStroke *stroke)
113 AdgStrokePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(stroke,
114 ADG_TYPE_STROKE,
115 AdgStrokePrivate);
117 data->line_dress = ADG_DRESS_LINE_MEDIUM;
118 data->trail = NULL;
120 stroke->data = data;
123 static void
124 dispose(GObject *object)
126 AdgStroke *stroke = (AdgStroke *) object;
128 adg_stroke_set_trail(stroke, NULL);
130 if (PARENT_OBJECT_CLASS->dispose)
131 PARENT_OBJECT_CLASS->dispose(object);
134 static void
135 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
137 AdgStrokePrivate *data = ((AdgStroke *) object)->data;
139 switch (prop_id) {
140 case PROP_LINE_DRESS:
141 g_value_set_int(value, data->line_dress);
142 break;
143 case PROP_TRAIL:
144 g_value_set_object(value, &data->trail);
145 break;
146 default:
147 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
148 break;
152 static void
153 set_property(GObject *object, guint prop_id,
154 const GValue *value, GParamSpec *pspec)
156 AdgStroke *stroke;
157 AdgStrokePrivate *data;
159 stroke = (AdgStroke *) object;
160 data = stroke->data;
162 switch (prop_id) {
163 case PROP_LINE_DRESS:
164 adg_dress_set(&data->line_dress, g_value_get_int(value));
165 break;
166 case PROP_TRAIL:
167 set_trail(stroke, (AdgTrail *) g_value_get_object(value));
168 break;
169 default:
170 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
171 break;
177 * adg_stroke_new:
178 * @trail: the #AdgTrail to stroke
180 * Creates a new stroke entity.
182 * Returns: the newly created stroke entity
184 AdgStroke *
185 adg_stroke_new(AdgTrail *trail)
187 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
189 return g_object_new(ADG_TYPE_STROKE, "trail", trail, NULL);
193 * adg_stroke_set_line_dress:
194 * @stroke: an #AdgStroke
195 * @dress: the new #AdgDress to use
197 * Sets a new line dress for rendering @stroke. The new dress
198 * must be related to the original dress for this property:
199 * you cannot set a dress used for line styles to a dress
200 * managing fonts.
202 * The check is done by calling adg_dress_are_related() with
203 * @dress and the previous dress as arguments. Check out its
204 * documentation for details on what is a related dress.
206 void
207 adg_stroke_set_line_dress(AdgStroke *stroke, AdgDress dress)
209 AdgStrokePrivate *data;
211 g_return_if_fail(ADG_IS_STROKE(stroke));
213 data = stroke->data;
215 if (adg_dress_set(&data->line_dress, dress))
216 g_object_notify((GObject *) stroke, "line-dress");
220 * adg_stroke_get_line_dress:
221 * @stroke: an #AdgStroke
223 * Gets the line dress to be used in rendering @stroke.
225 * Returns: the current line dress
227 AdgDress
228 adg_stroke_get_line_dress(AdgStroke *stroke)
230 AdgStrokePrivate *data;
232 g_return_val_if_fail(ADG_IS_STROKE(stroke), ADG_DRESS_UNDEFINED);
234 data = stroke->data;
236 return data->line_dress;
240 * adg_stroke_set_trail:
241 * @stroke: an #AdgStroke
242 * @trail: the new #AdgTrail to bind
244 * Sets @trail as the new trail to be stroked by @stroke.
246 void
247 adg_stroke_set_trail(AdgStroke *stroke, AdgTrail *trail)
249 g_return_if_fail(ADG_IS_STROKE(stroke));
251 if (set_trail(stroke, trail))
252 g_object_notify((GObject *) stroke, "trail");
256 * adg_stroke_get_trail:
257 * @stroke: an #AdgStroke
259 * Gets the #AdgTrail bound to this @stroke entity.
261 * Returns: the requested #AdgTrail or %NULL on errors
263 AdgTrail *
264 adg_stroke_get_trail(AdgStroke *stroke)
266 AdgStrokePrivate *data;
268 g_return_val_if_fail(ADG_IS_STROKE(stroke), NULL);
270 data = stroke->data;
272 return data->trail;
276 static void
277 global_changed(AdgEntity *entity)
279 if (PARENT_ENTITY_CLASS->global_changed)
280 PARENT_ENTITY_CLASS->global_changed(entity);
282 adg_entity_invalidate(entity);
285 static void
286 local_changed(AdgEntity *entity)
288 if (PARENT_ENTITY_CLASS->local_changed)
289 PARENT_ENTITY_CLASS->local_changed(entity);
291 adg_entity_invalidate(entity);
294 static void
295 arrange(AdgEntity *entity)
297 AdgStroke *stroke;
298 AdgStrokePrivate *data;
299 CpmlExtents extents;
301 /* Check for cached result */
302 if (adg_entity_get_extents(entity)->is_defined)
303 return;
305 stroke = (AdgStroke *) entity;
306 data = stroke->data;
308 cpml_extents_copy(&extents, adg_trail_get_extents(data->trail));
309 cpml_extents_transform(&extents, adg_entity_get_local_matrix(entity));
310 cpml_extents_transform(&extents, adg_entity_get_global_matrix(entity));
312 adg_entity_set_extents(entity, &extents);
315 static void
316 render(AdgEntity *entity, cairo_t *cr)
318 AdgStroke *stroke;
319 AdgStrokePrivate *data;
320 const cairo_path_t *cairo_path;
322 stroke = (AdgStroke *) entity;
323 data = stroke->data;
324 cairo_path = adg_trail_get_cairo_path(data->trail);
326 if (cairo_path != NULL) {
327 cairo_save(cr);
328 cairo_transform(cr, adg_entity_get_local_matrix(entity));
329 cairo_append_path(cr, cairo_path);
330 cairo_restore(cr);
332 adg_entity_apply_dress(entity, data->line_dress, cr);
333 cairo_stroke(cr);
337 static gboolean
338 set_trail(AdgStroke *stroke, AdgTrail *trail)
340 AdgEntity *entity;
341 AdgStrokePrivate *data;
343 entity = (AdgEntity *) stroke;
344 data = stroke->data;
346 if (trail == data->trail)
347 return FALSE;
349 if (data->trail != NULL) {
350 g_object_weak_unref((GObject *) data->trail,
351 (GWeakNotify) unset_trail, stroke);
352 adg_model_remove_dependency((AdgModel *) data->trail, entity);
355 data->trail = trail;
357 if (data->trail != NULL) {
358 g_object_weak_ref((GObject *) data->trail,
359 (GWeakNotify) unset_trail, stroke);
360 adg_model_add_dependency((AdgModel *) data->trail, entity);
363 return TRUE;
366 static void
367 unset_trail(AdgStroke *stroke)
369 AdgStrokePrivate *data = stroke->data;
371 if (data->trail != NULL)
372 set_trail(stroke, NULL);