Code

Implement object-snapping for clones (see bug #1511260)
[inkscape.git] / src / sp-feblend.cpp
1 #define __SP_FEBLEND_CPP__
3 /** \file
4  * SVG <feBlend> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
10  *   Niko Kiirala <niko@kiirala.com>
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 <string.h>
23 #include "attributes.h"
24 #include "svg/svg.h"
25 #include "sp-feblend.h"
26 #include "xml/repr.h"
28 #include "display/nr-filter-blend.h"
30 /* FeBlend base class */
32 static void sp_feBlend_class_init(SPFeBlendClass *klass);
33 static void sp_feBlend_init(SPFeBlend *feBlend);
35 static void sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
36 static void sp_feBlend_release(SPObject *object);
37 static void sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value);
38 static void sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags);
39 static Inkscape::XML::Node *sp_feBlend_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
41 static SPFilterPrimitiveClass *feBlend_parent_class;
43 GType
44 sp_feBlend_get_type()
45 {
46     static GType feBlend_type = 0;
48     if (!feBlend_type) {
49         GTypeInfo feBlend_info = {
50             sizeof(SPFeBlendClass),
51             NULL, NULL,
52             (GClassInitFunc) sp_feBlend_class_init,
53             NULL, NULL,
54             sizeof(SPFeBlend),
55             16,
56             (GInstanceInitFunc) sp_feBlend_init,
57             NULL,    /* value_table */
58         };
59         feBlend_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeBlend", &feBlend_info, (GTypeFlags)0);
60     }
61     return feBlend_type;
62 }
64 static void
65 sp_feBlend_class_init(SPFeBlendClass *klass)
66 {
67     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
69     feBlend_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
71     sp_object_class->build = sp_feBlend_build;
72     sp_object_class->release = sp_feBlend_release;
73     sp_object_class->write = sp_feBlend_write;
74     sp_object_class->set = sp_feBlend_set;
75     sp_object_class->update = sp_feBlend_update;
76 }
78 static void
79 sp_feBlend_init(SPFeBlend *feBlend)
80 {
81 }
83 /**
84  * Reads the Inkscape::XML::Node, and initializes SPFeBlend variables.  For this to get called,
85  * our name must be associated with a repr via "sp_object_type_register".  Best done through
86  * sp-object-repr.cpp's repr_name_entries array.
87  */
88 static void
89 sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
90 {
91     if (((SPObjectClass *) feBlend_parent_class)->build) {
92         ((SPObjectClass *) feBlend_parent_class)->build(object, document, repr);
93     }
95     /*LOAD ATTRIBUTES FROM REPR HERE*/
96     sp_object_read_attr(object, "mode");
97 }
99 /**
100  * Drops any allocated memory.
101  */
102 static void
103 sp_feBlend_release(SPObject *object)
105     if (((SPObjectClass *) feBlend_parent_class)->release)
106         ((SPObjectClass *) feBlend_parent_class)->release(object);
109 static NR::FilterBlendMode sp_feBlend_readmode(gchar const *value)
111     if (!value) return NR::BLEND_NORMAL;
112     switch (value[0]) {
113         case 'n':
114             if (strncmp(value, "normal", 6) == 0)
115                 return NR::BLEND_NORMAL;
116             break;
117         case 'm':
118             if (strncmp(value, "multiply", 8) == 0)
119                 return NR::BLEND_MULTIPLY;
120             break;
121         case 's':
122             if (strncmp(value, "screen", 6) == 0)
123                 return NR::BLEND_SCREEN;
124             break;
125         case 'd':
126             if (strncmp(value, "darken", 6) == 0)
127                 return NR::BLEND_DARKEN;
128             break;
129         case 'l':
130             if (strncmp(value, "lighten", 7) == 0)
131                 return NR::BLEND_LIGHTEN;
132             break;
133         default:
134             // do nothing by default
135             break;
136     }
137     return NR::BLEND_NORMAL;
140 /**
141  * Sets a specific value in the SPFeBlend.
142  */
143 static void
144 sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value)
146     SPFeBlend *feBlend = SP_FEBLEND(object);
147     (void)feBlend;
149     switch(key) {
150         /*DEAL WITH SETTING ATTRIBUTES HERE*/
151         case SP_ATTR_MODE:
152             feBlend->blend_mode = sp_feBlend_readmode(value);
153             break;
154         default:
155             if (((SPObjectClass *) feBlend_parent_class)->set)
156                 ((SPObjectClass *) feBlend_parent_class)->set(object, key, value);
157             break;
158     }
162 /**
163  * Receives update notifications.
164  */
165 static void
166 sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags)
168     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
169                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
171         /* do something to trigger redisplay, updates? */
173     }
175     if (((SPObjectClass *) feBlend_parent_class)->update) {
176         ((SPObjectClass *) feBlend_parent_class)->update(object, ctx, flags);
177     }
180 /**
181  * Writes its settings to an incoming repr object, if any.
182  */
183 static Inkscape::XML::Node *
184 sp_feBlend_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
186     // Inkscape-only object, not copied during an "plain SVG" dump:
187     if (flags & SP_OBJECT_WRITE_EXT) {
188         if (repr) {
189             // is this sane?
190             // repr->mergeFrom(SP_OBJECT_REPR(object), "id");
191         } else {
192             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
193         }
194     }
196     if (((SPObjectClass *) feBlend_parent_class)->write) {
197         ((SPObjectClass *) feBlend_parent_class)->write(object, repr, flags);
198     }
200     return repr;
204 /*
205   Local Variables:
206   mode:c++
207   c-file-style:"stroustrup"
208   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
209   indent-tabs-mode:nil
210   fill-column:99
211   End:
212 */
213 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :