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 "enums.h"
11 #include "sp-use-reference.h"
13 #include "display/curve.h"
14 #include "livarot/Path.h"
15 #include "prefs-utils.h"
16 #include "sp-shape.h"
17 #include "sp-text.h"
18 #include "uri.h"
22 bool SPUseReference::_acceptObject(SPObject * const obj) const
23 {
24 if (SP_IS_ITEM(obj)) {
25 SPObject * const owner = getOwner();
26 /* Refuse references to us or to an ancestor. */
27 for ( SPObject *iter = owner ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
28 if ( iter == obj ) {
29 return false;
30 }
31 }
32 return true;
33 } else {
34 return false;
35 }
36 }
39 static void sp_usepath_href_changed(SPObject *old_ref, SPObject *ref, SPUsePath *offset);
40 static void sp_usepath_move_compensate(NR::Matrix const *mp, SPItem *original, SPUsePath *self);
41 static void sp_usepath_delete_self(SPObject *deleted, SPUsePath *offset);
42 static void sp_usepath_source_modified(SPObject *iSource, guint flags, SPUsePath *offset);
44 SPUsePath::SPUsePath(SPObject* i_owner):SPUseReference(i_owner)
45 {
46 owner=i_owner;
47 originalPath = NULL;
48 sourceDirty=false;
49 sourceHref = NULL;
50 sourceRepr = NULL;
51 sourceObject = NULL;
52 new (&_modified_connection) sigc::connection();
53 new (&_delete_connection) sigc::connection();
54 new (&_changed_connection) sigc::connection();
55 new (&_transformed_connection) sigc::connection();
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();
71 _modified_connection.~connection();
72 _delete_connection.~connection();
73 _changed_connection.~connection();
74 _transformed_connection.~connection();
75 }
77 void
78 SPUsePath::link(char *to)
79 {
80 if ( to == NULL ) {
81 quit_listening();
82 unlink();
83 } else {
84 if ( !sourceHref || ( strcmp(to, sourceHref) != 0 ) ) {
85 g_free(sourceHref);
86 sourceHref = g_strdup(to);
87 try {
88 attach(Inkscape::URI(to));
89 } catch (Inkscape::BadURIException &e) {
90 /* TODO: Proper error handling as per
91 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
92 */
93 g_warning("%s", e.what());
94 detach();
95 }
96 }
97 }
98 }
100 void
101 SPUsePath::unlink(void)
102 {
103 g_free(sourceHref);
104 sourceHref = NULL;
105 detach();
106 }
108 void
109 SPUsePath::start_listening(SPObject* to)
110 {
111 if ( to == NULL ) {
112 return;
113 }
114 sourceObject = to;
115 sourceRepr = SP_OBJECT_REPR(to);
116 _delete_connection = to->connectDelete(sigc::bind(sigc::ptr_fun(&sp_usepath_delete_self), this));
117 _transformed_connection = SP_ITEM(to)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_usepath_move_compensate), this));
118 _modified_connection = to->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_usepath_source_modified), this));
119 }
121 void
122 SPUsePath::quit_listening(void)
123 {
124 if ( sourceObject == NULL ) {
125 return;
126 }
127 _modified_connection.disconnect();
128 _delete_connection.disconnect();
129 _transformed_connection.disconnect();
130 sourceRepr = NULL;
131 sourceObject = NULL;
132 }
134 static void
135 sp_usepath_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUsePath *offset)
136 {
137 offset->quit_listening();
138 SPItem *refobj = offset->getObject();
139 if ( refobj ) {
140 offset->start_listening(refobj);
141 }
142 offset->sourceDirty=true;
143 SP_OBJECT(offset->owner)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
144 }
146 static void
147 sp_usepath_move_compensate(NR::Matrix const *mp, SPItem *original, SPUsePath *self)
148 {
149 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
150 if (mode == SP_CLONE_COMPENSATION_NONE) {
151 return;
152 }
153 SPItem *item = SP_ITEM(self->owner);
155 #if 0
156 NR::Matrix m(*mp);
157 if (!(m.is_translation())) {
158 return;
159 }
160 NR::Matrix const t(item->transform);
161 NR::Matrix clone_move = t.inverse() * m * t;
163 // Calculate the compensation matrix and the advertized movement matrix.
164 NR::Matrix advertized_move;
165 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
166 //clone_move = clone_move.inverse();
167 advertized_move.set_identity();
168 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
169 clone_move = clone_move.inverse() * m;
170 advertized_move = m;
171 } else {
172 g_assert_not_reached();
173 }
175 // Commit the compensation.
176 item->transform *= clone_move;
177 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
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 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value", SP_CLONE_ORPHANS_UNLINK);
189 if (mode == SP_CLONE_ORPHANS_UNLINK) {
190 // leave it be. just forget about the source
191 offset->quit_listening();
192 offset->unlink();
193 if (offset->user_unlink)
194 offset->user_unlink(offset->owner);
195 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
196 offset->owner->deleteObject();
197 }
198 }
200 static void
201 sp_usepath_source_modified(SPObject *iSource, guint flags, SPUsePath *offset)
202 {
203 offset->sourceDirty = true;
204 offset->owner->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
205 }
207 void SPUsePath::refresh_source()
208 {
209 sourceDirty = false;
210 delete originalPath;
211 originalPath = NULL;
213 // le mauvais cas: pas d'attribut d => il faut verifier que c'est une SPShape puis prendre le contour
214 // [tr: The bad case: no d attribute. Must check that it's a SPShape and then take the outline.]
215 SPObject *refobj = sourceObject;
216 if ( refobj == NULL ) return;
217 SPItem *item = SP_ITEM(refobj);
219 SPCurve *curve = NULL;
220 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
221 return;
222 }
223 if (SP_IS_SHAPE(item)) {
224 curve = sp_shape_get_curve(SP_SHAPE(item));
225 if (curve == NULL)
226 return;
227 }
228 if (SP_IS_TEXT(item)) {
229 curve = SP_TEXT(item)->getNormalizedBpath();
230 if (curve == NULL) {
231 return;
232 }
233 }
234 originalPath = new Path;
235 originalPath->LoadArtBPath(SP_CURVE_BPATH(curve), NR::Matrix(item->transform), true);
236 sp_curve_unref(curve);
237 }
240 /*
241 Local Variables:
242 mode:c++
243 c-file-style:"stroustrup"
244 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
245 indent-tabs-mode:nil
246 fill-column:99
247 End:
248 */
249 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :