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