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