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