]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/auto_gui.cxx
The code to find the highest hit below you didn't work quite right when
[flightgear.git] / src / Autopilot / auto_gui.cxx
index 56943208a6439b671c9b449057cdf0ada3fa904c..51363288ceb0106b145950c815c9b23f1c8abcbe 100644 (file)
 #  include <config.h>
 #endif
 
+#include <simgear/compiler.h>
+
+#include <simgear/route/route.hxx>
+
 #include <assert.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include STL_STRING
 
+#include <Aircraft/aircraft.hxx>
+#include <FDM/flight.hxx>
+#include <Controls/controls.hxx>
 #include <Scenery/scenery.hxx>
 
 #include <simgear/constants.h>
+#include <simgear/sg_inlines.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
-#include <simgear/misc/fgpath.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include <Airports/simple.hxx>
 #include <GUI/gui.h>
-#include <Main/bfi.hxx>
 #include <Main/fg_init.hxx>
-#include <Main/options.hxx>
+#include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
+#include <Navaids/fixlist.hxx>
 
 #include "auto_gui.hxx"
 #include "newauto.hxx"
 
+SG_USING_STD(string);
+
 
 #define mySlider puSlider
 
@@ -128,9 +142,15 @@ static puDialogBox     *TgtAptDialog = 0;
 static puFrame         *TgtAptDialogFrame = 0;
 static puText          *TgtAptDialogMessage = 0;
 static puInput         *TgtAptDialogInput = 0;
+static puListBox       *TgtAptDialogWPList = 0;
+static puSlider        *TgtAptDialogSlider = 0;
+static puArrowButton   *TgtAptDialogUPArrow = 0;
+static puArrowButton   *TgtAptDialogDNArrow = 0;
+static char**          WPList;
+static int             WPListsize;
 
 static char NewTgtAirportId[16];
-static char NewTgtAirportLabel[] = "Enter New TgtAirport ID"; 
+static char NewTgtAirportLabel[] = "New Apt/Fix ID";
 
 static puOneShot       *TgtAptDialogOkButton = 0;
 static puOneShot       *TgtAptDialogCancelButton = 0;
@@ -147,7 +167,6 @@ static int scan_number(char *s, double *new_value)
     char WordBuf[64];
     char *cptr = s;
     char *WordBufPtr = WordBuf;
-
     if (*cptr == '+')
        cptr++;
     if (*cptr == '-') {
@@ -189,10 +208,10 @@ void ApHeadingDialog_OK (puObject *me)
        double NewHeading;
        if( scan_number( c, &NewHeading ) )
            {
-               if ( !current_autopilot->get_HeadingEnabled() ) {
-                   current_autopilot->set_HeadingEnabled( true );
+               if ( !globals->get_autopilot()->get_HeadingEnabled() ) {
+                   globals->get_autopilot()->set_HeadingEnabled( true );
                }
-               current_autopilot->HeadingSet( NewHeading );
+               globals->get_autopilot()->HeadingSet( NewHeading );
            } else {
                error = 1;
                s = c;
@@ -207,19 +226,22 @@ void NewHeading(puObject *cb)
 {
     // string ApHeadingLabel( "Enter New Heading" );
     // ApHeadingDialogMessage  -> setLabel(ApHeadingLabel.c_str());
+    float heading = globals->get_autopilot()->get_DGTargetHeading();
+    while ( heading < 0.0 ) { heading += 360.0; }
+    ApHeadingDialogInput   ->    setValue ( heading );
     ApHeadingDialogInput    -> acceptInput();
     FG_PUSH_PUI_DIALOG( ApHeadingDialog );
 }
 
-void NewHeadingInit(void)
+void NewHeadingInit()
 {
     // printf("NewHeadingInit\n");
     char NewHeadingLabel[] = "Enter New Heading";
     char *s;
 
-    float heading = FGBFI::getHeading();
+    float heading = fgGetDouble("/orientation/heading-deg");
     int len = 260/2 -
-       (puGetStringWidth( puGetDefaultLabelFont(), NewHeadingLabel ) /2 );
+       (puGetDefaultLabelFont().getStringWidth( NewHeadingLabel ) / 2 );
 
     ApHeadingDialog = new puDialogBox (150, 50);
     {
@@ -263,10 +285,10 @@ void ApAltitudeDialog_OK (puObject *me)
        double NewAltitude;
        if( scan_number( c, &NewAltitude) )
            {
-               if ( !current_autopilot->get_AltitudeEnabled() ) {
-                   current_autopilot->set_AltitudeEnabled( true );
+               if ( !globals->get_autopilot()->get_AltitudeEnabled() ) {
+                   globals->get_autopilot()->set_AltitudeEnabled( true );
                }
-               current_autopilot->AltitudeSet( NewAltitude );
+               globals->get_autopilot()->AltitudeSet( NewAltitude );
            } else {
                error = 1;
                s = c;
@@ -279,11 +301,13 @@ void ApAltitudeDialog_OK (puObject *me)
 
 void NewAltitude(puObject *cb)
 {
+    float altitude = globals->get_autopilot()->get_TargetAltitude() * SG_METER_TO_FEET;
+    ApAltitudeDialogInput -> setValue( altitude );
     ApAltitudeDialogInput -> acceptInput();
     FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
 }
 
-void NewAltitudeInit(void)
+void NewAltitudeInit()
 {
     // printf("NewAltitudeInit\n");
     char NewAltitudeLabel[] = "Enter New Altitude";
@@ -291,12 +315,12 @@ void NewAltitudeInit(void)
 
     float alt = cur_fdm_state->get_Altitude();
 
-    if ( current_options.get_units() == fgOPTIONS::FG_UNITS_METERS) {
-       alt *= FEET_TO_METER;
+    if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
+       alt *= SG_FEET_TO_METER;
     }
 
     int len = 260/2 -
-       (puGetStringWidth( puGetDefaultLabelFont(), NewAltitudeLabel )/2);
+       (puGetDefaultLabelFont().getStringWidth( NewAltitudeLabel ) / 2);
 
     // ApAltitudeDialog = new puDialogBox (150, 50);
     ApAltitudeDialog = new puDialogBox (150, 200);
@@ -330,18 +354,15 @@ void NewAltitudeInit(void)
     FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
 }
 
-/////// simple AutoPilot GAIN / LIMITS ADJUSTER
-
-#define fgAP_CLAMP(val,min,max) ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
 
 static void maxroll_adj( puObject *hs ) {
     float val ;
     
     hs-> getValue ( &val ) ;
-    fgAP_CLAMP ( val, 0.1, 1.0 ) ;
+    SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
-    current_autopilot->set_MaxRoll( MaxRollAdjust * val );
-    sprintf( SliderText[ 0 ], "%05.2f", current_autopilot->get_MaxRoll() );
+    globals->get_autopilot()->set_MaxRoll( MaxRollAdjust * val );
+    sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
     APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
 }
 
@@ -349,10 +370,10 @@ static void rollout_adj( puObject *hs ) {
     float val ;
 
     hs-> getValue ( &val ) ;
-    fgAP_CLAMP ( val, 0.1, 1.0 ) ;
+    SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
-    current_autopilot->set_RollOut( RollOutAdjust * val );
-    sprintf( SliderText[ 1 ], "%05.2f", current_autopilot->get_RollOut() );
+    globals->get_autopilot()->set_RollOut( RollOutAdjust * val );
+    sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
     APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
 }
 
@@ -360,10 +381,10 @@ static void maxaileron_adj( puObject *hs ) {
     float val ;
 
     hs-> getValue ( &val ) ;
-    fgAP_CLAMP ( val, 0.1, 1.0 ) ;
+    SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
-    current_autopilot->set_MaxAileron( MaxAileronAdjust * val );
-    sprintf( SliderText[ 3 ], "%05.2f", current_autopilot->get_MaxAileron() );
+    globals->get_autopilot()->set_MaxAileron( MaxAileronAdjust * val );
+    sprintf( SliderText[ 3 ], "%05.2f", globals->get_autopilot()->get_MaxAileron() );
     APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
 }
 
@@ -371,10 +392,10 @@ static void rolloutsmooth_adj( puObject *hs ) {
     float val ;
 
     hs -> getValue ( &val ) ;
-    fgAP_CLAMP ( val, 0.1, 1.0 ) ;
+    SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
-    current_autopilot->set_RollOutSmooth( RollOutSmoothAdjust * val );
-    sprintf( SliderText[ 2 ], "%5.2f", current_autopilot->get_RollOutSmooth() );
+    globals->get_autopilot()->set_RollOutSmooth( RollOutSmoothAdjust * val );
+    sprintf( SliderText[ 2 ], "%5.2f", globals->get_autopilot()->get_RollOutSmooth() );
     APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
 
 }
@@ -385,35 +406,35 @@ static void goAwayAPAdjust (puObject *)
 }
 
 void cancelAPAdjust( puObject *self ) {
-    current_autopilot->set_MaxRoll( TmpMaxRollValue );
-    current_autopilot->set_RollOut( TmpRollOutValue );
-    current_autopilot->set_MaxAileron( TmpMaxAileronValue );
-    current_autopilot->set_RollOutSmooth( TmpRollOutSmoothValue );
+    globals->get_autopilot()->set_MaxRoll( TmpMaxRollValue );
+    globals->get_autopilot()->set_RollOut( TmpRollOutValue );
+    globals->get_autopilot()->set_MaxAileron( TmpMaxAileronValue );
+    globals->get_autopilot()->set_RollOutSmooth( TmpRollOutSmoothValue );
 
     goAwayAPAdjust(self);
 }
 
 void resetAPAdjust( puObject *self ) {
-    current_autopilot->set_MaxRoll( MaxRollAdjust / 2 );
-    current_autopilot->set_RollOut( RollOutAdjust / 2 );
-    current_autopilot->set_MaxAileron( MaxAileronAdjust / 2 );
-    current_autopilot->set_RollOutSmooth( RollOutSmoothAdjust / 2 );
+    globals->get_autopilot()->set_MaxRoll( MaxRollAdjust / 2 );
+    globals->get_autopilot()->set_RollOut( RollOutAdjust / 2 );
+    globals->get_autopilot()->set_MaxAileron( MaxAileronAdjust / 2 );
+    globals->get_autopilot()->set_RollOutSmooth( RollOutSmoothAdjust / 2 );
 
     FG_POP_PUI_DIALOG( APAdjustDialog );
 
     fgAPAdjust( self );
 }
 
-void fgAPAdjust( puObject * ) {
-    TmpMaxRollValue       = current_autopilot->get_MaxRoll();
-    TmpRollOutValue       = current_autopilot->get_RollOut();
-    TmpMaxAileronValue    = current_autopilot->get_MaxAileron();
-    TmpRollOutSmoothValue = current_autopilot->get_RollOutSmooth();
+void fgAPAdjust( puObject *self ) {
+    TmpMaxRollValue       = globals->get_autopilot()->get_MaxRoll();
+    TmpRollOutValue       = globals->get_autopilot()->get_RollOut();
+    TmpMaxAileronValue    = globals->get_autopilot()->get_MaxAileron();
+    TmpRollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth();
 
-    MaxRollValue       = current_autopilot->get_MaxRoll() / MaxRollAdjust;
-    RollOutValue       = current_autopilot->get_RollOut() / RollOutAdjust;
-    MaxAileronValue    = current_autopilot->get_MaxAileron() / MaxAileronAdjust;
-    RollOutSmoothValue = current_autopilot->get_RollOutSmooth()
+    MaxRollValue       = globals->get_autopilot()->get_MaxRoll() / MaxRollAdjust;
+    RollOutValue       = globals->get_autopilot()->get_RollOut() / RollOutAdjust;
+    MaxAileronValue    = globals->get_autopilot()->get_MaxAileron() / MaxAileronAdjust;
+    RollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth()
        / RollOutSmoothAdjust;
 
     APAdjustHS0-> setValue ( MaxRollValue ) ;
@@ -425,7 +446,7 @@ void fgAPAdjust( puObject * ) {
 }
 
 // Done once at system initialization
-void fgAPAdjustInit( void ) {
+void fgAPAdjustInit() {
 
     // printf("fgAPAdjustInit\n");
 #define HORIZONTAL  FALSE
@@ -438,7 +459,7 @@ void fgAPAdjustInit( void ) {
     char *s;
 
     int labelX = (DialogWidth / 2) -
-       (puGetStringWidth( puGetDefaultLabelFont(), Label ) / 2);
+       (puGetDefaultLabelFont().getStringWidth( Label ) / 2);
     labelX -= 30;  // KLUDGEY
 
     int nSliders = 4;
@@ -449,26 +470,26 @@ void fgAPAdjustInit( void ) {
     int slider_value_x = 160;
     float slider_delta = 0.1f;
 
-    TmpMaxRollValue       = current_autopilot->get_MaxRoll();
-    TmpRollOutValue       = current_autopilot->get_RollOut();
-    TmpMaxAileronValue    = current_autopilot->get_MaxAileron();
-    TmpRollOutSmoothValue = current_autopilot->get_RollOutSmooth();
+    TmpMaxRollValue       = globals->get_autopilot()->get_MaxRoll();
+    TmpRollOutValue       = globals->get_autopilot()->get_RollOut();
+    TmpMaxAileronValue    = globals->get_autopilot()->get_MaxAileron();
+    TmpRollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth();
 
-    MaxRollAdjust = 2 * current_autopilot->get_MaxRoll();
-    RollOutAdjust = 2 * current_autopilot->get_RollOut();
-    MaxAileronAdjust = 2 * current_autopilot->get_MaxAileron();
-    RollOutSmoothAdjust = 2 * current_autopilot->get_RollOutSmooth();
+    MaxRollAdjust = 2 * globals->get_autopilot()->get_MaxRoll();
+    RollOutAdjust = 2 * globals->get_autopilot()->get_RollOut();
+    MaxAileronAdjust = 2 * globals->get_autopilot()->get_MaxAileron();
+    RollOutSmoothAdjust = 2 * globals->get_autopilot()->get_RollOutSmooth();
 
-    MaxRollValue       = current_autopilot->get_MaxRoll() / MaxRollAdjust;
-    RollOutValue       = current_autopilot->get_RollOut() / RollOutAdjust;
-    MaxAileronValue    = current_autopilot->get_MaxAileron() / MaxAileronAdjust;
-    RollOutSmoothValue = current_autopilot->get_RollOutSmooth()
+    MaxRollValue       = globals->get_autopilot()->get_MaxRoll() / MaxRollAdjust;
+    RollOutValue       = globals->get_autopilot()->get_RollOut() / RollOutAdjust;
+    MaxAileronValue    = globals->get_autopilot()->get_MaxAileron() / MaxAileronAdjust;
+    RollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth()
        / RollOutSmoothAdjust;
 
     puGetDefaultFonts (  &APAdjustLegendFont,  &APAdjustLabelFont );
     APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
-       int horiz_slider_height = puGetStringHeight (APAdjustLabelFont) +
-           puGetStringDescender (APAdjustLabelFont) +
+       int horiz_slider_height = APAdjustLabelFont.getStringHeight() +
+           APAdjustLabelFont.getStringDescender() +
            PUSTR_TGAP + PUSTR_BGAP + 5;
 
        APAdjustFrame = new puFrame ( 0, 0,
@@ -489,7 +510,7 @@ void fgAPAdjustInit( void ) {
        APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
        APAdjustHS0-> setCallback ( maxroll_adj ) ;
 
-       sprintf( SliderText[ 0 ], "%05.2f", current_autopilot->get_MaxRoll() );
+       sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
        APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
        APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
@@ -506,7 +527,7 @@ void fgAPAdjustInit( void ) {
        APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
        APAdjustHS1-> setCallback ( rollout_adj ) ;
 
-       sprintf( SliderText[ 1 ], "%05.2f", current_autopilot->get_RollOut() );
+       sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
        APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
        APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
@@ -524,7 +545,7 @@ void fgAPAdjustInit( void ) {
        APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
 
        sprintf( SliderText[ 2 ], "%5.2f", 
-                current_autopilot->get_RollOutSmooth() );
+                globals->get_autopilot()->get_RollOutSmooth() );
        APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
        APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
@@ -542,7 +563,7 @@ void fgAPAdjustInit( void ) {
        APAdjustHS3-> setCallback ( maxaileron_adj ) ;
 
        sprintf( SliderText[ 3 ], "%05.2f", 
-                current_autopilot->get_MaxAileron() );
+                globals->get_autopilot()->get_MaxAileron() );
        APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
        APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
@@ -585,106 +606,231 @@ void TgtAptDialog_OK (puObject *)
     
     char *s;
     TgtAptDialogInput->getValue(&s);
-    TgtAptId = s;
-    
+
+    string tmp = s;
+    unsigned int pos = tmp.find( "@" );
+    if ( pos != string::npos ) {
+       TgtAptId = tmp.substr( 0, pos );
+    } else {
+       TgtAptId = tmp;
+    }
+
     TgtAptDialog_Cancel( NULL );
     
-    if ( TgtAptId.length() ) {
-        // set initial position from TgtAirport id
-        
-       FGPath path( current_options.get_fg_root() );
-       path.append( "Airports" );
-       path.append( "simple.mk4" );
-        FGAirports airports( path.c_str() );
-        FGAirport a;
-        
-        FG_LOG( FG_GENERAL, FG_INFO,
-                "Attempting to set starting position from airport code "
-                << s );
-        
-        if ( airports.search( TgtAptId, &a ) )
-           {
-               double course, reverse, distance;
-               //            fgAPset_tgt_airport_id( TgtAptId.c_str() );
-               current_options.set_airport_id( TgtAptId.c_str() );
-               sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
-                       
-               current_autopilot->set_TargetLatitude( a.latitude );
-               current_autopilot->set_TargetLongitude( a.longitude );
-               current_autopilot->MakeTargetLatLonStr(
-                                    current_autopilot->get_TargetLatitude(),
-                                    current_autopilot->get_TargetLongitude() );
-                       
-               current_autopilot->set_old_lat( FGBFI::getLatitude() );
-               current_autopilot->set_old_lon( FGBFI::getLongitude() );
-                       
-               // need to test for iter
-               if( ! geo_inverse_wgs_84( FGBFI::getAltitude() * FEET_TO_METER,
-                                         FGBFI::getLatitude(),
-                                         FGBFI::getLongitude(),
-                                         current_autopilot->get_TargetLatitude(),
-                                         current_autopilot->get_TargetLongitude(),
-                                         &course,
-                                         &reverse,
-                                         &distance ) ) {
-                   current_autopilot->set_TargetHeading( course );
-                   current_autopilot->MakeTargetHeadingStr(
-                                     current_autopilot->get_TargetHeading() );
-                   current_autopilot->set_TargetDistance( distance );
-                   current_autopilot->MakeTargetDistanceStr( distance );
-                   // This changes the AutoPilot Heading
-                   // following cast needed
-                   ApHeadingDialogInput->
-                       setValue((float)current_autopilot->get_TargetHeading() );
-                   // Force this !
-                   current_autopilot->set_HeadingEnabled( true );
-                   current_autopilot->set_HeadingMode(
-                                             FGAutopilot::FG_HEADING_WAYPOINT );
-               }
-           } else {
-               TgtAptId  += " not in database.";
-               mkDialog(TgtAptId.c_str());
-           }
+    /* s = input string, either 'FIX' or FIX@4000' */
+    /* TgtAptId is name of fix only; may get appended to below */
+
+    if ( NewWaypoint( TgtAptId ) == 0)
+    {
+        TgtAptId  += " not in database.";
+        mkDialog(TgtAptId.c_str());
     }
-    // get_control_values();
-    //    if( PauseMode != t->getPause() )
-    //        t->togglePauseMode();
 }
 
+/* add new waypoint (either from above popup window 'ok button or telnet session) */
+
+int NewWaypoint( string Tgt_Alt )
+{
+  string TgtAptId;
+  FGAirport a;
+  FGFix f;
+
+  double alt = 0.0;
+  unsigned int pos = Tgt_Alt.find( "@" );
+  if ( pos != string::npos ) {
+    TgtAptId = Tgt_Alt.substr( 0, pos );
+    string alt_str = Tgt_Alt.substr( pos + 1 );
+    alt = atof( alt_str.c_str() );
+    if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
+      alt *= SG_FEET_TO_METER;
+    }
+  } else {
+    TgtAptId = Tgt_Alt;
+  }
+
+  if ( fgFindAirportID( TgtAptId, &a ) ) {
+
+    SG_LOG( SG_GENERAL, SG_INFO,
+                 "Adding waypoint (airport) = " << TgtAptId );
+
+    sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+
+    SGWayPoint wp( a.longitude, a.latitude, alt,
+                        SGWayPoint::WGS84, TgtAptId );
+    globals->get_route()->add_waypoint( wp );
+
+         /* and turn on the autopilot */
+    globals->get_autopilot()->set_HeadingEnabled( true );
+    globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT
+);
+
+    return 1;
+
+  } else if ( current_fixlist->query( TgtAptId, &f ) )
+  {
+   SG_LOG( SG_GENERAL, SG_INFO,
+                 "Adding waypoint (fix) = " << TgtAptId );
+
+    sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+
+    SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
+                        SGWayPoint::WGS84, TgtAptId );
+    globals->get_route()->add_waypoint( wp );
+
+         /* and turn on the autopilot */
+   globals->get_autopilot()->set_HeadingEnabled( true );
+   globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
+   return 2;
+
+  }
+  else return 0;
+}
+
+
 void TgtAptDialog_Reset(puObject *)
 {
-    //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
-    sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
     TgtAptDialogInput->setValue ( NewTgtAirportId );
     TgtAptDialogInput->setCursor( 0 ) ;
 }
 
-void NewTgtAirport(puObject *cb)
+void TgtAptDialog_HandleSlider ( puObject * slider )
 {
-    //  strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
-    sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
+  float val ;
+  slider -> getValue ( &val ) ;
+  val = 1.0f - val ;
+
+  int index = int ( TgtAptDialogWPList -> getNumItems () * val ) ;
+  TgtAptDialogWPList -> setTopItem ( index ) ;
+}
+
+void TgtAptDialog_HandleArrow( puObject *arrow )
+{
+    int type = ((puArrowButton *)arrow)->getArrowType() ;
+    int inc = ( type == PUARROW_DOWN     ) ?   1 :
+            ( type == PUARROW_UP       ) ?  -1 :
+            ( type == PUARROW_FASTDOWN ) ?  10 :
+            ( type == PUARROW_FASTUP   ) ? -10 : 0 ;
+
+    float val ;
+    TgtAptDialogSlider -> getValue ( &val ) ;
+    val = 1.0f - val ;
+    int num_items = TgtAptDialogWPList->getNumItems () - 1 ;
+    if ( num_items > 0 )
+    {
+      int index = int ( num_items * val + 0.5 ) + inc ;
+      if ( index > num_items ) index = num_items ;
+      if ( index < 0 ) index = 0 ;
+
+      TgtAptDialogSlider -> setValue ( 1.0f - (float)index / num_items ) ;
+      TgtAptDialogWPList -> setTopItem ( index ) ;
+  }
+
+}
+
+void AddWayPoint(puObject *cb)
+{
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
     TgtAptDialogInput->setValue( NewTgtAirportId );
     
+    /* refresh waypoint list */
+    char WPString[100];
+
+    int i;
+    if ( WPList != NULL ) {
+        for (i = 0; i < WPListsize; i++ ) {
+          delete WPList[i];
+        }
+        delete [] WPList[i];
+    }
+    if ( globals->get_route()->size() > 0 ) { 
+        WPListsize = globals->get_route()->size();
+        WPList = new char* [ WPListsize + 1 ];
+        for (i = 0; i < globals->get_route()->size(); i++ ) {
+           sprintf(WPString, "%5s %3.2flon %3.2flat", globals->get_route()->get_waypoint(i).get_id().c_str(), globals->get_route()->get_waypoint(i).get_target_lon(), globals->get_route()->get_waypoint(i).get_target_lat());
+           WPList [i] = new char[ strlen(WPString)+1 ];
+           strcpy ( WPList [i], WPString );
+        }
+    } else {
+        WPListsize = 1;
+        WPList = new char* [ 2 ];
+        WPList [0] = new char[18];
+        strcpy ( WPList [0], "** List Empty **");
+    }    
+    WPList [ WPListsize ] = NULL;
+    TgtAptDialogWPList->newList( WPList );
+
+   // if non-empty list, adjust the size of the slider...
+   TgtAptDialogSlider->setSliderFraction (0.9999f) ;
+   TgtAptDialogSlider->hide();
+   TgtAptDialogUPArrow->hide();
+   TgtAptDialogDNArrow->hide();
+   if (WPListsize > 10) {
+      TgtAptDialogSlider->setSliderFraction (10.0f/(WPListsize-1)) ;
+      TgtAptDialogSlider->reveal();
+      TgtAptDialogUPArrow->reveal();
+      TgtAptDialogDNArrow->reveal();
+    }
+
     FG_PUSH_PUI_DIALOG( TgtAptDialog );
 }
 
-void NewTgtAirportInit(void)
+void PopWayPoint(puObject *cb)
 {
-    FG_LOG( FG_AUTOPILOT, FG_INFO, " enter NewTgtAirportInit()" );
-    // fgAPset_tgt_airport_id( current_options.get_airport_id() );     
-    sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
-    FG_LOG( FG_AUTOPILOT, FG_INFO, " NewTgtAirportId " << NewTgtAirportId );
-    // printf(" NewTgtAirportId %s\n", NewTgtAirportId);
-    int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
-                                      NewTgtAirportLabel ) / 2;
+    globals->get_route()->delete_first();
+
+    // see if there are more waypoints on the list
+    if ( globals->get_route()->size() ) {
+       // more waypoints
+       globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
+    } else {
+       // end of the line
+       globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_TC_HEADING_LOCK );
+
+       // use current heading
+       globals->get_autopilot()
+            ->set_TargetHeading(fgGetDouble("/orientation/heading-deg"));
+    }
+}
+
+void ClearRoute(puObject *cb)
+{
+    globals->get_route()->clear();
+}
+
+void NewTgtAirportInit()
+{
+    SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
+    SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
     
-    TgtAptDialog = new puDialogBox (150, 50);
+    TgtAptDialog = new puDialogBox (150, 350);
     {
-        TgtAptDialogFrame   = new puFrame           (0,0,350, 150);
-        TgtAptDialogMessage = new puText            (len, 110);
-        TgtAptDialogMessage ->    setLabel          (NewTgtAirportLabel);
+        TgtAptDialogFrame   = new puFrame           (0,0,350, 350);
         
+        TgtAptDialogWPList = new puListBox ( 50, 130, 300, 320 ) ;
+        TgtAptDialogWPList -> setLabel ( "Flight Plan" );
+        TgtAptDialogWPList -> setLabelPlace ( PUPLACE_ABOVE ) ;
+        TgtAptDialogWPList -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
+        TgtAptDialogWPList -> setValue ( 0 ) ;
+
+        TgtAptDialogSlider = new puSlider (300, 150, 150 ,TRUE,20);
+        TgtAptDialogSlider->setValue(1.0f);
+        TgtAptDialogSlider->setSliderFraction (0.2f) ;
+        TgtAptDialogSlider->setDelta(0.1f);
+        TgtAptDialogSlider->setCBMode( PUSLIDER_DELTA );
+        TgtAptDialogSlider->setCallback( TgtAptDialog_HandleSlider );
+
+        TgtAptDialogUPArrow = new puArrowButton ( 300, 300, 320, 320, PUARROW_UP ) ;
+        TgtAptDialogUPArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
+
+        TgtAptDialogDNArrow = new puArrowButton ( 300, 130, 320, 150, PUARROW_DOWN ) ;
+        TgtAptDialogDNArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
+
+
         TgtAptDialogInput   = new puInput           (50, 70, 300, 100);
+        TgtAptDialogInput -> setLabel ( NewTgtAirportLabel );
+        TgtAptDialogInput -> setLabelPlace ( PUPLACE_ABOVE ) ;
         TgtAptDialogInput   ->    setValue          (NewTgtAirportId);
         TgtAptDialogInput   ->    acceptInput();
         
@@ -700,7 +846,12 @@ void NewTgtAirportInit(void)
         TgtAptDialogResetButton  =  new puOneShot   (240, 10, 300, 50);
         TgtAptDialogResetButton  ->     setLegend   (gui_msg_RESET);
         TgtAptDialogResetButton  ->     setCallback (TgtAptDialog_Reset);
+
     }
+
     FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
-    printf("leave NewTgtAirportInit()");
+    SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");
 }
+
+
+