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_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
26 static SPObjectClass *livepatheffect_parent_class;
27 /**
28 * Registers the LivePathEffect class with Gdk and returns its type number.
29 */
30 GType
31 LivePathEffectObject::livepatheffect_get_type ()
32 {
33 static GType livepatheffect_type = 0;
35 if (!livepatheffect_type) {
36 GTypeInfo livepatheffect_info = {
37 sizeof (LivePathEffectObjectClass),
38 NULL, NULL,
39 (GClassInitFunc) LivePathEffectObject::livepatheffect_class_init,
40 NULL, NULL,
41 sizeof (LivePathEffectObject),
42 16,
43 (GInstanceInitFunc) LivePathEffectObject::livepatheffect_init,
44 NULL,
45 };
46 livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
47 }
48 return livepatheffect_type;
49 }
51 static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
52 NULL, /* child_added */
53 NULL, /* child_removed */
54 livepatheffect_on_repr_attr_changed,
55 NULL, /* content_changed */
56 NULL /* order_changed */
57 };
60 /**
61 * Callback to initialize livepatheffect vtable.
62 */
63 void
64 LivePathEffectObject::livepatheffect_class_init(LivePathEffectObjectClass *klass)
65 {
66 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
68 livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
70 sp_object_class->build = livepatheffect_build;
71 sp_object_class->release = livepatheffect_release;
73 sp_object_class->set = livepatheffect_set;
74 sp_object_class->write = livepatheffect_write;
75 }
77 /**
78 * Callback to initialize livepatheffect object.
79 */
80 void
81 LivePathEffectObject::livepatheffect_init(LivePathEffectObject *lpeobj)
82 {
83 #ifdef LIVEPATHEFFECT_VERBOSE
84 g_message("Init livepatheffectobject");
85 #endif
86 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
87 lpeobj->lpe = NULL;
89 lpeobj->effecttype_set = false;
90 }
92 /**
93 * Virtual build: set livepatheffect attributes from its associated XML node.
94 */
95 void
96 LivePathEffectObject::livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
97 {
98 #ifdef LIVEPATHEFFECT_VERBOSE
99 g_message("Build livepatheffect");
100 #endif
101 g_assert(object != NULL);
102 g_assert(SP_IS_OBJECT(object));
104 if (((SPObjectClass *) livepatheffect_parent_class)->build)
105 (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);
107 sp_object_read_attr(object, "effect");
109 if (repr) {
110 repr->addListener (&livepatheffect_repr_events, object);
111 }
113 /* Register ourselves, is this necessary? */
114 // sp_document_add_resource(document, "path-effect", object);
115 }
117 /**
118 * Virtual release of livepatheffect members before destruction.
119 */
120 void
121 LivePathEffectObject::livepatheffect_release(SPObject *object)
122 {
123 #ifdef LIVEPATHEFFECT_VERBOSE
124 g_print("Releasing livepatheffect");
125 #endif
127 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
129 SP_OBJECT_REPR(object)->removeListenerByData(object);
132 /*
133 if (SP_OBJECT_DOCUMENT(object)) {
134 // Unregister ourselves
135 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
136 }
138 if (gradient->ref) {
139 gradient->modified_connection.disconnect();
140 gradient->ref->detach();
141 delete gradient->ref;
142 gradient->ref = NULL;
143 }
145 gradient->modified_connection.~connection();
147 */
149 if (lpeobj->lpe) {
150 delete lpeobj->lpe;
151 lpeobj->lpe = NULL;
152 }
153 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
155 if (((SPObjectClass *) livepatheffect_parent_class)->release)
156 ((SPObjectClass *) livepatheffect_parent_class)->release(object);
157 }
159 /**
160 * Virtual set: set attribute to value.
161 */
162 void
163 LivePathEffectObject::livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
164 {
165 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
166 #ifdef LIVEPATHEFFECT_VERBOSE
167 g_print("Set livepatheffect");
168 #endif
169 switch (key) {
170 case SP_PROP_PATH_EFFECT:
171 if (lpeobj->lpe) {
172 delete lpeobj->lpe;
173 lpeobj->lpe = NULL;
174 }
176 if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
177 lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
178 lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
179 lpeobj->effecttype_set = true;
180 } else {
181 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
182 lpeobj->effecttype_set = false;
183 }
184 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
185 break;
186 }
188 if (((SPObjectClass *) livepatheffect_parent_class)->set) {
189 ((SPObjectClass *) livepatheffect_parent_class)->set(object, key, value);
190 }
191 }
193 /**
194 * Virtual write: write object attributes to repr.
195 */
196 Inkscape::XML::Node *
197 LivePathEffectObject::livepatheffect_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
198 {
199 #ifdef LIVEPATHEFFECT_VERBOSE
200 g_print("Write livepatheffect");
201 #endif
203 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
205 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
206 repr = xml_doc->createElement("inkscape:path-effect");
207 }
209 if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->lpe) {
210 repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
212 lpeobj->lpe->writeParamsToSVG();
213 }
215 if (((SPObjectClass *) livepatheffect_parent_class)->write)
216 (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, xml_doc, repr, flags);
218 return repr;
219 }
221 static void
222 livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
223 const gchar *key,
224 const gchar */*oldval*/,
225 const gchar *newval,
226 bool /*is_interactive*/,
227 void * data )
228 {
229 #ifdef LIVEPATHEFFECT_VERBOSE
230 g_print("livepatheffect_on_repr_attr_changed");
231 #endif
233 if (!data)
234 return;
236 LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
237 if (!lpeobj->get_lpe())
238 return;
240 lpeobj->get_lpe()->setParameter(key, newval);
242 lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
243 }
245 /**
246 * If this has other users, create a new private duplicate and return it
247 * returns 'this' when no forking was necessary (and therefore no duplicate was made)
248 */
249 LivePathEffectObject *
250 LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
251 {
252 if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
253 SPDocument *doc = SP_OBJECT_DOCUMENT(this);
254 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
256 Inkscape::XML::Node *repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
257 SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
258 LivePathEffectObject *lpeobj_new = (LivePathEffectObject *) doc->getObjectByRepr(repr);
259 Inkscape::GC::release(repr);
260 return lpeobj_new;
261 }
262 return this;
263 }
265 /*
266 Local Variables:
267 mode:c++
268 c-file-style:"stroustrup"
269 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
270 indent-tabs-mode:nil
271 fill-column:99
272 End:
273 */
274 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :