Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / 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         DCAST_HACK_ENABLE();
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         const std::vector<ValueBase> strings((*strings_)(t).get_list());
102         const String before((*before_)(t).get(String()));
103         const String separator((*separator_)(t).get(String()));
104         const String after((*after_)(t).get(String()));
105
106         switch (get_type())
107         {
108         case ValueBase::TYPE_STRING:
109         {
110                 bool first = true;
111                 String ret(before);
112                 for (std::vector<ValueBase>::const_iterator iter = strings.begin(); iter != strings.end(); iter++)
113                 {
114                         if (first)
115                                 first = false;
116                         else
117                                 ret += separator;
118                         ret += iter->get(String());
119                 }
120                 ret += after;
121                 return ret;
122         }
123         default:
124                 break;
125         }
126
127         assert(0);
128         return ValueBase();
129 }
130
131 String
132 ValueNode_Join::get_name()const
133 {
134         return "join";
135 }
136
137 String
138 ValueNode_Join::get_local_name()const
139 {
140         return _("Joined List");
141 }
142
143 bool
144 ValueNode_Join::set_link_vfunc(int i,ValueNode::Handle value)
145 {
146         assert(i>=0 && i<link_count());
147
148         switch(i)
149         {
150         case 0: CHECK_TYPE_AND_SET_VALUE(strings_, ValueBase::TYPE_LIST);
151         case 1: CHECK_TYPE_AND_SET_VALUE(before_, ValueBase::TYPE_STRING);
152         case 2: CHECK_TYPE_AND_SET_VALUE(separator_, ValueBase::TYPE_STRING);
153         case 3: CHECK_TYPE_AND_SET_VALUE(after_, ValueBase::TYPE_STRING);
154         }
155         return false;
156 }
157
158 ValueNode::LooseHandle
159 ValueNode_Join::get_link_vfunc(int i)const
160 {
161         assert(i>=0 && i<link_count());
162
163         switch(i)
164         {
165         case 0: return strings_;
166         case 1: return before_;
167         case 2: return separator_;
168         case 3: return after_;
169         }
170
171         return 0;
172 }
173
174 int
175 ValueNode_Join::link_count()const
176 {
177         return 4;
178 }
179
180 String
181 ValueNode_Join::link_name(int i)const
182 {
183         assert(i>=0 && i<link_count());
184
185         switch(i)
186         {
187                 case 0: return "strings";
188                 case 1: return "before";
189                 case 2: return "separator";
190                 case 3: return "after";
191         }
192         return String();
193 }
194
195 String
196 ValueNode_Join::link_local_name(int i)const
197 {
198         assert(i>=0 && i<link_count());
199
200         switch(i)
201         {
202                 case 0: return _("Strings");
203                 case 1: return _("Before");
204                 case 2: return _("Separator");
205                 case 3: return _("After");
206         }
207         return String();
208 }
209
210 int
211 ValueNode_Join::get_link_index_from_name(const String &name)const
212 {
213         if (name=="strings") return 0;
214         if (name=="before") return 1;
215         if (name=="separator") return 2;
216         if (name=="after") return 3;
217
218         throw Exception::BadLinkName(name);
219 }
220
221 bool
222 ValueNode_Join::check_type(ValueBase::Type type)
223 {
224         return
225                 type==ValueBase::TYPE_STRING;
226 }