1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * @Section_Id:AdgDress
25 * @short_description: The ADG way to associate styles to entities
27 * The dress is a virtualization of an #AdgStyle instance. #AdgEntity
28 * objects do not directly refer to #AdgStyle but use #AdgDress values
29 * instead. This allows some advanced operations, such as overriding
30 * a dress only in a specific entity branch of the hierarchy or
31 * customize multiple entities at once.
37 * An index representing a virtual #AdgStyle.
41 * ADG_VALUE_HOLDS_DRESS:
44 * Checks whether a #GValue is actually holding an #AdgDress value or not.
46 * Returns: %TRUE is @value is holding an #AdgDress, %FALSE otherwise
50 #include "adg-dress.h"
51 #include "adg-dress-private.h"
52 #include "adg-color-style.h"
53 #include "adg-line-style.h"
54 #include "adg-font-style.h"
55 #include "adg-dim-style.h"
56 #include "adg-arrow.h"
59 static AdgDress
quark_to_dress (GQuark quark
);
60 static void dress_to_string (const GValue
*src
,
62 static void string_to_dress (const GValue
*src
,
64 static void param_class_init (GParamSpecClass
*klass
);
65 static gboolean
value_validate (GParamSpec
*spec
,
68 static guint
array_append (AdgDressPrivate
*data
);
69 static AdgDressPrivate
*
70 array_lookup (guint n
);
71 static guint
array_len (void);
75 adg_dress_get_type(void)
77 static GType type
= 0;
79 if (G_UNLIKELY(type
== 0)) {
80 const GTypeInfo info
= { 0, };
82 type
= g_type_register_static(G_TYPE_INT
, "AdgDress", &info
, 0);
84 g_value_register_transform_func(type
, G_TYPE_STRING
, dress_to_string
);
85 g_value_register_transform_func(G_TYPE_STRING
, type
, string_to_dress
);
93 * @name: the dress name
94 * @style: the style to associate
96 * Creates a new dress. It is a convenient wrapper of adg_dress_new_full()
97 * that uses as ancestor the G_TYPE_FROM_INSTANCE() of @style.
99 * After a succesfull call, a new reference is added to @style.
101 * Returns: the new #AdgDress value or #ADG_DRESS_UNDEFINED on errors
104 adg_dress_new(const gchar
*name
, AdgStyle
*style
)
106 return adg_dress_new_full(name
, style
, G_TYPE_FROM_INSTANCE(style
));
110 * adg_dress_new_full:
111 * @name: the dress name
112 * @style: the style to associate
113 * @ancestor_type: the common ancestor type
115 * Creates a new dress, explicitely setting the ancestor type.
116 * @ancestor_type must be present in the @style hierarchy: check
117 * out the adg_dress_set_style() documentation to know what the
118 * ancestor type is used for.
120 * If a dress with the same name exists, a warning is raised and
121 * #ADG_DRESS_UNDEFINED is returned without further actions.
123 * After a succesfull call, a new reference is added to @style.
125 * Returns: the new #AdgDress value or #ADG_DRESS_UNDEFINED on errors
128 adg_dress_new_full(const gchar
*name
, AdgStyle
*style
, GType ancestor_type
)
132 AdgDressPrivate data
;
134 g_return_val_if_fail(name
!= NULL
, 0);
135 g_return_val_if_fail(style
!= NULL
, 0);
136 g_return_val_if_fail(G_TYPE_CHECK_INSTANCE_TYPE(style
, ancestor_type
), 0);
138 quark
= g_quark_from_string(name
);
139 dress
= quark_to_dress(quark
);
142 g_warning("%s: `%s' name yet used by the `%d' dress",
143 G_STRLOC
, name
, dress
);
144 return ADG_DRESS_UNDEFINED
;
149 data
.ancestor_type
= ancestor_type
;
153 return array_append(&data
) - 1;
157 * adg_dress_are_related:
158 * @dress1: an #AdgDress
159 * @dress2: another #AdgDress
161 * Checks whether @dress1 and @dress2 are related, that is
162 * if they have the same ancestor type as returned by
163 * adg_dress_get_ancestor_type().
165 * Returns: %TRUE if the dresses are related, %FALSE otherwise
168 adg_dress_are_related(AdgDress dress1
, AdgDress dress2
)
170 GType ancestor_type1
, ancestor_type2
;
172 ancestor_type1
= adg_dress_get_ancestor_type(dress1
);
173 if (ancestor_type1
<= 0)
176 ancestor_type2
= adg_dress_get_ancestor_type(dress2
);
177 if (ancestor_type2
<= 0)
180 return ancestor_type1
== ancestor_type2
;
185 * @dress: a pointer to an #AdgDress
186 * @src: the source dress
188 * Copies @src in @dress. This operation can be succesful only if
189 * @dress is #ADG_DRESS_UNDEFINED or if it contains a dress related
190 * to @src, that is adg_dress_are_related() returns %TRUE.
192 * Returns: %TRUE on copy done, %FALSE on copy failed or not needed
195 adg_dress_set(AdgDress
*dress
, AdgDress src
)
200 if (*dress
!= ADG_DRESS_UNDEFINED
&&
201 !adg_dress_are_related(*dress
, src
)) {
202 const gchar
*dress_name
;
203 const gchar
*src_name
;
205 dress_name
= adg_dress_name(*dress
);
206 if (dress_name
== NULL
)
207 dress_name
= "UNDEFINED";
209 src_name
= adg_dress_name(src
);
210 if (src_name
== NULL
)
211 src_name
= "UNDEFINED";
213 g_warning("%s: `%d' (%s) and `%d' (%s) dresses are not related",
214 G_STRLOC
, *dress
, dress_name
, src
, src_name
);
226 * @dress: an #AdgDress
228 * Gets the name associated to @dress. No warnings are raised if
229 * @dress is not found.
231 * Returns: the requested name or %NULL if not found
234 adg_dress_name(AdgDress dress
)
236 if (dress
<= 0 || dress
>= array_len())
239 return g_quark_to_string(array_lookup(dress
)->quark
);
243 * adg_dress_from_name:
244 * @name: the name of a dress
246 * Gets the dress binded to a @name string. No warnings are raised
247 * if the dress is not found.
249 * Returns: the #AdgDress code or #ADG_DRESS_UNDEFINED if not found
252 adg_dress_from_name(const gchar
*name
)
254 return quark_to_dress(g_quark_try_string(name
));
258 * adg_dress_get_ancestor_type:
259 * @dress: an #AdgDress
261 * Gets the base type that should be present in every #AdgStyle
262 * acceptable by @dress.
264 * Returns: the ancestor type
267 adg_dress_get_ancestor_type(AdgDress dress
)
269 AdgDressPrivate
*data
;
271 if (dress
<= 0 || dress
>= array_len())
274 data
= array_lookup(dress
);
276 return data
->ancestor_type
;
280 * adg_dress_get_style:
281 * @dress: an #AdgDress
283 * Gets the style associated to @dress. No warnings are raised
284 * if the dress is not found.
286 * Returns: the requested #AdgStyle derived instance or %NULL if not found
289 adg_dress_get_style(AdgDress dress
)
291 AdgDressPrivate
*data
;
293 if (dress
<= 0 || dress
>= array_len())
296 data
= array_lookup(dress
);
302 * adg_dress_set_style:
303 * @dress: an #AdgDress
304 * @style: the new style
306 * Associates a new @style to @dress. If the dress does not exist
307 * (it was not previously created by adg_dress_new()), a warning
308 * message is raised and the function fails.
310 * @style is checked for compatibily with @dress. Any dress holds
311 * an ancestor type: if this type is not found in the @style
312 * hierarchy, a warning message is raised the function fails.
314 * After a succesfull call, the reference to the old style (if any)
315 * is dropped while a new reference to @style is added.
318 adg_dress_set_style(AdgDress dress
, AdgStyle
*style
)
320 AdgDressPrivate
*data
;
322 if (dress
<= 0 || dress
>= array_len()) {
323 g_warning("%s: `%d' dress undefined", G_STRLOC
, dress
);
327 data
= array_lookup(dress
);
329 if (data
->style
== style
)
332 /* Check if the new style is compatible with this dress */
333 if (!G_TYPE_CHECK_INSTANCE_TYPE(style
, data
->ancestor_type
)) {
334 g_warning("%s: `%s' is not compatible with `%s' for `%s' dress",
335 G_STRLOC
, g_type_name(G_TYPE_FROM_INSTANCE(style
)),
336 g_type_name(data
->ancestor_type
), adg_dress_name(dress
));
340 if (data
->style
!= NULL
)
341 g_object_unref(data
->style
);
345 if (data
->style
!= NULL
)
346 g_object_ref(data
->style
);
350 * adg_dress_accept_style:
351 * @dress: an #AdgDress
352 * @style: the #AdgStyle to check
354 * Checks whether @style could be associated to @dress using
355 * adg_dress_set_style().
357 * Returns: %TRUE if @dress can accept @style, %FALSE otherwise
360 adg_dress_accept_style(AdgDress dress
, AdgStyle
*style
)
362 GType ancestor_type
= adg_dress_get_ancestor_type(dress
);
364 g_return_val_if_fail(ancestor_type
> 0, FALSE
);
365 g_return_val_if_fail(ADG_IS_STYLE(style
), FALSE
);
367 return G_TYPE_CHECK_INSTANCE_TYPE(style
, ancestor_type
);
372 quark_to_dress(GQuark quark
)
375 AdgDressPrivate
*data
;
377 for (dress
= 0; dress
< array_len(); ++dress
) {
378 data
= array_lookup(dress
);
380 if (data
->quark
== quark
)
384 return ADG_DRESS_UNDEFINED
;
388 dress_to_string(const GValue
*src
, GValue
*dst
)
390 g_value_set_string(dst
, adg_dress_name(g_value_get_int(src
)));
394 string_to_dress(const GValue
*src
, GValue
*dst
)
396 g_value_set_int(dst
, adg_dress_from_name(g_value_get_string(src
)));
400 typedef struct _AdgParamSpecDress AdgParamSpecDress
;
402 struct _AdgParamSpecDress
{
403 GParamSpecInt parent
;
404 AdgDress source_dress
;
409 _adg_param_spec_dress_get_type(void)
411 static GType type
= 0;
413 if (G_UNLIKELY(type
== 0)) {
414 const GTypeInfo info
= {
415 sizeof(GParamSpecClass
),
418 (GClassInitFunc
) param_class_init
,
421 sizeof(AdgParamSpecDress
),
425 type
= g_type_register_static(G_TYPE_PARAM_INT
,
426 "AdgParamSpecDress", &info
, 0);
433 param_class_init(GParamSpecClass
*klass
)
435 klass
->value_type
= ADG_TYPE_DRESS
;
436 klass
->value_validate
= value_validate
;
440 value_validate(GParamSpec
*spec
, GValue
*value
)
442 AdgParamSpecDress
*fspec
;
443 AdgDress dress
, new_dress
;
445 fspec
= (AdgParamSpecDress
*) spec
;
446 dress
= value
->data
[0].v_int
;
447 new_dress
= ADG_DRESS_UNDEFINED
;
449 adg_dress_set(&new_dress
, dress
);
450 value
->data
[0].v_int
= new_dress
;
452 return dress
!= new_dress
;
457 * adg_param_spec_dress:
458 * @name: canonical name
459 * @nick: nickname of the param
460 * @blurb: brief desciption
461 * @dress: the #AdgDress dress
462 * @flags: a combination of #GParamFlags
464 * Creates a param spec to hold a dress value.
466 * Returns: the newly allocated #GParamSpec
469 adg_param_spec_dress(const gchar
*name
, const gchar
*nick
, const gchar
*blurb
,
470 AdgDress dress
, GParamFlags flags
)
472 AdgParamSpecDress
*fspec
;
474 if (dress
<= 0 || dress
>= array_len()) {
475 g_warning("%s: `%d' dress undefined", G_STRLOC
, dress
);
479 fspec
= g_param_spec_internal(ADG_TYPE_PARAM_SPEC_DRESS
,
480 name
, nick
, blurb
, flags
);
481 fspec
->source_dress
= dress
;
483 return (GParamSpec
*) fspec
;
488 * ADG_DRESS_UNDEFINED:
490 * A value reperesenting an undefined #AdgDress.
494 * ADG_DRESS_COLOR_REGULAR:
496 * The default builtin #AdgDress color. This dress will be resolved
497 * to an #AdgColorStyle instance.
500 _adg_dress_color_regular(void)
502 static AdgDress dress
= 0;
504 if (G_UNLIKELY(dress
== 0)) {
505 AdgStyle
*style
= g_object_new(ADG_TYPE_COLOR_STYLE
, NULL
);
507 dress
= adg_dress_new("color-regular", style
);
508 g_object_unref(style
);
515 * ADG_DRESS_COLOR_DIMENSION:
517 * The builtin #AdgDress color used by dimensioning items. This dress
518 * will be resolved to an #AdgColorStyle instance.
521 _adg_dress_color_dimension(void)
523 static AdgDress dress
= 0;
525 if (G_UNLIKELY(dress
== 0)) {
526 AdgStyle
*style
= g_object_new(ADG_TYPE_COLOR_STYLE
,
529 dress
= adg_dress_new("color-dimension", style
);
530 g_object_unref(style
);
537 * ADG_DRESS_LINE_REGULAR:
539 * The default builtin #AdgDress line. This dress will be resolved
540 * to an #AdgLineStyle instance.
543 _adg_dress_line_regular(void)
545 static AdgDress dress
= 0;
547 if (G_UNLIKELY(dress
== 0)) {
548 AdgStyle
*style
= g_object_new(ADG_TYPE_LINE_STYLE
,
551 dress
= adg_dress_new("line-regular", style
);
552 g_object_unref(style
);
559 * ADG_DRESS_LINE_DIMENSION:
561 * The builtin #AdgDress line type used by base and extension lines in
562 * dimensioning. This dress will be resolved to an #AdgLineStyle instance.
565 _adg_dress_line_dimension(void)
567 static AdgDress dress
= 0;
569 if (G_UNLIKELY(dress
== 0)) {
570 AdgStyle
*style
= g_object_new(ADG_TYPE_LINE_STYLE
,
571 "antialias", CAIRO_ANTIALIAS_NONE
,
574 dress
= adg_dress_new("line-dimension", style
);
575 g_object_unref(style
);
582 * ADG_DRESS_TEXT_REGULAR:
584 * The default builtin #AdgDress font. This dress will be resolved
585 * to an #AdgFontStyle instance.
588 _adg_dress_text_regular(void)
590 static AdgDress dress
= 0;
592 if (G_UNLIKELY(dress
== 0)) {
593 AdgStyle
*style
= g_object_new(ADG_TYPE_FONT_STYLE
,
597 dress
= adg_dress_new("text-regular", style
);
598 g_object_unref(style
);
605 * ADG_DRESS_TEXT_VALUE:
607 * The builtin #AdgDress font used to render the nominal value of a
608 * dimension. This dress will be resolved to an #AdgFontStyle instance.
611 _adg_dress_text_value(void)
613 static AdgDress dress
= 0;
615 if (G_UNLIKELY(dress
== 0)) {
616 AdgStyle
*style
= g_object_new(ADG_TYPE_FONT_STYLE
,
618 "weight", CAIRO_FONT_WEIGHT_BOLD
,
621 dress
= adg_dress_new("text-value", style
);
622 g_object_unref(style
);
629 * ADG_DRESS_TEXT_LIMIT:
631 * The builtin #AdgDress font used to render the limits of either
632 * the min and max values of a dimension. This dress will be
633 * resolved to an #AdgFontStyle instance.
636 _adg_dress_text_limit(void)
638 static AdgDress dress
= 0;
640 if (G_UNLIKELY(dress
== 0)) {
641 AdgStyle
*style
= g_object_new(ADG_TYPE_FONT_STYLE
,
645 dress
= adg_dress_new("text-limit", style
);
646 g_object_unref(style
);
653 * ADG_DRESS_DIMENSION_REGULAR:
655 * The default builtin #AdgDress dimensioning style. This dress
656 * will be resolved to an #AdgDimStyle instance.
659 _adg_dress_dimension_regular(void)
661 static AdgDress dress
= 0;
663 if (G_UNLIKELY(dress
== 0)) {
664 AdgMarker
*arrow
= g_object_new(ADG_TYPE_ARROW
, NULL
);
665 AdgStyle
*style
= g_object_new(ADG_TYPE_DIM_STYLE
, NULL
);
667 adg_dim_style_use_marker1((AdgDimStyle
*) style
, arrow
);
668 adg_marker_set_pos(arrow
, 1);
669 adg_dim_style_use_marker2((AdgDimStyle
*) style
, arrow
);
671 dress
= adg_dress_new("dimension-regular", style
);
672 g_object_unref(style
);
673 g_object_unref(arrow
);
680 static GArray
* array_singleton (void) G_GNUC_CONST
;
683 array_singleton(void)
685 static GArray
*array
= NULL
;
688 const AdgDressPrivate data
= { 0, };
690 array
= g_array_new(FALSE
, FALSE
, sizeof(AdgDressPrivate
));
692 /* Reserve the first item for the undefined dress */
693 g_array_append_val(array
, data
);
700 array_append(AdgDressPrivate
*data
)
702 return g_array_append_val(array_singleton(), *data
)->len
;
705 static AdgDressPrivate
*
706 array_lookup(guint n
)
708 return &g_array_index(array_singleton(), AdgDressPrivate
, n
);
714 return array_singleton()->len
;