1 #define INKSCAPE_LIVEPATHEFFECT_OBJECT_CPP
3 /*
4 * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
5 *
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #include "live_effects/lpeobject.h"
11 #include "live_effects/effect.h"
13 #include "xml/repr.h"
14 #include "xml/node-event-vector.h"
15 #include "sp-object.h"
16 #include "attributes.h"
17 #include "document.h"
18 #include "document-private.h"
20 #include <glibmm/i18n.h>
22 //#define LIVEPATHEFFECT_VERBOSE
24 static void livepatheffect_class_init(LivePathEffectObjectClass *klass);
25 static void livepatheffect_init(LivePathEffectObject *stop);
27 static void livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
28 static void livepatheffect_release(SPObject *object);
30 static void livepatheffect_set(SPObject *object, unsigned key, gchar const *value);
31 static Inkscape::XML::Node *livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
33 static void livepatheffect_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
35 static SPObjectClass *livepatheffect_parent_class;
36 /**
37 * Registers the LivePathEffect class with Gdk and returns its type number.
38 */
39 GType
40 livepatheffect_get_type ()
41 {
42 static GType livepatheffect_type = 0;
44 if (!livepatheffect_type) {
45 GTypeInfo livepatheffect_info = {
46 sizeof (LivePathEffectObjectClass),
47 NULL, NULL,
48 (GClassInitFunc) livepatheffect_class_init,
49 NULL, NULL,
50 sizeof (LivePathEffectObject),
51 16,
52 (GInstanceInitFunc) livepatheffect_init,
53 NULL,
54 };
55 livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
56 }
57 return livepatheffect_type;
58 }
60 static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
61 NULL, /* child_added */
62 NULL, /* child_removed */
63 livepatheffect_on_repr_attr_changed,
64 NULL, /* content_changed */
65 NULL /* order_changed */
66 };
69 /**
70 * Callback to initialize livepatheffect vtable.
71 */
72 static void
73 livepatheffect_class_init(LivePathEffectObjectClass *klass)
74 {
75 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
77 livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
79 sp_object_class->build = livepatheffect_build;
80 sp_object_class->release = livepatheffect_release;
82 sp_object_class->set = livepatheffect_set;
83 sp_object_class->write = livepatheffect_write;
84 }
86 /**
87 * Callback to initialize livepatheffect object.
88 */
89 static void
90 livepatheffect_init(LivePathEffectObject *lpeobj)
91 {
92 #ifdef LIVEPATHEFFECT_VERBOSE
93 g_message("Init livepatheffectobject");
94 #endif
95 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
96 lpeobj->lpe = NULL;
98 lpeobj->effecttype_set = false;
99 }
101 /**
102 * Virtual build: set livepatheffect attributes from its associated XML node.
103 */
104 static void
105 livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
106 {
107 #ifdef LIVEPATHEFFECT_VERBOSE
108 g_message("Build livepatheffect");
109 #endif
110 g_assert(object != NULL);
111 g_assert(SP_IS_OBJECT(object));
113 if (((SPObjectClass *) livepatheffect_parent_class)->build)
114 (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);
116 sp_object_read_attr(object, "effect");
118 if (repr) {
119 repr->addListener (&livepatheffect_repr_events, object);
120 }
122 /* Register ourselves, is this necessary? */
123 // sp_document_add_resource(document, "path-effect", object);
124 }
126 /**
127 * Virtual release of livepatheffect members before destruction.
128 */
129 static void
130 livepatheffect_release(SPObject *object)
131 {
132 #ifdef LIVEPATHEFFECT_VERBOSE
133 g_print("Releasing livepatheffect");
134 #endif
136 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
138 SP_OBJECT_REPR(object)->removeListenerByData(object);
141 /*
142 if (SP_OBJECT_DOCUMENT(object)) {
143 // Unregister ourselves
144 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
145 }
147 if (gradient->ref) {
148 gradient->modified_connection.disconnect();
149 gradient->ref->detach();
150 delete gradient->ref;
151 gradient->ref = NULL;
152 }
154 gradient->modified_connection.~connection();
156 */
158 if (lpeobj->lpe) {
159 delete lpeobj->lpe;
160 lpeobj->lpe = NULL;
161 }
162 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
164 if (((SPObjectClass *) livepatheffect_parent_class)->release)
165 ((SPObjectClass *) livepatheffect_parent_class)->release(object);
166 }
168 /**
169 * Virtual set: set attribute to value.
170 */
171 static void
172 livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
173 {
174 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
175 #ifdef LIVEPATHEFFECT_VERBOSE
176 g_print("Set livepatheffect");
177 #endif
178 switch (key) {
179 case SP_PROP_PATH_EFFECT:
180 if (lpeobj->lpe) {
181 delete lpeobj->lpe;
182 lpeobj->lpe = NULL;
183 }
185 if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
186 lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
187 lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
188 lpeobj->effecttype_set = true;
189 } else {
190 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
191 lpeobj->effecttype_set = false;
192 }
193 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
194 break;
195 }
197 if (((SPObjectClass *) livepatheffect_parent_class)->set) {
198 ((SPObjectClass *) livepatheffect_parent_class)->set(object, key, value);
199 }
200 }
202 /**
203 * Virtual write: write object attributes to repr.
204 */
205 static Inkscape::XML::Node *
206 livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
207 {
208 #ifdef LIVEPATHEFFECT_VERBOSE
209 g_print("Write livepatheffect");
210 #endif
212 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
214 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
215 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
216 repr = xml_doc->createElement("inkscape:path-effect");
217 }
219 if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->effecttype_set)
220 repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
222 // lpeobj->lpe->write(repr); something like this.
224 if (((SPObjectClass *) livepatheffect_parent_class)->write)
225 (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, repr, flags);
227 return repr;
228 }
230 static void
231 livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
232 const gchar *key,
233 const gchar */*oldval*/,
234 const gchar *newval,
235 bool /*is_interactive*/,
236 void * data )
237 {
238 #ifdef LIVEPATHEFFECT_VERBOSE
239 g_print("livepatheffect_on_repr_attr_changed");
240 #endif
242 if (!data)
243 return;
245 LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
246 if (!lpeobj->lpe)
247 return;
249 lpeobj->lpe->setParameter(key, newval);
251 lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
252 }
254 /**
255 * If this has other users, create a new private duplicate and return it
256 * returns 'this' when no forking was necessary (and therefore no duplicate was made)
257 */
258 LivePathEffectObject *
259 LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
260 {
261 if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
262 SPDocument *doc = SP_OBJECT_DOCUMENT(this);
263 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
265 Inkscape::XML::Node *repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
266 SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
267 LivePathEffectObject *lpeobj_new = (LivePathEffectObject *) doc->getObjectByRepr(repr);
268 Inkscape::GC::release(repr);
269 return lpeobj_new;
270 }
271 return this;
272 }
274 /*
275 Local Variables:
276 mode:c++
277 c-file-style:"stroustrup"
278 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
279 indent-tabs-mode:nil
280 fill-column:99
281 End:
282 */
283 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :