Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / filters / distantlight.cpp
1 #define __SP_FEDISTANTLIGHT_CPP__
3 /** \file
4  * SVG <fedistantlight> implementation.
5  */
6 /*
7  * Authors:
8  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
9  *   Niko Kiirala <niko@kiirala.com>
10  *   Jean-Rene Reinhard <jr@komite.net>
11  *
12  * Copyright (C) 2006,2007 Authors
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <glib.h>
23 #include "attributes.h"
24 #include "document.h"
25 #include "distantlight.h"
26 #include "diffuselighting-fns.h"
27 #include "specularlighting-fns.h"
28 #include "xml/repr.h"
30 #define SP_MACROS_SILENT
31 #include "macros.h"
33 /* FeDistantLight class */
35 static void sp_fedistantlight_class_init(SPFeDistantLightClass *klass);
36 static void sp_fedistantlight_init(SPFeDistantLight *fedistantlight);
38 static void sp_fedistantlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
39 static void sp_fedistantlight_release(SPObject *object);
40 static void sp_fedistantlight_set(SPObject *object, unsigned int key, gchar const *value);
41 static void sp_fedistantlight_update(SPObject *object, SPCtx *ctx, guint flags);
42 static Inkscape::XML::Node *sp_fedistantlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
44 static SPObjectClass *feDistantLight_parent_class;
46 GType
47 sp_fedistantlight_get_type()
48 {
49     static GType fedistantlight_type = 0;
51     if (!fedistantlight_type) {
52         GTypeInfo fedistantlight_info = {
53             sizeof(SPFeDistantLightClass),
54             NULL, NULL,
55             (GClassInitFunc) sp_fedistantlight_class_init,
56             NULL, NULL,
57             sizeof(SPFeDistantLight),
58             16,
59             (GInstanceInitFunc) sp_fedistantlight_init,
60             NULL,    /* value_table */
61         };
62         fedistantlight_type = g_type_register_static(SP_TYPE_OBJECT, "SPFeDistantLight", &fedistantlight_info, (GTypeFlags)0);
63     }
64     return fedistantlight_type;
65 }
67 static void
68 sp_fedistantlight_class_init(SPFeDistantLightClass *klass)
69 {
71     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
73     feDistantLight_parent_class = (SPObjectClass*)g_type_class_peek_parent(klass);
75     sp_object_class->build = sp_fedistantlight_build;
76     sp_object_class->release = sp_fedistantlight_release;
77     sp_object_class->write = sp_fedistantlight_write;
78     sp_object_class->set = sp_fedistantlight_set;
79     sp_object_class->update = sp_fedistantlight_update;
80 }
82 static void
83 sp_fedistantlight_init(SPFeDistantLight *fedistantlight)
84 {
85     fedistantlight->azimuth = 0;
86     fedistantlight->elevation = 0;
87     fedistantlight->azimuth_set = FALSE;
88     fedistantlight->elevation_set = FALSE;
89 }
91 /**
92  * Reads the Inkscape::XML::Node, and initializes SPDistantLight variables.  For this to get called,
93  * our name must be associated with a repr via "sp_object_type_register".  Best done through
94  * sp-object-repr.cpp's repr_name_entries array.
95  */
96 static void
97 sp_fedistantlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
98 {
99     if (((SPObjectClass *) feDistantLight_parent_class)->build) {
100         ((SPObjectClass *) feDistantLight_parent_class)->build(object, document, repr);
101     }
103     //Read values of key attributes from XML nodes into object.
104     object->readAttr( "azimuth");
105     object->readAttr( "elevation");
107 //is this necessary?
108     document->add_resource("fedistantlight", object);
111 /**
112  * Drops any allocated memory.
113  */
114 static void
115 sp_fedistantlight_release(SPObject *object)
117     //SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
119     if (SP_OBJECT_DOCUMENT(object)) {
120         /* Unregister ourselves */
121         SP_OBJECT_DOCUMENT(object)->remove_resource("fedistantlight", SP_OBJECT(object));
122     }
124 //TODO: release resources here
127 /**
128  * Sets a specific value in the SPFeDistantLight.
129  */
130 static void
131 sp_fedistantlight_set(SPObject *object, unsigned int key, gchar const *value)
133     SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
134     gchar *end_ptr;
135     switch (key) {
136     case SP_ATTR_AZIMUTH:
137         end_ptr =NULL;
138         if (value) {
139             fedistantlight->azimuth = g_ascii_strtod(value, &end_ptr);
140             if (end_ptr) {
141                 fedistantlight->azimuth_set = TRUE;
142             }
143         }
144         if (!value || !end_ptr) {
145                 fedistantlight->azimuth_set = FALSE;
146                 fedistantlight->azimuth = 0;
147         }
148         if (object->parent &&
149                 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
150                  SP_IS_FESPECULARLIGHTING(object->parent))) {
151             object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
152         }
153         break;
154     case SP_ATTR_ELEVATION:
155         end_ptr =NULL;
156         if (value) {
157             fedistantlight->elevation = g_ascii_strtod(value, &end_ptr);
158             if (end_ptr) {
159                 fedistantlight->elevation_set = TRUE;
160             }
161         }
162         if (!value || !end_ptr) {
163                 fedistantlight->elevation_set = FALSE;
164                 fedistantlight->elevation = 0;
165         }
166         if (object->parent &&
167                 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
168                  SP_IS_FESPECULARLIGHTING(object->parent))) {
169             object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
170         }
171         break;
172     default:
173         // See if any parents need this value.
174         if (((SPObjectClass *) feDistantLight_parent_class)->set) {
175             ((SPObjectClass *) feDistantLight_parent_class)->set(object, key, value);
176         }
177         break;
178     }
181 /**
182  *  * Receives update notifications.
183  *   */
184 static void
185 sp_fedistantlight_update(SPObject *object, SPCtx *ctx, guint flags)
187     SPFeDistantLight *feDistantLight = SP_FEDISTANTLIGHT(object);
188     (void)feDistantLight;
190     if (flags & SP_OBJECT_MODIFIED_FLAG) {
191         /* do something to trigger redisplay, updates? */
192         object->readAttr( "azimuth");
193         object->readAttr( "elevation");
194     }
196     if (((SPObjectClass *) feDistantLight_parent_class)->update) {
197         ((SPObjectClass *) feDistantLight_parent_class)->update(object, ctx, flags);
198     }
201 /**
202  * Writes its settings to an incoming repr object, if any.
203  */
204 static Inkscape::XML::Node *
205 sp_fedistantlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
207     SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
209     if (!repr) {
210         repr = SP_OBJECT_REPR(object)->duplicate(doc);
211     }
213     if (fedistantlight->azimuth_set)
214         sp_repr_set_css_double(repr, "azimuth", fedistantlight->azimuth);
215     if (fedistantlight->elevation_set)
216         sp_repr_set_css_double(repr, "elevation", fedistantlight->elevation);
218     if (((SPObjectClass *) feDistantLight_parent_class)->write) {
219         ((SPObjectClass *) feDistantLight_parent_class)->write(object, doc, repr, flags);
220     }
222     return repr;
225 /*
226   Local Variables:
227   mode:c++
228   c-file-style:"stroustrup"
229   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
230   indent-tabs-mode:nil
231   fill-column:99
232   End:
233 */
234 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :