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 <2geom/transforms.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 "preferences.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::Document *doc, 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, Geom::Matrix const &transform, unsigned const flags);
55 static void sp_use_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
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, Geom::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 if (use->child) {
143 sp_object_detach(SP_OBJECT(obj), use->child);
144 use->child = NULL;
145 }
147 use->ref->detach();
148 delete use->ref;
150 use->_delete_connection.~connection();
151 use->_changed_connection.~connection();
153 use->_transformed_connection.~connection();
154 }
156 static void
157 sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
158 {
159 if (((SPObjectClass *) parent_class)->build) {
160 (* ((SPObjectClass *) parent_class)->build)(object, document, repr);
161 }
163 sp_object_read_attr(object, "x");
164 sp_object_read_attr(object, "y");
165 sp_object_read_attr(object, "width");
166 sp_object_read_attr(object, "height");
167 sp_object_read_attr(object, "xlink:href");
169 // We don't need to create child here:
170 // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
171 // which will call sp_use_href_changed, and that will take care of the child
172 }
174 static void
175 sp_use_release(SPObject *object)
176 {
177 SPUse *use = SP_USE(object);
179 if (use->child) {
180 sp_object_detach(object, use->child);
181 use->child = NULL;
182 }
184 use->_delete_connection.disconnect();
185 use->_changed_connection.disconnect();
186 use->_transformed_connection.disconnect();
188 g_free(use->href);
189 use->href = NULL;
191 use->ref->detach();
193 if (((SPObjectClass *) parent_class)->release) {
194 ((SPObjectClass *) parent_class)->release(object);
195 }
196 }
198 static void
199 sp_use_set(SPObject *object, unsigned key, gchar const *value)
200 {
201 SPUse *use = SP_USE(object);
203 switch (key) {
204 case SP_ATTR_X:
205 use->x.readOrUnset(value);
206 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
207 break;
208 case SP_ATTR_Y:
209 use->y.readOrUnset(value);
210 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
211 break;
212 case SP_ATTR_WIDTH:
213 use->width.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
214 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
215 break;
216 case SP_ATTR_HEIGHT:
217 use->height.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
218 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
219 break;
221 case SP_ATTR_XLINK_HREF: {
222 if ( value && use->href && ( strcmp(value, use->href) == 0 ) ) {
223 /* No change, do nothing. */
224 } else {
225 g_free(use->href);
226 use->href = NULL;
227 if (value) {
228 // First, set the href field, because sp_use_href_changed will need it.
229 use->href = g_strdup(value);
231 // Now do the attaching, which emits the changed signal.
232 try {
233 use->ref->attach(Inkscape::URI(value));
234 } catch (Inkscape::BadURIException &e) {
235 g_warning("%s", e.what());
236 use->ref->detach();
237 }
238 } else {
239 use->ref->detach();
240 }
241 }
242 break;
243 }
245 default:
246 if (((SPObjectClass *) parent_class)->set) {
247 ((SPObjectClass *) parent_class)->set(object, key, value);
248 }
249 break;
250 }
251 }
253 static Inkscape::XML::Node *
254 sp_use_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
255 {
256 SPUse *use = SP_USE(object);
258 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
259 repr = xml_doc->createElement("svg:use");
260 }
262 if (((SPObjectClass *) (parent_class))->write) {
263 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
264 }
266 sp_repr_set_svg_double(repr, "x", use->x.computed);
267 sp_repr_set_svg_double(repr, "y", use->y.computed);
268 sp_repr_set_svg_double(repr, "width", use->width.computed);
269 sp_repr_set_svg_double(repr, "height", use->height.computed);
271 if (use->ref->getURI()) {
272 gchar *uri_string = use->ref->getURI()->toString();
273 repr->setAttribute("xlink:href", uri_string);
274 g_free(uri_string);
275 }
277 return repr;
278 }
280 static void
281 sp_use_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
282 {
283 SPUse const *use = SP_USE(item);
285 if (use->child && SP_IS_ITEM(use->child)) {
286 SPItem *child = SP_ITEM(use->child);
287 Geom::Matrix const ct( child->transform
288 * Geom::Translate(use->x.computed,
289 use->y.computed)
290 * transform );
291 Geom::OptRect optbbox;
292 sp_item_invoke_bbox_full(child, optbbox, ct, flags, FALSE);
293 if (optbbox) {
294 bbox->x0 = (*optbbox)[0][0];
295 bbox->y0 = (*optbbox)[1][0];
296 bbox->x1 = (*optbbox)[0][1];
297 bbox->y1 = (*optbbox)[1][1];
298 }
299 }
300 }
302 static void
303 sp_use_print(SPItem *item, SPPrintContext *ctx)
304 {
305 bool translated = false;
306 SPUse *use = SP_USE(item);
308 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
309 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
310 sp_print_bind(ctx, tp, 1.0);
311 translated = true;
312 }
314 if (use->child && SP_IS_ITEM(use->child)) {
315 sp_item_invoke_print(SP_ITEM(use->child), ctx);
316 }
318 if (translated) {
319 sp_print_release(ctx);
320 }
321 }
323 static gchar *
324 sp_use_description(SPItem *item)
325 {
326 SPUse *use = SP_USE(item);
328 char *ret;
329 if (use->child) {
330 static unsigned recursion_depth = 0;
331 if (recursion_depth >= 4) {
332 /* TRANSLATORS: Used for statusbar description for long <use> chains:
333 * "Clone of: Clone of: ... in Layer 1". */
334 return g_strdup(_("..."));
335 /* We could do better, e.g. chasing the href chain until we reach something other than
336 * a <use>, and giving its description. */
337 }
338 ++recursion_depth;
339 char *child_desc = sp_item_description(SP_ITEM(use->child));
340 --recursion_depth;
342 ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
343 g_free(child_desc);
344 return ret;
345 } else {
346 return g_strdup(_("<b>Orphaned clone</b>"));
347 }
348 }
350 static NRArenaItem *
351 sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
352 {
353 SPUse *use = SP_USE(item);
355 NRArenaItem *ai = NRArenaGroup::create(arena);
356 nr_arena_group_set_transparent(NR_ARENA_GROUP(ai), FALSE);
357 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
359 if (use->child) {
360 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
361 if (ac) {
362 nr_arena_item_add_child(ai, ac, NULL);
363 }
364 Geom::Translate t(use->x.computed,
365 use->y.computed);
366 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), Geom::Matrix(t));
367 }
369 return ai;
370 }
372 static void
373 sp_use_hide(SPItem *item, unsigned key)
374 {
375 SPUse *use = SP_USE(item);
377 if (use->child) {
378 sp_item_invoke_hide(SP_ITEM(use->child), key);
379 }
381 if (((SPItemClass *) parent_class)->hide) {
382 ((SPItemClass *) parent_class)->hide(item, key);
383 }
384 }
386 /**
387 * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
388 * which is not an SPUse). If no original is found, NULL is returned (it is the responsibility
389 * of the caller to make sure that this is handled correctly).
390 *
391 * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
392 * the trivial case) and not the "true original".
393 */
394 SPItem *
395 sp_use_root(SPUse *use)
396 {
397 SPObject *orig = use->child;
398 while (orig && SP_IS_USE(orig)) {
399 orig = SP_USE(orig)->child;
400 }
401 if (!orig || !SP_IS_ITEM(orig))
402 return NULL;
403 return SP_ITEM(orig);
404 }
406 /**
407 * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
408 * included.
409 */
410 Geom::Matrix
411 sp_use_get_root_transform(SPUse *use)
412 {
413 //track the ultimate source of a chain of uses
414 SPObject *orig = use->child;
415 GSList *chain = NULL;
416 chain = g_slist_prepend(chain, use);
417 while (SP_IS_USE(orig)) {
418 chain = g_slist_prepend(chain, orig);
419 orig = SP_USE(orig)->child;
420 }
421 chain = g_slist_prepend(chain, orig);
424 //calculate the accummulated transform, starting from the original
425 Geom::Matrix t(Geom::identity());
426 for (GSList *i = chain; i != NULL; i = i->next) {
427 SPItem *i_tem = SP_ITEM(i->data);
429 // "An additional transformation translate(x,y) is appended to the end (i.e.,
430 // right-side) of the transform attribute on the generated 'g', where x and y
431 // represent the values of the x and y attributes on the 'use' element." - http://www.w3.org/TR/SVG11/struct.html#UseElement
432 if (SP_IS_USE(i_tem)) {
433 SPUse *i_use = SP_USE(i_tem);
434 if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
435 t = t * Geom::Translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
436 }
437 }
439 t *= i_tem->transform;
440 }
442 g_slist_free(chain);
443 return t;
444 }
446 /**
447 * Returns the transform that leads to the use from its immediate original.
448 * Does not inlcude the original's transform if any.
449 */
450 Geom::Matrix
451 sp_use_get_parent_transform(SPUse *use)
452 {
453 Geom::Matrix t(Geom::identity());
454 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
455 t *= Geom::Translate(use->x._set ? use->x.computed : 0,
456 use->y._set ? use->y.computed : 0);
457 }
459 t *= SP_ITEM(use)->transform;
460 return t;
461 }
463 /**
464 * Sensing a movement of the original, this function attempts to compensate for it in such a way
465 * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the
466 * clone's transform.
467 */
468 static void
469 sp_use_move_compensate(Geom::Matrix const *mp, SPItem */*original*/, SPUse *self)
470 {
471 // the clone is orphaned; or this is not a real use, but a clone of another use;
472 // we skip it, otherwise duplicate compensation will occur
473 if (SP_OBJECT_IS_CLONED(self)) {
474 return;
475 }
477 // never compensate uses which are used in flowtext
478 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
479 return;
480 }
482 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
483 guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL);
484 // user wants no compensation
485 if (mode == SP_CLONE_COMPENSATION_NONE)
486 return;
488 Geom::Matrix m(*mp);
490 // this is not a simple move, do not try to compensate
491 if (!(m.isTranslation()))
492 return;
494 // restore item->transform field from the repr, in case it was changed by seltrans
495 sp_object_read_attr (SP_OBJECT (self), "transform");
497 Geom::Matrix t = sp_use_get_parent_transform(self);
498 Geom::Matrix clone_move = t.inverse() * m * t;
500 // calculate the compensation matrix and the advertized movement matrix
501 Geom::Matrix advertized_move;
502 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
503 clone_move = clone_move.inverse() * m;
504 advertized_move = m;
505 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
506 clone_move = clone_move.inverse();
507 advertized_move.setIdentity();
508 } else {
509 g_assert_not_reached();
510 }
512 // commit the compensation
513 SPItem *item = SP_ITEM(self);
514 item->transform *= clone_move;
515 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
516 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
517 }
519 static void
520 sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
521 {
522 SPItem *item = SP_ITEM(use);
524 use->_delete_connection.disconnect();
525 use->_transformed_connection.disconnect();
527 if (use->child) {
528 sp_object_detach(SP_OBJECT(use), use->child);
529 use->child = NULL;
530 }
532 if (use->href) {
533 SPItem *refobj = use->ref->getObject();
534 if (refobj) {
535 Inkscape::XML::Node *childrepr = SP_OBJECT_REPR(refobj);
536 GType type = sp_repr_type_lookup(childrepr);
537 g_return_if_fail(type > G_TYPE_NONE);
538 if (g_type_is_a(type, SP_TYPE_ITEM)) {
539 use->child = (SPObject*) g_object_new(type, 0);
540 sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
541 sp_object_unref(use->child, SP_OBJECT(use));
542 sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
544 for (SPItemView *v = item->display; v != NULL; v = v->next) {
545 NRArenaItem *ai;
546 ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
547 if (ai) {
548 nr_arena_item_add_child(v->arenaitem, ai, NULL);
549 }
550 }
552 }
553 use->_delete_connection = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_use_delete_self), use));
554 use->_transformed_connection = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_use_move_compensate), use));
555 }
556 }
557 }
559 static void
560 sp_use_delete_self(SPObject */*deleted*/, SPUse *self)
561 {
562 // always delete uses which are used in flowtext
563 if (SP_OBJECT_PARENT(self) && SP_IS_FLOWREGION(SP_OBJECT_PARENT(self))) {
564 SP_OBJECT(self)->deleteObject();
565 return;
566 }
568 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
569 guint const mode = prefs->getInt("/options/cloneorphans/value",
570 SP_CLONE_ORPHANS_UNLINK);
572 if (mode == SP_CLONE_ORPHANS_UNLINK) {
573 sp_use_unlink(self);
574 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
575 SP_OBJECT(self)->deleteObject();
576 }
577 }
579 static void
580 sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
581 {
582 SPItem *item = SP_ITEM(object);
583 SPUse *use = SP_USE(object);
584 SPItemCtx *ictx = (SPItemCtx *) ctx;
585 SPItemCtx cctx = *ictx;
587 if (((SPObjectClass *) (parent_class))->update)
588 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
590 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
591 flags &= SP_OBJECT_MODIFIED_CASCADE;
593 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
594 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
595 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
596 }
597 }
599 /* Set up child viewport */
600 if (use->x.unit == SVGLength::PERCENT) {
601 use->x.computed = use->x.value * (ictx->vp.x1 - ictx->vp.x0);
602 }
603 if (use->y.unit == SVGLength::PERCENT) {
604 use->y.computed = use->y.value * (ictx->vp.y1 - ictx->vp.y0);
605 }
606 if (use->width.unit == SVGLength::PERCENT) {
607 use->width.computed = use->width.value * (ictx->vp.x1 - ictx->vp.x0);
608 }
609 if (use->height.unit == SVGLength::PERCENT) {
610 use->height.computed = use->height.value * (ictx->vp.y1 - ictx->vp.y0);
611 }
612 cctx.vp.x0 = 0.0;
613 cctx.vp.y0 = 0.0;
614 cctx.vp.x1 = use->width.computed;
615 cctx.vp.y1 = use->height.computed;
616 cctx.i2vp = Geom::identity();
617 flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
619 if (use->child) {
620 g_object_ref(G_OBJECT(use->child));
621 if (flags || (use->child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
622 if (SP_IS_ITEM(use->child)) {
623 SPItem const &chi = *SP_ITEM(use->child);
624 cctx.i2doc = chi.transform * ictx->i2doc;
625 cctx.i2vp = chi.transform * ictx->i2vp;
626 use->child->updateDisplay((SPCtx *)&cctx, flags);
627 } else {
628 use->child->updateDisplay(ctx, flags);
629 }
630 }
631 g_object_unref(G_OBJECT(use->child));
632 }
634 /* As last step set additional transform of arena group */
635 for (SPItemView *v = item->display; v != NULL; v = v->next) {
636 Geom::Matrix t(Geom::Translate(use->x.computed, use->y.computed));
637 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), t);
638 }
639 }
641 static void
642 sp_use_modified(SPObject *object, guint flags)
643 {
644 SPUse *use_obj = SP_USE(object);
646 if (flags & SP_OBJECT_MODIFIED_FLAG) {
647 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
648 }
649 flags &= SP_OBJECT_MODIFIED_CASCADE;
651 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
652 for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
653 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
654 }
655 }
657 SPObject *child = use_obj->child;
658 if (child) {
659 g_object_ref(G_OBJECT(child));
660 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
661 child->emitModified(flags);
662 }
663 g_object_unref(G_OBJECT(child));
664 }
665 }
667 SPItem *
668 sp_use_unlink(SPUse *use)
669 {
670 if (!use) return NULL;
672 Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
673 if (!repr) return NULL;
675 Inkscape::XML::Node *parent = sp_repr_parent(repr);
676 SPDocument *document = SP_OBJECT(use)->document;
677 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
679 // Track the ultimate source of a chain of uses.
680 SPItem *orig = sp_use_root(use);
681 g_return_val_if_fail(orig, NULL);
683 // Calculate the accumulated transform, starting from the original.
684 Geom::Matrix t = sp_use_get_root_transform(use);
686 Inkscape::XML::Node *copy = NULL;
687 if (SP_IS_SYMBOL(orig)) { // make a group, copy children
688 copy = xml_doc->createElement("svg:g");
689 for (Inkscape::XML::Node *child = SP_OBJECT_REPR(orig)->firstChild() ; child != NULL; child = child->next()) {
690 Inkscape::XML::Node *newchild = child->duplicate(xml_doc);
691 copy->appendChild(newchild);
692 }
693 } else { // just copy
694 copy = SP_OBJECT_REPR(orig)->duplicate(xml_doc);
695 }
697 // Add the duplicate repr just after the existing one.
698 parent->addChild(copy, repr);
700 // Retrieve the SPItem of the resulting repr.
701 SPObject *unlinked = document->getObjectByRepr(copy);
703 // Merge style from the use.
704 SPStyle *unli_sty = SP_OBJECT_STYLE(unlinked);
705 SPStyle const *use_sty = SP_OBJECT_STYLE(use);
706 sp_style_merge_from_dying_parent(unli_sty, use_sty);
707 sp_style_merge_from_parent(unli_sty, unlinked->parent->style);
709 SP_OBJECT(unlinked)->updateRepr();
711 // Hold onto our SPObject and repr for now.
712 sp_object_ref(SP_OBJECT(use), NULL);
713 Inkscape::GC::anchor(repr);
715 // Remove ourselves, not propagating delete events to avoid a
716 // chain-reaction with other elements that might reference us.
717 SP_OBJECT(use)->deleteObject(false);
719 // Give the copy our old id and let go of our old repr.
720 copy->setAttribute("id", repr->attribute("id"));
721 Inkscape::GC::release(repr);
723 // Remove tiled clone attrs.
724 copy->setAttribute("inkscape:tiled-clone-of", NULL);
725 copy->setAttribute("inkscape:tile-w", NULL);
726 copy->setAttribute("inkscape:tile-h", NULL);
727 copy->setAttribute("inkscape:tile-cx", NULL);
728 copy->setAttribute("inkscape:tile-cy", NULL);
730 // Establish the succession and let go of our object.
731 SP_OBJECT(use)->setSuccessor(unlinked);
732 sp_object_unref(SP_OBJECT(use), NULL);
734 SPItem *item = SP_ITEM(unlinked);
735 // Set the accummulated transform.
736 {
737 Geom::Matrix nomove(Geom::identity());
738 // Advertise ourselves as not moving.
739 sp_item_write_transform(item, SP_OBJECT_REPR(item), t, &nomove);
740 }
741 return item;
742 }
744 SPItem *
745 sp_use_get_original(SPUse *use)
746 {
747 SPItem *ref = use->ref->getObject();
748 return ref;
749 }
751 static void
752 sp_use_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
753 {
754 g_assert (item != NULL);
755 g_assert (SP_IS_ITEM(item));
756 g_assert (SP_IS_USE(item));
758 SPUse *use = SP_USE(item);
759 SPItem *root = sp_use_root(use);
760 if (!root)
761 return;
763 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(root);
764 if (item_class.snappoints) {
765 item_class.snappoints(root, p, snapprefs);
766 }
767 }
770 /*
771 Local Variables:
772 mode:c++
773 c-file-style:"stroustrup"
774 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
775 indent-tabs-mode:nil
776 fill-column:99
777 End:
778 */
779 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :