[AdgADim] Added "has-extension[12]" properties
[adg.git] / src / adg / adg-adim.c
blob753795493018a5af7dc933708b6f688b3e1daf0c
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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-adim
23 * @short_description: Angular dimensions
25 * The #AdgADim entity defines an angular dimension.
28 /**
29 * AdgADim:
31 * All fields are privates and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-internal.h"
37 #include "adg-adim.h"
38 #include "adg-adim-private.h"
39 #include "adg-dim-private.h"
41 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
42 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
45 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM);
47 enum {
48 PROP_0,
49 PROP_VALUE,
50 PROP_ORG1,
51 PROP_ORG2,
52 PROP_HAS_EXTENSION1,
53 PROP_HAS_EXTENSION2
57 static void _adg_dispose (GObject *object);
58 static void _adg_get_property (GObject *object,
59 guint param_id,
60 GValue *value,
61 GParamSpec *pspec);
62 static void _adg_set_property (GObject *object,
63 guint param_id,
64 const GValue *value,
65 GParamSpec *pspec);
66 static void _adg_global_changed (AdgEntity *entity);
67 static void _adg_local_changed (AdgEntity *entity);
68 static void _adg_invalidate (AdgEntity *entity);
69 static void _adg_arrange (AdgEntity *entity);
70 static void _adg_render (AdgEntity *entity,
71 cairo_t *cr);
72 static gchar * _adg_default_value (AdgDim *dim);
73 static gboolean _adg_update_geometry (AdgADim *adim);
74 static void _adg_update_entities (AdgADim *adim);
75 static void _adg_unset_trail (AdgADim *adim);
76 static void _adg_dispose_markers (AdgADim *adim);
77 static gboolean _adg_get_info (AdgADim *adim,
78 CpmlVector vector[],
79 AdgPair *center,
80 gdouble *distance);
81 static CpmlPath * _adg_trail_callback (AdgTrail *trail,
82 gpointer user_data);
85 static void
86 adg_adim_class_init(AdgADimClass *klass)
88 GObjectClass *gobject_class;
89 AdgEntityClass *entity_class;
90 AdgDimClass *dim_class;
91 GParamSpec *param, *old_param;
93 gobject_class = (GObjectClass *) klass;
94 entity_class = (AdgEntityClass *) klass;
95 dim_class = (AdgDimClass *) klass;
97 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
99 gobject_class->dispose = _adg_dispose;
100 gobject_class->get_property = _adg_get_property;
101 gobject_class->set_property = _adg_set_property;
103 entity_class->global_changed = _adg_global_changed;
104 entity_class->local_changed = _adg_local_changed;
105 entity_class->invalidate = _adg_invalidate;
106 entity_class->arrange = _adg_arrange;
107 entity_class->render = _adg_render;
109 dim_class->default_value = _adg_default_value;
111 /* Override #AdgDim:value to append a degree symbol
112 * to the default set value template string */
113 old_param = g_object_class_find_property(gobject_class, "value");
114 param = g_param_spec_string(old_param->name,
115 g_param_spec_get_nick(old_param),
116 g_param_spec_get_blurb(old_param),
117 "<>" ADG_UTF8_DEGREE,
118 old_param->flags);
119 g_object_class_install_property(gobject_class, PROP_VALUE, param);
121 param = g_param_spec_boxed("org1",
122 P_("First Origin"),
123 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
124 ADG_TYPE_POINT,
125 G_PARAM_READWRITE);
126 g_object_class_install_property(gobject_class, PROP_ORG1, param);
128 param = g_param_spec_boxed("org2",
129 P_("Second Origin"),
130 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
131 ADG_TYPE_POINT,
132 G_PARAM_READWRITE);
133 g_object_class_install_property(gobject_class, PROP_ORG2, param);
135 param = g_param_spec_boolean("has-extension1",
136 P_("Has First Extension Line flag"),
137 P_("Show (TRUE) or hide (FALSE) the first extension line"),
138 TRUE, G_PARAM_READWRITE);
139 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION1, param);
141 param = g_param_spec_boolean("has-extension2",
142 P_("Has Second Extension Line flag"),
143 P_("Show (TRUE) or hide (FALSE) the second extension line"),
144 TRUE, G_PARAM_READWRITE);
145 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION2, param);
148 static void
149 adg_adim_init(AdgADim *adim)
151 AdgADimPrivate *data;
152 cairo_path_data_t move_to, line_to, arc_to;
154 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
155 move_to.header.type = CPML_MOVE;
156 move_to.header.length = 2;
157 line_to.header.type = CPML_LINE;
158 line_to.header.length = 2;
159 arc_to.header.type = CPML_ARC;
160 arc_to.header.length = 3;
162 data->org1 = NULL;
163 data->org2 = NULL;
164 data->has_extension1 = TRUE;
165 data->has_extension2 = TRUE;
167 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
168 data->cpml.path.data = data->cpml.data;
169 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
170 data->cpml.path.data[0] = move_to;
171 data->cpml.path.data[2] = arc_to;
172 data->cpml.path.data[5] = move_to;
173 data->cpml.path.data[7] = line_to;
174 data->cpml.path.data[9] = move_to;
175 data->cpml.path.data[11] = line_to;
177 data->trail = NULL;
178 data->marker1 = NULL;
179 data->marker2 = NULL;
181 data->geometry_arranged = FALSE;
183 adim->data = data;
186 static void
187 _adg_dispose(GObject *object)
189 AdgEntity *entity;
190 AdgADimPrivate *data;
192 entity = (AdgEntity *) object;
193 data = ((AdgADim *) object)->data;
195 _adg_dispose_markers((AdgADim *) object);
197 if (data->org1 != NULL)
198 data->org1 = adg_entity_point(entity, data->org1, NULL);
200 if (data->org2 != NULL)
201 data->org2 = adg_entity_point(entity, data->org2, NULL);
203 if (_ADG_OLD_OBJECT_CLASS->dispose)
204 _ADG_OLD_OBJECT_CLASS->dispose(object);
207 static void
208 _adg_get_property(GObject *object, guint prop_id,
209 GValue *value, GParamSpec *pspec)
211 AdgADimPrivate *data = ((AdgADim *) object)->data;
213 switch (prop_id) {
214 case PROP_VALUE:
215 g_value_set_string(value, adg_dim_get_value((AdgDim *) object));
216 break;
217 case PROP_ORG1:
218 g_value_set_boxed(value, data->org1);
219 break;
220 case PROP_ORG2:
221 g_value_set_boxed(value, data->org2);
222 break;
223 case PROP_HAS_EXTENSION1:
224 g_value_set_boolean(value, data->has_extension1);
225 break;
226 case PROP_HAS_EXTENSION2:
227 g_value_set_boolean(value, data->has_extension2);
228 break;
229 default:
230 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
231 break;
235 static void
236 _adg_set_property(GObject *object, guint prop_id,
237 const GValue *value, GParamSpec *pspec)
239 AdgEntity *entity;
240 AdgADimPrivate *data;
242 entity = (AdgEntity *) object;
243 data = ((AdgADim *) object)->data;
245 switch (prop_id) {
246 case PROP_VALUE:
247 adg_dim_set_value((AdgDim *) object, g_value_get_string(value));
248 break;
249 case PROP_ORG1:
250 data->org1 = adg_entity_point(entity, data->org1,
251 g_value_get_boxed(value));
252 break;
253 case PROP_ORG2:
254 data->org2 = adg_entity_point(entity, data->org2,
255 g_value_get_boxed(value));
256 break;
257 case PROP_HAS_EXTENSION1:
258 data->has_extension1 = g_value_get_boolean(value);
259 break;
260 case PROP_HAS_EXTENSION2:
261 data->has_extension2 = g_value_get_boolean(value);
262 break;
263 default:
264 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
265 break;
271 * adg_adim_new:
273 * Creates a new - undefined - angular dimension. You must, at least,
274 * define the first line by setting #AdgADim:org1 (start point) and
275 * #AdgDim:ref1 (end point), the second line by setting #AdgADim:org2
276 * (start point) and #AdgDim:ref2 (end point) and the position of
277 * the quote in #AdgDim:pos.
279 * Returns: the newly created angular dimension entity
281 AdgADim *
282 adg_adim_new(void)
284 return g_object_new(ADG_TYPE_ADIM, NULL);
288 * adg_adim_new_full:
289 * @ref1: first reference point
290 * @ref2: second reference point
291 * @org1: first origin point
292 * @org2: second origin point
294 * Creates a new angular dimension, specifing all the needed
295 * properties in one shot using #AdgPair.
297 * Returns: the newly created angular dimension entity
299 AdgADim *
300 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
301 const AdgPair *org1, const AdgPair *org2,
302 const AdgPair *pos)
304 AdgADim *adim;
305 AdgDim *dim;
307 adim = g_object_new(ADG_TYPE_ADIM, NULL);
308 dim = (AdgDim *) adim;
310 adg_dim_set_ref1_from_pair(dim, ref1);
311 adg_dim_set_ref2_from_pair(dim, ref2);
312 adg_dim_set_pos_from_pair(dim, pos);
313 adg_adim_set_org1_from_pair(adim, org1);
314 adg_adim_set_org2_from_pair(adim, org2);
316 return adim;
320 * adg_adim_new_full_explicit:
321 * @ref1_x: the x coordinate of the first reference point
322 * @ref1_y: the y coordinate of the first reference point
323 * @ref2_x: the x coordinate of the second reference point
324 * @ref2_y: the y coordinate of the second reference point
325 * @direction: angle where to extend the dimension
326 * @pos_x: the x coordinate of the position reference
327 * @pos_y: the y coordinate of the position reference
329 * Wrappes adg_adim_new_full() with explicit values.
331 * Returns: the newly created linear dimension entity
333 AdgADim *
334 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
335 gdouble ref2_x, gdouble ref2_y,
336 gdouble org1_x, gdouble org1_y,
337 gdouble org2_x, gdouble org2_y,
338 gdouble pos_x, gdouble pos_y)
340 AdgPair ref1, ref2, org1, org2, pos;
342 ref1.x = ref1_x;
343 ref1.y = ref1_y;
344 ref2.x = ref2_x;
345 ref2.y = ref2_y;
346 org1.x = org1_x;
347 org1.y = org1_y;
348 org2.x = org2_x;
349 org2.y = org2_y;
350 pos.x = pos_x;
351 pos.y = pos_y;
353 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
357 * adg_adim_new_full_from_model:
358 * @model: the model from which the named pairs are taken
359 * @ref1: the end point of the first line
360 * @ref2: the end point of the second line
361 * @org1: the origin of the first line
362 * @org2: the origin of the second line
363 * @pos: the position reference
365 * Creates a new angular dimension, specifing all the needed properties
366 * in one shot and using named pairs from @model.
368 * Returns: the newly created angular dimension entity
370 AdgADim *
371 adg_adim_new_full_from_model(AdgModel *model,
372 const gchar *ref1, const gchar *ref2,
373 const gchar *org1, const gchar *org2,
374 const gchar *pos)
376 AdgADim *adim;
377 AdgDim *dim;
379 adim = g_object_new(ADG_TYPE_ADIM, NULL);
380 dim = (AdgDim *) adim;
382 adg_dim_set_ref1_from_model(dim, model, ref1);
383 adg_dim_set_ref2_from_model(dim, model, ref2);
384 adg_dim_set_pos_from_model(dim, model, pos);
385 adg_adim_set_org1_from_model(adim, model, org1);
386 adg_adim_set_org2_from_model(adim, model, org2);
388 return adim;
392 * adg_adim_set_org1:
393 * @adim: an #AdgADim
394 * @org1: the new point to use as first reference
396 * Sets the #AdgADim:org1 property to @org1. The old point
397 * is silently discarded, unreferencing its model if that
398 * point was bound to a named pair (hence, possibly destroying
399 * the model if this was the last reference).
401 * @org1 can be %NULL, in which case the point is destroyed.
403 void
404 adg_adim_set_org1(AdgADim *adim, const AdgPoint *org1)
406 g_return_if_fail(ADG_IS_ADIM(adim));
407 g_object_set((GObject *) adim, "org1", org1, NULL);
411 * adg_adim_set_org1_explicit:
412 * @adim: an #AdgADim
413 * @x: x coordinate of the first reference point
414 * @y: y coordinate of the first reference point
416 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
417 * coordinates. The old point is silently discarded,
418 * unreferencing its model if that point was bound to a named
419 * pair (hence, possibly destroying the model if this was the
420 * last reference).
422 void
423 adg_adim_set_org1_explicit(AdgADim *adim, gdouble x, gdouble y)
425 AdgPoint *point = adg_point_new();
427 adg_point_set_pair_explicit(point, x, y);
428 adg_adim_set_org1(adim, point);
430 adg_point_destroy(point);
434 * adg_adim_set_org1_from_pair:
435 * @adim: an #AdgADim
436 * @org1: the coordinates pair of the first reference point
438 * Convenient function to set the #AdgADim:org1 property using a
439 * pair instead of explicit coordinates.
441 void
442 adg_adim_set_org1_from_pair(AdgADim *adim, const AdgPair *org1)
444 g_return_if_fail(org1 != NULL);
446 adg_adim_set_org1_explicit(adim, org1->x, org1->y);
450 * adg_adim_set_org1_from_model:
451 * @adim: an #AdgADim
452 * @model: the source #AdgModel
453 * @org1: a named pair in @model
455 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
456 * is %NULL, the point will be unset. In any case, the old point
457 * is silently discarded, unreferencing its model if that point
458 * was bound to a named pair (hence, possibly destroying the model
459 * if this was the last reference).
461 * The assignment is lazy so @org1 could be not be present in @model.
462 * Anyway, at the first access to this point an error will be raised
463 * if the named pair is still missing.
465 void
466 adg_adim_set_org1_from_model(AdgADim *adim, AdgModel *model, const gchar *org1)
468 AdgPoint *point = adg_point_new();
470 adg_point_set_pair_from_model(point, model, org1);
471 adg_adim_set_org1(adim, point);
473 adg_point_destroy(point);
477 * adg_adim_get_org1:
478 * @adim: an #AdgADim
480 * Gets the #AdgADim:org1 point. The returned point is internally owned
481 * and must not be freed or modified. Anyway, it is not const because
482 * adg_point_get_pair() must be able to modify the internal cache of
483 * the returned point.
485 * Returns: the first reference point
487 AdgPoint *
488 adg_adim_get_org1(AdgADim *adim)
490 AdgADimPrivate *data;
492 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
494 data = adim->data;
496 return data->org1;
500 * adg_adim_set_org2:
501 * @adim: an #AdgADim
502 * @org2: the new point to use as first reference
504 * Sets the #AdgADim:org2 property to @org2. The old point
505 * is silently discarded, unreferencing its model if that
506 * point was bound to a named pair (hence, possibly destroying
507 * the model if this was the last reference).
509 * @org2 can be %NULL, in which case the point is destroyed.
511 void
512 adg_adim_set_org2(AdgADim *adim, const AdgPoint *org2)
514 g_return_if_fail(ADG_IS_ADIM(adim));
515 g_object_set((GObject *) adim, "org2", org2, NULL);
519 * adg_adim_set_org2_explicit:
520 * @adim: an #AdgADim
521 * @x: x coordinate of the first reference point
522 * @y: y coordinate of the first reference point
524 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
525 * coordinates. The old point is silently discarded,
526 * unreferencing its model if that point was bound to a named
527 * pair (hence, possibly destroying the model if this was the
528 * last reference).
530 void
531 adg_adim_set_org2_explicit(AdgADim *adim, gdouble x, gdouble y)
533 AdgPoint *point = adg_point_new();
535 adg_point_set_pair_explicit(point, x, y);
536 adg_adim_set_org2(adim, point);
538 adg_point_destroy(point);
542 * adg_adim_set_org2_from_pair:
543 * @adim: an #AdgADim
544 * @org2: the coordinates pair of the first reference point
546 * Convenient function to set the #AdgADim:org2 property using a
547 * pair instead of explicit coordinates.
549 void
550 adg_adim_set_org2_from_pair(AdgADim *adim, const AdgPair *org2)
552 g_return_if_fail(org2 != NULL);
554 adg_adim_set_org2_explicit(adim, org2->x, org2->y);
558 * adg_adim_set_org2_from_model:
559 * @adim: an #AdgADim
560 * @model: the source #AdgModel
561 * @org2: a named pair in @model
563 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
564 * is %NULL, the point will be unset. In any case, the old point
565 * is silently discarded, unreferencing its model if that point
566 * was bound to a named pair (hence, possibly destroying the model
567 * if this was the last reference).
569 * The assignment is lazy so @org2 could be not be present in @model.
570 * Anyway, at the first access to this point an error will be raised
571 * if the named pair is still missing.
573 void
574 adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2)
576 AdgPoint *point = adg_point_new();
578 adg_point_set_pair_from_model(point, model, org2);
579 adg_adim_set_org2(adim, point);
581 adg_point_destroy(point);
585 * adg_adim_get_org2:
586 * @adim: an #AdgADim
588 * Gets the #AdgADim:org2 point. The returned point is internally owned
589 * and must not be freed or modified. Anyway, it is not const because
590 * adg_point_get_pair() must be able to modify the internal cache of
591 * the returned point.
593 * Returns: the first reference point
595 AdgPoint *
596 adg_adim_get_org2(AdgADim *adim)
598 AdgADimPrivate *data;
600 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
602 data = adim->data;
604 return data->org2;
608 * adg_adim_switch_extension1:
609 * @adim: an #AdgADim entity
610 * @new_state: the new state
612 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
613 * the first extension line of @adim.
615 void
616 adg_adim_switch_extension1(AdgADim *adim, gboolean new_state)
618 g_return_if_fail(ADG_IS_ADIM(adim));
619 g_return_if_fail(adg_is_boolean_value(new_state));
620 g_object_set(adim, "has-extension1", new_state, NULL);
624 * adg_adim_has_extension1:
625 * @adim: an #AdgADim entity
627 * Checks if @adim should render the first extension line.
629 * Returns: %TRUE on first extension line presents, %FALSE otherwise
631 gboolean
632 adg_adim_has_extension1(AdgADim *adim)
634 AdgADimPrivate *data;
636 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
638 data = adim->data;
640 return data->has_extension1;
644 * adg_adim_switch_extension2:
645 * @adim: an #AdgADim entity
646 * @new_state: the new new_state
648 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
649 * the second extension line of @adim.
651 void
652 adg_adim_switch_extension2(AdgADim *adim, gboolean new_state)
654 g_return_if_fail(ADG_IS_ADIM(adim));
655 g_return_if_fail(adg_is_boolean_value(new_state));
656 g_object_set(adim, "has-extension2", new_state, NULL);
660 * adg_adim_has_extension2:
661 * @adim: an #AdgADim entity
663 * Checks if @adim should render the second extension line.
665 * Returns: %TRUE on first extension line presents, %FALSE otherwise
667 gboolean
668 adg_adim_has_extension2(AdgADim *adim)
670 AdgADimPrivate *data;
672 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
674 data = adim->data;
676 return data->has_extension2;
680 static void
681 _adg_global_changed(AdgEntity *entity)
683 AdgADimPrivate *data = ((AdgADim *) entity)->data;
685 _adg_unset_trail((AdgADim *) entity);
687 if (_ADG_OLD_ENTITY_CLASS->global_changed)
688 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
690 if (data->marker1 != NULL)
691 adg_entity_global_changed((AdgEntity *) data->marker1);
693 if (data->marker2 != NULL)
694 adg_entity_global_changed((AdgEntity *) data->marker2);
697 static void
698 _adg_local_changed(AdgEntity *entity)
700 _adg_unset_trail((AdgADim *) entity);
702 if (_ADG_OLD_ENTITY_CLASS->local_changed)
703 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
706 static void
707 _adg_invalidate(AdgEntity *entity)
709 AdgADim *adim;
710 AdgADimPrivate *data;
712 adim = (AdgADim *) entity;
713 data = adim->data;
715 _adg_dispose_markers(adim);
716 data->geometry_arranged = FALSE;
717 _adg_unset_trail(adim);
719 if (data->org1)
720 adg_point_invalidate(data->org1);
721 if (data->org2)
722 adg_point_invalidate(data->org2);
724 if (_ADG_OLD_ENTITY_CLASS->invalidate)
725 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
728 static void
729 _adg_arrange(AdgEntity *entity)
731 AdgADim *adim;
732 AdgDim *dim;
733 AdgADimPrivate *data;
734 AdgAlignment *quote;
735 const AdgMatrix *local;
736 AdgPair ref1, ref2, base1, base12, base2;
737 AdgPair pair;
739 if (_ADG_OLD_ENTITY_CLASS->arrange)
740 _ADG_OLD_ENTITY_CLASS->arrange(entity);
742 adim = (AdgADim *) entity;
744 if (!_adg_update_geometry(adim))
745 return;
747 dim = (AdgDim *) adim;
748 data = adim->data;
749 quote = adg_dim_get_quote(dim);
751 _adg_update_entities(adim);
753 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
754 AdgEntity *quote_entity = (AdgEntity *) quote;
755 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
756 return;
759 local = adg_entity_get_local_matrix(entity);
760 cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim)));
761 cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim)));
762 cpml_pair_copy(&base1, &data->point.base1);
763 cpml_pair_copy(&base12, &data->point.base12);
764 cpml_pair_copy(&base2, &data->point.base2);
766 /* Apply the local matrix to the relevant points */
767 cpml_pair_transform(&ref1, local);
768 cpml_pair_transform(&ref2, local);
769 cpml_pair_transform(&base1, local);
770 cpml_pair_transform(&base12, local);
771 cpml_pair_transform(&base2, local);
773 /* Combine points and global shifts to build the path */
774 pair.x = ref1.x + data->shift.from1.x;
775 pair.y = ref1.y + data->shift.from1.y;
776 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
778 pair.x = base1.x + data->shift.base1.x;
779 pair.y = base1.y + data->shift.base1.y;
780 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
782 pair.x += data->shift.to1.x;
783 pair.y += data->shift.to1.y;
784 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
786 pair.x = base12.x + data->shift.base12.x;
787 pair.y = base12.y + data->shift.base12.y;
788 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
790 pair.x = ref2.x + data->shift.from2.x;
791 pair.y = ref2.y + data->shift.from2.y;
792 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
794 pair.x = base2.x + data->shift.base2.x;
795 pair.y = base2.y + data->shift.base2.y;
796 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
798 pair.x += data->shift.to2.x;
799 pair.y += data->shift.to2.y;
800 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
802 /* Play with header lengths to show or hide the extension lines */
803 if (data->has_extension1) {
804 data->cpml.data[7].header.length = data->has_extension2 ? 2 : 6;
805 } else {
806 data->cpml.data[2].header.length = data->has_extension2 ? 7 : 11;
809 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
811 if (quote != NULL) {
812 /* Update global and local map of the quote */
813 AdgEntity *quote_entity;
814 gdouble angle;
815 AdgMatrix map;
817 quote_entity = (AdgEntity *) quote;
818 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
819 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
821 adg_alignment_set_factor_explicit(quote, 0.5, 0);
823 cairo_matrix_init_translate(&map, pair.x, pair.y);
824 cairo_matrix_rotate(&map, angle);
825 adg_entity_set_global_map(quote_entity, &map);
827 adg_matrix_copy(&data->quote.global_map,
828 adg_entity_get_global_map(quote_entity));
831 /* Signal to the markers (if any) that the path has changed */
832 if (data->marker1 != NULL) {
833 adg_marker_set_segment(data->marker1, data->trail, 1);
834 adg_entity_local_changed((AdgEntity *) data->marker1);
837 if (data->marker2 != NULL) {
838 adg_marker_set_segment(data->marker2, data->trail, 1);
839 adg_entity_local_changed((AdgEntity *) data->marker2);
842 /* TODO: compute the extents */
845 static void
846 _adg_render(AdgEntity *entity, cairo_t *cr)
848 AdgADim *adim;
849 AdgDim *dim;
850 AdgADimPrivate *data;
851 AdgDimStyle *dim_style;
852 AdgDress dress;
853 const cairo_path_t *cairo_path;
855 adim = (AdgADim *) entity;
856 data = adim->data;
858 if (!data->geometry_arranged) {
859 /* Entity not arranged, probably due to undefined pair found */
860 return;
863 dim = (AdgDim *) entity;
864 dim_style = _ADG_GET_DIM_STYLE(dim);
866 adg_style_apply((AdgStyle *) dim_style, entity, cr);
867 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
869 if (data->marker1 != NULL)
870 adg_entity_render((AdgEntity *) data->marker1, cr);
871 if (data->marker2 != NULL)
872 adg_entity_render((AdgEntity *) data->marker2, cr);
874 cairo_transform(cr, adg_entity_get_global_matrix(entity));
875 dress = adg_dim_style_get_line_dress(dim_style);
876 adg_entity_apply_dress(entity, dress, cr);
878 cairo_path = adg_trail_get_cairo_path(data->trail);
879 cairo_append_path(cr, cairo_path);
880 cairo_stroke(cr);
883 static gchar *
884 _adg_default_value(AdgDim *dim)
886 AdgADim *adim;
887 AdgADimPrivate *data;
888 AdgDimStyle *dim_style;
889 gdouble angle;
890 const gchar *format;
892 adim = (AdgADim *) dim;
893 data = adim->data;
894 dim_style = _ADG_GET_DIM_STYLE(dim);
895 format = adg_dim_style_get_number_format(dim_style);
897 if (!_adg_update_geometry(adim))
898 return g_strdup("undef");
900 angle = (data->angle2 - data->angle1) * 180 / M_PI;
901 return g_strdup_printf(format, angle);
904 /* With "geometry" is considered any data (point, vector or angle)
905 * that can be cached: this is strictly related on how the arrange()
906 * method works */
907 static gboolean
908 _adg_update_geometry(AdgADim *adim)
910 AdgADimPrivate *data;
911 AdgDimStyle *dim_style;
912 gdouble from_offset, to_offset;
913 gdouble spacing, level;
914 CpmlVector vector[3];
915 CpmlPair center;
916 gdouble distance;
918 data = adim->data;
920 if (data->geometry_arranged)
921 return TRUE;
922 else if (!_adg_get_info(adim, vector, &center, &distance))
923 return FALSE;
925 dim_style = _ADG_GET_DIM_STYLE(adim);
926 from_offset = adg_dim_style_get_from_offset(dim_style);
927 to_offset = adg_dim_style_get_to_offset(dim_style);
928 spacing = adg_dim_style_get_baseline_spacing(dim_style);
929 level = adg_dim_get_level((AdgDim *) adim);
931 /* shift.from1 */
932 cpml_vector_set_length(&vector[0], from_offset);
933 cpml_pair_copy(&data->shift.from1, &vector[0]);
935 /* shift.base1 */
936 cpml_vector_set_length(&vector[0], level * spacing);
937 cpml_pair_copy(&data->shift.base1, &vector[0]);
939 /* shift.to1 */
940 cpml_vector_set_length(&vector[0], to_offset);
941 cpml_pair_copy(&data->shift.to1, &vector[0]);
943 /* shift.from2 */
944 cpml_vector_set_length(&vector[2], from_offset);
945 cpml_pair_copy(&data->shift.from2, &vector[2]);
947 /* shift.base2 */
948 cpml_vector_set_length(&vector[2], level * spacing);
949 cpml_pair_copy(&data->shift.base2, &vector[2]);
951 /* shift.to2 */
952 cpml_vector_set_length(&vector[2], to_offset);
953 cpml_pair_copy(&data->shift.to2, &vector[2]);
955 /* shift.base12 */
956 cpml_vector_set_length(&vector[1], level * spacing);
957 cpml_pair_copy(&data->shift.base12, &vector[1]);
959 /* Distance can be 0, so the following will leave the
960 * vector array in undefined state */
962 /* point.base1 */
963 cpml_vector_set_length(&vector[0], distance);
964 data->point.base1.x = vector[0].x + center.x;
965 data->point.base1.y = vector[0].y + center.y;
967 /* point.base2 */
968 cpml_vector_set_length(&vector[2], distance);
969 data->point.base2.x = vector[2].x + center.x;
970 data->point.base2.y = vector[2].y + center.y;
972 /* point.base12 */
973 cpml_vector_set_length(&vector[1], distance);
974 data->point.base12.x = vector[1].x + center.x;
975 data->point.base12.y = vector[1].y + center.y;
977 data->geometry_arranged = TRUE;
979 return TRUE;
982 static void
983 _adg_update_entities(AdgADim *adim)
985 AdgEntity *entity;
986 AdgADimPrivate *data;
987 AdgDimStyle *dim_style;
989 entity = (AdgEntity *) adim;
990 data = adim->data;
991 dim_style = _ADG_GET_DIM_STYLE(adim);
993 if (data->trail == NULL)
994 data->trail = adg_trail_new(_adg_trail_callback, adim);
996 if (data->marker1 == NULL) {
997 data->marker1 = adg_dim_style_marker1_new(dim_style);
998 adg_entity_set_parent((AdgEntity *) data->marker1, entity);
1001 if (data->marker2 == NULL) {
1002 data->marker2 = adg_dim_style_marker2_new(dim_style);
1003 adg_entity_set_parent((AdgEntity *) data->marker2, entity);
1007 static void
1008 _adg_unset_trail(AdgADim *adim)
1010 AdgADimPrivate *data = adim->data;
1012 if (data->trail != NULL)
1013 adg_model_clear((AdgModel *) data->trail);
1015 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
1018 static void
1019 _adg_dispose_markers(AdgADim *adim)
1021 AdgADimPrivate *data = adim->data;
1023 if (data->trail != NULL) {
1024 g_object_unref(data->trail);
1025 data->trail = NULL;
1028 if (data->marker1 != NULL) {
1029 g_object_unref(data->marker1);
1030 data->marker1 = NULL;
1033 if (data->marker2 != NULL) {
1034 g_object_unref(data->marker2);
1035 data->marker2 = NULL;
1039 static gboolean
1040 _adg_get_info(AdgADim *adim, CpmlVector vector[],
1041 AdgPair *center, gdouble *distance)
1043 AdgDim *dim;
1044 AdgADimPrivate *data;
1045 const AdgPair *ref1, *ref2, *pos;
1046 const AdgPair *org1, *org2;
1047 gdouble factor;
1049 dim = (AdgDim *) adim;
1050 data = adim->data;
1051 ref1 = adg_point_get_pair(adg_dim_get_ref1(dim));
1052 ref2 = adg_point_get_pair(adg_dim_get_ref2(dim));
1053 pos = adg_point_get_pair(adg_dim_get_pos(dim));
1054 org1 = adg_point_get_pair(data->org1);
1055 org2 = adg_point_get_pair(data->org2);
1057 if (ref1 == NULL || ref2 == NULL || pos == NULL ||
1058 org1 == NULL || org2 == NULL)
1059 return FALSE;
1061 vector[0].x = ref1->x - org1->x;
1062 vector[0].y = ref1->y - org1->y;
1063 vector[2].x = ref2->x - org2->x;
1064 vector[2].y = ref2->y - org2->y;
1066 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
1067 if (factor == 0) {
1068 /* Parallel lines: hang with an error message */
1069 g_warning("%s: trying to set an angular dimension on parallel lines",
1070 G_STRLOC);
1071 return FALSE;
1074 factor = ((ref1->y - ref2->y) * vector[2].x -
1075 (ref1->x - ref2->x) * vector[2].y) / factor;
1077 center->x = ref1->x + vector[0].x * factor;
1078 center->y = ref1->y + vector[0].y * factor;
1079 *distance = cpml_pair_distance(center, pos);
1080 data->angle1 = cpml_vector_angle(&vector[0]);
1081 data->angle2 = cpml_vector_angle(&vector[2]);
1082 while (data->angle2 < data->angle1)
1083 data->angle2 += M_PI * 2;
1085 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
1087 return TRUE;
1090 static CpmlPath *
1091 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
1093 AdgADim *adim;
1094 AdgADimPrivate *data;
1096 adim = (AdgADim *) user_data;
1097 data = adim->data;
1099 return &data->cpml.path;