1 #define __SP_USE_C__
3 /*
4 * SVG <use> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 1999-2005 authors
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #include <cstring>
21 #include <string>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include "libnr/nr-matrix-translate-ops.h"
26 #include <glibmm/i18n.h>
27 #include "display/nr-arena-group.h"
28 #include "attributes.h"
29 #include "document.h"
30 #include "sp-object-repr.h"
31 #include "sp-flowregion.h"
32 #include "uri.h"
33 #include "print.h"
34 #include "xml/repr.h"
35 #include "prefs-utils.h"
36 #include "style.h"
37 #include "sp-symbol.h"
38 #include "sp-use.h"
39 #include "sp-use-reference.h"
41 /* fixme: */
43 static void sp_use_class_init(SPUseClass *classname);
44 static void sp_use_init(SPUse *use);
45 static void sp_use_finalize(GObject *obj);
47 static void sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
48 static void sp_use_release(SPObject *object);
49 static void sp_use_set(SPObject *object, unsigned key, gchar const *value);
50 static Inkscape::XML::Node *sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
51 static void sp_use_update(SPObject *object, SPCtx *ctx, guint flags);
52 static void sp_use_modified(SPObject *object, guint flags);
54 static void sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
55 static void sp_use_snappoints(SPItem const *item, SnapPointsIter p);
56 static void sp_use_print(SPItem *item, SPPrintContext *ctx);
57 static gchar *sp_use_description(SPItem *item);
58 static NRArenaItem *sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
59 static void sp_use_hide(SPItem *item, unsigned key);
61 static void sp_use_href_changed(SPObject *old_ref, SPObject *ref, SPUse *use);
63 static void sp_use_delete_self(SPObject *deleted, SPUse *self);
65 static SPItemClass *parent_class;
67 //void m_print(gchar *say, NR::Matrix m)
68 //{ g_print("%s %g %g %g %g %g %g\n", say, m[0], m[1], m[2], m[3], m[4], m[5]); }
70 GType
71 sp_use_get_type(void)
72 {
73 static GType use_type = 0;
74 if (!use_type) {
75 GTypeInfo use_info = {
76 sizeof(SPUseClass),
77 NULL, /* base_init */
78 NULL, /* base_finalize */
79 (GClassInitFunc) sp_use_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
82 sizeof(SPUse),
83 16, /* n_preallocs */
84 (GInstanceInitFunc) sp_use_init,
85 NULL, /* value_table */
86 };
87 use_type = g_type_register_static(SP_TYPE_ITEM, "SPUse", &use_info, (GTypeFlags)0);
88 }
89 return use_type;
90 }
92 static void
93 sp_use_class_init(SPUseClass *classname)
94 {
95 GObjectClass *gobject_class = (GObjectClass *) classname;
96 SPObjectClass *sp_object_class = (SPObjectClass *) classname;
97 SPItemClass *item_class = (SPItemClass *) classname;
99 parent_class = (SPItemClass*) g_type_class_ref(SP_TYPE_ITEM);
101 gobject_class->finalize = sp_use_finalize;
103 sp_object_class->build = sp_use_build;
104 sp_object_class->release = sp_use_release;
105 sp_object_class->set = sp_use_set;
106 sp_object_class->write = sp_use_write;
107 sp_object_class->update = sp_use_update;
108 sp_object_class->modified = sp_use_modified;
110 item_class->bbox = sp_use_bbox;
111 item_class->description = sp_use_description;
112 item_class->print = sp_use_print;
113 item_class->show = sp_use_show;
114 item_class->hide = sp_use_hide;
115 item_class->snappoints = sp_use_snappoints;
116 }
118 static void
119 sp_use_init(SPUse *use)
120 {
121 use->x.unset();
122 use->y.unset();
123 use->width.unset(SVGLength::PERCENT, 1.0, 1.0);
124 use->height.unset(SVGLength::PERCENT, 1.0, 1.0);
125 use->href = NULL;
127 new (&use->_delete_connection) sigc::connection();
128 new (&use->_changed_connection) sigc::connection();
130 new (&use->_transformed_connection) sigc::connection();
132 use->ref = new SPUseReference(SP_OBJECT(use));
134 use->_changed_connection = use->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_use_href_changed), use));
135 }
137 static void
138 sp_use_finalize(GObject *obj)
139 {
140 SPUse *use = (SPUse *) obj;
142 delete use->ref;
144 use->_delete_connection.~connection();
145 use->_changed_connection.~connection();
147 use->_transformed_connection.~connection();
148 }
150 static void
151 sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
152 {
153 if (((SPObjectClass *) parent_class)->build) {
154 (* ((SPObjectClass *) parent_class)->build)(object, document, repr);
155 }
157 sp_object_read_attr(object, "x");
158 sp_object_read_attr(object, "y");
159 sp_object_read_attr(object, "width");
160 sp_object_read_attr(object, "height");
161 sp_object_read_attr(object, "xlink:href");
163 // We don't need to create child here:
164 // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
165 // which will call sp_use_href_changed, and that will take care of the child
166 }
168 static void
169 sp_use_release(SPObject *object)
170 {
171 SPUse *use = SP_USE(object);
173 use->child = NULL;
175 use->_delete_connection.disconnect();
176 use->_changed_connection.disconnect();
177 use->_transformed_connection.disconnect();
179 g_free(use->href);
180 use->href = NULL;
182 use->ref->detach();
184 if (((SPObjectClass *) parent_class)->release) {
185 ((SPObjectClass *) parent_class)->release(object);
186 }
187 }
189 static void
190 sp_use_set(SPObject *object, unsigned key, gchar const *value)
191 {
192 SPUse *use = SP_USE(object);
194 switch (key) {
195 case SP_ATTR_X:
196 use->x.readOrUnset(value);
197 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
198 break;
199 case SP_ATTR_Y:
200 use->y.readOrUnset(value);
201 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 break;
203 case SP_ATTR_WIDTH:
204 use->width.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
205 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 break;
207 case SP_ATTR_HEIGHT:
208 use->height.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
209 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
210 break;
212 case SP_ATTR_XLINK_HREF: {
213 if ( value && use->href && ( strcmp(value, use->href) == 0 ) ) {
214 /* No change, do nothing. */
215 } else {
216 g_free(use->href);
217 use->href = NULL;
218 if (value) {
219 // First, set the href field, because sp_use_href_changed will need it.
220 use->href = g_strdup(value);
222 // Now do the attaching, which emits the changed signal.
223 try {
224 use->ref->attach(Inkscape::URI(value));
225 } catch (Inkscape::BadURIException &e) {
226 g_warning("%s", e.what());
227 use->ref->detach();
228 }
229 } else {
230 use->ref->detach();
231 }
232 }
233 break;
234 }
236 default:
237 if (((SPObjectClass *) parent_class)->set) {
238 ((SPObjectClass *) parent_class)->set(object, key, value);
239 }
240 break;
241 }
242 }
244 static Inkscape::XML::Node *
245 sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
246 {
247 SPUse *use = SP_USE(object);
249 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
250 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
251 repr = xml_doc->createElement("svg:use");
252 }
254 if (((SPObjectClass *) (parent_class))->write) {
255 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
256 }
258 sp_repr_set_svg_double(repr, "x", use->x.computed);
259 sp_repr_set_svg_double(repr, "y", use->y.computed);
260 sp_repr_set_svg_double(repr, "width", use->width.computed);
261 sp_repr_set_svg_double(repr, "height", use->height.computed);
263 if (use->ref->getURI()) {
264 gchar *uri_string = use->ref->getURI()->toString();
265 repr->setAttribute("xlink:href", uri_string);
266 g_free(uri_string);
267 }
269 return repr;
270 }
272 static void
273 sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
274 {
275 SPUse const *use = SP_USE(item);
277 if (use->child && SP_IS_ITEM(use->child)) {
278 SPItem *child = SP_ITEM(use->child);
279 NR::Matrix const ct( child->transform
280 * NR::translate(use->x.computed,
281 use->y.computed)
282 * transform );
283 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
284 }
285 }
287 static void
288 sp_use_print(SPItem *item, SPPrintContext *ctx)
289 {
290 bool translated = false;
291 NRMatrix tp;
292 SPUse *use = SP_USE(item);
294 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
295 nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
296 sp_print_bind(ctx, &tp, 1.0);
297 translated = true;
298 }
300 if (use->child && SP_IS_ITEM(use->child)) {
301 sp_item_invoke_print(SP_ITEM(use->child), ctx);
302 }
304 if (translated) {
305 sp_print_release(ctx);
306 }
307 }
309 static gchar *
310 sp_use_description(SPItem *item)
311 {
312 SPUse *use = SP_USE(item);
314 char *ret;
315 if (use->child) {
316 static unsigned recursion_depth = 0;
317 if (recursion_depth >= 4) {
318 /* TRANSLATORS: Used for statusbar description for long <use> chains:
319 * "Clone of: Clone of: ... in Layer 1". */
320 return g_strdup(_("..."));
321 /* We could do better, e.g. chasing the href chain until we reach something other than
322 * a <use>, and giving its description. */
323 }
324 ++recursion_depth;
325 char *child_desc = sp_item_description(SP_ITEM(use->child));
326 --recursion_depth;
328 ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
329 g_free(child_desc);
330 return ret;
331 } else {
332 return g_strdup(_("<b>Orphaned clone</b>"));
333 }
334 }
336 static NRArenaItem *
337 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
338 {
339 SPUse *use = SP_USE(item);
341 NRArenaItem *ai = NRArenaGroup::create(arena);
342 nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
343 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
345 if (use->child) {
346 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
347 if (ac) {
348 nr_arena_item_add_child(ai, ac, NULL);
349 nr_arena_item_unref(ac);
350 }
351 NR::translate t(use->x.computed,
352 use->y.computed);
353 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
354 }
356 return ai;
357 }
359 static void
360 sp_use_hide(SPItem *item, unsigned key)
361 {
362 SPUse *use = SP_USE(item);
364 if (use->child) {
365 sp_item_invoke_hide(SP_ITEM(use->child), key);
366 }
368 if (((SPItemClass *) parent_class)->hide) {
369 ((SPItemClass *) parent_class)->hide(item, key);
370 }
371 }
373 /**
374 * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
375 * which is not an SPUse). If no original is found, NULL is returned (it is the responsibility
376 * of the caller to make sure that this is handled correctly).
377 *
378 * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
379 * the trivial case) and not the "true original".
380 */
381 SPItem *
382 sp_use_root(SPUse *use)
383 {
384 SPObject *orig = use->child;
385 while (SP_IS_USE(orig)) {
386 orig = SP_USE(orig)->child;
387 }
388 g_return_val_if_fail(SP_IS_ITEM(orig), NULL);
389 return SP_ITEM(orig);
390 }
392 /**
393 * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
394 * included.
395 */
396 NR::Matrix
397 sp_use_get_root_transform(SPUse *use)
398 {
399 //track the ultimate source of a chain of uses
400 SPObject *orig = use->child;
401 GSList *chain = NULL;
402 chain = g_slist_prepend(chain, use);
403 while (SP_IS_USE(orig)) {
404 chain = g_slist_prepend(chain, orig);
405 orig = SP_USE(orig)->child;
406 }
407 chain = g_slist_prepend(chain, orig);
410 //calculate the accummulated transform, starting from the original
411 NR::Matrix t(NR::identity());
412 for (GSList *i = chain; i != NULL; i = i->next) {
413 SPItem *i_tem = SP_ITEM(i->data);
415 // "An additional transformation translate(x,y) is appended to the end (i.e.,
416 // right-side) of the transform attribute on the generated 'g', where x and y
417 // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
418 if (SP_IS_USE(i_tem)) {
419 SPUse *i_use = SP_USE(i_tem);
420 if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
421 t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
422 }
423 }
425 t *= i_tem->transform;
426 }
428 g_slist_free(chain);
429 return t;
430 }
432 /**
433 * Returns the transform that leads to the use from its immediate original.
434 * Does not inlcude the original's transform if any.
435 */
436 NR::Matrix
437 sp_use_get_parent_transform(SPUse *use)
438 {
439 NR::Matrix t(NR::identity());
440 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
441 t *= NR::translate(use->x._set ? use->x.computed : 0,
442 use->y._set ? use->y.computed : 0);
443 }
445 t *= SP_ITEM(use)->transform;
446 return t;
447 }
449 /**
450 * Sensing a movement of the original, this function attempts to compensate for it in such a way
451 * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
452 * clone's transform.
453 */
454 static void
455 sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
456 {
457 // the clone is orphaned; or this is not a real use, but a clone of another use;
458 // we skip it, otherwise duplicate compensation will occur
459 if (SP_OBJECT_IS_CLONED(self)) {
460 return;
461 }
463 // never compensate uses which are used in flowtext
464 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
465 return;
466 }
468 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
469 // user wants no compensation
470 if (mode == SP_CLONE_COMPENSATION_NONE)
471 return;
473 NR::Matrix m(*mp);
475 // this is not a simple move, do not try to compensate
476 if (!(m.is_translation()))
477 return;
479 // restore item->transform field from the repr, in case it was changed by seltrans
480 sp_object_read_attr (SP_OBJECT (self), "transform");
482 NR::Matrix t = sp_use_get_parent_transform(self);
483 NR::Matrix clone_move = t.inverse() * m * t;
485 // calculate the compensation matrix and the advertized movement matrix
486 NR::Matrix advertized_move;
487 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
488 clone_move = clone_move.inverse() * m;
489 advertized_move = m;
490 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
491 clone_move = clone_move.inverse();
492 advertized_move.set_identity();
493 } else {
494 g_assert_not_reached();
495 }
497 // commit the compensation
498 SPItem *item = SP_ITEM(self);
499 item->transform *= clone_move;
500 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
501 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
502 }
504 static void
505 sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
506 {
507 SPItem *item = SP_ITEM(use);
509 use->_delete_connection.disconnect();
510 use->_transformed_connection.disconnect();
512 if (use->child) {
513 sp_object_detach(SP_OBJECT(use), use->child);
514 use->child = NULL;
515 }
517 if (use->href) {
518 SPItem *refobj = use->ref->getObject();
519 if (refobj) {
520 Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
521 GType type = sp_repr_type_lookup(childrepr);
522 g_return_if_fail(type > G_TYPE_NONE);
523 if (g_type_is_a(type, SP_TYPE_ITEM)) {
524 use->child = (SPObject*) g_object_new(type, 0);
525 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
526 sp_object_unref(use->child, SP_OBJECT(use));
527 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
529 for (SPItemView *v = item->display; v != NULL; v = v->next) {
530 NRArenaItem *ai;
531 ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
532 if (ai) {
533 nr_arena_item_add_child(v->arenaitem, ai, NULL);
534 nr_arena_item_unref(ai);
535 }
536 }
538 }
539 use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
540 use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
541 }
542 }
543 }
545 static void
546 sp_use_delete_self(SPObject */*deleted*/, SPUse *self)
547 {
548 // always delete uses which are used in flowtext
549 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
550 SP_OBJECT(self)->deleteObject();
551 return;
552 }
554 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
555 SP_CLONE_ORPHANS_UNLINK);
557 if (mode == SP_CLONE_ORPHANS_UNLINK) {
558 sp_use_unlink(self);
559 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
560 SP_OBJECT(self)->deleteObject();
561 }
562 }
564 static void
565 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
566 {
567 SPItem *item = SP_ITEM(object);
568 SPUse *use = SP_USE(object);
569 SPItemCtx *ictx = (SPItemCtx *) ctx;
570 SPItemCtx cctx = *ictx;
572 if (((SPObjectClass *) (parent_class))->update)
573 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
575 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
576 flags &= SP_OBJECT_MODIFIED_CASCADE;
578 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
579 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
580 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
581 }
582 }
584 /* Set up child viewport */
585 if (use->x.unit == SVGLength::PERCENT) {
586 use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
587 }
588 if (use->y.unit == SVGLength::PERCENT) {
589 use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
590 }
591 if (use->width.unit == SVGLength::PERCENT) {
592 use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
593 }
594 if (use->height.unit == SVGLength::PERCENT) {
595 use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
596 }
597 cctx.vp.x0 = 0.0;
598 cctx.vp.y0 = 0.0;
599 cctx.vp.x1 = use->width.computed;
600 cctx.vp.y1 = use->height.computed;
601 cctx.i2vp = NR::identity();
602 flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
604 if (use->child) {
605 g_object_ref(G_OBJECT(use->child));
606 if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
607 if (SP_IS_ITEM(use->child)) {
608 SPItem const &chi = *SP_ITEM(use->child);
609 cctx.i2doc = chi.transform * ictx->i2doc;
610 cctx.i2vp = chi.transform * ictx->i2vp;
611 use->child->updateDisplay((SPCtx *)&cctx, flags);
612 } else {
613 use->child->updateDisplay(ctx, flags);
614 }
615 }
616 g_object_unref(G_OBJECT(use->child));
617 }
619 /* As last step set additional transform of arena group */
620 for (SPItemView *v = item->display; v != NULL; v = v->next) {
621 NRMatrix t;
622 nr_matrix_set_translate(&t, use->x.computed, use->y.computed);
623 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
624 }
625 }
627 static void
628 sp_use_modified(SPObject *object, guint flags)
629 {
630 SPUse *use_obj = SP_USE(object);
632 if (flags & SP_OBJECT_MODIFIED_FLAG) {
633 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
634 }
635 flags &= SP_OBJECT_MODIFIED_CASCADE;
637 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
638 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
639 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
640 }
641 }
643 SPObject *child = use_obj->child;
644 if (child) {
645 g_object_ref(G_OBJECT(child));
646 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
647 child->emitModified(flags);
648 }
649 g_object_unref(G_OBJECT(child));
650 }
651 }
653 SPItem *
654 sp_use_unlink(SPUse *use)
655 {
656 if (!use) return NULL;
658 Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
659 if (!repr) return NULL;
661 Inkscape::XML::Node *parent = sp_repr_parent(repr);
662 SPDocument *document = SP_OBJECT(use)->document;
663 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
665 // Track the ultimate source of a chain of uses.
666 SPItem *orig = sp_use_root(use);
667 g_return_val_if_fail(orig, NULL);
669 // Calculate the accumulated transform, starting from the original.
670 NR::Matrix t = sp_use_get_root_transform(use);
672 Inkscape::XML::Node *copy = NULL;
673 if (SP_IS_SYMBOL(orig)) { // make a group, copy children
674 copy = xml_doc->createElement("svg:g");
675 for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
676 Inkscape::XML::Node *newchild = child->duplicate(xml_doc);
677 copy->appendChild(newchild);
678 }
679 } else { // just copy
680 copy = SP_OBJECT_REPR(orig)->duplicate(xml_doc);
681 }
683 // Add the duplicate repr just after the existing one.
684 parent->addChild(copy, repr);
686 // Retrieve the SPItem of the resulting repr.
687 SPObject *unlinked = document->getObjectByRepr(copy);
689 // Merge style from the use.
690 SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
691 SPStyle const *use_sty = SP_OBJECT_STYLE(use);
692 sp_style_merge_from_dying_parent(unli_sty, use_sty);
693 sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
695 SP_OBJECT(unlinked)->updateRepr();
697 // Hold onto our SPObject and repr for now.
698 sp_object_ref(SP_OBJECT(use), NULL);
699 Inkscape::GC::anchor(repr);
701 // Remove ourselves, not propagating delete events to avoid a
702 // chain-reaction with other elements that might reference us.
703 SP_OBJECT(use)->deleteObject(false);
705 // Give the copy our old id and let go of our old repr.
706 copy->setAttribute("id", repr->attribute("id"));
707 Inkscape::GC::release(repr);
709 // Remove tiled clone attrs.
710 copy->setAttribute("inkscape:tiled-clone-of", NULL);
711 copy->setAttribute("inkscape:tile-w", NULL);
712 copy->setAttribute("inkscape:tile-h", NULL);
713 copy->setAttribute("inkscape:tile-cx", NULL);
714 copy->setAttribute("inkscape:tile-cy", NULL);
716 // Establish the succession and let go of our object.
717 SP_OBJECT(use)->setSuccessor(unlinked);
718 sp_object_unref(SP_OBJECT(use), NULL);
720 SPItem *item = SP_ITEM(unlinked);
721 // Set the accummulated transform.
722 {
723 NR::Matrix nomove(NR::identity());
724 NRMatrix ctrans = t.operator const NRMatrix&();
725 // Advertise ourselves as not moving.
726 sp_item_write_transform(item, SP_OBJECT_REPR(item), &ctrans, &nomove);
727 }
728 return item;
729 }
731 SPItem *
732 sp_use_get_original(SPUse *use)
733 {
734 SPItem *ref = use->ref->getObject();
735 return ref;
736 }
738 static void
739 sp_use_snappoints(SPItem const *item, SnapPointsIter p)
740 {
741 g_assert (item != NULL);
742 g_assert (SP_IS_ITEM(item));
743 g_assert (SP_IS_USE(item));
745 SPUse *use = SP_USE(item);
746 SPItem *root = sp_use_root(use);
747 g_return_if_fail(root);
749 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(root);
750 if (item_class.snappoints) {
751 item_class.snappoints(root, p);
752 }
753 }
756 /*
757 Local Variables:
758 mode:c++
759 c-file-style:"stroustrup"
760 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
761 indent-tabs-mode:nil
762 fill-column:99
763 End:
764 */
765 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :