ADG: corrected g-ir-scanner warnings where possible
[adg.git] / src / adg / adg-container.c
blobc71fcd31a211ecf4a40dc6591c61b4dcaba23edd
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.
21 /**
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.
35 * Since: 1.0
36 **/
38 /**
39 * AdgContainer:
41 * All fields are private and should not be used directly.
42 * Use its public methods instead.
44 * Since: 1.0
45 **/
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)
60 enum {
61 PROP_0,
62 PROP_CHILD
65 enum {
66 ADD,
67 REMOVE,
68 LAST_SIGNAL
72 static void _adg_dispose (GObject *object);
73 static void _adg_set_property (GObject *object,
74 guint prop_id,
75 const GValue *value,
76 GParamSpec *pspec);
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,
84 cairo_t *cr);
85 static GSList * _adg_children (AdgContainer *container);
86 static void _adg_add (AdgContainer *container,
87 AdgEntity *entity);
88 static void _adg_remove (AdgContainer *container,
89 AdgEntity *entity);
90 static void _adg_remove_from_list (gpointer container,
91 GObject *entity);
93 static guint _adg_signals[LAST_SIGNAL] = { 0 };
96 static void
97 adg_container_class_init(AdgContainerClass *klass)
99 GObjectClass *gobject_class;
100 AdgEntityClass *entity_class;
101 GParamSpec *param;
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",
122 P_("Child"),
123 P_("Can be used to add a new child to the container"),
124 ADG_TYPE_ENTITY,
125 G_PARAM_WRITABLE);
126 g_object_class_install_property(gobject_class, PROP_CHILD, param);
129 * AdgContainer::add:
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.
136 * Since: 1.0
138 _adg_signals[ADD] = g_signal_new("add",
139 G_OBJECT_CLASS_TYPE(gobject_class),
140 G_SIGNAL_RUN_FIRST,
141 G_STRUCT_OFFSET(AdgContainerClass, add),
142 NULL, NULL,
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.
153 * Since: 1.0
155 _adg_signals[REMOVE] = g_signal_new("remove",
156 G_OBJECT_CLASS_TYPE(gobject_class),
157 G_SIGNAL_RUN_FIRST,
158 G_STRUCT_OFFSET(AdgContainerClass, remove),
159 NULL, NULL,
160 adg_marshal_VOID__OBJECT,
161 G_TYPE_NONE, 1, ADG_TYPE_ENTITY);
164 static void
165 adg_container_init(AdgContainer *container)
167 AdgContainerPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(container,
168 ADG_TYPE_CONTAINER,
169 AdgContainerPrivate);
171 data->children = NULL;
173 container->data = data;
176 static void
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);
196 static void
197 _adg_set_property(GObject *object,
198 guint prop_id, const GValue *value, GParamSpec *pspec)
200 AdgContainer *container = (AdgContainer *) object;
202 switch (prop_id) {
203 case PROP_CHILD:
204 adg_container_add(container, g_value_get_object(value));
205 break;
206 default:
207 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
213 * adg_container_new:
215 * Creates a new container entity.
217 * Returns: the newly created container entity
219 * Since: 1.0
221 AdgContainer *
222 adg_container_new(void)
224 return g_object_new(ADG_TYPE_CONTAINER, NULL);
229 * adg_container_add:
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().
241 * Since: 1.0
243 void
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>:
270 * |[
271 * g_object_ref(entity);
272 * adg_container_remove(container1, entity);
273 * adg_container_add(container2, entity)
274 * g_object_unref(entity);
275 * ]|
277 * Since: 1.0
279 void
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
296 * to the oldest one.
298 * Returns: (element-type AdgEntity) (transfer container): a newly allocated #GSList of #AdgEntity or %NULL empty list or on errors
300 * Since: 1.0
302 GSList *
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)
312 return 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:
326 * |[
327 * void callback(AdgEntity *entity, gpointer user_data);
328 * ]|
330 * Since: 1.0
332 void
333 adg_container_foreach(AdgContainer *container,
334 GCallback callback, gpointer user_data)
336 GSList *children;
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.
364 * Since: 1.0
366 void
367 adg_container_propagate(AdgContainer *container,
368 guint signal_id, GQuark detail, ...)
370 va_list var_args;
372 va_start(var_args, detail);
373 adg_container_propagate_valist(container, signal_id, detail, var_args);
374 va_end(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.
389 * Since: 1.0
391 void
392 adg_container_propagate_by_name(AdgContainer *container,
393 const gchar *detailed_signal, ...)
395 guint signal_id;
396 GQuark detail = 0;
397 va_list var_args;
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);
403 return;
406 va_start(var_args, detailed_signal);
407 adg_container_propagate_valist(container, signal_id, detail, var_args);
408 va_end(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.
424 * Since: 1.0
426 void
427 adg_container_propagate_valist(AdgContainer *container,
428 guint signal_id, GQuark detail, va_list var_args)
430 GSList *children;
431 va_list var_copy;
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);
448 static void
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");
457 static void
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");
466 static void
467 _adg_invalidate(AdgEntity *entity)
469 adg_container_propagate_by_name((AdgContainer *) entity, "invalidate");
472 static void
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);
483 static void
484 _adg_add_extents(AdgEntity *entity, CpmlExtents *extents)
486 cpml_extents_add(extents, adg_entity_get_extents(entity));
489 static void
490 _adg_render(AdgEntity *entity, cairo_t *cr)
492 adg_container_propagate_by_name((AdgContainer *) entity, "render", cr);
496 static GSList *
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);
505 static void
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 "
515 "of type %s"),
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)));
519 return;
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);
530 static void
531 _adg_remove_from_list(gpointer container, GObject *entity)
533 AdgContainerPrivate *data = ((AdgContainer *) container)->data;
534 data->children = g_slist_remove(data->children, entity);
537 static void
538 _adg_remove(AdgContainer *container, AdgEntity *entity)
540 AdgContainerPrivate *data;
541 GSList *node;
543 data = container->data;
544 node = g_slist_find(data->children, entity);
546 if (node == NULL) {
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)));
551 return;
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);