summaryrefslogtreecommitdiffstats
path: root/src/live_effects/lpeobject.cpp
blob: a73f9ccd6ff1e0c610a3915e2d398922d216402c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#define INKSCAPE_LIVEPATHEFFECT_OBJECT_CPP

/*
 * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "live_effects/lpeobject.h"

#include "live_effects/effect.h"

#include "xml/repr.h"
#include "xml/node-event-vector.h"
#include "sp-object.h"
#include "attributes.h"
#include "document.h"
#include "document-private.h"

#include <glibmm/i18n.h>

//#define LIVEPATHEFFECT_VERBOSE

static void livepatheffect_class_init(LivePathEffectObjectClass *klass);
static void livepatheffect_init(LivePathEffectObject *stop);

static void livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
static void livepatheffect_release(SPObject *object);

static void livepatheffect_set(SPObject *object, unsigned key, gchar const *value);
static Inkscape::XML::Node *livepatheffect_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);

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);

static SPObjectClass *livepatheffect_parent_class;
/**
 * Registers the LivePathEffect class with Gdk and returns its type number.
 */
GType
livepatheffect_get_type ()
{
    static GType livepatheffect_type = 0;

    if (!livepatheffect_type) {
        GTypeInfo livepatheffect_info = {
            sizeof (LivePathEffectObjectClass),
            NULL, NULL,
            (GClassInitFunc) livepatheffect_class_init,
            NULL, NULL,
            sizeof (LivePathEffectObject),
            16,
            (GInstanceInitFunc) livepatheffect_init,
            NULL,
        };
        livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
    }
    return livepatheffect_type;
}

static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
    NULL, /* child_added */
    NULL, /* child_removed */
    livepatheffect_on_repr_attr_changed,
    NULL, /* content_changed */
    NULL  /* order_changed */
};


/**
 * Callback to initialize livepatheffect vtable.
 */
static void
livepatheffect_class_init(LivePathEffectObjectClass *klass)
{
    SPObjectClass *sp_object_class = (SPObjectClass *) klass;

    livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);

    sp_object_class->build = livepatheffect_build;
    sp_object_class->release = livepatheffect_release;

    sp_object_class->set = livepatheffect_set;
    sp_object_class->write = livepatheffect_write;
}

/**
 * Callback to initialize livepatheffect object.
 */
static void
livepatheffect_init(LivePathEffectObject *lpeobj)
{
#ifdef LIVEPATHEFFECT_VERBOSE
    g_message("Init livepatheffectobject");
#endif
    lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
    lpeobj->lpe = NULL;

    lpeobj->effecttype_set = false;
}

/**
 * Virtual build: set livepatheffect attributes from its associated XML node.
 */
static void
livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
{
#ifdef LIVEPATHEFFECT_VERBOSE
    g_message("Build livepatheffect");
#endif
    g_assert(object != NULL);
    g_assert(SP_IS_OBJECT(object));

    if (((SPObjectClass *) livepatheffect_parent_class)->build)
        (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);

    sp_object_read_attr(object, "effect");

    if (repr) {
        repr->addListener (&livepatheffect_repr_events, object);
    }

    /* Register ourselves, is this necessary? */
//    sp_document_add_resource(document, "path-effect", object);
}

/**
 * Virtual release of livepatheffect members before destruction.
 */
static void
livepatheffect_release(SPObject *object)
{
#ifdef LIVEPATHEFFECT_VERBOSE
    g_print("Releasing livepatheffect");
#endif

    LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);

    SP_OBJECT_REPR(object)->removeListenerByData(object);


/*
    if (SP_OBJECT_DOCUMENT(object)) {
        // Unregister ourselves
        sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
    }

    if (gradient->ref) {
        gradient->modified_connection.disconnect();
        gradient->ref->detach();
        delete gradient->ref;
        gradient->ref = NULL;
    }

    gradient->modified_connection.~connection();

*/

    if (lpeobj->lpe) {
        delete lpeobj->lpe;
        lpeobj->lpe = NULL;
    }
    lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;

    if (((SPObjectClass *) livepatheffect_parent_class)->release)
        ((SPObjectClass *) livepatheffect_parent_class)->release(object);
}

/**
 * Virtual set: set attribute to value.
 */
static void
livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
{
    LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
#ifdef LIVEPATHEFFECT_VERBOSE
    g_print("Set livepatheffect");
#endif
    switch (key) {
        case SP_PROP_PATH_EFFECT:
            if (lpeobj->lpe) {
                delete lpeobj->lpe;
                lpeobj->lpe = NULL;
            }

            if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
                lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
                lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
                lpeobj->effecttype_set = true;
            } else {
                lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
                lpeobj->effecttype_set = false;
            }
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
            break;
    }

    if (((SPObjectClass *) livepatheffect_parent_class)->set) {
        ((SPObjectClass *) livepatheffect_parent_class)->set(object, key, value);
    }
}

/**
 * Virtual write: write object attributes to repr.
 */
static Inkscape::XML::Node *
livepatheffect_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
#ifdef LIVEPATHEFFECT_VERBOSE
    g_print("Write livepatheffect");
#endif

    LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);

    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
        repr = xml_doc->createElement("inkscape:path-effect");
    }

    if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->effecttype_set)
        repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());

//    lpeobj->lpe->write(repr); something like this.

    lpeobj->lpe->writeParamsToSVG();

    if (((SPObjectClass *) livepatheffect_parent_class)->write)
        (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, xml_doc, repr, flags);

    return repr;
}

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 )
{
#ifdef LIVEPATHEFFECT_VERBOSE
    g_print("livepatheffect_on_repr_attr_changed");
#endif

    if (!data)
        return;

    LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
    if (!lpeobj->lpe)
        return;

    lpeobj->lpe->setParameter(key, newval);

    lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
}

/**
 * If this has other users, create a new private duplicate and return it
 * returns 'this' when no forking was necessary (and therefore no duplicate was made)
 */
LivePathEffectObject *
LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
{
    if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
        SPDocument *doc = SP_OBJECT_DOCUMENT(this);
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);

        Inkscape::XML::Node *repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
        SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
        LivePathEffectObject *lpeobj_new = (LivePathEffectObject *) doc->getObjectByRepr(repr);
        Inkscape::GC::release(repr);
        return lpeobj_new;
    }
    return this;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :