1 // kln89_page_*.[ch]xx - this file is one of the "pages" that
2 // are used in the KLN89 GPS unit simulation.
4 // Written by David Luff, started 2005.
6 // Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "kln89_page_fpl.hxx"
29 KLN89FplPage::KLN89FplPage(KLN89* parent)
49 KLN89FplPage::~KLN89FplPage() {
52 void KLN89FplPage::Update(double dt) {
55 // NOTE - we need to draw the active leg arrow outside of this block to avoid the _delFP check.
56 // TODO - we really ought to merge the page 0 and other pages drawing code with a couple of lines of extra logic.
57 if(_subPage == 0 && !_delFP) { // Note that in the _delFP case, the active flightplan gets a header, and hence the same geometry as the other fps, so we draw it there.
59 // NOTE THAT FOR THE ACTIVE FLIGHT PLAN, TOP POSITION IS STILL 4 in the underline position scheme, to make
60 // copy and paste easier!!!!
62 // ---------------------------------- Copy the active FlightPlan and insert approach header and fence if required ---------------
63 // For synatical convienience
64 //vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
65 // Copy every waypoint for now.
66 // This is inefficient, but allows us to insert dummy waypoints to represent the header and fence
67 // in our *local copy* of the flightplan, if an approach is loaded. There must be a better way to do this!!!!
68 //cout << "AA" << endl;
69 vector<GPSWaypoint> waylist;
70 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
71 waylist.push_back(*_kln89->_flightPlans[_subPage]->waypoints[i]);
73 //cout << "BB" << endl;
76 if(_kln89->_approachLoaded) {
79 wp.type = GPS_WP_VIRT;
81 //cout << "CC" << endl;
82 for(unsigned int i=0; i<waylist.size(); ++i) {
83 // Insert the hdr immediately before the IAF
84 if(waylist[i].appType == GPS_IAF) {
85 waylist.insert(waylist.begin()+i, wp);
86 // Need to insert empty string into the params to keep them in sync
87 _params.insert(_params.begin()+i-1, "");
92 //cout << "DD" << endl;
94 wp.type = GPS_WP_VIRT;
95 wp.appType = GPS_FENCE;
96 for(unsigned int i=0; i<waylist.size(); ++i) {
97 // Insert the fence between the MAF and the MAP
98 if(waylist[i].appType == GPS_MAHP) {
99 waylist.insert(waylist.begin()+i, wp);
100 // Need to insert empty string into the params to keep them in sync
101 _params.insert(_params.begin()+i-1, "");
108 // Now make up a vector of waypoint numbers, since they aren't aligned with list position anymore
112 for(unsigned int i=0; i<waylist.size(); ++i) {
113 if(waylist[i].appType != GPS_HDR && waylist[i].appType != GPS_FENCE) {
114 numlist.push_back(num);
117 numlist.push_back(-1);
122 for(unsigned int i=0; i<waylist.size(); ++i) {
123 //cout << i + 1 - hfcount << ": ID= " << waylist[i].id;
124 if(waylist[i].appType == GPS_HDR) {
128 if(waylist[i].appType == GPS_FENCE) {
134 //----------------------------------------- end active FP copy ------------------------------------------------
136 // Recalculate which waypoint is displayed at the top of the list if required (generally if this page has lost focus).
137 int idx = _parent->GetActiveWaypointIndex();
139 if(waylist.size() <= 1) {
141 } else if(waylist.size() <= 4) {
144 // Make the active waypoint the second WP displayed
150 //cout << "HeaderPos = " << _hdrPos << ", fencePos = " << _fencePos << ", _fplPos = " << _fplPos << ", active waypoint index = " << _parent->GetActiveWaypointIndex() << '\n';
151 if(_hdrPos >= 0 && idx >= _hdrPos) {
153 if(_fencePos >= 0 && (idx + 1) >= _fencePos) {
157 _resetFplPos0 = false;
160 // Increment the active waypoint position if required due hdr and fence here not above so it gets called every frame
161 if(_hdrPos >= 0 && idx >= _hdrPos) {
163 if(_fencePos >= 0 && idx >= _fencePos) {
168 // Draw the leg arrow etc
169 int diff = idx - (int)_fplPos;
172 // No active waypoint
173 } else if(diff < 0) {
174 // Off screen to the top
175 } else if(diff > 2) {
180 // Only the head is blinked during waypoint alerting
181 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
182 _kln89->DrawSpecialChar(4, 2, 0, 3-drawPos);
184 // If the active waypoint is immediately after an approach header then we need to do an extra-long tail leg
185 if(_hdrPos >= 0 && idx == _hdrPos + 1) {
186 if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLongLegTail(3-drawPos);
188 if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLegTail(3-drawPos);
191 //cout << "Top pos is " << _fplPos0 << ' ';
193 if(_kln89->_mode == KLN89_MODE_CRSR) {
195 _kln89->Underline(2, 13, 3, 3);
196 } else if(_uLinePos >= 4) {
199 _kln89->Underline(2, 5, 3 - (_uLinePos - 4), 4);
200 } else if(_wLinePos == 4) {
201 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 4);
203 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), _wLinePos);
204 _kln89->Underline(2, 5 + _wLinePos, 3 - (_uLinePos - 4), 4 - _wLinePos);
206 if(!_kln89->_blink) {
207 //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
211 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 5);
215 // ----------------------------------
216 //cout << "A1" << endl;
218 // Sanity check the top position - remember that we can have an extra blank one at the bottom even if CRSR is off if crsr is switched on then off
219 if((int)_fplPos > ((int)waylist.size()) - 3) _fplPos = (((int)waylist.size()) - 3 < 0 ? 0 : waylist.size() - 3);
220 unsigned int last_pos;
221 if(waylist.empty()) {
224 last_pos = ((int)_fplPos == ((int)waylist.size()) - 3 ? waylist.size() : waylist.size() - 1);
226 //cout << "Initialising last_pos, last_pos = " << last_pos << '\n';
227 //cout << "B1" << endl;
228 if(waylist.size() < 4) last_pos = waylist.size();
230 // Don't draw the cyclic field header if the top waypoint is the approach header
231 // Not sure if this also applies to the fence - don't think so but TODO - check!
232 if(waylist[_fplPos].appType != GPS_HDR) {
233 _kln89->DrawChar('>', 2, 12, 3);
234 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) {
240 for(unsigned int i=0; i<4; ++i) {
241 //cout << "F1... " << i << endl;
242 // Don't draw the waypoint number for the header or fence
243 //cout << "_fplPos0 = " << _fplPos0 << ", waylist size is " << waylist.size() << '\n';
244 //cout << "F1, i = " << i << ", way id = " << waylist[_fplPos0+i].id << '\n';
245 if((waylist[_fplPos+i].appType == GPS_HDR || waylist[_fplPos+i].appType == GPS_FENCE)
246 && i != 3) { // By definition, the header and fence lines can't be displayed on the last line hence the unconditional !i==3 is safe.
250 int n = (i < 3 ? _fplPos + i + 1 : last_pos + 1);
251 if(_kln89->_approachLoaded) {
253 if(n > _fencePos) --n;
255 string s = GPSitoa(n);
256 // Don't draw the colon for waypoints that are part of the published approach
257 if(waylist[_fplPos+i].appType == GPS_APP_NONE) {
260 if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - (s[s.size()-1] == ':' ? s.size() : s.size()+1), 3 - i);
262 //cout << "F1 done!\n";
264 if(_delWp && _uLinePos == i+4) {
265 if(!_kln89->_blink) {
266 _kln89->DrawText("Del", 2, 0, 3-i);
267 _kln89->DrawChar('?', 2, 10, 3-i);
268 _kln89->Underline(2, 0, 3-i, 11);
271 } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
272 if(!_kln89->_blink) {
273 if(_wLinePos >= _entWp->id.size()) {
274 _kln89->DrawText(_entWp->id, 2, 4, 3-i);
275 _kln89->DrawChar(' ', 2, 4+_wLinePos, 3-i, false, true);
277 _kln89->DrawText(_entWp->id.substr(0, _wLinePos), 2, 4, 3-i);
278 _kln89->DrawChar(_entWp->id[_wLinePos], 2, 4+_wLinePos, 3-i, false, true);
279 _kln89->DrawText(_entWp->id.substr(_wLinePos+1, _entWp->id.size()-_wLinePos-1), 2, 5+_wLinePos, 3-i);
284 //cout << "F2" << endl;
286 //cout << "F2a" << endl;
287 if(i == 3 || _fplPos + i == waylist.size()) {
288 //cout << "F2a1" << endl;
289 //cout << "_uLinePos = " << _uLinePos << ", i = " << i << ", waylist.size() = " << waylist.size() << endl;
290 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
291 //cout << "Drawing underline..." << endl;
292 _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos].GetAprId() : "_____", 2, 4, 3-i);
293 //cout << "2" << endl;
295 //cout << "3" << endl;
296 //cout << "last_pos = " << last_pos << endl;
297 if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
298 //cout << "4" << endl;
300 if(_actFpMode == 0) {
301 string s = _params[last_pos - 1];
302 _kln89->DrawText(s, 2, 16-s.size(), 3-i);
303 } else if(_actFpMode == 3) {
304 string s = _params[last_pos - 1];
305 _kln89->DrawText(s, 2, 15-s.size(), 3-i);
306 _kln89->DrawSpecialChar(0, 2, 15, 3-i);
308 //cout << "5" << endl;
310 //cout << "6" << endl;
313 //cout << "F2a2" << endl;
314 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
315 if(waylist[_fplPos+i].appType == GPS_HDR) {
317 _kln89->DrawText("DELETE APPR?", 2, 1, 3-i);
318 } else if(_changeAppr) {
319 _kln89->DrawText("CHANGE APPR?", 2, 1, 3-i);
321 _kln89->DrawText(_kln89->_approachAbbrev, 2, 1, 3-i);
322 _kln89->DrawText(_kln89->_approachRwyStr, 2, 7, 3-i);
323 _kln89->DrawText(_kln89->_approachID, 2, 12, 3-i);
325 } else if(waylist[_fplPos+i].appType == GPS_FENCE) {
326 _kln89->DrawText("*NO WPT SEQ", 2, 0, 3-i);
328 _kln89->DrawText(waylist[_fplPos+i].GetAprId(), 2, 4, 3-i);
332 //cout << "F2b" << endl;
335 //cout << "i > 0 param draw...\n";
336 if(_actFpMode == 0) {
337 string s = _params[_fplPos + i - 1];
338 _kln89->DrawText(s, 2, 16-s.size(), 3-i);
339 } else if(_actFpMode == 3) {
340 string s = _params[_fplPos + i - 1];
341 _kln89->DrawText(s, 2, 15-s.size(), 3-i);
342 _kln89->DrawSpecialChar(0, 2, 15, 3-i);
345 //cout << "F2c" << endl;
347 //cout << "F3" << endl;
349 //cout << "GGGGGG" << endl;
350 } else { // Not active flightplan
351 //cout << "Top pos is " << _fplPos << ' ';
352 // For synatical convienience
353 //int nWp = (_subPage == 0 && !_delFP ? 4 : 3); // number of waypoints to display
354 vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
355 if(waylist.empty()) {
356 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 1 && _kln89->_blink)) {
357 _kln89->DrawText(_delFP ? "Delete FPL?" : "Copy FPL 0?", 2, 0, 3);
360 if(!(_kln89->_mode == KLN89_MODE_CRSR && (_uLinePos == 1 || _uLinePos == 2) && _kln89->_blink)) {
361 _kln89->DrawText(_delFP ? "Delete FPL?" : "Use?", 2, 0, 3);
363 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 2 && _kln89->_blink)) {
364 if(!_delFP) _kln89->DrawText("Inverted?", 2, 5, 3);
368 // ----------------------------------
369 if(_kln89->_mode == KLN89_MODE_CRSR) {
371 if(!_kln89->_blink) {
372 _kln89->Underline(2, 0, 3, (waylist.empty() || _delFP ? 11 : 4)); // This underline is blinked
375 } else if(_uLinePos == 2) {
376 // assert(!waylist.empty());
377 if(!_kln89->_blink) {
378 _kln89->Underline(2, 0, 3, 14); // This underline is blinked
381 } else if(_uLinePos == 3) {
382 _kln89->Underline(2, 13, 2, 3);
383 } else if(_uLinePos >= 4) {
386 _kln89->Underline(2, 5, 2 - (_uLinePos - 4), 4);
387 } else if(_wLinePos == 4) {
388 _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 4);
390 _kln89->Underline(2, 4, 2 - (_uLinePos - 4), _wLinePos);
391 _kln89->Underline(2, 5 + _wLinePos, 2 - (_uLinePos - 4), 4 - _wLinePos);
393 if(!_kln89->_blink) {
394 //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
398 if(!_delWp) _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 5);
402 // ----------------------------------
404 _kln89->DrawChar('>', 2, 12, 2);
405 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) DrawFpMode(2);
406 // Sanity check the top position - remember that we can have an extra blank one at the bottom even if CRSR is off if crsr is switched on then off
407 if((int)_fplPos > ((int)waylist.size()) - 2) _fplPos = (((int)waylist.size()) - 2 < 0 ? 0 : waylist.size() - 2);
408 unsigned int last_pos;
409 if(waylist.empty()) {
412 last_pos = ((int)_fplPos == ((int)waylist.size()) - 2 ? waylist.size() : waylist.size() - 1);
414 if(waylist.size() < 3) last_pos = waylist.size();
415 for(unsigned int i=0; i<3; ++i) {
416 string s = GPSitoa(i < 2 ? _fplPos + i + 1 : last_pos + 1);
418 if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - s.size(), 2 - i);
420 if(_delWp && _uLinePos == i+4) {
421 if(!_kln89->_blink) {
422 _kln89->DrawText("Del", 2, 0, 2-i);
423 _kln89->DrawChar('?', 2, 10, 2-i);
424 _kln89->Underline(2, 0, 2-i, 11);
427 } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
428 if(!_kln89->_blink) {
429 if(_wLinePos >= _entWp->id.size()) {
430 _kln89->DrawText(_entWp->id, 2, 4, 2-i);
431 _kln89->DrawChar(' ', 2, 4+_wLinePos, 2-i, false, true);
433 _kln89->DrawText(_entWp->id.substr(0, _wLinePos), 2, 4, 2-i);
434 _kln89->DrawChar(_entWp->id[_wLinePos], 2, 4+_wLinePos, 2-i, false, true);
435 _kln89->DrawText(_entWp->id.substr(_wLinePos+1, _entWp->id.size()-_wLinePos-1), 2, 5+_wLinePos, 2-i);
441 if(i == 2 || _fplPos + i == waylist.size()) {
442 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
443 _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos]->id : "_____", 2, 4, 2-i);
445 if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
448 string s = _params[last_pos - 1];
449 _kln89->DrawText(s, 2, 16-s.size(), 2-i);
454 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
455 _kln89->DrawText(waylist[_fplPos+i]->id, 2, 4, 2-i);
460 string s = _params[_fplPos + i - 1];
461 _kln89->DrawText(s, 2, 16-s.size(), 2-i);
469 KLN89Page::Update(dt);
472 void KLN89FplPage::DrawFpMode(int ypos) {
475 if(_actFpMode == 1) {
477 } else if(_actFpMode == 2) {
478 s = "UTC"; // TODO - alter depending on chosen timezone
479 } else if(_actFpMode == 3) {
480 s = (_kln89->_obsMode ? "OBS" : "Dtk");
487 _kln89->DrawText(s, 2, 13, ypos);
490 // Bit of an ipsy-dipsy function this one - calc the required parameters for the displayed flightplan.
491 void KLN89FplPage::Calc() {
493 GPSFlightPlan* fp = _kln89->_flightPlans[_subPage];
494 vector<GPSWaypoint*> wv = fp->waypoints;
496 // Active FP - parameters are only displayed for the active waypoint onwards for the active plan,
497 // and distance is cumulative from the user position.
498 if(0 == _actFpMode) {
500 double cum_tot = 0.0;
502 //cum_tot += _kln89->GetHorizontalSeparation(_kln89->_gpsLat, _kln89->_gpsLon, wv[0]->lat, wv[0]->lon) * SG_METER_TO_NM;
503 cum_tot += _kln89->GetGreatCircleDistance(_kln89->_gpsLat, _kln89->_gpsLon, wv[0]->lat, wv[0]->lon);
505 for(unsigned int i=1; i<wv.size(); ++i) {
506 //cum_tot += _kln89->GetHorizontalSeparation(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon) * SG_METER_TO_NM; // TODO - add units switch!
507 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon); // TODO - add units switch!
508 int n = (int)(cum_tot + 0.5);
509 _params.push_back(GPSitoa(n));
511 } else if(1 == _actFpMode) {
512 } else if(2 == _actFpMode) {
515 for(int i=1; i<wv.size(); ++i) {
516 double dtk = _kln89->GetMagHeadingFromTo(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);
517 int n = (int)(dtk + 0.5);
518 _params.push_back(GPSitoa(n));
525 double cum_tot = 0.0;
526 for(int i=1; i<wv.size(); ++i) {
527 //cum_tot += _kln89->GetHorizontalSeparation(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon) * SG_METER_TO_NM; // TODO - add units switch!
528 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon); // TODO - add units switch!
529 int n = (int)(cum_tot + 0.5);
530 _params.push_back(GPSitoa(n));
537 void KLN89FplPage::CrsrPressed() {
540 _kln89->_mode = KLN89_MODE_DISP;
545 if(_kln89->_mode == KLN89_MODE_DISP) {
548 for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
549 if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
550 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
559 if(_kln89->_obsMode) {
562 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
565 _uLinePos = (_subPage == 0 ? 3 : 1);
571 void KLN89FplPage::ClrPressed() {
573 _kln89->_mode = KLN89_MODE_DISP;
575 } else if(_delAppr) {
576 _kln89->_mode = KLN89_MODE_DISP;
579 if(KLN89_MODE_CRSR == _kln89->_mode) {
580 // TODO - see if we need to delete a waypoint
583 _kln89->_mode = KLN89_MODE_DISP;
586 // First check that we're not trying to delete an approach waypoint. Note that we can delete the approach by deleting the header though.
587 // Check for approach waypoints or header/fences in flightplan 0
588 int n = _fplPos + _uLinePos - 4;
590 bool fencePos = false;
591 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
593 //cout << "HEADER POS\n";
597 //cout << "FENCE POS\n";
600 if(_hdrPos >= 0 && n > _hdrPos) --n;
601 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
602 //cout << "New n = " << n << '\n';
606 } else if(fencePos) {
609 } else if(n >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
610 // no-op - off the end of the list on the entry field
611 } else if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType == GPS_APP_NONE) {
613 _kln89->_mode = KLN89_MODE_CRSR;
616 ShowScratchpadMessage("Invald", " Del ");
619 } else if(_uLinePos == 3) {
622 if(_actFpMode > 3) _actFpMode = 0;
625 if(_fpMode > 1) _fpMode = 0;
631 _kln89->_mode = KLN89_MODE_CRSR;
636 void KLN89FplPage::CleanUp() {
637 // TODO - possibly need to clean up _delWp here as well, since it goes off if dto and then ent are pressed.
640 for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
641 if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
642 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
648 KLN89Page::CleanUp();
651 void KLN89FplPage::LooseFocus() {
653 _resetFplPos0 = true;
657 _scratchpadMsg = false;
660 void KLN89FplPage::EntPressed() {
662 _parent->ClearFlightPlan(_subPage);
665 int pos = _uLinePos - 4 + _fplPos;
666 // Sanity check - the calculated wp position should never be off the end of the waypoint list.
667 if(pos > _kln89->_flightPlans[_subPage]->waypoints.size() - 1) {
668 cout << "ERROR - _uLinePos too big in KLN89FplPage::EntPressed!\n";
671 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + pos);
673 // Do we need to re-calc _fplPos here?
674 } else if(_bEntExp) {
677 _entWp = NULL; // DON'T delete it! - it's been pushed onto the waypoint list at this point.
679 _kln89->_cleanUpPage = -1;
681 // TODO - in actual fact the previously underlined waypoint stays in the same position and underlined
682 // in some or possibly all circumstances - need to check this out and match it, but not too important
686 // TODO - should be able to get rid of this switch I think and use the enum values.
687 switch(_entWp->type) {
689 _kln89->_activePage = _kln89->_pages[0];
690 _kln89->_curPage = 0;
691 ((KLN89Page*)_kln89->_pages[0])->SetEntInvert(true);
694 _kln89->_activePage = _kln89->_pages[1];
695 _kln89->_curPage = 1;
696 ((KLN89Page*)_kln89->_pages[1])->SetEntInvert(true);
699 _kln89->_activePage = _kln89->_pages[2];
700 _kln89->_curPage = 2;
701 ((KLN89Page*)_kln89->_pages[2])->SetEntInvert(true);
704 _kln89->_activePage = _kln89->_pages[3];
705 _kln89->_curPage = 3;
706 ((KLN89Page*)_kln89->_pages[3])->SetEntInvert(true);
709 _kln89->_activePage = _kln89->_pages[4];
710 _kln89->_curPage = 4;
711 ((KLN89Page*)_kln89->_pages[4])->SetEntInvert(true);
714 cout << "Error - unknown waypoint type found in KLN89::FplPage::EntPressed()\n";
716 _kln89->_activePage->SetId(_entWp->id);
717 _kln89->_entJump = 7;
718 _kln89->_cleanUpPage = 7;
719 _kln89->_entRestoreCrsr = true;
720 _kln89->_mode = KLN89_MODE_DISP;
723 } else if(_uLinePos == 1) {
724 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
726 for(unsigned int i=0; i<_kln89->_flightPlans[0]->waypoints.size(); ++i) {
727 GPSWaypoint* wp = new GPSWaypoint;
728 *wp = *(_kln89->_flightPlans[0]->waypoints[i]);
729 _kln89->_flightPlans[_subPage]->waypoints.push_back(wp);
733 _parent->ClearFlightPlan(0);
734 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
735 GPSWaypoint* wp = new GPSWaypoint;
736 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
737 _kln89->_flightPlans[0]->waypoints.push_back(wp);
739 _kln89->OrientateToActiveFlightPlan();
742 _parent->CrsrPressed();
743 } else if(_uLinePos == 2) {
744 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
748 _parent->ClearFlightPlan(0);
749 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
750 GPSWaypoint* wp = new GPSWaypoint;
751 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
752 // FIXME - very inefficient - use a reverse iterator on the source array and push_back instead!!!!!!!!
753 _kln89->_flightPlans[0]->waypoints.insert(_kln89->_flightPlans[0]->waypoints.begin(), wp);
755 _kln89->OrientateToActiveFlightPlan();
757 _parent->CrsrPressed();
762 void KLN89FplPage::Knob1Left1() {
770 if(_kln89->_mode == KLN89_MODE_CRSR) {
772 if(_wLinePos > 0) _wLinePos--;
774 // _uLinePos with empty/not-empty plan: 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
777 } else if(_uLinePos == 1 || _uLinePos == 2) {
779 } else if(_uLinePos == 3) {
781 } else if(_uLinePos == 4) {
782 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
783 _uLinePos = (_subPage == 0 ? 0 : 1);
784 } else if(_fplPos == 0) {
785 _uLinePos = (_subPage == 0 ? 0 : 2);
789 } else if(_uLinePos == 5) {
795 if(_subPage == 0 && _uLinePos > 3) {
796 int ix = _fplPos + (_uLinePos - 4);
797 if(_fencePos >= 0 && ix >= _fencePos) ix--;
798 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
799 if(ix >= _kln89->_activeFP->waypoints.size()) {
802 _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
806 //cout << "Not page 0, or not in waypoints, clearing id!\n";
812 void KLN89FplPage::Knob1Right1() {
820 if(_kln89->_mode == KLN89_MODE_CRSR) {
822 if(_wLinePos < 4) _wLinePos++;
824 // _uLinePos with empty/not-empty plan:
825 // 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
827 _uLinePos = (_subPage == 0 ? 4 : 1);
828 } else if(_uLinePos == 1) {
829 _uLinePos = (_kln89->_flightPlans[_subPage]->IsEmpty() ? 4 : 2);
830 } else if(_uLinePos == 2) {
832 } else if(_uLinePos == 3) {
833 if(!_kln89->_flightPlans[_subPage]->IsEmpty()) _uLinePos = 5;
834 } else if(_uLinePos == 4) {
836 } else if((_subPage == 0 && _uLinePos == 6) || (_subPage > 0 && _uLinePos == 5)) {
837 // Urrggh - complicated!
839 // 1: We're on the entry field at the end of the list, and can't move any more.
840 // 2: We're on the last or second-last field, and move to the last position
841 // 3: We're on a field before the second-last one, and don't move, but change the list-head position
842 // And 4: _subPage 0 can be complicated by the presence of header/fence lines in an approach.
845 if(_hdrPos >= 0) hfcount++;
846 if(_fencePos >= 0) hfcount++;
848 if(_kln89->_flightPlans[_subPage]->waypoints.size() == 1 || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - 1) {
850 } else if(_fplPos >= _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - (_subPage == 0 ? 4 : 3)) {
855 } else if(_uLinePos == 5) {
856 // Must be _subPage 0
859 // Must be the last line - either _uLinePos 6 or 7 depending on _subPage
860 int thresh = (_subPage == 0 ? 3 : 2);
861 if(_kln89->_flightPlans[_subPage]->waypoints.size() == thresh || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() - thresh) {
868 if(_subPage == 0 && _uLinePos > 3) {
869 int ix = _fplPos + (_uLinePos - 4);
870 if(_fencePos >= 0 && ix >= _fencePos) ix--;
871 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
872 if(ix >= _kln89->_activeFP->waypoints.size()) {
875 _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
879 //cout << "Not page 0, or not in waypoints, clearing id!\n";
885 void KLN89FplPage::Knob2Left1() {
892 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
893 if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
894 KLN89Page::Knob2Left1();
897 // Check for approach waypoints or header/fences in flightplan 0
898 int n = _fplPos + _uLinePos - 4;
900 bool fencePos = false;
902 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
904 //cout << "HEADER POS\n";
908 //cout << "FENCE POS\n";
911 if(_hdrPos >= 0 && n > _hdrPos) --n;
912 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
913 //cout << "New n = " << n << '\n';
915 if(n < _kln89->_flightPlans[_subPage]->waypoints.size()) {
916 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
922 // TODO - not sure what we actually do in this condition
924 } else if(fencePos) {
927 ShowScratchpadMessage("Invald", " Add ");
929 if((_wLinePos + 1) > _entWpStr.size()) {
932 _entWpStr[_wLinePos] = _kln89->DecChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
935 _fp0SelWpId.clear(); // Waypoints don't become the DTO default whilst being entered.
938 const GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1), multi, false);
943 _entWp = new GPSWaypoint;
944 if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
945 _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
947 _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);
958 void KLN89FplPage::Knob2Right1() {
965 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
966 if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
967 KLN89Page::Knob2Right1();
970 // Check for approach waypoints or header/fences in flightplan 0
971 int n = _fplPos + _uLinePos - 4;
973 bool fencePos = false;
975 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
977 //cout << "HEADER POS\n";
981 //cout << "FENCE POS\n";
984 if(_hdrPos >= 0 && n > _hdrPos) --n;
985 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
986 //cout << "New n = " << n << '\n';
988 if(n < _kln89->_flightPlans[_subPage]->waypoints.size()) {
989 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
995 // TODO - not sure what we actually do in this condition
997 } else if(fencePos) {
1000 ShowScratchpadMessage("Invald", " Add ");
1002 if((_wLinePos + 1) > _entWpStr.size()) {
1005 _entWpStr[_wLinePos] = _kln89->DecChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
1008 _fp0SelWpId.clear(); // Waypoints don't become the DTO default whilst being entered.
1011 const GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1), multi, false);
1015 if(_entWp == NULL) {
1016 _entWp = new GPSWaypoint;
1017 if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
1018 _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
1020 _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);