Code

LPE: add on-canvas editing of path parameters!
[inkscape.git] / src / live_effects / lpeobject.cpp
1 #define INKSCAPE_LIVEPATHEFFECT_OBJECT_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "xml/repr.h"
10 #include "xml/node-event-vector.h"
11 #include "sp-object.h"
12 #include "attributes.h"
14 #include "document.h"
15 #include <glibmm/i18n.h>
17 #include "live_effects/lpeobject.h"
18 #include "live_effects/effect.h"
20 //#define LIVEPATHEFFECT_VERBOSE
22 static void livepatheffect_class_init(LivePathEffectObjectClass *klass);
23 static void livepatheffect_init(LivePathEffectObject *stop);
25 static void livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
26 static void livepatheffect_release(SPObject *object);
28 static void livepatheffect_set(SPObject *object, unsigned key, gchar const *value);
29 static Inkscape::XML::Node *livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
31 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);
33 static SPObjectClass *livepatheffect_parent_class;
34 /**
35  * Registers the LivePathEffect class with Gdk and returns its type number.
36  */
37 GType
38 livepatheffect_get_type ()
39 {
40     static GType livepatheffect_type = 0;
42     if (!livepatheffect_type) {
43         GTypeInfo livepatheffect_info = {
44             sizeof (LivePathEffectObjectClass),
45             NULL, NULL,
46             (GClassInitFunc) livepatheffect_class_init,
47             NULL, NULL,
48             sizeof (LivePathEffectObject),
49             16,
50             (GInstanceInitFunc) livepatheffect_init,
51             NULL,
52         };
53         livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
54     }
55     return livepatheffect_type;
56 }
58 static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
59     NULL, /* child_added */
60     NULL, /* child_removed */
61     livepatheffect_on_repr_attr_changed,
62     NULL, /* content_changed */
63     NULL  /* order_changed */
64 };
67 /**
68  * Callback to initialize livepatheffect vtable.
69  */
70 static void 
71 livepatheffect_class_init(LivePathEffectObjectClass *klass)
72 {
73     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
75     livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
77     sp_object_class->build = livepatheffect_build;
78     sp_object_class->release = livepatheffect_release;
80     sp_object_class->set = livepatheffect_set;
81     sp_object_class->write = livepatheffect_write;
82 }
84 /**
85  * Callback to initialize livepatheffect object.
86  */
87 static void
88 livepatheffect_init(LivePathEffectObject *lpeobj)
89 {
90 #ifdef LIVEPATHEFFECT_VERBOSE
91     g_message("Init livepatheffectobject");
92 #endif
93     lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
94     lpeobj->lpe = NULL;
96     lpeobj->effecttype_set = false;
97 }
99 /**
100  * Virtual build: set livepatheffect attributes from its associated XML node.
101  */
102 static void 
103 livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
105 #ifdef LIVEPATHEFFECT_VERBOSE
106     g_message("Build livepatheffect");
107 #endif
108     g_assert(object != NULL);
109     g_assert(SP_IS_OBJECT(object));
111     if (((SPObjectClass *) livepatheffect_parent_class)->build)
112         (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);
114     sp_object_read_attr(object, "effect");
116     if (repr) {
117         repr->addListener (&livepatheffect_repr_events, object);
118     }
120     /* Register ourselves, is this necessary? */
121 //    sp_document_add_resource(document, "path-effect", object);
124 /**
125  * Virtual release of livepatheffect members before destruction.
126  */
127 static void
128 livepatheffect_release(SPObject *object)
130 #ifdef LIVEPATHEFFECT_VERBOSE
131     g_print("Releasing livepatheffect");
132 #endif
134     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
136     SP_OBJECT_REPR(object)->removeListenerByData(object);
139 /*
140     if (SP_OBJECT_DOCUMENT(object)) {
141         // Unregister ourselves
142         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
143     }
145     if (gradient->ref) {
146         gradient->modified_connection.disconnect();
147         gradient->ref->detach();
148         delete gradient->ref;
149         gradient->ref = NULL;
150     }
152     gradient->modified_connection.~connection();
154 */
156     if (lpeobj->lpe) {
157         delete lpeobj->lpe;
158         lpeobj->lpe = NULL;
159     }
160     lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
162     if (((SPObjectClass *) livepatheffect_parent_class)->release)
163         ((SPObjectClass *) livepatheffect_parent_class)->release(object);
166 /**
167  * Virtual set: set attribute to value.
168  */
169 static void
170 livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
172     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
173 #ifdef LIVEPATHEFFECT_VERBOSE
174     g_print("Set livepatheffect");
175 #endif
176     switch (key) {
177         case SP_PROP_PATH_EFFECT:
178             if (lpeobj->lpe) {
179                 delete lpeobj->lpe;
180                 lpeobj->lpe = NULL;
181             }
183             if (value) {
184                 lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
185                 if (lpeobj->effecttype != Inkscape::LivePathEffect::INVALID_LPE) {
186                     lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
187                 }
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::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         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;
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 )
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);
255 /*
256   Local Variables:
257   mode:c++
258   c-file-style:"stroustrup"
259   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
260   indent-tabs-mode:nil
261   fill-column:99
262   End:
263 */
264 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :