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.
23 * @short_description: A stroked entity
25 * The #AdgStroke object is a stroked representation of an #AdgTrail model.
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
36 #include "adg-internal.h"
37 #include "adg-stroke.h"
38 #include "adg-stroke-private.h"
39 #include "adg-dress-builtins.h"
40 #include "adg-line-style.h"
42 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_stroke_parent_class)
43 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_stroke_parent_class)
52 static void dispose (GObject
*object
);
53 static void get_property (GObject
*object
,
57 static void set_property (GObject
*object
,
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
,
66 static gboolean
set_trail (AdgStroke
*stroke
,
68 static void unset_trail (AdgStroke
*stroke
);
71 G_DEFINE_TYPE(AdgStroke
, adg_stroke
, ADG_TYPE_ENTITY
);
75 adg_stroke_class_init(AdgStrokeClass
*klass
)
77 GObjectClass
*gobject_class
;
78 AdgEntityClass
*entity_class
;
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",
97 P_("The dress to use for stroking this entity"),
98 ADG_DRESS_LINE_MEDIUM
,
100 g_object_class_install_property(gobject_class
, PROP_LINE_DRESS
, param
);
102 param
= g_param_spec_object("trail",
104 P_("The trail to be stroked"),
106 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
107 g_object_class_install_property(gobject_class
, PROP_TRAIL
, param
);
111 adg_stroke_init(AdgStroke
*stroke
)
113 AdgStrokePrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(stroke
,
117 data
->line_dress
= ADG_DRESS_LINE_MEDIUM
;
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
);
135 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
137 AdgStrokePrivate
*data
= ((AdgStroke
*) object
)->data
;
140 case PROP_LINE_DRESS
:
141 g_value_set_int(value
, data
->line_dress
);
144 g_value_set_object(value
, &data
->trail
);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
153 set_property(GObject
*object
, guint prop_id
,
154 const GValue
*value
, GParamSpec
*pspec
)
157 AdgStrokePrivate
*data
;
159 stroke
= (AdgStroke
*) object
;
163 case PROP_LINE_DRESS
:
164 adg_dress_set(&data
->line_dress
, g_value_get_int(value
));
167 set_trail(stroke
, (AdgTrail
*) g_value_get_object(value
));
170 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
178 * @trail: the #AdgTrail to stroke
180 * Creates a new stroke entity.
182 * Returns: the newly created stroke entity
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
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.
207 adg_stroke_set_line_dress(AdgStroke
*stroke
, AdgDress dress
)
209 AdgStrokePrivate
*data
;
211 g_return_if_fail(ADG_IS_STROKE(stroke
));
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
228 adg_stroke_get_line_dress(AdgStroke
*stroke
)
230 AdgStrokePrivate
*data
;
232 g_return_val_if_fail(ADG_IS_STROKE(stroke
), ADG_DRESS_UNDEFINED
);
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.
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
264 adg_stroke_get_trail(AdgStroke
*stroke
)
266 AdgStrokePrivate
*data
;
268 g_return_val_if_fail(ADG_IS_STROKE(stroke
), NULL
);
277 global_changed(AdgEntity
*entity
)
279 if (PARENT_ENTITY_CLASS
->global_changed
)
280 PARENT_ENTITY_CLASS
->global_changed(entity
);
282 adg_entity_invalidate(entity
);
286 local_changed(AdgEntity
*entity
)
288 if (PARENT_ENTITY_CLASS
->local_changed
)
289 PARENT_ENTITY_CLASS
->local_changed(entity
);
291 adg_entity_invalidate(entity
);
295 arrange(AdgEntity
*entity
)
298 AdgStrokePrivate
*data
;
301 /* Check for cached result */
302 if (adg_entity_get_extents(entity
)->is_defined
)
305 stroke
= (AdgStroke
*) entity
;
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
);
316 render(AdgEntity
*entity
, cairo_t
*cr
)
319 AdgStrokePrivate
*data
;
320 const cairo_path_t
*cairo_path
;
322 stroke
= (AdgStroke
*) entity
;
324 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
326 if (cairo_path
!= NULL
) {
328 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
329 cairo_append_path(cr
, cairo_path
);
332 adg_entity_apply_dress(entity
, data
->line_dress
, cr
);
338 set_trail(AdgStroke
*stroke
, AdgTrail
*trail
)
341 AdgStrokePrivate
*data
;
343 entity
= (AdgEntity
*) stroke
;
346 if (trail
== data
->trail
)
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
);
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
);
367 unset_trail(AdgStroke
*stroke
)
369 AdgStrokePrivate
*data
= stroke
->data
;
371 if (data
->trail
!= NULL
)
372 set_trail(stroke
, NULL
);