Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_intstring.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_intstring.cpp
3 **      \brief Implementation of the "IntString" 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_intstring.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_IntString::ValueNode_IntString(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         switch(value.get_type())
60         {
61         case ValueBase::TYPE_STRING:
62                 set_link("int",ValueNode_Const::create(int(0)));
63                 set_link("width",ValueNode_Const::create(int(0)));
64                 set_link("zero_pad",ValueNode_Const::create(bool(false)));
65                 break;
66         default:
67                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
68         }
69 }
70
71 LinkableValueNode*
72 ValueNode_IntString::create_new()const
73 {
74         return new ValueNode_IntString(get_type());
75 }
76
77 ValueNode_IntString*
78 ValueNode_IntString::create(const ValueBase &x)
79 {
80         return new ValueNode_IntString(x);
81 }
82
83 ValueNode_IntString::~ValueNode_IntString()
84 {
85         unlink_all();
86 }
87
88 ValueBase
89 ValueNode_IntString::operator()(Time t)const
90 {
91         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
92                 printf("%s:%d operator()\n", __FILE__, __LINE__);
93
94         int integer((*int_)(t).get(int()));
95         int width((*width_)(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%dd",
102                                                                    zero_pad ? "0" : "",
103                                                                    width).c_str(), integer);
104         default:
105                 break;
106         }
107
108         assert(0);
109         return ValueBase();
110 }
111
112 String
113 ValueNode_IntString::get_name()const
114 {
115         return "intstring";
116 }
117
118 String
119 ValueNode_IntString::get_local_name()const
120 {
121         return _("Int String");
122 }
123
124 bool
125 ValueNode_IntString::set_link_vfunc(int i,ValueNode::Handle value)
126 {
127         assert(i>=0 && i<link_count());
128
129         switch(i)
130         {
131         case 0: CHECK_TYPE_AND_SET_VALUE(int_, ValueBase::TYPE_INTEGER);
132         case 1: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_INTEGER);
133         case 2: CHECK_TYPE_AND_SET_VALUE(zero_pad_, ValueBase::TYPE_BOOL);
134         }
135         return false;
136 }
137
138 ValueNode::LooseHandle
139 ValueNode_IntString::get_link_vfunc(int i)const
140 {
141         assert(i>=0 && i<link_count());
142
143         switch(i)
144         {
145         case 0: return int_;
146         case 1: return width_;
147         case 2: return zero_pad_;
148         }
149
150         return 0;
151 }
152
153 int
154 ValueNode_IntString::link_count()const
155 {
156         return 3;
157 }
158
159 String
160 ValueNode_IntString::link_name(int i)const
161 {
162         assert(i>=0 && i<link_count());
163
164         switch(i)
165         {
166                 case 0: return "int";
167                 case 1: return "width";
168                 case 2: return "zero_pad";
169         }
170         return String();
171 }
172
173 String
174 ValueNode_IntString::link_local_name(int i)const
175 {
176         assert(i>=0 && i<link_count());
177
178         switch(i)
179         {
180                 case 0: return _("Int");
181                 case 1: return _("Width");
182                 case 2: return _("Zero Padded");
183         }
184         return String();
185 }
186
187 int
188 ValueNode_IntString::get_link_index_from_name(const String &name)const
189 {
190         if (name=="int") return 0;
191         if (name=="width") return 1;
192         if (name=="zero_pad") return 2;
193
194         throw Exception::BadLinkName(name);
195 }
196
197 bool
198 ValueNode_IntString::check_type(ValueBase::Type type)
199 {
200         return
201                 type==ValueBase::TYPE_STRING;
202 }
203
204 LinkableValueNode::Vocab
205 ValueNode_IntString::get_children_vocab_vfunc()const
206 {
207         if(children_vocab.size())
208                 return children_vocab;
209
210         LinkableValueNode::Vocab ret;
211
212         ret.push_back(ParamDesc(ValueBase(),"int")
213                 .set_local_name(_("Int"))
214                 .set_description(_("Value to convert to string"))
215         );
216
217         ret.push_back(ParamDesc(ValueBase(),"width")
218                 .set_local_name(_("Width"))
219                 .set_description(_("Width of the string"))
220         );
221
222         ret.push_back(ParamDesc(ValueBase(),"zero_pad")
223                 .set_local_name(_("Zero Padded"))
224                 .set_description(_("When checked, the string is left filled with zeros to match the width"))
225         );
226
227         return ret;
228 }