Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_realstring.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_realstring.cpp
3 **      \brief Implementation of the "RealString" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenode_realstring.h"
34 #include "valuenode_const.h"
35 #include "canvas.h"
36 #include "general.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_RealString::ValueNode_RealString(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         switch(value.get_type())
58         {
59         case ValueBase::TYPE_STRING:
60                 set_link("real",ValueNode_Const::create(Real(0)));
61                 set_link("width",ValueNode_Const::create(int(0)));
62                 set_link("precision",ValueNode_Const::create(int(3)));
63                 set_link("zero_pad",ValueNode_Const::create(bool(false)));
64                 break;
65         default:
66                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
67         }
68 }
69
70 LinkableValueNode*
71 ValueNode_RealString::create_new()const
72 {
73         return new ValueNode_RealString(get_type());
74 }
75
76 ValueNode_RealString*
77 ValueNode_RealString::create(const ValueBase &x)
78 {
79         return new ValueNode_RealString(x);
80 }
81
82 ValueNode_RealString::~ValueNode_RealString()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_RealString::operator()(Time t)const
89 {
90         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
91                 printf("%s:%d operator()\n", __FILE__, __LINE__);
92
93         Real real((*real_)(t).get(Real()));
94         int width((*width_)(t).get(int()));
95         int precision((*precision_)(t).get(int()));
96         int zero_pad((*zero_pad_)(t).get(bool()));
97
98         switch (get_type())
99         {
100         case ValueBase::TYPE_STRING:
101                 return strprintf(strprintf("%%%s%d.%df",
102                                                                    zero_pad ? "0" : "",
103                                                                    width,
104                                                                    precision).c_str(), real);
105         default:
106                 break;
107         }
108
109         assert(0);
110         return ValueBase();
111 }
112
113 String
114 ValueNode_RealString::get_name()const
115 {
116         return "realstring";
117 }
118
119 String
120 ValueNode_RealString::get_local_name()const
121 {
122         return _("Real String");
123 }
124
125 bool
126 ValueNode_RealString::set_link_vfunc(int i,ValueNode::Handle value)
127 {
128         assert(i>=0 && i<link_count());
129
130         switch(i)
131         {
132         case 0: CHECK_TYPE_AND_SET_VALUE(real_, ValueBase::TYPE_REAL);
133         case 1: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_INTEGER);
134         case 2: CHECK_TYPE_AND_SET_VALUE(precision_, ValueBase::TYPE_INTEGER);
135         case 3: CHECK_TYPE_AND_SET_VALUE(zero_pad_, ValueBase::TYPE_BOOL);
136         }
137         return false;
138 }
139
140 ValueNode::LooseHandle
141 ValueNode_RealString::get_link_vfunc(int i)const
142 {
143         assert(i>=0 && i<link_count());
144
145         switch(i)
146         {
147         case 0: return real_;
148         case 1: return width_;
149         case 2: return precision_;
150         case 3: return zero_pad_;
151         }
152
153         return 0;
154 }
155
156 int
157 ValueNode_RealString::link_count()const
158 {
159         return 4;
160 }
161
162 String
163 ValueNode_RealString::link_name(int i)const
164 {
165         assert(i>=0 && i<link_count());
166
167         switch(i)
168         {
169                 case 0: return "real";
170                 case 1: return "width";
171                 case 2: return "precision";
172                 case 3: return "zero_pad";
173         }
174         return String();
175 }
176
177 String
178 ValueNode_RealString::link_local_name(int i)const
179 {
180         assert(i>=0 && i<link_count());
181
182         switch(i)
183         {
184                 case 0: return _("Real");
185                 case 1: return _("Width");
186                 case 2: return _("Precision");
187                 case 3: return _("Zero Padded");
188         }
189         return String();
190 }
191
192 int
193 ValueNode_RealString::get_link_index_from_name(const String &name)const
194 {
195         if (name=="real") return 0;
196         if (name=="width") return 1;
197         if (name=="precision") return 2;
198         if (name=="zero_pad") return 3;
199
200         throw Exception::BadLinkName(name);
201 }
202
203 bool
204 ValueNode_RealString::check_type(ValueBase::Type type)
205 {
206         return
207                 type==ValueBase::TYPE_STRING;
208 }
209
210 LinkableValueNode::Vocab
211 ValueNode_RealString::get_children_vocab_vfunc()const
212 {
213         LinkableValueNode::Vocab ret;
214
215         ret.push_back(ParamDesc(ValueBase(),"real")
216                 .set_local_name(_("Real"))
217                 .set_description(_("Value to convert to string"))
218         );
219
220         ret.push_back(ParamDesc(ValueBase(),"width")
221                 .set_local_name(_("Width"))
222                 .set_description(_("Width of the string"))
223         );
224
225         ret.push_back(ParamDesc(ValueBase(),"precision")
226                 .set_local_name(_("Precision"))
227                 .set_description(_("Number of decimal places"))
228         );
229
230         ret.push_back(ParamDesc(ValueBase(),"zero_pad")
231                 .set_local_name(_("Zero Padded"))
232                 .set_description(_("When checked, the string is left filled with zeros to match the width"))
233         );
234
235
236         return ret;
237 }