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 _changed_connection = changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_usepath_href_changed), this)); // listening to myself, this should be virtual instead
54 user_unlink = NULL;
55 }
57 SPUsePath::~SPUsePath(void)
58 {
59 delete originalPath;
60 originalPath = NULL;
62 _changed_connection.disconnect(); // to do before unlinking
64 quit_listening();
65 unlink();
66 }
68 void
69 SPUsePath::link(char *to)
70 {
71 if ( to == NULL ) {
72 quit_listening();
73 unlink();
74 } else {
75 if ( !sourceHref || ( strcmp(to, sourceHref) != 0 ) ) {
76 g_free(sourceHref);
77 sourceHref = g_strdup(to);
78 try {
79 attach(Inkscape::URI(to));
80 } catch (Inkscape::BadURIException &e) {
81 /* TODO: Proper error handling as per
82 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
83 */
84 g_warning("%s", e.what());
85 detach();
86 }
87 }
88 }
89 }
91 void
92 SPUsePath::unlink(void)
93 {
94 g_free(sourceHref);
95 sourceHref = NULL;
96 detach();
97 }
99 void
100 SPUsePath::start_listening(SPObject* to)
101 {
102 if ( to == NULL ) {
103 return;
104 }
105 sourceObject = to;
106 sourceRepr = SP_OBJECT_REPR(to);
107 _delete_connection = to->connectDelete(sigc::bind(sigc::ptr_fun(&sp_usepath_delete_self), this));
108 _transformed_connection = SP_ITEM(to)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_usepath_move_compensate), this));
109 _modified_connection = to->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_usepath_source_modified), this));
110 }
112 void
113 SPUsePath::quit_listening(void)
114 {
115 if ( sourceObject == NULL ) {
116 return;
117 }
118 _modified_connection.disconnect();
119 _delete_connection.disconnect();
120 _transformed_connection.disconnect();
121 sourceRepr = NULL;
122 sourceObject = NULL;
123 }
125 static void
126 sp_usepath_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUsePath *offset)
127 {
128 offset->quit_listening();
129 SPItem *refobj = offset->getObject();
130 if ( refobj ) {
131 offset->start_listening(refobj);
132 }
133 offset->sourceDirty=true;
134 SP_OBJECT(offset->owner)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
135 }
137 static void
138 sp_usepath_move_compensate(NR::Matrix const *mp, SPItem *original, SPUsePath *self)
139 {
140 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
141 if (mode == SP_CLONE_COMPENSATION_NONE) {
142 return;
143 }
144 SPItem *item = SP_ITEM(self->owner);
146 // TODO kill naughty naughty #if 0
147 #if 0
148 NR::Matrix m(*mp);
149 if (!(m.is_translation())) {
150 return;
151 }
152 NR::Matrix const t(item->transform);
153 NR::Matrix clone_move = t.inverse() * m * t;
155 // Calculate the compensation matrix and the advertized movement matrix.
156 NR::Matrix advertized_move;
157 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
158 //clone_move = clone_move.inverse();
159 advertized_move.set_identity();
160 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
161 clone_move = clone_move.inverse() * m;
162 advertized_move = m;
163 } else {
164 g_assert_not_reached();
165 }
167 // Commit the compensation.
168 item->transform *= clone_move;
169 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
170 #else
171 (void)mp;
172 (void)original;
173 #endif
175 self->sourceDirty = true;
176 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
177 }
179 static void
180 sp_usepath_delete_self(SPObject */*deleted*/, SPUsePath *offset)
181 {
182 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value", SP_CLONE_ORPHANS_UNLINK);
184 if (mode == SP_CLONE_ORPHANS_UNLINK) {
185 // leave it be. just forget about the source
186 offset->quit_listening();
187 offset->unlink();
188 if (offset->user_unlink)
189 offset->user_unlink(offset->owner);
190 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
191 offset->owner->deleteObject();
192 }
193 }
195 static void
196 sp_usepath_source_modified(SPObject */*iSource*/, guint /*flags*/, SPUsePath *offset)
197 {
198 offset->sourceDirty = true;
199 offset->owner->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
200 }
202 void SPUsePath::refresh_source()
203 {
204 sourceDirty = false;
205 delete originalPath;
206 originalPath = NULL;
208 // le mauvais cas: pas d'attribut d => il faut verifier que c'est une SPShape puis prendre le contour
209 // [tr: The bad case: no d attribute. Must check that it's a SPShape and then take the outline.]
210 SPObject *refobj = sourceObject;
211 if ( refobj == NULL ) return;
212 SPItem *item = SP_ITEM(refobj);
214 SPCurve *curve = NULL;
215 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
216 return;
217 }
218 if (SP_IS_SHAPE(item)) {
219 curve = sp_shape_get_curve(SP_SHAPE(item));
220 if (curve == NULL)
221 return;
222 }
223 if (SP_IS_TEXT(item)) {
224 curve = SP_TEXT(item)->getNormalizedBpath();
225 if (curve == NULL) {
226 return;
227 }
228 }
229 originalPath = new Path;
230 originalPath->LoadArtBPath(SP_CURVE_BPATH(curve), NR::Matrix(item->transform), true);
231 sp_curve_unref(curve);
232 }
235 /*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :