1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * @short_description: The base class for renderable objects
25 * This abstract class provides the base for all renderable objects.
27 * To provide a proper #AdgEntity derived type, you must at least
28 * implement its arrange() and render() virtual methods. Also, if
29 * you are using some sort of caching, ensure to clear it in the
30 * invalidate() method.
38 * All fields are private and should not be used directly.
39 * Use its public methods instead.
46 * @destroy: when a destroy request has been explicitely requested
47 * @parent_set: called whenever the parent of an entity has changed
48 * @global_changed: the global matrix has been invalidated
49 * @local_changed: the local matrix has been invalidated
50 * @invalidate: invalidating callback, used to clear the internal cache
51 * @arrange: prepare the layout and fill the extents struct
52 * @render: rendering callback, it must be implemented by every entity
54 * Any entity (if not abstract) must implement at least the @render method.
55 * The other signal handlers can be overriden to provide custom behaviors
56 * and usually must chain up the original handler.
63 * @entity: an #AdgEntity
64 * @user_data: a general purpose pointer
66 * Callback used when inspecting or browsing entities. For example,
67 * it is passed to adg_model_foreach_dependency() to perform an
68 * operation on all the entities depending on an #AdgModel.
74 #include "adg-internal.h"
75 #if GTK3_ENABLED || GTK2_ENABLED
79 #include "adg-container.h"
80 #include "adg-table.h"
81 #include "adg-title-block.h"
82 #include <adg-canvas.h>
83 #include "adg-dress.h"
84 #include "adg-style.h"
85 #include "adg-model.h"
86 #include "adg-point.h"
87 #include "adg-cairo-fallback.h"
89 #include "adg-entity-private.h"
92 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_entity_parent_class)
95 G_DEFINE_ABSTRACT_TYPE(AdgEntity
, adg_entity
, G_TYPE_INITIALLY_UNOWNED
)
117 static void _adg_dispose (GObject
*object
);
118 static void _adg_get_property (GObject
*object
,
122 static void _adg_set_property (GObject
*object
,
126 static void _adg_set_parent (AdgEntity
*entity
,
128 static void _adg_global_changed (AdgEntity
*entity
);
129 static void _adg_local_changed (AdgEntity
*entity
);
130 static void _adg_real_invalidate (AdgEntity
*entity
);
131 static void _adg_real_arrange (AdgEntity
*entity
);
132 static void _adg_real_render (AdgEntity
*entity
,
134 static guint _adg_signals
[LAST_SIGNAL
] = { 0 };
135 static gboolean _adg_show_extents
= FALSE
;
139 adg_entity_class_init(AdgEntityClass
*klass
)
141 GObjectClass
*gobject_class
;
144 GType param_types
[1];
146 gobject_class
= (GObjectClass
*) klass
;
148 g_type_class_add_private(klass
, sizeof(AdgEntityPrivate
));
150 gobject_class
->dispose
= _adg_dispose
;
151 gobject_class
->get_property
= _adg_get_property
;
152 gobject_class
->set_property
= _adg_set_property
;
154 klass
->destroy
= (void (*)(AdgEntity
*)) g_object_unref
;
155 klass
->parent_set
= NULL
;
156 klass
->global_changed
= _adg_global_changed
;
157 klass
->local_changed
= _adg_local_changed
;
158 klass
->invalidate
= NULL
;
159 klass
->arrange
= NULL
;
160 klass
->render
= NULL
;
162 param
= g_param_spec_object("parent",
164 P_("The parent entity of this entity or NULL if this is a top-level entity"),
167 g_object_class_install_property(gobject_class
, PROP_PARENT
, param
);
169 param
= g_param_spec_boxed("global-map",
171 P_("The transformation to be combined with the parent ones to get the global matrix"),
172 CAIRO_GOBJECT_TYPE_MATRIX
,
174 g_object_class_install_property(gobject_class
, PROP_GLOBAL_MAP
, param
);
176 param
= g_param_spec_boxed("local-map",
178 P_("The local transformation that could be used to compute the local matrix in the way specified by the #AdgEntity:local-mix property"),
179 CAIRO_GOBJECT_TYPE_MATRIX
,
181 g_object_class_install_property(gobject_class
, PROP_LOCAL_MAP
, param
);
183 param
= g_param_spec_enum("local-mix",
184 P_("Local Mix Method"),
185 P_("Define how the local maps of the entity and its ancestors should be combined to get the local matrix"),
186 ADG_TYPE_MIX
, ADG_MIX_ANCESTORS
,
188 g_object_class_install_property(gobject_class
, PROP_LOCAL_MIX
, param
);
191 * AdgEntity::destroy:
192 * @entity: an #AdgEntity
194 * Emitted to explicitely destroy @entity. It unreferences
195 * @entity so that will be destroyed, unless the caller owns
196 * an additional references added with g_object_ref().
198 * In the usual case, this is equivalent of calling
199 * g_object_unref() on @entity but, for composite entities or
200 * containers, the destroy signal is propagated to the children.
204 _adg_signals
[DESTROY
] =
205 g_signal_new("destroy",
206 G_OBJECT_CLASS_TYPE(gobject_class
),
208 G_STRUCT_OFFSET(AdgEntityClass
, destroy
),
210 adg_marshal_VOID__VOID
,
214 * AdgEntity::parent-set:
215 * @entity: an #AdgEntity
216 * @old_parent: the old parent
218 * Emitted after the parent entity has changed. The new parent
219 * can be inspected using adg_entity_get_parent().
221 * It is allowed for both old and new parent to be %NULL.
225 _adg_signals
[PARENT_SET
] =
226 g_signal_new("parent-set",
227 G_OBJECT_CLASS_TYPE(gobject_class
),
229 G_STRUCT_OFFSET(AdgEntityClass
, parent_set
),
231 adg_marshal_VOID__OBJECT
,
232 G_TYPE_NONE
, 1, ADG_TYPE_ENTITY
);
235 * AdgEntity::global-changed:
236 * @entity: an #AdgEntity
238 * Emitted when the global map of @entity or any of its parent
239 * has changed. The default handler will compute the new global
240 * matrix, updating the internal cache.
244 _adg_signals
[GLOBAL_CHANGED
] =
245 g_signal_new("global-changed",
246 G_OBJECT_CLASS_TYPE(gobject_class
),
248 G_STRUCT_OFFSET(AdgEntityClass
, global_changed
),
250 adg_marshal_VOID__VOID
,
254 * AdgEntity::local-changed:
255 * @entity: an #AdgEntity
257 * Emitted when the local map of @entity or any of its parent
258 * has changed. The default handler will compute the new local
259 * matrix, updating the internal cache.
263 _adg_signals
[LOCAL_CHANGED
] =
264 g_signal_new("local-changed",
265 G_OBJECT_CLASS_TYPE(gobject_class
),
267 G_STRUCT_OFFSET(AdgEntityClass
, local_changed
),
269 adg_marshal_VOID__VOID
,
273 * AdgEntity::invalidate:
274 * @entity: an #AdgEntity
276 * Invalidates the whole @entity, that is resets all the cache
277 * (if present) built during the #AdgEntity::arrange signal.
278 * The resulting state is a clean entity, similar to what you
279 * have just before the first rendering.
283 closure
= g_cclosure_new(G_CALLBACK(_adg_real_invalidate
), NULL
, NULL
);
284 _adg_signals
[INVALIDATE
] =
285 g_signal_newv("invalidate", ADG_TYPE_ENTITY
,
286 G_SIGNAL_RUN_LAST
, closure
, NULL
, NULL
,
287 adg_marshal_VOID__VOID
,
288 G_TYPE_NONE
, 0, param_types
);
291 * AdgEntity::arrange:
292 * @entity: an #AdgEntity
294 * Arranges the layout of @entity, updating the cache if necessary,
295 * and computes the extents of @entity.
299 closure
= g_cclosure_new(G_CALLBACK(_adg_real_arrange
), NULL
, NULL
);
300 _adg_signals
[ARRANGE
] =
301 g_signal_newv("arrange", ADG_TYPE_ENTITY
,
302 G_SIGNAL_RUN_LAST
, closure
, NULL
, NULL
,
303 adg_marshal_VOID__VOID
,
304 G_TYPE_NONE
, 0, param_types
);
308 * @entity: an #AdgEntity
309 * @cr: a #cairo_t drawing context
311 * Causes the rendering of @entity on @cr. A render signal will
312 * automatically emit #AdgEntity::arrange just before the real
313 * rendering on the cairo context.
317 closure
= g_cclosure_new(G_CALLBACK(_adg_real_render
), NULL
, NULL
);
318 param_types
[0] = G_TYPE_POINTER
;
319 _adg_signals
[RENDER
] =
320 g_signal_newv("render", ADG_TYPE_ENTITY
,
321 G_SIGNAL_RUN_LAST
, closure
, NULL
, NULL
,
322 adg_marshal_VOID__POINTER
,
323 G_TYPE_NONE
, 1, param_types
);
327 adg_entity_init(AdgEntity
*entity
)
329 AdgEntityPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(entity
,
333 cairo_matrix_init_identity(&data
->global_map
);
334 cairo_matrix_init_identity(&data
->local_map
);
335 data
->local_mix
= ADG_MIX_ANCESTORS
;
336 data
->hash_styles
= NULL
;
337 data
->global
.is_defined
= FALSE
;
338 adg_matrix_copy(&data
->global
.matrix
, adg_matrix_null());
339 data
->local
.is_defined
= FALSE
;
340 adg_matrix_copy(&data
->local
.matrix
, adg_matrix_null());
341 data
->extents
.is_defined
= FALSE
;
347 _adg_dispose(GObject
*object
)
350 AdgEntityPrivate
*data
;
352 entity
= (AdgEntity
*) object
;
355 /* This call will emit a "notify" signal for parent.
356 * Consequentially, the references to the old parent is dropped. */
357 adg_entity_set_parent(entity
, NULL
);
359 if (data
->hash_styles
!= NULL
) {
360 g_hash_table_destroy(data
->hash_styles
);
361 data
->hash_styles
= NULL
;
364 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
365 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
369 _adg_get_property(GObject
*object
, guint prop_id
,
370 GValue
*value
, GParamSpec
*pspec
)
373 AdgEntityPrivate
*data
;
375 entity
= (AdgEntity
*) object
;
380 g_value_set_object(value
, data
->parent
);
382 case PROP_GLOBAL_MAP
:
383 g_value_set_boxed(value
, &data
->global_map
);
386 g_value_set_boxed(value
, &data
->local_map
);
389 g_value_set_enum(value
, data
->local_mix
);
392 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
398 _adg_set_property(GObject
*object
, guint prop_id
,
399 const GValue
*value
, GParamSpec
*pspec
)
401 AdgEntityPrivate
*data
= ((AdgEntity
*) object
)->data
;
405 _adg_set_parent((AdgEntity
*) object
,
406 (AdgEntity
*) g_value_get_object(value
));
408 case PROP_GLOBAL_MAP
:
409 adg_matrix_copy(&data
->global_map
, g_value_get_boxed(value
));
410 data
->global
.is_defined
= FALSE
;
413 adg_matrix_copy(&data
->local_map
, g_value_get_boxed(value
));
414 data
->local
.is_defined
= FALSE
;
417 data
->local_mix
= g_value_get_enum(value
);
418 g_signal_emit(object
, _adg_signals
[LOCAL_CHANGED
], 0);
421 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
428 * adg_switch_extents:
429 * @state: new extents state
431 * Strokes (if @state is %TRUE) a rectangle around every entity to
432 * show their extents. Useful for debugging purposes.
437 adg_switch_extents(gboolean state
)
439 _adg_show_extents
= state
;
443 * adg_entity_destroy:
444 * @entity: an #AdgEntity
446 * Emits the #AdgEntity::destroy signal on @entity and on all of
447 * its children, if any.
452 adg_entity_destroy(AdgEntity
*entity
)
454 g_return_if_fail(ADG_IS_ENTITY(entity
));
456 g_signal_emit(entity
, _adg_signals
[DESTROY
], 0);
460 * adg_entity_get_canvas:
461 * @entity: an #AdgEntity
463 * Walks on the @entity hierarchy and gets the first parent of @entity,
464 * that is the first #AdgCanvas instance. The returned object is
465 * owned by @entity and should not be freed or modified.
467 * Returns: (transfer none): the requested canvas or %NULL on errors
468 * or if there is no #AdgCanvas in the
474 adg_entity_get_canvas(AdgEntity
*entity
)
476 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
479 if (ADG_IS_CANVAS(entity
))
480 return (AdgCanvas
*) entity
;
482 entity
= adg_entity_get_parent(entity
);
489 * adg_entity_set_parent:
490 * @entity: an #AdgEntity
491 * @parent: the parent entity
494 * This function is only useful in entity implementations.
497 * Sets a new parent on @entity.
502 adg_entity_set_parent(AdgEntity
*entity
, AdgEntity
*parent
)
504 g_return_if_fail(ADG_IS_ENTITY(entity
));
505 g_object_set(entity
, "parent", parent
, NULL
);
509 * adg_entity_get_parent:
510 * @entity: an #AdgEntity
512 * Gets the parent of @entity. The returned object is owned
513 * by @entity and should not be freed or modified.
515 * Returns: (transfer none): the parent entity or %NULL on errors
516 * or if @entity is a toplevel.
521 adg_entity_get_parent(AdgEntity
*entity
)
523 AdgEntityPrivate
*data
;
525 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
533 * adg_entity_set_global_map:
534 * @entity: an #AdgEntity object
537 * Sets the new global transformation of @entity to @map:
538 * the old map is discarded. If @map is %NULL, the global
539 * map is left unchanged.
544 adg_entity_set_global_map(AdgEntity
*entity
, const cairo_matrix_t
*map
)
546 g_return_if_fail(ADG_IS_ENTITY(entity
));
547 g_object_set(entity
, "global-map", map
, NULL
);
551 * adg_entity_transform_global_map:
552 * @entity: an #AdgEntity object
553 * @transformation: the transformation to apply
554 * @mode: how @transformation should be applied
556 * Convenient function to change the global map of @entity by
557 * applying @tranformation using the @mode operator. This is
558 * logically equivalent to the following:
561 * cairo_matrix_t map;
562 * adg_matrix_copy(&map, adg_entity_get_global_map(entity));
563 * adg_matrix_transform(&map, transformation, mode);
564 * adg_entity_set_global_map(entity, &map);
570 adg_entity_transform_global_map(AdgEntity
*entity
,
571 const cairo_matrix_t
*transformation
,
572 AdgTransformMode mode
)
574 AdgEntityPrivate
*data
;
577 g_return_if_fail(ADG_IS_ENTITY(entity
));
578 g_return_if_fail(transformation
!= NULL
);
582 adg_matrix_copy(&map
, &data
->global_map
);
583 adg_matrix_transform(&map
, transformation
, mode
);
585 g_object_set(entity
, "global-map", &map
, NULL
);
589 * adg_entity_get_global_map:
590 * @entity: an #AdgEntity object
592 * Gets the transformation to be used to compute the global matrix
595 * Returns: the requested map or %NULL on errors
599 const cairo_matrix_t
*
600 adg_entity_get_global_map(AdgEntity
*entity
)
602 AdgEntityPrivate
*data
;
604 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
608 return &data
->global_map
;
612 * adg_entity_get_global_matrix:
613 * @entity: an #AdgEntity object
615 * Gets the current global matrix of @entity. The returned value
616 * is owned by @entity and should not be changed or freed.
618 * The global matrix is computed in the arrange() phase by
619 * combining all the global maps of the @entity hierarchy using
620 * the %ADG_MIX_ANCESTORS method.
622 * Returns: the global matrix or %NULL on errors
626 const cairo_matrix_t
*
627 adg_entity_get_global_matrix(AdgEntity
*entity
)
629 AdgEntityPrivate
*data
;
631 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
635 return &data
->global
.matrix
;
639 * adg_entity_set_local_map:
640 * @entity: an #AdgEntity object
643 * Sets the new local transformation of @entity to @map:
644 * the old map is discarded. If @map is %NULL, the local
645 * map is left unchanged.
650 adg_entity_set_local_map(AdgEntity
*entity
, const cairo_matrix_t
*map
)
652 g_return_if_fail(ADG_IS_ENTITY(entity
));
653 g_object_set(entity
, "local-map", map
, NULL
);
657 * adg_entity_transform_local_map:
658 * @entity: an #AdgEntity object
659 * @transformation: the transformation to apply
660 * @mode: how @transformation should be applied
662 * Convenient function to change the local map of @entity by
663 * applying @tranformation using the @mode operator. This is
664 * logically equivalent to the following:
667 * cairo_matrix_t map;
668 * adg_matrix_copy(&map, adg_entity_get_local_map(entity));
669 * adg_matrix_transform(&map, transformation, mode);
670 * adg_entity_set_local_map(entity, &map);
676 adg_entity_transform_local_map(AdgEntity
*entity
,
677 const cairo_matrix_t
*transformation
,
678 AdgTransformMode mode
)
680 AdgEntityPrivate
*data
;
683 g_return_if_fail(ADG_IS_ENTITY(entity
));
684 g_return_if_fail(transformation
!= NULL
);
688 adg_matrix_copy(&map
, &data
->local_map
);
689 adg_matrix_transform(&map
, transformation
, mode
);
690 g_object_set(entity
, "local-map", &map
, NULL
);
694 * adg_entity_get_local_map:
695 * @entity: an #AdgEntity object
697 * Gets the transformation to be used to compute the local matrix
698 * of @entity and store it in @map.
700 * Returns: the requested map or %NULL on errors
704 const cairo_matrix_t
*
705 adg_entity_get_local_map(AdgEntity
*entity
)
707 AdgEntityPrivate
*data
;
709 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
713 return &data
->local_map
;
717 * adg_entity_get_local_matrix:
718 * @entity: an #AdgEntity object
720 * Gets the current local matrix of @entity. The returned value
721 * is owned by @entity and should not be changed or freed.
723 * The local matrix is computed in the arrange() phase by
724 * combining all the local maps of the @entity hierarchy using
725 * the method specified by the #AdgEntity:local-mix property.
727 * Returns: the local matrix or %NULL on errors
731 const cairo_matrix_t
*
732 adg_entity_get_local_matrix(AdgEntity
*entity
)
734 AdgEntityPrivate
*data
;
736 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
740 return &data
->local
.matrix
;
744 * adg_entity_set_local_mix:
745 * @entity: an #AdgEntity object
746 * @local_mix: new mix method
748 * Sets a new local mix method on @entity. The
749 * #AdgEntity:local-mix property defines how the local
750 * matrix must be computed: check out the #AdgMix
751 * documentation to know what are the availables methods
752 * and how they affect the local matrix computation.
754 * Setting a different local mix method emits an
755 * #Adgentity::local-changed signal on @entity.
760 adg_entity_set_local_mix(AdgEntity
*entity
, AdgMix local_mix
)
762 g_return_if_fail(ADG_IS_ENTITY(entity
));
763 g_object_set(entity
, "local-mix", local_mix
, NULL
);
767 * adg_entity_get_local_mix:
768 * @entity: an #AdgEntity object
770 * Gets the local mix method of @entity. Check out the
771 * adg_entity_set_local_mix() documentation to know what the
772 * local mix method is used for.
774 * Returns: the local mix method of @entity or %ADG_MIX_UNDEFINED on errors
779 adg_entity_get_local_mix(AdgEntity
*entity
)
781 AdgEntityPrivate
*data
;
783 g_return_val_if_fail(ADG_IS_ENTITY(entity
), ADG_MIX_UNDEFINED
);
787 return data
->local_mix
;
791 * adg_entity_set_extents:
792 * @entity: an #AdgEntity
793 * @extents: the new extents
796 * This function is only useful in entity implementations.
799 * Sets a new bounding box for @entity. @extents can be %NULL,
800 * in which case the extents are unset.
805 adg_entity_set_extents(AdgEntity
*entity
, const CpmlExtents
*extents
)
807 AdgEntityPrivate
*data
;
809 g_return_if_fail(ADG_IS_ENTITY(entity
));
814 data
->extents
.is_defined
= FALSE
;
816 cpml_extents_copy(&data
->extents
, extents
);
820 * adg_entity_get_extents:
821 * @entity: an #AdgEntity
823 * Gets the bounding box of @entity. The returned struct is
824 * owned by @entity and should not modified or freed.
826 * This struct specifies the surface portion (in global space
827 * of @entity) occupied by the entity without taking into
828 * account rendering properties such as line thickness or caps.
830 * The #AdgEntity::arrange signal should be emitted before
831 * this call (either explicitely trought adg_entity_arrange()
832 * or implicitely with adg_entity_render()) in order to get
833 * an up to date boundary box.
835 * Returns: the bounding box of @entity or %NULL on errors
840 adg_entity_get_extents(AdgEntity
*entity
)
842 AdgEntityPrivate
*data
;
844 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
848 return &data
->extents
;
852 * adg_entity_set_style:
853 * @entity: an #AdgEntity
854 * @dress: a dress style
855 * @style: the new style to use
857 * Overrides the style of @dress for @entity and its children.
858 * If @style is %NULL, any previous override is removed.
860 * The new style must still be compatible with @dress: check out
861 * the adg_dress_style_is_compatible() documentation to know
862 * what a compatible style means.
867 adg_entity_set_style(AdgEntity
*entity
, AdgDress dress
, AdgStyle
*style
)
869 AdgEntityPrivate
*data
;
873 g_return_if_fail(ADG_IS_ENTITY(entity
));
877 if (data
->hash_styles
== NULL
&& style
== NULL
)
880 if (data
->hash_styles
== NULL
)
881 data
->hash_styles
= g_hash_table_new_full(NULL
, NULL
,
882 NULL
, g_object_unref
);
884 p_dress
= GINT_TO_POINTER(dress
);
885 old_style
= g_hash_table_lookup(data
->hash_styles
, p_dress
);
887 if (style
== old_style
)
891 g_hash_table_remove(data
->hash_styles
, p_dress
);
895 if (!adg_dress_style_is_compatible(dress
, style
)) {
896 GType ancestor_type
= adg_dress_get_ancestor_type(dress
);
898 g_warning(_("%s: `%s' is not compatible with `%s' for `%s' dress"),
899 G_STRLOC
, g_type_name(G_TYPE_FROM_INSTANCE(style
)),
900 g_type_name(ancestor_type
), adg_dress_get_name(dress
));
906 g_hash_table_replace(data
->hash_styles
, p_dress
, style
);
910 * adg_entity_get_style:
911 * @entity: an #AdgEntity
912 * @dress: the dress of the style to get
914 * Gets the overriden @dress style from @entity. This is a kind
915 * of accessor function: for rendering purpose use adg_entity_style()
916 * instead. The returned object is owned by @entity and should not be
919 * Returns: (transfer none): the requested style or %NULL
920 * if the @dress style is not overriden
925 adg_entity_get_style(AdgEntity
*entity
, AdgDress dress
)
927 AdgEntityPrivate
*data
;
929 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
933 if (data
->hash_styles
== NULL
)
936 return g_hash_table_lookup(data
->hash_styles
, GINT_TO_POINTER(dress
));
941 * @entity: an #AdgEntity
942 * @dress: the dress of the style to get
944 * Gets the style to be used for @entity. @dress specifies which
945 * "family" of style to get.
947 * The following sequence of checks is performed to get the proper
948 * style, stopping at the first succesfull result:
951 * <listitem>check if the style is directly overriden by this entity,
952 * as returned by adg_entity_get_style();</listitem>
953 * <listitem>check if @entity has a parent, in which case returns the
954 * adg_entity_style() of the parent;</listitem>
955 * <listitem>returns the main style with adg_dress_get_fallback().</listitem>
958 * The returned object is owned by @entity and should not be
961 * Returns: (transfer none): the requested style or %NULL for
962 * transparent dresses or errors.
967 adg_entity_style(AdgEntity
*entity
, AdgDress dress
)
971 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
973 style
= adg_entity_get_style(entity
, dress
);
976 AdgEntityPrivate
*data
= entity
->data
;
978 if (data
->parent
!= NULL
)
979 style
= adg_entity_style(data
->parent
, dress
);
981 style
= adg_dress_get_fallback(dress
);
988 * adg_entity_apply_dress:
989 * @entity: an #AdgEntity
990 * @dress: the dress style to apply
991 * @cr: a #cairo_t drawing context
993 * Convenient function to apply a @dress style (as returned by
994 * adg_entity_style()) to the @cr cairo context.
999 adg_entity_apply_dress(AdgEntity
*entity
, AdgDress dress
, cairo_t
*cr
)
1003 g_return_if_fail(ADG_IS_ENTITY(entity
));
1004 g_return_if_fail(cr
!= NULL
);
1006 style
= adg_entity_style(entity
, dress
);
1009 adg_style_apply(style
, entity
, cr
);
1013 * adg_entity_global_changed:
1014 * @entity: an #AdgEntity
1016 * Emits the #AdgEntity::global-changed signal on @entity and on all of
1017 * its children, if any.
1022 adg_entity_global_changed(AdgEntity
*entity
)
1024 g_return_if_fail(ADG_IS_ENTITY(entity
));
1026 g_signal_emit(entity
, _adg_signals
[GLOBAL_CHANGED
], 0);
1030 * adg_entity_local_changed:
1031 * @entity: an #AdgEntity
1033 * Emits the #AdgEntity::local-changed signal on @entity and on all of
1034 * its children, if any.
1039 adg_entity_local_changed(AdgEntity
*entity
)
1041 g_return_if_fail(ADG_IS_ENTITY(entity
));
1043 g_signal_emit(entity
, _adg_signals
[LOCAL_CHANGED
], 0);
1047 * adg_entity_invalidate:
1048 * @entity: an #AdgEntity
1050 * Emits the #AdgEntity::invalidate signal on @entity and on all of
1051 * its children, if any, clearing the eventual cache stored by the
1052 * #AdgEntity::arrange signal and setting the entity state similary
1053 * to the just initialized entity.
1058 adg_entity_invalidate(AdgEntity
*entity
)
1060 g_return_if_fail(ADG_IS_ENTITY(entity
));
1062 g_signal_emit(entity
, _adg_signals
[INVALIDATE
], 0);
1066 * adg_entity_arrange:
1067 * @entity: an #AdgEntity
1069 * Emits the #AdgEntity::arrange signal on @entity and all its children,
1070 * if any. The arrange call is implicitely called by the
1071 * #AdgEntity::render signal but not by adg_entity_get_extents().
1076 adg_entity_arrange(AdgEntity
*entity
)
1078 g_return_if_fail(ADG_IS_ENTITY(entity
));
1080 g_signal_emit(entity
, _adg_signals
[ARRANGE
], 0);
1084 * adg_entity_render:
1085 * @entity: an #AdgEntity
1086 * @cr: a #cairo_t drawing context
1088 * Emits the #AdgEntity::render signal on @entity and on all of its
1089 * children, if any, causing the rendering to the @cr cairo context.
1094 adg_entity_render(AdgEntity
*entity
, cairo_t
*cr
)
1096 g_return_if_fail(ADG_IS_ENTITY(entity
));
1098 g_signal_emit(entity
, _adg_signals
[RENDER
], 0, cr
);
1103 * @entity: an #AdgEntity
1104 * @point: the #AdgPoint to define
1105 * @new_point: the new #AdgPoint value
1108 * This function is only useful in entity implementations.
1111 * A convenient method to set an #AdgPoint owned by @entity.
1112 * @old_point is the old value while @new_point is the new value.
1113 * It can be used for changing a private #AdgPoint struct, such as:
1116 * data->point = adg_entity_point(entity, data->point, new_point);
1119 * This function takes care of the dependencies between @entity and
1120 * the eventual models bound to the old and new points.
1122 * @old_point can be %NULL, in which case a clone of @new_point will
1123 * be returned. Also @new_point can be %NULL, in which case @old_point
1124 * is destroyed and %NULL will be returned.
1126 * Returns: (transfer full): the new properly defined point
1131 adg_entity_point(AdgEntity
*entity
, AdgPoint
*old_point
, const AdgPoint
*new_point
)
1135 g_return_val_if_fail(ADG_IS_ENTITY(entity
), NULL
);
1139 if (! adg_point_equal(old_point
, new_point
)) {
1140 AdgModel
*old_model
, *new_model
;
1142 old_model
= old_point
!= NULL
? adg_point_get_model(old_point
) : NULL
;
1143 new_model
= new_point
!= NULL
? adg_point_get_model(new_point
) : NULL
;
1145 if (new_model
!= old_model
) {
1146 /* Handle model-entity dependencies */
1147 if (new_model
!= NULL
)
1148 adg_model_add_dependency(new_model
, entity
);
1149 if (old_model
!= NULL
)
1150 adg_model_remove_dependency(old_model
, entity
);
1153 if (new_point
!= NULL
)
1154 point
= adg_point_dup(new_point
);
1155 if (old_point
!= NULL
)
1156 adg_point_destroy(old_point
);
1164 _adg_set_parent(AdgEntity
*entity
, AdgEntity
*parent
)
1166 AdgEntityPrivate
*data
;
1167 AdgEntity
*old_parent
;
1169 data
= entity
->data
;
1170 old_parent
= data
->parent
;
1173 g_object_ref(parent
);
1175 data
->parent
= parent
;
1176 data
->global
.is_defined
= FALSE
;
1177 data
->local
.is_defined
= FALSE
;
1179 g_signal_emit(entity
, _adg_signals
[PARENT_SET
], 0, old_parent
);
1182 g_object_unref(old_parent
);
1186 _adg_global_changed(AdgEntity
*entity
)
1188 AdgEntityPrivate
*data
;
1189 const cairo_matrix_t
*map
;
1190 cairo_matrix_t
*matrix
;
1192 data
= entity
->data
;
1193 map
= &data
->global_map
;
1194 matrix
= &data
->global
.matrix
;
1197 adg_matrix_copy(matrix
, adg_entity_get_global_matrix(data
->parent
));
1198 adg_matrix_transform(matrix
, map
, ADG_TRANSFORM_BEFORE
);
1200 adg_matrix_copy(matrix
, map
);
1205 _adg_local_changed(AdgEntity
*entity
)
1207 AdgEntityPrivate
*data
;
1208 const cairo_matrix_t
*map
;
1209 cairo_matrix_t
*matrix
;
1211 data
= entity
->data
;
1212 map
= &data
->local_map
;
1213 matrix
= &data
->local
.matrix
;
1215 switch (data
->local_mix
) {
1216 case ADG_MIX_DISABLED
:
1217 adg_matrix_copy(matrix
, adg_matrix_identity());
1220 adg_matrix_copy(matrix
, map
);
1222 case ADG_MIX_ANCESTORS
:
1224 adg_matrix_copy(matrix
, adg_entity_get_local_matrix(data
->parent
));
1225 adg_matrix_transform(matrix
, map
, ADG_TRANSFORM_BEFORE
);
1227 adg_matrix_copy(matrix
, map
);
1230 case ADG_MIX_ANCESTORS_NORMALIZED
:
1232 adg_matrix_copy(matrix
, adg_entity_get_local_matrix(data
->parent
));
1233 adg_matrix_transform(matrix
, map
, ADG_TRANSFORM_BEFORE
);
1235 adg_matrix_copy(matrix
, map
);
1237 adg_matrix_normalize(matrix
);
1239 case ADG_MIX_PARENT
:
1241 adg_matrix_copy(matrix
, adg_entity_get_local_map(data
->parent
));
1242 adg_matrix_transform(matrix
, map
, ADG_TRANSFORM_BEFORE
);
1244 adg_matrix_copy(matrix
, map
);
1247 case ADG_MIX_PARENT_NORMALIZED
:
1249 adg_matrix_copy(matrix
, adg_entity_get_local_map(data
->parent
));
1250 adg_matrix_transform(matrix
, map
, ADG_TRANSFORM_BEFORE
);
1252 adg_matrix_copy(matrix
, map
);
1254 adg_matrix_normalize(matrix
);
1256 case ADG_MIX_UNDEFINED
:
1257 g_warning(_("%s: requested to mix the maps using an undefined mix method"),
1261 g_return_if_reached();
1267 _adg_real_invalidate(AdgEntity
*entity
)
1269 AdgEntityClass
*klass
= ADG_ENTITY_GET_CLASS(entity
);
1270 AdgEntityPrivate
*data
= entity
->data
;
1272 /* Do not raise any warning if invalidate() is not defined,
1273 * assuming entity does not have additional cache to be cleared */
1274 if (klass
->invalidate
)
1275 klass
->invalidate(entity
);
1277 data
->extents
.is_defined
= FALSE
;
1281 _adg_real_arrange(AdgEntity
*entity
)
1283 AdgEntityClass
*klass
;
1284 AdgEntityPrivate
*data
;
1286 klass
= ADG_ENTITY_GET_CLASS(entity
);
1287 data
= entity
->data
;
1289 /* Update the global matrix, if required */
1290 if (!data
->global
.is_defined
) {
1291 data
->global
.is_defined
= TRUE
;
1292 g_signal_emit(entity
, _adg_signals
[GLOBAL_CHANGED
], 0);
1295 /* Update the local matrix, if required */
1296 if (!data
->local
.is_defined
) {
1297 data
->local
.is_defined
= TRUE
;
1298 g_signal_emit(entity
, _adg_signals
[LOCAL_CHANGED
], 0);
1301 /* The arrange() method must be defined */
1302 if (klass
->arrange
== NULL
) {
1303 g_warning(_("%s: `arrange' method not implemented for type `%s'"),
1304 G_STRLOC
, g_type_name(G_OBJECT_TYPE(entity
)));
1305 data
->extents
.is_defined
= FALSE
;
1309 klass
->arrange(entity
);
1313 _adg_real_render(AdgEntity
*entity
, cairo_t
*cr
)
1315 AdgEntityClass
*klass
= ADG_ENTITY_GET_CLASS(entity
);
1317 /* The render method must be defined */
1318 if (klass
->render
== NULL
) {
1319 g_warning(_("%s: `render' method not implemented for type `%s'"),
1320 G_STRLOC
, g_type_name(G_OBJECT_TYPE(entity
)));
1324 /* Before the rendering, the entity should be arranged */
1325 g_signal_emit(entity
, _adg_signals
[ARRANGE
], 0);
1328 klass
->render(entity
, cr
);
1331 if (_adg_show_extents
) {
1332 AdgEntityPrivate
*data
= entity
->data
;
1333 CpmlExtents
*extents
= &data
->extents
;
1335 if (extents
->is_defined
) {
1337 cairo_set_source_rgba(cr
, 0.15, 0.15, 0.15, 0.15);
1338 cairo_rectangle(cr
, extents
->org
.x
, extents
->org
.y
,
1339 extents
->size
.x
, extents
->size
.y
);