1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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.
22 * SECTION:adg-container
23 * @short_description: Base class for entity that can contain other entities
25 * The #AdgContainer is an entity that can contains more sub-entities.
26 * Moreover, it can apply a common transformation to local and/or global
27 * maps: see http://adg.entidi.com/tutorial/view/3 for further details.
29 * Adding an entity to a container will make a circular dependency
30 * between the two objects. The container will also add a weak reference
31 * to the child entity to intercept when the entity is manually
32 * destroyed (usually by calling g_object_unref()) and remove the child
33 * reference from the internal children list.
41 * All fields are private and should not be used directly.
42 * Use its public methods instead.
48 #include "adg-internal.h"
50 #include "adg-container.h"
51 #include "adg-container-private.h"
54 #define _ADG_PARENT_OBJECT_CLASS ((GObjectClass *) adg_container_parent_class)
55 #define _ADG_PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_container_parent_class)
58 G_DEFINE_TYPE(AdgContainer
, adg_container
, ADG_TYPE_ENTITY
)
72 static void _adg_dispose (GObject
*object
);
73 static void _adg_set_property (GObject
*object
,
77 static void _adg_global_changed (AdgEntity
*entity
);
78 static void _adg_local_changed (AdgEntity
*entity
);
79 static void _adg_invalidate (AdgEntity
*entity
);
80 static void _adg_arrange (AdgEntity
*entity
);
81 static void _adg_add_extents (AdgEntity
*entity
,
82 CpmlExtents
*extents
);
83 static void _adg_render (AdgEntity
*entity
,
85 static GSList
* _adg_children (AdgContainer
*container
);
86 static void _adg_add (AdgContainer
*container
,
88 static void _adg_remove (AdgContainer
*container
,
90 static void _adg_remove_from_list (gpointer container
,
93 static guint _adg_signals
[LAST_SIGNAL
] = { 0 };
97 adg_container_class_init(AdgContainerClass
*klass
)
99 GObjectClass
*gobject_class
;
100 AdgEntityClass
*entity_class
;
103 gobject_class
= (GObjectClass
*) klass
;
104 entity_class
= (AdgEntityClass
*) klass
;
106 g_type_class_add_private(klass
, sizeof(AdgContainerPrivate
));
108 gobject_class
->dispose
= _adg_dispose
;
109 gobject_class
->set_property
= _adg_set_property
;
111 entity_class
->global_changed
= _adg_global_changed
;
112 entity_class
->local_changed
= _adg_local_changed
;
113 entity_class
->invalidate
= _adg_invalidate
;
114 entity_class
->arrange
= _adg_arrange
;
115 entity_class
->render
= _adg_render
;
117 klass
->children
= _adg_children
;
118 klass
->add
= _adg_add
;
119 klass
->remove
= _adg_remove
;
121 param
= g_param_spec_object("child",
123 P_("Can be used to add a new child to the container"),
126 g_object_class_install_property(gobject_class
, PROP_CHILD
, param
);
130 * @container: an #AdgContainer
131 * @entity: the #AdgEntity to add
133 * Adds @entity to @container. @entity must not be inside another
134 * container or the operation will fail.
138 _adg_signals
[ADD
] = g_signal_new("add",
139 G_OBJECT_CLASS_TYPE(gobject_class
),
141 G_STRUCT_OFFSET(AdgContainerClass
, add
),
143 adg_marshal_VOID__OBJECT
,
144 G_TYPE_NONE
, 1, ADG_TYPE_ENTITY
);
147 * AdgContainer::remove:
148 * @container: an #AdgContainer
149 * @entity: the #AdgEntity to remove
151 * Removes @entity from @container.
155 _adg_signals
[REMOVE
] = g_signal_new("remove",
156 G_OBJECT_CLASS_TYPE(gobject_class
),
158 G_STRUCT_OFFSET(AdgContainerClass
, remove
),
160 adg_marshal_VOID__OBJECT
,
161 G_TYPE_NONE
, 1, ADG_TYPE_ENTITY
);
165 adg_container_init(AdgContainer
*container
)
167 AdgContainerPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(container
,
169 AdgContainerPrivate
);
171 data
->children
= NULL
;
173 container
->data
= data
;
177 _adg_dispose(GObject
*object
)
179 AdgContainer
*container
;
180 AdgContainerPrivate
*data
;
182 container
= (AdgContainer
*) object
;
183 data
= container
->data
;
185 /* Remove all the children from the container: these will emit
186 * a "remove" signal for every child and will drop all the
187 * references from the children to this container (and, obviously,
188 * from the container to the children). */
189 while (data
->children
!= NULL
)
190 adg_container_remove(container
, (AdgEntity
*) data
->children
->data
);
192 if (_ADG_PARENT_OBJECT_CLASS
->dispose
)
193 _ADG_PARENT_OBJECT_CLASS
->dispose(object
);
197 _adg_set_property(GObject
*object
,
198 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
200 AdgContainer
*container
= (AdgContainer
*) object
;
204 adg_container_add(container
, g_value_get_object(value
));
207 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
215 * Creates a new container entity.
217 * Returns: the newly created container entity
222 adg_container_new(void)
224 return g_object_new(ADG_TYPE_CONTAINER
, NULL
);
230 * @container: an #AdgContainer
231 * @entity: an #AdgEntity
233 * Emits a #AdgContainer::add signal on @container passing @entity
234 * as argument. @entity must be added to only one container at a time,
235 * you can't place the same entity inside two different containers.
237 * Once @entity has been added, the floating reference will be removed
238 * and @container will own a reference to @entity. This means the only
239 * proper way to destroy @entity is to call adg_container_remove().
244 adg_container_add(AdgContainer
*container
, AdgEntity
*entity
)
246 g_return_if_fail(ADG_IS_CONTAINER(container
));
247 g_return_if_fail(ADG_IS_ENTITY(entity
));
249 g_signal_emit(container
, _adg_signals
[ADD
], 0, entity
);
253 * adg_container_remove:
254 * @container: an #AdgContainer
255 * @entity: an #AdgEntity
257 * Emits a #AdgContainer::remove signal on @container passing
258 * @entity as argument. @entity must be inside @container.
260 * Note that @container will own a reference to @entity and it
261 * may be the last reference held: this means removing an entity
262 * from its container can destroy it.
264 * If you want to use @entity again, you need to add a reference
265 * to it, using g_object_ref(), before removing it from @container.
266 * The following typical example shows you how to properly move
267 * <varname>entity</varname> from <varname>container1</varname>
268 * to <varname>container2</varname>:
271 * g_object_ref(entity);
272 * adg_container_remove(container1, entity);
273 * adg_container_add(container2, entity)
274 * g_object_unref(entity);
280 adg_container_remove(AdgContainer
*container
, AdgEntity
*entity
)
282 g_return_if_fail(ADG_IS_CONTAINER(container
));
283 g_return_if_fail(ADG_IS_ENTITY(entity
));
285 g_signal_emit(container
, _adg_signals
[REMOVE
], 0, entity
);
289 * adg_container_children:
290 * @container: an #AdgContainer
292 * Gets the children list of @container. This list must be manually
293 * freed with g_slist_free() when no longer user.
295 * The returned list is ordered from the most recently added child
298 * Returns: (element-type AdgEntity) (transfer container): a newly allocated #GSList of #AdgEntity or %NULL empty list or on errors
303 adg_container_children(AdgContainer
*container
)
305 AdgContainerClass
*klass
;
307 g_return_val_if_fail(ADG_IS_CONTAINER(container
), NULL
);
309 klass
= ADG_CONTAINER_GET_CLASS(container
);
311 if (klass
->children
== NULL
)
314 return klass
->children(container
);
318 * adg_container_foreach:
319 * @container: an #AdgContainer
320 * @callback: (scope call): a callback
321 * @user_data: callback user data
323 * Invokes @callback on each child of @container.
324 * The callback should be declared as:
327 * void callback(AdgEntity *entity, gpointer user_data);
333 adg_container_foreach(AdgContainer
*container
,
334 GCallback callback
, gpointer user_data
)
338 g_return_if_fail(ADG_IS_CONTAINER(container
));
339 g_return_if_fail(callback
!= NULL
);
341 children
= adg_container_children(container
);
343 while (children
!= NULL
) {
344 if (children
->data
!= NULL
)
345 ((void (*) (gpointer
, gpointer
)) callback
) (children
->data
, user_data
);
347 children
= g_slist_delete_link(children
, children
);
352 * adg_container_propagate:
353 * @container: an #AdgContainer
354 * @signal_id: the signal id
355 * @detail: the detail
356 * @...: parameters to be passed to the signal, followed by a pointer
357 * to the allocated memory where to store the return type: if
358 * the signal is %G_TYPE_NONE (void return type), this trailing
359 * pointer should be omitted
361 * Emits the specified signal to all the children of @container
362 * using g_signal_emit_valist() calls.
367 adg_container_propagate(AdgContainer
*container
,
368 guint signal_id
, GQuark detail
, ...)
372 va_start(var_args
, detail
);
373 adg_container_propagate_valist(container
, signal_id
, detail
, var_args
);
378 * adg_container_propagate_by_name:
379 * @container: an #AdgContainer
380 * @detailed_signal: a string of the form "signal-name::detail".
381 * @...: parameters to be passed to the signal, followed by a pointer
382 * to the allocated memory where to store the return type: if
383 * the signal is %G_TYPE_NONE (void return type), this trailing
384 * pointer should be omitted
386 * Emits the specified signal to all the children of @container
387 * using g_signal_emit_valist() calls.
392 adg_container_propagate_by_name(AdgContainer
*container
,
393 const gchar
*detailed_signal
, ...)
399 if (!g_signal_parse_name(detailed_signal
, G_TYPE_FROM_INSTANCE(container
),
400 &signal_id
, &detail
, FALSE
)) {
401 g_warning(_("%s: signal `%s' is invalid for instance `%p'"),
402 G_STRLOC
, detailed_signal
, container
);
406 va_start(var_args
, detailed_signal
);
407 adg_container_propagate_valist(container
, signal_id
, detail
, var_args
);
412 * adg_container_propagate_valist:
413 * @container: an #AdgContainer
414 * @signal_id: the signal id
415 * @detail: the detail
416 * @var_args: parameters to be passed to the signal, followed by a
417 * pointer to the allocated memory where to store the
418 * return type: if the signal is %G_TYPE_NONE (void return
419 * type), this trailing pointer should be omitted
421 * Emits the specified signal to all the children of @container
422 * using g_signal_emit_valist() calls.
427 adg_container_propagate_valist(AdgContainer
*container
,
428 guint signal_id
, GQuark detail
, va_list var_args
)
433 g_return_if_fail(ADG_IS_CONTAINER(container
));
435 children
= adg_container_children(container
);
437 while (children
!= NULL
) {
438 if (children
->data
!= NULL
) {
439 G_VA_COPY(var_copy
, var_args
);
440 g_signal_emit_valist(children
->data
, signal_id
, detail
, var_copy
);
443 children
= g_slist_delete_link(children
, children
);
449 _adg_global_changed(AdgEntity
*entity
)
451 if (_ADG_PARENT_ENTITY_CLASS
->global_changed
)
452 _ADG_PARENT_ENTITY_CLASS
->global_changed(entity
);
454 adg_container_propagate_by_name((AdgContainer
*) entity
, "global-changed");
458 _adg_local_changed(AdgEntity
*entity
)
460 if (_ADG_PARENT_ENTITY_CLASS
->local_changed
)
461 _ADG_PARENT_ENTITY_CLASS
->local_changed(entity
);
463 adg_container_propagate_by_name((AdgContainer
*) entity
, "local-changed");
467 _adg_invalidate(AdgEntity
*entity
)
469 adg_container_propagate_by_name((AdgContainer
*) entity
, "invalidate");
473 _adg_arrange(AdgEntity
*entity
)
475 AdgContainer
*container
= (AdgContainer
*) entity
;
476 CpmlExtents extents
= { 0 };
478 adg_container_propagate_by_name(container
, "arrange", NULL
);
479 adg_container_foreach(container
, G_CALLBACK(_adg_add_extents
), &extents
);
480 adg_entity_set_extents(entity
, &extents
);
484 _adg_add_extents(AdgEntity
*entity
, CpmlExtents
*extents
)
486 cpml_extents_add(extents
, adg_entity_get_extents(entity
));
490 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
492 adg_container_propagate_by_name((AdgContainer
*) entity
, "render", cr
);
497 _adg_children(AdgContainer
*container
)
499 AdgContainerPrivate
*data
= container
->data
;
501 /* The NULL case is yet managed by GLib */
502 return g_slist_copy(data
->children
);
506 _adg_add(AdgContainer
*container
, AdgEntity
*entity
)
508 const AdgEntity
*old_parent
;
509 AdgContainerPrivate
*data
;
511 old_parent
= adg_entity_get_parent(entity
);
512 if (old_parent
!= NULL
) {
513 g_warning(_("Attempting to add an entity with type %s to a container "
514 "of type %s, but the entity is already inside a container "
516 g_type_name(G_OBJECT_TYPE(entity
)),
517 g_type_name(G_OBJECT_TYPE(container
)),
518 g_type_name(G_OBJECT_TYPE(old_parent
)));
522 data
= container
->data
;
523 data
->children
= g_slist_prepend(data
->children
, entity
);
525 g_object_ref_sink(entity
);
526 adg_entity_set_parent(entity
, (AdgEntity
*) container
);
527 g_object_weak_ref((GObject
*) entity
, _adg_remove_from_list
, container
);
531 _adg_remove_from_list(gpointer container
, GObject
*entity
)
533 AdgContainerPrivate
*data
= ((AdgContainer
*) container
)->data
;
534 data
->children
= g_slist_remove(data
->children
, entity
);
538 _adg_remove(AdgContainer
*container
, AdgEntity
*entity
)
540 AdgContainerPrivate
*data
;
543 data
= container
->data
;
544 node
= g_slist_find(data
->children
, entity
);
547 g_warning(_("Attempting to remove an entity with type %s from a "
548 "container of type %s, but the entity is not present"),
549 g_type_name(G_OBJECT_TYPE(entity
)),
550 g_type_name(G_OBJECT_TYPE(container
)));
554 g_object_weak_unref((GObject
*) entity
, _adg_remove_from_list
, container
);
555 data
->children
= g_slist_delete_link(data
->children
, node
);
556 adg_entity_set_parent(entity
, NULL
);
557 g_object_unref(entity
);