Update CHANGES.md before the release
[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-2016 - Scilab Enterprises - Clement DAVID
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include <string>
17 #include <vector>
18
19 #include "double.hxx"
20 #include "string.hxx"
21 #include "mlist.hxx"
22 #include "internal.hxx"
23
24 #include "utilities.hxx"
25 #include "Controller.hxx"
26 #include "TextAdapter.hxx"
27
28 extern "C" {
29 #include "sci_malloc.h"
30 #include "charEncoding.h"
31 }
32
33 namespace org_scilab_modules_scicos
34 {
35 namespace view_scilab
36 {
37 namespace
38 {
39
40 const std::wstring Graphics(L"graphics");
41 const std::wstring orig(L"orig");
42 const std::wstring sz(L"sz");
43 const std::wstring exprs(L"exprs");
44 const std::wstring style(L"style");
45
46 struct graphics
47 {
48     static types::InternalType* get(const TextAdapter& adaptor, const Controller& controller)
49     {
50         ScicosID adaptee = adaptor.getAdaptee()->id();
51
52         types::MList* o = new types::MList();
53         types::String* MListFields = new types::String(1, 5);
54         MListFields->set(0, Graphics.c_str());
55         MListFields->set(1, orig.c_str());
56         MListFields->set(2, sz.c_str());
57         MListFields->set(3, exprs.c_str());
58         MListFields->set(4, style.c_str());
59         o->append(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->append(origField);
74         o->append(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->append(exprsField);
88
89         // style
90         std::string Style;
91         controller.getObjectProperty(adaptee, ANNOTATION, STYLE, Style);
92         types::String* styleField = new types::String(Style.data());
93         o->append(styleField);
94
95         return o;
96     }
97
98     static bool set(TextAdapter& adaptor, types::InternalType* v, Controller& controller)
99     {
100         if (v->getType() != types::InternalType::ScilabMList)
101         {
102             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s: Mlist expected.\n"), "graphics");
103             return false;
104         }
105
106         types::MList* current = v->getAs<types::MList>();
107         if (current->getSize() < 4)
108         {
109             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong size for field %s: at least %d elements expected.\n"), "graphics", 4);
110             return false;
111         }
112
113         types::InternalType* currentField;
114         types::Double* currentFieldDouble;
115         types::String* currentFieldString;
116
117         ScicosID adaptee = adaptor.getAdaptee()->id();
118
119         // orig
120         if ((currentField = current->getField(orig.c_str())) == nullptr)
121         {
122             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong value for header of field %s: field %s expected.\n"), "graphics", "orig");
123             return false;
124         }
125         if (currentField->getType() != types::InternalType::ScilabDouble)
126         {
127             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: Real matrix expected.\n"), "graphics", "orig");
128             return false;
129         }
130         currentFieldDouble = currentField->getAs<types::Double>();
131         if (currentFieldDouble->getSize() != 2)
132         {
133             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimensions for field %s.%s: %d-by-%d expected.\n"), "graphics", "orig", 1, 2);
134             return false;
135         }
136         std::vector<double> origField;
137         controller.getObjectProperty(adaptee, ANNOTATION, GEOMETRY, origField);
138         origField[0] = currentFieldDouble->get(0);
139         origField[1] = currentFieldDouble->get(1);
140         controller.setObjectProperty(adaptee, ANNOTATION, GEOMETRY, origField);
141
142         // sz
143         if ((currentField = current->getField(sz.c_str())) == nullptr)
144         {
145             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong value for header of field %s: field %s expected.\n"), "graphics", "sz");
146             return false;
147         }
148         if (currentField->getType() != types::InternalType::ScilabDouble)
149         {
150             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: Real matrix expected.\n"), "graphics", "sz");
151             return false;
152         }
153         currentFieldDouble = currentField->getAs<types::Double>();
154         if (currentFieldDouble->getSize() != 2)
155         {
156             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimensions for field %s.%s: %d-by-%d expected.\n"), "graphics", "sz", 1, 2);
157             return false;
158         }
159         std::vector<double> szField;
160         controller.getObjectProperty(adaptee, ANNOTATION, GEOMETRY, szField);
161         szField[2] = currentFieldDouble->get(0);
162         szField[3] = currentFieldDouble->get(1);
163         controller.setObjectProperty(adaptee, ANNOTATION, GEOMETRY, szField);
164
165         // exprs
166         if ((currentField = current->getField(exprs.c_str())) == nullptr)
167         {
168             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong value for header of field %s: field %s expected.\n"), "graphics", "exprs");
169             return false;
170         }
171         if (currentField->getType() == types::InternalType::ScilabString)
172         {
173             currentFieldString = currentField->getAs<types::String>();
174             if (currentFieldString->getSize() != 3)
175             {
176                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimensions for field %s.%s: %d-by-%d expected.\n"), "graphics", "exprs", 1, 3);
177                 return false;
178             }
179
180             std::vector<std::string> exprsField (3);
181             for (int i = 0; i < (int) exprsField.size(); ++i)
182             {
183                 char* c_str = wide_string_to_UTF8(currentFieldString->get(i));
184                 exprsField[i] = std::string(c_str);
185                 FREE(c_str);
186             }
187             controller.setObjectProperty(adaptee, ANNOTATION, DESCRIPTION, exprsField[0]);
188             controller.setObjectProperty(adaptee, ANNOTATION, FONT, exprsField[1]);
189             controller.setObjectProperty(adaptee, ANNOTATION, FONT_SIZE, exprsField[2]);
190         }
191         else if (currentField->getType() == types::InternalType::ScilabDouble)
192         {
193             currentFieldDouble = currentField->getAs<types::Double>();
194             if (currentFieldDouble->getSize() != 0)
195             {
196                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "exprs");
197                 return false;
198             }
199
200             std::vector<std::string> exprsField (3);
201             controller.setObjectProperty(adaptee, ANNOTATION, DESCRIPTION, exprsField[0]);
202             controller.setObjectProperty(adaptee, ANNOTATION, FONT, exprsField[1]);
203             controller.setObjectProperty(adaptee, ANNOTATION, FONT_SIZE, exprsField[2]);
204         }
205         else
206         {
207             return false;
208         }
209
210         // style, if it is present
211         if ((current->getSize() >= 5) && ((currentField = current->getField(style.c_str())) != nullptr))
212         {
213             if (currentField->getType() == types::InternalType::ScilabDouble)
214             {
215                 currentFieldDouble = currentField->getAs<types::Double>();
216                 if (currentFieldDouble->getSize() != 0)
217                 {
218                     get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "style");
219                     return false;
220                 }
221                 std::string styleField;
222                 controller.setObjectProperty(adaptee, ANNOTATION, STYLE, styleField);
223                 return true;
224             }
225
226             if (currentField->getType() != types::InternalType::ScilabString)
227             {
228                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "style");
229                 return false;
230             }
231             currentFieldString = currentField->getAs<types::String>();
232             if (currentFieldString->getSize() != 1)
233             {
234                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimensions for field %s.%s: %d-by-%d expected.\n"), "graphics", "style", 1, 1);
235                 return false;
236             }
237
238             char* c_str = wide_string_to_UTF8(currentFieldString->get(0));
239             std::string styleField (c_str);
240             FREE(c_str);
241             controller.setObjectProperty(adaptee, ANNOTATION, STYLE, styleField);
242         }
243
244         return true;
245     }
246 };
247
248 struct dummy_property
249 {
250
251     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
252     {
253         // Return an empty matrix because this field isn't used.
254         return types::Double::Empty();
255     }
256
257     static bool set(TextAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*controller*/)
258     {
259         // everything should be right as the properties mapped using this adapter do not perform anything
260         return true;
261     }
262 };
263
264 struct gui
265 {
266
267     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
268     {
269         return new types::String("TEXT_f");
270     }
271 };
272
273 } /* namespace */
274
275 #ifndef _MSC_VER
276 template<>
277 #endif
278 property<TextAdapter>::props_t property<TextAdapter>::fields = property<TextAdapter>::props_t();
279 TextAdapter::TextAdapter(const Controller& c, org_scilab_modules_scicos::model::Annotation* adaptee) :
280     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(c, adaptee)
281 {
282     if (property<TextAdapter>::properties_have_not_been_set())
283     {
284         property<TextAdapter>::reserve_properties(4);
285         property<TextAdapter>::add_property(L"graphics", &graphics::get, &graphics::set);
286         property<TextAdapter>::add_property(L"model", &dummy_property::get, &dummy_property::set);
287         property<TextAdapter>::add_property(L"void", &dummy_property::get, &dummy_property::set);
288         property<TextAdapter>::add_property(L"gui", &gui::get, &dummy_property::set);
289         property<TextAdapter>::shrink_to_fit();
290     }
291 }
292
293 TextAdapter::TextAdapter(const TextAdapter& adapter) :
294     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(adapter)
295 {
296 }
297
298 TextAdapter::~TextAdapter()
299 {
300 }
301
302 std::wstring TextAdapter::getTypeStr() const
303 {
304     return getSharedTypeStr();
305 }
306 std::wstring TextAdapter::getShortTypeStr() const
307 {
308     return getSharedTypeStr();
309 }
310
311 } /* namespace view_scilab */
312 } /* namespace org_scilab_modules_scicos */