]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/KLN89/kln89_page_apt.cxx
Merge branch 'maint2' into next
[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                         float degLonPerHr = 360.0 / 24.0;       // 15 degrees per hour difference.
186                         // Since 0 longitude is the middle of UTC, the boundaries will be at 7.5, 22.5, 37.5 etc.
187                         int hrDiff = ((int)((fabs(ap->getLongitude())) + 7.5)) / 15;
188                         _kln89->DrawText("UTC", 2, 0, 2);
189                         if(hrDiff != 0) {
190                                 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 3, 2);
191                                 char buf[3];
192                                 snprintf(buf, 3, "%02i", hrDiff);
193                                 _kln89->DrawText((string)buf, 2, 4, 2);
194                                 _kln89->DrawText("(   DT)", 2, 6, 2);
195                                 if(ap->getLongitude() >= 0.0) {
196                                         hrDiff++;
197                                 } else {
198                                         hrDiff--;
199                                 }
200                                 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 7, 2);
201                                 snprintf(buf, 3, "%02i", hrDiff);
202                                 _kln89->DrawText((string)buf, 2, 8, 2);
203                         }
204                         // I guess we can make a heuristic guess as to fuel availability from the runway sizes
205                         // For now assume that airports with asphalt or concrete runways will have at least 100L,
206                         // and that runways over 4000ft will have JET.
207                         if(_aptRwys[0]->surface() <= 2) {
208                                 if(_aptRwys[0]->lengthFt() >= 4000) {
209                                         _kln89->DrawText("JET 100L", 2, 0, 1);
210                                 } else {
211                                         _kln89->DrawText("100L", 2, 0, 1);
212                                 }
213                         }
214                         if(_iaps.empty()) {
215                                 _kln89->DrawText("NO APR", 2, 0, 0);
216                         } else {
217                                 // TODO - output proper differentiation of ILS and NP APR and NP APR type eg GPS(R)
218                                 _kln89->DrawText("NP APR", 2, 0, 0);
219                         }
220                 } else if(_subPage == 3) {
221                         if(_nRwyPages > 1) {
222                                 _kln89->DrawChar('+', 1, 3, 0);
223                         }
224                         unsigned int i = _curRwyPage * 2;
225                         string s;
226                         if(i < _aptRwys.size()) {
227                                 // Rwy No.
228                                 string s = _aptRwys[i]->ident();
229                                 _kln89->DrawText(s, 2, 9, 3);
230                                 _kln89->DrawText("/", 2, 12, 3);
231                                 _kln89->DrawText(GetReverseRunwayNo(s), 2, 13, 3);
232                                 // Length
233                                 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
234                                 _kln89->DrawText(s, 2, 5 - s.size(), 2);
235                                 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 2);
236                                 // Surface
237                                 // TODO - why not store these strings as an array?
238                                 switch(_aptRwys[i]->surface()) {
239                                 case 1:
240                                         // Asphalt - fall through
241                                 case 2:
242                                         // Concrete
243                                         _kln89->DrawText("HRD", 2, 9, 2);
244                                         break;
245                                 case 3:
246                                 case 8:
247                                         // Turf / Turf helipad
248                                         _kln89->DrawText("TRF", 2, 9, 2);
249                                         break;
250                                 case 4:
251                                 case 9:
252                                         // Dirt / Dirt helipad
253                                         _kln89->DrawText("DRT", 2, 9, 2);
254                                         break;
255                                 case 5:
256                                         // Gravel
257                                         _kln89->DrawText("GRV", 2, 9, 2);
258                                         break;
259                                 case 6:
260                                         // Asphalt helipad - fall through
261                                 case 7:
262                                         // Concrete helipad
263                                         _kln89->DrawText("HRD", 2, 9, 2);
264                                         break;
265                                 case 12:
266                                         // Lakebed
267                                         _kln89->DrawText("CLY", 2, 9, 2);
268                                 default:
269                                         // erm? ...
270                                         _kln89->DrawText("MAT", 2, 9, 2);
271                                 }
272                         }
273                         i++;
274                         if(i < _aptRwys.size()) {
275                                 // Rwy No.
276                                 string s = _aptRwys[i]->ident();
277                                 _kln89->DrawText(s, 2, 9, 1);
278                                 _kln89->DrawText("/", 2, 12, 1);
279                                 _kln89->DrawText(GetReverseRunwayNo(s), 2, 13, 1);
280                                 // Length
281                                 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
282                                 _kln89->DrawText(s, 2, 5 - s.size(), 0);
283                                 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 0);
284                                 // Surface
285                                 // TODO - why not store these strings as an array?
286                                 switch(_aptRwys[i]->surface()) {
287                                 case 1:
288                                         // Asphalt - fall through
289                                 case 2:
290                                         // Concrete
291                                         _kln89->DrawText("HRD", 2, 9, 0);
292                                         break;
293                                 case 3:
294                                 case 8:
295                                         // Turf / Turf helipad
296                                         _kln89->DrawText("TRF", 2, 9, 0);
297                                         break;
298                                 case 4:
299                                 case 9:
300                                         // Dirt / Dirt helipad
301                                         _kln89->DrawText("DRT", 2, 9, 0);
302                                         break;
303                                 case 5:
304                                         // Gravel
305                                         _kln89->DrawText("GRV", 2, 9, 0);
306                                         break;
307                                 case 6:
308                                         // Asphalt helipad - fall through
309                                 case 7:
310                                         // Concrete helipad
311                                         _kln89->DrawText("HRD", 2, 9, 0);
312                                         break;
313                                 case 12:
314                                         // Lakebed
315                                         _kln89->DrawText("CLY", 2, 9, 0);
316                                 default:
317                                         // erm? ...
318                                         _kln89->DrawText("MAT", 2, 9, 0);
319                                 }
320                         }
321                 } else if(_subPage == 4) {
322                         if(_nFreqPages > 1) {
323                                 _kln89->DrawChar('+', 1, 3, 0);
324                         }
325                         unsigned int i = _curFreqPage * 3;
326                         if(i < _aptFreqs.size()) {
327                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 2);
328                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 2);
329                         }
330                         i++;
331                         if(i < _aptFreqs.size()) {
332                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 1);
333                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 1);
334                         }
335                         i++;
336                         if(i < _aptFreqs.size()) {
337                                 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 0);
338                                 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 0);
339                         }
340                 } else if(_subPage == 5) {
341                         // TODO - user ought to be allowed to leave persistent remarks
342                         _kln89->DrawText("[Remarks]", 2, 2, 2);
343                 } else if(_subPage == 6) {
344                         // We don't have SID/STAR database yet
345                         // TODO
346                         _kln89->DrawText("No SID/STAR", 2, 3, 2);
347                         _kln89->DrawText("In Data Base", 2, 2, 1);
348                         _kln89->DrawText("For This Airport", 2, 0, 0);
349                 } else if(_subPage == 7) {
350                         if(_iaps.empty()) {
351                                 _kln89->DrawText("IAP", 2, 11, 3);
352                                 _kln89->DrawText("No Approach", 2, 3, 2);
353                                 _kln89->DrawText("In Data Base", 2, 2, 1);
354                                 _kln89->DrawText("For This Airport", 2, 0, 0);
355                         } else {
356                                 if(_iafDialog) {
357                                         _kln89->DrawText(_iaps[_curIap]->_abbrev, 2, 1, 3);
358                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
359                                         _kln89->DrawText(_iaps[_curIap]->_id, 2, 12, 3);
360                                         _kln89->DrawText("IAF", 2, 2, 2);
361                                         int line = 0;
362                                         for(unsigned int i=_iafStart; i<_IAF.size(); ++i) {
363                                                 if(line == 2) {
364                                                         i = _IAF.size() - 1;
365                                                 }
366                                                 // Assume that the IAF number is always single digit!
367                                                 _kln89->DrawText(GPSitoa(i+1), 2, 6, 2-line);
368                                                 if(!(_kln89->_mode == KLN89_MODE_CRSR && _kln89->_blink && _uLinePos == (line + 1))) {
369                                                         _kln89->DrawText(_IAF[i]->id, 2, 8, 2-line);
370                                                 }
371                                                 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (line + 1) && !(_kln89->_blink )) {
372                                                         _kln89->Underline(2, 8, 2-line, 5);
373                                                 }
374                                                 ++line;
375                                         }
376                                         if(_uLinePos > 0 && !(_kln89->_blink)) {
377                                                 _kln89->DrawEnt();
378                                         }
379                                 } else if(_addDialog) {
380                                         _kln89->DrawText(_iaps[_curIap]->_abbrev, 2, 1, 3);
381                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
382                                         _kln89->DrawText(_iaps[_curIap]->_id, 2, 12, 3);
383                                         string s = GPSitoa(_fStart + 1);
384                                         _kln89->DrawText(s, 2, 2-s.size(), 2);
385                                         s = GPSitoa(_kln89->_approachFP->waypoints.size());
386                                         _kln89->DrawText(s, 2, 2-s.size(), 1);
387                                         if(!(_uLinePos == _fStart+1 && _kln89->_blink)) {
388                                                 _kln89->DrawText(_kln89->_approachFP->waypoints[_fStart]->id, 2, 4, 2);
389                                                 if(_uLinePos == _fStart+1) _kln89->Underline(2, 4, 2, 6);
390                                         }
391                                         if(!(_uLinePos == _maxULinePos-1 && _kln89->_blink)) {
392                                                 _kln89->DrawText(_kln89->_approachFP->waypoints[_kln89->_approachFP->waypoints.size()-1]->id, 2, 4, 1);
393                                                 if(_uLinePos == _maxULinePos-1) _kln89->Underline(2, 4, 1, 6);
394                                         }
395                                         if(!(_uLinePos > _kln89->_approachFP->waypoints.size() && _kln89->_blink)) {
396                                                 _kln89->DrawText("ADD TO FPL 0?", 2, 2, 0);
397                                                 if(_uLinePos > _kln89->_approachFP->waypoints.size()) {
398                                                         _kln89->Underline(2, 2, 0, 13);
399                                                         _kln89->DrawEnt();
400                                                 }
401                                         }
402                                 } else if(_replaceDialog) {
403                                         _kln89->DrawText(_iaps[_curIap]->_abbrev, 2, 1, 3);
404                                         _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
405                                         _kln89->DrawText(_iaps[_curIap]->_id, 2, 12, 3);
406                                         _kln89->DrawText("Replace Existing", 2, 0, 2);
407                                         _kln89->DrawText("Approach", 2, 4, 1);
408                                         if(_uLinePos > 0 && !(_kln89->_blink)) {
409                                                 _kln89->DrawText("APPROVE?", 2, 4, 0);
410                                                 _kln89->Underline(2, 4, 0, 8);
411                                                 _kln89->DrawEnt();
412                                         }
413                                 } else {
414                                         _kln89->DrawText("IAP", 2, 11, 3);
415                                         int check = 0;
416                                         bool selApp = false;
417                                         if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 4) {
418                                                 selApp = true;
419                                                 if(!_kln89->_blink) _kln89->DrawEnt();
420                                         }
421                                         for(unsigned int i=0; i<_iaps.size(); ++i) {    // TODO - do this properly when > 3 IAPs
422                                                 string s = GPSitoa(i+1);
423                                                 _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
424                                                 if(!(selApp && _uLinePos == 5+i && _kln89->_blink)) {
425                                                         _kln89->DrawText(_iaps[i]->_abbrev, 2, 3, 2-i);
426                                                         _kln89->DrawText(_iaps[i]->_rwyStr, 2, 9, 2-i);
427                                                 }
428                                                 if(selApp && _uLinePos == 5+i && !_kln89->_blink) {
429                                                         _kln89->Underline(2, 3, 2-i, 9);
430                                                 }
431                                                 check++;
432                                                 if(check > 2) break;
433                                         }
434                                 }
435                         }
436                 }
437         } else {
438                 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_apt_id, 2, 1, 3);
439                 if(_subPage == 0) {
440                         /*
441                         _kln89->DrawText("----.-", 2, 9, 3);
442                         _kln89->DrawText("--------------", 2, 0, 2);
443                         _kln89->DrawText("- -- --.--'", 2, 3, 1);
444                         _kln89->DrawText("---- --.--'", 2, 3, 0);
445                         _kln89->DrawSpecialChar(0, 2, 7, 1);
446                         _kln89->DrawSpecialChar(0, 2, 7, 0);
447                         */
448                 }
449         }
450         
451         if(_kln89->_mode == KLN89_MODE_CRSR) {
452                 if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
453                         if(_uLinePos > 0 && _uLinePos < 5) {
454                                 // TODO - blink as well
455                                 _kln89->Underline(2, _uLinePos, 3, 1);
456                         }
457                         for(unsigned int i = 0; i < _apt_id.size(); ++i) {
458                                 if(_uLinePos != (i + 1)) {
459                                         _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
460                                 } else {
461                                         if(!_kln89->_blink) _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
462                                 }
463                         }
464                 }
465         }
466         
467         _id = _apt_id;
468
469         KLN89Page::Update(dt);
470 }
471
472 void KLN89AptPage::SetId(const string& s) {
473         if(s != _apt_id || s != _last_apt_id) {
474                 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.
475                 /*
476                         DCL: Hmmm - I wrote the comment above, but I don't quite understand it!
477                         I'm not quite sure why I don't simply set _apt_id here (and NOT _last_apt_id)
478                         and let the logic in Update(...) handle the airport details cache update.
479                 */
480         }
481         _last_apt_id = _apt_id;
482         _save_apt_id = _apt_id;
483         _apt_id = s;
484 }
485
486 // Update the cached airport details
487 void KLN89AptPage::UpdateAirport(const string& id) {
488         // Frequencies
489         _aptFreqs.clear();
490         ATCData ad;
491         AptFreq aq;
492         //cout << "UpdateAirport called, id = " << id << '\n';
493         // TODO - the logic below only returns one service per type per airport - they can be on more than one freq though.
494         if(current_commlist->FindByCode(id, ad, ATIS)) {
495                 //cout << "Found ATIS\n";
496                 aq.service = "ATIS*";
497                 aq.freq = ad.freq;
498                 _aptFreqs.push_back(aq);
499         }
500         if(current_commlist->FindByCode(id, ad, GROUND)) {
501                 aq.service = "GRND*";
502                 aq.freq = ad.freq;
503                 _aptFreqs.push_back(aq);
504         }
505         if(current_commlist->FindByCode(id, ad, TOWER)) {
506                 aq.service = "TWR *";
507                 aq.freq = ad.freq;
508                 _aptFreqs.push_back(aq);
509         }
510         if(current_commlist->FindByCode(id, ad, APPROACH)) {
511                 aq.service = "APR";
512                 aq.freq = ad.freq;
513                 _aptFreqs.push_back(aq);
514         }
515         _nFreqPages = (unsigned int)ceil((float(_aptFreqs.size())) / 3.0f);
516         
517         // Runways
518         _aptRwys.clear();
519   const FGAirport* apt = fgFindAirportID(id);
520   assert(apt);
521   
522   // build local array, longest runway first
523   for (unsigned int r=0; r<apt->numRunways(); ++r) {
524     FGRunway* rwy(apt->getRunwayByIndex(r));
525     if ((r > 0) && (rwy->lengthFt() > _aptRwys.front()->lengthFt())) {
526       _aptRwys.insert(_aptRwys.begin(), rwy);
527     } else {
528       _aptRwys.push_back(rwy);
529     }
530   }
531   
532         _nRwyPages = (_aptRwys.size() + 1) / 2; // 2 runways per page.
533         if(_nFreqPages < 1) _nFreqPages = 1;
534         if(_nRwyPages < 1) _nRwyPages = 1;
535         
536         // Instrument approaches
537         // Only non-precision for now - TODO - handle precision approaches if necessary
538         _iaps.clear();
539         iap_map_iterator itr = _kln89->_np_iap.find(id);
540         if(itr != _kln89->_np_iap.end()) {
541                 _iaps = itr->second;
542         }
543         if(_subPage == 7) { 
544                 if(_iafDialog || _addDialog || _replaceDialog) {
545                         // Eek - major logic error if an airport details cache update occurs
546                         // with one of these dialogs active.
547                         // TODO - output a warning.
548                         //cout << "HELP!!!!!!!!!!\n";
549                 } else {
550                         _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!
551                 }
552         }
553 }
554
555 void KLN89AptPage::CrsrPressed() {
556         if(_kln89->_mode == KLN89_MODE_DISP) {
557                 if(_subPage == 7) {
558                         // Pressing crsr jumps back to vanilla IAP page.
559                         _iafDialog = false;
560                         _addDialog = false;
561                         _replaceDialog = false;
562                 }
563                 return;
564         }
565         if(_kln89->_obsMode) {
566                 _uLinePos = 0;
567         } else {
568                 _uLinePos = 1;
569         }
570         if(_subPage == 0) {
571                 _maxULinePos = 32;
572         } else if(_subPage == 7) {
573                 // Don't *think* we need some of this since some of it we can only get to by pressing ENT, not CRSR.
574                 if(_iafDialog) {
575                         _maxULinePos = _IAF.size();
576                         _uLinePos = 1;
577                 } else if(_addDialog) {
578                         _maxULinePos = 1;
579                         _uLinePos = 1;
580                 } else if(_replaceDialog) {
581                         _maxULinePos = 1;
582                         _uLinePos = 1;
583                 } else {
584                         _maxULinePos = 4 + _iaps.size();
585                         if(_iaps.empty()) {
586                                 _uLinePos = 1;
587                         } else {
588                                 _uLinePos = 5;
589                         }
590                 }
591         } else {
592                 _maxULinePos = 5;
593         }
594 }
595
596 void KLN89AptPage::ClrPressed() {
597         if(_subPage == 1 && _uLinePos == 5) {
598                 _to_flag = !_to_flag;
599         } else if(_subPage == 7) {
600                 // Clear backs out IAP selection one step at a time
601                 if(_iafDialog) {
602                         _iafDialog = false;
603                         _maxULinePos = 4 + _iaps.size();
604                         if(_iaps.empty()) {
605                                 _uLinePos = 1;
606                         } else {
607                                 _uLinePos = 5;
608                         }
609                 } else if(_addDialog) {
610                         _addDialog = false;
611                         if(_IAF.size() > 1) {
612                                 _iafDialog = true;
613                                 _maxULinePos = 1;
614                                 // Don't reset _curIaf since it is remembed.
615                                 _uLinePos = 1 + _curIaf;        // TODO - make this robust to more than 3 IAF
616                         } else {
617                                 _maxULinePos = 4 + _iaps.size();
618                                 if(_iaps.empty()) {
619                                         _uLinePos = 1;
620                                 } else {
621                                         _uLinePos = 5;
622                                 }
623                         }                       
624                 } else if(_replaceDialog) {
625                         _replaceDialog = false;
626                         _addDialog = true;
627                         _maxULinePos = 1;
628                         _uLinePos = 1;
629                 }
630         }
631 }
632
633 void KLN89AptPage::EntPressed() {
634         if(_entInvert) {
635                 _entInvert = false;
636                 _last_apt_id = _apt_id;
637                 _apt_id = _save_apt_id;
638         } else if(_subPage == 7 && _kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 0) {
639                 // We are selecting an approach
640                 if(_iafDialog) {
641                         if(_uLinePos > 0) {
642                                 // Record the IAF that was picked
643                                 if(_uLinePos == 3) {
644                                         _curIaf = _IAF.size() - 1;
645                                 } else {
646                                         _curIaf = _uLinePos - 1 + _iafStart;
647                                 }
648                                 //cout << "_curIaf = " << _curIaf << '\n';
649                                 // TODO - delete the waypoints inside _approachFP before clearing them!!!!!!!
650                                 _kln89->_approachFP->waypoints.clear();
651                                 GPSWaypoint* wp = new GPSWaypoint;
652                                 *wp = *_IAF[_curIaf];   // Need to make copies here since we're going to alter ID and type sometimes
653                                 string iafid = wp->id;
654                                 //wp->id += 'i';
655                                 _kln89->_approachFP->waypoints.push_back(wp);
656                                 for(unsigned int i=0; i<_IAP.size(); ++i) {
657                                         if(_IAP[i]->id != iafid) {      // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
658                         // FIXME - allow the same waypoint to be both the IAF and the FAF in some
659                         // approaches that have a procedure turn eg. KDLL
660                         // Also allow MAF to be the same as IAF!
661                                                 wp = new GPSWaypoint;
662                                                 *wp = *_IAP[i];
663                                                 //cout << "Adding waypoint " << wp->id << ", type is " << wp->appType << '\n';
664                                                 //if(wp->appType == GPS_FAF) wp->id += 'f';
665                                                 //if(wp->appType == GPS_MAP) wp->id += 'm';
666                                                 //cout << "New id = " << wp->id << '\n';
667                                                 _kln89->_approachFP->waypoints.push_back(wp);
668                                         }
669                                 }
670                                 // Only add 1 missed approach procedure waypoint for now.  I think this might be standard always anyway.
671                                 wp = new GPSWaypoint;
672                                 *wp = *_MAP[0];
673                                 //wp->id += 'h';
674                                 _kln89->_approachFP->waypoints.push_back(wp);
675                                 _iafDialog = false;
676                                 _addDialog = true;
677                                 _maxULinePos = _kln89->_approachFP->waypoints.size() + 1;
678                                 _uLinePos = _maxULinePos;
679                         }
680                 } else if(_addDialog) {
681                         if(_uLinePos == _maxULinePos) {
682                                 _addDialog = false;
683                                 if(_kln89->ApproachLoaded()) {
684                                         _replaceDialog = true;
685                                         _uLinePos = 1;
686                                         _maxULinePos = 1;
687                                 } else {
688                     // Now load the approach into the active flightplan.
689                     // As far as I can tell, the rules are this:
690                     // 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).
691                     // If the airport is not in the flightplan, append the approach to the flightplan, even if it is closer than the current active leg,
692                     // in which case reorientate to flightplan might put us on the approach, but unable to activate it.
693                     // However, it appears from the sim as if this can indeed happen if the user is not carefull.
694                     bool added = false;
695                     for(unsigned int i=0; i<_kln89->_activeFP->waypoints.size(); ++i) {
696                         if(_kln89->_activeFP->waypoints[i]->id == _apt_id) {
697                             _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.begin()+i, _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
698                             added = true;
699                             break;
700                         }
701                     }
702                     if(!added) {
703                         _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.end(), _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
704                     }
705                                         _kln89->_approachID = _apt_id;
706                                         _kln89->_approachAbbrev = _iaps[_curIap]->_abbrev;
707                                         _kln89->_approachRwyStr = _iaps[_curIap]->_rwyStr;
708                                         _kln89->_approachLoaded = true;
709                                         //_kln89->_messageStack.push_back("*Press ALT To Set Baro");
710                                         // Actually - this message is only sent when we go into appraoch-arm mode.
711                                         // TODO - check the flightplan for consistency
712                                         _kln89->OrientateToActiveFlightPlan();
713                                         _kln89->_mode = KLN89_MODE_DISP;
714                                         _kln89->_curPage = 7;
715                                         _kln89->_activePage = _kln89->_pages[7];        // Do we need to clean up here at all before jumping?
716                                 }
717                         }
718                 } else if(_replaceDialog) {
719                         // TODO - load the approach!
720                 } else if(_uLinePos > 4) {
721                         _IAF.clear();
722                         _IAP.clear();
723                         _MAP.clear();
724                         _curIaf = 0;
725                         _IAF = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAF;
726                         _IAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAP;
727                         _MAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_MAP;
728                         _curIap = _uLinePos - 5;        // TODO - handle the start of list ! no. 1, and the end of list not sequential!
729                         _uLinePos = 1;
730                         if(_IAF.size() > 1) {
731                                 // More than 1 IAF - display the selection dialog
732                                 _iafDialog = true;
733                                 _maxULinePos = _IAF.size();
734                         } else {
735                                 _addDialog = true;
736                                 _maxULinePos = 1;
737                         }
738                 }
739         }
740 }
741
742 void KLN89AptPage::Knob1Left1() {
743         if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
744                 if(_uLinePos == _maxULinePos) {
745                         _uLinePos--;
746                         if(_kln89->_approachFP->waypoints.size() > 1) _fStart = _kln89->_approachFP->waypoints.size() - 2;
747                 } else if(_uLinePos == _maxULinePos - 1) {
748                         _uLinePos--;
749                 } else if(_uLinePos > 0) {
750                         if(_fStart == 0) {
751                                 _uLinePos--;
752                         } else {
753                                 _uLinePos--;
754                                 _fStart--;
755                         }
756                 }
757         } else {
758                 KLN89Page::Knob1Left1();
759         }
760 }
761
762 void KLN89AptPage::Knob1Right1() {
763         if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
764                 if(_uLinePos == _maxULinePos) {
765                         // no-op
766                 } else if(_uLinePos == _maxULinePos - 1) {
767                         _uLinePos++;
768                         _fStart = 0;
769                 } else if(_uLinePos > 0) {
770                         if(_fStart >= _kln89->_approachFP->waypoints.size() - 2) {
771                                 _uLinePos++;
772                         } else {
773                                 _uLinePos++;
774                                 _fStart++;
775                         }
776                 } else if(_uLinePos == 0) {
777                         _uLinePos++;
778                         _fStart = 0;
779                 }
780         } else {
781                 KLN89Page::Knob1Right1();
782         }
783 }
784
785 void KLN89AptPage::Knob2Left1() {
786         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
787                 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
788                         KLN89Page::Knob2Left1();
789                 } else if(_subPage == 5) {
790                         _subPage = 4;
791                         _curFreqPage = _nFreqPages - 1;
792                 } else if(_subPage == 4) {
793                         // Freqency pages
794                         if(_curFreqPage == 0) {
795                                 _subPage = 3;
796                                 _curRwyPage = _nRwyPages - 1;
797                         } else {
798                                 _curFreqPage--;
799                         }
800                 } else if(_subPage == 3) {
801                         if(_curRwyPage == 0) {
802                                 KLN89Page::Knob2Left1();
803                         } else {
804                                 _curRwyPage--;
805                         }
806                 } else {
807                         KLN89Page::Knob2Left1();
808                 }
809         } else {
810                 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
811                         // Same logic for all pages - set the ID
812                         _apt_id = _apt_id.substr(0, _uLinePos);
813                         // ASSERT(_uLinePos > 0);
814                         if(_uLinePos == (_apt_id.size() + 1)) {
815                                 _apt_id += '9';
816                         } else {
817                                 _apt_id[_uLinePos - 1] = _kln89->DecChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
818                         }
819                 } else {
820                         if(_subPage == 0) {
821                                 // TODO - set by name
822                         } else {
823                                 // NO-OP - to/fr is cycled by clr button
824                         }
825                 }
826         }
827 }
828
829 void KLN89AptPage::Knob2Right1() {
830         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
831                 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
832                         KLN89Page::Knob2Right1();
833                 } else if(_subPage == 2) {
834                         _subPage = 3;
835                         _curRwyPage = 0;
836                 } else if(_subPage == 3) {
837                         if(_curRwyPage == _nRwyPages - 1) {
838                                 _subPage = 4;
839                                 _curFreqPage = 0;
840                         } else {
841                                 _curRwyPage++;
842                         }
843                 } else if(_subPage == 4) {
844                         if(_curFreqPage == _nFreqPages - 1) {
845                                 _subPage = 5;
846                         } else {
847                                 _curFreqPage++;
848                         }
849                 } else {
850                         KLN89Page::Knob2Right1();
851                 }
852         } else {
853                 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
854                         // Same logic for all pages - set the ID
855                         _apt_id = _apt_id.substr(0, _uLinePos);
856                         // ASSERT(_uLinePos > 0);
857                         if(_uLinePos == (_apt_id.size() + 1)) {
858                                 _apt_id += 'A';
859                         } else {
860                                 _apt_id[_uLinePos - 1] = _kln89->IncChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
861                         }
862                 } else {
863                         if(_subPage == 0) {
864                                 // TODO - set by name
865                         } else {
866                                 // NO-OP - to/fr is cycled by clr button
867                         }
868                 }
869         }
870 }