TaskStats: make "flight_mode_height_margin" a constant
[xcsoar.git] / src / Form / Util.cpp
blob1776a27bfa800dc4c6175278af9f3c7d6ac881dc
1 /*
2 Copyright_License {
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"
34 #include <assert.h>
36 void
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);
44 void
45 ShowOptionalFormControl(SubForm &form, const TCHAR *control_name,
46 bool visible)
48 Window *window = form.FindByName(control_name);
49 if (window != NULL)
50 window->SetVisible(visible);
53 void
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);
61 void
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);
68 assert(ctl != NULL);
70 ctl->SetText(value);
73 void
74 LoadFormProperty(SubForm &form, const TCHAR *control_name, bool value)
76 assert(control_name != NULL);
78 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
79 if (ctl == NULL)
80 return;
82 DataFieldBoolean &df = *(DataFieldBoolean *)ctl->GetDataField();
83 assert(df.GetType() == DataField::Type::BOOLEAN);
84 df.Set(value);
85 ctl->RefreshDisplay();
88 void
89 LoadFormProperty(SubForm &form, const TCHAR *control_name, unsigned int value)
91 assert(control_name != NULL);
93 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
94 if (ctl == NULL)
95 return;
97 ctl->GetDataField()->SetAsInteger(value);
98 ctl->RefreshDisplay();
101 void
102 LoadFormPropertyEnum(SubForm &form, const TCHAR *control_name, int value)
104 assert(control_name != NULL);
106 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
107 assert(ctl != NULL);
109 DataFieldEnum &df = *(DataFieldEnum *)ctl->GetDataField();
110 assert(df.GetType() == DataField::Type::ENUM);
111 df.Set(value);
112 ctl->RefreshDisplay();
115 void
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);
122 assert(ctl != NULL);
124 DataFieldEnum &df = *(DataFieldEnum *)ctl->GetDataField();
125 assert(df.GetType() == DataField::Type::ENUM);
126 if (list[0].help != NULL)
127 df.EnableItemHelp(true);
129 df.AddChoices(list);
130 df.Set(value);
132 ctl->RefreshDisplay();
135 void
136 LoadFormProperty(SubForm &form, const TCHAR *control_name, fixed value)
138 assert(control_name != NULL);
140 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
141 assert(ctl != NULL);
143 DataFieldFloat &df = *(DataFieldFloat *)ctl->GetDataField();
144 assert(df.GetType() == DataField::Type::REAL);
145 df.Set(value);
146 ctl->RefreshDisplay();
149 void
150 LoadOptionalFormProperty(SubForm &form, const TCHAR *control_name,
151 fixed value)
153 assert(control_name != NULL);
155 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
156 if (ctl == NULL)
157 return;
159 DataFieldFloat &df = *(DataFieldFloat *)ctl->GetDataField();
160 assert(df.GetType() == DataField::Type::REAL);
161 df.Set(value);
162 ctl->RefreshDisplay();
165 void
166 LoadFormProperty(SubForm &form, const TCHAR *control_name,
167 const TCHAR *value)
169 assert(control_name != NULL);
170 assert(value != NULL);
172 WndProperty *ctl = (WndProperty *)form.FindByName(control_name);
173 assert(ctl != NULL);
175 DataFieldString &df = *(DataFieldString *)ctl->GetDataField();
176 assert(df.GetType() == DataField::Type::STRING);
178 df.Set(value);
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();
194 fixed
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();
206 bool
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();
221 const TCHAR *
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();
236 const TCHAR *
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();
252 bool
253 SaveFormProperty(const SubForm &form, const TCHAR *field, bool &value)
255 bool new_value = GetFormValueBoolean(form, field);
256 if (new_value == value)
257 return false;
259 value = new_value;
260 return true;
263 bool
264 SaveFormProperty(const SubForm &form, const TCHAR *field, unsigned int &value)
266 unsigned new_value = (unsigned)GetFormValueInteger(form, field);
267 if (new_value == value)
268 return false;
270 value = new_value;
271 return true;
274 bool
275 SaveFormProperty(SubForm &form, const TCHAR *control_name, fixed &value)
277 fixed new_value = GetFormValueFixed(form, control_name);
278 if (new_value == value)
279 return false;
281 value = new_value;
282 return true;
285 #ifdef FIXED_MATH
286 bool
287 SaveFormProperty(SubForm &form, const TCHAR *control_name, double &value)
289 double new_value = (double)GetFormValueFixed(form, control_name);
290 if (new_value == value)
291 return false;
293 value = new_value;
294 return true;
296 #endif
298 bool
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'))
312 /* not modified */
313 return false;
315 std::copy(value, value + length, buffer);
316 buffer[length] = _T('\0');
317 return true;