Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_join.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_join.cpp
3 **      \brief Implementation of the "Join" 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_join.h"
34 #include "valuenode_const.h"
35 #include "valuenode_dynamiclist.h"
36 #include "canvas.h"
37 #include "general.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 ValueNode_Join::ValueNode_Join(const ValueBase &value):
56         LinkableValueNode(value.get_type())
57 {
58         Vocab ret(get_children_vocab());
59         set_children_vocab(ret);
60         switch(value.get_type())
61         {
62         case ValueBase::TYPE_STRING:
63         {
64                 vector<ValueBase> v(1, value.get(String()));
65
66                 // "insert item (smart)" inserts before the selected entry, making it hard to append to the end
67                 // add an extra element at the end to allow the easy insertion of text after the given value's string
68                 v.push_back("...");
69
70                 set_link("strings",ValueNode_DynamicList::create_from(v));
71                 set_link("before",ValueNode_Const::create(String("")));
72                 set_link("separator",ValueNode_Const::create(String(" ")));
73                 set_link("after",ValueNode_Const::create(String("")));
74                 break;
75         }
76         default:
77                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
78         }
79 }
80
81 LinkableValueNode*
82 ValueNode_Join::create_new()const
83 {
84         return new ValueNode_Join(get_type());
85 }
86
87 ValueNode_Join*
88 ValueNode_Join::create(const ValueBase &x)
89 {
90         return new ValueNode_Join(x);
91 }
92
93 ValueNode_Join::~ValueNode_Join()
94 {
95         unlink_all();
96 }
97
98 ValueBase
99 ValueNode_Join::operator()(Time t)const
100 {
101         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
102                 printf("%s:%d operator()\n", __FILE__, __LINE__);
103
104         const std::vector<ValueBase> strings((*strings_)(t).get_list());
105         const String before((*before_)(t).get(String()));
106         const String separator((*separator_)(t).get(String()));
107         const String after((*after_)(t).get(String()));
108
109         switch (get_type())
110         {
111         case ValueBase::TYPE_STRING:
112         {
113                 bool first = true;
114                 String ret(before);
115                 for (std::vector<ValueBase>::const_iterator iter = strings.begin(); iter != strings.end(); iter++)
116                 {
117                         if (first)
118                                 first = false;
119                         else
120                                 ret += separator;
121                         ret += iter->get(String());
122                 }
123                 ret += after;
124                 return ret;
125         }
126         default:
127                 break;
128         }
129
130         assert(0);
131         return ValueBase();
132 }
133
134 String
135 ValueNode_Join::get_name()const
136 {
137         return "join";
138 }
139
140 String
141 ValueNode_Join::get_local_name()const
142 {
143         return _("Joined List");
144 }
145
146 bool
147 ValueNode_Join::set_link_vfunc(int i,ValueNode::Handle value)
148 {
149         assert(i>=0 && i<link_count());
150
151         switch(i)
152         {
153         case 0: CHECK_TYPE_AND_SET_VALUE(strings_, ValueBase::TYPE_LIST);
154         case 1: CHECK_TYPE_AND_SET_VALUE(before_, ValueBase::TYPE_STRING);
155         case 2: CHECK_TYPE_AND_SET_VALUE(separator_, ValueBase::TYPE_STRING);
156         case 3: CHECK_TYPE_AND_SET_VALUE(after_, ValueBase::TYPE_STRING);
157         }
158         return false;
159 }
160
161 ValueNode::LooseHandle
162 ValueNode_Join::get_link_vfunc(int i)const
163 {
164         assert(i>=0 && i<link_count());
165
166         switch(i)
167         {
168         case 0: return strings_;
169         case 1: return before_;
170         case 2: return separator_;
171         case 3: return after_;
172         }
173
174         return 0;
175 }
176
177 int
178 ValueNode_Join::link_count()const
179 {
180         return 4;
181 }
182
183 String
184 ValueNode_Join::link_name(int i)const
185 {
186         assert(i>=0 && i<link_count());
187
188         switch(i)
189         {
190                 case 0: return "strings";
191                 case 1: return "before";
192                 case 2: return "separator";
193                 case 3: return "after";
194         }
195         return String();
196 }
197
198 String
199 ValueNode_Join::link_local_name(int i)const
200 {
201         assert(i>=0 && i<link_count());
202
203         switch(i)
204         {
205                 case 0: return _("Strings");
206                 case 1: return _("Before");
207                 case 2: return _("Separator");
208                 case 3: return _("After");
209         }
210         return String();
211 }
212
213 int
214 ValueNode_Join::get_link_index_from_name(const String &name)const
215 {
216         if (name=="strings") return 0;
217         if (name=="before") return 1;
218         if (name=="separator") return 2;
219         if (name=="after") return 3;
220
221         throw Exception::BadLinkName(name);
222 }
223
224 bool
225 ValueNode_Join::check_type(ValueBase::Type type)
226 {
227         return
228                 type==ValueBase::TYPE_STRING;
229 }
230
231 LinkableValueNode::Vocab
232 ValueNode_Join::get_children_vocab_vfunc()const
233 {
234         if(children_vocab.size())
235                 return children_vocab;
236
237         LinkableValueNode::Vocab ret;
238
239         ret.push_back(ParamDesc(ValueBase(),"strings")
240                 .set_local_name(_("Strings"))
241                 .set_description(_("The List of strings to join"))
242         );
243
244         ret.push_back(ParamDesc(ValueBase(),"before")
245                 .set_local_name(_("Before"))
246                 .set_description(_("The string to place before the joined strings"))
247         );
248
249         ret.push_back(ParamDesc(ValueBase(),"separator")
250                 .set_local_name(_("Separator"))
251                 .set_description(_("The string to place between each string joined"))
252         );
253
254         ret.push_back(ParamDesc(ValueBase(),"after")
255                 .set_local_name(_("After"))
256                 .set_description(_("The string to place after the joined strings"))
257         );
258
259         return ret;
260 }