1 /*
2 * The reference corresponding to href of <use> element.
3 *
4 * Copyright (C) 2004 Bulia Byak
5 * Copyright (C) 2004 Monash University
6 *
7 * Released under GNU GPL, read the file 'COPYING' for more information.
8 */
10 #include <cstring>
11 #include <string>
12 #include <string.h>
14 #include "enums.h"
15 #include "sp-use-reference.h"
17 #include "display/curve.h"
18 #include "livarot/Path.h"
19 #include "preferences.h"
20 #include "sp-shape.h"
21 #include "sp-text.h"
22 #include "uri.h"
26 bool SPUseReference::_acceptObject(SPObject * const obj) const
27 {
28 if (SP_IS_ITEM(obj)) {
29 SPObject * const owner = getOwner();
30 /* Refuse references to us or to an ancestor. */
31 for ( SPObject *iter = owner ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
32 if ( iter == obj ) {
33 return false;
34 }
35 }
36 return true;
37 } else {
38 return false;
39 }
40 }
43 static void sp_usepath_href_changed(SPObject *old_ref, SPObject *ref, SPUsePath *offset);
44 static void sp_usepath_move_compensate(Geom::Matrix const *mp, SPItem *original, SPUsePath *self);
45 static void sp_usepath_delete_self(SPObject *deleted, SPUsePath *offset);
46 static void sp_usepath_source_modified(SPObject *iSource, guint flags, SPUsePath *offset);
48 SPUsePath::SPUsePath(SPObject* i_owner):SPUseReference(i_owner)
49 {
50 owner=i_owner;
51 originalPath = NULL;
52 sourceDirty=false;
53 sourceHref = NULL;
54 sourceRepr = NULL;
55 sourceObject = NULL;
56 _changed_connection = changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_usepath_href_changed), this)); // listening to myself, this should be virtual instead
58 user_unlink = NULL;
59 }
61 SPUsePath::~SPUsePath(void)
62 {
63 delete originalPath;
64 originalPath = NULL;
66 _changed_connection.disconnect(); // to do before unlinking
68 quit_listening();
69 unlink();
70 }
72 void
73 SPUsePath::link(char *to)
74 {
75 if ( to == NULL ) {
76 quit_listening();
77 unlink();
78 } else {
79 if ( !sourceHref || ( strcmp(to, sourceHref) != 0 ) ) {
80 g_free(sourceHref);
81 sourceHref = g_strdup(to);
82 try {
83 attach(Inkscape::URI(to));
84 } catch (Inkscape::BadURIException &e) {
85 /* TODO: Proper error handling as per
86 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
87 */
88 g_warning("%s", e.what());
89 detach();
90 }
91 }
92 }
93 }
95 void
96 SPUsePath::unlink(void)
97 {
98 g_free(sourceHref);
99 sourceHref = NULL;
100 detach();
101 }
103 void
104 SPUsePath::start_listening(SPObject* to)
105 {
106 if ( to == NULL ) {
107 return;
108 }
109 sourceObject = to;
110 sourceRepr = SP_OBJECT_REPR(to);
111 _delete_connection = to->connectDelete(sigc::bind(sigc::ptr_fun(&sp_usepath_delete_self), this));
112 _transformed_connection = SP_ITEM(to)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_usepath_move_compensate), this));
113 _modified_connection = to->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_usepath_source_modified), this));
114 }
116 void
117 SPUsePath::quit_listening(void)
118 {
119 if ( sourceObject == NULL ) {
120 return;
121 }
122 _modified_connection.disconnect();
123 _delete_connection.disconnect();
124 _transformed_connection.disconnect();
125 sourceRepr = NULL;
126 sourceObject = NULL;
127 }
129 static void
130 sp_usepath_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUsePath *offset)
131 {
132 offset->quit_listening();
133 SPItem *refobj = offset->getObject();
134 if ( refobj ) {
135 offset->start_listening(refobj);
136 }
137 offset->sourceDirty=true;
138 SP_OBJECT(offset->owner)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
139 }
141 static void
142 sp_usepath_move_compensate(Geom::Matrix const *mp, SPItem *original, SPUsePath *self)
143 {
144 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
145 guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL);
146 if (mode == SP_CLONE_COMPENSATION_NONE) {
147 return;
148 }
149 SPItem *item = SP_ITEM(self->owner);
151 // TODO kill naughty naughty #if 0
152 #if 0
153 Geom::Matrix m(*mp);
154 if (!(m.is_translation())) {
155 return;
156 }
157 Geom::Matrix const t(item->transform);
158 Geom::Matrix clone_move = t.inverse() * m * t;
160 // Calculate the compensation matrix and the advertized movement matrix.
161 Geom::Matrix advertized_move;
162 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
163 //clone_move = clone_move.inverse();
164 advertized_move.set_identity();
165 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
166 clone_move = clone_move.inverse() * m;
167 advertized_move = m;
168 } else {
169 g_assert_not_reached();
170 }
172 // Commit the compensation.
173 item->transform *= clone_move;
174 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
175 #else
176 (void)mp;
177 (void)original;
178 #endif
180 self->sourceDirty = true;
181 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
182 }
184 static void
185 sp_usepath_delete_self(SPObject */*deleted*/, SPUsePath *offset)
186 {
187 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
188 guint const mode = prefs->getInt("/options/cloneorphans/value", SP_CLONE_ORPHANS_UNLINK);
190 if (mode == SP_CLONE_ORPHANS_UNLINK) {
191 // leave it be. just forget about the source
192 offset->quit_listening();
193 offset->unlink();
194 if (offset->user_unlink)
195 offset->user_unlink(offset->owner);
196 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
197 offset->owner->deleteObject();
198 }
199 }
201 static void
202 sp_usepath_source_modified(SPObject */*iSource*/, guint /*flags*/, SPUsePath *offset)
203 {
204 offset->sourceDirty = true;
205 offset->owner->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 }
208 void SPUsePath::refresh_source()
209 {
210 sourceDirty = false;
211 delete originalPath;
212 originalPath = NULL;
214 // le mauvais cas: pas d'attribut d => il faut verifier que c'est une SPShape puis prendre le contour
215 // [tr: The bad case: no d attribute. Must check that it's a SPShape and then take the outline.]
216 SPObject *refobj = sourceObject;
217 if ( refobj == NULL ) return;
218 SPItem *item = SP_ITEM(refobj);
220 SPCurve *curve = NULL;
221 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
222 return;
223 }
224 if (SP_IS_SHAPE(item)) {
225 curve = sp_shape_get_curve(SP_SHAPE(item));
226 if (curve == NULL)
227 return;
228 }
229 if (SP_IS_TEXT(item)) {
230 curve = SP_TEXT(item)->getNormalizedBpath();
231 if (curve == NULL) {
232 return;
233 }
234 }
235 originalPath = new Path;
236 originalPath->LoadPathVector(curve->get_pathvector(), item->transform, true);
237 curve->unref();
238 }
241 /*
242 Local Variables:
243 mode:c++
244 c-file-style:"stroustrup"
245 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
246 indent-tabs-mode:nil
247 fill-column:99
248 End:
249 */
250 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :