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