Code

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