Merge remote-tracking branch 'origin/master' into windows
[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  * 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::string Graphics("graphics");
41 const std::string orig("orig");
42 const std::string sz("sz");
43 const std::string exprs("exprs");
44 const std::string style("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                 exprsField[i] = currentFieldString->get(i);
184             }
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         }
189         else if (currentField->getType() == types::InternalType::ScilabDouble)
190         {
191             currentFieldDouble = currentField->getAs<types::Double>();
192             if (currentFieldDouble->getSize() != 0)
193             {
194                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "exprs");
195                 return false;
196             }
197
198             std::vector<std::string> exprsField (3);
199             controller.setObjectProperty(adaptee, ANNOTATION, DESCRIPTION, exprsField[0]);
200             controller.setObjectProperty(adaptee, ANNOTATION, FONT, exprsField[1]);
201             controller.setObjectProperty(adaptee, ANNOTATION, FONT_SIZE, exprsField[2]);
202         }
203         else
204         {
205             return false;
206         }
207
208         // style, if it is present
209         if ((current->getSize() >= 5) && ((currentField = current->getField(style.c_str())) != nullptr))
210         {
211             if (currentField->getType() == types::InternalType::ScilabDouble)
212             {
213                 currentFieldDouble = currentField->getAs<types::Double>();
214                 if (currentFieldDouble->getSize() != 0)
215                 {
216                     get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "style");
217                     return false;
218                 }
219                 std::string styleField;
220                 controller.setObjectProperty(adaptee, ANNOTATION, STYLE, styleField);
221                 return true;
222             }
223
224             if (currentField->getType() != types::InternalType::ScilabString)
225             {
226                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: String matrix expected.\n"), "graphics", "style");
227                 return false;
228             }
229             currentFieldString = currentField->getAs<types::String>();
230             if (currentFieldString->getSize() != 1)
231             {
232                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimensions for field %s.%s: %d-by-%d expected.\n"), "graphics", "style", 1, 1);
233                 return false;
234             }
235
236             std::string styleField (currentFieldString->get(0));
237             controller.setObjectProperty(adaptee, ANNOTATION, STYLE, styleField);
238         }
239
240         return true;
241     }
242 };
243
244 struct dummy_property
245 {
246
247     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
248     {
249         // Return an empty matrix because this field isn't used.
250         return types::Double::Empty();
251     }
252
253     static bool set(TextAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*controller*/)
254     {
255         // everything should be right as the properties mapped using this adapter do not perform anything
256         return true;
257     }
258 };
259
260 struct gui
261 {
262
263     static types::InternalType* get(const TextAdapter& /*adaptor*/, const Controller& /*controller*/)
264     {
265         return new types::String("TEXT_f");
266     }
267 };
268
269 } /* namespace */
270
271 template<> property<TextAdapter>::props_t property<TextAdapter>::fields = property<TextAdapter>::props_t();
272
273 TextAdapter::TextAdapter(const Controller& c, org_scilab_modules_scicos::model::Annotation* adaptee) :
274     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(c, adaptee)
275 {
276     if (property<TextAdapter>::properties_have_not_been_set())
277     {
278         property<TextAdapter>::fields.reserve(4);
279         property<TextAdapter>::add_property(Graphics, &graphics::get, &graphics::set);
280         property<TextAdapter>::add_property("model", &dummy_property::get, &dummy_property::set);
281         property<TextAdapter>::add_property("void", &dummy_property::get, &dummy_property::set);
282         property<TextAdapter>::add_property("gui", &gui::get, &dummy_property::set);
283     }
284 }
285
286 TextAdapter::TextAdapter(const TextAdapter& adapter) :
287     BaseAdapter<TextAdapter, org_scilab_modules_scicos::model::Annotation>(adapter)
288 {
289 }
290
291 TextAdapter::~TextAdapter()
292 {
293 }
294
295 std::string TextAdapter::getTypeStr()
296 {
297     return getSharedTypeStr();
298 }
299 std::string TextAdapter::getShortTypeStr()
300 {
301     return getSharedTypeStr();
302 }
303
304 } /* namespace view_scilab */
305 } /* namespace org_scilab_modules_scicos */