Xcos MVC: implement params wpar / title / tol / tf / context
[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         return adaptor.getDocContent();
110     }
111
112     static bool set(BlockAdapter& adaptor, types::InternalType* v, Controller& controller)
113     {
114         adaptor.setDocContent(v->clone());
115         return true;
116     }
117 };
118
119 } /* namespace */
120
121 template<> property<BlockAdapter>::props_t property<BlockAdapter>::fields = property<BlockAdapter>::props_t();
122
123 BlockAdapter::BlockAdapter(const BlockAdapter& o) :
124     BaseAdapter<BlockAdapter, org_scilab_modules_scicos::model::Block>(o), doc_content(o.doc_content->clone()) {}
125
126 BlockAdapter::BlockAdapter(org_scilab_modules_scicos::model::Block* o) :
127     BaseAdapter<BlockAdapter, org_scilab_modules_scicos::model::Block>(o)
128 {
129     if (property<BlockAdapter>::properties_have_not_been_set())
130     {
131         property<BlockAdapter>::fields.reserve(4);
132         property<BlockAdapter>::add_property(L"graphics", &graphics::get, &graphics::set);
133         property<BlockAdapter>::add_property(L"model", &model::get, &model::set);
134         property<BlockAdapter>::add_property(L"gui", &gui::get, &gui::set);
135         property<BlockAdapter>::add_property(L"doc", &doc::get, &doc::set);
136     }
137
138     doc_content = new types::List();
139 }
140
141 BlockAdapter::~BlockAdapter()
142 {
143     delete doc_content;
144 }
145
146 std::wstring BlockAdapter::getTypeStr()
147 {
148     return getSharedTypeStr();
149 }
150 std::wstring BlockAdapter::getShortTypeStr()
151 {
152     return getSharedTypeStr();
153 }
154
155 types::InternalType* BlockAdapter::getDocContent() const
156 {
157     return doc_content;
158 }
159
160 void BlockAdapter::setDocContent(types::InternalType* v)
161 {
162     delete doc_content;
163     doc_content = v->clone();
164 }
165
166 } /* namespace view_scilab */
167 } /* namespace org_scilab_modules_scicos */