1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Authors:
4  *   Nicholas Bishop <nicholasbishop@gmail.com>
5  *   Rodrigo Kumpera <kumpera@gmail.com>
6  *
7  * Copyright (C) 2007 Authors
8  *
9  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10  */
11 
12 #ifndef INKSCAPE_UI_WIDGET_ATTR_WIDGET_H
13 #define INKSCAPE_UI_WIDGET_ATTR_WIDGET_H
14 
15 #include "attributes.h"
16 #include "object/sp-object.h"
17 #include "xml/node.h"
18 
19 namespace Inkscape {
20 namespace UI {
21 namespace Widget {
22 
23 enum DefaultValueType
24 {
25     T_NONE,
26     T_DOUBLE,
27     T_VECT_DOUBLE,
28     T_BOOL,
29     T_UINT,
30     T_CHARPTR
31 };
32 
33 /**
34  * Very basic interface for classes that control attributes.
35  */
36 class DefaultValueHolder
37 {
38     DefaultValueType type;
39     union {
40         double d_val;
41         std::vector<double>* vt_val;
42         bool b_val;
43         unsigned int uint_val;
44         char* cptr_val;
45     } value;
46 
47     //FIXME remove copy ctor and assignment operator as private to avoid double free of the vector
48 public:
DefaultValueHolder()49     DefaultValueHolder () {
50         type = T_NONE;
51     }
52 
DefaultValueHolder(double d)53     DefaultValueHolder (double d) {
54         type = T_DOUBLE;
55         value.d_val = d;
56     }
57 
DefaultValueHolder(std::vector<double> * d)58     DefaultValueHolder (std::vector<double>* d) {
59         type = T_VECT_DOUBLE;
60         value.vt_val = d;
61     }
62 
DefaultValueHolder(char * c)63     DefaultValueHolder (char* c) {
64         type = T_CHARPTR;
65         value.cptr_val = c;
66     }
67 
DefaultValueHolder(bool d)68     DefaultValueHolder (bool d) {
69         type = T_BOOL;
70         value.b_val = d;
71     }
72 
DefaultValueHolder(unsigned int ui)73     DefaultValueHolder (unsigned int ui) {
74         type = T_UINT;
75         value.uint_val = ui;
76     }
77 
~DefaultValueHolder()78     ~DefaultValueHolder() {
79         if (type == T_VECT_DOUBLE)
80             delete value.vt_val;
81     }
82 
as_uint()83     unsigned int as_uint() {
84         g_assert (type == T_UINT);
85         return value.uint_val;
86     }
87 
as_bool()88     bool as_bool() {
89         g_assert (type == T_BOOL);
90         return value.b_val;
91     }
92 
as_double()93     double as_double() {
94         g_assert (type == T_DOUBLE);
95         return value.d_val;
96     }
97 
as_vector()98     std::vector<double>* as_vector() {
99         g_assert (type == T_VECT_DOUBLE);
100         return value.vt_val;
101     }
102 
as_charptr()103     char* as_charptr() {
104         g_assert (type == T_CHARPTR);
105         return value.cptr_val;
106     }
107 };
108 
109 class AttrWidget
110 {
111 public:
AttrWidget(const SPAttr a,unsigned int value)112     AttrWidget(const SPAttr a, unsigned int value)
113         : _attr(a),
114           _default(value)
115     {}
116 
AttrWidget(const SPAttr a,double value)117     AttrWidget(const SPAttr a, double value)
118         : _attr(a),
119           _default(value)
120     {}
121 
AttrWidget(const SPAttr a,bool value)122     AttrWidget(const SPAttr a, bool value)
123         : _attr(a),
124           _default(value)
125     {}
126 
AttrWidget(const SPAttr a,char * value)127     AttrWidget(const SPAttr a, char* value)
128         : _attr(a),
129           _default(value)
130     {}
131 
AttrWidget(const SPAttr a)132     AttrWidget(const SPAttr a)
133         : _attr(a),
134           _default()
135     {}
136 
137     virtual ~AttrWidget()
138     = default;
139 
140     virtual Glib::ustring get_as_attribute() const = 0;
141     virtual void set_from_attribute(SPObject*) = 0;
142 
get_attribute()143     SPAttr get_attribute() const
144     {
145         return _attr;
146     }
147 
signal_attr_changed()148     sigc::signal<void>& signal_attr_changed()
149     {
150         return _signal;
151     }
152 protected:
get_default()153     DefaultValueHolder* get_default() { return &_default; }
attribute_value(SPObject * o)154     const gchar* attribute_value(SPObject* o) const
155     {
156         const gchar* name = (const gchar*)sp_attribute_name(_attr);
157         if(name && o) {
158             const gchar* val = o->getRepr()->attribute(name);
159             return val;
160         }
161         return nullptr;
162     }
163 
164 private:
165     const SPAttr _attr;
166     DefaultValueHolder _default;
167     sigc::signal<void> _signal;
168 };
169 
170 }
171 }
172 }
173 
174 #endif
175 
176 /*
177   Local Variables:
178   mode:c++
179   c-file-style:"stroustrup"
180   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
181   indent-tabs-mode:nil
182   fill-column:99
183   End:
184 */
185 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
186