4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Form/Util.hpp"
25 #include "Form/SubForm.hpp"
26 #include "Form/Edit.hpp"
27 #include "DataField/Base.hpp"
28 #include "DataField/Boolean.hpp"
29 #include "DataField/Float.hpp"
30 #include "DataField/Enum.hpp"
31 #include "DataField/String.hpp"
32 #include "DataField/FileReader.hpp"
37 ShowFormControl(SubForm
&form
, const TCHAR
*control_name
, bool visible
)
39 Window
*window
= form
.FindByName(control_name
);
40 assert(window
!= NULL
);
41 window
->SetVisible(visible
);
45 ShowOptionalFormControl(SubForm
&form
, const TCHAR
*control_name
,
48 Window
*window
= form
.FindByName(control_name
);
50 window
->SetVisible(visible
);
54 SetFormControlEnabled(SubForm
&form
, const TCHAR
*control_name
, bool enabled
)
56 Window
*window
= form
.FindByName(control_name
);
57 assert(window
!= NULL
);
58 window
->SetEnabled(enabled
);
62 SetFormValue(SubForm
&form
, const TCHAR
*control_name
, const TCHAR
*value
)
64 assert(control_name
!= NULL
);
65 assert(value
!= NULL
);
67 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
74 LoadFormProperty(SubForm
&form
, const TCHAR
*control_name
, bool value
)
76 assert(control_name
!= NULL
);
78 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
82 DataFieldBoolean
&df
= *(DataFieldBoolean
*)ctl
->GetDataField();
83 assert(df
.GetType() == DataField::Type::BOOLEAN
);
85 ctl
->RefreshDisplay();
89 LoadFormProperty(SubForm
&form
, const TCHAR
*control_name
, unsigned int value
)
91 assert(control_name
!= NULL
);
93 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
97 ctl
->GetDataField()->SetAsInteger(value
);
98 ctl
->RefreshDisplay();
102 LoadFormPropertyEnum(SubForm
&form
, const TCHAR
*control_name
, int value
)
104 assert(control_name
!= NULL
);
106 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
109 DataFieldEnum
&df
= *(DataFieldEnum
*)ctl
->GetDataField();
110 assert(df
.GetType() == DataField::Type::ENUM
);
112 ctl
->RefreshDisplay();
116 LoadFormProperty(SubForm
&form
, const TCHAR
*control_name
,
117 const StaticEnumChoice
*list
, unsigned value
)
119 assert(control_name
!= NULL
);
121 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
124 DataFieldEnum
&df
= *(DataFieldEnum
*)ctl
->GetDataField();
125 assert(df
.GetType() == DataField::Type::ENUM
);
126 if (list
[0].help
!= NULL
)
127 df
.EnableItemHelp(true);
132 ctl
->RefreshDisplay();
136 LoadFormProperty(SubForm
&form
, const TCHAR
*control_name
, fixed value
)
138 assert(control_name
!= NULL
);
140 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
143 DataFieldFloat
&df
= *(DataFieldFloat
*)ctl
->GetDataField();
144 assert(df
.GetType() == DataField::Type::REAL
);
146 ctl
->RefreshDisplay();
150 LoadOptionalFormProperty(SubForm
&form
, const TCHAR
*control_name
,
153 assert(control_name
!= NULL
);
155 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
159 DataFieldFloat
&df
= *(DataFieldFloat
*)ctl
->GetDataField();
160 assert(df
.GetType() == DataField::Type::REAL
);
162 ctl
->RefreshDisplay();
166 LoadFormProperty(SubForm
&form
, const TCHAR
*control_name
,
169 assert(control_name
!= NULL
);
170 assert(value
!= NULL
);
172 WndProperty
*ctl
= (WndProperty
*)form
.FindByName(control_name
);
175 DataFieldString
&df
= *(DataFieldString
*)ctl
->GetDataField();
176 assert(df
.GetType() == DataField::Type::STRING
);
179 ctl
->RefreshDisplay();
183 GetFormValueInteger(const SubForm
&form
, const TCHAR
*control_name
)
185 assert(control_name
!= NULL
);
187 const WndProperty
*control
=
188 (const WndProperty
*)form
.FindByName(control_name
);
189 assert(control
!= NULL
);
191 return control
->GetDataField()->GetAsInteger();
195 GetFormValueFixed(const SubForm
&form
, const TCHAR
*control_name
)
197 const WndProperty
*control
=
198 (const WndProperty
*)form
.FindByName(control_name
);
199 assert(control
!= NULL
);
201 const DataFieldFloat
&df
= *(const DataFieldFloat
*)control
->GetDataField();
202 assert(df
.GetType() == DataField::Type::REAL
);
203 return df
.GetAsFixed();
207 GetFormValueBoolean(const SubForm
&form
, const TCHAR
*control_name
)
209 assert(control_name
!= NULL
);
211 const WndProperty
*control
=
212 (const WndProperty
*)form
.FindByName(control_name
);
213 assert(control
!= NULL
);
215 const DataFieldBoolean
&df
=
216 *(const DataFieldBoolean
*)control
->GetDataField();
217 assert(df
.GetType() == DataField::Type::BOOLEAN
);
218 return df
.GetAsBoolean();
222 GetFormValueString(const SubForm
&form
, const TCHAR
*control_name
)
224 assert(control_name
!= NULL
);
226 const WndProperty
*control
=
227 (const WndProperty
*)form
.FindByName(control_name
);
228 assert(control
!= NULL
);
230 const DataFieldString
&df
= *(const DataFieldString
*)control
->GetDataField();
231 assert(df
.GetType() == DataField::Type::STRING
);
233 return df
.GetAsString();
237 GetFormValueFile(const SubForm
&form
, const TCHAR
*control_name
)
239 assert(control_name
!= NULL
);
241 const WndProperty
*control
=
242 (const WndProperty
*)form
.FindByName(control_name
);
243 assert(control
!= NULL
);
245 const DataFieldFileReader
&df
=
246 *(const DataFieldFileReader
*)control
->GetDataField();
247 assert(df
.GetType() == DataField::Type::FILE);
249 return df
.GetPathFile();
253 SaveFormProperty(const SubForm
&form
, const TCHAR
*field
, bool &value
)
255 bool new_value
= GetFormValueBoolean(form
, field
);
256 if (new_value
== value
)
264 SaveFormProperty(const SubForm
&form
, const TCHAR
*field
, unsigned int &value
)
266 unsigned new_value
= (unsigned)GetFormValueInteger(form
, field
);
267 if (new_value
== value
)
275 SaveFormProperty(SubForm
&form
, const TCHAR
*control_name
, fixed
&value
)
277 fixed new_value
= GetFormValueFixed(form
, control_name
);
278 if (new_value
== value
)
287 SaveFormProperty(SubForm
&form
, const TCHAR
*control_name
, double &value
)
289 double new_value
= (double)GetFormValueFixed(form
, control_name
);
290 if (new_value
== value
)
299 SaveFormProperty(const SubForm
&form
, const TCHAR
*control_name
,
300 TCHAR
*buffer
, size_t max_size
)
302 assert(max_size
> 0);
304 const TCHAR
*value
= GetFormValueString(form
, control_name
);
305 assert(value
!= NULL
);
307 size_t length
= _tcslen(value
);
308 if (length
>= max_size
)
309 length
= max_size
- 1;
311 if (_tcsncmp(value
, buffer
, length
) == 0 && buffer
[length
] == _T('\0'))
315 std::copy(value
, value
+ length
, buffer
);
316 buffer
[length
] = _T('\0');