Xcos MVC: add a test and use unnamed functions parameters
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / TextAdapter.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 #include <string>
14 #include <vector>
15 #include <memory>
16
17 #include "double.hxx"
18 #include "string.hxx"
19 #include "mlist.hxx"
20 #include "internal.hxx"
21
22 #include "Controller.hxx"
23 #include "TextAdapter.hxx"
24
25 extern "C" {
26 #include "sci_malloc.h"
27 #include "charEncoding.h"
28 }
29
30 namespace org_scilab_modules_scicos
31 {
32 namespace view_scilab
33 {
34 namespace
35 {
36
37 const std::wstring Graphics(L"graphics");
38 const std::wstring orig(L"orig");
39 const std::wstring sz(L"sz");
40 const std::wstring exprs(L"exprs");
41
42 const std::wstring Model(L"model");
43 const std::wstring sim(L"sim");
44 const std::wstring rpar(L"rpar");
45 const std::wstring ipar(L"ipar");
46
47 struct graphics
48 {
49     static types::InternalType* get(const TextAdapter& adaptor, const Controller& controller)
50     {
51         ScicosID adaptee = adaptor.getAdaptee()->id();
52
53         types::MList* o = new types::MList();
54         types::String* MListFields = new types::String(1, 4);
55         MListFields->set(0, Graphics.c_str());
56         MListFields->set(1, orig.c_str());
57         MListFields->set(2, sz.c_str());
58         MListFields->set(3, exprs.c_str());
59         o->set(0, MListFields);
60
61         // orig and sz
62         std::vector<double> geom;
63         controller.getObjectProperty(adaptee, ANNOTATION, GEOMETRY, geom);
64
65         double* dataOrig;
66         double* dataSz;
67         types::Double* origField = new types::Double(1, 2, &dataOrig);
68         types::Double* szField   = new types::Double(1, 2, &dataSz);
69         dataOrig[0] = geom[0];
70         dataOrig[1] = geom[1];
71         dataSz[0]   = geom[2];
72         dataSz[1]   = geom[3];
73         o->set(1, origField);
74         o->set(2, szField);
75
76         // exprs
77         std::vector<std::string> Exprs (3);
78         controller.getObjectProperty(adaptee, ANNOTATION, DESCRIPTION, Exprs[0]);
79         controller.getObjectProperty(adaptee, ANNOTATION, FONT, Exprs[1]);
80         controller.getObjectProperty(adaptee, ANNOTATION, FONT_SIZE, Exprs[2]);
81
82         types::String* exprsField = new types::String(3, 1);
83         for (int i = 0; i < (int) Exprs.size(); ++i)
84         {
85             exprsField->set(i, Exprs[i].data());
86         }
87         o->set(3, exprsField);
88
89         return o;
90     }
91
92     static bool set(TextAdapter& adaptor, types::InternalType* v, Controller& controller)
93     {
94         if (v->getType() != types::InternalType::ScilabMList)
95         {
96             return false;
97         }
98
99         types::MList* current = v->getAs<types::MList>();
100         if (current->getSize() < 4)
101         {
102             return false;
103         }
104
105         types::InternalType* currentField;
106         types::Double* currentFieldDouble;
107         types::String* currentFieldString;
108
109         ScicosID adaptee = adaptor.getAdaptee()->id();
110
111         // orig
112         if ((currentField = current->getField(orig.c_str())) == NULL)
113         {
114             return false;
115         }
116         if (currentField->getType() != types::InternalType::ScilabDouble)
117         {
118             return false;
119         }
120         currentFieldDouble = currentField->getAs<types::Double>();
121         if (currentFieldDouble->getRows() != 1 || currentFieldDouble->getCols() != 2)
122         {
123             return false;
124         }
125         std::vector<double> origField;
126         controller.getObjectProperty(adaptee, ANNOTATION, GEOMETRY, origField);
127         origField[0] = currentFieldDouble->get(0);
128         origField[1] = currentFieldDouble->get(1);
129         controller.setObjectProperty(adaptee, ANNOTATION, GEOMETRY, origField);
130
131         // sz
132         if ((currentField = current->getField(sz.c_str())) == NULL)
133         {
134             return false;
135         }
136         if (currentField->getType() != types::InternalType::ScilabDouble)
137         {
138             return false;
139         }
140         currentFieldDouble = currentField->getAs<types::Double>();
141         if (currentFieldDouble->getRows() != 1 || currentFieldDouble->getCols() != 2)
142         {
143             return false;
144         }
145         std::vector<double> szField;
146         controller.getObjectProperty(adaptee, ANNOTATION, GEOMETRY, szField);
147         szField[2] = currentFieldDouble->get(0);
148         szField[3] = currentFieldDouble->get(1);
149         controller.setObjectProperty(adaptee, ANNOTATION, GEOMETRY, szField);
150
151         // exprs
152         if ((currentField = current->getField(exprs.c_str())) == NULL)
153         {
154             return false;
155         }
156         if (currentField->getType() == types::InternalType::ScilabString)
157         {
158             currentFieldString = currentField->getAs<types::String>();
159             if (currentFieldString->getCols() != 1 || currentFieldString->getSize() != 3)
160             {
161                 return false;
162             }
163
164             std::vector<std::string> exprsField (3);
165             for (int i = 0; i < (int) exprsField.size(); ++i)
166             {
167                 char* c_str = wide_string_to_UTF8(currentFieldString->get(i));
168                 exprsField[i] = std::string(c_str);
169                 FREE(c_str);
170             }
171             controller.setObjectProperty(adaptee, ANNOTATION, DESCRIPTION, exprsField[0]);
172             controller.setObjectProperty(adaptee, ANNOTATION, FONT, exprsField[1]);
173             controller.setObjectProperty(adaptee, ANNOTATION, FONT_SIZE, exprsField[2]);
174             return true;
175         }
176         else if (currentField->getType() == types::InternalType::ScilabDouble)
177         {
178             currentFieldDouble = currentField->getAs<types::Double>();
179             if (currentFieldDouble->getSize() != 0)
180             {
181                 return false;
182             }
183
184             std::vector<std::string> exprsField (3);
185             controller.setObjectProperty(adaptee, ANNOTATION, DESCRIPTION, exprsField[0]);
186             controller.setObjectProperty(adaptee, ANNOTATION, FONT, exprsField[1]);
187             controller.setObjectProperty(adaptee, ANNOTATION, FONT_SIZE, exprsField[2]);
188             return true;
189         }
190
191         return false;
192     }
193 };
194
195 struct model
196 {
197
198     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
199     {
200         // Return an empty "model"-typed mlist because this field isn't used.
201         types::MList* o = new types::MList();
202         types::String* MListFields = new types::String(1, 4);
203
204         MListFields->set(0, Model.c_str());
205         MListFields->set(1, sim.c_str());
206         MListFields->set(2, rpar.c_str());
207         MListFields->set(3, ipar.c_str());
208
209         o->set(0, MListFields);
210         // 'sim' field needs to be defined for the console display.
211         o->set(1, types::Double::Empty());
212         o->set(2, types::Double::Empty());
213         o->set(3, types::Double::Empty());
214         return o;
215     }
216
217     static bool set(TextAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*controller*/)
218     {
219         // Everything should be right as the properties mapped using this adapter do not perform anything
220         return true;
221     }
222 };
223
224 struct dummy_property
225 {
226
227     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
228     {
229         // Return an empty matrix because this field isn't used.
230         return types::Double::Empty();
231     }
232
233     static bool set(TextAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*controller*/)
234     {
235         // everything should be right as the properties mapped using this adapter do not perform anything
236         return true;
237     }
238 };
239
240 } /* namespace */
241
242 template<> property<TextAdapter>::props_t property<TextAdapter>::fields = property<TextAdapter>::props_t();
243
244 TextAdapter::TextAdapter(std::shared_ptr<org_scilab_modules_scicos::model::Annotation> adaptee) :
245     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(adaptee)
246 {
247     if (property<TextAdapter>::properties_have_not_been_set())
248     {
249         property<TextAdapter>::fields.reserve(4);
250         property<TextAdapter>::add_property(Graphics, &graphics::get, &graphics::set);
251         property<TextAdapter>::add_property(Model, &model::get, &model::set);
252         property<TextAdapter>::add_property(L"void", &dummy_property::get, &dummy_property::set);
253         property<TextAdapter>::add_property(L"gui", &dummy_property::get, &dummy_property::set);
254     }
255 }
256
257 TextAdapter::TextAdapter(const TextAdapter& adapter) :
258     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(adapter)
259 {
260 }
261
262 TextAdapter::~TextAdapter()
263 {
264 }
265
266 std::wstring TextAdapter::getTypeStr()
267 {
268     return getSharedTypeStr();
269 }
270 std::wstring TextAdapter::getShortTypeStr()
271 {
272     return getSharedTypeStr();
273 }
274
275 } /* namespace view_scilab */
276 } /* namespace org_scilab_modules_scicos */