]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCDialog.cxx
new FSF address
[flightgear.git] / src / ATC / ATCDialog.cxx
index 22e886f2eaed373f8cee392428ddb1ffd295bc31..6265c6b549c8218d9bd6193bc95020168a795ac5 100644 (file)
 //
 // 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.
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
 #include <simgear/compiler.h>
 
 #include <simgear/structure/commands.hxx>
 
 #include <Main/globals.hxx>
-#include <GUI/gui.h>
+#include <GUI/gui.h>           // mkDialog
+#include <GUI/new_gui.hxx>
 
 #include "ATCDialog.hxx"
 #include "ATC.hxx"
 #include "ATCmgr.hxx"
+#include "ATCdisplay.hxx"
 #include "commlist.hxx"
 #include "ATCutils.hxx"
 #include <Airports/simple.hxx>
 
 #include <sstream>
+#include <map>
 
 SG_USING_STD(ostringstream);
+SG_USING_STD(map);
 
 FGATCDialog *current_atcdialog;
 
 // For the command manager - maybe eventually this should go in the built in command list
 static bool do_ATC_dialog(const SGPropertyNode* arg) {
-       globals->get_ATC_mgr()->doPopupDialog();
+       current_atcdialog->PopupDialog();
        return(true);
 }
 
@@ -51,536 +59,370 @@ static bool do_ATC_freq_search(const SGPropertyNode* arg) {
 
 ATCMenuEntry::ATCMenuEntry() {
   stationid    = "";
-  stationfr    = 0;
+  //stationfr    = 0;
   transmission = "";
   menuentry    = "";
+  callback_code = 0;
 }
 
 ATCMenuEntry::~ATCMenuEntry() {
 }
 
-static char* t0 = "No communication currently available";
-//static char* t1 = "Request departure clearance";
-//static char* t2 = "Report Runway vacated";
-static char** towerOptions = new char*[2];
-
-// ----------------------- DCL ------------------------------------------
-// For the ATC dialog - copied from the Autopilot new heading dialog code!
-static puDialogBox*            atcDialog;
-static puFrame*                        atcDialogFrame;
-static puText*                 atcDialogMessage;
-//static puInput*                      atcDialogInput;
-static puOneShot*              atcDialogOkButton;
-static puOneShot*              atcDialogCancelButton;
-static puButtonBox*            atcDialogCommunicationOptions;
-// ----------------------------------------------------------------------
-
-// ------------------------ AK ------------------------------------------
-static puDialogBox  *ATCMenuBox = 0;
-static puFrame      *ATCMenuFrame = 0;
-static puText       *ATCMenuBoxMessage = 0;
-static puButtonBox     *ATCOptionsList = 0;
-// ----------------------------------------------------------------------
-
-// AK
-static void AKATCDialogOK(puObject *)
-{
-       switch(ATCOptionsList->getValue()) {
-       case 0:
-               //cout << "Option 0 chosen\n";
-               fgSetBool("/sim/atc/opt0",true);
-               break;
-       case 1:
-               //cout << "Option 1 chosen\n";
-               fgSetBool("/sim/atc/opt1",true);
-               break;
-       case 2:
-               //cout << "Option 2 chosen\n";
-               fgSetBool("/sim/atc/opt2",true);
-               break;
-       case 3:
-               //cout << "Option 2 chosen\n";
-               fgSetBool("/sim/atc/opt3",true);
-               break;
-       default:
-               break;
+static void atcUppercase(string &s) {
+       for(unsigned int i=0; i<s.size(); ++i) {
+               s[i] = toupper(s[i]);
        }
-       FG_POP_PUI_DIALOG( ATCMenuBox );
 }
 
-// AK
-static void AKATCDialogCancel(puObject *)
-{
-    FG_POP_PUI_DIALOG( ATCMenuBox );
-}
+// find child whose <name>...</name> entry matches 'name'
+static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
+       SGPropertyNode* p;
 
-// DCL
-static void ATCDialogCancel(puObject *)
-{
-    //ATCDialogInput->rejectInput();
-    FG_POP_PUI_DIALOG( atcDialog );
+       for (int i = 0; i < prop->nChildren(); i++)
+               if ((p = getNamedNode(prop->getChild(i), name)))
+                       return p;
+
+       if (!strcmp(prop->getStringValue("name"), name))
+               return prop;
+
+       return 0;
 }
 
-// DCL
-static void ATCDialogOK (puObject *me)
-{
-       // Note that currently the dialog is hardwired to comm1 only here.
-       switch(globals->get_ATC_mgr()->GetComm1ATCType()) {
-       case INVALID:
-               break;
-       case ATIS:
-               break;
-       case TOWER: {
-               /*
-               FGTower* twr = (FGTower*)globals->get_ATC_mgr()->GetComm1ATCPointer();
-               switch(atcDialogCommunicationOptions->getValue()) {
-               case 0:
-                       //cout << "Option 0 chosen\n";
-                       twr->RequestLandingClearance("charlie foxtrot sierra");
-                       break;
-               case 1:
-                       //cout << "Option 1 chosen\n";
-                       twr->RequestDepartureClearance("charlie foxtrot sierra");
-                       break;
-               case 2:
-                       //cout << "Option 2 chosen\n";
-                       twr->ReportRunwayVacated("charlie foxtrot sierra");
-                       break;
-               default:
-                       break;
-               }
-               */
-               break;
-       }
-       case GROUND:
-               break;
-       case APPROACH:
-               break;
-       default:
-               break;
-       }
 
-    ATCDialogCancel(me);
-    //if(error) mkDialog(s.c_str());
+FGATCDialog::FGATCDialog() {
+       _callbackPending = false;
+       _callbackTimer = 0.0;
+       _callbackWait = 0.0;
+       _callbackPtr = NULL;
+       _callbackCode = 0;
+       _gui = (NewGUI *)globals->get_subsystem("gui");
 }
 
-// DCL
-static void ATCDialog(puObject *cb)
-{
-    //ApHeadingDialogInput   ->    setValue ( heading );
-    //ApHeadingDialogInput    -> acceptInput();
-    FG_PUSH_PUI_DIALOG(atcDialog);
+FGATCDialog::~FGATCDialog() {
 }
 
-// DCL
-void ATCDialogInit()
-{
-       char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
-       char *s;
-
-       // Option lists hardwired per ATC type  
-       towerOptions[0] = new char[strlen(t0)+1];
-       strcpy(towerOptions[0], t0);
-       //towerOptions[1] = new char[strlen(t1)+1];
-       //strcpy(towerOptions[1], t1);
-       //towerOptions[2] = new char[strlen(t2)+1];
-       //strcpy(towerOptions[2], t2);
-       towerOptions[1] = NULL;
-       
-       atcDialog = new puDialogBox (150, 50);
-       {
-               atcDialogFrame   = new puFrame (0, 0, 500, 250);
-               
-               atcDialogMessage = new puText          (250, 220);
-               atcDialogMessage    -> setDefaultValue (defaultATCLabel);
-               atcDialogMessage    -> getDefaultValue (&s);
-               atcDialogMessage    -> setLabel        (s);
-               atcDialogMessage    -> setLabelPlace   (PUPLACE_TOP_CENTERED);
-
-               atcDialogCommunicationOptions = new puButtonBox (50, 50, 450, 210, NULL, true);
-               
-               atcDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
-               atcDialogOkButton     ->     setLegend         (gui_msg_OK);
-               atcDialogOkButton     ->     makeReturnDefault (TRUE);
-               atcDialogOkButton     ->     setCallback       (ATCDialogOK);
-               
-               atcDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
-               atcDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
-               atcDialogCancelButton ->     setCallback       (ATCDialogCancel);
-               
-       }
-       FG_FINALIZE_PUI_DIALOG(atcDialog);
-       
+void FGATCDialog::Init() {
        // Add ATC-dialog to the command list
        globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
+       // Add ATC-freq-search to the command list
+       globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
+
+       // initialize properties polled in Update()
+       globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
+       globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
 }
 
-///////////////////////////////////////////////////////////////////////
-//
-// ATCDoDialog is in a state of flux at the moment
-// Stations other than approach are handled by DCL's simple code
-// Approach is handled by AK's fancy dynamic-list code
-// Hopefully all interactive stations should go to AK's code eventually
-//
-///////////////////////////////////////////////////////////////////////
-void ATCDoDialog(atc_type type) {
-       switch(type) {
-       case INVALID:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Not tuned in to any ATC service.");
-               break;
-       case ATIS:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to ATIS: no communication possible.");
-               break;
-       case TOWER: 
-               atcDialogCommunicationOptions->newList(towerOptions);
-               atcDialogMessage->setLabel("Tuned in to Tower - select communication to transmit:");
-               break;
-       case GROUND:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to Ground - select communication to transmit:");
-               break;
-       case APPROACH:
-               current_atcdialog->DoDialog();
-               break;
-       default:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to unknown ATC service - enter transmission:");
-               break;
+void FGATCDialog::Update(double dt) {
+       static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
+       string s = airport->getStringValue();
+       if (!s.empty()) {
+               airport->setStringValue("");
+               FreqDisplay(s);
        }
 
-       // Third - display the dialog without pausing sim.
-       if(type != APPROACH) {  
-               ATCDialog(NULL);
+       static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
+       int n = trans_num->getIntValue();
+       if (n >= 0) {
+               trans_num->setIntValue(-1);
+               PopupCallback(n);
        }
-}
 
-void FGATCDialog::Init() {
-       // Add ATC-freq-search to the command list
-       globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
+       if(_callbackPending) {
+               if(_callbackTimer > _callbackWait) {
+                       _callbackPtr->ReceiveUserCallback(_callbackCode);
+                       _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
+                       _callbackPending = false;
+               } else {
+                       _callbackTimer += dt;
+               }
+       }
 }
 
-// AK
 // Add an entry
-void FGATCDialog::add_entry(string station, string transmission, string menutext ) {
-  
+void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
+
   ATCMenuEntry a;
 
   a.stationid = station;
   a.transmission = transmission;
   a.menuentry = menutext;
+  a.callback_code = code;
+
+  (available_dialog[type])[station.c_str()].push_back(a);
 
-  atcmentrylist_station[station.c_str()].push_back(a);
+}
 
+void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
+  atcmentry_vec_type* p = &((available_dialog[type])[station]);
+  atcmentry_vec_iterator current = p->begin();
+  while(current != p->end()) {
+    if(current->transmission == trans) current = p->erase(current);
+       else ++current;
+  }
 }
 
-// AK
-// query the database whether the transmission is already registered; 
-bool FGATCDialog::trans_reg( const string &station, const string &trans ) {
+void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
+  atcmentry_vec_type* p = &((available_dialog[type])[station]);
+  atcmentry_vec_iterator current = p->begin();
+  while(current != p->end()) {
+    if(current->callback_code == code) current = p->erase(current);
+       else ++current;
+  }
+}
 
-  atcmentry_list_type     atcmlist = atcmentrylist_station[station];
-  atcmentry_list_iterator current  = atcmlist.begin();
-  atcmentry_list_iterator last     = atcmlist.end();
-  
-  for ( ; current != last ; ++current ) {
+// query the database whether the transmission is already registered;
+bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
+  atcmentry_vec_type* p = &((available_dialog[type])[station]);
+  atcmentry_vec_iterator current = p->begin();
+  for ( ; current != p->end() ; ++current ) {
     if ( current->transmission == trans ) return true;
   }
   return false;
 }
 
-// AK
-// ===================================================
-// ===  Update ATC menue and look for keys pressed ===
-// ===================================================
-void FGATCDialog::DoDialog() {
-       
-       static string mentry[10];
-       static string mtrans[10];
-       char   buf[10];
-       TransPar TPar;
-       FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
-       
-       if(atcptr != NULL) {
-               
-               atcmentry_list_type     atcmlist = atcmentrylist_station[atcptr->get_ident()];
-               //atcmentry_list_type     atcmlist = atcmentrylist_station["EGNX"];
-               atcmentry_list_iterator current  = atcmlist.begin();
-               atcmentry_list_iterator last     = atcmlist.end();
-               
-               // Set all opt flags to false before displaying box
-               fgSetBool("/sim/atc/opt0",false);
-               fgSetBool("/sim/atc/opt1",false);
-               fgSetBool("/sim/atc/opt2",false);
-               fgSetBool("/sim/atc/opt3",false);
-               fgSetBool("/sim/atc/opt4",false);
-               fgSetBool("/sim/atc/opt5",false);
-               fgSetBool("/sim/atc/opt6",false);
-               fgSetBool("/sim/atc/opt7",false);
-               fgSetBool("/sim/atc/opt8",false);
-               fgSetBool("/sim/atc/opt9",false);
-               
-               //int yc = 10;
-               int yc = 70;
-               int xsize = 600;
-               
-               if ( atcmlist.size() != 0 ){ 
-                       int k=atcmlist.size();
-                       //int k = 3;
-                       //cout << "k = " << k << endl;
-                       int y = (fgGetInt("/sim/startup/ysize") - 200 - 20 - k*20);
-                       ATCMenuBox = new puDialogBox (100, y);
-                       ATCMenuFrame      =  new puFrame (0,0,xsize,yc+40);
-                       // loop over all entries in atcmentrylist
-                       ATCOptionsList = new puButtonBox (50, 50, 450, 50+(k*25), NULL, true);
-                       char** optList = new char*[k+1];
-                       int kk = 0;
-                       for ( ; current != last ; ++current ) {
-                               string dum;
-                               sprintf( buf, "%i", kk+1 );
-                               buf[1] = '\0';
-                               dum = buf;
-                               mentry[kk] = dum + ". " + current->menuentry;
-                               optList[kk] = new char[strlen(mentry[kk].c_str()) + 1];
-                               strcpy(optList[kk], mentry[kk].c_str());
-                               //cout << "optList[" << kk << "] = " << optList[kk] << endl; 
-                               mtrans[kk] =              current->transmission;
-                               //ATCMenuBoxMessage =  new puText (10, yc);
-                               //ATCMenuBoxMessage ->     setLabel( mentry[kk].c_str() );
-                               yc += 20;
-                               ++kk;
-                       } 
-                       yc += 2*20;
-                       optList[k] = NULL;
-                       ATCOptionsList->newList(optList);
-               } else {
-                       int y = (fgGetInt("/sim/startup/ysize") - 100 - 20 );
-                       ATCMenuBox = new puDialogBox (10, y);
-                       ATCMenuFrame      =  new puFrame (0,0,xsize,yc+40);
-                       ATCMenuBoxMessage =  new puText (10, yc-10);
-                       ATCMenuBoxMessage ->     setLabel( "No transmission available" );
-               }
-               
-               ATCMenuBoxMessage =  new puText (10, yc+10);
-               ATCMenuBoxMessage ->     setLabel( "ATC Menu" );
-               atcDialogOkButton     =  new puOneShot         ((xsize/2)-85, 10, (xsize/2)-25, 50);
-               atcDialogOkButton     ->     setLegend         (gui_msg_OK);
-               atcDialogOkButton     ->     makeReturnDefault (TRUE);
-               atcDialogOkButton     ->     setCallback       (AKATCDialogOK);
-               
-               atcDialogCancelButton =  new puOneShot         ((xsize/2)+25, 10, (xsize/2)+85, 50);
-               atcDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
-               atcDialogCancelButton ->     setCallback       (AKATCDialogCancel);
-               FG_FINALIZE_PUI_DIALOG( ATCMenuBox );
-               FG_PUSH_PUI_DIALOG( ATCMenuBox );
-               
-               
-               /*      
-               if ( atckey != -1 && TransDisplayed && mtrans[atckey-1].c_str() != "" ) {
-                       cout << mtrans[atckey-1].c_str() << endl;
-                       TPar = current_transmissionlist->extract_transpar( mtrans[atckey-1].c_str() );
-                       current_atcmentrylist->reset = true;
-                       current_transparlist->add_entry( TPar );
-                       
-                       //    transpar_list_type test = current_transparlist;
-                       // transpar_list_iterator current = test.begin();
-                       //for ( ; current != test.end(); ++current ) {
-                               // current->tpar.intention;
-                       //}
-               }
-               
-               if ( current_atcmentrylist->freq != (int)(comm1_freq*100.0 + 0.5) ) {
-                       current_atcmentrylist->reset = true;
-               }
-               
-               // reset (delete) ATCmenue list if reset is true
-               if ( current_atcmentrylist->reset == true ) {
-                       delete ( current_atcmentrylist );
-                       current_atcmentrylist = new FGatcmentryList;
-                       current_atcmentrylist->init( (int)(comm1_freq*100.0 + 0.5) );
-                       if ( TransDisplayed ) {
-                               FG_POP_PUI_DIALOG( ATCMenuBox ); 
-                               TransDisplayed = false;
-                       }
-               }
-               */      
-       }
+// query the database whether the transmission is already registered;
+bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
+  atcmentry_vec_type* p = &((available_dialog[type])[station]);
+  atcmentry_vec_iterator current = p->begin();
+  for ( ; current != p->end() ; ++current ) {
+    if ( current->callback_code == code ) return true;
+  }
+  return false;
 }
 
-//////////////////////////////////////////////////////
-//
-//  STUFF FOR THE FREQUENCY SEARCH DIALOG
-//
-//////////////////////////////////////////////////////
+// Display the ATC popup dialog box with options relevant to the users current situation.
+void FGATCDialog::PopupDialog() {
+       const char *dialog_name = "atc-dialog";
+       SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
+       if (!dlg)
+               return;
+
+       _gui->closeDialog(dialog_name);
+
+       SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
+       // remove all transmission buttons
+       button_group->removeChildren("button", false);
+
+       string label;
+       FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
+
+       if (!atcptr) {
+               label = "Not currently tuned to any ATC service";
+               mkDialog(label.c_str());
+               return;
+       }
 
-static puDialogBox*            atcFreqDialog;
-static puFrame*                        atcFreqDialogFrame;
-static puText*                 atcFreqDialogMessage;
-static puInput*                        atcFreqDialogInput;
-static puOneShot*              atcFreqDialogOkButton;
-static puOneShot*              atcFreqDialogCancelButton;
-//static puButtonBox*          atcFreqDialogCommunicationOptions;
+       if(atcptr->GetType() == ATIS) {
+               label = "Tuned to ATIS - no communication possible";
+               mkDialog(label.c_str());
+               return;
+       }
 
-static puText* atcFreqDisplayText[20];
+       atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
+       atcmentry_vec_iterator current = atcmlist.begin();
+       atcmentry_vec_iterator last = atcmlist.end();
        
-static void FreqDialogCancel(puObject*) {
-       FG_POP_PUI_DIALOG(atcFreqDialog);
-       delete atcFreqDialog;
-}
+       if(!atcmlist.size()) {
+               label = "No transmission available";
+               mkDialog(label.c_str());
+               return;
+       }
 
-static void FreqDialogOK(puObject*) {
-       string tmp = atcFreqDialogInput->getStringValue();
-       FG_POP_PUI_DIALOG(atcFreqDialog);
-       delete atcFreqDialog;
-       current_atcdialog->FreqDisplay(tmp);
+       const int bufsize = 32;
+       char buf[bufsize];
+       // loop over all entries in atcmentrylist
+       for (int n = 0; n < 10; ++n) {
+               snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
+               fgSetBool(buf, false);
+
+               if (current == last)
+                       continue;
+
+               // add transmission button (modified copy of <button-template>)
+               SGPropertyNode *entry = button_group->getNode("button", n, true);
+               copyProperties(button_group->getNode("button-template", true), entry);
+               entry->removeChildren("hide", false);
+               entry->setStringValue("property", buf);
+               entry->setIntValue("keynum", '1' + n);
+               if (n == 0)
+                       entry->setBoolValue("default", true);
+
+               snprintf(buf, bufsize, "%d", n + 1);
+               string legend = string(buf) + ". " + current->menuentry;
+               entry->setStringValue("legend", legend.c_str());
+               entry->setIntValue("binding/value", n);
+               current++;
+       }
+
+       _gui->showDialog(dialog_name);
+       return;
 }
 
-static void FreqDisplayOK(puObject*) {
-       FG_POP_PUI_DIALOG(atcFreqDialog);
-       delete atcFreqDialog;
+void FGATCDialog::PopupCallback(int num) {
+       FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
+
+       if (!atcptr)
+               return;
+
+       if (atcptr->GetType() == TOWER) {
+               //cout << "TOWER " << endl;
+               //cout << "ident is " << atcptr->get_ident() << endl;
+               atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
+               unsigned int size = atcmlist.size();
+               if(size && num < size) {
+                       //cout << "Doing callback...\n";
+                       ATCMenuEntry a = atcmlist[num];
+                       atcptr->SetFreqInUse();
+                       string pilot = atcptr->GenText(a.transmission, a.callback_code);
+                       fgSetString("/sim/messages/pilot", pilot.c_str());
+                       // This is the user's speech getting displayed.
+                       globals->get_ATC_display()->RegisterSingleMessage(pilot);
+                       _callbackPending = true;
+                       _callbackTimer = 0.0;
+                       _callbackWait = 5.0;
+                       _callbackPtr = atcptr;
+                       _callbackCode = a.callback_code;
+               } else {
+                       //cout << "No options available...\n";
+               }
+               //cout << "Donded" << endl;
+       }
 }
 
+// map() key data type (removes duplicates and sorts by distance)
+struct atcdata {
+       atcdata() {}
+       atcdata(const string i, const string n, const double d) {
+               id = i, name = n, distance = d;
+       }
+       bool operator<(const atcdata& a) const {
+               return id != a.id && distance < a.distance;
+       }
+       bool operator==(const atcdata& a) const {
+               return id == a.id && distance == a.distance;
+       }
+       string id;
+       string name;
+       double distance;
+};
+
 void FGATCDialog::FreqDialog() {
+       const char *dialog_name = "atc-freq-search";
+       SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
+       if (!dlg)
+               return;
+
+       _gui->closeDialog(dialog_name);
+
+       SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
+       // remove all dynamic airport/ATC buttons
+       button_group->removeChildren("button", false);
 
        // Find the ATC stations within a reasonable range (about 40 miles?)
-       //comm_list_type atc_stations;
-       //comm_list_iterator atc_stat_itr;
-       
-       //double lon = fgGetDouble("/position/longitude-deg");
-       //double lat = fgGetDouble("/position/latitude-deg");
-       //double elev = fgGetDouble("/position/altitude-ft");
+       comm_list_type atc_stations;
+       comm_list_iterator atc_stat_itr;
        
-       /*
+       double lon = fgGetDouble("/position/longitude-deg");
+       double lat = fgGetDouble("/position/latitude-deg");
+       double elev = fgGetDouble("/position/altitude-ft");
+       Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
+               lat * SGD_DEGREES_TO_RADIANS, elev));
+
        // search stations in range
        int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
        if (num_stat != 0) {
-       } else {
-               // Make up a message saying no things in range
-       }
-       */
-       
-       //char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
-       char *s;
-       
-       int hsize = 150;
-       atcFreqDialog = new puDialogBox (150, 50);
-       {
-               atcFreqDialogFrame   = new puFrame (0, 0, 300, hsize);
-               
-               atcFreqDialogMessage = new puText          (40, (hsize - 30));
-               atcFreqDialogMessage    -> setDefaultValue ("Enter airport identifier:");
-               atcFreqDialogMessage    -> getDefaultValue (&s);
-               atcFreqDialogMessage    -> setLabel        (s);
-               //atcFreqDialogMessage    -> setLabelPlace   (PUPLACE_TOP_CENTERED);
-               //atcFreqDialogMessage    -> setLabelPlace   (0);
-
-               atcFreqDialogInput = new puInput (50, (hsize - 75), 150, (hsize - 45));
-               atcFreqDialogInput->acceptInput();
-               
-               atcFreqDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
-               atcFreqDialogOkButton     ->     setLegend         (gui_msg_OK);
-               atcFreqDialogOkButton     ->     makeReturnDefault (TRUE);
-               atcFreqDialogOkButton     ->     setCallback       (FreqDialogOK);
-               
-               atcFreqDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
-               atcFreqDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
-               atcFreqDialogCancelButton ->     setCallback       (FreqDialogCancel);
-               
+               map<atcdata, bool> uniq;
+               // fill map (sorts by distance and removes duplicates)
+               comm_list_iterator itr = atc_stations.begin();
+               for (; itr != atc_stations.end(); ++itr) {
+                       Point3D station = Point3D(itr->x, itr->y, itr->z);
+                       double distance = aircraft.distance3Dsquared(station);
+                       uniq[atcdata(itr->ident, itr->name, distance)] = true;
+               }
+               // create button per map entry (modified copy of <button-template>)
+               map<atcdata, bool>::iterator uit = uniq.begin();
+               for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
+                       SGPropertyNode *entry = button_group->getNode("button", n, true);
+                       copyProperties(button_group->getNode("button-template", true), entry);
+                       entry->removeChildren("hide", false);
+                       entry->setStringValue("legend", uit->first.id.c_str());
+                       entry->setStringValue("binding[0]/value", uit->first.id.c_str());
+               }
        }
-       FG_FINALIZE_PUI_DIALOG(atcFreqDialog);
-       
-       FG_PUSH_PUI_DIALOG(atcFreqDialog);
-       
-}
 
-static void atcUppercase(string &s)
-{
-       for(unsigned int i=0; i<s.size(); ++i) {
-               s[i] = toupper(s[i]);
-       }
+       // (un)hide message saying no things in range
+       SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
+       range_error->setBoolValue("hide", num_stat);
+
+       _gui->showDialog(dialog_name);
 }
 
-void FGATCDialog::FreqDisplay(string ident) {
+void FGATCDialog::FreqDisplay(string& ident) {
+       const char *dialog_name = "atc-freq-display";
+       SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
+       if (!dlg)
+               return;
+
+       _gui->closeDialog(dialog_name);
+
+       SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
+       // remove all frequency entries
+       freq_group->removeChildren("group", false);
 
        atcUppercase(ident);
-       
-       //char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
-       string label = "Frequencies for airport ";
-       label += ident;
-       label += ":";
-       char *s;
-       
+       string label;
+
+       const FGAirport *a = fgFindAirportID(ident);
+       if (!a) {
+               label = "Airport " + ident + " not found in database.";
+               mkDialog(label.c_str());
+               return;
+       }
+
+       // set title
+       label = ident + " Frequencies";
+       dlg->setStringValue("text/label", label.c_str());
+
        int n = 0;      // Number of ATC frequencies at this airport
-       string freqs[20];
-       char buf[8];
-
-    FGAirport a;
-    if ( dclFindAirportID( ident, &a ) ) {
-               comm_list_type stations;
-               int found = current_commlist->FindByPos(a.longitude, a.latitude, a.elevation, 20.0, &stations);
-               if(found) {
-                       ostringstream ostr;
-                       comm_list_iterator itr = stations.begin();
-                       while(itr != stations.end()) {
-                               if((*itr).ident == ident) {
-                                       if((*itr).type != INVALID) {
-                                               ostr << (*itr).type;
-                                               freqs[n] = ostr.str();
-                                                freqs[n].append("     -     ");
-                                               sprintf(buf, "%.2f", ((*itr).freq / 100.0));    // Convert from KHz to MHz
-                                               // Hack alert!
-                                               if(buf[5] == '3') buf[5] = '2';
-                                               if(buf[5] == '8') buf[5] = '7';
-                                               freqs[n] += buf;
-                                               ostr.seekp(0);
-                                               n++;
-                                       }
-                               }
-                               ++itr;
-                       }
+
+       comm_list_type stations;
+       int found = current_commlist->FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 20.0, &stations);
+       if(found) {
+               ostringstream ostr;
+               comm_list_iterator itr = stations.begin();
+               for (n = 0; itr != stations.end(); ++itr) {
+                       if(itr->ident != ident)
+                               continue;
+
+                       if(itr->type == INVALID)
+                               continue;
+
+                       // add frequency line (modified copy of <group-template>)
+                       SGPropertyNode *entry = freq_group->getNode("group", n, true);
+                       copyProperties(freq_group->getNode("group-template", true), entry);
+                       entry->removeChildren("hide", false);
+
+                       ostr << itr->type;
+                       entry->setStringValue("text[0]/label", ostr.str().c_str());
+
+                       char buf[8];
+                       snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
+                       if(buf[5] == '3') buf[5] = '2';
+                       if(buf[5] == '8') buf[5] = '7';
+                       buf[7] = '\0';
+
+                       entry->setStringValue("text[1]/label", buf);
+
+                       ostr.seekp(0);
+                       n++;
                }
-    } else {
-               label = "Airport ";
-               label += ident;
-               label += " not found in database.";
-               n = -1;
        }
-       
        if(n == 0) {
-               label = "No frequencies found for airport ";
-               label += ident;
+               label = "No frequencies found for airport " + ident;
+               mkDialog(label.c_str());
+               return;
        }
 
-       int hsize = (n < 0 ? 100 : 105 + (n * 30));
-       atcFreqDialog = new puDialogBox (250, 50);
-       {
-               atcFreqDialogFrame   = new puFrame (0, 0, 400, hsize);
-               
-               atcFreqDialogMessage = new puText          (40, (hsize - 30));
-               atcFreqDialogMessage    -> setDefaultValue (label.c_str());
-               atcFreqDialogMessage    -> getDefaultValue (&s);
-               atcFreqDialogMessage    -> setLabel        (s);
-               
-               for(int i=0; i<n; ++i) {
-                       atcFreqDisplayText[i] = new puText(40, hsize - 65 - (30 * i));
-                       atcFreqDisplayText[i]->setDefaultValue(freqs[i].c_str());
-                       atcFreqDisplayText[i]-> getDefaultValue (&s);
-                       atcFreqDisplayText[i]-> setLabel        (s);
-               }
-               
-               atcFreqDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
-               atcFreqDialogOkButton     ->     setLegend         (gui_msg_OK);
-               atcFreqDialogOkButton     ->     makeReturnDefault (TRUE);
-               atcFreqDialogOkButton     ->     setCallback       (FreqDisplayOK);
-               
-               atcFreqDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
-               atcFreqDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
-               atcFreqDialogCancelButton ->     setCallback       (FreqDialogCancel);
-               
-       }
-       FG_FINALIZE_PUI_DIALOG(atcFreqDialog);
-       
-       FG_PUSH_PUI_DIALOG(atcFreqDialog);
-       
+       _gui->showDialog(dialog_name);
 }