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 SPUse *use = SP_USE(item);
293 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
294 NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
295 sp_print_bind(ctx, tp, 1.0);
296 translated = true;
297 }
299 if (use->child && SP_IS_ITEM(use->child)) {
300 sp_item_invoke_print(SP_ITEM(use->child), ctx);
301 }
303 if (translated) {
304 sp_print_release(ctx);
305 }
306 }
308 static gchar *
309 sp_use_description(SPItem *item)
310 {
311 SPUse *use = SP_USE(item);
313 char *ret;
314 if (use->child) {
315 static unsigned recursion_depth = 0;
316 if (recursion_depth >= 4) {
317 /* TRANSLATORS: Used for statusbar description for long <use> chains:
318 * "Clone of: Clone of: ... in Layer 1". */
319 return g_strdup(_("..."));
320 /* We could do better, e.g. chasing the href chain until we reach something other than
321 * a <use>, and giving its description. */
322 }
323 ++recursion_depth;
324 char *child_desc = sp_item_description(SP_ITEM(use->child));
325 --recursion_depth;
327 ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
328 g_free(child_desc);
329 return ret;
330 } else {
331 return g_strdup(_("<b>Orphaned clone</b>"));
332 }
333 }
335 static NRArenaItem *
336 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
337 {
338 SPUse *use = SP_USE(item);
340 NRArenaItem *ai = NRArenaGroup::create(arena);
341 nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
342 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
344 if (use->child) {
345 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
346 if (ac) {
347 nr_arena_item_add_child(ai, ac, NULL);
348 nr_arena_item_unref(ac);
349 }
350 NR::translate t(use->x.computed,
351 use->y.computed);
352 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
353 }
355 return ai;
356 }
358 static void
359 sp_use_hide(SPItem *item, unsigned key)
360 {
361 SPUse *use = SP_USE(item);
363 if (use->child) {
364 sp_item_invoke_hide(SP_ITEM(use->child), key);
365 }
367 if (((SPItemClass *) parent_class)->hide) {
368 ((SPItemClass *) parent_class)->hide(item, key);
369 }
370 }
372 /**
373 * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
374 * which is not an SPUse). If no original is found, NULL is returned (it is the responsibility
375 * of the caller to make sure that this is handled correctly).
376 *
377 * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
378 * the trivial case) and not the "true original".
379 */
380 SPItem *
381 sp_use_root(SPUse *use)
382 {
383 SPObject *orig = use->child;
384 while (SP_IS_USE(orig)) {
385 orig = SP_USE(orig)->child;
386 }
387 g_return_val_if_fail(SP_IS_ITEM(orig), NULL);
388 return SP_ITEM(orig);
389 }
391 /**
392 * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
393 * included.
394 */
395 NR::Matrix
396 sp_use_get_root_transform(SPUse *use)
397 {
398 //track the ultimate source of a chain of uses
399 SPObject *orig = use->child;
400 GSList *chain = NULL;
401 chain = g_slist_prepend(chain, use);
402 while (SP_IS_USE(orig)) {
403 chain = g_slist_prepend(chain, orig);
404 orig = SP_USE(orig)->child;
405 }
406 chain = g_slist_prepend(chain, orig);
409 //calculate the accummulated transform, starting from the original
410 NR::Matrix t(NR::identity());
411 for (GSList *i = chain; i != NULL; i = i->next) {
412 SPItem *i_tem = SP_ITEM(i->data);
414 // "An additional transformation translate(x,y) is appended to the end (i.e.,
415 // right-side) of the transform attribute on the generated 'g', where x and y
416 // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
417 if (SP_IS_USE(i_tem)) {
418 SPUse *i_use = SP_USE(i_tem);
419 if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
420 t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
421 }
422 }
424 t *= i_tem->transform;
425 }
427 g_slist_free(chain);
428 return t;
429 }
431 /**
432 * Returns the transform that leads to the use from its immediate original.
433 * Does not inlcude the original's transform if any.
434 */
435 NR::Matrix
436 sp_use_get_parent_transform(SPUse *use)
437 {
438 NR::Matrix t(NR::identity());
439 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
440 t *= NR::translate(use->x._set ? use->x.computed : 0,
441 use->y._set ? use->y.computed : 0);
442 }
444 t *= SP_ITEM(use)->transform;
445 return t;
446 }
448 /**
449 * Sensing a movement of the original, this function attempts to compensate for it in such a way
450 * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
451 * clone's transform.
452 */
453 static void
454 sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
455 {
456 // the clone is orphaned; or this is not a real use, but a clone of another use;
457 // we skip it, otherwise duplicate compensation will occur
458 if (SP_OBJECT_IS_CLONED(self)) {
459 return;
460 }
462 // never compensate uses which are used in flowtext
463 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
464 return;
465 }
467 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
468 // user wants no compensation
469 if (mode == SP_CLONE_COMPENSATION_NONE)
470 return;
472 NR::Matrix m(*mp);
474 // this is not a simple move, do not try to compensate
475 if (!(m.is_translation()))
476 return;
478 // restore item->transform field from the repr, in case it was changed by seltrans
479 sp_object_read_attr (SP_OBJECT (self), "transform");
481 NR::Matrix t = sp_use_get_parent_transform(self);
482 NR::Matrix clone_move = t.inverse() * m * t;
484 // calculate the compensation matrix and the advertized movement matrix
485 NR::Matrix advertized_move;
486 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
487 clone_move = clone_move.inverse() * m;
488 advertized_move = m;
489 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
490 clone_move = clone_move.inverse();
491 advertized_move.set_identity();
492 } else {
493 g_assert_not_reached();
494 }
496 // commit the compensation
497 SPItem *item = SP_ITEM(self);
498 item->transform *= clone_move;
499 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
500 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
501 }
503 static void
504 sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
505 {
506 SPItem *item = SP_ITEM(use);
508 use->_delete_connection.disconnect();
509 use->_transformed_connection.disconnect();
511 if (use->child) {
512 sp_object_detach(SP_OBJECT(use), use->child);
513 use->child = NULL;
514 }
516 if (use->href) {
517 SPItem *refobj = use->ref->getObject();
518 if (refobj) {
519 Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
520 GType type = sp_repr_type_lookup(childrepr);
521 g_return_if_fail(type > G_TYPE_NONE);
522 if (g_type_is_a(type, SP_TYPE_ITEM)) {
523 use->child = (SPObject*) g_object_new(type, 0);
524 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
525 sp_object_unref(use->child, SP_OBJECT(use));
526 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
528 for (SPItemView *v = item->display; v != NULL; v = v->next) {
529 NRArenaItem *ai;
530 ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
531 if (ai) {
532 nr_arena_item_add_child(v->arenaitem, ai, NULL);
533 nr_arena_item_unref(ai);
534 }
535 }
537 }
538 use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
539 use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
540 }
541 }
542 }
544 static void
545 sp_use_delete_self(SPObject */*deleted*/, SPUse *self)
546 {
547 // always delete uses which are used in flowtext
548 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
549 SP_OBJECT(self)->deleteObject();
550 return;
551 }
553 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
554 SP_CLONE_ORPHANS_UNLINK);
556 if (mode == SP_CLONE_ORPHANS_UNLINK) {
557 sp_use_unlink(self);
558 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
559 SP_OBJECT(self)->deleteObject();
560 }
561 }
563 static void
564 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
565 {
566 SPItem *item = SP_ITEM(object);
567 SPUse *use = SP_USE(object);
568 SPItemCtx *ictx = (SPItemCtx *) ctx;
569 SPItemCtx cctx = *ictx;
571 if (((SPObjectClass *) (parent_class))->update)
572 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
574 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
575 flags &= SP_OBJECT_MODIFIED_CASCADE;
577 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
578 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
579 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
580 }
581 }
583 /* Set up child viewport */
584 if (use->x.unit == SVGLength::PERCENT) {
585 use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
586 }
587 if (use->y.unit == SVGLength::PERCENT) {
588 use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
589 }
590 if (use->width.unit == SVGLength::PERCENT) {
591 use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
592 }
593 if (use->height.unit == SVGLength::PERCENT) {
594 use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
595 }
596 cctx.vp.x0 = 0.0;
597 cctx.vp.y0 = 0.0;
598 cctx.vp.x1 = use->width.computed;
599 cctx.vp.y1 = use->height.computed;
600 cctx.i2vp = NR::identity();
601 flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
603 if (use->child) {
604 g_object_ref(G_OBJECT(use->child));
605 if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
606 if (SP_IS_ITEM(use->child)) {
607 SPItem const &chi = *SP_ITEM(use->child);
608 cctx.i2doc = chi.transform * ictx->i2doc;
609 cctx.i2vp = chi.transform * ictx->i2vp;
610 use->child->updateDisplay((SPCtx *)&cctx, flags);
611 } else {
612 use->child->updateDisplay(ctx, flags);
613 }
614 }
615 g_object_unref(G_OBJECT(use->child));
616 }
618 /* As last step set additional transform of arena group */
619 for (SPItemView *v = item->display; v != NULL; v = v->next) {
620 NR::Matrix t(NR::translate(use->x.computed, use->y.computed));
621 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), t);
622 }
623 }
625 static void
626 sp_use_modified(SPObject *object, guint flags)
627 {
628 SPUse *use_obj = SP_USE(object);
630 if (flags & SP_OBJECT_MODIFIED_FLAG) {
631 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
632 }
633 flags &= SP_OBJECT_MODIFIED_CASCADE;
635 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
636 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
637 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
638 }
639 }
641 SPObject *child = use_obj->child;
642 if (child) {
643 g_object_ref(G_OBJECT(child));
644 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
645 child->emitModified(flags);
646 }
647 g_object_unref(G_OBJECT(child));
648 }
649 }
651 SPItem *
652 sp_use_unlink(SPUse *use)
653 {
654 if (!use) return NULL;
656 Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
657 if (!repr) return NULL;
659 Inkscape::XML::Node *parent = sp_repr_parent(repr);
660 SPDocument *document = SP_OBJECT(use)->document;
661 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
663 // Track the ultimate source of a chain of uses.
664 SPItem *orig = sp_use_root(use);
665 g_return_val_if_fail(orig, NULL);
667 // Calculate the accumulated transform, starting from the original.
668 NR::Matrix t = sp_use_get_root_transform(use);
670 Inkscape::XML::Node *copy = NULL;
671 if (SP_IS_SYMBOL(orig)) { // make a group, copy children
672 copy = xml_doc->createElement("svg:g");
673 for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
674 Inkscape::XML::Node *newchild = child->duplicate(xml_doc);
675 copy->appendChild(newchild);
676 }
677 } else { // just copy
678 copy = SP_OBJECT_REPR(orig)->duplicate(xml_doc);
679 }
681 // Add the duplicate repr just after the existing one.
682 parent->addChild(copy, repr);
684 // Retrieve the SPItem of the resulting repr.
685 SPObject *unlinked = document->getObjectByRepr(copy);
687 // Merge style from the use.
688 SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
689 SPStyle const *use_sty = SP_OBJECT_STYLE(use);
690 sp_style_merge_from_dying_parent(unli_sty, use_sty);
691 sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
693 SP_OBJECT(unlinked)->updateRepr();
695 // Hold onto our SPObject and repr for now.
696 sp_object_ref(SP_OBJECT(use), NULL);
697 Inkscape::GC::anchor(repr);
699 // Remove ourselves, not propagating delete events to avoid a
700 // chain-reaction with other elements that might reference us.
701 SP_OBJECT(use)->deleteObject(false);
703 // Give the copy our old id and let go of our old repr.
704 copy->setAttribute("id", repr->attribute("id"));
705 Inkscape::GC::release(repr);
707 // Remove tiled clone attrs.
708 copy->setAttribute("inkscape:tiled-clone-of", NULL);
709 copy->setAttribute("inkscape:tile-w", NULL);
710 copy->setAttribute("inkscape:tile-h", NULL);
711 copy->setAttribute("inkscape:tile-cx", NULL);
712 copy->setAttribute("inkscape:tile-cy", NULL);
714 // Establish the succession and let go of our object.
715 SP_OBJECT(use)->setSuccessor(unlinked);
716 sp_object_unref(SP_OBJECT(use), NULL);
718 SPItem *item = SP_ITEM(unlinked);
719 // Set the accummulated transform.
720 {
721 NR::Matrix nomove(NR::identity());
722 // Advertise ourselves as not moving.
723 sp_item_write_transform(item, SP_OBJECT_REPR(item), t, &nomove);
724 }
725 return item;
726 }
728 SPItem *
729 sp_use_get_original(SPUse *use)
730 {
731 SPItem *ref = use->ref->getObject();
732 return ref;
733 }
735 static void
736 sp_use_snappoints(SPItem const *item, SnapPointsIter p)
737 {
738 g_assert (item != NULL);
739 g_assert (SP_IS_ITEM(item));
740 g_assert (SP_IS_USE(item));
742 SPUse *use = SP_USE(item);
743 SPItem *root = sp_use_root(use);
744 g_return_if_fail(root);
746 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(root);
747 if (item_class.snappoints) {
748 item_class.snappoints(root, p);
749 }
750 }
753 /*
754 Local Variables:
755 mode:c++
756 c-file-style:"stroustrup"
757 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
758 indent-tabs-mode:nil
759 fill-column:99
760 End:
761 */
762 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :