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