Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / filters / flood.cpp
1 #define __SP_FEFLOOD_CPP__
3 /** \file
4  * SVG <feFlood> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   hugo Rodrigues <haa.rodrigues@gmail.com>
10  *
11  * Copyright (C) 2006 Hugo Rodrigues
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 "strneq.h"
22 #include "attributes.h"
23 #include "svg/svg.h"
24 #include "flood.h"
25 #include "xml/repr.h"
26 #include "helper-fns.h"
28 /* FeFlood base class */
30 static void sp_feFlood_class_init(SPFeFloodClass *klass);
31 static void sp_feFlood_init(SPFeFlood *feFlood);
33 static void sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
34 static void sp_feFlood_release(SPObject *object);
35 static void sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value);
36 static void sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags);
37 static Inkscape::XML::Node *sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
38 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
40 static SPFilterPrimitiveClass *feFlood_parent_class;
42 GType
43 sp_feFlood_get_type()
44 {
45     static GType feFlood_type = 0;
47     if (!feFlood_type) {
48         GTypeInfo feFlood_info = {
49             sizeof(SPFeFloodClass),
50             NULL, NULL,
51             (GClassInitFunc) sp_feFlood_class_init,
52             NULL, NULL,
53             sizeof(SPFeFlood),
54             16,
55             (GInstanceInitFunc) sp_feFlood_init,
56             NULL,    /* value_table */
57         };
58         feFlood_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeFlood", &feFlood_info, (GTypeFlags)0);
59     }
60     return feFlood_type;
61 }
63 static void
64 sp_feFlood_class_init(SPFeFloodClass *klass)
65 {
66     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
67     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
69     feFlood_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
71     sp_object_class->build = sp_feFlood_build;
72     sp_object_class->release = sp_feFlood_release;
73     sp_object_class->write = sp_feFlood_write;
74     sp_object_class->set = sp_feFlood_set;
75     sp_object_class->update = sp_feFlood_update;
76     sp_primitive_class->build_renderer = sp_feFlood_build_renderer;
77 }
79 static void
80 sp_feFlood_init(SPFeFlood *feFlood)
81 {
82     feFlood->opacity = 1;
83     feFlood->icc = NULL;
84 }
86 /**
87  * Reads the Inkscape::XML::Node, and initializes SPFeFlood variables.  For this to get called,
88  * our name must be associated with a repr via "sp_object_type_register".  Best done through
89  * sp-object-repr.cpp's repr_name_entries array.
90  */
91 static void
92 sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
93 {
94     if (((SPObjectClass *) feFlood_parent_class)->build) {
95         ((SPObjectClass *) feFlood_parent_class)->build(object, document, repr);
96     }
98     /*LOAD ATTRIBUTES FROM REPR HERE*/
99     object->readAttr( "flood-opacity");
100     object->readAttr( "flood-color");
103 /**
104  * Drops any allocated memory.
105  */
106 static void
107 sp_feFlood_release(SPObject *object)
109     if (((SPObjectClass *) feFlood_parent_class)->release)
110         ((SPObjectClass *) feFlood_parent_class)->release(object);
113 /**
114  * Sets a specific value in the SPFeFlood.
115  */
116 static void
117 sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value)
119     SPFeFlood *feFlood = SP_FEFLOOD(object);
120     (void)feFlood;
121     gchar const *cend_ptr = NULL;
122     gchar *end_ptr = NULL;
123     guint32 read_color;
124     double read_num;
125     bool dirty = false;
126     
127     switch(key) {
128         /*DEAL WITH SETTING ATTRIBUTES HERE*/
129         case SP_PROP_FLOOD_COLOR:
130             cend_ptr = NULL;
131             read_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
133             if (cend_ptr && read_color != feFlood->color){
134                 feFlood->color = read_color;
135                 dirty=true;
136             }
138             if (cend_ptr){
139                 while (g_ascii_isspace(*cend_ptr)) {
140                     ++cend_ptr;
141                 }
142                 if (strneq(cend_ptr, "icc-color(", 10)) {
143                     if (!feFlood->icc) feFlood->icc = new SVGICCColor();
144                     if ( ! sp_svg_read_icc_color( cend_ptr, feFlood->icc ) ) {
145                         delete feFlood->icc;
146                         feFlood->icc = NULL;
147                     }
148                     dirty = true;
149                 }
150             }
151             if (dirty)
152                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
153             break;
154         case SP_PROP_FLOOD_OPACITY:
155             if (value) {
156                 read_num = g_ascii_strtod(value, &end_ptr);
157                 if (*end_ptr) {
158                     g_warning("Unable to convert \"%s\" to number", value);
159                     read_num = 1;
160                 }
161             }
162             else {
163                 read_num = 1;
164             }
165             if (read_num != feFlood->opacity){
166                 feFlood->opacity = read_num;
167                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
168             }
169             break;
170         default:
171             if (((SPObjectClass *) feFlood_parent_class)->set)
172                 ((SPObjectClass *) feFlood_parent_class)->set(object, key, value);
173             break;
174     }
178 /**
179  * Receives update notifications.
180  */
181 static void
182 sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags)
184     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
185                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
187         /* do something to trigger redisplay, updates? */
189     }
191     if (((SPObjectClass *) feFlood_parent_class)->update) {
192         ((SPObjectClass *) feFlood_parent_class)->update(object, ctx, flags);
193     }
196 /**
197  * Writes its settings to an incoming repr object, if any.
198  */
199 static Inkscape::XML::Node *
200 sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
202     /* TODO: Don't just clone, but create a new repr node and write all
203      * relevant values into it */
204     if (!repr) {
205         repr = SP_OBJECT_REPR(object)->duplicate(doc);
206     }
208     if (((SPObjectClass *) feFlood_parent_class)->write) {
209         ((SPObjectClass *) feFlood_parent_class)->write(object, doc, repr, flags);
210     }
212     return repr;
215 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
216     g_assert(primitive != NULL);
217     g_assert(filter != NULL);
219     SPFeFlood *sp_flood = SP_FEFLOOD(primitive);
220     (void)sp_flood;
222     int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_FLOOD);
223     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
224     Inkscape::Filters::FilterFlood *nr_flood = dynamic_cast<Inkscape::Filters::FilterFlood*>(nr_primitive);
225     g_assert(nr_flood != NULL);
227     sp_filter_primitive_renderer_common(primitive, nr_primitive);
228     
229     nr_flood->set_opacity(sp_flood->opacity);
230     nr_flood->set_color(sp_flood->color);
231     nr_flood->set_icc(sp_flood->icc);
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :