]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/auto_gui.cxx
working on the termination of the last hardcoded dialogs in Autopilot/auto_gui.cxx:
[flightgear.git] / src / Autopilot / auto_gui.cxx
index 4aca7092e27d7e1524ec39c712d0bb21bde84759..01ebce80e2f4d3595b3789ecf7380532f006e6f7 100644 (file)
@@ -1,7 +1,7 @@
 // auto_gui.cxx -- autopilot gui interface
 //
 // Written by Norman Vine <nhv@cape.com>
-// Arranged by Curt Olson <curt@flightgear.org>
+// Arranged by Curt Olson <http://www.flightgear.org/~curt>
 //
 // Copyright (C) 1998 - 2000
 //
@@ -17,7 +17,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -28,8 +28,6 @@
 
 #include <simgear/compiler.h>
 
-#include <simgear/route/route.hxx>
-
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -38,7 +36,6 @@
 
 #include <Aircraft/aircraft.hxx>
 #include <FDM/flight.hxx>
-#include <Controls/controls.hxx>
 #include <Scenery/scenery.hxx>
 
 #include <simgear/constants.h>
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Navaids/fixlist.hxx>
+#include <Navaids/navlist.hxx>
+#include <Navaids/navrecord.hxx>
 
 #include "auto_gui.hxx"
-#include "newauto.hxx"
+#include "route_mgr.hxx"
+#include "xmlauto.hxx"
 
 SG_USING_STD(string);
 
@@ -63,56 +63,56 @@ SG_USING_STD(string);
 #define mySlider puSlider
 
 // Climb speed constants
-const double min_climb = 70.0; // kts
-const double best_climb = 75.0;        // kts
-const double ideal_climb_rate = 500.0; // fpm
+// const double min_climb = 70.0;      // kts
+// const double best_climb = 75.0;     // kts
+// const double ideal_climb_rate = 500.0; // fpm
 
 /// These statics will eventually go into the class
 /// they are just here while I am experimenting -- NHV :-)
 // AutoPilot Gain Adjuster members
-static double MaxRollAdjust;        // MaxRollAdjust       = 2 * APData->MaxRoll;
-static double RollOutAdjust;        // RollOutAdjust       = 2 * APData->RollOut;
-static double MaxAileronAdjust;     // MaxAileronAdjust    = 2 * APData->MaxAileron;
-static double RollOutSmoothAdjust;  // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
-
-static float MaxRollValue;          // 0.1 -> 1.0
-static float RollOutValue;
-static float MaxAileronValue;
-static float RollOutSmoothValue;
-
-static float TmpMaxRollValue;       // for cancel operation
-static float TmpRollOutValue;
-static float TmpMaxAileronValue;
-static float TmpRollOutSmoothValue;
-
-static puDialogBox *APAdjustDialog;
-static puFrame     *APAdjustFrame;
-static puText      *APAdjustDialogMessage;
-static puFont      APAdjustLegendFont;
-static puFont      APAdjustLabelFont;
-
-static puOneShot *APAdjustOkButton;
-static puOneShot *APAdjustResetButton;
-static puOneShot *APAdjustCancelButton;
-
-//static puButton        *APAdjustDragButton;
-
-static puText *APAdjustMaxRollTitle;
-static puText *APAdjustRollOutTitle;
-static puText *APAdjustMaxAileronTitle;
-static puText *APAdjustRollOutSmoothTitle;
-
-static puText *APAdjustMaxAileronText;
-static puText *APAdjustMaxRollText;
-static puText *APAdjustRollOutText;
-static puText *APAdjustRollOutSmoothText;
-
-static mySlider *APAdjustHS0;
-static mySlider *APAdjustHS1;
-static mySlider *APAdjustHS2;
-static mySlider *APAdjustHS3;
-
-static char SliderText[ 4 ][ 8 ];
+// static double MaxRollAdjust;        // MaxRollAdjust       = 2 * APData->MaxRoll;
+// static double RollOutAdjust;        // RollOutAdjust       = 2 * APData->RollOut;
+// static double MaxAileronAdjust;     // MaxAileronAdjust    = 2 * APData->MaxAileron;
+// static double RollOutSmoothAdjust;  // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
+
+// static float MaxRollValue;          // 0.1 -> 1.0
+// static float RollOutValue;
+// static float MaxAileronValue;
+// static float RollOutSmoothValue;
+
+// static float TmpMaxRollValue;       // for cancel operation
+// static float TmpRollOutValue;
+// static float TmpMaxAileronValue;
+// static float TmpRollOutSmoothValue;
+
+// static puDialogBox *APAdjustDialog;
+// static puFrame     *APAdjustFrame;
+// static puText      *APAdjustDialogMessage;
+// static puFont      APAdjustLegendFont;
+// static puFont      APAdjustLabelFont;
+
+// static puOneShot *APAdjustOkButton;
+// static puOneShot *APAdjustResetButton;
+// static puOneShot *APAdjustCancelButton;
+
+// static puButton        *APAdjustDragButton;
+
+// static puText *APAdjustMaxRollTitle;
+// static puText *APAdjustRollOutTitle;
+// static puText *APAdjustMaxAileronTitle;
+// static puText *APAdjustRollOutSmoothTitle;
+
+// static puText *APAdjustMaxAileronText;
+// static puText *APAdjustMaxRollText;
+// static puText *APAdjustRollOutText;
+// static puText *APAdjustRollOutSmoothText;
+
+// static mySlider *APAdjustHS0;
+// static mySlider *APAdjustHS1;
+// static mySlider *APAdjustHS2;
+// static mySlider *APAdjustHS3;
+
+// static char SliderText[ 4 ][ 8 ];
 
 ///////// AutoPilot New Heading Dialog
 
@@ -140,7 +140,7 @@ static puOneShot       *ApAltitudeDialogCancelButton = 0;
 //  These statics should disapear when this is a class
 static puDialogBox     *TgtAptDialog = 0;
 static puFrame         *TgtAptDialogFrame = 0;
-static puText          *TgtAptDialogMessage = 0;
+// static puText          *TgtAptDialogMessage = 0;
 static puInput         *TgtAptDialogInput = 0;
 static puListBox       *TgtAptDialogWPList = 0;
 static puSlider        *TgtAptDialogSlider = 0;
@@ -206,27 +206,25 @@ void ApHeadingDialog_OK (puObject *me)
 
     if( strlen(c) ) {
        double NewHeading;
-       if( scan_number( c, &NewHeading ) )
-           {
-               if ( !globals->get_autopilot()->get_HeadingEnabled() ) {
-                   globals->get_autopilot()->set_HeadingEnabled( true );
-               }
-               globals->get_autopilot()->HeadingSet( NewHeading );
-           } else {
-               error = 1;
-               s = c;
-               s += " is not a valid number.";
-           }
+       if( scan_number( c, &NewHeading ) ) {
+            fgSetString( "/autopilot/locks/heading", "dg-heading-hold" );
+            fgSetDouble( "/autopilot/settings/heading-bug-deg", 
+                         NewHeading );
+        } else {
+            error = 1;
+            s = c;
+            s += " is not a valid number.";
+        }
     }
     ApHeadingDialog_Cancel(me);
-    if( error )  mkDialog(s.c_str());
+    if ( error ) mkDialog(s.c_str());
 }
 
 void NewHeading(puObject *cb)
 {
     // string ApHeadingLabel( "Enter New Heading" );
     // ApHeadingDialogMessage  -> setLabel(ApHeadingLabel.c_str());
-    float heading = globals->get_autopilot()->get_DGTargetHeading();
+    float heading = fgGetDouble( "/autopilot/settings/heading-bug-deg" );
     while ( heading < 0.0 ) { heading += 360.0; }
     ApHeadingDialogInput   ->    setValue ( heading );
     ApHeadingDialogInput    -> acceptInput();
@@ -281,19 +279,16 @@ void ApAltitudeDialog_OK (puObject *me)
     char *c;
     ApAltitudeDialogInput->getValue( &c );
 
-    if( strlen( c ) ) {
+    if ( strlen( c ) ) {
        double NewAltitude;
-       if( scan_number( c, &NewAltitude) )
-           {
-               if ( !globals->get_autopilot()->get_AltitudeEnabled() ) {
-                   globals->get_autopilot()->set_AltitudeEnabled( true );
-               }
-               globals->get_autopilot()->AltitudeSet( NewAltitude );
-           } else {
-               error = 1;
-               s = c;
-               s += " is not a valid number.";
-           }
+       if ( scan_number( c, &NewAltitude) ) {
+            fgSetString( "/autopilot/locks/altitude", "altitude-hold" );
+            fgSetDouble( "/autopilot/settings/altitude-ft", NewAltitude );
+        } else {
+            error = 1;
+            s = c;
+            s += " is not a valid number.";
+        }
     }
     ApAltitudeDialog_Cancel(me);
     if( error )  mkDialog(s.c_str());
@@ -301,7 +296,8 @@ void ApAltitudeDialog_OK (puObject *me)
 
 void NewAltitude(puObject *cb)
 {
-    float altitude = globals->get_autopilot()->get_TargetAltitude() * SG_METER_TO_FEET;
+    float altitude = fgGetDouble("/autopilot/settings/altitude-ft")
+        * SG_METER_TO_FEET;
     ApAltitudeDialogInput -> setValue( altitude );
     ApAltitudeDialogInput -> acceptInput();
     FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
@@ -355,14 +351,16 @@ void NewAltitudeInit()
 }
 
 
+#if 0
 static void maxroll_adj( puObject *hs ) {
     float val ;
     
     hs-> getValue ( &val ) ;
     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
-    globals->get_autopilot()->set_MaxRoll( MaxRollAdjust * val );
-    sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
+    fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust * val );
+    sprintf( SliderText[ 0 ], "%05.2f",
+             fgGetDouble("/autopilot/config/max-roll-deg") );
     APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
 }
 
@@ -372,8 +370,9 @@ static void rollout_adj( puObject *hs ) {
     hs-> getValue ( &val ) ;
     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
-    globals->get_autopilot()->set_RollOut( RollOutAdjust * val );
-    sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
+    fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust * val );
+    sprintf( SliderText[ 1 ], "%05.2f",
+             fgGetDouble("/autopilot/config/roll-out-deg") );
     APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
 }
 
@@ -383,8 +382,9 @@ static void maxaileron_adj( puObject *hs ) {
     hs-> getValue ( &val ) ;
     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
-    globals->get_autopilot()->set_MaxAileron( MaxAileronAdjust * val );
-    sprintf( SliderText[ 3 ], "%05.2f", globals->get_autopilot()->get_MaxAileron() );
+    fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust * val );
+    sprintf( SliderText[ 3 ], "%05.2f",
+             fgGetDouble("/autopilot/config/max-aileron") );
     APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
 }
 
@@ -394,8 +394,10 @@ static void rolloutsmooth_adj( puObject *hs ) {
     hs -> getValue ( &val ) ;
     SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
     //    printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
-    globals->get_autopilot()->set_RollOutSmooth( RollOutSmoothAdjust * val );
-    sprintf( SliderText[ 2 ], "%5.2f", globals->get_autopilot()->get_RollOutSmooth() );
+    fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
+                 RollOutSmoothAdjust * val );
+    sprintf( SliderText[ 2 ], "%5.2f",
+             fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
     APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
 
 }
@@ -406,19 +408,21 @@ static void goAwayAPAdjust (puObject *)
 }
 
 void cancelAPAdjust( puObject *self ) {
-    globals->get_autopilot()->set_MaxRoll( TmpMaxRollValue );
-    globals->get_autopilot()->set_RollOut( TmpRollOutValue );
-    globals->get_autopilot()->set_MaxAileron( TmpMaxAileronValue );
-    globals->get_autopilot()->set_RollOutSmooth( TmpRollOutSmoothValue );
+    fgSetDouble( "/autopilot/config/max-roll-deg", TmpMaxRollValue );
+    fgSetDouble( "/autopilot/config/roll-out-deg", TmpRollOutValue );
+    fgSetDouble( "/autopilot/config/max-aileron", TmpMaxAileronValue );
+    fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
+                 TmpRollOutSmoothValue );
 
     goAwayAPAdjust(self);
 }
 
 void resetAPAdjust( puObject *self ) {
-    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 );
+    fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust / 2 );
+    fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust / 2 );
+    fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust / 2 );
+    fgSetDouble( "/autopilot/config/roll-out-smooth-deg", 
+                 RollOutSmoothAdjust / 2 );
 
     FG_POP_PUI_DIALOG( APAdjustDialog );
 
@@ -426,15 +430,18 @@ void resetAPAdjust( puObject *self ) {
 }
 
 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       = 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()
+    TmpMaxRollValue       = fgGetDouble("/autopilot/config/max-roll-deg");
+    TmpRollOutValue       = fgGetDouble("/autopilot/config/roll-out-deg");
+    TmpMaxAileronValue    = fgGetDouble("/autopilot/config/max-aileron");
+    TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
+
+    MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
+        / MaxRollAdjust;
+    RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
+        / RollOutAdjust;
+    MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
+        / MaxAileronAdjust;
+    RollOutSmoothValue =  fgGetDouble("/autopilot/config/roll-out-smooth-deg")
        / RollOutSmoothAdjust;
 
     APAdjustHS0-> setValue ( MaxRollValue ) ;
@@ -470,20 +477,22 @@ void fgAPAdjustInit() {
     int slider_value_x = 160;
     float slider_delta = 0.1f;
 
-    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 * 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       = 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()
+    TmpMaxRollValue       = fgGetDouble("/autopilot/config/max-roll-deg");
+    TmpRollOutValue       = fgGetDouble("/autopilot/config/roll-out-deg");
+    TmpMaxAileronValue    = fgGetDouble("/autopilot/config/max-aileron");
+    TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
+    MaxRollAdjust = 2 * fgGetDouble("/autopilot/config/max-roll-deg");
+    RollOutAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-deg");
+    MaxAileronAdjust = 2 * fgGetDouble("/autopilot/config/max-aileron");
+    RollOutSmoothAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-smooth-deg");
+
+    MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
+        / MaxRollAdjust;
+    RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
+        / RollOutAdjust;
+    MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
+        / MaxAileronAdjust;
+    RollOutSmoothValue =  fgGetDouble("/autopilot/config/roll-out-smooth-deg")
        / RollOutSmoothAdjust;
 
     puGetDefaultFonts (  &APAdjustLegendFont,  &APAdjustLabelFont );
@@ -510,7 +519,8 @@ void fgAPAdjustInit() {
        APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
        APAdjustHS0-> setCallback ( maxroll_adj ) ;
 
-       sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
+       sprintf( SliderText[ 0 ], "%05.2f",
+                 fgGetDouble("/autopilot/config/max-roll-deg") );
        APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
        APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
@@ -527,7 +537,8 @@ void fgAPAdjustInit() {
        APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
        APAdjustHS1-> setCallback ( rollout_adj ) ;
 
-       sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
+       sprintf( SliderText[ 1 ], "%05.2f",
+                 fgGetDouble("/autopilot/config/roll-out-deg") );
        APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
        APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
@@ -545,7 +556,7 @@ void fgAPAdjustInit() {
        APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
 
        sprintf( SliderText[ 2 ], "%5.2f", 
-                globals->get_autopilot()->get_RollOutSmooth() );
+                fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
        APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
        APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
@@ -563,7 +574,7 @@ void fgAPAdjustInit() {
        APAdjustHS3-> setCallback ( maxaileron_adj ) ;
 
        sprintf( SliderText[ 3 ], "%05.2f", 
-                globals->get_autopilot()->get_MaxAileron() );
+                fgGetDouble("/autopilot/config/max-aileron") );
        APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
        APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
        APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
@@ -588,6 +599,7 @@ void fgAPAdjustInit() {
 
 #undef HORIZONTAL
 }
+#endif
 
 // Simple Dialog to input Target Airport
 void TgtAptDialog_Cancel(puObject *)
@@ -608,60 +620,120 @@ void TgtAptDialog_OK (puObject *)
     TgtAptDialogInput->getValue(&s);
 
     string tmp = s;
-    double alt = 0.0;
     unsigned int pos = tmp.find( "@" );
     if ( pos != string::npos ) {
        TgtAptId = tmp.substr( 0, pos );
-       string alt_str = tmp.substr( pos + 1 );
-       alt = atof( alt_str.c_str() );
-       if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
-           alt *= SG_FEET_TO_METER;
-       }
     } else {
        TgtAptId = tmp;
     }
 
     TgtAptDialog_Cancel( NULL );
     
-    FGAirport a;
-    FGFix f;
-    double t1, t2;
-    if ( fgFindAirportID( TgtAptId, &a ) ) {
+    /* s = input string, either 'FIX' or FIX@4000' */
+    /* TgtAptId is name of fix only; may get appended to below */
 
-        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 );
-
-    } else if ( current_fixlist->query( TgtAptId, 0.0, 0.0, 0.0,
-                                       &f, &t1, &t2 ) )
+    if ( NewWaypoint( TgtAptId ) == 0)
     {
-        SG_LOG( SG_GENERAL, SG_INFO,
-                "Adding waypoint (fix) = " << TgtAptId );
-        
-        sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+        TgtAptId  += " not in database.";
+        mkDialog(TgtAptId.c_str());
+    }
+}
 
-        SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
-                       SGWayPoint::WGS84, TgtAptId );
-        globals->get_route()->add_waypoint( wp );
+/* add new waypoint (either from above popup window 'ok button or telnet session) */
 
-         /* and turn on the autopilot */
-         globals->get_autopilot()->set_HeadingEnabled( true );
-         globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
-    } else {
-       TgtAptId  += " not in database.";
-       mkDialog(TgtAptId.c_str());
+int NewWaypoint( const string& Tgt_Alt )
+{
+  string TgtAptId;
+  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;
+  }
+
+  FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
+  const FGAirport *a = fgFindAirportID( TgtAptId);
+  if (a) {
+
+      SG_LOG( SG_GENERAL, SG_INFO,
+              "Adding waypoint (airport) = " << TgtAptId );
+
+      sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+
+      SGWayPoint wp( a->getLongitude(), a->getLatitude(), alt,
+                     SGWayPoint::WGS84, TgtAptId );
+      rm->add_waypoint( wp );
+
+      /* and turn on the autopilot */
+      fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
+
+      return 1;
+
+  } else if ( globals->get_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 );
+      rm->add_waypoint( wp );
+
+      /* and turn on the autopilot */
+      fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
+      return 2;
+  } else {
+         // Try finding a nav matching the ID
+         double lat, lon;
+         // The base lon/lat are determined by the last WP,
+         // or the current pos if the WP list is empty.
+         const int wps = rm->size();
+         if (wps > 0) {
+                 SGWayPoint wp = rm->get_waypoint(wps-1);
+                 lat = wp.get_target_lat();
+                 lon = wp.get_target_lon();
+         }
+         else {
+                 lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
+                 lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
+         }
+
+         lat *= SGD_DEGREES_TO_RADIANS;
+         lon *= SGD_DEGREES_TO_RADIANS;
+
+         SG_LOG( SG_GENERAL, SG_INFO,
+                         "Looking for nav " << TgtAptId << " at " << lon << " " << lat);
+         if (FGNavRecord* nav =
+                         globals->get_navlist()->findByIdent(TgtAptId.c_str(), lon, lat))
+         {
+                 SG_LOG( SG_GENERAL, SG_INFO,
+                                 "Adding waypoint (nav) = " << TgtAptId );
+
+                 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+
+                 SGWayPoint wp( nav->get_lon(), nav->get_lat(), alt,
+                                                SGWayPoint::WGS84, TgtAptId );
+                 rm->add_waypoint( wp );
+
+                 /* and turn on the autopilot */
+                 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
+                 return 3;
+         }
+         else {
+                 return 0;
+         }
+  }
 }
 
+
 void TgtAptDialog_Reset(puObject *)
 {
     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
@@ -718,12 +790,17 @@ void AddWayPoint(puObject *cb)
         }
         delete [] WPList[i];
     }
-    if ( globals->get_route()->size() > 0 ) { 
-        WPListsize = globals->get_route()->size();
+    FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
+    WPListsize = rm->size();
+    if ( WPListsize > 0 ) { 
         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) ];
+        for (i = 0; i < WPListsize; i++ ) {
+            SGWayPoint wp = rm->get_waypoint(i);
+            sprintf( WPString, "%5s %3.2flon %3.2flat",
+                     wp.get_id().c_str(),
+                     wp.get_target_lon(),
+                     wp.get_target_lat() );
+           WPList [i] = new char[ strlen(WPString)+1 ];
            strcpy ( WPList [i], WPString );
         }
     } else {
@@ -752,25 +829,14 @@ void AddWayPoint(puObject *cb)
 
 void PopWayPoint(puObject *cb)
 {
-    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"));
-    }
+    FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
+    rm->pop_waypoint();
 }
 
 void ClearRoute(puObject *cb)
 {
-    globals->get_route()->clear();
+    FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
+    rm->init();
 }
 
 void NewTgtAirportInit()
@@ -825,7 +891,7 @@ void NewTgtAirportInit()
     }
 
     FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
-    printf("leave NewTgtAirportInit()");
+    SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");
 }