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, SPItem *item);
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 (&_delete_connection) sigc::connection();
53 new (&_changed_connection) sigc::connection();
54 new (&_transformed_connection) sigc::connection();
55 _changed_connection = changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_usepath_href_changed), this)); // listening to myself, this should be virtual instead
57 user_unlink = NULL;
58 }
60 SPUsePath::~SPUsePath(void)
61 {
62 delete originalPath;
63 originalPath = NULL;
65 _changed_connection.disconnect(); // to do before unlinking
67 quit_listening();
68 unlink();
70 _delete_connection.~connection();
71 _changed_connection.~connection();
72 _transformed_connection.~connection();
73 }
75 void
76 SPUsePath::link(char *to)
77 {
78 if ( to == NULL ) {
79 quit_listening();
80 unlink();
81 } else {
82 if ( !sourceHref || ( strcmp(to, sourceHref) != 0 ) ) {
83 g_free(sourceHref);
84 sourceHref = g_strdup(to);
85 try {
86 attach(Inkscape::URI(to));
87 } catch (Inkscape::BadURIException &e) {
88 /* TODO: Proper error handling as per
89 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
90 */
91 g_warning("%s", e.what());
92 detach();
93 }
94 }
95 }
96 }
98 void
99 SPUsePath::unlink(void)
100 {
101 g_free(sourceHref);
102 sourceHref = NULL;
103 detach();
104 }
106 void
107 SPUsePath::start_listening(SPObject* to)
108 {
109 if ( to == NULL ) {
110 return;
111 }
112 sourceObject = to;
113 sourceRepr = SP_OBJECT_REPR(to);
114 _delete_connection = to->connectDelete(sigc::bind(sigc::ptr_fun(&sp_usepath_delete_self), this));
115 _transformed_connection = SP_ITEM(to)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_usepath_move_compensate), this));
116 _modified_connection = g_signal_connect(G_OBJECT(to), "modified", G_CALLBACK(sp_usepath_source_modified), this);
117 }
119 void
120 SPUsePath::quit_listening(void)
121 {
122 if ( sourceObject == NULL ) {
123 return;
124 }
125 g_signal_handler_disconnect(sourceObject, _modified_connection);
126 _delete_connection.disconnect();
127 _transformed_connection.disconnect();
128 sourceRepr = NULL;
129 sourceObject = NULL;
130 }
132 static void
133 sp_usepath_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUsePath *offset)
134 {
135 offset->quit_listening();
136 SPItem *refobj = offset->getObject();
137 if ( refobj ) {
138 offset->start_listening(refobj);
139 }
140 offset->sourceDirty=true;
141 SP_OBJECT(offset->owner)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
142 }
144 static void
145 sp_usepath_move_compensate(NR::Matrix const *mp, SPItem *original, SPUsePath *self)
146 {
147 guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
148 if (mode == SP_CLONE_COMPENSATION_NONE) {
149 return;
150 }
151 SPItem *item = SP_ITEM(self->owner);
153 #if 0
154 NR::Matrix m(*mp);
155 if (!(m.is_translation())) {
156 return;
157 }
158 NR::Matrix const t(item->transform);
159 NR::Matrix clone_move = t.inverse() * m * t;
161 // Calculate the compensation matrix and the advertized movement matrix.
162 NR::Matrix advertized_move;
163 if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
164 //clone_move = clone_move.inverse();
165 advertized_move.set_identity();
166 } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
167 clone_move = clone_move.inverse() * m;
168 advertized_move = m;
169 } else {
170 g_assert_not_reached();
171 }
173 // Commit the compensation.
174 item->transform *= clone_move;
175 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
176 #endif
178 self->sourceDirty = true;
179 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
180 }
182 static void
183 sp_usepath_delete_self(SPObject */*deleted*/, SPUsePath *offset)
184 {
185 guint const mode = prefs_get_int_attribute("options.cloneorphans", "value", SP_CLONE_ORPHANS_UNLINK);
187 if (mode == SP_CLONE_ORPHANS_UNLINK) {
188 // leave it be. just forget about the source
189 offset->quit_listening();
190 offset->unlink();
191 if (offset->user_unlink)
192 offset->user_unlink(offset->owner);
193 } else if (mode == SP_CLONE_ORPHANS_DELETE) {
194 offset->owner->deleteObject();
195 }
196 }
198 static void
199 sp_usepath_source_modified(SPObject *iSource, guint flags, SPItem *item)
200 {
201 SPUsePath *offset = (SPUsePath*)item;
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->LoadArtBPath(curve->bpath, NR::Matrix(item->transform), true);
235 sp_curve_unref(curve);
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 :