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