Driver/Volkslogger: rename function to ReadAllFlights()
[xcsoar.git] / src / Form / DataField / Angle.cpp
blob9f71cfdecfd2400989f778698b8b1a905f165d01
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 "Angle.hpp"
25 #include "ComboList.hpp"
26 #include "Util/NumberParser.hpp"
27 #include "Util/Macros.hpp"
29 #include <stdio.h>
31 static TCHAR buffer[16];
33 unsigned
34 AngleDataField::Import(int value)
36 assert(value >= -int(MAX));
37 if (value < 0)
38 return value + MAX;
40 return Import(unsigned(value));
43 void
44 AngleDataField::ModifyValue(unsigned _value)
46 unsigned value2 = Import(_value);
47 if (value2 == value)
48 return;
50 value = value2;
51 Modified();
54 void
55 AngleDataField::ModifyValue(int _value)
57 unsigned value2 = Import(_value);
58 if (value2 == value)
59 return;
61 value = value2;
62 Modified();
65 void
66 AngleDataField::ModifyValue(Angle _value)
68 unsigned value2 = Import(_value);
69 if (value2 == value)
70 return;
72 value = value2;
73 Modified();
76 int
77 AngleDataField::GetAsInteger() const
79 return GetIntegerValue();
82 const TCHAR *
83 AngleDataField::GetAsString() const
85 _stprintf(buffer, _T("%u"), GetIntegerValue());
86 return buffer;
89 const TCHAR *
90 AngleDataField::GetAsDisplayString() const
92 _stprintf(buffer, _T("%u°"), GetIntegerValue());
93 return buffer;
96 void
97 AngleDataField::SetAsInteger(int _value)
99 ModifyValue(_value);
102 void
103 AngleDataField::SetAsString(const TCHAR *_value)
105 ModifyValue(Angle::Degrees(ParseDouble(_value, nullptr)));
108 void
109 AngleDataField::Inc()
111 ModifyValue(value + step);
114 void
115 AngleDataField::Dec()
117 ModifyValue(MAX + value - step);
120 void
121 AngleDataField::SetFromCombo(int i, TCHAR *s)
123 assert(i >= 0);
124 assert(unsigned(i) < MAX);
126 ModifyValue(unsigned(i));
129 static void
130 AppendComboValue(ComboList &combo_list, unsigned value)
132 TCHAR buffer1[ARRAY_SIZE(buffer)], buffer2[ARRAY_SIZE(buffer)];
133 _stprintf(buffer1, _T("%u"), value);
134 _stprintf(buffer2, _T("%u°"), value);
135 combo_list.Append(value, buffer1, buffer2);
138 ComboList *
139 AngleDataField::CreateComboList() const
141 ComboList *combo_list = new ComboList();
143 const unsigned fine_step = std::max(1u, step / 10u);
144 const unsigned fine_start_value = (value >= step) ? value - step : 0;
145 const unsigned fine_stop_value = value + step;
147 bool found_current = false;
148 bool in_fine_step = false;
149 unsigned current_step = step;
150 unsigned i = 0;
152 while (i < MAX) {
153 if (!found_current && value <= i) {
154 combo_list->ComboPopupItemSavedIndex = combo_list->size();
156 if (value < i)
157 /* the current value is not listed - insert it here */
158 AppendComboValue(*combo_list, value);
160 found_current = true;
163 AppendComboValue(*combo_list, i);
165 if (fine) {
166 if (i + current_step > fine_stop_value) {
167 if (in_fine_step) {
168 in_fine_step = false;
169 current_step = step;
170 i = ((i + step) / step) * step;
171 } else
172 i += current_step;
173 } else if (i + current_step > fine_start_value) {
174 if (!in_fine_step) {
175 in_fine_step = true;
176 current_step = fine_step;
177 i = fine_start_value + fine_step;
178 } else
179 i += current_step;
180 } else
181 i += current_step;
182 } else
183 i += current_step;
186 if (!found_current) {
187 /* the current value out of range - append it here */
188 combo_list->ComboPopupItemSavedIndex = combo_list->size();
189 AppendComboValue(*combo_list, value);
192 return combo_list;