Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / filters / pointlight.cpp
1 /** \file
2  * SVG <fepointlight> implementation.
3  */
4 /*
5  * Authors:
6  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
7  *   Niko Kiirala <niko@kiirala.com>
8  *   Jean-Rene Reinhard <jr@komite.net>
9  *   Abhishek Sharma
10  *
11  * Copyright (C) 2006,2007 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include <glib.h>
22 #include "attributes.h"
23 #include "document.h"
24 #include "pointlight.h"
25 #include "diffuselighting-fns.h"
26 #include "specularlighting-fns.h"
27 #include "xml/repr.h"
29 #define SP_MACROS_SILENT
30 #include "macros.h"
32 /* FePointLight class */
34 static void sp_fepointlight_class_init(SPFePointLightClass *klass);
35 static void sp_fepointlight_init(SPFePointLight *fepointlight);
37 static void sp_fepointlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
38 static void sp_fepointlight_release(SPObject *object);
39 static void sp_fepointlight_set(SPObject *object, unsigned int key, gchar const *value);
40 static void sp_fepointlight_update(SPObject *object, SPCtx *ctx, guint flags);
41 static Inkscape::XML::Node *sp_fepointlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
43 static SPObjectClass *fePointLight_parent_class;
45 GType
46 sp_fepointlight_get_type()
47 {
48     static GType fepointlight_type = 0;
50     if (!fepointlight_type) {
51         GTypeInfo fepointlight_info = {
52             sizeof(SPFePointLightClass),
53             NULL, NULL,
54             (GClassInitFunc) sp_fepointlight_class_init,
55             NULL, NULL,
56             sizeof(SPFePointLight),
57             16,
58             (GInstanceInitFunc) sp_fepointlight_init,
59             NULL,    /* value_table */
60         };
61         fepointlight_type = g_type_register_static(SP_TYPE_OBJECT, "SPFePointLight", &fepointlight_info, (GTypeFlags)0);
62     }
63     return fepointlight_type;
64 }
66 static void
67 sp_fepointlight_class_init(SPFePointLightClass *klass)
68 {
70     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
72     fePointLight_parent_class = (SPObjectClass*)g_type_class_peek_parent(klass);
74     sp_object_class->build = sp_fepointlight_build;
75     sp_object_class->release = sp_fepointlight_release;
76     sp_object_class->write = sp_fepointlight_write;
77     sp_object_class->set = sp_fepointlight_set;
78     sp_object_class->update = sp_fepointlight_update;
79 }
81 static void
82 sp_fepointlight_init(SPFePointLight *fepointlight)
83 {
84     fepointlight->x = 0;
85     fepointlight->y = 0;
86     fepointlight->z = 0;
88     fepointlight->x_set = FALSE;
89     fepointlight->y_set = FALSE;
90     fepointlight->z_set = FALSE;
91 }
93 /**
94  * Reads the Inkscape::XML::Node, and initializes SPPointLight variables.  For this to get called,
95  * our name must be associated with a repr via "sp_object_type_register".  Best done through
96  * sp-object-repr.cpp's repr_name_entries array.
97  */
98 static void
99 sp_fepointlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
101     if (((SPObjectClass *) fePointLight_parent_class)->build) {
102         ((SPObjectClass *) fePointLight_parent_class)->build(object, document, repr);
103     }
105     //Read values of key attributes from XML nodes into object.
106     object->readAttr( "x" );
107     object->readAttr( "y" );
108     object->readAttr( "z" );
110 //is this necessary?
111     document->addResource("fepointlight", object);
114 /**
115  * Drops any allocated memory.
116  */
117 static void
118 sp_fepointlight_release(SPObject *object)
120     //SPFePointLight *fepointlight = SP_FEPOINTLIGHT(object);
122     if (SP_OBJECT_DOCUMENT(object)) {
123         /* Unregister ourselves */
124         SP_OBJECT_DOCUMENT(object)->removeResource("fepointlight", SP_OBJECT(object));
125     }
127 //TODO: release resources here
130 /**
131  * Sets a specific value in the SPFePointLight.
132  */
133 static void
134 sp_fepointlight_set(SPObject *object, unsigned int key, gchar const *value)
136     SPFePointLight *fepointlight = SP_FEPOINTLIGHT(object);
137     gchar *end_ptr;
138     switch (key) {
139     case SP_ATTR_X:
140         end_ptr = NULL;
141         if (value) {
142             fepointlight->x = g_ascii_strtod(value, &end_ptr);
143             if (end_ptr) {
144                 fepointlight->x_set = TRUE;
145             }
146         }
147         if (!value || !end_ptr) {
148             fepointlight->x = 0;
149             fepointlight->x_set = FALSE;
150         }
151         if (object->parent &&
152                 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
153                  SP_IS_FESPECULARLIGHTING(object->parent))) {
154             object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
155         }
156         break;
157     case SP_ATTR_Y:
158         end_ptr = NULL;
159         if (value) {
160             fepointlight->y = g_ascii_strtod(value, &end_ptr);
161             if (end_ptr) {
162                 fepointlight->y_set = TRUE;
163             }
164         }
165         if (!value || !end_ptr) {
166             fepointlight->y = 0;
167             fepointlight->y_set = FALSE;
168         }
169         if (object->parent &&
170                 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
171                  SP_IS_FESPECULARLIGHTING(object->parent))) {
172             object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
173         }
174         break;
175     case SP_ATTR_Z:
176         end_ptr = NULL;
177         if (value) {
178             fepointlight->z = g_ascii_strtod(value, &end_ptr);
179             if (end_ptr) {
180                 fepointlight->z_set = TRUE;
181             }
182         }
183         if (!value || !end_ptr) {
184             fepointlight->z = 0;
185             fepointlight->z_set = FALSE;
186         }
187         if (object->parent &&
188                 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
189                  SP_IS_FESPECULARLIGHTING(object->parent))) {
190             object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
191         }
192         break;
193     default:
194         // See if any parents need this value.
195         if (((SPObjectClass *) fePointLight_parent_class)->set) {
196             ((SPObjectClass *) fePointLight_parent_class)->set(object, key, value);
197         }
198         break;
199     }
202 /**
203  *  * Receives update notifications.
204  *   */
205 static void
206 sp_fepointlight_update(SPObject *object, SPCtx *ctx, guint flags)
208     SPFePointLight *fePointLight = SP_FEPOINTLIGHT(object);
209     (void)fePointLight;
211     if (flags & SP_OBJECT_MODIFIED_FLAG) {
212         /* do something to trigger redisplay, updates? */
213         object->readAttr( "x" );
214         object->readAttr( "y" );
215         object->readAttr( "z" );
216     }
218     if (((SPObjectClass *) fePointLight_parent_class)->update) {
219         ((SPObjectClass *) fePointLight_parent_class)->update(object, ctx, flags);
220     }
223 /**
224  * Writes its settings to an incoming repr object, if any.
225  */
226 static Inkscape::XML::Node *
227 sp_fepointlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
229     SPFePointLight *fepointlight = SP_FEPOINTLIGHT(object);
231     if (!repr) {
232         repr = SP_OBJECT_REPR(object)->duplicate(doc);
233     }
235     if (fepointlight->x_set)
236         sp_repr_set_css_double(repr, "x", fepointlight->x);
237     if (fepointlight->y_set)
238         sp_repr_set_css_double(repr, "y", fepointlight->y);
239     if (fepointlight->z_set)
240         sp_repr_set_css_double(repr, "z", fepointlight->z);
242     if (((SPObjectClass *) fePointLight_parent_class)->write) {
243         ((SPObjectClass *) fePointLight_parent_class)->write(object, doc, repr, flags);
244     }
246     return repr;
249 /*
250   Local Variables:
251   mode:c++
252   c-file-style:"stroustrup"
253   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254   indent-tabs-mode:nil
255   fill-column:99
256   End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :