adg: improved AdgPoint handling
[adg.git] / src / adg / adg-marker.c
blob66829c24f0829eacb82726481e5b1f70e73bd300
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-marker
23 * @short_description: Base entity for markers
25 * A marker is an entity to be applied at the start or end of a segment.
26 * Typical examples include arrows, ticks, dots and so on.
28 * The #AdgMarker:trail and #AdgMarker:n-segment properties specify the
29 * segment where the marker should be applied. Similarly to the
30 * #AdgStroke type, if the associated trail is destroyed the above
31 * properties are unset.
33 * The local map is used internally to align the marker to the trail end,
34 * so adg_entity_set_local_map() and friends is reserved. Therefore, if
35 * the trail is modified and the marker had no way to know it, you should
36 * call adg_entity_local_changed() to update the marker position.
38 * Use adg_marker_set_pos() to select the position where the marker
39 * should be put: %0 means the start point of the segment while %1
40 * means the end point.
42 * The #AdgMarker:model property and APIs are intended only for marker
43 * implementation purposes.
45 * Since: 1.0
46 **/
48 /**
49 * AdgMarker:
51 * All fields are privates and should not be used directly.
52 * Use its public methods instead.
54 * Since: 1.0
55 **/
57 /**
58 * AdgMarkerClass:
59 * @create_model: abstract virtual method that creates a model template for
60 * all the markers used by this class.
62 * The @create_model method must be implemented by any #AdgMarker derived
63 * classes. The derived classes are expected to apply a single model (the one
64 * returned by this method) to every path endings by using different
65 * transformations.
67 * Since: 1.0
68 **/
71 #include "adg-internal.h"
72 #include "adg-model.h"
73 #include "adg-trail.h"
74 #include <string.h>
76 #include "adg-marker.h"
77 #include "adg-marker-private.h"
80 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_marker_parent_class)
81 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_marker_parent_class)
84 G_DEFINE_ABSTRACT_TYPE(AdgMarker, adg_marker, ADG_TYPE_ENTITY)
86 enum {
87 PROP_0,
88 PROP_TRAIL,
89 PROP_N_SEGMENT,
90 PROP_POS,
91 PROP_SIZE,
92 PROP_MODEL
96 static void _adg_dispose (GObject *object);
97 static void _adg_get_property (GObject *object,
98 guint prop_id,
99 GValue *value,
100 GParamSpec *pspec);
101 static void _adg_set_property (GObject *object,
102 guint prop_id,
103 const GValue *value,
104 GParamSpec *pspec);
105 static void _adg_local_changed (AdgEntity *entity);
106 static void _adg_invalidate (AdgEntity *entity);
107 static void _adg_clear_trail (AdgMarker *marker);
108 static gboolean _adg_set_segment (AdgMarker *marker,
109 AdgTrail *trail,
110 guint n_segment);
111 static AdgModel * _adg_create_model (AdgMarker *marker);
114 static void
115 adg_marker_class_init(AdgMarkerClass *klass)
117 GObjectClass *gobject_class;
118 AdgEntityClass *entity_class;
119 GParamSpec *param;
121 gobject_class = (GObjectClass *) klass;
122 entity_class = (AdgEntityClass *) klass;
124 g_type_class_add_private(klass, sizeof(AdgMarkerPrivate));
126 gobject_class->dispose = _adg_dispose;
127 gobject_class->set_property = _adg_set_property;
128 gobject_class->get_property = _adg_get_property;
130 entity_class->local_changed = _adg_local_changed;
131 entity_class->invalidate = _adg_invalidate;
133 klass->create_model = _adg_create_model;
135 param = g_param_spec_object("trail",
136 P_("Trail"),
137 P_("The subject AdgTrail for this marker"),
138 ADG_TYPE_TRAIL,
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
140 g_object_class_install_property(gobject_class, PROP_TRAIL, param);
142 param = g_param_spec_uint("n-segment",
143 P_("Segment Index"),
144 P_("The segment on trail where this marker should be applied (where 0 means undefined segment, 1 the first segment and so on)"),
145 0, G_MAXUINT, 0,
146 G_PARAM_READWRITE);
147 g_object_class_install_property(gobject_class, PROP_N_SEGMENT, param);
149 param = g_param_spec_double("pos",
150 P_("Position"),
151 P_("The position ratio inside the segment where to put the marker (0 means the start point while 1 means the end point)"),
152 0, 1, 0,
153 G_PARAM_READWRITE|G_PARAM_CONSTRUCT);
154 g_object_class_install_property(gobject_class, PROP_POS, param);
156 param = g_param_spec_double("size",
157 P_("Marker Size"),
158 P_("The size (in global space) of the marker"),
159 0, G_MAXDOUBLE, 10,
160 G_PARAM_READWRITE);
161 g_object_class_install_property(gobject_class, PROP_SIZE, param);
163 param = g_param_spec_object("model",
164 P_("Model"),
165 P_("A general purpose model usable by the marker implementations"),
166 ADG_TYPE_MODEL,
167 G_PARAM_READWRITE);
168 g_object_class_install_property(gobject_class, PROP_MODEL, param);
171 static void
172 adg_marker_init(AdgMarker *marker)
174 AdgMarkerPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(marker,
175 ADG_TYPE_MARKER,
176 AdgMarkerPrivate);
177 data->trail = NULL;
178 data->n_segment = 0;
179 data->backup_segment = NULL;
180 memset(&data->segment, 0, sizeof(data->segment));
181 data->pos = 0;
182 data->size = 10;
183 data->model = NULL;
185 marker->data = data;
188 static void
189 _adg_dispose(GObject *object)
191 AdgMarker *marker = (AdgMarker *) object;
193 adg_marker_set_model(marker, NULL);
194 adg_marker_set_segment(marker, NULL, 0);
196 if (_ADG_OLD_OBJECT_CLASS->dispose)
197 _ADG_OLD_OBJECT_CLASS->dispose(object);
201 static void
202 _adg_get_property(GObject *object, guint prop_id,
203 GValue *value, GParamSpec *pspec)
205 AdgMarkerPrivate *data = ((AdgMarker *) object)->data;
207 switch (prop_id) {
208 case PROP_TRAIL:
209 g_value_set_object(value, data->trail);
210 break;
211 case PROP_N_SEGMENT:
212 g_value_set_uint(value, data->n_segment);
213 break;
214 case PROP_POS:
215 g_value_set_double(value, data->pos);
216 break;
217 case PROP_SIZE:
218 g_value_set_double(value, data->size);
219 break;
220 case PROP_MODEL:
221 g_value_set_object(value, data->model);
222 break;
223 default:
224 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
225 break;
229 static void
230 _adg_set_property(GObject *object, guint prop_id,
231 const GValue *value, GParamSpec *pspec)
233 AdgMarker *marker = (AdgMarker *) object;
234 AdgMarkerPrivate *data = ((AdgMarker *) object)->data;
235 AdgModel *old_model;
237 switch (prop_id) {
238 case PROP_TRAIL:
239 _adg_set_segment(marker, g_value_get_object(value), data->n_segment);
240 break;
241 case PROP_N_SEGMENT:
242 _adg_set_segment(marker, data->trail, g_value_get_uint(value));
243 break;
244 case PROP_POS:
245 data->pos = g_value_get_double(value);
246 break;
247 case PROP_SIZE:
248 data->size = g_value_get_double(value);
249 break;
250 case PROP_MODEL:
251 old_model = data->model;
252 data->model = g_value_get_object(value);
254 if (data->model) {
255 g_object_ref((GObject *) data->model);
256 adg_entity_local_changed((AdgEntity *) object);
258 if (old_model)
259 g_object_unref(old_model);
260 break;
261 default:
262 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
263 break;
269 * adg_marker_set_trail:
270 * @marker: an #AdgMarker
271 * @trail: the new trail to use
273 * Sets the #AdgMarker:trail property to @trail. It is allowed to pass
274 * %NULL to clear the current trail.
276 * This method could fail unexpectedly if the segment index specified
277 * by the #AdgMarker:n-segment property is not present inside the new
278 * segment: if you want to set a new segment it is more convenient to
279 * change both properties (#AdgMarker:trail and #AdgMarker:n-segment)
280 * at once with adg_marker_set_segment().
282 * Since: 1.0
284 void
285 adg_marker_set_trail(AdgMarker *marker, AdgTrail *trail)
287 g_return_if_fail(ADG_IS_MARKER(marker));
288 g_object_set(marker, "trail", trail, NULL);
292 * adg_marker_get_trail:
293 * @marker: an #AdgMarker
295 * Gets the trail where this marker should be applied.
296 * The returned object is owned by @marker and should not be
297 * freed or modified.
299 * Returns: (transfer none): the requested trail or %NULL on errors.
301 * Since: 1.0
303 AdgTrail *
304 adg_marker_get_trail(AdgMarker *marker)
306 AdgMarkerPrivate *data;
308 g_return_val_if_fail(ADG_IS_MARKER(marker), NULL);
310 data = marker->data;
312 return data->trail;
316 * adg_marker_set_n_segment:
317 * @marker: an #AdgMarker
318 * @n_segment: the new segment index
320 * Sets the #AdgMarker:n-segment property to @n_segment. The trail
321 * is unchanged. If you want to set both properties at once (as
322 * usually requested to refer to a specific segment),
323 * adg_marker_set_segment() should be more convenient.
325 * Since: 1.0
327 void
328 adg_marker_set_n_segment(AdgMarker *marker, guint n_segment)
330 g_return_if_fail(ADG_IS_MARKER(marker));
331 g_object_set(marker, "n-segment", n_segment, NULL);
335 * adg_marker_get_n_segment:
336 * @marker: an #AdgMarker
338 * Returns the segment of the associated trail where this marker
339 * will be applied, where %1 is the first segment.
341 * Returns: an index greather than %0 on success or %0 on errors
343 * Since: 1.0
345 guint
346 adg_marker_get_n_segment(AdgMarker *marker)
348 AdgMarkerPrivate *data;
350 g_return_val_if_fail(ADG_IS_MARKER(marker), 0);
352 data = marker->data;
354 return data->n_segment;
358 * adg_marker_set_segment:
359 * @marker: an #AdgMarker
360 * @trail: the #AdgTrail
361 * @n_segment: a segment index
363 * Sets a new segment where the marker should be applied at once.
364 * A dependency between @trail and @marker is added, so when @trail
365 * changes @marker is invalidated.
367 * A callback is added to #AdgTrail::remove-dependency so manually
368 * removing the dependency (such as when @trail is destroyed) will
369 * unlink @marker from it.
371 * Since: 1.0
373 void
374 adg_marker_set_segment(AdgMarker *marker, AdgTrail *trail, guint n_segment)
376 g_return_if_fail(ADG_IS_MARKER(marker));
377 /* To avoid referring to an inexistent trail/n-segment couple, the
378 * "n-segment" property is reset before to avoid segment validation */
379 g_object_set(marker, "n-segment", 0,
380 "trail", trail, "n-segment", n_segment, NULL);
384 * adg_marker_get_segment:
385 * @marker: an #AdgMarker
387 * <note><para>
388 * This function is only useful in marker implementations.
389 * </para></note>
391 * Gets the segment where the marker will be applied. This segment
392 * is eventually a modified version of the backup segment, after
393 * having applied the marker.
395 * Returns: the segment or %NULL on errors
397 * Since: 1.0
399 const CpmlSegment *
400 adg_marker_get_segment(AdgMarker *marker)
402 AdgMarkerPrivate *data;
404 g_return_val_if_fail(ADG_IS_MARKER(marker), NULL);
406 data = marker->data;
408 return &data->segment;
412 * adg_marker_backup_segment:
413 * @marker: an #AdgMarker
415 * <note><para>
416 * This function is only useful in marker implementations.
417 * </para></note>
419 * Duplicates the current subject segment for backup purpose: this
420 * segment can be accessed by adg_marker_get_backup_segment().
421 * Obviously, a current segment should exist (either the
422 * #AdgMarker:trail and #AdgMarker:n-segment properties must be
423 * properly defined) or this method will fail without further
424 * processing.
426 * When the subject segment is changed (either by changing
427 * #AdgMarker:trail or #AdgMarker:n-segment) the original segment
428 * is automatically restored.
430 * Since: 1.0
432 void
433 adg_marker_backup_segment(AdgMarker *marker)
435 AdgMarkerPrivate *data;
437 g_return_if_fail(ADG_IS_MARKER(marker));
439 data = marker->data;
441 if (data->n_segment > 0) {
442 g_return_if_fail(data->trail != NULL);
444 g_free(data->backup_segment);
446 /* Backup the segment, if a segment to backup exists */
447 if (adg_trail_put_segment(data->trail, data->n_segment,
448 &data->segment))
449 data->backup_segment = cpml_segment_deep_dup(&data->segment);
454 * adg_marker_get_backup_segment:
455 * @marker: an #AdgMarker
457 * <note><para>
458 * This function is only useful in marker implementations.
459 * </para></note>
461 * Gets the original segment where the marker has been applied.
462 * Applying a marker could modify the underlying trail, usually
463 * by trimming the original segment of a #AdgMarker:size dependent
464 * length from the ends. The marker instance holds a copy of the
465 * original segment, generated by adg_marker_backup_segment(),
466 * to be used in recomputation, for example when the marker
467 * changes its size.
469 * When the subject segment is changed (either by changing
470 * #AdgMarker:trail or #AdgMarker:n-segment) the original segment
471 * is automatically restored.
473 * Returns: the original segment or %NULL on errors
475 * Since: 1.0
477 const CpmlSegment *
478 adg_marker_get_backup_segment(AdgMarker *marker)
480 AdgMarkerPrivate *data;
482 g_return_val_if_fail(ADG_IS_MARKER(marker), NULL);
484 data = marker->data;
486 return data->backup_segment;
490 * adg_marker_set_pos:
491 * @marker: an #AdgMarker
492 * @pos: the new pos
494 * Sets a new position on @marker. Check out adg_marker_get_pos() for
495 * details on what @pos represents.
497 * Since: 1.0
499 void
500 adg_marker_set_pos(AdgMarker *marker, gdouble pos)
502 g_return_if_fail(ADG_IS_MARKER(marker));
503 g_object_set(marker, "pos", pos, NULL);
507 * adg_marker_get_pos:
508 * @marker: an #AdgMarker
510 * Gets the current position of @marker. The returned value is a ratio
511 * position referred to the segment associated to @marker: %0 means the
512 * start point and %1 means the end point of the segment.
514 * Returns: the marker position
516 * Since: 1.0
518 gdouble
519 adg_marker_get_pos(AdgMarker *marker)
521 AdgMarkerPrivate *data;
523 g_return_val_if_fail(ADG_IS_MARKER(marker), 0);
525 data = marker->data;
527 return data->pos;
531 * adg_marker_set_size:
532 * @marker: an #AdgMarker
533 * @size: the new size
535 * Sets a new size on @marker. The @size is an implementation-dependent
536 * property: it has meaning only when used by an #AdgMarker derived type.
538 * Since: 1.0
540 void
541 adg_marker_set_size(AdgMarker *marker, gdouble size)
543 g_return_if_fail(ADG_IS_MARKER(marker));
544 g_object_set(marker, "size", size, NULL);
548 * adg_marker_get_size:
549 * @marker: an #AdgMarker
551 * Gets the current size of @marker.
553 * Returns: the marker size, in global space
555 * Since: 1.0
557 gdouble
558 adg_marker_get_size(AdgMarker *marker)
560 AdgMarkerPrivate *data;
562 g_return_val_if_fail(ADG_IS_MARKER(marker), 0);
564 data = marker->data;
566 return data->size;
570 * adg_marker_set_model:
571 * @marker: an #AdgMarker
572 * @model: a new #AdgModel
574 * <note><para>
575 * This function is only useful in marker implementations.
576 * </para></note>
578 * Sets a new model for @marker. The reference to the old model (if an
579 * old model was present) is dropped while a new reference is added to
580 * @model.
582 * Since: 1.0
584 void
585 adg_marker_set_model(AdgMarker *marker, AdgModel *model)
587 g_return_if_fail(ADG_IS_MARKER(marker));
588 g_object_set(marker, "model", model, NULL);
592 * adg_marker_get_model:
593 * @marker: an #AdgMarker
595 * <note><para>
596 * This function is only useful in marker implementations.
597 * </para></note>
599 * Gets the current model of @marker. This is an accessor method:
600 * if you need to get the model for rendering, use adg_marker_model()
601 * instead. The returned object is owned by @marker and should not be
602 * freed or modified.
604 * Returns: (transfer none): the cached model or %NULL on errors.
606 * Since: 1.0
608 AdgModel *
609 adg_marker_get_model(AdgMarker *marker)
611 AdgMarkerPrivate *data;
613 g_return_val_if_fail(ADG_IS_MARKER(marker), NULL);
615 data = marker->data;
617 return data->model;
621 * adg_marker_model:
622 * @marker: an #AdgMarker
624 * <note><para>
625 * This function is only useful in marker implementations.
626 * </para></note>
628 * Gets the model of @marker. If the model is not found, it is
629 * automatically created by calling the create_model() virtual method.
630 * The returned object is owned by @marker and should not be
631 * freed or modified.
633 * Returns: (transfer none): the current model or %NULL on errors.
635 * Since: 1.0
637 AdgModel *
638 adg_marker_model(AdgMarker *marker)
640 AdgMarkerPrivate *data;
642 g_return_val_if_fail(ADG_IS_MARKER(marker), NULL);
644 data = marker->data;
646 if (data->model == NULL) {
647 /* Model not found: regenerate it */
648 AdgMarkerClass *marker_class = ADG_MARKER_GET_CLASS(marker);
650 if (marker_class->create_model)
651 adg_marker_set_model(marker, marker_class->create_model(marker));
654 return data->model;
658 static void
659 _adg_local_changed(AdgEntity *entity)
661 AdgMarkerPrivate *data;
662 CpmlPair pair;
663 CpmlVector vector;
664 cairo_matrix_t map;
666 data = ((AdgMarker *) entity)->data;
667 if (data->trail == NULL)
668 return;
670 cpml_segment_put_pair_at(&data->segment, data->pos, &pair);
671 cpml_segment_put_vector_at(&data->segment, data->pos, &vector);
672 cpml_vector_set_length(&vector, data->size);
674 if (data->pos > 0.5) {
675 vector.x = -vector.x;
676 vector.y = -vector.y;
679 cairo_matrix_init(&map, vector.x, vector.y,
680 -vector.y, vector.x, pair.x, pair.y);
682 adg_entity_set_local_map(entity, &map);
684 if (_ADG_OLD_ENTITY_CLASS->local_changed)
685 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
688 static void
689 _adg_invalidate(AdgEntity *entity)
691 adg_marker_set_model((AdgMarker *) entity, NULL);
695 static void
696 _adg_clear_trail(AdgMarker *marker)
698 AdgMarkerPrivate *data = marker->data;
700 if (data->trail && data->backup_segment) {
701 /* Restore the original segment in the old trail */
702 cpml_segment_deep_copy(&data->segment, data->backup_segment);
703 g_free(data->backup_segment);
704 data->backup_segment = NULL;
707 data->trail = NULL;
708 data->n_segment = 0;
711 static gboolean
712 _adg_set_segment(AdgMarker *marker, AdgTrail *trail, guint n_segment)
714 AdgMarkerPrivate *data;
715 CpmlSegment segment = { 0 };
717 g_return_val_if_fail(trail == NULL || ADG_IS_TRAIL(trail), FALSE);
719 /* Segment validation, but only if n_segment is specified */
720 if (trail && n_segment > 0 && !adg_trail_put_segment(trail, n_segment, &segment))
721 return FALSE;
723 data = marker->data;
725 /* Do not try to cache results! Although @trail and @n_segment
726 * could be the same, the internal CpmlSegment could change.
727 * This is the case when AdgLDim arranges the layout and changes
728 * the path data (to force outside arrows for example) reusing
729 * the same CpmlPath. In other words, do not do this:
731 * if (trail == data->trail && n_segment == data->n_segment)
732 * return FALSE;
734 * Incidentally, on a 64 bit platform this issue has been never
735 * exposed. Avoiding the cache will solve the issue 33:
737 * http://dev.entidi.com/p/adg/issues/33/
740 if (trail != data->trail) {
741 AdgEntity *entity = (AdgEntity *) marker;
743 if (data->trail)
744 adg_model_remove_dependency((AdgModel *) data->trail, entity);
746 data->trail = trail;
748 if (trail) {
749 adg_model_add_dependency((AdgModel *) trail, entity);
750 g_signal_connect_swapped(trail, "remove-dependency",
751 G_CALLBACK(_adg_clear_trail), marker);
755 cpml_segment_copy(&data->segment, &segment);
756 data->n_segment = n_segment;
758 return TRUE;
761 static AdgModel *
762 _adg_create_model(AdgMarker *marker)
764 g_warning(_("%s: `create_model' method not implemented for type `%s'"),
765 G_STRLOC, g_type_name(G_OBJECT_TYPE(marker)));
766 return NULL;