Code

a73f9ccd6ff1e0c610a3915e2d398922d216402c
[inkscape.git] / src / live_effects / lpeobject.cpp
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::Document *doc, 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)
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);
126 /**
127  * Virtual release of livepatheffect members before destruction.
128  */
129 static void
130 livepatheffect_release(SPObject *object)
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);
168 /**
169  * Virtual set: set attribute to value.
170  */
171 static void
172 livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
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     }
202 /**
203  * Virtual write: write object attributes to repr.
204  */
205 static Inkscape::XML::Node *
206 livepatheffect_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
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         repr = xml_doc->createElement("inkscape:path-effect");
216     }
218     if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->effecttype_set)
219         repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
221 //    lpeobj->lpe->write(repr); something like this.
223     lpeobj->lpe->writeParamsToSVG();
225     if (((SPObjectClass *) livepatheffect_parent_class)->write)
226         (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, xml_doc, repr, flags);
228     return repr;
231 static void
232 livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
233                                       const gchar *key,
234                                       const gchar */*oldval*/,
235                                       const gchar *newval,
236                                       bool /*is_interactive*/,
237                                       void * data )
239 #ifdef LIVEPATHEFFECT_VERBOSE
240     g_print("livepatheffect_on_repr_attr_changed");
241 #endif
243     if (!data)
244         return;
246     LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
247     if (!lpeobj->lpe)
248         return;
250     lpeobj->lpe->setParameter(key, newval);
252     lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
255 /**
256  * If this has other users, create a new private duplicate and return it
257  * returns 'this' when no forking was necessary (and therefore no duplicate was made)
258  */
259 LivePathEffectObject *
260 LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
262     if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
263         SPDocument *doc = SP_OBJECT_DOCUMENT(this);
264         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
266         Inkscape::XML::Node *repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
267         SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
268         LivePathEffectObject *lpeobj_new = (LivePathEffectObject *) doc->getObjectByRepr(repr);
269         Inkscape::GC::release(repr);
270         return lpeobj_new;
271     }
272     return this;
275 /*
276   Local Variables:
277   mode:c++
278   c-file-style:"stroustrup"
279   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
280   indent-tabs-mode:nil
281   fill-column:99
282   End:
283 */
284 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :