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 "prefs-utils.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(NR::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(NR::Matrix const *mp, SPItem *original, SPUsePath *self)
143 {
144 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
145 if (mode == SP_CLONE_COMPENSATION_NONE) {
146 return;
147 }
148 SPItem *item = SP_ITEM(self->owner);
150 // TODO kill naughty naughty #if 0
151 #if 0
152 NR::Matrix m(*mp);
153 if (!(m.is_translation())) {
154 return;
155 }
156 NR::Matrix const t(item->transform);
157 NR::Matrix clone_move = t.inverse() * m * t;
159 // Calculate the compensation matrix and the advertized movement matrix.
160 NR::Matrix advertized_move;
161 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
162 //clone_move = clone_move.inverse();
163 advertized_move.set_identity();
164 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
165 clone_move = clone_move.inverse() * m;
166 advertized_move = m;
167 } else {
168 g_assert_not_reached();
169 }
171 // Commit the compensation.
172 item->transform *= clone_move;
173 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
174 #else
175 (void)mp;
176 (void)original;
177 #endif
179 self->sourceDirty = true;
180 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
181 }
183 static void
184 sp_usepath_delete_self(SPObject */*deleted*/, SPUsePath *offset)
185 {
186 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value", SP_CLONE_ORPHANS_UNLINK);
188 if (mode == SP_CLONE_ORPHANS_UNLINK) {
189 // leave it be. just forget about the source
190 offset->quit_listening();
191 offset->unlink();
192 if (offset->user_unlink)
193 offset->user_unlink(offset->owner);
194 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
195 offset->owner->deleteObject();
196 }
197 }
199 static void
200 sp_usepath_source_modified(SPObject */*iSource*/, guint /*flags*/, SPUsePath *offset)
201 {
202 offset->sourceDirty = true;
203 offset->owner->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
204 }
206 void SPUsePath::refresh_source()
207 {
208 sourceDirty = false;
209 delete originalPath;
210 originalPath = NULL;
212 // le mauvais cas: pas d'attribut d => il faut verifier que c'est une SPShape puis prendre le contour
213 // [tr: The bad case: no d attribute. Must check that it's a SPShape and then take the outline.]
214 SPObject *refobj = sourceObject;
215 if ( refobj == NULL ) return;
216 SPItem *item = SP_ITEM(refobj);
218 SPCurve *curve = NULL;
219 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
220 return;
221 }
222 if (SP_IS_SHAPE(item)) {
223 curve = sp_shape_get_curve(SP_SHAPE(item));
224 if (curve == NULL)
225 return;
226 }
227 if (SP_IS_TEXT(item)) {
228 curve = SP_TEXT(item)->getNormalizedBpath();
229 if (curve == NULL) {
230 return;
231 }
232 }
233 originalPath = new Path;
234 originalPath->LoadPathVector(curve->get_pathvector(), to_2geom(item->transform), true);
235 curve->unref();
236 }
239 /*
240 Local Variables:
241 mode:c++
242 c-file-style:"stroustrup"
243 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
244 indent-tabs-mode:nil
245 fill-column:99
246 End:
247 */
248 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :