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