Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / live_effects / lpeobject.cpp
1 /*
2  * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
3  *   Abhishek Sharma
4  *
5  * Released under GNU GPL, read the file 'COPYING' for more information
6  */
8 #include "live_effects/lpeobject.h"
10 #include "live_effects/effect.h"
12 #include "xml/repr.h"
13 #include "xml/node-event-vector.h"
14 #include "sp-object.h"
15 #include "attributes.h"
16 #include "document.h"
17 #include "document-private.h"
19 #include <glibmm/i18n.h>
21 //#define LIVEPATHEFFECT_VERBOSE
23 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);
25 static SPObjectClass *livepatheffect_parent_class;
26 /**
27  * Registers the LivePathEffect class with Gdk and returns its type number.
28  */
29 GType
30 LivePathEffectObject::livepatheffect_get_type ()
31 {
32     static GType livepatheffect_type = 0;
34     if (!livepatheffect_type) {
35         GTypeInfo livepatheffect_info = {
36             sizeof (LivePathEffectObjectClass),
37             NULL, NULL,
38             (GClassInitFunc) LivePathEffectObject::livepatheffect_class_init,
39             NULL, NULL,
40             sizeof (LivePathEffectObject),
41             16,
42             (GInstanceInitFunc) LivePathEffectObject::livepatheffect_init,
43             NULL,
44         };
45         livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
46     }
47     return livepatheffect_type;
48 }
50 static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
51     NULL, /* child_added */
52     NULL, /* child_removed */
53     livepatheffect_on_repr_attr_changed,
54     NULL, /* content_changed */
55     NULL  /* order_changed */
56 };
59 /**
60  * Callback to initialize livepatheffect vtable.
61  */
62 void
63 LivePathEffectObject::livepatheffect_class_init(LivePathEffectObjectClass *klass)
64 {
65     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
67     livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
69     sp_object_class->build = livepatheffect_build;
70     sp_object_class->release = livepatheffect_release;
72     sp_object_class->set = livepatheffect_set;
73     sp_object_class->write = livepatheffect_write;
74 }
76 /**
77  * Callback to initialize livepatheffect object.
78  */
79 void
80 LivePathEffectObject::livepatheffect_init(LivePathEffectObject *lpeobj)
81 {
82 #ifdef LIVEPATHEFFECT_VERBOSE
83     g_message("Init livepatheffectobject");
84 #endif
85     lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
86     lpeobj->lpe = NULL;
88     lpeobj->effecttype_set = false;
89 }
91 /**
92  * Virtual build: set livepatheffect attributes from its associated XML node.
93  */
94 void
95 LivePathEffectObject::livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
96 {
97 #ifdef LIVEPATHEFFECT_VERBOSE
98     g_message("Build livepatheffect");
99 #endif
100     g_assert(object != NULL);
101     g_assert(SP_IS_OBJECT(object));
103     if (((SPObjectClass *) livepatheffect_parent_class)->build)
104         (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);
106     object->readAttr( "effect" );
108     if (repr) {
109         repr->addListener (&livepatheffect_repr_events, object);
110     }
112     /* Register ourselves, is this necessary? */
113 //    document->addResource("path-effect", object);
116 /**
117  * Virtual release of livepatheffect members before destruction.
118  */
119 void
120 LivePathEffectObject::livepatheffect_release(SPObject *object)
122 #ifdef LIVEPATHEFFECT_VERBOSE
123     g_print("Releasing livepatheffect");
124 #endif
126     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
128     SP_OBJECT_REPR(object)->removeListenerByData(object);
131 /*
132     if (SP_OBJECT_DOCUMENT(object)) {
133         // Unregister ourselves
134         sp_document_removeResource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
135     }
137     if (gradient->ref) {
138         gradient->modified_connection.disconnect();
139         gradient->ref->detach();
140         delete gradient->ref;
141         gradient->ref = NULL;
142     }
144     gradient->modified_connection.~connection();
146 */
148     if (lpeobj->lpe) {
149         delete lpeobj->lpe;
150         lpeobj->lpe = NULL;
151     }
152     lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
154     if (((SPObjectClass *) livepatheffect_parent_class)->release)
155         ((SPObjectClass *) livepatheffect_parent_class)->release(object);
158 /**
159  * Virtual set: set attribute to value.
160  */
161 void
162 LivePathEffectObject::livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
164     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
165 #ifdef LIVEPATHEFFECT_VERBOSE
166     g_print("Set livepatheffect");
167 #endif
168     switch (key) {
169         case SP_PROP_PATH_EFFECT:
170             if (lpeobj->lpe) {
171                 delete lpeobj->lpe;
172                 lpeobj->lpe = NULL;
173             }
175             if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
176                 lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
177                 lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
178                 lpeobj->effecttype_set = true;
179             } else {
180                 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
181                 lpeobj->effecttype_set = false;
182             }
183             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
184             break;
185     }
187     if (((SPObjectClass *) livepatheffect_parent_class)->set) {
188         ((SPObjectClass *) livepatheffect_parent_class)->set(object, key, value);
189     }
192 /**
193  * Virtual write: write object attributes to repr.
194  */
195 Inkscape::XML::Node *
196 LivePathEffectObject::livepatheffect_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
198 #ifdef LIVEPATHEFFECT_VERBOSE
199     g_print("Write livepatheffect");
200 #endif
202     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
204     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
205         repr = xml_doc->createElement("inkscape:path-effect");
206     }
208     if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->lpe) {
209         repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
211         lpeobj->lpe->writeParamsToSVG();
212     }
214     if (((SPObjectClass *) livepatheffect_parent_class)->write)
215         (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, xml_doc, repr, flags);
217     return repr;
220 static void
221 livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
222                                       const gchar *key,
223                                       const gchar */*oldval*/,
224                                       const gchar *newval,
225                                       bool /*is_interactive*/,
226                                       void * data )
228 #ifdef LIVEPATHEFFECT_VERBOSE
229     g_print("livepatheffect_on_repr_attr_changed");
230 #endif
232     if (!data)
233         return;
235     LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
236     if (!lpeobj->get_lpe())
237         return;
239     lpeobj->get_lpe()->setParameter(key, newval);
241     lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
244 /**
245  * If this has other users, create a new private duplicate and return it
246  * returns 'this' when no forking was necessary (and therefore no duplicate was made)
247  * Check out sp_lpe_item_fork_path_effects_if_necessary !
248  */
249 LivePathEffectObject *LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
251     if (hrefcount > nr_of_allowed_users) {
252         SPDocument *doc = SP_OBJECT_DOCUMENT(this);
253         Inkscape::XML::Document *xml_doc = doc->getReprDoc();
254         Inkscape::XML::Node *dup_repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
256         SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(dup_repr, NULL);
257         LivePathEffectObject *lpeobj_new = LIVEPATHEFFECT( doc->getObjectByRepr(dup_repr) );
259         Inkscape::GC::release(dup_repr);
260         return lpeobj_new;
261     }
262     return this;
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 :