1 #define __SP_DEFS_C__
3 /*
4 * SVG <defs> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
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 static void sp_defs_class_init(SPDefsClass *dc);
24 static void sp_defs_init(SPDefs *defs);
26 static void sp_defs_release(SPObject *object);
27 static void sp_defs_update(SPObject *object, SPCtx *ctx, guint flags);
28 static void sp_defs_modified(SPObject *object, guint flags);
29 static Inkscape::XML::Node *sp_defs_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
31 static SPObjectClass *parent_class;
33 GType sp_defs_get_type(void)
34 {
35 static GType defs_type = 0;
37 if (!defs_type) {
38 GTypeInfo defs_info = {
39 sizeof(SPDefsClass),
40 NULL, /* base_init */
41 NULL, /* base_finalize */
42 (GClassInitFunc) sp_defs_class_init,
43 NULL, /* class_finalize */
44 NULL, /* class_data */
45 sizeof(SPDefs),
46 16, /* n_preallocs */
47 (GInstanceInitFunc) sp_defs_init,
48 NULL, /* value_table */
49 };
50 defs_type = g_type_register_static(SP_TYPE_OBJECT, "SPDefs", &defs_info, (GTypeFlags) 0);
51 }
53 return defs_type;
54 }
56 static void sp_defs_class_init(SPDefsClass *dc)
57 {
58 parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
59 SPObjectClass *sp_object_class = (SPObjectClass *) dc;
61 sp_object_class->release = sp_defs_release;
62 sp_object_class->update = sp_defs_update;
63 sp_object_class->modified = sp_defs_modified;
64 sp_object_class->write = sp_defs_write;
65 }
67 static void sp_defs_init(SPDefs */*defs*/)
68 {
70 }
72 static void sp_defs_release(SPObject *object)
73 {
74 if (((SPObjectClass *) (parent_class))->release) {
75 ((SPObjectClass *) (parent_class))->release(object);
76 }
77 }
79 static void sp_defs_update(SPObject *object, SPCtx *ctx, guint flags)
80 {
81 if (flags & SP_OBJECT_MODIFIED_FLAG) {
82 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
83 }
85 flags &= SP_OBJECT_MODIFIED_CASCADE;
87 GSList *l = g_slist_reverse(object->childList(true));
88 while (l) {
89 SPObject *child = SP_OBJECT(l->data);
90 l = g_slist_remove(l, child);
91 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
92 child->updateDisplay(ctx, flags);
93 }
94 g_object_unref (G_OBJECT (child));
95 }
96 }
98 static void sp_defs_modified(SPObject *object, guint flags)
99 {
100 if (flags & SP_OBJECT_MODIFIED_FLAG) {
101 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
102 }
104 flags &= SP_OBJECT_MODIFIED_CASCADE;
106 GSList *l = NULL;
107 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
108 g_object_ref(G_OBJECT(child));
109 l = g_slist_prepend(l, child);
110 }
112 l = g_slist_reverse(l);
114 while (l) {
115 SPObject *child = SP_OBJECT(l->data);
116 l = g_slist_remove(l, child);
117 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
118 child->emitModified(flags);
119 }
120 g_object_unref(G_OBJECT (child));
121 }
122 }
124 static Inkscape::XML::Node *sp_defs_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
125 {
126 if (flags & SP_OBJECT_WRITE_BUILD) {
128 if (!repr) {
129 repr = xml_doc->createElement("svg:defs");
130 }
132 GSList *l = NULL;
133 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
134 Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
135 if (crepr) l = g_slist_prepend(l, crepr);
136 }
138 while (l) {
139 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
140 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
141 l = g_slist_remove(l, l->data);
142 }
144 } else {
145 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
146 child->updateRepr(flags);
147 }
148 }
150 if (((SPObjectClass *) (parent_class))->write) {
151 (* ((SPObjectClass *) (parent_class))->write)(object, xml_doc, repr, flags);
152 }
154 return repr;
155 }
157 /*
158 Local Variables:
159 mode:c++
160 c-file-style:"stroustrup"
161 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
162 indent-tabs-mode:nil
163 fill-column:99
164 End:
165 */
166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :