]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/KLN89/kln89_page_apt.cxx
95b34cfe6eee977337058fb0882af3ac73f4d8ca
[flightgear.git] / src / Instrumentation / KLN89 / kln89_page_apt.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_apt.hxx"
29
30 #include <simgear/structure/exception.hxx>
31 #include <cassert>
32
33 #include <ATC/CommStation.hxx>
34 #include <Main/globals.hxx>
35 #include <Airports/runways.hxx>
36 #include <Airports/simple.hxx>
37
38 KLN89AptPage::KLN89AptPage(KLN89* parent) 
39 : KLN89Page(parent) {
40         _nSubPages = 8;
41         _subPage = 0;
42         _name = "APT";
43         _apt_id = "KHWD";
44         // Make sure that _last_apt_id doesn't match at startup to force airport data to be fetched on first update.
45         _last_apt_id = "XXXX";
46         _nRwyPages = 1;
47         _curRwyPage = 0;
48         _nFreqPages = 1;
49         _curFreqPage = 0;
50         ap = NULL;
51         _iapStart = 0;
52         _iafStart = 0;
53         _fStart = 0;
54         _iaps.clear();
55         _iafDialog = false;
56         _addDialog = false;
57         _replaceDialog = false;
58         _curIap = 0;
59         _curIaf = 0;
60 }
61
62 KLN89AptPage::~KLN89AptPage() {
63 }
64
65 void KLN89AptPage::Update(double dt) {
66         bool actPage = (_kln89->_activePage->GetName() == "ACT" ? true : false);
67         bool multi;  // Not set by FindFirst...
68         bool exact = false;
69         if(_apt_id.size() == 4) exact = true;
70         // TODO - move this search out to where the button is pressed, and cache the result!
71         if(_apt_id != _last_apt_id || ap == NULL) ap = _kln89->FindFirstAptById(_apt_id, multi, exact);
72         //if(np == NULL) cout << "NULL... ";
73         //if(b == false) cout << "false...\n";
74         /*
75         if(np && b) {
76                 cout << "VOR FOUND!\n";
77         } else {
78                 cout << ":-(\n";
79         }
80         */
81         
82         if(ap) {
83                 //cout << "Valid airport found! id = " << ap->getId() << ", elev = " << ap->getElevation() << '\n';
84                 if(_apt_id != _last_apt_id) {
85                         UpdateAirport(ap->getId());
86                         _last_apt_id = _apt_id;
87                         _curFreqPage = 0;
88                         _curRwyPage = 0;
89                 }
90                 _apt_id = ap->getId();
91                 if(_kln89->GetActiveWaypoint()) {
92                         if(_apt_id == _kln89->GetActiveWaypoint()->id) {
93                                 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
94                                         // Active waypoint arrow
95                                         _kln89->DrawSpecialChar(4, 2, 0, 3);
96                                 }
97                         }
98                 }
99                 if(_kln89->_mode != KLN89_MODE_CRSR) {
100                         if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {  // Don't draw the airport name when the IAP dialogs are active
101                                 if(!_entInvert) {
102                                         if(!actPage) {
103                                                 _kln89->DrawText(ap->getId(), 2, 1, 3);
104                                         } else {
105                                                 // If it's the ACT page, The ID is shifted slightly right to make space for the waypoint index.
106                                                 _kln89->DrawText(ap->getId(), 2, 4, 3);
107                                                 char buf[3];
108                                                 int n = snprintf(buf, 3, "%i", _kln89->GetActiveWaypointIndex() + 1);
109                                                 _kln89->DrawText((string)buf, 2, 3 - n, 3);
110                                         }
111                                 } else {
112                                         if(!_kln89->_blink) {
113                                                 _kln89->DrawText(ap->getId(), 2, 1, 3, false, 99);
114                                                 _kln89->DrawEnt();
115                                         }
116                                 }
117                         }
118                 }
119                 if(_subPage == 0) {
120                         // Name
121                         _kln89->DrawText(ap->getName(), 2, 0, 2);
122                         // Elevation
123                         _kln89->DrawText(_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m", 2, 14, 3);
124                         char buf[6];
125                         int n = snprintf(buf, 5, "%i", (_kln89->_altUnits == GPS_ALT_UNITS_FT ? (int)(ap->getElevation()) : (int)((double)ap->getElevation() * SG_FEET_TO_METER)));
126                         _kln89->DrawText((string)buf, 2, 14 - n, 3);
127                         // Town
128                         airport_id_str_map_iterator itr = _kln89->_airportTowns.find(_apt_id);
129                         if(itr != _kln89->_airportTowns.end()) {
130                                 _kln89->DrawText(itr->second, 2, 0, 1);
131                         }
132                         // State / Province / Country
133                         itr = _kln89->_airportStates.find(_apt_id);
134                         if(itr != _kln89->_airportStates.end()) {
135                                 _kln89->DrawText(itr->second, 2, 0, 0);
136                         }
137                 } else if(_subPage == 1) {
138                         _kln89->DrawLatitude(ap->getLatitude(), 2, 3, 2);
139                         _kln89->DrawLongitude(ap->getLongitude(), 2, 3, 1);
140                         _kln89->DrawDirDistField(ap->getLatitude() * SG_DEGREES_TO_RADIANS, ap->getLongitude() * SG_DEGREES_TO_RADIANS, 
141                                                  2, 0, 0, _to_flag, (_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 5 ? true : false));
142                 } else if(_subPage == 2) {
143                         // Try and calculate a realistic difference from UTC based on longitude
144                         // Since 0 longitude is the middle of UTC, the boundaries will be at 7.5, 22.5, 37.5 etc.
145                         int hrDiff = ((int)((fabs(ap->getLongitude())) + 7.5)) / 15;
146                         _kln89->DrawText("UTC", 2, 0, 2);
147                         if(hrDiff != 0) {
148                                 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 3, 2);
149                                 char buf[3];
150                                 snprintf(buf, 3, "%02i", hrDiff);
151                                 _kln89->DrawText((string)buf, 2, 4, 2);
152                                 _kln89->DrawText("(   DT)", 2, 6, 2);
153                                 if(ap->getLongitude() >= 0.0) {
154                                         hrDiff++;
155                                 } else {
156                                         hrDiff--;
157                                 }
158                                 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 7, 2);
159                                 snprintf(buf, 3, "%02i", hrDiff);
160                                 _kln89->DrawText((string)buf, 2, 8, 2);
161                         }
162                         // I guess we can make a heuristic guess as to fuel availability from the runway sizes
163                         // For now assume that airports with asphalt or concrete runways will have at least 100L,
164                         // and that runways over 4000ft will have JET.
165                         if(_aptRwys[0]->surface() <= 2) {
166                                 if(_aptRwys[0]->lengthFt() >= 4000) {
167                                         _kln89->DrawText("JET 100L", 2, 0, 1);
168                                 } else {
169                                         _kln89->DrawText("100L", 2, 0, 1);
170                                 }
171                         }
172                         if(_iaps.empty()) {
173                                 _kln89->DrawText("NO APR", 2, 0, 0);
174                         } else {
175                                 // TODO - output proper differentiation of ILS and NP APR and NP APR type eg GPS(R)
176                                 _kln89->DrawText("NP APR", 2, 0, 0);
177                         }
178                 } else if(_subPage == 3) {
179                         if(_nRwyPages > 1) {
180                                 _kln89->DrawChar('+', 1, 3, 0);
181                         }
182                         unsigned int i = _curRwyPage * 2;
183                         string s;
184                         if(i < _aptRwys.size()) {
185                                 // Rwy No.
186                                 string s = _aptRwys[i]->ident();
187                                 _kln89->DrawText(s, 2, 9, 3);
188                                 _kln89->DrawText("/", 2, 12, 3);
189                 string recipIdent = _aptRwys[i]->reciprocalRunway()->ident();
190                                 _kln89->DrawText(recipIdent, 2, 13, 3);
191                                 // Length
192                                 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
193                                 _kln89->DrawText(s, 2, 5 - s.size(), 2);
194                                 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 2);
195                                 // Surface
196                                 // TODO - why not store these strings as an array?
197                                 switch(_aptRwys[i]->surface()) {
198                                 case 1:
199                                         // Asphalt - fall through
200                                 case 2:
201                                         // Concrete
202                                         _kln89->DrawText("HRD", 2, 9, 2);
203                                         break;
204                                 case 3:
205                                 case 8:
206                                         // Turf / Turf helipad
207                                         _kln89->DrawText("TRF", 2, 9, 2);
208                                         break;
209                                 case 4:
210                                 case 9:
211                                         // Dirt / Dirt helipad
212                                         _kln89->DrawText("DRT", 2, 9, 2);
213                                         break;
214                                 case 5:
215                                         // Gravel
216                                         _kln89->DrawText("GRV", 2, 9, 2);
217                                         break;
218                                 case 6:
219                                         // Asphalt helipad - fall through
220                                 case 7:
221                                         // Concrete helipad
222                                         _kln89->DrawText("HRD", 2, 9, 2);
223                                         break;
224                                 case 12:
225                                         // Lakebed
226                                         _kln89->DrawText("CLY", 2, 9, 2);
227                                 default:
228                                         // erm? ...
229                                         _kln89->DrawText("MAT", 2, 9, 2);
230                                 }
231                         }
232                         i++;
233                         if(i < _aptRwys.size()) {
234                                 // Rwy No.
235                                 string s = _aptRwys[i]->ident();
236                                 _kln89->DrawText(s, 2, 9, 1);
237                                 _kln89->DrawText("/", 2, 12, 1);
238                 string recip = _aptRwys[i]->reciprocalRunway()->ident();
239                                 _kln89->DrawText(recip, 2, 13, 1);
240                                 // Length
241                                 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
242                                 _kln89->DrawText(s, 2, 5 - s.size(), 0);
243                                 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 0);
244                                 // Surface
245                                 // TODO - why not store these strings as an array?
246                                 switch(_aptRwys[i]->surface()) {
247                                 case 1:
248                                         // Asphalt - fall through
249                                 case 2:
250                                         // Concrete
251                                         _kln89->DrawText("HRD", 2, 9, 0);
252                                         break;
253                                 case 3:
254                                 case 8:
255                                         // Turf / Turf helipad
256                                         _kln89->DrawText("TRF", 2, 9, 0);
257                                         break;
258                                 case 4:
259                                 case 9:
260                                         // Dirt / Dirt helipad
261                                         _kln89->DrawText("DRT", 2, 9, 0);
262                                         break;
263                                 case 5:
264                                         // Gravel
265                                         _kln89->DrawText("GRV", 2, 9, 0);
266                                         break;
267                                 case 6:
268                                         // Asphalt helipad - fall through
269                                 case 7:
270                                         // Concrete helipad
271                                         _kln89->DrawText("HRD", 2, 9, 0);
272                                         break;
273                                 case 12:
274                                         // Lakebed
275                                         _kln89->DrawText("CLY", 2, 9, 0);
276                                 default:
277                                         // erm? ...
278                                         _kln89->DrawText("MAT", 2, 9, 0);
279                                 }
280                         }
281                 } else if(_subPage == 4) {
282                         if(_nFreqPages > 1) {
283                                 _kln89->DrawChar('+', 1, 3, 0);
284                         }
285                         unsigned int i = _curFreqPage * 3;
286                         if(i < _aptFreqs.size()) {
287                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 2);
288                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 2);
289                         }
290                         i++;
291                         if(i < _aptFreqs.size()) {
292                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 1);
293                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 1);
294                         }
295                         i++;
296                         if(i < _aptFreqs.size()) {
297                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 0);
298                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 0);
299                         }
300                 } else if(_subPage == 5) {
301                         // TODO - user ought to be allowed to leave persistent remarks
302                         _kln89->DrawText("[Remarks]", 2, 2, 2);
303                 } else if(_subPage == 6) {
304                         // We don't have SID/STAR database yet
305                         // TODO
306                         _kln89->DrawText("No SID/STAR", 2, 3, 2);
307                         _kln89->DrawText("In Data Base", 2, 2, 1);
308                         _kln89->DrawText("For This Airport", 2, 0, 0);
309                 } else if(_subPage == 7) {
310                         if(_iaps.empty()) {
311                                 _kln89->DrawText("IAP", 2, 11, 3);
312                                 _kln89->DrawText("No Approach", 2, 3, 2);
313                                 _kln89->DrawText("In Data Base", 2, 2, 1);
314                                 _kln89->DrawText("For This Airport", 2, 0, 0);
315                         } else {
316                                 if(_iafDialog) {
317                                         _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
318                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
319                                         _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
320                                         _kln89->DrawText("IAF", 2, 2, 2);
321                                         unsigned int line = 0;
322                                         for(unsigned int i=_iafStart; i<_approachRoutes.size(); ++i) {
323                                                 if(line == 2) {
324                                                         i = _approachRoutes.size() - 1;
325                                                 }
326                                                 // Assume that the IAF number is always single digit!
327                                                 _kln89->DrawText(GPSitoa(i+1), 2, 6, 2-line);
328                                                 if(!(_kln89->_mode == KLN89_MODE_CRSR && _kln89->_blink && _uLinePos == (line + 1))) {
329                                                         _kln89->DrawText(_approachRoutes[i]->waypoints[0]->id, 2, 8, 2-line);
330                                                 }
331                                                 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (line + 1) && !(_kln89->_blink )) {
332                                                         _kln89->Underline(2, 8, 2-line, 5);
333                                                 }
334                                                 ++line;
335                                         }
336                                         if(_uLinePos > 0 && !(_kln89->_blink)) {
337                                                 _kln89->DrawEnt();
338                                         }
339                                 } else if(_addDialog) {
340                                         _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
341                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
342                                         _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
343                                         string s = GPSitoa(_fStart + 1);
344                                         _kln89->DrawText(s, 2, 2-s.size(), 2);
345                                         s = GPSitoa(_kln89->_approachFP->waypoints.size());
346                                         _kln89->DrawText(s, 2, 2-s.size(), 1);
347                                         if(!(_uLinePos == _fStart+1 && _kln89->_blink)) {
348                                                 _kln89->DrawText(_kln89->_approachFP->waypoints[_fStart]->id, 2, 4, 2);
349                                                 if(_uLinePos == _fStart+1) _kln89->Underline(2, 4, 2, 6);
350                                         }
351                                         if(!(_uLinePos == _maxULinePos-1 && _kln89->_blink)) {
352                                                 _kln89->DrawText(_kln89->_approachFP->waypoints[_kln89->_approachFP->waypoints.size()-1]->id, 2, 4, 1);
353                                                 if(_uLinePos == _maxULinePos-1) _kln89->Underline(2, 4, 1, 6);
354                                         }
355                                         if(!(_uLinePos > _kln89->_approachFP->waypoints.size() && _kln89->_blink)) {
356                                                 _kln89->DrawText("ADD TO FPL 0?", 2, 2, 0);
357                                                 if(_uLinePos > _kln89->_approachFP->waypoints.size()) {
358                                                         _kln89->Underline(2, 2, 0, 13);
359                                                         _kln89->DrawEnt();
360                                                 }
361                                         }
362                                 } else if(_replaceDialog) {
363                                         _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
364                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
365                                         _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
366                                         _kln89->DrawText("Replace Existing", 2, 0, 2);
367                                         _kln89->DrawText("Approach", 2, 4, 1);
368                                         if(_uLinePos > 0 && !(_kln89->_blink)) {
369                                                 _kln89->DrawText("APPROVE?", 2, 4, 0);
370                                                 _kln89->Underline(2, 4, 0, 8);
371                                                 _kln89->DrawEnt();
372                                         }
373                                 } else {
374                                         _kln89->DrawText("IAP", 2, 11, 3);
375                                         bool selApp = false;
376                                         if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 4) {
377                                                 selApp = true;
378                                                 if(!_kln89->_blink) _kln89->DrawEnt();
379                                         }
380                                         // _maxULine pos should be 4 + iaps.size() at this point.
381                                         // Draw a maximum of 3 IAPs.
382                                         // If there are more than 3 IAPs for this airport, then we need to offset the start
383                                         // of the list if _uLinePos is pointing at the 4th or later IAP.
384                                         unsigned int offset = 0;
385                                         unsigned int index;
386                                         if(_uLinePos > 7) {
387                                                 offset = _uLinePos - 7;
388                                         }
389                                         for(unsigned int i=0; i<3; ++i) {
390                                                 index = offset + i;
391                                                 if(index < _iaps.size()) {
392                                                         string s = GPSitoa(index+1);
393                                                         _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
394                                                         if(!(selApp && _uLinePos == index+5 && _kln89->_blink)) {
395                                                                 _kln89->DrawText(_iaps[index]->_ident, 2, 3, 2-i);
396                                                                 _kln89->DrawText(_iaps[index]->_rwyStr, 2, 9, 2-i);
397                                                         }
398                                                         if(selApp && _uLinePos == index+5 && !_kln89->_blink) {
399                                                                 _kln89->Underline(2, 3, 2-i, 9);
400                                                         }
401                                                 } else {
402                                                         break;
403                                                 }
404                                         }
405                                 }
406                         }
407                 }
408         } else {
409                 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_apt_id, 2, 1, 3);
410                 if(_subPage == 0) {
411                         /*
412                         _kln89->DrawText("----.-", 2, 9, 3);
413                         _kln89->DrawText("--------------", 2, 0, 2);
414                         _kln89->DrawText("- -- --.--'", 2, 3, 1);
415                         _kln89->DrawText("---- --.--'", 2, 3, 0);
416                         _kln89->DrawSpecialChar(0, 2, 7, 1);
417                         _kln89->DrawSpecialChar(0, 2, 7, 0);
418                         */
419                 }
420         }
421         
422         if(_kln89->_mode == KLN89_MODE_CRSR) {
423                 if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
424                         if(_uLinePos > 0 && _uLinePos < 5) {
425                                 // TODO - blink as well
426                                 _kln89->Underline(2, _uLinePos, 3, 1);
427                         }
428                         for(unsigned int i = 0; i < _apt_id.size(); ++i) {
429                                 if(_uLinePos != (i + 1)) {
430                                         _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
431                                 } else {
432                                         if(!_kln89->_blink) _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
433                                 }
434                         }
435                 }
436         }
437         
438         _id = _apt_id;
439
440         KLN89Page::Update(dt);
441 }
442
443 void KLN89AptPage::SetId(const string& s) {
444         if(s != _apt_id || s != _last_apt_id) {
445                 UpdateAirport(s);       // If we don't do this here we break things if s is the same as the current ID since the update wouldn't get called then.
446                 /*
447                         DCL: Hmmm - I wrote the comment above, but I don't quite understand it!
448                         I'm not quite sure why I don't simply set _apt_id here (and NOT _last_apt_id)
449                         and let the logic in Update(...) handle the airport details cache update.
450                 */
451         }
452         _last_apt_id = _apt_id;
453         _save_apt_id = _apt_id;
454         _apt_id = s;
455 }
456
457 // Update the cached airport details
458 void KLN89AptPage::UpdateAirport(const string& id) {
459         // Frequencies
460         _aptFreqs.clear();      
461         
462     const FGAirport* apt = fgFindAirportID(id);
463     if (!apt) {
464         throw sg_exception("UpdateAirport: unknown airport id " + id);
465     }
466         
467     for (unsigned int c=0; c<apt->commStations().size(); ++c) {
468         flightgear::CommStation* comm = apt->commStations()[c];
469         AptFreq aq;
470         aq.freq = comm->freqKHz();
471         switch (comm->type()) {
472         case FGPositioned::FREQ_ATIS:
473             aq.service = "ATIS*"; break;
474         case FGPositioned::FREQ_GROUND:
475             aq.service = "GRND*"; break;
476         case FGPositioned::FREQ_TOWER:
477             aq.service = "TWR *"; break;
478         case FGPositioned::FREQ_APP_DEP:
479             aq.service = "APR *"; break;
480         default:
481             continue;
482         }
483     }
484
485         _nFreqPages = (unsigned int)ceil((float(_aptFreqs.size())) / 3.0f);
486         
487         // Runways
488         _aptRwys.clear();
489   
490   // build local array, longest runway first
491   for (unsigned int r=0; r<apt->numRunways(); ++r) {
492     FGRunway* rwy(apt->getRunwayByIndex(r));
493     if ((r > 0) && (rwy->lengthFt() > _aptRwys.front()->lengthFt())) {
494       _aptRwys.insert(_aptRwys.begin(), rwy);
495     } else {
496       _aptRwys.push_back(rwy);
497     }
498   }
499   
500         _nRwyPages = (_aptRwys.size() + 1) / 2; // 2 runways per page.
501         if(_nFreqPages < 1) _nFreqPages = 1;
502         if(_nRwyPages < 1) _nRwyPages = 1;
503         
504         // Instrument approaches
505         // Only non-precision for now - TODO - handle precision approaches if necessary
506         _iaps.clear();
507         iap_map_iterator itr = _kln89->_np_iap.find(id);
508         if(itr != _kln89->_np_iap.end()) {
509                 _iaps = itr->second;
510         }
511         if(_subPage == 7) { 
512                 if(_iafDialog || _addDialog || _replaceDialog) {
513                         // Eek - major logic error if an airport details cache update occurs
514                         // with one of these dialogs active.
515                         // TODO - output a warning.
516                         //cout << "HELP!!!!!!!!!!\n";
517                 } else {
518                         _maxULinePos = 4 + _iaps.size();        // We shouldn't need to check the crsr for out-of-bounds here since we only update the airport details when the airport code is changed - ie. _uLinePos <= 4!
519                 }
520         }
521 }
522
523 void KLN89AptPage::CrsrPressed() {
524         if(_kln89->_mode == KLN89_MODE_DISP) {
525                 if(_subPage == 7) {
526                         // Pressing crsr jumps back to vanilla IAP page.
527                         _iafDialog = false;
528                         _addDialog = false;
529                         _replaceDialog = false;
530                 }
531                 return;
532         }
533         if(_kln89->_obsMode) {
534                 _uLinePos = 0;
535         } else {
536                 _uLinePos = 1;
537         }
538         if(_subPage == 0) {
539                 _maxULinePos = 32;
540         } else if(_subPage == 7) {
541                 // Don't *think* we need some of this since some of it we can only get to by pressing ENT, not CRSR.
542                 if(_iafDialog) {
543                         _maxULinePos = _approachRoutes.size();
544                         _uLinePos = 1;
545                 } else if(_addDialog) {
546                         _maxULinePos = 1;
547                         _uLinePos = 1;
548                 } else if(_replaceDialog) {
549                         _maxULinePos = 1;
550                         _uLinePos = 1;
551                 } else {
552                         _maxULinePos = 4 + _iaps.size();
553                         if(_iaps.empty()) {
554                                 _uLinePos = 1;
555                         } else {
556                                 _uLinePos = 5;
557                         }
558                 }
559         } else {
560                 _maxULinePos = 5;
561         }
562 }
563
564 void KLN89AptPage::ClrPressed() {
565         if(_subPage == 1 && _uLinePos == 5) {
566                 _to_flag = !_to_flag;
567         } else if(_subPage == 7) {
568                 // Clear backs out IAP selection one step at a time
569                 if(_iafDialog) {
570                         _iafDialog = false;
571                         _maxULinePos = 4 + _iaps.size();
572                         if(_iaps.empty()) {
573                                 _uLinePos = 1;
574                         } else {
575                                 _uLinePos = 5;
576                         }
577                 } else if(_addDialog) {
578                         _addDialog = false;
579                         if(_approachRoutes.size() > 1) {
580                                 _iafDialog = true;
581                                 _maxULinePos = 1;
582                                 // Don't reset _curIaf since it is remembed.
583                                 _uLinePos = 1 + _curIaf;        // TODO - make this robust to more than 3 IAF
584                         } else {
585                                 _maxULinePos = 4 + _iaps.size();
586                                 if(_iaps.empty()) {
587                                         _uLinePos = 1;
588                                 } else {
589                                         _uLinePos = 5;
590                                 }
591                         }                       
592                 } else if(_replaceDialog) {
593                         _replaceDialog = false;
594                         _addDialog = true;
595                         _maxULinePos = 1;
596                         _uLinePos = 1;
597                 }
598         }
599 }
600
601 void KLN89AptPage::EntPressed() {
602         if(_entInvert) {
603                 _entInvert = false;
604                 if(_kln89->_dtoReview) {
605                         _kln89->DtoInitiate(_apt_id);
606                 } else {
607                         _last_apt_id = _apt_id;
608                         _apt_id = _save_apt_id;
609                 }
610         } else if(_subPage == 7 && _kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 0) {
611                 // We are selecting an approach
612                 if(_iafDialog) {
613                         if(_uLinePos > 0) {
614                                 // Record the IAF that was picked
615                                 if(_uLinePos == 3) {
616                                         _curIaf = _approachRoutes.size() - 1;
617                                 } else {
618                                         _curIaf = _uLinePos - 1 + _iafStart;
619                                 }
620                                 //cout << "_curIaf = " << _curIaf << '\n';
621                                 // TODO - delete the waypoints inside _approachFP before clearing them!!!!!!!
622                                 _kln89->_approachFP->waypoints.clear();
623                                 GPSWaypoint* wp = new GPSWaypoint;
624                                 *wp = *(_approachRoutes[_curIaf]->waypoints[0]);        // Need to make copies here since we're going to alter ID and type sometimes
625                                 string iafid = wp->id;
626                                 _kln89->_approachFP->waypoints.push_back(wp);
627                                 for(unsigned int i=0; i<_IAP.size(); ++i) {
628                                         if(_IAP[i]->id != iafid) {      // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
629                         // FIXME - allow the same waypoint to be both the IAF and the FAF in some
630                         // approaches that have a procedure turn eg. KDLL
631                         // Also allow MAF to be the same as IAF!
632                                                 wp = new GPSWaypoint;
633                                                 *wp = *_IAP[i];
634                                                 //cout << "Adding waypoint " << wp->id << ", type is " << wp->appType << '\n';
635                                                 //if(wp->appType == GPS_FAF) wp->id += 'f';
636                                                 //if(wp->appType == GPS_MAP) wp->id += 'm';
637                                                 //cout << "New id = " << wp->id << '\n';
638                                                 _kln89->_approachFP->waypoints.push_back(wp);
639                                         }
640                                 }
641                                 _iafDialog = false;
642                                 _addDialog = true;
643                                 _maxULinePos = _kln89->_approachFP->waypoints.size() + 1;
644                                 _uLinePos = _maxULinePos;
645                         }
646                 } else if(_addDialog) {
647                         if(_uLinePos == _maxULinePos) {
648                                 _addDialog = false;
649                                 if(_kln89->ApproachLoaded()) {
650                                         _replaceDialog = true;
651                                         _uLinePos = 1;
652                                         _maxULinePos = 1;
653                                 } else {
654                     // Now load the approach into the active flightplan.
655                     // As far as I can tell, the rules are this:
656                     // If the airport of the approach is in the flightplan, insert it prior to this.  (Not sure what happens if airport has already been passed).
657                     // If the airport is not in the flightplan, append the approach to the flightplan, even if it is closer than the current active leg,
658                     // in which case reorientate to flightplan might put us on the approach, but unable to activate it.
659                     // However, it appears from the sim as if this can indeed happen if the user is not carefull.
660                     bool added = false;
661                     for(unsigned int i=0; i<_kln89->_activeFP->waypoints.size(); ++i) {
662                         if(_kln89->_activeFP->waypoints[i]->id == _apt_id) {
663                             _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.begin()+i, _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
664                             added = true;
665                             break;
666                         }
667                     }
668                     if(!added) {
669                         _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.end(), _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
670                     }
671                                         _kln89->_approachID = _apt_id;
672                                         _kln89->_approachAbbrev = _iaps[_curIap]->_ident;
673                                         _kln89->_approachRwyStr = _iaps[_curIap]->_rwyStr;
674                                         _kln89->_approachLoaded = true;
675                                         //_kln89->_messageStack.push_back("*Press ALT To Set Baro");
676                                         // Actually - this message is only sent when we go into appraoch-arm mode.
677                                         // TODO - check the flightplan for consistency
678                                         _kln89->OrientateToActiveFlightPlan();
679                                         _kln89->_mode = KLN89_MODE_DISP;
680                                         _kln89->_curPage = 7;
681                                         _kln89->_activePage = _kln89->_pages[7];        // Do we need to clean up here at all before jumping?
682                                 }
683                         }
684                 } else if(_replaceDialog) {
685                         // TODO - load the approach!
686                 } else if(_uLinePos > 4) {
687                         _approachRoutes.clear();
688                         _IAP.clear();
689                         _curIaf = 0;
690                         _approachRoutes = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_approachRoutes;
691                         _IAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAP;
692                         _curIap = _uLinePos - 5;        // TODO - handle the start of list ! no. 1, and the end of list not sequential!
693                         _uLinePos = 1;
694                         if(_approachRoutes.size() > 1) {
695                                 // More than 1 IAF - display the selection dialog
696                                 _iafDialog = true;
697                                 _maxULinePos = _approachRoutes.size();
698                         } else {
699                                 // There is only 1 IAF, so load the waypoints into the approach flightplan here.
700                                 // TODO - there is nasty code duplication loading the approach FP between the case here where we have only one
701                                 // IAF and the case where we must choose the IAF from a list.  Try to tidy this after it is all working properly.
702                                 _kln89->_approachFP->waypoints.clear();
703                                 GPSWaypoint* wp = new GPSWaypoint;
704                                 *wp = *(_approachRoutes[0]->waypoints[0]);      // Need to make copies here since we're going to alter ID and type sometimes
705                                 string iafid = wp->id;
706                                 _kln89->_approachFP->waypoints.push_back(wp);
707                                 for(unsigned int i=0; i<_IAP.size(); ++i) {
708                                         if(_IAP[i]->id != iafid) {      // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
709                                                 // FIXME - allow the same waypoint to be both the IAF and the FAF in some
710                                                 // approaches that have a procedure turn eg. KDLL
711                                                 // Also allow MAF to be the same as IAF!
712                                                 wp = new GPSWaypoint;
713                                                 *wp = *_IAP[i];
714                                                 _kln89->_approachFP->waypoints.push_back(wp);
715                                         }
716                                 }
717                                 _addDialog = true;
718                                 _maxULinePos = 1;
719                         }
720                 }
721         }
722 }
723
724 void KLN89AptPage::Knob1Left1() {
725         if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
726                 if(_uLinePos == _maxULinePos) {
727                         _uLinePos--;
728                         if(_kln89->_approachFP->waypoints.size() > 1) _fStart = _kln89->_approachFP->waypoints.size() - 2;
729                 } else if(_uLinePos == _maxULinePos - 1) {
730                         _uLinePos--;
731                 } else if(_uLinePos > 0) {
732                         if(_fStart == 0) {
733                                 _uLinePos--;
734                         } else {
735                                 _uLinePos--;
736                                 _fStart--;
737                         }
738                 }
739         } else {
740                 KLN89Page::Knob1Left1();
741         }
742 }
743
744 void KLN89AptPage::Knob1Right1() {
745         if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
746                 if(_uLinePos == _maxULinePos) {
747                         // no-op
748                 } else if(_uLinePos == _maxULinePos - 1) {
749                         _uLinePos++;
750                         _fStart = 0;
751                 } else if(_uLinePos > 0) {
752                         if(_fStart >= _kln89->_approachFP->waypoints.size() - 2) {
753                                 _uLinePos++;
754                         } else {
755                                 _uLinePos++;
756                                 _fStart++;
757                         }
758                 } else if(_uLinePos == 0) {
759                         _uLinePos++;
760                         _fStart = 0;
761                 }
762         } else {
763                 KLN89Page::Knob1Right1();
764         }
765 }
766
767 void KLN89AptPage::Knob2Left1() {
768         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
769                 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
770                         KLN89Page::Knob2Left1();
771                 } else if(_subPage == 5) {
772                         _subPage = 4;
773                         _curFreqPage = _nFreqPages - 1;
774                 } else if(_subPage == 4) {
775                         // Freqency pages
776                         if(_curFreqPage == 0) {
777                                 _subPage = 3;
778                                 _curRwyPage = _nRwyPages - 1;
779                         } else {
780                                 _curFreqPage--;
781                         }
782                 } else if(_subPage == 3) {
783                         if(_curRwyPage == 0) {
784                                 KLN89Page::Knob2Left1();
785                         } else {
786                                 _curRwyPage--;
787                         }
788                 } else if(_subPage == 0) {
789                         _subPage = 7;
790                         // We have to set _uLinePos here even though the cursor isn't pressed, to
791                         // ensure that the list displays properly.
792                         if(_iaps.empty()) {
793                                 _uLinePos = 1;
794                         } else {
795                                 _uLinePos = 5;
796                         }
797                 } else {
798                         KLN89Page::Knob2Left1();
799                 }
800         } else {
801                 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
802                         // Same logic for all pages - set the ID
803                         _apt_id = _apt_id.substr(0, _uLinePos);
804                         // ASSERT(_uLinePos > 0);
805                         if(_uLinePos == (_apt_id.size() + 1)) {
806                                 _apt_id += '9';
807                         } else {
808                                 _apt_id[_uLinePos - 1] = _kln89->DecChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
809                         }
810                 } else {
811                         if(_subPage == 0) {
812                                 // TODO - set by name
813                         } else {
814                                 // NO-OP - to/fr is cycled by clr button
815                         }
816                 }
817         }
818 }
819
820 void KLN89AptPage::Knob2Right1() {
821         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
822                 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
823                         KLN89Page::Knob2Right1();
824                 } else if(_subPage == 2) {
825                         _subPage = 3;
826                         _curRwyPage = 0;
827                 } else if(_subPage == 3) {
828                         if(_curRwyPage == _nRwyPages - 1) {
829                                 _subPage = 4;
830                                 _curFreqPage = 0;
831                         } else {
832                                 _curRwyPage++;
833                         }
834                 } else if(_subPage == 4) {
835                         if(_curFreqPage == _nFreqPages - 1) {
836                                 _subPage = 5;
837                         } else {
838                                 _curFreqPage++;
839                         }
840                 } else if(_subPage == 6) {
841                         _subPage = 7;
842                         // We have to set _uLinePos here even though the cursor isn't pressed, to
843                         // ensure that the list displays properly.
844                         if(_iaps.empty()) {
845                                 _uLinePos = 1;
846                         } else {
847                                 _uLinePos = 5;
848                         }
849                 } else {
850                         KLN89Page::Knob2Right1();
851                 }
852         } else {
853                 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
854                         // Same logic for all pages - set the ID
855                         _apt_id = _apt_id.substr(0, _uLinePos);
856                         // ASSERT(_uLinePos > 0);
857                         if(_uLinePos == (_apt_id.size() + 1)) {
858                                 _apt_id += 'A';
859                         } else {
860                                 _apt_id[_uLinePos - 1] = _kln89->IncChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
861                         }
862                 } else {
863                         if(_subPage == 0) {
864                                 // TODO - set by name
865                         } else {
866                                 // NO-OP - to/fr is cycled by clr button
867                         }
868                 }
869         }
870 }