[AdgPath] Implemented adg_path_chamfer()
[adg.git] / adg / adg-container.c
blob04da195abbbf2f8c1c0c22cdce08f81077895142
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:container
23 * @title: AdgContainer
24 * @short_description: Base class for entity that can contain other entities
26 * The #AdgContainer is an entity that can contains more sub-entities.
27 * Each AdgContainer has its paper matrix (#AdgContainer:paper_matrix) to be
28 * used on paper-dependent references (such as font and arrow sizes, line
29 * thickness etc...) and a model matrix usually applied to the model view. See
30 * http://adg.entidi.com/tutorial/view/3 for details on this topic.
32 * This means an AdgContainer can be thought as a group of entities with the
33 * same geometrical identity (same scale, reference point ecc...).
36 #include "adg-container.h"
37 #include "adg-container-private.h"
38 #include "adg-intl.h"
41 enum {
42 PROP_0,
43 PROP_CHILD,
44 PROP_MODEL_TRANSFORMATION,
45 PROP_PAPER_TRANSFORMATION
48 enum {
49 ADD,
50 REMOVE,
51 LAST_SIGNAL
55 static void get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint prop_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void dispose (GObject *object);
64 static const AdgMatrix *get_model_matrix (AdgEntity *entity);
65 static const AdgMatrix *get_paper_matrix (AdgEntity *entity);
66 static void model_matrix_changed (AdgEntity *entity,
67 AdgMatrix *parent_matrix);
68 static void paper_matrix_changed (AdgEntity *entity,
69 AdgMatrix *parent_matrix);
70 static GSList * get_children (AdgContainer *container);
71 static gboolean add (AdgContainer *container,
72 AdgEntity *entity);
73 static void real_add (AdgContainer *container,
74 AdgEntity *entity,
75 gpointer user_data);
76 static gboolean remove (AdgContainer *container,
77 AdgEntity *entity);
78 static void real_remove (AdgContainer *container,
79 AdgEntity *entity,
80 gpointer user_data);
81 static void invalidate (AdgEntity *entity);
82 static void render (AdgEntity *entity,
83 cairo_t *cr);
85 static guint signals[LAST_SIGNAL] = { 0 };
88 G_DEFINE_TYPE(AdgContainer, adg_container, ADG_TYPE_ENTITY)
91 static void
92 adg_container_class_init(AdgContainerClass *klass)
94 GObjectClass *gobject_class;
95 AdgEntityClass *entity_class;
96 GParamSpec *param;
97 GClosure *closure;
98 GType param_types[1];
100 gobject_class = (GObjectClass *) klass;
101 entity_class = (AdgEntityClass *) klass;
103 g_type_class_add_private(klass, sizeof(AdgContainerPrivate));
105 gobject_class->get_property = get_property;
106 gobject_class->set_property = set_property;
107 gobject_class->dispose = dispose;
109 entity_class->model_matrix_changed = model_matrix_changed;
110 entity_class->paper_matrix_changed = paper_matrix_changed;
111 entity_class->get_model_matrix = get_model_matrix;
112 entity_class->get_paper_matrix = get_paper_matrix;
113 entity_class->invalidate = invalidate;
114 entity_class->render = render;
116 klass->get_children = get_children;
117 klass->add = add;
118 klass->remove = remove;
120 param = g_param_spec_boxed("child",
121 P_("Child"),
122 P_("Can be used to add a new child to the container"),
123 ADG_TYPE_ENTITY, G_PARAM_WRITABLE);
124 g_object_class_install_property(gobject_class, PROP_CHILD, param);
126 param = g_param_spec_boxed("model-transformation",
127 P_("The model transformation"),
128 P_("The model transformation to be applied to this container and its children entities"),
129 ADG_TYPE_MATRIX, G_PARAM_READWRITE);
130 g_object_class_install_property(gobject_class,
131 PROP_MODEL_TRANSFORMATION, param);
133 param = g_param_spec_boxed("paper-transformation",
134 P_("The paper transformation"),
135 P_("The paper transformation to be applied to this container and its children entities"),
136 ADG_TYPE_MATRIX, G_PARAM_READWRITE);
137 g_object_class_install_property(gobject_class,
138 PROP_PAPER_TRANSFORMATION, param);
141 * AdgContainer::add:
142 * @container: an #AdgContainer
143 * @entity: the #AdgEntity to add
145 * Adds @entity to @container.
147 closure = g_cclosure_new(G_CALLBACK(real_add), (gpointer)0xdeadbeaf, NULL);
148 param_types[0] = G_TYPE_OBJECT;
149 signals[ADD] = g_signal_newv("add", ADG_TYPE_CONTAINER,
150 G_SIGNAL_RUN_FIRST, closure, NULL, NULL,
151 g_cclosure_marshal_VOID__OBJECT,
152 G_TYPE_NONE, 1, param_types);
155 * AdgContainer::remove:
156 * @container: an #AdgContainer
157 * @entity: the #AdgEntity to remove
159 * Removes @entity from @container.
161 closure = g_cclosure_new(G_CALLBACK(real_remove), (gpointer)0xdeadbeaf, NULL);
162 param_types[0] = G_TYPE_OBJECT;
163 signals[REMOVE] = g_signal_newv("remove", ADG_TYPE_CONTAINER,
164 G_SIGNAL_RUN_FIRST, closure, NULL, NULL,
165 g_cclosure_marshal_VOID__OBJECT,
166 G_TYPE_NONE, 1, param_types);
169 static void
170 adg_container_init(AdgContainer *container)
172 AdgContainerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(container,
173 ADG_TYPE_CONTAINER,
174 AdgContainerPrivate);
176 priv->children = NULL;
177 cairo_matrix_init_identity(&priv->model_transformation);
178 cairo_matrix_init_identity(&priv->paper_transformation);
179 cairo_matrix_init_identity(&priv->model_matrix);
180 cairo_matrix_init_identity(&priv->paper_matrix);
182 container->priv = priv;
185 static void
186 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
188 AdgContainer *container = (AdgContainer *) object;
190 switch (prop_id) {
191 case PROP_MODEL_TRANSFORMATION:
192 g_value_set_boxed(value, &container->priv->model_transformation);
193 break;
194 case PROP_PAPER_TRANSFORMATION:
195 g_value_set_boxed(value, &container->priv->paper_transformation);
196 break;
197 default:
198 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
199 break;
203 static void
204 set_property(GObject *object,
205 guint prop_id, const GValue *value, GParamSpec *pspec)
207 AdgContainer *container = (AdgContainer *) object;
209 switch (prop_id) {
210 case PROP_CHILD:
211 adg_container_add(container, g_value_get_object(value));
212 break;
213 case PROP_MODEL_TRANSFORMATION:
214 adg_matrix_copy(&container->priv->model_transformation,
215 g_value_get_boxed(value));
216 break;
217 case PROP_PAPER_TRANSFORMATION:
218 adg_matrix_copy(&container->priv->paper_transformation,
219 g_value_get_boxed(value));
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
226 static void
227 dispose(GObject *object)
229 GObjectClass *object_class = (GObjectClass *) adg_container_parent_class;
231 adg_container_foreach((AdgContainer *) object,
232 G_CALLBACK(adg_entity_unparent), NULL);
234 if (object_class->dispose != NULL)
235 object_class->dispose(object);
239 static GSList *
240 get_children(AdgContainer *container)
242 return g_slist_copy(container->priv->children);
245 static gboolean
246 add(AdgContainer *container, AdgEntity *entity)
248 container->priv->children = g_slist_append(container->priv->children,
249 entity);
250 return TRUE;
253 static void
254 real_add(AdgContainer *container, AdgEntity *entity, gpointer user_data)
256 AdgContainer *old_parent;
258 g_assert(user_data == (gpointer) 0xdeadbeaf);
260 old_parent = adg_entity_get_parent(entity);
262 if (old_parent != NULL) {
263 g_warning("Attempting to add an object with type %s to a container "
264 "of type %s, but the object is already inside a container "
265 "of type %s.",
266 g_type_name(G_OBJECT_TYPE(entity)),
267 g_type_name(G_OBJECT_TYPE(container)),
268 g_type_name(G_OBJECT_TYPE(old_parent)));
269 return;
272 if (ADG_CONTAINER_GET_CLASS(container)->add(container, entity))
273 adg_entity_set_parent(entity, container);
274 else
275 g_signal_stop_emission(container, signals[ADD], 0);
278 static gboolean
279 remove(AdgContainer *container, AdgEntity *entity)
281 GSList *node = g_slist_find(container->priv->children, entity);
283 if (!node)
284 return FALSE;
286 container->priv->children = g_slist_delete_link(container->priv->children,
287 node);
288 return TRUE;
291 static void
292 real_remove(AdgContainer *container, AdgEntity *entity, gpointer user_data)
294 g_assert(user_data == (gpointer) 0xdeadbeaf);
296 if (ADG_CONTAINER_GET_CLASS(container)->remove(container, entity))
297 adg_entity_unparent(entity);
298 else
299 g_signal_stop_emission(container, signals[REMOVE], 0);
303 static void
304 model_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
306 AdgContainer *container;
307 AdgEntityClass *entity_class;
309 container = (AdgContainer *) entity;
310 entity_class = (AdgEntityClass *) adg_container_parent_class;
312 if (entity_class->model_matrix_changed != NULL)
313 entity_class->model_matrix_changed(entity, parent_matrix);
315 if (parent_matrix)
316 cairo_matrix_multiply(&container->priv->model_matrix,
317 parent_matrix,
318 &container->priv->model_transformation);
319 else
320 adg_matrix_copy(&container->priv->model_matrix,
321 &container->priv->model_transformation);
323 adg_container_propagate_by_name(container, "model-matrix-changed",
324 &container->priv->model_matrix);
327 static void
328 paper_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
330 AdgContainer *container;
331 AdgEntityClass *entity_class;
333 container = (AdgContainer *) entity;
334 entity_class = (AdgEntityClass *) adg_container_parent_class;
336 if (entity_class->paper_matrix_changed != NULL)
337 entity_class->paper_matrix_changed(entity, parent_matrix);
339 if (parent_matrix)
340 cairo_matrix_multiply(&container->priv->paper_matrix,
341 parent_matrix,
342 &container->priv->paper_transformation);
343 else
344 adg_matrix_copy(&container->priv->paper_matrix,
345 &container->priv->paper_transformation);
347 adg_container_propagate_by_name(container, "paper-matrix-changed",
348 &container->priv->paper_matrix);
351 static const AdgMatrix *
352 get_model_matrix(AdgEntity *entity)
354 AdgContainer *container = (AdgContainer *) entity;
356 return &container->priv->model_matrix;
359 static const AdgMatrix *
360 get_paper_matrix(AdgEntity *entity)
362 AdgContainer *container = (AdgContainer *) entity;
364 return &container->priv->paper_matrix;
368 static void
369 invalidate(AdgEntity *entity)
371 AdgEntityClass *entity_class = (AdgEntityClass *) adg_container_parent_class;
373 adg_container_propagate_by_name((AdgContainer *) entity, "invalidate");
375 if (entity_class->invalidate)
376 entity_class->invalidate(entity);
379 static void
380 render(AdgEntity *entity, cairo_t *cr)
382 AdgEntityClass *entity_class = (AdgEntityClass *) adg_container_parent_class;
384 cairo_set_matrix(cr, adg_entity_get_model_matrix(entity));
385 adg_container_propagate_by_name((AdgContainer *) entity, "render", cr);
387 if (entity_class->render)
388 entity_class->render(entity, cr);
393 * adg_container_add:
394 * @container: an #AdgContainer
395 * @entity: an #AdgEntity
397 * Emits a #AdgContainer::add signal on @container passing
398 * @entity as argument.
400 * @entity may be added to only one container at a time; you can't
401 * place the same entity inside two different containers.
403 void
404 adg_container_add(AdgContainer *container, AdgEntity *entity)
406 g_return_if_fail(ADG_IS_CONTAINER(container));
407 g_return_if_fail(ADG_IS_ENTITY(entity));
409 g_signal_emit(container, signals[ADD], 0, entity);
413 * adg_container_remove:
414 * @container: an #AdgContainer
415 * @entity: an #AdgEntity
417 * Emits a #AdgContainer::remove signal on @container passing
418 * @entity as argument. @entity must be inside @container.
420 * Note that @container will own a reference to @entity
421 * and that this may be the last reference held; so removing an
422 * entity from its container can destroy it.
424 * If you want to use @entity again, you need to add a reference
425 * to it, using g_object_ref(), before removing it from @container.
427 * If you don't want to use @entity again, it's usually more
428 * efficient to simply destroy it directly using g_object_unref()
429 * since this will remove it from the container.
431 void
432 adg_container_remove(AdgContainer *container, AdgEntity *entity)
434 g_return_if_fail(ADG_IS_CONTAINER(container));
435 g_return_if_fail(ADG_IS_ENTITY(entity));
437 g_signal_emit(container, signals[REMOVE], 0, entity);
441 * adg_container_get_children:
442 * @container: an #AdgContainer
444 * Gets the children list of @container.
445 * This list must be manually freed when no longer user.
447 * Returns: a newly allocated #GSList or %NULL on error
449 GSList *
450 adg_container_get_children(AdgContainer *container)
452 g_return_val_if_fail(ADG_IS_CONTAINER(container), NULL);
454 return ADG_CONTAINER_GET_CLASS(container)->get_children(container);
458 * adg_container_foreach:
459 * @container: an #AdgContainer
460 * @callback: a callback
461 * @user_data: callback user data
463 * Invokes @callback on each child of @container.
464 * The callback should be declared as:
466 * <code>
467 * void callback(AdgEntity *entity, gpointer user_data);
468 * </code>
470 void
471 adg_container_foreach(AdgContainer *container,
472 GCallback callback, gpointer user_data)
474 GSList *children;
476 g_return_if_fail(ADG_IS_CONTAINER(container));
477 g_return_if_fail(callback != NULL);
479 children = adg_container_get_children (container);
481 while (children) {
482 if (children->data)
483 ((void (*) (gpointer, gpointer)) callback) (children->data, user_data);
485 children = g_slist_delete_link(children, children);
490 * adg_container_propagate:
491 * @container: an #AdgContainer
492 * @signal_id: the signal id
493 * @detail: the detail
494 * @...: parameters to be passed to the signal, followed by a location for
495 * the return value. If the return type of the signal is G_TYPE_NONE,
496 * the return value location can be omitted.
498 * Emits the specified signal to all the children of @container
499 * using g_signal_emit_valist() calls.
501 void
502 adg_container_propagate(AdgContainer *container,
503 guint signal_id, GQuark detail, ...)
505 va_list var_args;
507 va_start(var_args, detail);
508 adg_container_propagate_valist(container, signal_id, detail, var_args);
509 va_end(var_args);
513 * adg_container_propagate_by_name:
514 * @container: an #AdgContainer
515 * @detailed_signal: a string of the form "signal-name::detail".
516 * @...: a list of parameters to be passed to the signal, followed by
517 * a location for the return value. If the return type of the signal
518 * is G_TYPE_NONE, the return value location can be omitted.
520 * Emits the specified signal to all the children of @container
521 * using g_signal_emit_valist() calls.
523 void
524 adg_container_propagate_by_name(AdgContainer *container,
525 const gchar *detailed_signal, ...)
527 guint signal_id;
528 GQuark detail = 0;
529 va_list var_args;
531 if (!g_signal_parse_name(detailed_signal, G_TYPE_FROM_INSTANCE(container),
532 &signal_id, &detail, FALSE)) {
533 g_warning("%s: signal `%s' is invalid for instance `%p'",
534 G_STRLOC, detailed_signal, container);
535 return;
538 va_start(var_args, detailed_signal);
539 adg_container_propagate_valist(container, signal_id, detail, var_args);
540 va_end(var_args);
544 * adg_container_propagate_valist:
545 * @container: an #AdgContainer
546 * @signal_id: the signal id
547 * @detail: the detail
548 * @var_args: a list of parameters to be passed to the signal, followed by a
549 * location for the return value. If the return type of the signal
550 * is G_TYPE_NONE, the return value location can be omitted.
552 * Emits the specified signal to all the children of @container
553 * using g_signal_emit_valist() calls.
555 void
556 adg_container_propagate_valist(AdgContainer *container,
557 guint signal_id, GQuark detail, va_list var_args)
559 GSList *children;
560 va_list var_copy;
562 g_return_if_fail(ADG_IS_CONTAINER(container));
564 children = adg_container_get_children(container);
566 while (children) {
567 if (children->data) {
568 G_VA_COPY(var_copy, var_args);
569 g_signal_emit_valist(children->data, signal_id, detail, var_copy);
572 children = g_slist_delete_link(children, children);
577 * adg_container_get_model_transformation:
578 * @container: an #AdgContainer
580 * Returns the transformation to be combined with the transformations of the
581 * parent hierarchy to get the final matrix to be applied in the model space.
583 * Return value: the model transformation
585 const AdgMatrix *
586 adg_container_get_model_transformation(AdgContainer *container)
588 g_return_val_if_fail(ADG_IS_CONTAINER(container), NULL);
589 return &container->priv->model_transformation;
593 * adg_container_set_model_transformation:
594 * @container: an #AdgContainer
595 * @transformation: the new model transformation
597 * Sets the transformation to be applied in model space.
599 void
600 adg_container_set_model_transformation(AdgContainer *container,
601 const AdgMatrix *transformation)
603 AdgEntity *entity;
604 AdgEntity *parent;
605 const AdgMatrix *parent_matrix;
607 g_return_if_fail(ADG_IS_CONTAINER(container));
608 g_return_if_fail(transformation != NULL);
610 entity = (AdgEntity *) container;
611 parent = (AdgEntity *) adg_entity_get_parent(entity);
612 parent_matrix = parent ? adg_entity_get_model_matrix(parent) : NULL;
614 adg_matrix_copy(&container->priv->model_transformation, transformation);
615 adg_entity_model_matrix_changed(entity, parent_matrix);
619 * adg_container_get_paper_transformation:
620 * @container: an #AdgContainer
622 * Returns the transformation to be combined with the transformations of the
623 * parent hierarchy to get the final matrix to be applied in the paper space.
625 * Return value: the paper transformation
627 const AdgMatrix *
628 adg_container_get_paper_transformation(AdgContainer *container)
630 g_return_val_if_fail(ADG_IS_CONTAINER(container), NULL);
631 return &container->priv->paper_transformation;
635 * adg_container_set_paper_transformation:
636 * @container: an #AdgContainer
637 * @transformation: the new paper transformation
639 * Sets the transformation to be applied in paper space.
641 void
642 adg_container_set_paper_transformation(AdgContainer *container,
643 const AdgMatrix *transformation)
645 AdgEntity *entity;
646 AdgEntity *parent;
647 const AdgMatrix *parent_matrix;
649 g_return_if_fail(ADG_IS_CONTAINER(container));
650 g_return_if_fail(transformation != NULL);
652 entity = (AdgEntity *) container;
653 parent = (AdgEntity *) adg_entity_get_parent(entity);
654 parent_matrix = parent ? adg_entity_get_paper_matrix(parent) : NULL;
656 adg_matrix_copy(&container->priv->paper_transformation, transformation);
657 adg_entity_paper_matrix_changed(entity, parent_matrix);