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