]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/auto_gui.cxx
Curt Olson:
[flightgear.git] / src / Autopilot / auto_gui.cxx
1 // auto_gui.cxx -- autopilot gui interface
2 //
3 // Written by Norman Vine <nhv@cape.com>
4 // Arranged by Curt Olson <curt@flightgear.org>
5 //
6 // Copyright (C) 1998 - 2000
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
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include STL_STRING
36
37 #include <Aircraft/aircraft.hxx>
38 #include <FDM/flight.hxx>
39 #include <Controls/controls.hxx>
40 #include <Scenery/scenery.hxx>
41
42 #include <simgear/constants.h>
43 #include <simgear/sg_inlines.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/sg_geodesy.hxx>
46 #include <simgear/misc/sg_path.hxx>
47
48 #include <Airports/simple.hxx>
49 #include <GUI/gui.h>
50 #include <Main/fg_init.hxx>
51 #include <Main/globals.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Navaids/fixlist.hxx>
54
55 #include "auto_gui.hxx"
56 #include "route_mgr.hxx"
57 #include "xmlauto.hxx"
58
59 SG_USING_STD(string);
60
61
62 #define mySlider puSlider
63
64 // Climb speed constants
65 const double min_climb = 70.0;  // kts
66 const double best_climb = 75.0; // kts
67 const double ideal_climb_rate = 500.0; // fpm
68
69 /// These statics will eventually go into the class
70 /// they are just here while I am experimenting -- NHV :-)
71 // AutoPilot Gain Adjuster members
72 static double MaxRollAdjust;        // MaxRollAdjust       = 2 * APData->MaxRoll;
73 static double RollOutAdjust;        // RollOutAdjust       = 2 * APData->RollOut;
74 static double MaxAileronAdjust;     // MaxAileronAdjust    = 2 * APData->MaxAileron;
75 static double RollOutSmoothAdjust;  // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
76
77 static float MaxRollValue;          // 0.1 -> 1.0
78 static float RollOutValue;
79 static float MaxAileronValue;
80 static float RollOutSmoothValue;
81
82 static float TmpMaxRollValue;       // for cancel operation
83 static float TmpRollOutValue;
84 static float TmpMaxAileronValue;
85 static float TmpRollOutSmoothValue;
86
87 static puDialogBox *APAdjustDialog;
88 static puFrame     *APAdjustFrame;
89 static puText      *APAdjustDialogMessage;
90 static puFont      APAdjustLegendFont;
91 static puFont      APAdjustLabelFont;
92
93 static puOneShot *APAdjustOkButton;
94 static puOneShot *APAdjustResetButton;
95 static puOneShot *APAdjustCancelButton;
96
97 //static puButton        *APAdjustDragButton;
98
99 static puText *APAdjustMaxRollTitle;
100 static puText *APAdjustRollOutTitle;
101 static puText *APAdjustMaxAileronTitle;
102 static puText *APAdjustRollOutSmoothTitle;
103
104 static puText *APAdjustMaxAileronText;
105 static puText *APAdjustMaxRollText;
106 static puText *APAdjustRollOutText;
107 static puText *APAdjustRollOutSmoothText;
108
109 static mySlider *APAdjustHS0;
110 static mySlider *APAdjustHS1;
111 static mySlider *APAdjustHS2;
112 static mySlider *APAdjustHS3;
113
114 static char SliderText[ 4 ][ 8 ];
115
116 ///////// AutoPilot New Heading Dialog
117
118 static puDialogBox     *ApHeadingDialog;
119 static puFrame         *ApHeadingDialogFrame;
120 static puText          *ApHeadingDialogMessage;
121 static puInput         *ApHeadingDialogInput;
122 static puOneShot       *ApHeadingDialogOkButton;
123 static puOneShot       *ApHeadingDialogCancelButton;
124
125
126 ///////// AutoPilot New Altitude Dialog
127
128 static puDialogBox     *ApAltitudeDialog = 0;
129 static puFrame         *ApAltitudeDialogFrame = 0;
130 static puText          *ApAltitudeDialogMessage = 0;
131 static puInput         *ApAltitudeDialogInput = 0;
132
133 static puOneShot       *ApAltitudeDialogOkButton = 0;
134 static puOneShot       *ApAltitudeDialogCancelButton = 0;
135
136
137 /// The beginnings of Lock AutoPilot to target location :-)
138 //  Needs cleaning up but works
139 //  These statics should disapear when this is a class
140 static puDialogBox     *TgtAptDialog = 0;
141 static puFrame         *TgtAptDialogFrame = 0;
142 static puText          *TgtAptDialogMessage = 0;
143 static puInput         *TgtAptDialogInput = 0;
144 static puListBox       *TgtAptDialogWPList = 0;
145 static puSlider        *TgtAptDialogSlider = 0;
146 static puArrowButton   *TgtAptDialogUPArrow = 0;
147 static puArrowButton   *TgtAptDialogDNArrow = 0;
148 static char**          WPList;
149 static int             WPListsize;
150
151 static char NewTgtAirportId[16];
152 static char NewTgtAirportLabel[] = "New Apt/Fix ID";
153
154 static puOneShot       *TgtAptDialogOkButton = 0;
155 static puOneShot       *TgtAptDialogCancelButton = 0;
156 static puOneShot       *TgtAptDialogResetButton = 0;
157
158
159 // extern char *coord_format_lat(float);
160 // extern char *coord_format_lon(float);
161
162 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
163 static int scan_number(char *s, double *new_value)
164 {
165     int ret = 0;
166     char WordBuf[64];
167     char *cptr = s;
168     char *WordBufPtr = WordBuf;
169     if (*cptr == '+')
170         cptr++;
171     if (*cptr == '-') {
172         *WordBufPtr++ = *cptr++;
173     }
174     while (isdigit(*cptr) ) {
175         *WordBufPtr++ = *cptr++;
176         ret = 1;
177     }
178     if (*cptr == '.') 
179         *WordBufPtr++ = *cptr++;  // put the '.' into the string
180     while (isdigit(*cptr)) {
181         *WordBufPtr++ = *cptr++;
182         ret = 1;
183     }
184     if( ret == 1 ) {
185         *WordBufPtr = '\0';
186         sscanf(WordBuf, "%lf", new_value);
187     }
188
189     return(ret);
190 } // scan_number
191
192
193 void ApHeadingDialog_Cancel(puObject *)
194 {
195     ApHeadingDialogInput->rejectInput();
196     FG_POP_PUI_DIALOG( ApHeadingDialog );
197 }
198
199 void ApHeadingDialog_OK (puObject *me)
200 {
201     int error = 0;
202     char *c;
203     string s;
204     ApHeadingDialogInput -> getValue( &c );
205
206     if( strlen(c) ) {
207         double NewHeading;
208         if( scan_number( c, &NewHeading ) ) {
209             fgSetString( "/autopilot/locks/heading", "dg-heading-hold" );
210             fgSetDouble( "/autopilot/settings/heading-bug-deg", 
211                          NewHeading );
212         } else {
213             error = 1;
214             s = c;
215             s += " is not a valid number.";
216         }
217     }
218     ApHeadingDialog_Cancel(me);
219     if ( error ) mkDialog(s.c_str());
220 }
221
222 void NewHeading(puObject *cb)
223 {
224     //  string ApHeadingLabel( "Enter New Heading" );
225     //  ApHeadingDialogMessage  -> setLabel(ApHeadingLabel.c_str());
226     float heading = fgGetDouble( "/autopilot/settings/heading-bug-deg" );
227     while ( heading < 0.0 ) { heading += 360.0; }
228     ApHeadingDialogInput   ->    setValue ( heading );
229     ApHeadingDialogInput    -> acceptInput();
230     FG_PUSH_PUI_DIALOG( ApHeadingDialog );
231 }
232
233 void NewHeadingInit()
234 {
235     //  printf("NewHeadingInit\n");
236     char NewHeadingLabel[] = "Enter New Heading";
237     char *s;
238
239     float heading = fgGetDouble("/orientation/heading-deg");
240     int len = 260/2 -
241         (puGetDefaultLabelFont().getStringWidth( NewHeadingLabel ) / 2 );
242
243     ApHeadingDialog = new puDialogBox (150, 50);
244     {
245         ApHeadingDialogFrame   = new puFrame (0, 0, 260, 150);
246
247         ApHeadingDialogMessage = new puText   (len, 110);
248         ApHeadingDialogMessage    -> setDefaultValue (NewHeadingLabel);
249         ApHeadingDialogMessage    -> getDefaultValue (&s);
250         ApHeadingDialogMessage    -> setLabel        (s);
251
252         ApHeadingDialogInput   = new puInput  ( 50, 70, 210, 100 );
253         ApHeadingDialogInput   ->    setValue ( heading );
254
255         ApHeadingDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
256         ApHeadingDialogOkButton     ->     setLegend         (gui_msg_OK);
257         ApHeadingDialogOkButton     ->     makeReturnDefault (TRUE);
258         ApHeadingDialogOkButton     ->     setCallback       (ApHeadingDialog_OK);
259
260         ApHeadingDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
261         ApHeadingDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
262         ApHeadingDialogCancelButton ->     setCallback       (ApHeadingDialog_Cancel);
263
264     }
265     FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
266 }
267
268 void ApAltitudeDialog_Cancel(puObject *)
269 {
270     ApAltitudeDialogInput -> rejectInput();
271     FG_POP_PUI_DIALOG( ApAltitudeDialog );
272 }
273
274 void ApAltitudeDialog_OK (puObject *me)
275 {
276     int error = 0;
277     string s;
278     char *c;
279     ApAltitudeDialogInput->getValue( &c );
280
281     if ( strlen( c ) ) {
282         double NewAltitude;
283         if ( scan_number( c, &NewAltitude) ) {
284             fgSetString( "/autopilot/locks/altitude", "altitude-hold" );
285             fgSetDouble( "/autopilot/settings/altitude-ft", NewAltitude );
286         } else {
287             error = 1;
288             s = c;
289             s += " is not a valid number.";
290         }
291     }
292     ApAltitudeDialog_Cancel(me);
293     if( error )  mkDialog(s.c_str());
294 }
295
296 void NewAltitude(puObject *cb)
297 {
298     float altitude = fgGetDouble("/autopilot/settings/altitude-ft")
299         * SG_METER_TO_FEET;
300     ApAltitudeDialogInput -> setValue( altitude );
301     ApAltitudeDialogInput -> acceptInput();
302     FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
303 }
304
305 void NewAltitudeInit()
306 {
307     //  printf("NewAltitudeInit\n");
308     char NewAltitudeLabel[] = "Enter New Altitude";
309     char *s;
310
311     float alt = cur_fdm_state->get_Altitude();
312
313     if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
314         alt *= SG_FEET_TO_METER;
315     }
316
317     int len = 260/2 -
318         (puGetDefaultLabelFont().getStringWidth( NewAltitudeLabel ) / 2);
319
320     //  ApAltitudeDialog = new puDialogBox (150, 50);
321     ApAltitudeDialog = new puDialogBox (150, 200);
322     {
323         ApAltitudeDialogFrame   = new puFrame  (0, 0, 260, 150);
324         ApAltitudeDialogMessage = new puText   (len, 110);
325         ApAltitudeDialogMessage    -> setDefaultValue (NewAltitudeLabel);
326         ApAltitudeDialogMessage    -> getDefaultValue (&s);
327         ApAltitudeDialogMessage    -> setLabel (s);
328
329         ApAltitudeDialogInput   = new puInput  ( 50, 70, 210, 100 );
330         ApAltitudeDialogInput      -> setValue ( alt );
331         // Uncomment the next line to have input active on startup
332         // ApAltitudeDialogInput   ->    acceptInput       ( );
333         // cursor at begining or end of line ?
334         //len = strlen(s);
335         //              len = 0;
336         //              ApAltitudeDialogInput   ->    setCursor         ( len );
337         //              ApAltitudeDialogInput   ->    setSelectRegion   ( 5, 9 );
338
339         ApAltitudeDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
340         ApAltitudeDialogOkButton     ->     setLegend         (gui_msg_OK);
341         ApAltitudeDialogOkButton     ->     makeReturnDefault (TRUE);
342         ApAltitudeDialogOkButton     ->     setCallback       (ApAltitudeDialog_OK);
343
344         ApAltitudeDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
345         ApAltitudeDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
346         ApAltitudeDialogCancelButton ->     setCallback       (ApAltitudeDialog_Cancel);
347
348     }
349     FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
350 }
351
352
353 static void maxroll_adj( puObject *hs ) {
354     float val ;
355     
356     hs-> getValue ( &val ) ;
357     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
358     //    printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
359     fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust * val );
360     sprintf( SliderText[ 0 ], "%05.2f",
361              fgGetDouble("/autopilot/config/max-roll-deg") );
362     APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
363 }
364
365 static void rollout_adj( puObject *hs ) {
366     float val ;
367
368     hs-> getValue ( &val ) ;
369     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
370     //    printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
371     fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust * val );
372     sprintf( SliderText[ 1 ], "%05.2f",
373              fgGetDouble("/autopilot/config/roll-out-deg") );
374     APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
375 }
376
377 static void maxaileron_adj( puObject *hs ) {
378     float val ;
379
380     hs-> getValue ( &val ) ;
381     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
382     //    printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
383     fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust * val );
384     sprintf( SliderText[ 3 ], "%05.2f",
385              fgGetDouble("/autopilot/config/max-aileron") );
386     APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
387 }
388
389 static void rolloutsmooth_adj( puObject *hs ) {
390     float val ;
391
392     hs -> getValue ( &val ) ;
393     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
394     //    printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
395     fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
396                  RollOutSmoothAdjust * val );
397     sprintf( SliderText[ 2 ], "%5.2f",
398              fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
399     APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
400
401 }
402
403 static void goAwayAPAdjust (puObject *)
404 {
405     FG_POP_PUI_DIALOG( APAdjustDialog );
406 }
407
408 void cancelAPAdjust( puObject *self ) {
409     fgSetDouble( "/autopilot/config/max-roll-deg", TmpMaxRollValue );
410     fgSetDouble( "/autopilot/config/roll-out-deg", TmpRollOutValue );
411     fgSetDouble( "/autopilot/config/max-aileron", TmpMaxAileronValue );
412     fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
413                  TmpRollOutSmoothValue );
414
415     goAwayAPAdjust(self);
416 }
417
418 void resetAPAdjust( puObject *self ) {
419     fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust / 2 );
420     fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust / 2 );
421     fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust / 2 );
422     fgSetDouble( "/autopilot/config/roll-out-smooth-deg", 
423                  RollOutSmoothAdjust / 2 );
424
425     FG_POP_PUI_DIALOG( APAdjustDialog );
426
427     fgAPAdjust( self );
428 }
429
430 void fgAPAdjust( puObject *self ) {
431     TmpMaxRollValue       = fgGetDouble("/autopilot/config/max-roll-deg");
432     TmpRollOutValue       = fgGetDouble("/autopilot/config/roll-out-deg");
433     TmpMaxAileronValue    = fgGetDouble("/autopilot/config/max-aileron");
434     TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
435
436     MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
437         / MaxRollAdjust;
438     RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
439         / RollOutAdjust;
440     MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
441         / MaxAileronAdjust;
442     RollOutSmoothValue =  fgGetDouble("/autopilot/config/roll-out-smooth-deg")
443         / RollOutSmoothAdjust;
444
445     APAdjustHS0-> setValue ( MaxRollValue ) ;
446     APAdjustHS1-> setValue ( RollOutValue ) ;
447     APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
448     APAdjustHS3-> setValue ( MaxAileronValue ) ;
449
450     FG_PUSH_PUI_DIALOG( APAdjustDialog );
451 }
452
453 // Done once at system initialization
454 void fgAPAdjustInit() {
455
456     //  printf("fgAPAdjustInit\n");
457 #define HORIZONTAL  FALSE
458
459     int DialogX = 40;
460     int DialogY = 100;
461     int DialogWidth = 230;
462
463     char Label[] =  "AutoPilot Adjust";
464     char *s;
465
466     int labelX = (DialogWidth / 2) -
467         (puGetDefaultLabelFont().getStringWidth( Label ) / 2);
468     labelX -= 30;  // KLUDGEY
469
470     int nSliders = 4;
471     int slider_x = 10;
472     int slider_y = 55;
473     int slider_width = 210;
474     int slider_title_x = 15;
475     int slider_value_x = 160;
476     float slider_delta = 0.1f;
477
478     TmpMaxRollValue       = fgGetDouble("/autopilot/config/max-roll-deg");
479     TmpRollOutValue       = fgGetDouble("/autopilot/config/roll-out-deg");
480     TmpMaxAileronValue    = fgGetDouble("/autopilot/config/max-aileron");
481     TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
482     MaxRollAdjust = 2 * fgGetDouble("/autopilot/config/max-roll-deg");
483     RollOutAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-deg");
484     MaxAileronAdjust = 2 * fgGetDouble("/autopilot/config/max-aileron");
485     RollOutSmoothAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-smooth-deg");
486
487     MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
488         / MaxRollAdjust;
489     RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
490         / RollOutAdjust;
491     MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
492         / MaxAileronAdjust;
493     RollOutSmoothValue =  fgGetDouble("/autopilot/config/roll-out-smooth-deg")
494         / RollOutSmoothAdjust;
495
496     puGetDefaultFonts (  &APAdjustLegendFont,  &APAdjustLabelFont );
497     APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
498         int horiz_slider_height = APAdjustLabelFont.getStringHeight() +
499             APAdjustLabelFont.getStringDescender() +
500             PUSTR_TGAP + PUSTR_BGAP + 5;
501
502         APAdjustFrame = new puFrame ( 0, 0,
503                                       DialogWidth,
504                                       85 + nSliders * horiz_slider_height );
505
506         APAdjustDialogMessage = new puText ( labelX,
507                                              52 + nSliders
508                                              * horiz_slider_height );
509         APAdjustDialogMessage -> setDefaultValue ( Label );
510         APAdjustDialogMessage -> getDefaultValue ( &s );
511         APAdjustDialogMessage -> setLabel        ( s );
512
513         APAdjustHS0 = new mySlider ( slider_x, slider_y,
514                                      slider_width, HORIZONTAL ) ;
515         APAdjustHS0-> setDelta ( slider_delta ) ;
516         APAdjustHS0-> setValue ( MaxRollValue ) ;
517         APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
518         APAdjustHS0-> setCallback ( maxroll_adj ) ;
519
520         sprintf( SliderText[ 0 ], "%05.2f",
521                  fgGetDouble("/autopilot/config/max-roll-deg") );
522         APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
523         APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
524         APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
525         APAdjustMaxRollTitle-> setLabel ( s ) ;
526         APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
527         APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
528
529         slider_y += horiz_slider_height;
530
531         APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width,
532                                      HORIZONTAL ) ;
533         APAdjustHS1-> setDelta ( slider_delta ) ;
534         APAdjustHS1-> setValue ( RollOutValue ) ;
535         APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
536         APAdjustHS1-> setCallback ( rollout_adj ) ;
537
538         sprintf( SliderText[ 1 ], "%05.2f",
539                  fgGetDouble("/autopilot/config/roll-out-deg") );
540         APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
541         APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
542         APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
543         APAdjustRollOutTitle-> setLabel ( s ) ;
544         APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
545         APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
546
547         slider_y += horiz_slider_height;
548
549         APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width,
550                                      HORIZONTAL ) ;
551         APAdjustHS2-> setDelta ( slider_delta ) ;
552         APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
553         APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
554         APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
555
556         sprintf( SliderText[ 2 ], "%5.2f", 
557                  fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
558         APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
559         APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
560         APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
561         APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
562         APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
563         APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
564
565         slider_y += horiz_slider_height;
566
567         APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width,
568                                      HORIZONTAL ) ;
569         APAdjustHS3-> setDelta ( slider_delta ) ;
570         APAdjustHS3-> setValue ( MaxAileronValue ) ;
571         APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
572         APAdjustHS3-> setCallback ( maxaileron_adj ) ;
573
574         sprintf( SliderText[ 3 ], "%05.2f", 
575                  fgGetDouble("/autopilot/config/max-aileron") );
576         APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
577         APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
578         APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
579         APAdjustMaxAileronTitle-> setLabel ( s ) ;
580         APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
581         APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
582
583         APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
584         APAdjustOkButton-> setLegend ( gui_msg_OK );
585         APAdjustOkButton-> makeReturnDefault ( TRUE );
586         APAdjustOkButton-> setCallback ( goAwayAPAdjust );
587
588         APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
589         APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
590         APAdjustCancelButton-> setCallback ( cancelAPAdjust );
591
592         APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
593         APAdjustResetButton-> setLegend ( gui_msg_RESET );
594         APAdjustResetButton-> setCallback ( resetAPAdjust );
595     }
596     FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
597
598 #undef HORIZONTAL
599 }
600
601 // Simple Dialog to input Target Airport
602 void TgtAptDialog_Cancel(puObject *)
603 {
604     FG_POP_PUI_DIALOG( TgtAptDialog );
605 }
606
607 void TgtAptDialog_OK (puObject *)
608 {
609     string TgtAptId;
610     
611     //    FGTime *t = FGTime::cur_time_params;
612     //    int PauseMode = t->getPause();
613     //    if(!PauseMode)
614     //        t->togglePauseMode();
615     
616     char *s;
617     TgtAptDialogInput->getValue(&s);
618
619     string tmp = s;
620     unsigned int pos = tmp.find( "@" );
621     if ( pos != string::npos ) {
622         TgtAptId = tmp.substr( 0, pos );
623     } else {
624         TgtAptId = tmp;
625     }
626
627     TgtAptDialog_Cancel( NULL );
628     
629     /* s = input string, either 'FIX' or FIX@4000' */
630     /* TgtAptId is name of fix only; may get appended to below */
631
632     if ( NewWaypoint( TgtAptId ) == 0)
633     {
634         TgtAptId  += " not in database.";
635         mkDialog(TgtAptId.c_str());
636     }
637 }
638
639 /* add new waypoint (either from above popup window 'ok button or telnet session) */
640
641 int NewWaypoint( string Tgt_Alt )
642 {
643   string TgtAptId;
644   FGAirport a;
645   FGFix f;
646
647   double alt = 0.0;
648   unsigned int pos = Tgt_Alt.find( "@" );
649   if ( pos != string::npos ) {
650     TgtAptId = Tgt_Alt.substr( 0, pos );
651     string alt_str = Tgt_Alt.substr( pos + 1 );
652     alt = atof( alt_str.c_str() );
653     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
654       alt *= SG_FEET_TO_METER;
655     }
656   } else {
657     TgtAptId = Tgt_Alt;
658   }
659
660   FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
661
662   if ( fgFindAirportID( TgtAptId, &a ) ) {
663
664       SG_LOG( SG_GENERAL, SG_INFO,
665               "Adding waypoint (airport) = " << TgtAptId );
666
667       sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
668
669       SGWayPoint wp( a.longitude, a.latitude, alt,
670                      SGWayPoint::WGS84, TgtAptId );
671       rm->add_waypoint( wp );
672
673       /* and turn on the autopilot */
674       fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
675
676       return 1;
677
678   } else if ( current_fixlist->query( TgtAptId, &f ) ) {
679       SG_LOG( SG_GENERAL, SG_INFO,
680               "Adding waypoint (fix) = " << TgtAptId );
681
682       sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
683
684       SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
685                      SGWayPoint::WGS84, TgtAptId );
686       rm->add_waypoint( wp );
687
688       /* and turn on the autopilot */
689       fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
690       return 2;
691   } else {
692       return 0;
693   }
694 }
695
696
697 void TgtAptDialog_Reset(puObject *)
698 {
699     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
700     TgtAptDialogInput->setValue ( NewTgtAirportId );
701     TgtAptDialogInput->setCursor( 0 ) ;
702 }
703
704 void TgtAptDialog_HandleSlider ( puObject * slider )
705 {
706   float val ;
707   slider -> getValue ( &val ) ;
708   val = 1.0f - val ;
709
710   int index = int ( TgtAptDialogWPList -> getNumItems () * val ) ;
711   TgtAptDialogWPList -> setTopItem ( index ) ;
712 }
713
714 void TgtAptDialog_HandleArrow( puObject *arrow )
715 {
716     int type = ((puArrowButton *)arrow)->getArrowType() ;
717     int inc = ( type == PUARROW_DOWN     ) ?   1 :
718             ( type == PUARROW_UP       ) ?  -1 :
719             ( type == PUARROW_FASTDOWN ) ?  10 :
720             ( type == PUARROW_FASTUP   ) ? -10 : 0 ;
721
722     float val ;
723     TgtAptDialogSlider -> getValue ( &val ) ;
724     val = 1.0f - val ;
725     int num_items = TgtAptDialogWPList->getNumItems () - 1 ;
726     if ( num_items > 0 )
727     {
728       int index = int ( num_items * val + 0.5 ) + inc ;
729       if ( index > num_items ) index = num_items ;
730       if ( index < 0 ) index = 0 ;
731
732       TgtAptDialogSlider -> setValue ( 1.0f - (float)index / num_items ) ;
733       TgtAptDialogWPList -> setTopItem ( index ) ;
734   }
735
736 }
737
738 void AddWayPoint(puObject *cb)
739 {
740     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
741     TgtAptDialogInput->setValue( NewTgtAirportId );
742     
743     /* refresh waypoint list */
744     char WPString[100];
745
746     int i;
747     if ( WPList != NULL ) {
748         for (i = 0; i < WPListsize; i++ ) {
749           delete WPList[i];
750         }
751         delete [] WPList[i];
752     }
753     FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
754     WPListsize = rm->size();
755     if ( WPListsize > 0 ) { 
756         WPList = new char* [ WPListsize + 1 ];
757         for (i = 0; i < WPListsize; i++ ) {
758             SGWayPoint wp = rm->get_waypoint(i);
759             sprintf( WPString, "%5s %3.2flon %3.2flat",
760                      wp.get_id().c_str(),
761                      wp.get_target_lon(),
762                      wp.get_target_lat() );
763            WPList [i] = new char[ strlen(WPString)+1 ];
764            strcpy ( WPList [i], WPString );
765         }
766     } else {
767         WPListsize = 1;
768         WPList = new char* [ 2 ];
769         WPList [0] = new char[18];
770         strcpy ( WPList [0], "** List Empty **");
771     }    
772     WPList [ WPListsize ] = NULL;
773     TgtAptDialogWPList->newList( WPList );
774
775    // if non-empty list, adjust the size of the slider...
776    TgtAptDialogSlider->setSliderFraction (0.9999f) ;
777    TgtAptDialogSlider->hide();
778    TgtAptDialogUPArrow->hide();
779    TgtAptDialogDNArrow->hide();
780    if (WPListsize > 10) {
781       TgtAptDialogSlider->setSliderFraction (10.0f/(WPListsize-1)) ;
782       TgtAptDialogSlider->reveal();
783       TgtAptDialogUPArrow->reveal();
784       TgtAptDialogDNArrow->reveal();
785     }
786
787     FG_PUSH_PUI_DIALOG( TgtAptDialog );
788 }
789
790 void PopWayPoint(puObject *cb)
791 {
792     FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
793     rm->pop_waypoint();
794 }
795
796 void ClearRoute(puObject *cb)
797 {
798     FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
799     rm->init();
800 }
801
802 void NewTgtAirportInit()
803 {
804     SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
805     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
806     SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
807     
808     TgtAptDialog = new puDialogBox (150, 350);
809     {
810         TgtAptDialogFrame   = new puFrame           (0,0,350, 350);
811         
812         TgtAptDialogWPList = new puListBox ( 50, 130, 300, 320 ) ;
813         TgtAptDialogWPList -> setLabel ( "Flight Plan" );
814         TgtAptDialogWPList -> setLabelPlace ( PUPLACE_ABOVE ) ;
815         TgtAptDialogWPList -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
816         TgtAptDialogWPList -> setValue ( 0 ) ;
817
818         TgtAptDialogSlider = new puSlider (300, 150, 150 ,TRUE,20);
819         TgtAptDialogSlider->setValue(1.0f);
820         TgtAptDialogSlider->setSliderFraction (0.2f) ;
821         TgtAptDialogSlider->setDelta(0.1f);
822         TgtAptDialogSlider->setCBMode( PUSLIDER_DELTA );
823         TgtAptDialogSlider->setCallback( TgtAptDialog_HandleSlider );
824
825         TgtAptDialogUPArrow = new puArrowButton ( 300, 300, 320, 320, PUARROW_UP ) ;
826         TgtAptDialogUPArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
827
828         TgtAptDialogDNArrow = new puArrowButton ( 300, 130, 320, 150, PUARROW_DOWN ) ;
829         TgtAptDialogDNArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
830
831
832         TgtAptDialogInput   = new puInput           (50, 70, 300, 100);
833         TgtAptDialogInput -> setLabel ( NewTgtAirportLabel );
834         TgtAptDialogInput -> setLabelPlace ( PUPLACE_ABOVE ) ;
835         TgtAptDialogInput   ->    setValue          (NewTgtAirportId);
836         TgtAptDialogInput   ->    acceptInput();
837         
838         TgtAptDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
839         TgtAptDialogOkButton     ->     setLegend   (gui_msg_OK);
840         TgtAptDialogOkButton     ->     setCallback (TgtAptDialog_OK);
841         TgtAptDialogOkButton     ->     makeReturnDefault(TRUE);
842         
843         TgtAptDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
844         TgtAptDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
845         TgtAptDialogCancelButton ->     setCallback (TgtAptDialog_Cancel);
846         
847         TgtAptDialogResetButton  =  new puOneShot   (240, 10, 300, 50);
848         TgtAptDialogResetButton  ->     setLegend   (gui_msg_RESET);
849         TgtAptDialogResetButton  ->     setCallback (TgtAptDialog_Reset);
850
851     }
852
853     FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
854     SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");
855 }
856
857
858