]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/KLN89/kln89_page_fpl.cxx
KLN89: Allow CLR to abort flightplan waypoint entry at the waypoint review page,...
[flightgear.git] / src / Instrumentation / KLN89 / kln89_page_fpl.cxx
1 // kln89_page_*.[ch]xx - this file is one of the "pages" that
2 //                       are used in the KLN89 GPS unit simulation. 
3 //
4 // Written by David Luff, started 2005.
5 //
6 // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
7 //
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.
12 //
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.
17 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "kln89_page_fpl.hxx"
29
30 #include <algorithm>
31 #include <iostream>
32
33 using namespace std;
34
35 KLN89FplPage::KLN89FplPage(KLN89* parent)
36 : KLN89Page(parent) {
37     _nSubPages = 26;
38     _subPage = 0;
39     _name = "FPL";
40     _fpMode = 0;
41     _actFpMode = 0;
42     _wLinePos = 0;
43     _bEntWp = false;
44     _bEntExp = false;
45     _entWp = NULL;
46     _fplPos = 0;
47     _resetFplPos0 = true;
48     _delFP = false;
49     _delWp = false;
50     _delAppr = false;
51     _changeAppr = false;
52     _fp0SelWpId = "";
53 }
54
55 KLN89FplPage::~KLN89FplPage() {
56 }
57
58 void KLN89FplPage::Update(double dt) {
59     Calc();
60     
61     // NOTE - we need to draw the active leg arrow outside of this block to avoid the _delFP check.
62     // TODO - we really ought to merge the page 0 and other pages drawing code with a couple of lines of extra logic.
63     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.
64         // active FlightPlan
65         // NOTE THAT FOR THE ACTIVE FLIGHT PLAN, TOP POSITION IS STILL 4 in the underline position scheme, to make 
66         // copy and paste easier!!!!
67
68         // ---------------------------------- Copy the active FlightPlan and insert approach header and fence if required ---------------
69         // For synatical convienience
70         //vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
71         // Copy every waypoint for now.
72         // This is inefficient, but allows us to insert dummy waypoints to represent the header and fence 
73         // in our *local copy* of the flightplan, if an approach is loaded.  There must be a better way to do this!!!!
74         vector<GPSWaypoint> waylist;
75         for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
76             waylist.push_back(*_kln89->_flightPlans[_subPage]->waypoints[i]);
77         }
78         _hdrPos = -1;
79         _fencePos = -1;
80         if(_kln89->_approachLoaded) {
81             GPSWaypoint wp;
82             wp.id = "HHHH";
83             wp.type = GPS_WP_VIRT;
84             wp.appType = GPS_HDR;
85             for(unsigned int i=0; i<waylist.size(); ++i) {
86                 // Insert the hdr immediately before the IAF
87                 if(waylist[i].appType == GPS_IAF) {
88                     waylist.insert(waylist.begin()+i, wp);
89                     // Need to insert empty string into the params to keep them in sync
90                     // Guard against empty params list.
91                     // This shouldn't happen, but currently it can until ETE and UTC params are implemeted
92                     // (and better the ugly params list ripped out and replaced with something more maintainable!).
93                     if(!_params.empty()) {
94                         _params.insert(_params.begin()+i-1, "");
95                     }
96                     _hdrPos = i;
97                     break;
98                 }
99             }
100             wp.id = "FFFF";
101             wp.type = GPS_WP_VIRT;
102             wp.appType = GPS_FENCE;
103             for(unsigned int i=0; i<waylist.size(); ++i) {
104                 // Insert the fence between the MAF and the MAP
105                 if(waylist[i].appType == GPS_MAHP) {
106                     waylist.insert(waylist.begin()+i, wp);
107                     // Need to insert empty string into the params to keep them in sync
108                     // Guard against empty params list. See comments a few lines above.
109                     if(!_params.empty()) {
110                         _params.insert(_params.begin()+i-1, "");
111                     }
112                     _fencePos = i;
113                     break;
114                 }
115             }
116         }
117         /*
118         // Now make up a vector of waypoint numbers, since they aren't aligned with list position anymore
119         int num = 0;
120         vector<int> numlist;
121         numlist.clear();
122         for(unsigned int i=0; i<waylist.size(); ++i) {
123             if(waylist[i].appType != GPS_HDR && waylist[i].appType != GPS_FENCE) {
124                 numlist.push_back(num);
125                 num++;
126             } else {
127                 numlist.push_back(-1);
128             }
129         }
130         */
131         int hfcount = 0;
132         for(unsigned int i=0; i<waylist.size(); ++i) {
133             //cout << i + 1 - hfcount << ":  ID= " << waylist[i].id;
134             if(waylist[i].appType == GPS_HDR) {
135                 hfcount++;
136                 //cout << " HDR!";
137             }
138             if(waylist[i].appType == GPS_FENCE) {
139                 hfcount++;
140                 //cout << " FENCE!";
141             }
142             //cout << '\n';
143         }
144         //----------------------------------------- end active FP copy ------------------------------------------------
145         
146         // Recalculate which waypoint is displayed at the top of the list if required (generally if this page has lost focus).
147         int idx = _kln89->GetActiveWaypointIndex();
148         if(_resetFplPos0) {
149             if(waylist.size() <= 1) {
150                 _fplPos = 0;
151             } else if(waylist.size() <= 4) {
152                 _fplPos = 1;
153             } else {
154                 // Make the active waypoint the second WP displayed
155                 _fplPos = idx;
156                 if(_fplPos != 0) {
157                     _fplPos--;
158                 }
159             }
160             //cout << "HeaderPos = " << _hdrPos << ", fencePos = " << _fencePos << ", _fplPos = " << _fplPos << ", active waypoint index = " << _parent->GetActiveWaypointIndex() << '\n';
161             if(_hdrPos >= 0 && idx >= _hdrPos) {
162                 _fplPos++;
163                 if(_fencePos >= 0 && (idx + 1) >= _fencePos) {
164                     _fplPos++;
165                 }
166             }
167             _resetFplPos0 = false;
168         }
169         
170         // Increment the active waypoint position if required due hdr and fence here not above so it gets called every frame
171         if(_hdrPos >= 0 && idx >= _hdrPos) {
172             idx++;
173             if(_fencePos >= 0 && idx >= _fencePos) {
174                 idx++;
175             }
176         }
177         
178         // Draw the leg arrow etc
179         int diff = idx - (int)_fplPos;
180         int drawPos = -1;
181         if(idx < 0) {
182             // No active waypoint
183         } else if(diff < 0) {
184             // Off screen to the top
185         } else if(diff > 2) {
186             // TODO !
187         } else {
188             drawPos = diff;
189         }
190         // Only the head is blinked during waypoint alerting
191         if(!(_kln89->_waypointAlert && _kln89->_blink)) {
192             _kln89->DrawSpecialChar(4, 2, 0, 3-drawPos);
193         }
194         // If the active waypoint is immediately after an approach header then we need to do an extra-long tail leg
195         if(_hdrPos >= 0 && idx == _hdrPos + 1) {
196             if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLongLegTail(3-drawPos);
197         } else {
198             if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLegTail(3-drawPos);
199         }
200         
201         //cout << "Top pos is " << _fplPos0 << ' ';
202         
203         if(_kln89->_mode == KLN89_MODE_CRSR) {
204             if(_uLinePos == 3) {
205                 _kln89->Underline(2, 13, 3, 3);
206             } else if(_uLinePos >= 4) {
207                 if(_bEntWp) {
208                     if(_wLinePos == 0) {
209                         _kln89->Underline(2, 5, 3 - (_uLinePos - 4), 4);
210                     } else if(_wLinePos == 4) {
211                         _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 4);
212                     } else {
213                         _kln89->Underline(2, 4, 3 - (_uLinePos - 4), _wLinePos);
214                         _kln89->Underline(2, 5 + _wLinePos, 3 - (_uLinePos - 4), 4 - _wLinePos);
215                     }
216                     if(!_kln89->_blink) {
217                         //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
218                         _kln89->DrawEnt();
219                     }
220                 } else {
221                     _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 5);
222                 }
223             }
224         }
225         // ----------------------------------
226             
227         // 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
228         if((int)_fplPos > ((int)waylist.size()) - 3) _fplPos = (((int)waylist.size()) - 3 < 0 ? 0 : waylist.size() - 3);
229         unsigned int last_pos;
230         if(waylist.empty()) {
231             last_pos = 0;
232         } else {
233             last_pos = ((int)_fplPos == ((int)waylist.size()) - 3 ? waylist.size() : waylist.size() - 1);
234         }
235         //cout << "Initialising last_pos, last_pos = " << last_pos << '\n';
236         if(waylist.size() < 4) last_pos = waylist.size();
237         
238         // Don't draw the cyclic field header if the top waypoint is the approach header
239         // Not sure if this also applies to the fence - don't think so but TODO - check!
240         if(!waylist.empty() && _fplPos < waylist.size()) {
241             if(waylist[_fplPos].appType != GPS_HDR) {
242                 _kln89->DrawChar('>', 2, 12, 3);
243                 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) {
244                     DrawFpMode(3);
245                 }
246             }
247         }
248         
249         //
250         // NOTE: this is the drawing routine for the ACTIVE flightplan, due to the number
251         // of differences there is a seperate routine for the stored flightplans.
252         //
253         // There are 4 rows to display flightplan data on the KLN89.  Draw each row.
254         for(unsigned int i=0; i<4; ++i) {
255             // Sanity check - we should no longer tickle this.
256             if((_fplPos+i) > waylist.size()) {
257                 break;
258             }
259
260             // Draw the number and (optional) colon for each row.
261             bool drawNum = true;
262             int n = (i < 3 ? _fplPos + i + 1 : last_pos + 1);
263             if(_kln89->_approachLoaded) {
264                 if(n > _hdrPos) --n;
265                 if(n > _fencePos) --n;
266             }
267             string s = GPSitoa(n);
268             if(_fplPos+i < waylist.size()) {
269                 // Don't draw the waypoint number for the header or fence
270                 if((waylist[_fplPos+i].appType == GPS_HDR || waylist[_fplPos+i].appType == GPS_FENCE) 
271                     && i != 3) {    // By definition, the header and fence lines can't be displayed on the last line hence the unconditional !i==3 is safe.
272                     drawNum = false;
273                 } else {
274                     // Don't draw the colon for waypoints that are part of the published approach
275                     if(waylist[_fplPos+i].appType == GPS_APP_NONE) {
276                         s += ':';
277                     }
278                 }
279             } else {
280                 // We must be drawing the next entry field at the end of the list - this has a colon
281                 s += ':';
282             }
283             if(drawNum) {
284                 if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - (s[s.size()-1] == ':' ? s.size() : s.size()+1), 3 - i);
285             }
286             // Done drawing numbers and colons.
287                 
288             bool drawID = true;
289             if(_delWp && _uLinePos == i+4) {
290                 if(!_kln89->_blink) {
291                     _kln89->DrawText("Del", 2, 0, 3-i);
292                     _kln89->DrawChar('?', 2, 10, 3-i);
293                     _kln89->Underline(2, 0, 3-i, 11);
294                     _kln89->DrawEnt();
295                 }
296             } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
297                 if(!_kln89->_blink) {
298                     if(_wLinePos >= _entWpStr.size()) {
299                         _kln89->DrawText(_entWpStr, 2, 4, 3-i);
300                         _kln89->DrawChar(' ', 2, 4+_wLinePos, 3-i, false, true);
301                     } else {
302                         _kln89->DrawText(_entWpStr.substr(0, _wLinePos), 2, 4, 3-i);
303                         _kln89->DrawChar(_entWpStr[_wLinePos], 2, 4+_wLinePos, 3-i, false, true);
304                         _kln89->DrawText(_entWpStr.substr(_wLinePos+1, _entWpStr.size()-_wLinePos-1), 2, 5+_wLinePos, 3-i);
305                     }
306                 }
307                 drawID = false;
308             }
309             if(drawID) {
310                 if(i == 3 || _fplPos + i == waylist.size()) {
311                     //cout << "_uLinePos = " << _uLinePos << ", i = " << i << ", waylist.size() = " << waylist.size() << endl;
312                     if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
313                         // Draw the underline symbol at the end of the flightplan
314                         _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos].GetAprId() : "_____", 2, 4, 3-i);
315                     }
316                     //cout << "last_pos = " << last_pos << endl;
317                     if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
318                         // Draw the param
319                         if(_actFpMode == 0) {
320                             string s = _params[last_pos - 1];
321                             _kln89->DrawText(s, 2, 16-s.size(), 3-i);
322                         } else if(_actFpMode == 3) {
323                             string s = _params[last_pos - 1];
324                             _kln89->DrawText(s, 2, 15-s.size(), 3-i);
325                             _kln89->DrawSpecialChar(0, 2, 15, 3-i);
326                         }
327                     }
328                     break;
329                 } else {
330                     if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
331                         if(waylist[_fplPos+i].appType == GPS_HDR) {
332                             if(_delAppr) {
333                                 _kln89->DrawText("DELETE APPR?", 2, 1, 3-i);
334                             } else if(_changeAppr) {
335                                 _kln89->DrawText("CHANGE APPR?", 2, 1, 3-i);
336                             } else {
337                                 _kln89->DrawText(_kln89->_approachAbbrev, 2, 1, 3-i);
338                                 _kln89->DrawText(_kln89->_approachRwyStr, 2, 7, 3-i);
339                                 _kln89->DrawText(_kln89->_approachID, 2, 12, 3-i);
340                             }
341                         } else if(waylist[_fplPos+i].appType == GPS_FENCE) {
342                             _kln89->DrawText("*NO WPT SEQ", 2, 0, 3-i);
343                         } else {
344                             _kln89->DrawText(waylist[_fplPos+i].GetAprId(), 2, 4, 3-i);
345                         }
346                     }
347                 }
348                 if(i > 0) {
349                     // Draw the param
350                     //cout << "i > 0 param draw...\n";
351                     if(_actFpMode == 0) {
352                         string s = _params[_fplPos + i - 1];
353                         _kln89->DrawText(s, 2, 16-s.size(), 3-i);
354                     } else if(_actFpMode == 3) {
355                         string s = _params[_fplPos + i - 1];
356                         _kln89->DrawText(s, 2, 15-s.size(), 3-i);
357                         _kln89->DrawSpecialChar(0, 2, 15, 3-i);
358                     }
359                 }
360             }
361         }
362     } else {  // Not active flightplan
363         //cout << "Top pos is " << _fplPos << ' ';
364         // For synatical convienience
365         //int nWp = (_subPage == 0 && !_delFP ? 4 : 3); // number of waypoints to display
366         vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
367         if(waylist.empty()) {
368             if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 1 && _kln89->_blink)) {
369                 _kln89->DrawText(_delFP ? "Delete FPL?" : "Copy FPL 0?", 2, 0, 3);
370             }
371         } else {
372             if(!(_kln89->_mode == KLN89_MODE_CRSR && (_uLinePos == 1 || _uLinePos == 2) && _kln89->_blink)) {
373                 _kln89->DrawText(_delFP ? "Delete FPL?" : "Use?", 2, 0, 3);
374             }
375             if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 2 && _kln89->_blink)) {
376                 if(!_delFP) _kln89->DrawText("Inverted?", 2, 5, 3);
377             }
378         }
379         
380         // ----------------------------------
381         if(_kln89->_mode == KLN89_MODE_CRSR) {
382             if(_uLinePos == 1) {
383                 if(!_kln89->_blink) {
384                     _kln89->Underline(2, 0, 3, (waylist.empty() || _delFP ? 11 : 4));   // This underline is blinked
385                     _kln89->DrawEnt();
386                 }
387             } else if(_uLinePos == 2) {
388                 // assert(!waylist.empty());
389                 if(!_kln89->_blink) {
390                     _kln89->Underline(2, 0, 3, 14); // This underline is blinked
391                     _kln89->DrawEnt();
392                 }
393             } else if(_uLinePos == 3) {
394                 _kln89->Underline(2, 13, 2, 3);
395             } else if(_uLinePos >= 4) {
396                 if(_bEntWp) {
397                     if(_wLinePos == 0) {
398                         _kln89->Underline(2, 5, 2 - (_uLinePos - 4), 4);
399                     } else if(_wLinePos == 4) {
400                         _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 4);
401                     } else {
402                         _kln89->Underline(2, 4, 2 - (_uLinePos - 4), _wLinePos);
403                         _kln89->Underline(2, 5 + _wLinePos, 2 - (_uLinePos - 4), 4 - _wLinePos);
404                     }
405                     if(!_kln89->_blink) {
406                         //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
407                         _kln89->DrawEnt();
408                     }
409                 } else {
410                     if(!_delWp) _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 5);
411                 }
412             }
413         }
414         // ----------------------------------
415             
416         _kln89->DrawChar('>', 2, 12, 2);
417         if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) DrawFpMode(2);
418         // 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
419         if((int)_fplPos > ((int)waylist.size()) - 2) _fplPos = (((int)waylist.size()) - 2 < 0 ? 0 : waylist.size() - 2);
420         unsigned int last_pos;
421         if(waylist.empty()) {
422             last_pos = 0;
423         } else {
424             last_pos = ((int)_fplPos == ((int)waylist.size()) - 2 ? waylist.size() : waylist.size() - 1);
425         }
426         if(waylist.size() < 3) last_pos = waylist.size();
427         for(unsigned int i=0; i<3; ++i) {
428             string s = GPSitoa(i < 2 ? _fplPos + i + 1 : last_pos + 1);
429             s += ':';
430             if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - s.size(), 2 - i);
431             bool drawID = true;
432             if(_delWp && _uLinePos == i+4) {
433                 if(!_kln89->_blink) {
434                     _kln89->DrawText("Del", 2, 0, 2-i);
435                     _kln89->DrawChar('?', 2, 10, 2-i);
436                     _kln89->Underline(2, 0, 2-i, 11);
437                     _kln89->DrawEnt();
438                 }
439             } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
440                 if(!_kln89->_blink) {
441                     if(_wLinePos >= _entWpStr.size()) {
442                         _kln89->DrawText(_entWpStr, 2, 4, 2-i);
443                         _kln89->DrawChar(' ', 2, 4+_wLinePos, 2-i, false, true);
444                     } else {
445                         _kln89->DrawText(_entWpStr.substr(0, _wLinePos), 2, 4, 2-i);
446                         _kln89->DrawChar(_entWpStr[_wLinePos], 2, 4+_wLinePos, 2-i, false, true);
447                         _kln89->DrawText(_entWpStr.substr(_wLinePos+1, _entWpStr.size()-_wLinePos-1), 2, 5+_wLinePos, 2-i);
448                     }
449                 }
450                 drawID = false;
451             }
452             if(drawID) {
453                 if(i == 2 || _fplPos + i == waylist.size()) {
454                     if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
455                         _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos]->id : "_____", 2, 4, 2-i);
456                     }
457                     if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
458                         // Draw the param
459                         if(_fpMode == 0) {
460                             string s = _params[last_pos - 1];
461                             _kln89->DrawText(s, 2, 16-s.size(), 2-i);
462                         }
463                     }
464                     break;
465                 } else {
466                     if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
467                         _kln89->DrawText(waylist[_fplPos+i]->id, 2, 4, 2-i);
468                     }
469                     if(i > 0) {
470                         // Draw the param
471                         if(_fpMode == 0) {
472                             string s = _params[_fplPos + i - 1];
473                             _kln89->DrawText(s, 2, 16-s.size(), 2-i);
474                         }
475                     }
476                 }
477             }
478         }
479     }
480     
481     KLN89Page::Update(dt);
482 }
483
484 void KLN89FplPage::DrawFpMode(int ypos) {
485     string s = "Dis";
486     if(0 == _subPage) {
487         if(_actFpMode == 1) {
488             s = "ETE";
489         } else if(_actFpMode == 2) {
490             s = "UTC";  // TODO - alter depending on chosen timezone
491         } else if(_actFpMode == 3) {
492             s = (_kln89->_obsMode ? "OBS" : "Dtk");
493         }
494     } else {
495         if(_fpMode == 1) {
496             s = "Dtk";
497         }
498     }
499     _kln89->DrawText(s, 2, 13, ypos);
500 }
501
502 // Calculate the displayable parameters for the currently displayed flightplan.
503 // These are Distance, ETE, ETA (UTC) and DTK for the active flight plan, and Distance and DTK for the stored flightplans.
504 // These are then converted into strings and pushed onto a string list (_params) which matches the flightplan,
505 // which is a really really really ugly and potentially bug-prone and hard to maintain way of doing this.
506 // TODO: When the unit is fully working rip out _params and replace with a better solution.
507 void KLN89FplPage::Calc() {
508     _params.clear();
509     GPSFlightPlan* fp = _kln89->_flightPlans[_subPage];
510     vector<GPSWaypoint*> wv = fp->waypoints;
511     // Some parameters are calculated differently for the active and the stored flightplans, so
512     // do the two cases seperately.
513     if(0 == _subPage) {
514         // Active FP - parameters are only displayed for the active waypoint onwards for the active plan,
515         // and distance is cumulative from the user position.
516         if(0 == _actFpMode) {
517             // Dis
518             double cum_tot = 0.0;
519             if(wv.size() > 0) {
520                 cum_tot += _kln89->GetGreatCircleDistance(_kln89->_gpsLat, _kln89->_gpsLon, wv[0]->lat, wv[0]->lon);
521             }
522             for(unsigned int i=1; i<wv.size(); ++i) {
523                 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);  // TODO - add units switch!
524                 int n = (int)(cum_tot + 0.5);
525                 _params.push_back(GPSitoa(n));
526             }
527         } else if(1 == _actFpMode) {
528             // TODO
529         } else if(2 == _actFpMode) {
530             // TODO
531         } else {
532             // Dtk
533             for(unsigned int i=1; i<wv.size(); ++i) {
534                 double dtk = _kln89->GetMagHeadingFromTo(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);
535                 int n = (int)(dtk + 0.5);
536                 _params.push_back(GPSitoa(n));
537             }
538             
539         }
540     } else {
541         // other FPs
542         if(0 == _fpMode) {
543             double cum_tot = 0.0;
544             for(unsigned int i=1; i<wv.size(); ++i) {
545                 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);  // TODO - add units switch!
546                 int n = (int)(cum_tot + 0.5);
547                 _params.push_back(GPSitoa(n));
548             }
549         } else {
550             // Dtk
551             for(unsigned int i=1; i<wv.size(); ++i) {
552                 double dtk = _kln89->GetMagHeadingFromTo(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);
553                 int n = (int)(dtk + 0.5);
554                 _params.push_back(GPSitoa(n));
555             }
556         }
557     }
558 }
559
560 void KLN89FplPage::CrsrPressed() {
561     if(_delFP || _delAppr) {
562         _delFP = _delAppr = false;
563         _kln89->_mode = KLN89_MODE_DISP;
564         return;
565     }
566
567     _wLinePos = 0;
568     if(_kln89->_mode == KLN89_MODE_DISP) {
569         _fp0SelWpId.clear();
570         if(_bEntWp) {
571             for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
572                 if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
573                     _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
574                 }
575             }
576             delete _entWp;
577             _entWp = NULL;
578             _bEntWp = false;
579             _entWpStr.clear();
580         }
581     } else {
582         if(_kln89->_obsMode) {
583             _uLinePos = 0;
584         } else {
585             if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
586                 _uLinePos = 4;
587             } else {
588                 _uLinePos = (_subPage == 0 ? 3 : 1);
589             }
590         }
591     }
592 }
593
594 void KLN89FplPage::ClrPressed() {
595     if(_delFP || _delAppr) {
596         _delFP = _delAppr = false;
597         _kln89->_mode = KLN89_MODE_DISP;
598     } else {
599         if(KLN89_MODE_CRSR == _kln89->_mode) {
600             // TODO - see if we need to delete a waypoint
601             if(_uLinePos >= 4) {
602                 if(_delWp) {
603                     // If we are already displaying a clear waypoint dialog in response to the CLR button,
604                     // then a further press of the CLR button cancels the dialog.
605                     _kln89->_mode = KLN89_MODE_DISP;
606                     _delWp = false;
607                 } else if(_bEntWp) {
608                     // If we are currently entering a waypoint, then CLR deletes it unconditionally
609                     // without a confirmation dialog and cancels waypoint entry.
610                     int pos = _uLinePos - 4 + _fplPos;
611                     // Sanity check - the calculated wp position should never be off the end of the waypoint list.
612                     if(pos > static_cast<int>(_kln89->_flightPlans[_subPage]->waypoints.size()) - 1) {
613                         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - _uLinePos too big in KLN89FplPage::ClrPressed!\n");
614                         return;
615                     }
616                     _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + pos);
617                     _bEntWp = false;
618                     _entWp = NULL;
619                     _entWpStr.clear();
620                     _wLinePos = 0;
621                     // We can also get here from the waypoint review page, so clear _bEntExp as well
622                     _bEntExp = false;
623                     // Do we need to re-calc _fplPos here?                    
624                 } else {
625                     // First check that we're not trying to delete an approach waypoint.  Note that we can delete the approach by deleting the header though.
626                     // Check for approach waypoints or header/fences in flightplan 0
627                     int n = _fplPos + _uLinePos - 4;
628                     bool hdrPos = false;
629                     bool fencePos = false;
630                     //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
631                     if(n == _hdrPos) {
632                         //cout << "HEADER POS\n";
633                         hdrPos = true;
634                     }
635                     if(n == _fencePos) {
636                         //cout << "FENCE POS\n";
637                         fencePos = true;
638                     }
639                     if(_hdrPos >= 0 && n > _hdrPos) --n;
640                     if(_fencePos >= 0 && n >= _fencePos) --n;   // This one needs to be >= since n is already decremented by 1 in the line above!
641                     //cout << "New n = " << n << '\n';
642                     if(hdrPos) {
643                         //cout << "HDRP\n";
644                         _delAppr = true;
645                     } else if(fencePos) {
646                         //cout << "FENP\n";
647                         // no-op
648                     } else if(n >= static_cast<int>(_kln89->_flightPlans[_subPage]->waypoints.size())) {
649                         // no-op - off the end of the list on the entry field
650                     } else if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType == GPS_APP_NONE) {
651                         //cout << "DELFP\n";
652                         _kln89->_mode = KLN89_MODE_CRSR;
653                         _delWp = true;
654                     } else {
655                         ShowScratchpadMessage("Invald", " Del  ");
656                     }
657                 }
658             } else if(_uLinePos == 3) {
659                 if(_subPage == 0) {
660                     _actFpMode++;
661                     if(_actFpMode > 3) _actFpMode = 0;
662                 } else {
663                     _fpMode++;
664                     if(_fpMode > 1) _fpMode = 0;
665                 }
666             }
667         } else {
668             _delFP = true;
669             _uLinePos = 1;
670             _kln89->_mode = KLN89_MODE_CRSR;
671         }
672     }
673 }
674
675 void KLN89FplPage::CleanUp() {
676     // TODO - possibly need to clean up _delWp here as well, since it goes off if dto and then ent are pressed.
677     
678     _bEntWp = false;
679     for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
680         if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
681             _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
682         }
683     }
684     delete _entWp;
685     _entWp = NULL;
686     _entWpStr.clear();
687     KLN89Page::CleanUp();
688 }
689
690 void KLN89FplPage::LooseFocus() {
691     _fplPos = 0;
692     _resetFplPos0 = true;
693     _wLinePos = 0;
694     _uLinePos = 0;
695     _fp0SelWpId.clear();
696     _scratchpadMsg = false;
697 }
698
699 void KLN89FplPage::EntPressed() {
700     if(_delFP) {
701         _kln89->ClearFlightPlan(_subPage);
702         CrsrPressed();
703     } else if(_delWp) {
704         int pos = _uLinePos - 4 + _fplPos;
705         // Sanity check - the calculated wp position should never be off the end of the waypoint list.
706         if(pos > static_cast<int>(_kln89->_flightPlans[_subPage]->waypoints.size()) - 1) {
707             SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - _uLinePos too big in KLN89FplPage::EntPressed!\n");
708             return;
709         }
710         _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + pos);
711         _delWp = false;
712         // Do we need to re-calc _fplPos here?
713     } else if(_bEntExp) {
714         _bEntWp = false;
715         _bEntExp = false;
716         _entWp = NULL;  // DON'T delete it! - it's been pushed onto the waypoint list at this point.
717         _entWpStr.clear();
718         _kln89->_cleanUpPage = -1;
719         _wLinePos = 0;
720         // TODO - in actual fact the previously underlined waypoint stays in the same position and underlined
721         // in some or possibly all circumstances - need to check this out and match it, but not too important
722         // for now.
723     } else if(_bEntWp) {
724         if(_entWp != NULL) {
725             // TODO - should be able to get rid of this switch I think and use the enum values.
726             switch(_entWp->type) {
727             case GPS_WP_APT:
728                 _kln89->_activePage = _kln89->_pages[0];
729                 _kln89->_curPage = 0;
730                 ((KLN89Page*)_kln89->_pages[0])->SetEntInvert(true);
731                 break;
732             case GPS_WP_VOR:
733                 _kln89->_activePage = _kln89->_pages[1];
734                 _kln89->_curPage = 1;
735                 ((KLN89Page*)_kln89->_pages[1])->SetEntInvert(true);
736                 break;
737             case GPS_WP_NDB:
738                 _kln89->_activePage = _kln89->_pages[2];
739                 _kln89->_curPage = 2;
740                 ((KLN89Page*)_kln89->_pages[2])->SetEntInvert(true);
741                 break;
742             case GPS_WP_INT:
743                 _kln89->_activePage = _kln89->_pages[3];
744                 _kln89->_curPage = 3;
745                 ((KLN89Page*)_kln89->_pages[3])->SetEntInvert(true);
746                 break;
747             case GPS_WP_USR:
748                 _kln89->_activePage = _kln89->_pages[4];
749                 _kln89->_curPage = 4;
750                 ((KLN89Page*)_kln89->_pages[4])->SetEntInvert(true);
751                 break;
752             default:
753                 SG_LOG(SG_GENERAL, SG_ALERT, "Error - unknown waypoint type found in KLN89::FplPage::EntPressed()\n");
754             }
755             _kln89->_activePage->SetId(_entWp->id);
756             _kln89->_entJump = _kln89->_clrJump = 7;
757             _kln89->_cleanUpPage = 7;
758             _kln89->_jumpRestoreCrsr = true;
759             _kln89->_mode = KLN89_MODE_DISP;
760         }
761         _bEntExp = true;
762     } else if(_uLinePos == 1) {
763         if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
764             // Copy fpl 0
765             for(unsigned int i=0; i<_kln89->_flightPlans[0]->waypoints.size(); ++i) {
766                 GPSWaypoint* wp = new GPSWaypoint;
767                 *wp = *(_kln89->_flightPlans[0]->waypoints[i]);
768                 _kln89->_flightPlans[_subPage]->waypoints.push_back(wp);
769             }
770         } else {
771             // Use
772             _kln89->ClearFlightPlan(0);
773             for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
774                 GPSWaypoint* wp = new GPSWaypoint;
775                 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
776                 _kln89->_flightPlans[0]->waypoints.push_back(wp);
777             }
778             _kln89->OrientateToActiveFlightPlan();
779             _subPage = 0;
780         }
781         _kln89->CrsrPressed();
782     } else if(_uLinePos == 2) {
783         if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
784             // ERROR !!!
785         } else {
786             // Use Invert
787             _kln89->ClearFlightPlan(0);
788             for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
789                 GPSWaypoint* wp = new GPSWaypoint;
790                 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
791                 // FIXME - very inefficient - use a reverse iterator on the source array and push_back instead!!!!!!!!
792                 _kln89->_flightPlans[0]->waypoints.insert(_kln89->_flightPlans[0]->waypoints.begin(), wp);
793             }
794             _kln89->OrientateToActiveFlightPlan();
795         }
796         _kln89->CrsrPressed();
797         _subPage = 0;
798     }
799 }
800
801 void KLN89FplPage::Knob1Left1() {
802     if(_delFP) {
803         _delFP = false;
804         return;
805     }
806     _delWp = false;
807     _changeAppr = false;
808
809     if(_kln89->_mode == KLN89_MODE_CRSR) {
810         if(_bEntWp) {
811             if(_wLinePos > 0) _wLinePos--;
812         } else {
813             // _uLinePos with empty/not-empty plan: 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
814             if(_uLinePos == 0) {
815                 // No-op
816             } else if(_uLinePos == 1 || _uLinePos == 2) {
817                 _uLinePos--;
818             } else if(_uLinePos == 3) {
819                 _uLinePos = 4;
820             } else if(_uLinePos == 4) {
821                 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
822                     _uLinePos = (_subPage == 0 ? 0 : 1);
823                 } else if(_fplPos == 0) {
824                     _uLinePos = (_subPage == 0 ? 0 : 2);
825                 } else {
826                     _fplPos--;
827                 }
828             } else if(_uLinePos == 5) {
829                 _uLinePos = 3;
830             } else {
831                 _uLinePos--;
832             }
833
834             if(_subPage == 0 && _uLinePos > 3) {
835                 int ix = _fplPos + (_uLinePos - 4);
836                 if(_fencePos >= 0 && ix >= _fencePos) ix--;
837                 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
838                 if(ix >= static_cast<int>(_kln89->_activeFP->waypoints.size())) {
839                     _fp0SelWpId.clear();
840                 } else {
841                     _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
842                 }
843             } else {
844                 _fp0SelWpId.clear();
845                 //cout << "Not page 0, or not in waypoints, clearing id!\n";
846             }
847         }
848     }
849 }
850
851 void KLN89FplPage::Knob1Right1() {
852     if(_delFP) {
853         _delFP = false;
854         return;
855     }
856     _delWp = false;
857     _changeAppr = false;
858     
859     if(_kln89->_mode == KLN89_MODE_CRSR) {
860         if(_bEntWp) {
861             if(_wLinePos < 4) _wLinePos++;
862         } else {
863             // _uLinePos with empty/not-empty plan: 
864             // 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
865             if(_uLinePos == 0) {
866                 _uLinePos = (_subPage == 0 ? 4 : 1);
867             } else if(_uLinePos == 1) {
868                 _uLinePos = (_kln89->_flightPlans[_subPage]->IsEmpty() ? 4 : 2);
869             } else if(_uLinePos == 2) {
870                 _uLinePos = 4;
871             } else if(_uLinePos == 3) {
872                 if(!_kln89->_flightPlans[_subPage]->IsEmpty()) _uLinePos = 5;
873             } else if(_uLinePos == 4) {
874                 _uLinePos = 3;
875             } else if((_subPage == 0 && _uLinePos == 6) || (_subPage > 0 && _uLinePos == 5)) {
876                 // Urrggh - complicated!
877                 // 3 possibilities:
878                 // 1: We're on the entry field at the end of the list, and can't move any more.
879                 // 2: We're on the last or second-last field, and move to the last position
880                 // 3: We're on a field before the second-last one, and don't move, but change the list-head position
881                 // And 4: _subPage 0 can be complicated by the presence of header/fence lines in an approach.
882                 int hfcount = 0;
883                 if(_subPage == 0) {
884                     if(_hdrPos >= 0) hfcount++;
885                     if(_fencePos >= 0) hfcount++;
886                 }
887                 if(_kln89->_flightPlans[_subPage]->waypoints.size() == 1 || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - 1) {
888                     // 1: Don't move
889                 } else if(_fplPos >= _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - (_subPage == 0 ? 4 : 3)) {
890                     _uLinePos++;
891                 } else {
892                     _fplPos++;
893                 }
894             } else if(_uLinePos == 5) {
895                 // Must be _subPage 0
896                 _uLinePos++;
897             } else {
898                 // Must be the last line - either _uLinePos 6 or 7 depending on _subPage
899                 const unsigned thresh = (_subPage == 0 ? 3 : 2);
900                 if(_kln89->_flightPlans[_subPage]->waypoints.size() == thresh || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() - thresh) {
901                     // Don't move
902                 } else {
903                     _fplPos++;
904                 }
905             }
906             
907             if(_subPage == 0 && _uLinePos > 3) {
908                 int ix = _fplPos + (_uLinePos - 4);
909                 if(_fencePos >= 0 && ix >= _fencePos) ix--;
910                 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
911                 if(ix >= static_cast<int>(_kln89->_activeFP->waypoints.size())) {
912                     _fp0SelWpId.clear();
913                 } else {
914                     _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
915                 }
916             } else {
917                 _fp0SelWpId.clear();
918                 //cout << "Not page 0, or not in waypoints, clearing id!\n";
919             }
920         }
921     }
922 }
923
924 void KLN89FplPage::Knob2Left1() {
925     if(_delFP) {
926         _delFP = false;
927         return;
928     }
929     _delWp = false;
930
931     if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
932         if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
933         KLN89Page::Knob2Left1();
934     } else {
935         if(_uLinePos > 3) {
936             // Check for approach waypoints or header/fences in flightplan 0
937             int n = _fplPos + _uLinePos - 4;
938             bool hdrPos = false;
939             bool fencePos = false;
940             bool appWp = false;
941             //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
942             if(n == _hdrPos) {
943                 //cout << "HEADER POS\n";
944                 hdrPos = true;
945             }
946             if(n == _fencePos) {
947                 //cout << "FENCE POS\n";
948                 fencePos = true;
949             }
950             if(_hdrPos >= 0 && n > _hdrPos) --n;
951             if(_fencePos >= 0 && n >= _fencePos) --n;   // This one needs to be >= since n is already decremented by 1 in the line above!
952             //cout << "New n = " << n << '\n';
953             
954             if(n < static_cast<int>(_kln89->_flightPlans[_subPage]->waypoints.size())) {
955                 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
956                     appWp = true;
957                 }
958             }
959             
960             if(hdrPos) {
961                 // TODO - not sure what we actually do in this condition
962                 _changeAppr = true;
963             } else if(fencePos) {
964                 // no-op?
965             } else if(appWp) {
966                 ShowScratchpadMessage("Invald", " Add  ");
967             } else {
968                 if((_wLinePos + 1) > _entWpStr.size()) {
969                     _entWpStr += '9';
970                 } else {
971                     _entWpStr[_wLinePos] = _kln89->DecChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
972                 }
973                 _bEntWp = true;
974                 _fp0SelWpId.clear();    // Waypoints don't become the DTO default whilst being entered.
975                 
976                 GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1));
977                 if(NULL == wp) {
978                     // No ID matches the partial ID entered so _entWpStr must be shortened to the cursor
979                     // position if it was longer due to a match on the previous character.
980                     if(_entWpStr.size() > _wLinePos+1) {
981                         _entWpStr = _entWpStr.substr(0, _wLinePos+1);
982                     }
983                 } else {
984                     // There is a matching full ID to the entered partial ID, so copy the full ID
985                     // into _entWpStr
986                     _entWpStr = wp->id;
987                     if(_entWp) {
988                         *_entWp = *wp; // copy
989                         delete wp;
990                     } else {
991                         _entWp = wp;
992                         if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
993                             _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
994                         } else {
995                             _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);
996                         }
997                     }
998                 }
999             }
1000         }
1001     }
1002 }
1003
1004 void KLN89FplPage::Knob2Right1() {
1005     if(_delFP) {
1006         _delFP = false;
1007         return;
1008     }
1009     _delWp = false;
1010
1011     if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
1012         if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
1013         KLN89Page::Knob2Right1();
1014     } else {
1015         if(_uLinePos > 3) {
1016             // Check for approach waypoints or header/fences in flightplan 0
1017             int n = _fplPos + _uLinePos - 4;
1018             bool hdrPos = false;
1019             bool fencePos = false;
1020             bool appWp = false;
1021             //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
1022             if(n == _hdrPos) {
1023                 //cout << "HEADER POS\n";
1024                 hdrPos = true;
1025             }
1026             if(n == _fencePos) {
1027                 //cout << "FENCE POS\n";
1028                 fencePos = true;
1029             }
1030             if(_hdrPos >= 0 && n > _hdrPos) --n;
1031             if(_fencePos >= 0 && n >= _fencePos) --n;   // This one needs to be >= since n is already decremented by 1 in the line above!
1032             //cout << "New n = " << n << '\n';
1033             
1034             if(n < static_cast<int>(_kln89->_flightPlans[_subPage]->waypoints.size())) {
1035                 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
1036                     appWp = true;
1037                 }
1038             }
1039             
1040             if(hdrPos) {
1041                 // TODO - not sure what we actually do in this condition
1042                 _changeAppr = true;
1043             } else if(fencePos) {
1044                 // no-op?
1045             } else if(appWp) {
1046                 ShowScratchpadMessage("Invald", " Add  ");
1047             } else {
1048                 if((_wLinePos + 1) > _entWpStr.size()) {
1049                     _entWpStr += 'A';
1050                 } else {
1051                     _entWpStr[_wLinePos] = _kln89->IncChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
1052                 }
1053                 _bEntWp = true;
1054                 _fp0SelWpId.clear();    // Waypoints don't become the DTO default whilst being entered.
1055                 
1056                 GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1));
1057                 if(NULL == wp) {
1058                     // No ID matches the partial ID entered so _entWpStr must be shortened to the cursor
1059                     // position if it was longer due to a match on the previous character.
1060                     if(_entWpStr.size() > _wLinePos+1) {
1061                         _entWpStr = _entWpStr.substr(0, _wLinePos+1);
1062                     }
1063                 } else {
1064                     // There is a matching full ID to the entered partial ID, so copy the full ID
1065                     // into _entWpStr
1066                     _entWpStr = wp->id;
1067                     if(_entWp) {
1068                         *_entWp = *wp; // copy
1069                         delete wp;
1070                     } else {
1071                         _entWp = wp;
1072                         if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
1073                             _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
1074                         } else {
1075                             _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);
1076                         }
1077                     }
1078                 }
1079             }
1080         }
1081     }
1082 }