[AdgDim] Added named pair support
[adg.git] / adg / adg-dim.c
blob6b4205da28d008453ea8c338e79eee806848aeca
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-dim
23 * @short_description: Root abstract class for all dimension entities
25 * The #AdgDim class is the base stub of all the dimension entities.
26 **/
28 /**
29 * AdgDim:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-dim.h"
37 #include "adg-dim-private.h"
38 #include "adg-dim-style.h"
39 #include "adg-dress-builtins.h"
40 #include "adg-toy-text.h"
41 #include "adg-type-builtins.h"
42 #include "adg-intl.h"
44 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_dim_parent_class)
45 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_dim_parent_class)
48 enum {
49 PROP_0,
50 PROP_DIM_DRESS,
51 PROP_REF1,
52 PROP_REF2,
53 PROP_POS,
54 PROP_LEVEL,
55 PROP_OUTSIDE,
56 PROP_VALUE,
57 PROP_MIN,
58 PROP_MAX
62 static void dispose (GObject *object);
63 static void finalize (GObject *object);
64 static void get_property (GObject *object,
65 guint param_id,
66 GValue *value,
67 GParamSpec *pspec);
68 static void set_property (GObject *object,
69 guint param_id,
70 const GValue *value,
71 GParamSpec *pspec);
72 static void global_changed (AdgEntity *entity);
73 static void local_changed (AdgEntity *entity);
74 static void invalidate (AdgEntity *entity);
75 static void arrange (AdgEntity *entity);
76 static gchar * default_value (AdgDim *dim);
77 static gdouble quote_angle (gdouble angle);
78 static gboolean set_dim_dress (AdgDim *dim,
79 AdgDress dress);
80 static gboolean set_value (AdgDim *dim,
81 const gchar *value);
82 static gboolean set_min (AdgDim *dim,
83 const gchar *min);
84 static gboolean set_max (AdgDim *dim,
85 const gchar *max);
88 G_DEFINE_ABSTRACT_TYPE(AdgDim, adg_dim, ADG_TYPE_ENTITY);
91 static void
92 adg_dim_class_init(AdgDimClass *klass)
94 GObjectClass *gobject_class;
95 AdgEntityClass *entity_class;
96 GParamSpec *param;
98 gobject_class = (GObjectClass *) klass;
99 entity_class = (AdgEntityClass *) klass;
101 g_type_class_add_private(klass, sizeof(AdgDimPrivate));
103 gobject_class->dispose = dispose;
104 gobject_class->finalize = finalize;
105 gobject_class->get_property = get_property;
106 gobject_class->set_property = set_property;
108 entity_class->global_changed = global_changed;
109 entity_class->local_changed = local_changed;
110 entity_class->invalidate = invalidate;
111 entity_class->arrange = arrange;
113 klass->quote_angle = quote_angle;
114 klass->default_value = default_value;
116 param = adg_param_spec_dress("dim-dress",
117 P_("Dress Style"),
118 P_("The dress to use for rendering this dimension"),
119 ADG_DRESS_DIMENSION,
120 G_PARAM_READWRITE);
121 g_object_class_install_property(gobject_class, PROP_DIM_DRESS, param);
123 param = g_param_spec_boxed("ref1",
124 P_("Reference 1"),
125 P_("First reference point of the dimension"),
126 ADG_TYPE_POINT,
127 G_PARAM_READWRITE);
128 g_object_class_install_property(gobject_class, PROP_REF1, param);
130 param = g_param_spec_boxed("ref2",
131 P_("Reference 2"),
132 P_("Second reference point of the dimension"),
133 ADG_TYPE_POINT,
134 G_PARAM_READWRITE);
135 g_object_class_install_property(gobject_class, PROP_REF2, param);
137 param = g_param_spec_boxed("pos",
138 P_("Position"),
139 P_("The reference position in local space of the quote: it will be combined with \"level\" to get the real quote position"),
140 ADG_TYPE_POINT,
141 G_PARAM_READWRITE);
142 g_object_class_install_property(gobject_class, PROP_POS, param);
144 param = g_param_spec_double("level",
145 P_("Level"),
146 P_("The dimension level, that is the factor to multiply the baseline spacing (defined in the dimension style) to get the offset from pos where the quote should be rendered"),
147 -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
148 G_PARAM_READWRITE);
149 g_object_class_install_property(gobject_class, PROP_LEVEL, param);
151 param = g_param_spec_enum("outside",
152 P_("Outside"),
153 P_("Whether the arrows must be inside the extension lines (ADG_THREE_STATE_OFF), must be extended outside the extension lines (ADG_THREE_STATE_ON) or should be automatically handled depending on the available space"),
154 ADG_TYPE_THREE_STATE, ADG_THREE_STATE_UNKNOWN,
155 G_PARAM_READWRITE);
156 g_object_class_install_property(gobject_class, PROP_OUTSIDE, param);
158 param = g_param_spec_string("value",
159 P_("Basic Value"),
160 P_("The theoretically exact value for this quote: set to NULL to automatically get the default value"),
161 NULL,
162 G_PARAM_READWRITE);
163 g_object_class_install_property(gobject_class, PROP_VALUE, param);
165 param = g_param_spec_string("value-min",
166 P_("Minimum Value or Low Tolerance"),
167 P_("The minimum value allowed or the lowest tolerance from value (depending of the dimension style): set to NULL to suppress"),
168 NULL,
169 G_PARAM_READWRITE);
170 g_object_class_install_property(gobject_class, PROP_MIN, param);
172 param = g_param_spec_string("value-max",
173 P_("Maximum Value or High Tolerance"),
174 P_("The maximum value allowed or the highest tolerance from value (depending of the dimension style): set to NULL to suppress"),
175 NULL,
176 G_PARAM_READWRITE);
177 g_object_class_install_property(gobject_class, PROP_MAX, param);
180 static void
181 adg_dim_init(AdgDim *dim)
183 AdgDimPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(dim, ADG_TYPE_DIM,
184 AdgDimPrivate);
186 data->dim_dress = ADG_DRESS_DIMENSION;
187 data->ref1 = NULL;
188 data->ref2 = NULL;
189 data->pos = NULL;
190 data->level = 1;
191 data->outside = ADG_THREE_STATE_UNKNOWN;
192 data->value = NULL;
193 data->min = NULL;
194 data->max = NULL;
196 dim->data = data;
199 static void
200 dispose(GObject *object)
202 AdgDimPrivate *data = ((AdgDim *) object)->data;
204 if (data->quote.container != NULL) {
205 g_object_unref(data->quote.container);
206 data->quote.container = NULL;
208 if (data->ref1 != NULL) {
209 adg_point_destroy(data->ref1);
210 data->ref1 = NULL;
212 if (data->ref2 != NULL) {
213 adg_point_destroy(data->ref2);
214 data->ref2 = NULL;
216 if (data->pos != NULL) {
217 adg_point_destroy(data->pos);
218 data->pos = NULL;
221 if (PARENT_OBJECT_CLASS->dispose != NULL)
222 PARENT_OBJECT_CLASS->dispose(object);
225 static void
226 finalize(GObject *object)
228 AdgDimPrivate *data = ((AdgDim *) object)->data;
230 g_free(data->value);
231 g_free(data->min);
232 g_free(data->max);
234 if (PARENT_OBJECT_CLASS->finalize != NULL)
235 PARENT_OBJECT_CLASS->finalize(object);
238 static void
239 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
241 AdgDimPrivate *data = ((AdgDim *) object)->data;
243 switch (prop_id) {
244 case PROP_DIM_DRESS:
245 g_value_set_int(value, data->dim_dress);
246 break;
247 case PROP_REF1:
248 g_value_set_boxed(value, data->ref1);
249 break;
250 case PROP_REF2:
251 g_value_set_boxed(value, data->ref2);
252 break;
253 case PROP_POS:
254 g_value_set_boxed(value, data->pos);
255 break;
256 case PROP_LEVEL:
257 g_value_set_double(value, data->level);
258 break;
259 case PROP_OUTSIDE:
260 g_value_set_enum(value, data->outside);
261 break;
262 case PROP_VALUE:
263 g_value_set_string(value, data->value);
264 break;
265 case PROP_MIN:
266 g_value_set_string(value, data->min);
267 break;
268 case PROP_MAX:
269 g_value_set_string(value, data->max);
270 break;
271 default:
272 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
273 break;
277 static void
278 set_property(GObject *object, guint prop_id,
279 const GValue *value, GParamSpec *pspec)
281 AdgDim *dim;
282 AdgDimPrivate *data;
284 dim = (AdgDim *) object;
285 data = dim->data;
287 switch (prop_id) {
288 case PROP_DIM_DRESS:
289 set_dim_dress(dim, g_value_get_int(value));
290 break;
291 case PROP_REF1:
292 if (data->ref1 != NULL)
293 adg_point_destroy(data->ref1);
294 data->ref1 = g_value_dup_boxed(value);
295 break;
296 case PROP_REF2:
297 if (data->ref2 != NULL)
298 adg_point_destroy(data->ref2);
299 data->ref2 = g_value_dup_boxed(value);
300 break;
301 case PROP_POS:
302 if (data->pos != NULL)
303 adg_point_destroy(data->pos);
304 data->pos = g_value_dup_boxed(value);
305 break;
306 case PROP_LEVEL:
307 data->level = g_value_get_double(value);
308 break;
309 case PROP_OUTSIDE:
310 data->outside = g_value_get_enum(value);
311 break;
312 case PROP_VALUE:
313 set_value(dim, g_value_get_string(value));
314 break;
315 case PROP_MIN:
316 set_min(dim, g_value_get_string(value));
317 break;
318 case PROP_MAX:
319 set_max(dim, g_value_get_string(value));
320 break;
321 default:
322 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
323 break;
329 * adg_dim_get_dim_dress:
330 * @dim: an #AdgDim
332 * Gets the dimension dress to be used in rendering @dim.
334 * Returns: the current dimension dress
336 AdgDress
337 adg_dim_get_dim_dress(AdgDim *dim)
339 AdgDimPrivate *data;
341 g_return_val_if_fail(ADG_IS_DIM(dim), ADG_DRESS_UNDEFINED);
343 data = dim->data;
345 return data->dim_dress;
349 * adg_dim_set_dim_dress:
350 * @dim: an #AdgDim
351 * @dress: the new #AdgDress to use
353 * Sets a new dimension dress to @dim. The new dress must be
354 * related to the original dress for this property: you cannot
355 * set a dress used for line styles to a dress managing fonts.
357 * The check is done by calling adg_dress_are_related() with
358 * @dress and the previous dress as arguments. Check out its
359 * documentation for details on what is a related dress.
361 void
362 adg_dim_set_dim_dress(AdgDim *dim, AdgDress dress)
364 g_return_if_fail(ADG_IS_DIM(dim));
366 if (set_dim_dress(dim, dress))
367 g_object_notify((GObject *) dim, "dim-dress");
371 * adg_dim_get_ref1:
372 * @dim: an #AdgDim
374 * Gets the ref1 coordinates. The returned pair is internally owned
375 * and must not be freed or modified.
377 * Returns: the ref1 coordinates
379 const AdgPair *
380 adg_dim_get_ref1(AdgDim *dim)
382 AdgDimPrivate *data;
384 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
386 data = dim->data;
388 return ADG_POINT_PAIR(data->ref1);
392 * adg_dim_get_ref2:
393 * @dim: an #AdgDim
395 * Gets the ref2 coordinates. The returned pair is internally owned
396 * and must not be freed or modified.
398 * Returns: the ref2 coordinates
400 const AdgPair *
401 adg_dim_get_ref2(AdgDim *dim)
403 AdgDimPrivate *data;
405 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
407 data = dim->data;
409 return ADG_POINT_PAIR(data->ref2);
413 * adg_dim_set_ref:
414 * @dim: an #AdgDim
415 * @ref1: the ref1 coordinates
416 * @ref2: the ref2 coordinates
418 * Sets the #AdgDim:ref1 and #AdgDim:ref2 reference points
419 * using @ref1 and @ref2 pairs. @ref1 or @ref2 could be
420 * %NULL (but not both), in which case only the non-null
421 * reference point is changed.
423 void
424 adg_dim_set_ref(AdgDim *dim, const AdgPair *ref1, const AdgPair *ref2)
426 GObject *object;
427 AdgDimPrivate *data;
429 g_return_if_fail(ADG_IS_DIM(dim));
430 g_return_if_fail(ref1 != NULL || ref2 != NULL);
432 data = dim->data;
433 object = (GObject *) dim;
435 g_object_freeze_notify(object);
437 if (ref1 != NULL) {
438 if (data->ref1 == NULL)
439 data->ref1 = adg_point_new();
441 adg_point_set(data->ref1, ref1);
443 g_object_notify(object, "ref1");
446 if (ref2 != NULL) {
447 if (data->ref2 == NULL)
448 data->ref2 = adg_point_new();
450 adg_point_set(data->ref2, ref2);
452 g_object_notify(object, "ref2");
455 g_object_thaw_notify(object);
459 * adg_dim_set_ref_explicit:
460 * @dim: an #AdgDim
461 * @ref1_x: x coordinate of ref1
462 * @ref1_y: y coordinate of ref1
463 * @ref2_x: x coordinate of ref2
464 * @ref2_y: y coordinate of ref2
466 * Works in the same way as adg_dim_set_ref() but using
467 * explicit coordinates instead of #AdgPair args. The
468 * notable difference is that, by using gdouble values,
469 * you can't set only one reference point.
471 void
472 adg_dim_set_ref_explicit(AdgDim *dim,
473 gdouble ref1_x, gdouble ref1_y,
474 gdouble ref2_x, gdouble ref2_y)
476 AdgPair ref1;
477 AdgPair ref2;
479 ref1.x = ref1_x;
480 ref1.y = ref1_y;
481 ref2.x = ref2_x;
482 ref2.y = ref2_y;
484 adg_dim_set_ref(dim, &ref1, &ref2);
488 * adg_dim_set_ref_from_model:
489 * @dim: an #AdgDim
490 * @model: the source #AdgModel
491 * @ref1: name of the pair in @model to use as ref1
492 * @ref2: name of the pair in @model to use as ref2
494 * Sets #AdgDim:ref1 and #AdgDim:ref2 properties by linking
495 * them to the @ref1 and @ref2 named pairs in @model. @ref1
496 * or @ref2 could be %NULL (but not both), in which case
497 * only the non-null reference point is changed.
499 * Using this function twice you can also link the reference
500 * points to named pairs taken from different models:
502 * |[
503 * adg_dim_set_ref_from_model(dim, model1, ref1, NULL);
504 * adg_dim_set_ref_from_model(dim, model2, NULL, ref2);
505 * ]|
507 void
508 adg_dim_set_ref_from_model(AdgDim *dim, AdgModel *model,
509 const gchar *ref1, const gchar *ref2)
511 GObject *object;
512 AdgDimPrivate *data;
514 g_return_if_fail(ADG_IS_DIM(dim));
515 g_return_if_fail(ADG_IS_MODEL(model));
516 g_return_if_fail(ref1 != NULL || ref2 != NULL);
518 object = (GObject *) dim;
519 data = dim->data;
521 g_object_freeze_notify(object);
523 if (ref1 != NULL) {
524 if (data->ref1 == NULL)
525 data->ref1 = adg_point_new();
527 adg_point_set_from_model(data->ref1, model, ref1);
529 g_object_notify(object, "ref1");
532 if (ref2 != NULL) {
533 if (data->ref2 == NULL)
534 data->ref2 = adg_point_new();
536 adg_point_set_from_model(data->ref2, model, ref2);
538 g_object_notify(object, "ref2");
541 g_object_thaw_notify(object);
545 * adg_dim_get_pos:
546 * @dim: an #AdgDim
548 * Gets the position coordinates. The returned pair is internally owned
549 * and must not be freed or modified.
551 * Returns: the pos coordinates
553 const AdgPair *
554 adg_dim_get_pos(AdgDim *dim)
556 AdgDimPrivate *data;
558 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
560 data = dim->data;
562 return ADG_POINT_PAIR(data->pos);
566 * adg_dim_set_pos:
567 * @dim: an #AdgDim
568 * @pos: the pos coordinates
570 * Sets a new #AdgDim:pos position.
572 void
573 adg_dim_set_pos(AdgDim *dim, const AdgPair *pos)
575 AdgDimPrivate *data;
577 g_return_if_fail(ADG_IS_DIM(dim));
578 g_return_if_fail(pos != NULL);
580 data = dim->data;
582 if (data->pos == NULL)
583 data->pos = adg_point_new();
585 adg_point_set(data->pos, pos);
587 g_object_notify((GObject *) dim, "pos");
591 * adg_dim_set_pos_explicit:
592 * @dim: an #AdgDim
593 * @pos_x: x coordinate of pos
594 * @pos_y: y coordinate of pos
596 * Shortcut to set #AdgDim:pos using explicit coordinates.
598 void
599 adg_dim_set_pos_explicit(AdgDim *dim, gdouble x, gdouble y)
601 AdgPair pos;
603 pos.x = x;
604 pos.y = y;
606 adg_dim_set_pos(dim, &pos);
610 * adg_dim_set_pos_from_model:
611 * @dim: an #AdgDim
612 * @model: the source #AdgModel
613 * @ref1: name of the pair in @model to use as pos
615 * Sets #AdgDim:pos by linking it to the @pos named pair
616 * in @model.
618 void
619 adg_dim_set_pos_from_model(AdgDim *dim, AdgModel *model, const gchar *pos)
621 AdgDimPrivate *data;
623 g_return_if_fail(ADG_IS_DIM(dim));
624 g_return_if_fail(ADG_IS_MODEL(model));
625 g_return_if_fail(pos != NULL);
627 data = dim->data;
629 if (data->pos == NULL)
630 data->pos = adg_point_new();
632 adg_point_set_from_model(data->pos, model, pos);
634 g_object_notify((GObject *) dim, "pos");
638 * adg_dim_get_level:
639 * @dim: an #AdgDim
641 * Gets the level of this dimension.
643 * Returns: the level value
645 gdouble
646 adg_dim_get_level(AdgDim *dim)
648 AdgDimPrivate *data;
650 g_return_val_if_fail(ADG_IS_DIM(dim), 0);
652 data = dim->data;
654 return data->level;
658 * adg_dim_set_level:
659 * @dim: an #AdgDim
660 * @level: the new level
662 * Sets a new level for this dimension. The level is used to
663 * stack the quotes using a spacing value from dim_style
664 * (specified in global space).
666 void
667 adg_dim_set_level(AdgDim *dim, gdouble level)
669 AdgDimPrivate *data;
671 g_return_if_fail(ADG_IS_DIM(dim));
673 data = dim->data;
674 data->level = level;
676 g_object_notify((GObject *) dim, "level");
680 * adg_dim_get_outside:
681 * @dim: an #AdgDim
683 * Gets the state of the #AdgDim:outside property: check the property
684 * documentation for further details.
686 * Returns: the current flag state
688 AdgThreeState
689 adg_dim_get_outside(AdgDim *dim)
691 AdgDimPrivate *data;
693 g_return_val_if_fail(ADG_IS_DIM(dim), ADG_THREE_STATE_UNKNOWN);
695 data = dim->data;
697 return data->outside;
701 * adg_dim_set_outside:
702 * @dim: an #AdgDim
703 * @outside: the new outside state
705 * Sets a new state for the #AdgDim:outside flag: check the property
706 * documentation for further details.
708 void
709 adg_dim_set_outside(AdgDim *dim, AdgThreeState outside)
711 AdgDimPrivate *data;
713 g_return_if_fail(ADG_IS_DIM(dim));
715 data = dim->data;
716 data->outside = outside;
718 g_object_notify((GObject *) dim, "outside");
722 * adg_dim_get_value:
723 * @dim: an #AdgDim
725 * Gets the value text. The string is internally owned and
726 * must not be freed or modified.
728 * Returns: the value text
730 const gchar *
731 adg_dim_get_value(AdgDim *dim)
733 AdgDimPrivate *data;
735 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
737 data = dim->data;
739 return data->value;
743 * adg_dim_set_value:
744 * @dim: an #AdgDim
745 * @value: the value text
747 * Explicitely sets the text to use as value. If @value is %NULL or
748 * was never set, an automatic text is calculated using the format
749 * specified in the current #AdgDimStyle and getting its value by
750 * calling the default_value() virtual method.
752 void
753 adg_dim_set_value(AdgDim *dim, const gchar *value)
755 g_return_if_fail(ADG_IS_DIM(dim));
757 if (set_value(dim, value))
758 g_object_notify((GObject *) dim, "value");
762 * adg_dim_get_min:
763 * @dim: an #AdgDim
765 * Gets the minimum value text or %NULL on minimum value disabled.
766 * The string is internally owned and must not be freed or modified.
768 * Returns: the mimimum value text
770 const gchar *
771 adg_dim_get_min(AdgDim *dim)
773 AdgDimPrivate *data;
775 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
777 data = dim->data;
779 return data->min;
783 * adg_dim_set_min:
784 * @dim: an #AdgDim
785 * @min: the new minimum limit
787 * Sets the minimum value. Use %NULL as @min to disable it.
789 void
790 adg_dim_set_min(AdgDim *dim, const gchar *min)
792 g_return_if_fail(ADG_IS_DIM(dim));
794 if (set_min(dim, min))
795 g_object_notify((GObject *) dim, "value-min");
799 * adg_dim_get_max:
800 * @dim: an #AdgDim
802 * Gets the maximum value text or %NULL on maximum value disabled.
803 * The string is internally owned and must not be freed or modified.
805 * Returns: the maximum value text
807 const gchar *
808 adg_dim_get_max(AdgDim *dim)
810 AdgDimPrivate *data;
812 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
814 data = dim->data;
816 return data->max;
820 * adg_dim_set_max:
821 * @dim: an #AdgDim
822 * @max: the new maximum value
824 * Sets the maximum value. Use %NULL as @max to disable it.
826 void
827 adg_dim_set_max(AdgDim *dim, const gchar *max)
829 g_return_if_fail(ADG_IS_DIM(dim));
831 if (set_max(dim, max))
832 g_object_notify((GObject *) dim, "value-max");
836 * adg_dim_set_limits:
837 * @dim: an #AdgDim
838 * @min: the new minumum value
839 * @max: the new maximum value
841 * Shortcut to set both the limits at once.
843 void
844 adg_dim_set_limits(AdgDim *dim, const gchar *min, const gchar *max)
846 g_return_if_fail(ADG_IS_DIM(dim));
848 g_object_freeze_notify((GObject *) dim);
849 adg_dim_set_min(dim, min);
850 adg_dim_set_max(dim, max);
851 g_object_thaw_notify((GObject *) dim);
855 * adg_dim_get_quote:
856 * @dim: an #AdgDim
858 * <note><para>
859 * This function is only useful in new dimension implementations.
860 * </para></note>
862 * Gets the quote container, if any. This function is valid only
863 * after the #AdgDim implementation of the arrange() virtual method
864 * has been called.
866 * Returns: the quote container
868 AdgContainer *
869 adg_dim_get_quote(AdgDim *dim)
871 AdgDimPrivate *data;
873 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
875 data = dim->data;
877 return data->quote.container;
881 * adg_dim_quote_angle:
882 * @dim: an #AdgDim
883 * @angle: an angle (in radians)
885 * <note><para>
886 * This function is only useful in new dimension implementations.
887 * </para></note>
889 * Converts @angle accordling to the style of @dim. Any quote angle
890 * should be validated by this method because every dimensioning
891 * style has its own convention regardling the text rotation.
893 * Returns: the angle to use (always in radians)
895 gdouble
896 adg_dim_quote_angle(AdgDim *dim, gdouble angle)
898 AdgDimClass *klass;
900 g_return_val_if_fail(ADG_IS_DIM(dim), angle);
902 klass = ADG_DIM_GET_CLASS(dim);
904 if (klass->quote_angle == NULL)
905 return angle;
907 return klass->quote_angle(angle);
911 * _adg_dim_get_dim_style:
912 * @dim: an #AdgDim entity
914 * Gets the #AdgDimStyle associated to @dim. The dress to style
915 * resolution is done in the arrange() method so this value is
916 * typically available in render() or in a derived arrange()
917 * method, after the #AdgDim arrange() function has been chained up.
919 * Returns: the dim style of @entity
921 AdgDimStyle *
922 _adg_dim_get_dim_style(AdgDim *dim)
924 AdgDimPrivate *data;
926 g_return_val_if_fail(ADG_IS_DIM(dim), NULL);
928 data = dim->data;
930 return data->dim_style;
934 static void
935 arrange(AdgEntity *entity)
937 AdgDim *dim;
938 AdgDimPrivate *data;
939 AdgEntity *container_entity;
940 AdgEntity *value_entity;
941 AdgEntity *min_entity;
942 AdgEntity *max_entity;
943 CpmlExtents extents;
944 AdgMatrix map;
945 const AdgPair *shift;
947 dim = (AdgDim *) entity;
948 data = dim->data;
950 /* Resolve the dim style */
951 if (data->dim_style == NULL)
952 data->dim_style = (AdgDimStyle *) adg_entity_style(entity,
953 data->dim_dress);
955 if (data->quote.container == NULL)
956 data->quote.container = g_object_new(ADG_TYPE_CONTAINER,
957 "parent", dim, NULL);
959 if (data->quote.value == NULL) {
960 AdgDress dress = adg_dim_style_get_value_dress(data->dim_style);
962 data->quote.value = g_object_new(ADG_TYPE_TOY_TEXT,
963 "font-dress", dress, NULL);
965 adg_container_add(data->quote.container,
966 (AdgEntity *) data->quote.value);
968 if (data->value == NULL) {
969 /* Automatically generate the value text */
970 gchar *text = ADG_DIM_GET_CLASS(dim)->default_value(dim);
971 adg_toy_text_set_label(data->quote.value, text);
972 g_free(text);
973 } else {
974 adg_toy_text_set_label(data->quote.value, data->value);
978 if (data->quote.min == NULL && data->min != NULL) {
979 AdgDress dress = adg_dim_style_get_min_dress(data->dim_style);
981 data->quote.min = g_object_new(ADG_TYPE_TOY_TEXT,
982 "font-dress", dress, NULL);
984 adg_container_add(data->quote.container, (AdgEntity *) data->quote.min);
985 adg_toy_text_set_label(data->quote.min, data->min);
988 if (data->quote.max == NULL && data->max != NULL) {
989 AdgDress dress = adg_dim_style_get_max_dress(data->dim_style);
991 data->quote.max = g_object_new(ADG_TYPE_TOY_TEXT,
992 "font-dress", dress, NULL);
994 adg_container_add(data->quote.container, (AdgEntity *) data->quote.max);
995 adg_toy_text_set_label(data->quote.max, data->max);
998 container_entity = (AdgEntity *) data->quote.container;
999 value_entity = (AdgEntity *) data->quote.value;
1000 min_entity = (AdgEntity *) data->quote.min;
1001 max_entity = (AdgEntity *) data->quote.max;
1003 /* Propagate the arrange signal to the quote container */
1004 adg_entity_arrange(container_entity);
1006 /* Basic value */
1007 adg_entity_get_extents(value_entity, &extents);
1009 /* Limit values (min and max) */
1010 if (min_entity != NULL || max_entity != NULL) {
1011 CpmlExtents min_extents = { 0 };
1012 CpmlExtents max_extents = { 0 };
1013 gdouble spacing = 0;
1015 /* Minimum limit */
1016 if (min_entity != NULL)
1017 adg_entity_get_extents(min_entity, &min_extents);
1019 /* Maximum limit */
1020 if (max_entity != NULL)
1021 adg_entity_get_extents(max_entity, &max_extents);
1023 shift = adg_dim_style_get_limits_shift(data->dim_style);
1024 if (min_entity != NULL && max_entity != NULL)
1025 spacing = adg_dim_style_get_limits_spacing(data->dim_style);
1027 cairo_matrix_init_translate(&map, extents.size.x + shift->x,
1028 (spacing + min_extents.size.y +
1029 max_extents.size.y) / 2 +
1030 shift->y - extents.size.y / 2);
1032 if (min_entity != NULL)
1033 adg_entity_set_global_map(min_entity, &map);
1035 if (max_entity != NULL) {
1036 cairo_matrix_translate(&map, 0, -min_extents.size.y - spacing);
1037 adg_entity_set_global_map(max_entity, &map);
1040 extents.size.x += shift->x + MAX(min_extents.size.x, max_extents.size.x);
1043 /* Center and apply the style displacements */
1044 shift = adg_dim_style_get_quote_shift(data->dim_style);
1045 cairo_matrix_init_translate(&map, shift->x - extents.size.x / 2, shift->y);
1046 adg_entity_set_global_map(container_entity, &map);
1048 adg_entity_arrange(container_entity);
1051 static void
1052 global_changed(AdgEntity *entity)
1054 AdgDimPrivate *data = ((AdgDim *) entity)->data;
1056 PARENT_ENTITY_CLASS->global_changed(entity);
1058 if (data->quote.container != NULL)
1059 adg_entity_global_changed((AdgEntity *) data->quote.container);
1062 static void
1063 local_changed(AdgEntity *entity)
1065 AdgDimPrivate *data = ((AdgDim *) entity)->data;
1067 PARENT_ENTITY_CLASS->local_changed(entity);
1069 if (data->quote.container != NULL)
1070 adg_entity_local_changed((AdgEntity *) data->quote.container);
1073 static void
1074 invalidate(AdgEntity *entity)
1076 AdgDimPrivate *data = ((AdgDim *) entity)->data;
1078 if (data->quote.container != NULL)
1079 adg_entity_invalidate((AdgEntity *) data->quote.container);
1082 static gchar *
1083 default_value(AdgDim *dim)
1085 g_warning("AdgDim::default_value not implemented for `%s'",
1086 g_type_name(G_TYPE_FROM_INSTANCE(dim)));
1087 return g_strdup("undef");
1090 static gdouble
1091 quote_angle(gdouble angle)
1093 angle = cpml_angle(angle);
1095 if (angle > G_PI_4 * 4 / 3 || angle <= -G_PI_4 * 3)
1096 angle = cpml_angle(angle + G_PI);
1098 return angle;
1101 static gboolean
1102 set_dim_dress(AdgDim *dim, AdgDress dress)
1104 AdgDimPrivate *data = dim->data;
1106 if (adg_dress_set(&data->dim_dress, dress)) {
1107 data->dim_style = NULL;
1108 return TRUE;
1111 return FALSE;
1114 static gboolean
1115 set_value(AdgDim *dim, const gchar *value)
1117 AdgDimPrivate *data;
1119 data = dim->data;
1121 if (adg_strcmp(value, data->value) == 0)
1122 return FALSE;
1124 g_free(data->value);
1125 data->value = g_strdup(value);
1127 if (data->quote.value != NULL) {
1128 g_object_unref(data->quote.value);
1129 data->quote.value = NULL;
1132 return TRUE;
1135 static gboolean
1136 set_min(AdgDim *dim, const gchar *min)
1138 AdgDimPrivate *data = dim->data;
1140 if (adg_strcmp(min, data->min) == 0)
1141 return FALSE;
1143 g_free(data->min);
1144 data->min = g_strdup(min);
1146 if (data->quote.min != NULL) {
1147 g_object_unref(data->quote.min);
1148 data->quote.min = NULL;
1151 return TRUE;
1154 static gboolean
1155 set_max(AdgDim *dim, const gchar *max)
1157 AdgDimPrivate *data = dim->data;
1159 if (adg_strcmp(max, data->max) == 0)
1160 return FALSE;
1162 g_free(data->max);
1163 data->max = g_strdup(max);
1165 if (data->quote.max != NULL) {
1166 g_object_unref(data->quote.max);
1167 data->quote.max = NULL;
1170 return TRUE;