[AdgEntity] Emits "{global,local}-changed" on parent changed
[adg.git] / adg / adg-stroke.c
blob38cdf2fe06cc398cbffbf85e634cb74e6f9ddd73
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 local_changed (AdgEntity *entity);
62 static void arrange (AdgEntity *entity);
63 static void render (AdgEntity *entity,
64 cairo_t *cr);
65 static gboolean set_trail (AdgStroke *stroke,
66 AdgTrail *trail);
67 static void unset_trail (AdgStroke *stroke);
70 G_DEFINE_TYPE(AdgStroke, adg_stroke, ADG_TYPE_ENTITY);
73 static void
74 adg_stroke_class_init(AdgStrokeClass *klass)
76 GObjectClass *gobject_class;
77 AdgEntityClass *entity_class;
78 GParamSpec *param;
80 gobject_class = (GObjectClass *) klass;
81 entity_class = (AdgEntityClass *) klass;
83 g_type_class_add_private(klass, sizeof(AdgStrokePrivate));
85 gobject_class->dispose = dispose;
86 gobject_class->get_property = get_property;
87 gobject_class->set_property = set_property;
89 entity_class->local_changed = local_changed;
90 entity_class->arrange = arrange;
91 entity_class->render = render;
93 param = adg_param_spec_dress("line-dress",
94 P_("Line Dress"),
95 P_("The dress to use for stroking this entity"),
96 ADG_DRESS_LINE_MEDIUM,
97 G_PARAM_READWRITE);
98 g_object_class_install_property(gobject_class, PROP_LINE_DRESS, param);
100 param = g_param_spec_object("trail",
101 P_("Trail"),
102 P_("The trail to be stroked"),
103 ADG_TYPE_TRAIL,
104 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
105 g_object_class_install_property(gobject_class, PROP_TRAIL, param);
108 static void
109 adg_stroke_init(AdgStroke *stroke)
111 AdgStrokePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(stroke,
112 ADG_TYPE_STROKE,
113 AdgStrokePrivate);
115 data->line_dress = ADG_DRESS_LINE_MEDIUM;
116 data->trail = NULL;
118 stroke->data = data;
121 static void
122 dispose(GObject *object)
124 AdgStroke *stroke = (AdgStroke *) object;
126 adg_stroke_set_trail(stroke, NULL);
128 if (PARENT_OBJECT_CLASS->dispose != NULL)
129 PARENT_OBJECT_CLASS->dispose(object);
132 static void
133 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
135 AdgStrokePrivate *data = ((AdgStroke *) object)->data;
137 switch (prop_id) {
138 case PROP_LINE_DRESS:
139 g_value_set_int(value, data->line_dress);
140 break;
141 case PROP_TRAIL:
142 g_value_set_object(value, &data->trail);
143 break;
144 default:
145 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
146 break;
150 static void
151 set_property(GObject *object, guint prop_id,
152 const GValue *value, GParamSpec *pspec)
154 AdgStroke *stroke;
155 AdgStrokePrivate *data;
157 stroke = (AdgStroke *) object;
158 data = stroke->data;
160 switch (prop_id) {
161 case PROP_LINE_DRESS:
162 adg_dress_set(&data->line_dress, g_value_get_int(value));
163 break;
164 case PROP_TRAIL:
165 set_trail(stroke, (AdgTrail *) g_value_get_object(value));
166 break;
167 default:
168 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
169 break;
175 * adg_stroke_new:
176 * @trail: the #AdgTrail to stroke
178 * Creates a new stroke entity.
180 * Returns: the newly created stroke entity
182 AdgStroke *
183 adg_stroke_new(AdgTrail *trail)
185 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
187 return g_object_new(ADG_TYPE_STROKE, "trail", trail, NULL);
191 * adg_stroke_get_line_dress:
192 * @stroke: an #AdgStroke
194 * Gets the line dress to be used in rendering @stroke.
196 * Returns: the current line dress
198 AdgDress
199 adg_stroke_get_line_dress(AdgStroke *stroke)
201 AdgStrokePrivate *data;
203 g_return_val_if_fail(ADG_IS_STROKE(stroke), ADG_DRESS_UNDEFINED);
205 data = stroke->data;
207 return data->line_dress;
211 * adg_stroke_set_line_dress:
212 * @stroke: an #AdgStroke
213 * @dress: the new #AdgDress to use
215 * Sets a new line dress for rendering @stroke. The new dress
216 * must be related to the original dress for this property:
217 * you cannot set a dress used for line styles to a dress
218 * managing fonts.
220 * The check is done by calling adg_dress_are_related() with
221 * @dress and the previous dress as arguments. Check out its
222 * documentation for details on what is a related dress.
224 void
225 adg_stroke_set_line_dress(AdgStroke *stroke, AdgDress dress)
227 AdgStrokePrivate *data;
229 g_return_if_fail(ADG_IS_STROKE(stroke));
231 data = stroke->data;
233 if (adg_dress_set(&data->line_dress, dress))
234 g_object_notify((GObject *) stroke, "line-dress");
238 * adg_stroke_get_trail:
239 * @stroke: an #AdgStroke
241 * Gets the #AdgTrail bound to this @stroke entity.
243 * Returns: the requested #AdgTrail or %NULL on errors
245 AdgTrail *
246 adg_stroke_get_trail(AdgStroke *stroke)
248 AdgStrokePrivate *data;
250 g_return_val_if_fail(ADG_IS_STROKE(stroke), NULL);
252 data = stroke->data;
254 return data->trail;
258 * adg_stroke_set_trail:
259 * @stroke: an #AdgStroke
260 * @trail: the new #AdgTrail to bind
262 * Sets @trail as the new trail to be stroked by @stroke.
264 void
265 adg_stroke_set_trail(AdgStroke *stroke, AdgTrail *trail)
267 g_return_if_fail(ADG_IS_STROKE(stroke));
269 if (set_trail(stroke, trail))
270 g_object_notify((GObject *) stroke, "trail");
274 static void
275 local_changed(AdgEntity *entity)
277 PARENT_ENTITY_CLASS->local_changed(entity);
278 adg_entity_set_extents(entity, NULL);
281 static void
282 arrange(AdgEntity *entity)
284 CpmlExtents *extents = (CpmlExtents *) adg_entity_extents(entity);
286 if (!extents->is_defined) {
287 AdgStroke *stroke;
288 AdgStrokePrivate *data;
289 const AdgMatrix *local;
291 stroke = (AdgStroke *) entity;
292 data = stroke->data;
293 local = adg_entity_local_matrix(entity);
295 cpml_extents_copy(extents, adg_trail_extents(data->trail));
297 /* Apply the local matrix to the extents */
298 cpml_pair_transform(&extents->org, local);
299 cpml_vector_transform(&extents->size, local);
303 static void
304 render(AdgEntity *entity, cairo_t *cr)
306 AdgStroke *stroke;
307 AdgStrokePrivate *data;
308 const cairo_path_t *cairo_path;
310 stroke = (AdgStroke *) entity;
311 data = stroke->data;
312 cairo_path = adg_trail_get_cairo_path(data->trail);
314 if (cairo_path != NULL) {
315 cairo_save(cr);
316 cairo_set_matrix(cr, adg_entity_ctm(entity));
317 cairo_append_path(cr, cairo_path);
318 cairo_restore(cr);
320 adg_entity_apply_dress(entity, data->line_dress, cr);
321 cairo_stroke(cr);
325 static gboolean
326 set_trail(AdgStroke *stroke, AdgTrail *trail)
328 AdgEntity *entity;
329 AdgStrokePrivate *data;
331 entity = (AdgEntity *) stroke;
332 data = stroke->data;
334 if (trail == data->trail)
335 return FALSE;
337 if (data->trail != NULL) {
338 g_object_weak_unref((GObject *) data->trail,
339 (GWeakNotify) unset_trail, stroke);
340 adg_model_remove_dependency((AdgModel *) data->trail, entity);
343 data->trail = trail;
345 if (data->trail != NULL) {
346 g_object_weak_ref((GObject *) data->trail,
347 (GWeakNotify) unset_trail, stroke);
348 adg_model_add_dependency((AdgModel *) data->trail, entity);
351 return TRUE;
354 static void
355 unset_trail(AdgStroke *stroke)
357 AdgStrokePrivate *data = stroke->data;
359 if (data->trail != NULL)
360 set_trail(stroke, NULL);