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
19 #include <libnr/nr-matrix-ops.h>
20 #include <libnr/nr-matrix-fns.h>
21 #include "libnr/nr-matrix-translate-ops.h"
22 #include <glibmm/i18n.h>
23 #include "display/nr-arena-group.h"
24 #include "attributes.h"
25 #include "document.h"
26 #include "sp-object-repr.h"
27 #include "sp-flowregion.h"
28 #include "uri.h"
29 #include "print.h"
30 #include "xml/repr.h"
31 #include "prefs-utils.h"
32 #include "style.h"
33 #include "sp-symbol.h"
34 #include "sp-use.h"
35 #include "sp-use-reference.h"
37 /* fixme: */
39 static void sp_use_class_init(SPUseClass *classname);
40 static void sp_use_init(SPUse *use);
41 static void sp_use_finalize(GObject *obj);
43 static void sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_use_release(SPObject *object);
45 static void sp_use_set(SPObject *object, unsigned key, gchar const *value);
46 static Inkscape::XML::Node *sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
47 static void sp_use_update(SPObject *object, SPCtx *ctx, guint flags);
48 static void sp_use_modified(SPObject *object, guint flags);
50 static void sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
51 static void sp_use_print(SPItem *item, SPPrintContext *ctx);
52 static gchar *sp_use_description(SPItem *item);
53 static NRArenaItem *sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
54 static void sp_use_hide(SPItem *item, unsigned key);
56 static void sp_use_href_changed(SPObject *old_ref, SPObject *ref, SPUse *use);
58 static void sp_use_delete_self(SPObject *deleted, SPUse *self);
60 static SPItemClass *parent_class;
62 //void m_print(gchar *say, NR::Matrix m)
63 //{ g_print("%s %g %g %g %g %g %g\n", say, m[0], m[1], m[2], m[3], m[4], m[5]); }
65 GType
66 sp_use_get_type(void)
67 {
68 static GType use_type = 0;
69 if (!use_type) {
70 GTypeInfo use_info = {
71 sizeof(SPUseClass),
72 NULL, /* base_init */
73 NULL, /* base_finalize */
74 (GClassInitFunc) sp_use_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
77 sizeof(SPUse),
78 16, /* n_preallocs */
79 (GInstanceInitFunc) sp_use_init,
80 NULL, /* value_table */
81 };
82 use_type = g_type_register_static(SP_TYPE_ITEM, "SPUse", &use_info, (GTypeFlags)0);
83 }
84 return use_type;
85 }
87 static void
88 sp_use_class_init(SPUseClass *classname)
89 {
90 GObjectClass *gobject_class = (GObjectClass *) classname;
91 SPObjectClass *sp_object_class = (SPObjectClass *) classname;
92 SPItemClass *item_class = (SPItemClass *) classname;
94 parent_class = (SPItemClass*) g_type_class_ref(SP_TYPE_ITEM);
96 gobject_class->finalize = sp_use_finalize;
98 sp_object_class->build = sp_use_build;
99 sp_object_class->release = sp_use_release;
100 sp_object_class->set = sp_use_set;
101 sp_object_class->write = sp_use_write;
102 sp_object_class->update = sp_use_update;
103 sp_object_class->modified = sp_use_modified;
105 item_class->bbox = sp_use_bbox;
106 item_class->description = sp_use_description;
107 item_class->print = sp_use_print;
108 item_class->show = sp_use_show;
109 item_class->hide = sp_use_hide;
110 }
112 static void
113 sp_use_init(SPUse *use)
114 {
115 use->x.unset();
116 use->y.unset();
117 use->width.unset(SVGLength::PERCENT, 1.0, 1.0);
118 use->height.unset(SVGLength::PERCENT, 1.0, 1.0);
119 use->href = NULL;
121 new (&use->_delete_connection) sigc::connection();
122 new (&use->_changed_connection) sigc::connection();
124 new (&use->_transformed_connection) sigc::connection();
126 use->ref = new SPUseReference(SP_OBJECT(use));
128 use->_changed_connection = use->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_use_href_changed), use));
129 }
131 static void
132 sp_use_finalize(GObject *obj)
133 {
134 SPUse *use = (SPUse *) obj;
136 delete use->ref;
138 use->_delete_connection.~connection();
139 use->_changed_connection.~connection();
141 use->_transformed_connection.~connection();
142 }
144 static void
145 sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
146 {
147 if (((SPObjectClass *) parent_class)->build) {
148 (* ((SPObjectClass *) parent_class)->build)(object, document, repr);
149 }
151 sp_object_read_attr(object, "x");
152 sp_object_read_attr(object, "y");
153 sp_object_read_attr(object, "width");
154 sp_object_read_attr(object, "height");
155 sp_object_read_attr(object, "xlink:href");
157 // We don't need to create child here:
158 // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
159 // which will call sp_use_href_changed, and that will take care of the child
160 }
162 static void
163 sp_use_release(SPObject *object)
164 {
165 SPUse *use = SP_USE(object);
167 use->child = NULL;
169 use->_delete_connection.disconnect();
170 use->_changed_connection.disconnect();
171 use->_transformed_connection.disconnect();
173 g_free(use->href);
174 use->href = NULL;
176 use->ref->detach();
178 if (((SPObjectClass *) parent_class)->release) {
179 ((SPObjectClass *) parent_class)->release(object);
180 }
181 }
183 static void
184 sp_use_set(SPObject *object, unsigned key, gchar const *value)
185 {
186 SPUse *use = SP_USE(object);
188 switch (key) {
189 case SP_ATTR_X:
190 use->x.readOrUnset(value);
191 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192 break;
193 case SP_ATTR_Y:
194 use->y.readOrUnset(value);
195 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
196 break;
197 case SP_ATTR_WIDTH:
198 use->width.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
199 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
200 break;
201 case SP_ATTR_HEIGHT:
202 use->height.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
203 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
204 break;
206 case SP_ATTR_XLINK_HREF: {
207 if ( value && use->href && ( strcmp(value, use->href) == 0 ) ) {
208 /* No change, do nothing. */
209 } else {
210 g_free(use->href);
211 use->href = NULL;
212 if (value) {
213 // First, set the href field, because sp_use_href_changed will need it.
214 use->href = g_strdup(value);
216 // Now do the attaching, which emits the changed signal.
217 try {
218 use->ref->attach(Inkscape::URI(value));
219 } catch (Inkscape::BadURIException &e) {
220 g_warning("%s", e.what());
221 use->ref->detach();
222 }
223 } else {
224 use->ref->detach();
225 }
226 }
227 break;
228 }
230 default:
231 if (((SPObjectClass *) parent_class)->set) {
232 ((SPObjectClass *) parent_class)->set(object, key, value);
233 }
234 break;
235 }
236 }
238 static Inkscape::XML::Node *
239 sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
240 {
241 SPUse *use = SP_USE(object);
243 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
244 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
245 repr = xml_doc->createElement("svg:use");
246 }
248 if (((SPObjectClass *) (parent_class))->write) {
249 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
250 }
252 sp_repr_set_svg_double(repr, "x", use->x.computed);
253 sp_repr_set_svg_double(repr, "y", use->y.computed);
254 sp_repr_set_svg_double(repr, "width", use->width.computed);
255 sp_repr_set_svg_double(repr, "height", use->height.computed);
257 if (use->ref->getURI()) {
258 gchar *uri_string = use->ref->getURI()->toString();
259 repr->setAttribute("xlink:href", uri_string);
260 g_free(uri_string);
261 }
263 return repr;
264 }
266 static void
267 sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
268 {
269 SPUse const *use = SP_USE(item);
271 if (use->child && SP_IS_ITEM(use->child)) {
272 SPItem *child = SP_ITEM(use->child);
273 NR::Matrix const ct( child->transform
274 * NR::translate(use->x.computed,
275 use->y.computed)
276 * transform );
277 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
278 }
279 }
281 static void
282 sp_use_print(SPItem *item, SPPrintContext *ctx)
283 {
284 bool translated = false;
285 NRMatrix tp;
286 SPUse *use = SP_USE(item);
288 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
289 nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
290 sp_print_bind(ctx, &tp, 1.0);
291 translated = true;
292 }
294 if (use->child && SP_IS_ITEM(use->child)) {
295 sp_item_invoke_print(SP_ITEM(use->child), ctx);
296 }
298 if (translated) {
299 sp_print_release(ctx);
300 }
301 }
303 static gchar *
304 sp_use_description(SPItem *item)
305 {
306 SPUse *use = SP_USE(item);
308 char *ret;
309 if (use->child) {
310 static unsigned recursion_depth = 0;
311 if (recursion_depth >= 4) {
312 /* TRANSLATORS: Used for statusbar description for long <use> chains:
313 * "Clone of: Clone of: ... in Layer 1". */
314 return g_strdup(_("..."));
315 /* We could do better, e.g. chasing the href chain until we reach something other than
316 * a <use>, and giving its description. */
317 }
318 ++recursion_depth;
319 char *child_desc = sp_item_description(SP_ITEM(use->child));
320 --recursion_depth;
322 ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
323 g_free(child_desc);
324 return ret;
325 } else {
326 return g_strdup(_("<b>Orphaned clone</b>"));
327 }
328 }
330 static NRArenaItem *
331 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
332 {
333 SPUse *use = SP_USE(item);
335 NRArenaItem *ai = NRArenaGroup::create(arena);
336 nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
337 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
339 if (use->child) {
340 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
341 if (ac) {
342 nr_arena_item_add_child(ai, ac, NULL);
343 nr_arena_item_unref(ac);
344 }
345 NR::translate t(use->x.computed,
346 use->y.computed);
347 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
348 }
350 return ai;
351 }
353 static void
354 sp_use_hide(SPItem *item, unsigned key)
355 {
356 SPUse *use = SP_USE(item);
358 if (use->child) {
359 sp_item_invoke_hide(SP_ITEM(use->child), key);
360 }
362 if (((SPItemClass *) parent_class)->hide) {
363 ((SPItemClass *) parent_class)->hide(item, key);
364 }
365 }
367 /**
368 * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
369 * which is not an SPUse).
370 *
371 * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
372 * the trivial case) and not the "true original".
373 */
374 SPItem *
375 sp_use_root(SPUse *use)
376 {
377 SPObject *orig = use->child;
378 while (SP_IS_USE(orig)) {
379 orig = SP_USE(orig)->child;
380 }
381 g_assert(SP_IS_ITEM(orig));
382 return SP_ITEM(orig);
383 }
385 /**
386 * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
387 * included.
388 */
389 NR::Matrix
390 sp_use_get_root_transform(SPUse *use)
391 {
392 //track the ultimate source of a chain of uses
393 SPObject *orig = use->child;
394 GSList *chain = NULL;
395 chain = g_slist_prepend(chain, use);
396 while (SP_IS_USE(orig)) {
397 chain = g_slist_prepend(chain, orig);
398 orig = SP_USE(orig)->child;
399 }
400 chain = g_slist_prepend(chain, orig);
403 //calculate the accummulated transform, starting from the original
404 NR::Matrix t(NR::identity());
405 for (GSList *i = chain; i != NULL; i = i->next) {
406 SPItem *i_tem = SP_ITEM(i->data);
408 // "An additional transformation translate(x,y) is appended to the end (i.e.,
409 // right-side) of the transform attribute on the generated 'g', where x and y
410 // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
411 if (SP_IS_USE(i_tem)) {
412 SPUse *i_use = SP_USE(i_tem);
413 if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
414 t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
415 }
416 }
418 t *= i_tem->transform;
419 }
421 g_slist_free(chain);
422 return t;
423 }
425 /**
426 * Returns the transform that leads to the use from its immediate original.
427 * Does not inlcude the original's transform if any.
428 */
429 NR::Matrix
430 sp_use_get_parent_transform(SPUse *use)
431 {
432 NR::Matrix t(NR::identity());
433 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
434 t *= NR::translate(use->x._set ? use->x.computed : 0,
435 use->y._set ? use->y.computed : 0);
436 }
438 t *= SP_ITEM(use)->transform;
439 return t;
440 }
442 /**
443 * Sensing a movement of the original, this function attempts to compensate for it in such a way
444 * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
445 * clone's transform.
446 */
447 static void
448 sp_use_move_compensate(NR::Matrix const *mp, SPItem *original, SPUse *self)
449 {
450 // the clone is orphaned; or this is not a real use, but a clone of another use;
451 // we skip it, otherwise duplicate compensation will occur
452 if (SP_OBJECT_IS_CLONED(self)) {
453 return;
454 }
456 // never compensate uses which are used in flowtext
457 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
458 return;
459 }
461 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
462 // user wants no compensation
463 if (mode == SP_CLONE_COMPENSATION_NONE)
464 return;
466 NR::Matrix m(*mp);
468 // this is not a simple move, do not try to compensate
469 if (!(m.is_translation()))
470 return;
472 // restore item->transform field from the repr, in case it was changed by seltrans
473 sp_object_read_attr (SP_OBJECT (self), "transform");
475 NR::Matrix t = sp_use_get_parent_transform(self);
476 NR::Matrix clone_move = t.inverse() * m * t;
478 // calculate the compensation matrix and the advertized movement matrix
479 NR::Matrix advertized_move;
480 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
481 clone_move = clone_move.inverse() * m;
482 advertized_move = m;
483 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
484 clone_move = clone_move.inverse();
485 advertized_move.set_identity();
486 } else {
487 g_assert_not_reached();
488 }
490 // commit the compensation
491 SPItem *item = SP_ITEM(self);
492 item->transform *= clone_move;
493 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
494 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
495 }
497 static void
498 sp_use_href_changed(SPObject *old_ref, SPObject *ref, SPUse *use)
499 {
500 SPItem *item = SP_ITEM(use);
502 use->_delete_connection.disconnect();
503 use->_transformed_connection.disconnect();
505 if (use->child) {
506 sp_object_detach(SP_OBJECT(use), use->child);
507 use->child = NULL;
508 }
510 if (use->href) {
511 SPItem *refobj = use->ref->getObject();
512 if (refobj) {
513 Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
514 GType type = sp_repr_type_lookup(childrepr);
515 g_return_if_fail(type > G_TYPE_NONE);
516 if (g_type_is_a(type, SP_TYPE_ITEM)) {
517 use->child = (SPObject*) g_object_new(type, 0);
518 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
519 sp_object_unref(use->child, SP_OBJECT(use));
520 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
522 for (SPItemView *v = item->display; v != NULL; v = v->next) {
523 NRArenaItem *ai;
524 ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
525 if (ai) {
526 nr_arena_item_add_child(v->arenaitem, ai, NULL);
527 nr_arena_item_unref(ai);
528 }
529 }
531 }
532 use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
533 use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
534 }
535 }
536 }
538 static void
539 sp_use_delete_self(SPObject *deleted, SPUse *self)
540 {
541 // always delete uses which are used in flowtext
542 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
543 SP_OBJECT(self)->deleteObject();
544 return;
545 }
547 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
548 SP_CLONE_ORPHANS_UNLINK);
550 if (mode == SP_CLONE_ORPHANS_UNLINK) {
551 sp_use_unlink(self);
552 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
553 SP_OBJECT(self)->deleteObject();
554 }
555 }
557 static void
558 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
559 {
560 SPItem *item = SP_ITEM(object);
561 SPUse *use = SP_USE(object);
562 SPItemCtx *ictx = (SPItemCtx *) ctx;
563 SPItemCtx cctx = *ictx;
565 if (((SPObjectClass *) (parent_class))->update)
566 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
568 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
569 flags &= SP_OBJECT_MODIFIED_CASCADE;
571 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
572 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
573 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
574 }
575 }
577 /* Set up child viewport */
578 if (use->x.unit == SVGLength::PERCENT) {
579 use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
580 }
581 if (use->y.unit == SVGLength::PERCENT) {
582 use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
583 }
584 if (use->width.unit == SVGLength::PERCENT) {
585 use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
586 }
587 if (use->height.unit == SVGLength::PERCENT) {
588 use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
589 }
590 cctx.vp.x0 = 0.0;
591 cctx.vp.y0 = 0.0;
592 cctx.vp.x1 = use->width.computed;
593 cctx.vp.y1 = use->height.computed;
594 cctx.i2vp = NR::identity();
595 flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
597 if (use->child) {
598 g_object_ref(G_OBJECT(use->child));
599 if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
600 if (SP_IS_ITEM(use->child)) {
601 SPItem const &chi = *SP_ITEM(use->child);
602 cctx.i2doc = chi.transform * ictx->i2doc;
603 cctx.i2vp = chi.transform * ictx->i2vp;
604 use->child->updateDisplay((SPCtx *)&cctx, flags);
605 } else {
606 use->child->updateDisplay(ctx, flags);
607 }
608 }
609 g_object_unref(G_OBJECT(use->child));
610 }
612 /* As last step set additional transform of arena group */
613 for (SPItemView *v = item->display; v != NULL; v = v->next) {
614 NRMatrix t;
615 nr_matrix_set_translate(&t, use->x.computed, use->y.computed);
616 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
617 }
618 }
620 static void
621 sp_use_modified(SPObject *object, guint flags)
622 {
623 SPUse *use_obj = SP_USE(object);
625 if (flags & SP_OBJECT_MODIFIED_FLAG) {
626 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
627 }
628 flags &= SP_OBJECT_MODIFIED_CASCADE;
630 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
631 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
632 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
633 }
634 }
636 SPObject *child = use_obj->child;
637 if (child) {
638 g_object_ref(G_OBJECT(child));
639 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
640 child->emitModified(flags);
641 }
642 g_object_unref(G_OBJECT(child));
643 }
644 }
646 SPItem *
647 sp_use_unlink(SPUse *use)
648 {
649 if (!use) return NULL;
651 Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
652 if (!repr) return NULL;
654 Inkscape::XML::Node *parent = sp_repr_parent(repr);
655 SPDocument *document = SP_OBJECT(use)->document;
656 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
658 // Track the ultimate source of a chain of uses.
659 SPItem *orig = sp_use_root(use);
661 // Calculate the accumulated transform, starting from the original.
662 NR::Matrix t = sp_use_get_root_transform(use);
664 Inkscape::XML::Node *copy = NULL;
665 if (SP_IS_SYMBOL(orig)) { // make a group, copy children
666 copy = xml_doc->createElement("svg:g");
667 for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
668 Inkscape::XML::Node *newchild = child->duplicate();
669 copy->appendChild(newchild);
670 }
671 } else { // just copy
672 copy = SP_OBJECT_REPR(orig)->duplicate();
673 }
675 // Add the duplicate repr just after the existing one.
676 parent->addChild(copy, repr);
678 // Retrieve the SPItem of the resulting repr.
679 SPObject *unlinked = document->getObjectByRepr(copy);
681 // Merge style from the use.
682 SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
683 SPStyle const *use_sty = SP_OBJECT_STYLE(use);
684 sp_style_merge_from_dying_parent(unli_sty, use_sty);
685 sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
687 SP_OBJECT(unlinked)->updateRepr();
689 // Hold onto our SPObject and repr for now.
690 sp_object_ref(SP_OBJECT(use), NULL);
691 Inkscape::GC::anchor(repr);
693 // Remove ourselves, not propagating delete events to avoid a
694 // chain-reaction with other elements that might reference us.
695 SP_OBJECT(use)->deleteObject(false);
697 // Give the copy our old id and let go of our old repr.
698 copy->setAttribute("id", repr->attribute("id"));
699 Inkscape::GC::release(repr);
701 // Remove tiled clone attrs.
702 copy->setAttribute("inkscape:tiled-clone-of", NULL);
703 copy->setAttribute("inkscape:tile-w", NULL);
704 copy->setAttribute("inkscape:tile-h", NULL);
705 copy->setAttribute("inkscape:tile-cx", NULL);
706 copy->setAttribute("inkscape:tile-cy", NULL);
708 // Establish the succession and let go of our object.
709 SP_OBJECT(use)->setSuccessor(unlinked);
710 sp_object_unref(SP_OBJECT(use), NULL);
712 SPItem *item = SP_ITEM(unlinked);
713 // Set the accummulated transform.
714 {
715 NR::Matrix nomove(NR::identity());
716 NRMatrix ctrans = t.operator const NRMatrix&();
717 // Advertise ourselves as not moving.
718 sp_item_write_transform(item, SP_OBJECT_REPR(item), &ctrans, &nomove);
719 }
720 return item;
721 }
723 SPItem *
724 sp_use_get_original(SPUse *use)
725 {
726 SPItem *ref = use->ref->getObject();
727 return ref;
728 }
731 /*
732 Local Variables:
733 mode:c++
734 c-file-style:"stroustrup"
735 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
736 indent-tabs-mode:nil
737 fill-column:99
738 End:
739 */
740 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :