1 /** \file
2 * SVG <fedistantlight> 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 "distantlight.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 /* FeDistantLight class */
34 static void sp_fedistantlight_class_init(SPFeDistantLightClass *klass);
35 static void sp_fedistantlight_init(SPFeDistantLight *fedistantlight);
37 static void sp_fedistantlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
38 static void sp_fedistantlight_release(SPObject *object);
39 static void sp_fedistantlight_set(SPObject *object, unsigned int key, gchar const *value);
40 static void sp_fedistantlight_update(SPObject *object, SPCtx *ctx, guint flags);
41 static Inkscape::XML::Node *sp_fedistantlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
43 static SPObjectClass *feDistantLight_parent_class;
45 GType
46 sp_fedistantlight_get_type()
47 {
48 static GType fedistantlight_type = 0;
50 if (!fedistantlight_type) {
51 GTypeInfo fedistantlight_info = {
52 sizeof(SPFeDistantLightClass),
53 NULL, NULL,
54 (GClassInitFunc) sp_fedistantlight_class_init,
55 NULL, NULL,
56 sizeof(SPFeDistantLight),
57 16,
58 (GInstanceInitFunc) sp_fedistantlight_init,
59 NULL, /* value_table */
60 };
61 fedistantlight_type = g_type_register_static(SP_TYPE_OBJECT, "SPFeDistantLight", &fedistantlight_info, (GTypeFlags)0);
62 }
63 return fedistantlight_type;
64 }
66 static void
67 sp_fedistantlight_class_init(SPFeDistantLightClass *klass)
68 {
70 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
72 feDistantLight_parent_class = (SPObjectClass*)g_type_class_peek_parent(klass);
74 sp_object_class->build = sp_fedistantlight_build;
75 sp_object_class->release = sp_fedistantlight_release;
76 sp_object_class->write = sp_fedistantlight_write;
77 sp_object_class->set = sp_fedistantlight_set;
78 sp_object_class->update = sp_fedistantlight_update;
79 }
81 static void
82 sp_fedistantlight_init(SPFeDistantLight *fedistantlight)
83 {
84 fedistantlight->azimuth = 0;
85 fedistantlight->elevation = 0;
86 fedistantlight->azimuth_set = FALSE;
87 fedistantlight->elevation_set = FALSE;
88 }
90 /**
91 * Reads the Inkscape::XML::Node, and initializes SPDistantLight variables. For this to get called,
92 * our name must be associated with a repr via "sp_object_type_register". Best done through
93 * sp-object-repr.cpp's repr_name_entries array.
94 */
95 static void
96 sp_fedistantlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
97 {
98 if (((SPObjectClass *) feDistantLight_parent_class)->build) {
99 ((SPObjectClass *) feDistantLight_parent_class)->build(object, document, repr);
100 }
102 //Read values of key attributes from XML nodes into object.
103 object->readAttr( "azimuth" );
104 object->readAttr( "elevation" );
106 //is this necessary?
107 document->addResource("fedistantlight", object);
108 }
110 /**
111 * Drops any allocated memory.
112 */
113 static void
114 sp_fedistantlight_release(SPObject *object)
115 {
116 //SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
118 if (SP_OBJECT_DOCUMENT(object)) {
119 /* Unregister ourselves */
120 SP_OBJECT_DOCUMENT(object)->removeResource("fedistantlight", SP_OBJECT(object));
121 }
123 //TODO: release resources here
124 }
126 /**
127 * Sets a specific value in the SPFeDistantLight.
128 */
129 static void
130 sp_fedistantlight_set(SPObject *object, unsigned int key, gchar const *value)
131 {
132 SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
133 gchar *end_ptr;
134 switch (key) {
135 case SP_ATTR_AZIMUTH:
136 end_ptr =NULL;
137 if (value) {
138 fedistantlight->azimuth = g_ascii_strtod(value, &end_ptr);
139 if (end_ptr) {
140 fedistantlight->azimuth_set = TRUE;
141 }
142 }
143 if (!value || !end_ptr) {
144 fedistantlight->azimuth_set = FALSE;
145 fedistantlight->azimuth = 0;
146 }
147 if (object->parent &&
148 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
149 SP_IS_FESPECULARLIGHTING(object->parent))) {
150 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
151 }
152 break;
153 case SP_ATTR_ELEVATION:
154 end_ptr =NULL;
155 if (value) {
156 fedistantlight->elevation = g_ascii_strtod(value, &end_ptr);
157 if (end_ptr) {
158 fedistantlight->elevation_set = TRUE;
159 }
160 }
161 if (!value || !end_ptr) {
162 fedistantlight->elevation_set = FALSE;
163 fedistantlight->elevation = 0;
164 }
165 if (object->parent &&
166 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
167 SP_IS_FESPECULARLIGHTING(object->parent))) {
168 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 }
170 break;
171 default:
172 // See if any parents need this value.
173 if (((SPObjectClass *) feDistantLight_parent_class)->set) {
174 ((SPObjectClass *) feDistantLight_parent_class)->set(object, key, value);
175 }
176 break;
177 }
178 }
180 /**
181 * * Receives update notifications.
182 * */
183 static void
184 sp_fedistantlight_update(SPObject *object, SPCtx *ctx, guint flags)
185 {
186 SPFeDistantLight *feDistantLight = SP_FEDISTANTLIGHT(object);
187 (void)feDistantLight;
189 if (flags & SP_OBJECT_MODIFIED_FLAG) {
190 /* do something to trigger redisplay, updates? */
191 object->readAttr( "azimuth" );
192 object->readAttr( "elevation" );
193 }
195 if (((SPObjectClass *) feDistantLight_parent_class)->update) {
196 ((SPObjectClass *) feDistantLight_parent_class)->update(object, ctx, flags);
197 }
198 }
200 /**
201 * Writes its settings to an incoming repr object, if any.
202 */
203 static Inkscape::XML::Node *
204 sp_fedistantlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
205 {
206 SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
208 if (!repr) {
209 repr = SP_OBJECT_REPR(object)->duplicate(doc);
210 }
212 if (fedistantlight->azimuth_set)
213 sp_repr_set_css_double(repr, "azimuth", fedistantlight->azimuth);
214 if (fedistantlight->elevation_set)
215 sp_repr_set_css_double(repr, "elevation", fedistantlight->elevation);
217 if (((SPObjectClass *) feDistantLight_parent_class)->write) {
218 ((SPObjectClass *) feDistantLight_parent_class)->write(object, doc, repr, flags);
219 }
221 return repr;
222 }
224 /*
225 Local Variables:
226 mode:c++
227 c-file-style:"stroustrup"
228 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
229 indent-tabs-mode:nil
230 fill-column:99
231 End:
232 */
233 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :