Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-script.cpp
1 /*
2  * SVG <script> implementation
3  *
4  * Authors:
5  *   Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
6  *   Jon A. Cruz <jon@joncruz.org>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 2008 authors
10  *
11  * Released under GNU GPL version 2 or later, read the file 'COPYING' for more information
12  */
14 #include "sp-script.h"
15 #include "attributes.h"
16 #include <cstring>
17 #include "document.h"
19 static void sp_script_class_init(SPScriptClass *sc);
20 static void sp_script_init(SPScript *script);
22 static void sp_script_release(SPObject *object);
23 static void sp_script_update(SPObject *object, SPCtx *ctx, guint flags);
24 static void sp_script_modified(SPObject *object, guint flags);
25 static void sp_script_set(SPObject *object, unsigned int key, gchar const *value);
26 static void sp_script_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
27 static Inkscape::XML::Node *sp_script_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
29 static SPObjectClass *parent_class;
31 GType sp_script_get_type(void)
32 {
33     static GType script_type = 0;
35     if (!script_type) {
36         GTypeInfo script_info = {
37             sizeof(SPScriptClass),
38             NULL,       /* base_init */
39             NULL,       /* base_finalize */
40             (GClassInitFunc) sp_script_class_init,
41             NULL,       /* class_finalize */
42             NULL,       /* class_data */
43             sizeof(SPScript),
44             16, /* n_preallocs */
45             (GInstanceInitFunc) sp_script_init,
46             NULL,       /* value_table */
47         };
48         script_type = g_type_register_static(SP_TYPE_OBJECT, "SPScript", &script_info, (GTypeFlags) 0);
49     }
51     return script_type;
52 }
54 static void sp_script_class_init(SPScriptClass *sc)
55 {
56     parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
57     SPObjectClass *sp_object_class = (SPObjectClass *) sc;
59     sp_object_class->build = sp_script_build;
60     sp_object_class->release = sp_script_release;
61     sp_object_class->update = sp_script_update;
62     sp_object_class->modified = sp_script_modified;
63     sp_object_class->write = sp_script_write;
64     sp_object_class->set = sp_script_set;
65 }
67 static void sp_script_init(SPScript */*script*/)
68 {
70 }
73 /**
74  * Reads the Inkscape::XML::Node, and initializes SPScript variables.  For this to get called,
75  * our name must be associated with a repr via "sp_object_type_register".  Best done through
76  * sp-object-repr.cpp's repr_name_entries array.
77  */
78 static void
79 sp_script_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
80 {
81     if (((SPObjectClass *) parent_class)->build) {
82         ((SPObjectClass *) parent_class)->build(object, document, repr);
83     }
85     //Read values of key attributes from XML nodes into object.
86     object->readAttr( "xlink:href" );
88     document->addResource("script", object);
89 }
91 static void sp_script_release(SPObject *object)
92 {
93     if (SP_OBJECT_DOCUMENT(object)) {
94         /* Unregister ourselves */
95         SP_OBJECT_DOCUMENT(object)->removeResource("script", SP_OBJECT(object));
96     }
98     if (((SPObjectClass *) parent_class)->release)
99         ((SPObjectClass *) parent_class)->release(object);
102 static void sp_script_update(SPObject */*object*/, SPCtx */*ctx*/, guint /*flags*/)
106 static void sp_script_modified(SPObject */*object*/, guint /*flags*/)
110 static void
111 sp_script_set(SPObject *object, unsigned int key, gchar const *value)
113     SPScript *scr = SP_SCRIPT(object);
115     switch (key) {
116         case SP_ATTR_XLINK_HREF:
117             if (scr->xlinkhref) g_free(scr->xlinkhref);
118             scr->xlinkhref = g_strdup(value);
119             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
120             break;
121         default:
122             if (((SPObjectClass *) parent_class)->set)
123                 ((SPObjectClass *) parent_class)->set(object, key, value);
124             break;
125     }
128 static Inkscape::XML::Node *sp_script_write(SPObject */*object*/, Inkscape::XML::Document */*xml_doc*/, Inkscape::XML::Node *repr, guint /*flags*/)
130 /*
131 TODO:
132  code copied from sp-defs
133  decide what to do here!
135     if (flags & SP_OBJECT_WRITE_BUILD) {
137         if (!repr) {
138             repr = xml_doc->createElement("svg:script");
139         }
141         GSList *l = NULL;
142         for ( SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
143             Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
144             if (crepr) {
145                 l = g_slist_prepend(l, crepr);
146             }
147         }
149         while (l) {
150             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
151             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
152             l = g_slist_remove(l, l->data);
153         }
155     } else {
156         for ( SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
157             child->updateRepr(flags);
158         }
159     }
161     if (((SPObjectClass *) (parent_class))->write) {
162         (* ((SPObjectClass *) (parent_class))->write)(object, xml_doc, repr, flags);
163     }
164 */
165     return repr;
168 /*
169   Local Variables:
170   mode:c++
171   c-file-style:"stroustrup"
172   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
173   indent-tabs-mode:nil
174   fill-column:99
175   End:
176 */
177 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :