Xcos MVC: fix the adaptor-local parameters getters
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / BlockAdapter.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
15 #include "internal.hxx"
16 #include "list.hxx"
17 #include "mlist.hxx"
18 #include "string.hxx"
19 #include "types.hxx"
20 #include "user.hxx"
21
22 #include "Controller.hxx"
23 #include "model/Block.hxx"
24 #include "BlockAdapter.hxx"
25 #include "GraphicsAdapter.hxx"
26 #include "ModelAdapter.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 struct graphics
41 {
42     static types::InternalType* get(const BlockAdapter& adaptor, const Controller& controller)
43     {
44         GraphicsAdapter localAdaptor = GraphicsAdapter(adaptor.getAdaptee());
45         return localAdaptor.getAsTList(new types::MList(), controller);
46     }
47
48     static bool set(BlockAdapter& adaptor, types::InternalType* v, Controller& controller)
49     {
50         GraphicsAdapter localAdaptor = GraphicsAdapter(adaptor.getAdaptee());
51         return localAdaptor.setAsTList(v, controller);
52     }
53 };
54
55 struct model
56 {
57     static types::InternalType* get(const BlockAdapter& adaptor, const Controller& controller)
58     {
59         ModelAdapter localAdaptor = ModelAdapter(adaptor.getAdaptee());
60         return localAdaptor.getAsTList(new types::MList(), controller);
61     }
62
63     static bool set(BlockAdapter& adaptor, types::InternalType* v, Controller& controller)
64     {
65         ModelAdapter localAdaptor = ModelAdapter(adaptor.getAdaptee());
66         return localAdaptor.setAsTList(v, controller);
67     }
68 };
69
70 struct gui
71 {
72     static types::InternalType* get(const BlockAdapter& adaptor, const Controller& controller)
73     {
74         std::string interface;
75         org_scilab_modules_scicos::model::Block* adaptee = adaptor.getAdaptee();
76         controller.getObjectProperty(adaptee->id(), adaptee->kind(), INTERFACE_FUNCTION, interface);
77
78         return new types::String(interface.data());
79     }
80
81     static bool set(BlockAdapter& adaptor, types::InternalType* v, Controller& controller)
82     {
83         if (v->getType() != types::InternalType::ScilabString)
84         {
85             return false;
86         }
87
88         types::String* current = v->getAs<types::String>();
89         if (current->getRows() != 1 || current->getCols() != 1)
90         {
91             return false;
92         }
93
94         wchar_t* w_name = current->get(0);
95         char* name = wide_string_to_UTF8(w_name);
96         std::string stName(name);
97         FREE(name);
98
99         org_scilab_modules_scicos::model::Block* adaptee = adaptor.getAdaptee();
100         controller.setObjectProperty(adaptee->id(), adaptee->kind(), INTERFACE_FUNCTION, stName);
101         return true;
102     }
103 };
104
105 struct doc
106 {
107     static types::InternalType* get(const BlockAdapter& adaptor, const Controller& controller)
108     {
109         // silent unused parameter warnings
110         (void) controller;
111
112         return adaptor.getDocContent();
113     }
114
115     static bool set(BlockAdapter& adaptor, types::InternalType* v, Controller& controller)
116     {
117         // silent unused parameter warnings
118         (void) controller;
119
120         adaptor.setDocContent(v->clone());
121         return true;
122     }
123 };
124
125 } /* namespace */
126
127 template<> property<BlockAdapter>::props_t property<BlockAdapter>::fields = property<BlockAdapter>::props_t();
128
129 BlockAdapter::BlockAdapter(const BlockAdapter& o) :
130     BaseAdapter<BlockAdapter, org_scilab_modules_scicos::model::Block>(o), doc_content(o.doc_content->clone()) {}
131
132 BlockAdapter::BlockAdapter(org_scilab_modules_scicos::model::Block* o) :
133     BaseAdapter<BlockAdapter, org_scilab_modules_scicos::model::Block>(o)
134 {
135     if (property<BlockAdapter>::properties_have_not_been_set())
136     {
137         property<BlockAdapter>::fields.reserve(4);
138         property<BlockAdapter>::add_property(L"graphics", &graphics::get, &graphics::set);
139         property<BlockAdapter>::add_property(L"model", &model::get, &model::set);
140         property<BlockAdapter>::add_property(L"gui", &gui::get, &gui::set);
141         property<BlockAdapter>::add_property(L"doc", &doc::get, &doc::set);
142     }
143
144     doc_content = new types::List();
145 }
146
147 BlockAdapter::~BlockAdapter()
148 {
149     delete doc_content;
150 }
151
152 std::wstring BlockAdapter::getTypeStr()
153 {
154     return getSharedTypeStr();
155 }
156 std::wstring BlockAdapter::getShortTypeStr()
157 {
158     return getSharedTypeStr();
159 }
160
161 types::InternalType* BlockAdapter::getDocContent() const
162 {
163     return doc_content->clone();
164 }
165
166 void BlockAdapter::setDocContent(types::InternalType* v)
167 {
168     delete doc_content;
169     doc_content = v->clone();
170 }
171
172 } /* namespace view_scilab */
173 } /* namespace org_scilab_modules_scicos */