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