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 "sp-fedistantlight.h"
26 #include "sp-fediffuselighting-fns.h"
27 #include "sp-fespecularlighting-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::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 sp_object_read_attr(object, "azimuth");
105 sp_object_read_attr(object, "elevation");
107 //is this necessary?
108 sp_document_add_resource(document, "fedistantlight", object);
109 }
111 /**
112 * Drops any allocated memory.
113 */
114 static void
115 sp_fedistantlight_release(SPObject *object)
116 {
117 //SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
119 if (SP_OBJECT_DOCUMENT(object)) {
120 /* Unregister ourselves */
121 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "fedistantlight", SP_OBJECT(object));
122 }
124 //TODO: release resources here
125 }
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)
132 {
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 }
179 }
181 /**
182 * * Receives update notifications.
183 * */
184 static void
185 sp_fedistantlight_update(SPObject *object, SPCtx *ctx, guint flags)
186 {
187 SPFeDistantLight *feDistantLight = SP_FEDISTANTLIGHT(object);
189 if (flags & SP_OBJECT_MODIFIED_FLAG) {
190 /* do something to trigger redisplay, updates? */
191 sp_object_read_attr(object, "azimuth");
192 sp_object_read_attr(object, "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::Node *repr, guint flags)
205 {
206 SPFeDistantLight *fedistantlight = SP_FEDISTANTLIGHT(object);
208 if (!repr) {
209 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
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, 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:encoding=utf-8:textwidth=99 :