Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-defs.cpp
1 /*
2  * SVG <defs> implementation
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Jon A. Cruz <jon@joncruz.org>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 2000-2002 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 /*
15  * fixme: We should really check childrens validity - currently everything
16  * flips in
17  */
19 #include "sp-defs.h"
20 #include "xml/repr.h"
21 #include "document.h"
23 SPObjectClass * SPDefsClass::static_parent_class = 0;
25 GType SPDefs::sp_defs_get_type(void)
26 {
27     static GType defs_type = 0;
29     if (!defs_type) {
30         GTypeInfo defs_info = {
31             sizeof(SPDefsClass),
32             NULL,       /* base_init */
33             NULL,       /* base_finalize */
34             (GClassInitFunc) SPDefsClass::sp_defs_class_init,
35             NULL,       /* class_finalize */
36             NULL,       /* class_data */
37             sizeof(SPDefs),
38             16, /* n_preallocs */
39             (GInstanceInitFunc) init,
40             NULL,       /* value_table */
41         };
42         defs_type = g_type_register_static(SP_TYPE_OBJECT, "SPDefs", &defs_info, (GTypeFlags) 0);
43     }
45     return defs_type;
46 }
48 void SPDefsClass::sp_defs_class_init(SPDefsClass *dc)
49 {
50     static_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
51     SPObjectClass *sp_object_class = (SPObjectClass *) dc;
53     sp_object_class->release = SPDefs::release;
54     sp_object_class->update = SPDefs::update;
55     sp_object_class->modified = SPDefs::modified;
56     sp_object_class->write = SPDefs::write;
57 }
59 void SPDefs::init(SPDefs */*defs*/)
60 {
62 }
64 void SPDefs::release(SPObject *object)
65 {
66     if (((SPObjectClass *) (SPDefsClass::static_parent_class))->release) {
67         ((SPObjectClass *) (SPDefsClass::static_parent_class))->release(object);
68     }
69 }
71 void SPDefs::update(SPObject *object, SPCtx *ctx, guint flags)
72 {
73     if (flags & SP_OBJECT_MODIFIED_FLAG) {
74         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
75     }
77     flags &= SP_OBJECT_MODIFIED_CASCADE;
79     GSList *l = g_slist_reverse(object->childList(true));
80     while (l) {
81         SPObject *child = SP_OBJECT(l->data);
82         l = g_slist_remove(l, child);
83         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
84             child->updateDisplay(ctx, flags);
85         }
86         g_object_unref (G_OBJECT (child));
87     }
88 }
90 void SPDefs::modified(SPObject *object, guint flags)
91 {
92     if (flags & SP_OBJECT_MODIFIED_FLAG) {
93         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
94     }
96     flags &= SP_OBJECT_MODIFIED_CASCADE;
98     GSList *l = NULL;
99     for ( SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
100         g_object_ref(G_OBJECT(child));
101         l = g_slist_prepend(l, child);
102     }
104     l = g_slist_reverse(l);
106     while (l) {
107         SPObject *child = SP_OBJECT(l->data);
108         l = g_slist_remove(l, child);
109         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
110             child->emitModified(flags);
111         }
112         g_object_unref( G_OBJECT(child) );
113     }
116 Inkscape::XML::Node * SPDefs::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
118     if (flags & SP_OBJECT_WRITE_BUILD) {
120         if (!repr) {
121             repr = xml_doc->createElement("svg:defs");
122         }
124         GSList *l = NULL;
125         for ( SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
126             Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
127             if (crepr) {
128                 l = g_slist_prepend(l, crepr);
129             }
130         }
132         while (l) {
133             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
134             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
135             l = g_slist_remove(l, l->data);
136         }
138     } else {
139         for ( SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
140             child->updateRepr(flags);
141         }
142     }
144     if (((SPObjectClass *) (SPDefsClass::static_parent_class))->write) {
145         (* ((SPObjectClass *) (SPDefsClass::static_parent_class))->write)(object, xml_doc, repr, flags);
146     }
148     return repr;
151 /*
152   Local Variables:
153   mode:c++
154   c-file-style:"stroustrup"
155   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156   indent-tabs-mode:nil
157   fill-column:99
158   End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :