Code

handle the case of no filter primitives
[inkscape.git] / src / sp-use.cpp
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;
112 static void
113 sp_use_init(SPUse *use)
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));
131 static void
132 sp_use_finalize(GObject *obj)
134     SPUse *use = (SPUse *) obj;
136     delete use->ref;
138     use->_delete_connection.~connection();
139     use->_changed_connection.~connection();
141     use->_transformed_connection.~connection();
144 static void
145 sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
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
162 static void
163 sp_use_release(SPObject *object)
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     }
183 static void
184 sp_use_set(SPObject *object, unsigned key, gchar const *value)
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     }
238 static Inkscape::XML::Node *
239 sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
241     SPUse *use = SP_USE(object);
243     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
244         repr = sp_repr_new("svg:use");
245     }
247     if (((SPObjectClass *) (parent_class))->write) {
248         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
249     }
251     sp_repr_set_svg_double(repr, "x", use->x.computed);
252     sp_repr_set_svg_double(repr, "y", use->y.computed);
253     sp_repr_set_svg_double(repr, "width", use->width.computed);
254     sp_repr_set_svg_double(repr, "height", use->height.computed);
256     if (use->ref->getURI()) {
257         gchar *uri_string = use->ref->getURI()->toString();
258         repr->setAttribute("xlink:href", uri_string);
259         g_free(uri_string);
260     }
262     return repr;
265 static void
266 sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
268     SPUse const *use = SP_USE(item);
270     if (use->child && SP_IS_ITEM(use->child)) {
271         SPItem *child = SP_ITEM(use->child);
272         NR::Matrix const ct( child->transform
273                              * NR::translate(use->x.computed,
274                                              use->y.computed)
275                              * transform );
276         sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
277     }
280 static void
281 sp_use_print(SPItem *item, SPPrintContext *ctx)
283     bool translated = false;
284     NRMatrix tp;
285     SPUse *use = SP_USE(item);
287     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
288         nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
289         sp_print_bind(ctx, &tp, 1.0);
290         translated = true;
291     }
293     if (use->child && SP_IS_ITEM(use->child)) {
294         sp_item_invoke_print(SP_ITEM(use->child), ctx);
295     }
297     if (translated) {
298         sp_print_release(ctx);
299     }
302 static gchar *
303 sp_use_description(SPItem *item)
305     SPUse *use = SP_USE(item);
307     char *ret;
308     if (use->child) {
309         static unsigned recursion_depth = 0;
310         if (recursion_depth >= 4) {
311             /* TRANSLATORS: Used for statusbar description for long <use> chains:
312              * "Clone of: Clone of: ... in Layer 1". */
313             return g_strdup(_("..."));
314             /* We could do better, e.g. chasing the href chain until we reach something other than
315              * a <use>, and giving its description. */
316         }
317         ++recursion_depth;
318         char *child_desc = sp_item_description(SP_ITEM(use->child));
319         --recursion_depth;
321         ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
322         g_free(child_desc);
323         return ret;
324     } else {
325         return g_strdup(_("<b>Orphaned clone</b>"));
326     }
329 static NRArenaItem *
330 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
332     SPUse *use = SP_USE(item);
334     NRArenaItem *ai = NRArenaGroup::create(arena);
335     nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
336     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
338     if (use->child) {
339         NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
340         if (ac) {
341             nr_arena_item_add_child(ai, ac, NULL);
342             nr_arena_item_unref(ac);
343         }
344         NR::translate t(use->x.computed,
345                         use->y.computed);
346         nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
347     }
349     return ai;
352 static void
353 sp_use_hide(SPItem *item, unsigned key)
355     SPUse *use = SP_USE(item);
357     if (use->child) {
358         sp_item_invoke_hide(SP_ITEM(use->child), key);
359     }
361     if (((SPItemClass *) parent_class)->hide) {
362         ((SPItemClass *) parent_class)->hide(item, key);
363     }
366 /**
367  * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
368  * which is not an SPUse).
369  *
370  * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
371  * the trivial case) and not the "true original".
372  */
373 SPItem *
374 sp_use_root(SPUse *use)
376     SPObject *orig = use->child;
377     while (SP_IS_USE(orig)) {
378         orig = SP_USE(orig)->child;
379     }
380     g_assert(SP_IS_ITEM(orig));
381     return SP_ITEM(orig);
384 /**
385  * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
386  * included.
387  */
388 NR::Matrix
389 sp_use_get_root_transform(SPUse *use)
391     //track the ultimate source of a chain of uses
392     SPObject *orig = use->child;
393     GSList *chain = NULL;
394     chain = g_slist_prepend(chain, use);
395     while (SP_IS_USE(orig)) {
396         chain = g_slist_prepend(chain, orig);
397         orig = SP_USE(orig)->child;
398     }
399     chain = g_slist_prepend(chain, orig);
402     //calculate the accummulated transform, starting from the original
403     NR::Matrix t(NR::identity());
404     for (GSList *i = chain; i != NULL; i = i->next) {
405         SPItem *i_tem = SP_ITEM(i->data);
407         // "An additional transformation translate(x,y) is appended to the end (i.e.,
408         // right-side) of the transform attribute on the generated 'g', where x and y
409         // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
410         if (SP_IS_USE(i_tem)) {
411             SPUse *i_use = SP_USE(i_tem);
412             if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
413                 t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
414             }
415         }
417         t *= i_tem->transform;
418     }
420     g_slist_free(chain);
421     return t;
424 /**
425  * Returns the transform that leads to the use from its immediate original.
426  * Does not inlcude the original's transform if any.
427  */
428 NR::Matrix
429 sp_use_get_parent_transform(SPUse *use)
431     NR::Matrix t(NR::identity());
432     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
433         t *= NR::translate(use->x._set ? use->x.computed : 0,
434                            use->y._set ? use->y.computed : 0);
435     }
437     t *= SP_ITEM(use)->transform;
438     return t;
441 /**
442  * Sensing a movement of the original, this function attempts to compensate for it in such a way
443  * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
444  * clone's transform.
445  */
446 static void
447 sp_use_move_compensate(NR::Matrix const *mp, SPItem *original, SPUse *self)
449     // the clone is orphaned; or this is not a real use, but a clone of another use;
450     // we skip it, otherwise duplicate compensation will occur
451     if (SP_OBJECT_IS_CLONED(self)) {
452         return;
453     }
455     // never compensate uses which are used in flowtext
456     if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
457         return;
458     }
460     guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
461     // user wants no compensation
462     if (mode == SP_CLONE_COMPENSATION_NONE)
463         return;
465     NR::Matrix m(*mp);
467     // this is not a simple move, do not try to compensate
468     if (!(m.is_translation()))
469         return;
471     // restore item->transform field from the repr, in case it was changed by seltrans
472     sp_object_read_attr (SP_OBJECT (self), "transform");
474     NR::Matrix t = sp_use_get_parent_transform(self);
475     NR::Matrix clone_move = t.inverse() * m * t;
477     // calculate the compensation matrix and the advertized movement matrix
478     NR::Matrix advertized_move;
479     if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
480         clone_move = clone_move.inverse() * m;
481         advertized_move = m;
482     } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
483         clone_move = clone_move.inverse();
484         advertized_move.set_identity();
485     } else {
486         g_assert_not_reached();
487     }
489     // commit the compensation
490     SPItem *item = SP_ITEM(self);
491     item->transform *= clone_move;
492     sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
493     SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
496 static void
497 sp_use_href_changed(SPObject *old_ref, SPObject *ref, SPUse *use)
499     SPItem *item = SP_ITEM(use);
501     use->_delete_connection.disconnect();
502     use->_transformed_connection.disconnect();
504     if (use->child) {
505         sp_object_detach(SP_OBJECT(use), use->child);
506         use->child = NULL;
507     }
509     if (use->href) {
510         SPItem *refobj = use->ref->getObject();
511         if (refobj) {
512             Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
513             GType type = sp_repr_type_lookup(childrepr);
514             g_return_if_fail(type > G_TYPE_NONE);
515             if (g_type_is_a(type, SP_TYPE_ITEM)) {
516                 use->child = (SPObject*) g_object_new(type, 0);
517                 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
518                 sp_object_unref(use->child, SP_OBJECT(use));
519                 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
521                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
522                     NRArenaItem *ai;
523                     ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
524                     if (ai) {
525                         nr_arena_item_add_child(v->arenaitem, ai, NULL);
526                         nr_arena_item_unref(ai);
527                     }
528                 }
530             }
531             use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
532             use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
533         }
534     }
537 static void
538 sp_use_delete_self(SPObject *deleted, SPUse *self)
540     // always delete uses which are used in flowtext
541     if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
542         SP_OBJECT(self)->deleteObject();
543         return;
544     }
546     guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
547                                                SP_CLONE_ORPHANS_UNLINK);
549     if (mode == SP_CLONE_ORPHANS_UNLINK) {
550         sp_use_unlink(self);
551     } else if (mode == SP_CLONE_ORPHANS_DELETE) {
552         SP_OBJECT(self)->deleteObject();
553     }
556 static void
557 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
559     SPItem *item = SP_ITEM(object);
560     SPUse *use = SP_USE(object);
561     SPItemCtx *ictx = (SPItemCtx *) ctx;
562     SPItemCtx cctx = *ictx;
564     if (((SPObjectClass *) (parent_class))->update)
565         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
567     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
568     flags &= SP_OBJECT_MODIFIED_CASCADE;
570     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
571       for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
572         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
573       }
574     }
576     /* Set up child viewport */
577     if (use->x.unit == SVGLength::PERCENT) {
578         use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
579     }
580     if (use->y.unit == SVGLength::PERCENT) {
581         use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
582     }
583     if (use->width.unit == SVGLength::PERCENT) {
584         use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
585     }
586     if (use->height.unit == SVGLength::PERCENT) {
587         use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
588     }
589     cctx.vp.x0 = 0.0;
590     cctx.vp.y0 = 0.0;
591     cctx.vp.x1 = use->width.computed;
592     cctx.vp.y1 = use->height.computed;
593     cctx.i2vp = NR::identity();
594     flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
596     if (use->child) {
597         g_object_ref(G_OBJECT(use->child));
598         if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
599             if (SP_IS_ITEM(use->child)) {
600                 SPItem const &chi = *SP_ITEM(use->child);
601                 cctx.i2doc = chi.transform * ictx->i2doc;
602                 cctx.i2vp = chi.transform * ictx->i2vp;
603                 use->child->updateDisplay((SPCtx *)&cctx, flags);
604             } else {
605                 use->child->updateDisplay(ctx, flags);
606             }
607         }
608         g_object_unref(G_OBJECT(use->child));
609     }
611     /* As last step set additional transform of arena group */
612     for (SPItemView *v = item->display; v != NULL; v = v->next) {
613         NRMatrix t;
614         nr_matrix_set_translate(&t, use->x.computed, use->y.computed);
615         nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
616     }
619 static void
620 sp_use_modified(SPObject *object, guint flags)
622     SPUse *use_obj = SP_USE(object);
624     if (flags & SP_OBJECT_MODIFIED_FLAG) {
625         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
626     }
627     flags &= SP_OBJECT_MODIFIED_CASCADE;
629     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
630       for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
631         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
632       }
633     }
635     SPObject *child = use_obj->child;
636     if (child) {
637         g_object_ref(G_OBJECT(child));
638         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
639             child->emitModified(flags);
640         }
641         g_object_unref(G_OBJECT(child));
642     }
645 SPItem *
646 sp_use_unlink(SPUse *use)
648     if (!use) return NULL;
650     Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
651     if (!repr) return NULL;
653     Inkscape::XML::Node *parent = sp_repr_parent(repr);
654     SPDocument *document = SP_OBJECT(use)->document;
656     // Track the ultimate source of a chain of uses.
657     SPItem *orig = sp_use_root(use);
659     // Calculate the accumulated transform, starting from the original.
660     NR::Matrix t = sp_use_get_root_transform(use);
662     Inkscape::XML::Node *copy = NULL;
663     if (SP_IS_SYMBOL(orig)) { // make a group, copy children
664         copy = sp_repr_new("svg:g");
665         for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
666                 Inkscape::XML::Node *newchild = child->duplicate();
667                 copy->appendChild(newchild);
668         }
669     } else { // just copy
670         copy = SP_OBJECT_REPR(orig)->duplicate();
671     }
673     // Add the duplicate repr just after the existing one.
674     parent->addChild(copy, repr);
676     // Retrieve the SPItem of the resulting repr.
677     SPObject *unlinked = document->getObjectByRepr(copy);
679     // Merge style from the use.
680     SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
681     SPStyle const *use_sty = SP_OBJECT_STYLE(use);
682     sp_style_merge_from_dying_parent(unli_sty, use_sty);
683     sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
685     SP_OBJECT(unlinked)->updateRepr();
687     // Hold onto our SPObject and repr for now.
688     sp_object_ref(SP_OBJECT(use), NULL);
689     Inkscape::GC::anchor(repr);
691     // Remove ourselves, not propagating delete events to avoid a
692     // chain-reaction with other elements that might reference us.
693     SP_OBJECT(use)->deleteObject(false);
695     // Give the copy our old id and let go of our old repr.
696     copy->setAttribute("id", repr->attribute("id"));
697     Inkscape::GC::release(repr);
699     // Remove tiled clone attrs.
700     copy->setAttribute("inkscape:tiled-clone-of", NULL);
701     copy->setAttribute("inkscape:tile-w", NULL);
702     copy->setAttribute("inkscape:tile-h", NULL);
703     copy->setAttribute("inkscape:tile-cx", NULL);
704     copy->setAttribute("inkscape:tile-cy", NULL);
706     // Establish the succession and let go of our object.
707     SP_OBJECT(use)->setSuccessor(unlinked);
708     sp_object_unref(SP_OBJECT(use), NULL);
710     SPItem *item = SP_ITEM(unlinked);
711     // Set the accummulated transform.
712     {
713         NR::Matrix nomove(NR::identity());
714         NRMatrix ctrans = t.operator const NRMatrix&();
715         // Advertise ourselves as not moving.
716         sp_item_write_transform(item, SP_OBJECT_REPR(item), &ctrans, &nomove);
717     }
718     return item;
721 SPItem *
722 sp_use_get_original(SPUse *use)
724     SPItem *ref = use->ref->getObject();
725     return ref;
729 /*
730   Local Variables:
731   mode:c++
732   c-file-style:"stroustrup"
733   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
734   indent-tabs-mode:nil
735   fill-column:99
736   End:
737 */
738 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :