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_snappoints(SPItem const *item, SnapPointsIter p);
52 static void sp_use_print(SPItem *item, SPPrintContext *ctx);
53 static gchar *sp_use_description(SPItem *item);
54 static NRArenaItem *sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
55 static void sp_use_hide(SPItem *item, unsigned key);
57 static void sp_use_href_changed(SPObject *old_ref, SPObject *ref, SPUse *use);
59 static void sp_use_delete_self(SPObject *deleted, SPUse *self);
61 static SPItemClass *parent_class;
63 //void m_print(gchar *say, NR::Matrix m)
64 //{ g_print("%s %g %g %g %g %g %g\n", say, m[0], m[1], m[2], m[3], m[4], m[5]); }
66 GType
67 sp_use_get_type(void)
68 {
69 static GType use_type = 0;
70 if (!use_type) {
71 GTypeInfo use_info = {
72 sizeof(SPUseClass),
73 NULL, /* base_init */
74 NULL, /* base_finalize */
75 (GClassInitFunc) sp_use_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof(SPUse),
79 16, /* n_preallocs */
80 (GInstanceInitFunc) sp_use_init,
81 NULL, /* value_table */
82 };
83 use_type = g_type_register_static(SP_TYPE_ITEM, "SPUse", &use_info, (GTypeFlags)0);
84 }
85 return use_type;
86 }
88 static void
89 sp_use_class_init(SPUseClass *classname)
90 {
91 GObjectClass *gobject_class = (GObjectClass *) classname;
92 SPObjectClass *sp_object_class = (SPObjectClass *) classname;
93 SPItemClass *item_class = (SPItemClass *) classname;
95 parent_class = (SPItemClass*) g_type_class_ref(SP_TYPE_ITEM);
97 gobject_class->finalize = sp_use_finalize;
99 sp_object_class->build = sp_use_build;
100 sp_object_class->release = sp_use_release;
101 sp_object_class->set = sp_use_set;
102 sp_object_class->write = sp_use_write;
103 sp_object_class->update = sp_use_update;
104 sp_object_class->modified = sp_use_modified;
106 item_class->bbox = sp_use_bbox;
107 item_class->description = sp_use_description;
108 item_class->print = sp_use_print;
109 item_class->show = sp_use_show;
110 item_class->hide = sp_use_hide;
111 item_class->snappoints = sp_use_snappoints;
112 }
114 static void
115 sp_use_init(SPUse *use)
116 {
117 use->x.unset();
118 use->y.unset();
119 use->width.unset(SVGLength::PERCENT, 1.0, 1.0);
120 use->height.unset(SVGLength::PERCENT, 1.0, 1.0);
121 use->href = NULL;
123 new (&use->_delete_connection) sigc::connection();
124 new (&use->_changed_connection) sigc::connection();
126 new (&use->_transformed_connection) sigc::connection();
128 use->ref = new SPUseReference(SP_OBJECT(use));
130 use->_changed_connection = use->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_use_href_changed), use));
131 }
133 static void
134 sp_use_finalize(GObject *obj)
135 {
136 SPUse *use = (SPUse *) obj;
138 delete use->ref;
140 use->_delete_connection.~connection();
141 use->_changed_connection.~connection();
143 use->_transformed_connection.~connection();
144 }
146 static void
147 sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
148 {
149 if (((SPObjectClass *) parent_class)->build) {
150 (* ((SPObjectClass *) parent_class)->build)(object, document, repr);
151 }
153 sp_object_read_attr(object, "x");
154 sp_object_read_attr(object, "y");
155 sp_object_read_attr(object, "width");
156 sp_object_read_attr(object, "height");
157 sp_object_read_attr(object, "xlink:href");
159 // We don't need to create child here:
160 // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
161 // which will call sp_use_href_changed, and that will take care of the child
162 }
164 static void
165 sp_use_release(SPObject *object)
166 {
167 SPUse *use = SP_USE(object);
169 use->child = NULL;
171 use->_delete_connection.disconnect();
172 use->_changed_connection.disconnect();
173 use->_transformed_connection.disconnect();
175 g_free(use->href);
176 use->href = NULL;
178 use->ref->detach();
180 if (((SPObjectClass *) parent_class)->release) {
181 ((SPObjectClass *) parent_class)->release(object);
182 }
183 }
185 static void
186 sp_use_set(SPObject *object, unsigned key, gchar const *value)
187 {
188 SPUse *use = SP_USE(object);
190 switch (key) {
191 case SP_ATTR_X:
192 use->x.readOrUnset(value);
193 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
194 break;
195 case SP_ATTR_Y:
196 use->y.readOrUnset(value);
197 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
198 break;
199 case SP_ATTR_WIDTH:
200 use->width.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
201 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 break;
203 case SP_ATTR_HEIGHT:
204 use->height.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
205 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 break;
208 case SP_ATTR_XLINK_HREF: {
209 if ( value && use->href && ( strcmp(value, use->href) == 0 ) ) {
210 /* No change, do nothing. */
211 } else {
212 g_free(use->href);
213 use->href = NULL;
214 if (value) {
215 // First, set the href field, because sp_use_href_changed will need it.
216 use->href = g_strdup(value);
218 // Now do the attaching, which emits the changed signal.
219 try {
220 use->ref->attach(Inkscape::URI(value));
221 } catch (Inkscape::BadURIException &e) {
222 g_warning("%s", e.what());
223 use->ref->detach();
224 }
225 } else {
226 use->ref->detach();
227 }
228 }
229 break;
230 }
232 default:
233 if (((SPObjectClass *) parent_class)->set) {
234 ((SPObjectClass *) parent_class)->set(object, key, value);
235 }
236 break;
237 }
238 }
240 static Inkscape::XML::Node *
241 sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
242 {
243 SPUse *use = SP_USE(object);
245 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
246 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
247 repr = xml_doc->createElement("svg:use");
248 }
250 if (((SPObjectClass *) (parent_class))->write) {
251 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
252 }
254 sp_repr_set_svg_double(repr, "x", use->x.computed);
255 sp_repr_set_svg_double(repr, "y", use->y.computed);
256 sp_repr_set_svg_double(repr, "width", use->width.computed);
257 sp_repr_set_svg_double(repr, "height", use->height.computed);
259 if (use->ref->getURI()) {
260 gchar *uri_string = use->ref->getURI()->toString();
261 repr->setAttribute("xlink:href", uri_string);
262 g_free(uri_string);
263 }
265 return repr;
266 }
268 static void
269 sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
270 {
271 SPUse const *use = SP_USE(item);
273 if (use->child && SP_IS_ITEM(use->child)) {
274 SPItem *child = SP_ITEM(use->child);
275 NR::Matrix const ct( child->transform
276 * NR::translate(use->x.computed,
277 use->y.computed)
278 * transform );
279 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
280 }
281 }
283 static void
284 sp_use_print(SPItem *item, SPPrintContext *ctx)
285 {
286 bool translated = false;
287 NRMatrix tp;
288 SPUse *use = SP_USE(item);
290 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
291 nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
292 sp_print_bind(ctx, &tp, 1.0);
293 translated = true;
294 }
296 if (use->child && SP_IS_ITEM(use->child)) {
297 sp_item_invoke_print(SP_ITEM(use->child), ctx);
298 }
300 if (translated) {
301 sp_print_release(ctx);
302 }
303 }
305 static gchar *
306 sp_use_description(SPItem *item)
307 {
308 SPUse *use = SP_USE(item);
310 char *ret;
311 if (use->child) {
312 static unsigned recursion_depth = 0;
313 if (recursion_depth >= 4) {
314 /* TRANSLATORS: Used for statusbar description for long <use> chains:
315 * "Clone of: Clone of: ... in Layer 1". */
316 return g_strdup(_("..."));
317 /* We could do better, e.g. chasing the href chain until we reach something other than
318 * a <use>, and giving its description. */
319 }
320 ++recursion_depth;
321 char *child_desc = sp_item_description(SP_ITEM(use->child));
322 --recursion_depth;
324 ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
325 g_free(child_desc);
326 return ret;
327 } else {
328 return g_strdup(_("<b>Orphaned clone</b>"));
329 }
330 }
332 static NRArenaItem *
333 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
334 {
335 SPUse *use = SP_USE(item);
337 NRArenaItem *ai = NRArenaGroup::create(arena);
338 nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
339 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
341 if (use->child) {
342 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
343 if (ac) {
344 nr_arena_item_add_child(ai, ac, NULL);
345 nr_arena_item_unref(ac);
346 }
347 NR::translate t(use->x.computed,
348 use->y.computed);
349 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
350 }
352 return ai;
353 }
355 static void
356 sp_use_hide(SPItem *item, unsigned key)
357 {
358 SPUse *use = SP_USE(item);
360 if (use->child) {
361 sp_item_invoke_hide(SP_ITEM(use->child), key);
362 }
364 if (((SPItemClass *) parent_class)->hide) {
365 ((SPItemClass *) parent_class)->hide(item, key);
366 }
367 }
369 /**
370 * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
371 * which is not an SPUse).
372 *
373 * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
374 * the trivial case) and not the "true original".
375 */
376 SPItem *
377 sp_use_root(SPUse *use)
378 {
379 SPObject *orig = use->child;
380 while (SP_IS_USE(orig)) {
381 orig = SP_USE(orig)->child;
382 }
383 g_assert(SP_IS_ITEM(orig));
384 return SP_ITEM(orig);
385 }
387 /**
388 * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
389 * included.
390 */
391 NR::Matrix
392 sp_use_get_root_transform(SPUse *use)
393 {
394 //track the ultimate source of a chain of uses
395 SPObject *orig = use->child;
396 GSList *chain = NULL;
397 chain = g_slist_prepend(chain, use);
398 while (SP_IS_USE(orig)) {
399 chain = g_slist_prepend(chain, orig);
400 orig = SP_USE(orig)->child;
401 }
402 chain = g_slist_prepend(chain, orig);
405 //calculate the accummulated transform, starting from the original
406 NR::Matrix t(NR::identity());
407 for (GSList *i = chain; i != NULL; i = i->next) {
408 SPItem *i_tem = SP_ITEM(i->data);
410 // "An additional transformation translate(x,y) is appended to the end (i.e.,
411 // right-side) of the transform attribute on the generated 'g', where x and y
412 // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
413 if (SP_IS_USE(i_tem)) {
414 SPUse *i_use = SP_USE(i_tem);
415 if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
416 t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
417 }
418 }
420 t *= i_tem->transform;
421 }
423 g_slist_free(chain);
424 return t;
425 }
427 /**
428 * Returns the transform that leads to the use from its immediate original.
429 * Does not inlcude the original's transform if any.
430 */
431 NR::Matrix
432 sp_use_get_parent_transform(SPUse *use)
433 {
434 NR::Matrix t(NR::identity());
435 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
436 t *= NR::translate(use->x._set ? use->x.computed : 0,
437 use->y._set ? use->y.computed : 0);
438 }
440 t *= SP_ITEM(use)->transform;
441 return t;
442 }
444 /**
445 * Sensing a movement of the original, this function attempts to compensate for it in such a way
446 * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
447 * clone's transform.
448 */
449 static void
450 sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
451 {
452 // the clone is orphaned; or this is not a real use, but a clone of another use;
453 // we skip it, otherwise duplicate compensation will occur
454 if (SP_OBJECT_IS_CLONED(self)) {
455 return;
456 }
458 // never compensate uses which are used in flowtext
459 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
460 return;
461 }
463 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
464 // user wants no compensation
465 if (mode == SP_CLONE_COMPENSATION_NONE)
466 return;
468 NR::Matrix m(*mp);
470 // this is not a simple move, do not try to compensate
471 if (!(m.is_translation()))
472 return;
474 // restore item->transform field from the repr, in case it was changed by seltrans
475 sp_object_read_attr (SP_OBJECT (self), "transform");
477 NR::Matrix t = sp_use_get_parent_transform(self);
478 NR::Matrix clone_move = t.inverse() * m * t;
480 // calculate the compensation matrix and the advertized movement matrix
481 NR::Matrix advertized_move;
482 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
483 clone_move = clone_move.inverse() * m;
484 advertized_move = m;
485 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
486 clone_move = clone_move.inverse();
487 advertized_move.set_identity();
488 } else {
489 g_assert_not_reached();
490 }
492 // commit the compensation
493 SPItem *item = SP_ITEM(self);
494 item->transform *= clone_move;
495 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
496 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
497 }
499 static void
500 sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
501 {
502 SPItem *item = SP_ITEM(use);
504 use->_delete_connection.disconnect();
505 use->_transformed_connection.disconnect();
507 if (use->child) {
508 sp_object_detach(SP_OBJECT(use), use->child);
509 use->child = NULL;
510 }
512 if (use->href) {
513 SPItem *refobj = use->ref->getObject();
514 if (refobj) {
515 Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
516 GType type = sp_repr_type_lookup(childrepr);
517 g_return_if_fail(type > G_TYPE_NONE);
518 if (g_type_is_a(type, SP_TYPE_ITEM)) {
519 use->child = (SPObject*) g_object_new(type, 0);
520 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
521 sp_object_unref(use->child, SP_OBJECT(use));
522 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
524 for (SPItemView *v = item->display; v != NULL; v = v->next) {
525 NRArenaItem *ai;
526 ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
527 if (ai) {
528 nr_arena_item_add_child(v->arenaitem, ai, NULL);
529 nr_arena_item_unref(ai);
530 }
531 }
533 }
534 use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
535 use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
536 }
537 }
538 }
540 static void
541 sp_use_delete_self(SPObject */*deleted*/, SPUse *self)
542 {
543 // always delete uses which are used in flowtext
544 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
545 SP_OBJECT(self)->deleteObject();
546 return;
547 }
549 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
550 SP_CLONE_ORPHANS_UNLINK);
552 if (mode == SP_CLONE_ORPHANS_UNLINK) {
553 sp_use_unlink(self);
554 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
555 SP_OBJECT(self)->deleteObject();
556 }
557 }
559 static void
560 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
561 {
562 SPItem *item = SP_ITEM(object);
563 SPUse *use = SP_USE(object);
564 SPItemCtx *ictx = (SPItemCtx *) ctx;
565 SPItemCtx cctx = *ictx;
567 if (((SPObjectClass *) (parent_class))->update)
568 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
570 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
571 flags &= SP_OBJECT_MODIFIED_CASCADE;
573 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
574 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
575 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
576 }
577 }
579 /* Set up child viewport */
580 if (use->x.unit == SVGLength::PERCENT) {
581 use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
582 }
583 if (use->y.unit == SVGLength::PERCENT) {
584 use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
585 }
586 if (use->width.unit == SVGLength::PERCENT) {
587 use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
588 }
589 if (use->height.unit == SVGLength::PERCENT) {
590 use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
591 }
592 cctx.vp.x0 = 0.0;
593 cctx.vp.y0 = 0.0;
594 cctx.vp.x1 = use->width.computed;
595 cctx.vp.y1 = use->height.computed;
596 cctx.i2vp = NR::identity();
597 flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
599 if (use->child) {
600 g_object_ref(G_OBJECT(use->child));
601 if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
602 if (SP_IS_ITEM(use->child)) {
603 SPItem const &chi = *SP_ITEM(use->child);
604 cctx.i2doc = chi.transform * ictx->i2doc;
605 cctx.i2vp = chi.transform * ictx->i2vp;
606 use->child->updateDisplay((SPCtx *)&cctx, flags);
607 } else {
608 use->child->updateDisplay(ctx, flags);
609 }
610 }
611 g_object_unref(G_OBJECT(use->child));
612 }
614 /* As last step set additional transform of arena group */
615 for (SPItemView *v = item->display; v != NULL; v = v->next) {
616 NRMatrix t;
617 nr_matrix_set_translate(&t, use->x.computed, use->y.computed);
618 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
619 }
620 }
622 static void
623 sp_use_modified(SPObject *object, guint flags)
624 {
625 SPUse *use_obj = SP_USE(object);
627 if (flags & SP_OBJECT_MODIFIED_FLAG) {
628 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
629 }
630 flags &= SP_OBJECT_MODIFIED_CASCADE;
632 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
633 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
634 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
635 }
636 }
638 SPObject *child = use_obj->child;
639 if (child) {
640 g_object_ref(G_OBJECT(child));
641 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
642 child->emitModified(flags);
643 }
644 g_object_unref(G_OBJECT(child));
645 }
646 }
648 SPItem *
649 sp_use_unlink(SPUse *use)
650 {
651 if (!use) return NULL;
653 Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
654 if (!repr) return NULL;
656 Inkscape::XML::Node *parent = sp_repr_parent(repr);
657 SPDocument *document = SP_OBJECT(use)->document;
658 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
660 // Track the ultimate source of a chain of uses.
661 SPItem *orig = sp_use_root(use);
663 // Calculate the accumulated transform, starting from the original.
664 NR::Matrix t = sp_use_get_root_transform(use);
666 Inkscape::XML::Node *copy = NULL;
667 if (SP_IS_SYMBOL(orig)) { // make a group, copy children
668 copy = xml_doc->createElement("svg:g");
669 for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
670 Inkscape::XML::Node *newchild = child->duplicate(xml_doc);
671 copy->appendChild(newchild);
672 }
673 } else { // just copy
674 copy = SP_OBJECT_REPR(orig)->duplicate(xml_doc);
675 }
677 // Add the duplicate repr just after the existing one.
678 parent->addChild(copy, repr);
680 // Retrieve the SPItem of the resulting repr.
681 SPObject *unlinked = document->getObjectByRepr(copy);
683 // Merge style from the use.
684 SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
685 SPStyle const *use_sty = SP_OBJECT_STYLE(use);
686 sp_style_merge_from_dying_parent(unli_sty, use_sty);
687 sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
689 SP_OBJECT(unlinked)->updateRepr();
691 // Hold onto our SPObject and repr for now.
692 sp_object_ref(SP_OBJECT(use), NULL);
693 Inkscape::GC::anchor(repr);
695 // Remove ourselves, not propagating delete events to avoid a
696 // chain-reaction with other elements that might reference us.
697 SP_OBJECT(use)->deleteObject(false);
699 // Give the copy our old id and let go of our old repr.
700 copy->setAttribute("id", repr->attribute("id"));
701 Inkscape::GC::release(repr);
703 // Remove tiled clone attrs.
704 copy->setAttribute("inkscape:tiled-clone-of", NULL);
705 copy->setAttribute("inkscape:tile-w", NULL);
706 copy->setAttribute("inkscape:tile-h", NULL);
707 copy->setAttribute("inkscape:tile-cx", NULL);
708 copy->setAttribute("inkscape:tile-cy", NULL);
710 // Establish the succession and let go of our object.
711 SP_OBJECT(use)->setSuccessor(unlinked);
712 sp_object_unref(SP_OBJECT(use), NULL);
714 SPItem *item = SP_ITEM(unlinked);
715 // Set the accummulated transform.
716 {
717 NR::Matrix nomove(NR::identity());
718 NRMatrix ctrans = t.operator const NRMatrix&();
719 // Advertise ourselves as not moving.
720 sp_item_write_transform(item, SP_OBJECT_REPR(item), &ctrans, &nomove);
721 }
722 return item;
723 }
725 SPItem *
726 sp_use_get_original(SPUse *use)
727 {
728 SPItem *ref = use->ref->getObject();
729 return ref;
730 }
732 static void
733 sp_use_snappoints(SPItem const *item, SnapPointsIter p)
734 {
735 g_assert (item != NULL);
736 g_assert (SP_IS_ITEM(item));
737 g_assert (SP_IS_USE(item));
739 SPUse *use = SP_USE(item);
740 SPItem *root = sp_use_root(use);
742 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(root);
743 if (item_class.snappoints) {
744 item_class.snappoints(root, p);
745 }
746 }
749 /*
750 Local Variables:
751 mode:c++
752 c-file-style:"stroustrup"
753 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
754 indent-tabs-mode:nil
755 fill-column:99
756 End:
757 */
758 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :