[build] Make LT_INIT macro optional
[adg.git] / adg / adg-adim.c
blobac107ae961432648d7a61969518b8e217bd4618d
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-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 PARENT_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
42 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
45 enum {
46 PROP_0,
47 PROP_ORG1,
48 PROP_ORG2,
49 PROP_HAS_EXTENSION1,
50 PROP_HAS_EXTENSION2
54 static void dispose (GObject *object);
55 static void get_property (GObject *object,
56 guint param_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint param_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void local_changed (AdgEntity *entity);
64 static void invalidate (AdgEntity *entity);
65 static void arrange (AdgEntity *entity);
66 static void render (AdgEntity *entity,
67 cairo_t *cr);
68 static gchar * default_value (AdgDim *dim);
69 static void update_geometry (AdgADim *adim);
70 static void update_entities (AdgADim *adim);
71 static void unset_trail (AdgADim *adim);
72 static void dispose_markers (AdgADim *adim);
73 static gboolean get_info (AdgADim *adim,
74 CpmlVector vector[],
75 AdgPair *center,
76 gdouble *distance);
77 static CpmlPath * trail_callback (AdgTrail *trail,
78 gpointer user_data);
81 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM);
84 static void
85 adg_adim_class_init(AdgADimClass *klass)
87 GObjectClass *gobject_class;
88 AdgEntityClass *entity_class;
89 AdgDimClass *dim_class;
90 GParamSpec *param;
92 gobject_class = (GObjectClass *) klass;
93 entity_class = (AdgEntityClass *) klass;
94 dim_class = (AdgDimClass *) klass;
96 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
98 gobject_class->dispose = dispose;
99 gobject_class->get_property = get_property;
100 gobject_class->set_property = set_property;
102 entity_class->local_changed = local_changed;
103 entity_class->invalidate = invalidate;
104 entity_class->arrange = arrange;
105 entity_class->render = render;
107 dim_class->default_value = default_value;
109 param = g_param_spec_boxed("org1",
110 P_("First Origin"),
111 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
112 ADG_TYPE_POINT,
113 G_PARAM_READWRITE);
114 g_object_class_install_property(gobject_class, PROP_ORG1, param);
116 param = g_param_spec_boxed("org2",
117 P_("Second Origin"),
118 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
119 ADG_TYPE_POINT,
120 G_PARAM_READWRITE);
121 g_object_class_install_property(gobject_class, PROP_ORG2, param);
124 static void
125 adg_adim_init(AdgADim *adim)
127 AdgADimPrivate *data;
128 cairo_path_data_t move_to, line_to, arc_to;
130 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
131 move_to.header.type = CPML_MOVE;
132 move_to.header.length = 2;
133 line_to.header.type = CPML_LINE;
134 line_to.header.length = 2;
135 arc_to.header.type = CPML_ARC;
136 arc_to.header.length = 3;
138 data->org1 = NULL;
139 data->org2 = NULL;
140 data->has_extension1 = TRUE;
141 data->has_extension2 = TRUE;
143 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
144 data->cpml.path.data = data->cpml.data;
145 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
146 data->cpml.path.data[0] = move_to;
147 data->cpml.path.data[2] = arc_to;
148 data->cpml.path.data[5] = move_to;
149 data->cpml.path.data[7] = line_to;
150 data->cpml.path.data[9] = move_to;
151 data->cpml.path.data[11] = line_to;
153 data->trail = NULL;
154 data->marker1 = NULL;
155 data->marker2 = NULL;
157 data->geometry_arranged = FALSE;
159 adim->data = data;
162 static void
163 dispose(GObject *object)
165 AdgADim *adim;
166 AdgADimPrivate *data;
168 adim = (AdgADim *) object;
169 data = adim->data;
171 dispose_markers((AdgADim *) object);
173 if (data->org1 != NULL) {
174 adg_point_destroy(data->org1);
175 data->org1 = NULL;
177 if (data->org2 != NULL) {
178 adg_point_destroy(data->org2);
179 data->org2 = NULL;
182 if (PARENT_OBJECT_CLASS->dispose)
183 PARENT_OBJECT_CLASS->dispose(object);
186 static void
187 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
189 AdgADimPrivate *data = ((AdgADim *) object)->data;
191 switch (prop_id) {
192 case PROP_ORG1:
193 g_value_set_boxed(value, &data->org1);
194 break;
195 case PROP_ORG2:
196 g_value_set_boxed(value, &data->org2);
197 break;
198 case PROP_HAS_EXTENSION1:
199 g_value_set_boolean(value, data->has_extension1);
200 break;
201 case PROP_HAS_EXTENSION2:
202 g_value_set_boolean(value, data->has_extension2);
203 break;
204 default:
205 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
206 break;
210 static void
211 set_property(GObject *object, guint prop_id,
212 const GValue *value, GParamSpec *pspec)
214 AdgADim *adim;
215 AdgADimPrivate *data;
217 adim = (AdgADim *) object;
218 data = adim->data;
220 switch (prop_id) {
221 case PROP_ORG1:
222 if (data->org1 != NULL)
223 adg_point_destroy(data->org1);
224 data->org1 = g_value_dup_boxed(value);
225 break;
226 case PROP_ORG2:
227 if (data->org2 != NULL)
228 adg_point_destroy(data->org2);
229 data->org2 = g_value_dup_boxed(value);
230 break;
231 case PROP_HAS_EXTENSION1:
232 data->has_extension1 = g_value_get_boolean(value);
233 break;
234 case PROP_HAS_EXTENSION2:
235 data->has_extension2 = g_value_get_boolean(value);
236 break;
237 default:
238 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
239 break;
245 * adg_adim_new:
247 * Creates a new - undefined - angular dimension. You must, at least,
248 * define the reference points with adg_dim_set_ref(), the origins of
249 * the lines ending with the reference points with adg_adim_set_org()
250 * and the reference for positioning the quote with adg_dim_set_pos().
252 * Returns: the newly created angular dimension entity
254 AdgADim *
255 adg_adim_new(void)
257 return g_object_new(ADG_TYPE_ADIM, NULL);
261 * adg_adim_new_full:
262 * @ref1: first reference point
263 * @ref2: second reference point
264 * @org1: first origin point
265 * @org2: second origin point
267 * Creates a new angular dimension, specifing all the needed
268 * properties in one shot.
270 * Returns: the newly created angular dimension entity
272 AdgADim *
273 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
274 const AdgPair *org1, const AdgPair *org2,
275 const AdgPair *pos)
277 AdgADim *adim;
278 AdgDim *dim;
280 adim = g_object_new(ADG_TYPE_ADIM, NULL);
281 dim = (AdgDim *) adim;
283 adg_dim_set_ref(dim, ref1, ref2);
284 adg_adim_set_org(adim, org1, org2);
285 adg_dim_set_pos(dim, pos);
287 return adim;
291 * adg_adim_new_full_explicit:
292 * @ref1_x: the x coordinate of the first reference point
293 * @ref1_y: the y coordinate of the first reference point
294 * @ref2_x: the x coordinate of the second reference point
295 * @ref2_y: the y coordinate of the second reference point
296 * @direction: angle where to extend the dimension
297 * @pos_x: the x coordinate of the position reference
298 * @pos_y: the y coordinate of the position reference
300 * Wrappes adg_adim_new_full() with explicit values.
302 * Returns: the newly created linear dimension entity
304 AdgADim *
305 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
306 gdouble ref2_x, gdouble ref2_y,
307 gdouble org1_x, gdouble org1_y,
308 gdouble org2_x, gdouble org2_y,
309 gdouble pos_x, gdouble pos_y)
311 AdgPair ref1, ref2, org1, org2, pos;
313 ref1.x = ref1_x;
314 ref1.y = ref1_y;
315 ref2.x = ref2_x;
316 ref2.y = ref2_y;
317 org1.x = org1_x;
318 org1.y = org1_y;
319 org2.x = org2_x;
320 org2.y = org2_y;
321 pos.x = pos_x;
322 pos.y = pos_y;
324 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
328 * adg_adim_new_full_from_model:
329 * @model: the model from which the named pairs are taken
330 * @ref1: the end point of the first line
331 * @ref2: the end point of the second line
332 * @org1: the origin of the first line
333 * @org2: the origin of the second line
334 * @pos: the position reference
336 * Creates a new angular dimension, specifing all the needed properties
337 * in one shot and using named pairs from @model.
339 * Returns: the newly created angular dimension entity
341 AdgADim *
342 adg_adim_new_full_from_model(AdgModel *model,
343 const gchar *ref1, const gchar *ref2,
344 const gchar *org1, const gchar *org2,
345 const gchar *pos)
347 AdgADim *adim;
348 AdgDim *dim;
350 adim = g_object_new(ADG_TYPE_ADIM, NULL);
351 dim = (AdgDim *) adim;
353 adg_dim_set_ref_from_model(dim, model, ref1, ref2);
354 adg_dim_set_pos_from_model(dim, model, pos);
355 adg_adim_set_org_from_model(adim, model, org1, org2);
357 return adim;
361 * adg_adim_set_org:
362 * @adim: an #AdgADim
363 * @org1: the first origin
364 * @org2: the second origin
366 * Sets at once the two origins on @adim. One of @org1 or @org2
367 * (but not both) could be %NULL, in which case only the non-null
368 * origin is set.
370 void
371 adg_adim_set_org(AdgADim *adim, const AdgPair *org1, const AdgPair *org2)
373 GObject *object;
374 AdgADimPrivate *data;
376 g_return_if_fail(ADG_IS_ADIM(adim));
377 g_return_if_fail(org1 != NULL || org2 != NULL);
379 object = (GObject *) adim;
380 data = adim->data;
382 g_object_freeze_notify(object);
384 if (org1 != NULL) {
385 if (data->org1 == NULL)
386 data->org1 = adg_point_new();
388 adg_point_set_pair(data->org1, org1);
390 g_object_notify(object, "org1");
393 if (org2 != NULL) {
394 if (data->org2 == NULL)
395 data->org2 = adg_point_new();
397 adg_point_set_pair(data->org2, org2);
399 g_object_notify(object, "org2");
402 g_object_thaw_notify(object);
406 * adg_adim_set_org_explicit:
407 * @adim: an #AdgADim
408 * @org1_x: x coordinate of org1
409 * @org1_y: y coordinate of org1
410 * @org2_x: x coordinate of org2
411 * @org2_y: y coordinate of org2
413 * Works in the same way as adg_adim_set_org() but using
414 * explicit coordinates instead of #AdgPair args. The
415 * notable difference is that, by using gdouble values,
416 * you can't set only a single origin point.
418 void
419 adg_adim_set_org_explicit(AdgADim *adim,
420 gdouble org1_x, gdouble org1_y,
421 gdouble org2_x, gdouble org2_y)
423 AdgPair org1, org2;
425 org1.x = org1_x;
426 org1.y = org1_y;
427 org2.x = org2_x;
428 org2.y = org2_y;
430 adg_adim_set_org(adim, &org1, &org2);
434 * adg_adim_set_org_from_model:
435 * @adim: an #AdgADim
436 * @model: the source #AdgModel
437 * @org1: name of the pair in @model to use as org1
438 * @org2: name of the pair in @model to use as org2
440 * Sets #AdgADim:org1 and #AdgADim:org2 properties by linking
441 * them to the @org1 and @org2 named pairs in @model. @org1
442 * or @org2 could be %NULL (but not both), in which case
443 * only the non-null origin point is changed.
445 * Using this function twice you can also link the origin
446 * points to named pairs taken from different models:
448 * |[
449 * adg_adim_set_org_from_model(adim, model1, org1, NULL);
450 * adg_adim_set_org_from_model(adim, model2, NULL, org2);
451 * ]|
453 void
454 adg_adim_set_org_from_model(AdgADim *adim, AdgModel *model,
455 const gchar *org1, const gchar *org2)
457 GObject *object;
458 AdgADimPrivate *data;
460 g_return_if_fail(ADG_IS_ADIM(adim));
461 g_return_if_fail(ADG_IS_MODEL(model));
462 g_return_if_fail(org1 != NULL || org2 != NULL);
464 object = (GObject *) adim;
465 data = adim->data;
467 g_object_freeze_notify(object);
469 if (org1 != NULL) {
470 if (data->org1 == NULL)
471 data->org1 = adg_point_new();
473 adg_point_set_pair_from_model(data->org1, model, org1);
475 g_object_notify(object, "org1");
478 if (org2 != NULL) {
479 if (data->org2 == NULL)
480 data->org2 = adg_point_new();
482 adg_point_set_pair_from_model(data->org2, model, org2);
484 g_object_notify(object, "org2");
487 g_object_thaw_notify(object);
491 * adg_adim_get_org1:
492 * @adim: an #AdgADim
494 * Gets the first origin of @adim. The returned pair is owned by
495 * @adim and should not be modified or freed.
497 * Returns: a pointer to the internal #AdgPair or %NULL on errors
499 const AdgPair *
500 adg_adim_get_org1(AdgADim *adim)
502 AdgADimPrivate *data;
504 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
506 data = adim->data;
508 return adg_point_get_pair(data->org1);
512 * adg_adim_get_org2:
513 * @adim: an #AdgADim
515 * Gets the second origin of @adim. The returned pair is owned by
516 * @adim and should not be modified or freed.
518 * Returns: a pointer to the internal #AdgPair or %NULL on errors
520 const AdgPair *
521 adg_adim_get_org2(AdgADim *adim)
523 AdgADimPrivate *data;
525 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
527 data = adim->data;
529 return adg_point_get_pair(data->org2);
533 static void
534 local_changed(AdgEntity *entity)
536 unset_trail((AdgADim *) entity);
538 if (PARENT_ENTITY_CLASS->local_changed)
539 PARENT_ENTITY_CLASS->local_changed(entity);
542 static void
543 invalidate(AdgEntity *entity)
545 AdgADim *adim;
546 AdgADimPrivate *data;
548 adim = (AdgADim *) entity;
549 data = adim->data;
551 dispose_markers(adim);
552 data->geometry_arranged = FALSE;
553 unset_trail(adim);
555 adg_point_invalidate(data->org1);
556 adg_point_invalidate(data->org2);
558 if (PARENT_ENTITY_CLASS->invalidate)
559 PARENT_ENTITY_CLASS->invalidate(entity);
562 static void
563 arrange(AdgEntity *entity)
565 AdgADim *adim;
566 AdgDim *dim;
567 AdgADimPrivate *data;
568 AdgAlignment *quote;
569 const AdgMatrix *local;
570 AdgPair ref1, ref2, base1, base12, base2;
571 AdgPair pair;
573 if (PARENT_ENTITY_CLASS->arrange)
574 PARENT_ENTITY_CLASS->arrange(entity);
576 adim = (AdgADim *) entity;
577 dim = (AdgDim *) adim;
578 data = adim->data;
579 quote = adg_dim_get_quote(dim);
581 update_geometry(adim);
582 update_entities(adim);
584 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
585 AdgEntity *quote_entity = (AdgEntity *) quote;
586 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
587 return;
590 local = adg_entity_get_local_matrix(entity);
591 cpml_pair_copy(&ref1, adg_dim_get_ref1(dim));
592 cpml_pair_copy(&ref2, adg_dim_get_ref2(dim));
593 cpml_pair_copy(&base1, &data->point.base1);
594 cpml_pair_copy(&base12, &data->point.base12);
595 cpml_pair_copy(&base2, &data->point.base2);
597 /* Apply the local matrix to the relevant points */
598 cpml_pair_transform(&ref1, local);
599 cpml_pair_transform(&ref2, local);
600 cpml_pair_transform(&base1, local);
601 cpml_pair_transform(&base12, local);
602 cpml_pair_transform(&base2, local);
604 /* Combine points and global shifts to build the path */
605 pair.x = ref1.x + data->shift.from1.x;
606 pair.y = ref1.y + data->shift.from1.y;
607 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
609 pair.x = base1.x + data->shift.base1.x;
610 pair.y = base1.y + data->shift.base1.y;
611 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
613 pair.x += data->shift.to1.x;
614 pair.y += data->shift.to1.y;
615 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
617 pair.x = base12.x + data->shift.base12.x;
618 pair.y = base12.y + data->shift.base12.y;
619 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
621 pair.x = ref2.x + data->shift.from2.x;
622 pair.y = ref2.y + data->shift.from2.y;
623 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
625 pair.x = base2.x + data->shift.base2.x;
626 pair.y = base2.y + data->shift.base2.y;
627 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
629 pair.x += data->shift.to2.x;
630 pair.y += data->shift.to2.y;
631 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
633 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
635 if (quote != NULL) {
636 /* Update global and local map of the quote */
637 AdgEntity *quote_entity;
638 gdouble angle;
639 AdgMatrix map;
641 quote_entity = (AdgEntity *) quote;
642 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
643 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
645 adg_alignment_set_factor_explicit(quote, 0.5, 0);
647 cairo_matrix_init_translate(&map, pair.x, pair.y);
648 cairo_matrix_rotate(&map, angle);
649 adg_entity_set_global_map(quote_entity, &map);
651 adg_matrix_copy(&data->quote.global_map,
652 adg_entity_get_global_map(quote_entity));
655 /* Signal to the markers (if any) that the path has changed */
656 if (data->marker1 != NULL) {
657 adg_marker_set_segment(data->marker1, data->trail, 1);
658 adg_entity_local_changed((AdgEntity *) data->marker1);
661 if (data->marker2 != NULL) {
662 adg_marker_set_segment(data->marker2, data->trail, 1);
663 adg_entity_local_changed((AdgEntity *) data->marker2);
666 /* TODO: compute the extents */
669 static void
670 render(AdgEntity *entity, cairo_t *cr)
672 AdgADim *adim;
673 AdgDim *dim;
674 AdgADimPrivate *data;
675 AdgDimStyle *dim_style;
676 AdgDress dress;
677 const cairo_path_t *cairo_path;
679 adim = (AdgADim *) entity;
680 dim = (AdgDim *) entity;
681 data = adim->data;
682 dim_style = GET_DIM_STYLE(dim);
684 adg_style_apply((AdgStyle *) dim_style, entity, cr);
686 if (data->marker1 != NULL)
687 adg_entity_render((AdgEntity *) data->marker1, cr);
689 if (data->marker2 != NULL)
690 adg_entity_render((AdgEntity *) data->marker2, cr);
692 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
694 dress = adg_dim_style_get_line_dress(dim_style);
695 adg_entity_apply_dress(entity, dress, cr);
697 cairo_path = adg_trail_get_cairo_path(data->trail);
698 cairo_append_path(cr, cairo_path);
699 cairo_stroke(cr);
702 static gchar *
703 default_value(AdgDim *dim)
705 AdgADim *adim;
706 AdgADimPrivate *data;
707 AdgDimStyle *dim_style;
708 gdouble angle;
709 const gchar *format;
711 adim = (AdgADim *) dim;
712 data = adim->data;
713 dim_style = GET_DIM_STYLE(dim);
714 format = adg_dim_style_get_number_format(dim_style);
716 update_geometry(adim);
717 angle = (data->angle2 - data->angle1) * 180 / M_PI;
719 return g_strdup_printf(format, angle);
722 /* With "geometry" is considered any data (point, vector or angle)
723 * that can be cached: this is strictly related on how the arrange()
724 * method works */
725 static void
726 update_geometry(AdgADim *adim)
728 AdgADimPrivate *data;
729 AdgDimStyle *dim_style;
730 gdouble from_offset, to_offset;
731 gdouble spacing, level;
732 CpmlVector vector[3];
733 CpmlPair center;
734 gdouble distance;
736 data = adim->data;
738 if (data->geometry_arranged)
739 return;
741 if (!get_info(adim, vector, &center, &distance)) {
742 /* Parallel lines: hang with an error message */
743 g_warning("%s: trying to set an angular dimension on parallel lines",
744 G_STRLOC);
745 return;
748 dim_style = GET_DIM_STYLE(adim);
749 from_offset = adg_dim_style_get_from_offset(dim_style);
750 to_offset = adg_dim_style_get_to_offset(dim_style);
751 spacing = adg_dim_style_get_baseline_spacing(dim_style);
752 level = adg_dim_get_level((AdgDim *) adim);
754 /* shift.from1 */
755 cpml_vector_set_length(&vector[0], from_offset);
756 cpml_pair_copy(&data->shift.from1, &vector[0]);
758 /* shift.base1 */
759 cpml_vector_set_length(&vector[0], level * spacing);
760 cpml_pair_copy(&data->shift.base1, &vector[0]);
762 /* shift.to1 */
763 cpml_vector_set_length(&vector[0], to_offset);
764 cpml_pair_copy(&data->shift.to1, &vector[0]);
766 /* shift.from2 */
767 cpml_vector_set_length(&vector[2], from_offset);
768 cpml_pair_copy(&data->shift.from2, &vector[2]);
770 /* shift.base2 */
771 cpml_vector_set_length(&vector[2], level * spacing);
772 cpml_pair_copy(&data->shift.base2, &vector[2]);
774 /* shift.to2 */
775 cpml_vector_set_length(&vector[2], to_offset);
776 cpml_pair_copy(&data->shift.to2, &vector[2]);
778 /* shift.base12 */
779 cpml_vector_set_length(&vector[1], level * spacing);
780 cpml_pair_copy(&data->shift.base12, &vector[1]);
782 /* Distance can be 0, so the following will leave the
783 * vector array in undefined state */
785 /* point.base1 */
786 cpml_vector_set_length(&vector[0], distance);
787 data->point.base1.x = vector[0].x + center.x;
788 data->point.base1.y = vector[0].y + center.y;
790 /* point.base2 */
791 cpml_vector_set_length(&vector[2], distance);
792 data->point.base2.x = vector[2].x + center.x;
793 data->point.base2.y = vector[2].y + center.y;
795 /* point.base12 */
796 cpml_vector_set_length(&vector[1], distance);
797 data->point.base12.x = vector[1].x + center.x;
798 data->point.base12.y = vector[1].y + center.y;
800 data->geometry_arranged = TRUE;
803 static void
804 update_entities(AdgADim *adim)
806 AdgADimPrivate *data;
807 AdgDimStyle *dim_style;
809 data = adim->data;
810 dim_style = GET_DIM_STYLE(adim);
812 if (data->trail == NULL)
813 data->trail = adg_trail_new(trail_callback, adim);
815 if (data->marker1 == NULL)
816 data->marker1 = adg_dim_style_marker1_new(dim_style);
818 if (data->marker2 == NULL)
819 data->marker2 = adg_dim_style_marker2_new(dim_style);
822 static void
823 unset_trail(AdgADim *adim)
825 AdgADimPrivate *data = adim->data;
827 if (data->trail != NULL)
828 adg_model_clear((AdgModel *) data->trail);
830 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
833 static void
834 dispose_markers(AdgADim *adim)
836 AdgADimPrivate *data = adim->data;
838 if (data->trail != NULL) {
839 g_object_unref(data->trail);
840 data->trail = NULL;
843 if (data->marker1 != NULL) {
844 g_object_unref(data->marker1);
845 data->marker1 = NULL;
848 if (data->marker2 != NULL) {
849 g_object_unref(data->marker2);
850 data->marker2 = NULL;
854 static gboolean
855 get_info(AdgADim *adim, CpmlVector vector[],
856 AdgPair *center, gdouble *distance)
858 AdgDim *dim;
859 AdgADimPrivate *data;
860 const AdgPair *ref1, *ref2;
861 const AdgPair *org1, *org2;
862 gdouble factor;
864 dim = (AdgDim *) adim;
865 data = adim->data;
866 ref1 = adg_dim_get_ref1(dim);
867 ref2 = adg_dim_get_ref2(dim);
868 org1 = adg_point_get_pair(data->org1);
869 org2 = adg_point_get_pair(data->org2);
871 vector[0].x = ref1->x - org1->x;
872 vector[0].y = ref1->y - org1->y;
873 vector[2].x = ref2->x - org2->x;
874 vector[2].y = ref2->y - org2->y;
876 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
877 if (factor == 0)
878 return FALSE;
880 factor = ((ref1->y - ref2->y) * vector[2].x -
881 (ref1->x - ref2->x) * vector[2].y) / factor;
883 center->x = ref1->x + vector[0].x * factor;
884 center->y = ref1->y + vector[0].y * factor;
885 *distance = cpml_pair_distance(center, adg_dim_get_pos(dim));
886 data->angle1 = cpml_vector_angle(&vector[0]);
887 data->angle2 = cpml_vector_angle(&vector[2]);
888 while (data->angle2 < data->angle1)
889 data->angle2 += M_PI * 2;
891 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
893 return TRUE;
896 static CpmlPath *
897 trail_callback(AdgTrail *trail, gpointer user_data)
899 AdgADim *adim;
900 AdgADimPrivate *data;
902 adim = (AdgADim *) user_data;
903 data = adim->data;
905 return &data->cpml.path;