//----------------------------------------------------------------------------//
//    module: FOpenProject.cpp                                                //
//                                                                            //
//    Form that displays progress and other information when loading model    //
//    and data as specified in associated TProjectG. Loading is done using    //
//    a thread object (ThrOpenProject) which is newed in function OnShow()    //
//                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software and can be used under the terms of the    //
//    GNU licence. See header file for further information and disclaimer.    //
//                                                                            //
//----------------------------------------------------------------------------//



#include <vcl.h>
#pragma hdrstop

#pragma package(smart_init)
#pragma resource "*.dfm"


#include <filectrl.hpp>             // due to:  MinimizeName()

#include "FOpenProject.h"
#include "FMain.h"                  //          global f_DisableQuestions


TFOpenProject *FOpenProject;

//----------------------------------------------------------------------------------------------------------------------
// constructor
__fastcall TFOpenProject::TFOpenProject(TComponent* Owner) : TForm(Owner)
{
   prj = NULL;
}



//----------------------------------------------------------------------------------------------------------------------
// c-style wrapper used as callback function when thread (loading of model and data) is finished
void WrapperToCallOnFinish()
{
   FOpenProject->OnFinish();   // forward to member function
}


//----------------------------------------------------------------------------------------------------------------------
// load learn/test data and model file according to associated project object
void __fastcall TFOpenProject::FormShow(TObject *Sender)
{
   // a) check if project is associated
   IfTrueThrowTypeA(!prj, "No learn data object associated!", "TLoadProject::FormShow");
   IfTrueThrowTypeA(!szModelFile, "No model filename associated!", "TLoadProject::FormShow");


   // b) create and start new thread which loads project
   f_Stop = false;                                                                                  // reset stop flag
   thread = new ThrOpenProject(false, WrapperToCallOnFinish, prj, szModelFile, szTuningFile, &f_Stop); // note: deletes itself


   // c) enable/show and disable/hide controls
   PbOpenProject->Position=0;            // reset progress bar
   PbOpenProject->Visible = true;        // show
   BtPause->Enabled = true;              // enable 'Pause'
   BtAbort->Enabled = true;              // enable 'Abort'
   Timer  ->Enabled = true;              // start progress indication timer
   ActiveControl = BtPause;
}


//----------------------------------------------------------------------------------------------------------------------
// actualize status and progress
void __fastcall TFOpenProject::OnTimer(TObject *Sender)
{
   // set filename ...
   LaFile->Caption = MinimizeName(AnsiString(thread->Filename()), LaFile->Canvas, LaFile->Width);

   // ... status ...
   LaStatus->Caption = thread->Status();

   // ... and progress
   PbOpenProject->Position = thread->Progress();
}


//----------------------------------------------------------------------------------------------------------------------
// Button 'Pause' clicked:  Pause loading of data file
void __fastcall TFOpenProject::BtPauseClick(TObject*)
{
   BtPause   ->Visible   = false;                     // hide 'Pause' button
   BtPause   ->Refresh();
   thread    ->Suspended = true;                      // suspend thread
   Timer     ->Enabled   = false;                     // stop progress indication timer
   BtContinue->Visible   = true;                      // show 'Continue' button
   BtContinue->Refresh();
   ActiveControl = BtContinue;
   LaStatus->Caption = "Paused";                      // display status
}


//----------------------------------------------------------------------------------------------------------------------
// Button 'Continue' clicked:  Resume loading of data file
void __fastcall TFOpenProject::BtContinueClick(TObject *Sender)
{
   BtContinue->Visible   = false;                     // hide 'Continue' button
   BtContinue->Refresh();
   thread    ->Suspended = false;                     // continue thread
   Timer     ->Enabled   = true;                      // enable progress indication timer again

   BtPause   ->Visible   = true;                      // show 'Pause' button
   BtPause   ->Refresh();
   ActiveControl = BtPause;
   LaStatus->Caption = thread->Status();              // display status
}


//----------------------------------------------------------------------------------------------------------------------
// Button 'Cancel' :  Ask user and abort loading of data file
void __fastcall TFOpenProject::BtAbortClick(TObject *Sender)
{
   // a) suspend thread   note: necessary cause otherwise the code in c) could cause an exception
   BtPauseClick(this);

   // b) ask user if he really wants to abort
   if(f_DisableQuestions || Application->MessageBox("I'm loading! Do you really want to abort ??"
                                                   ,"Warning", MB_YESNO|MB_ICONQUESTION)==ID_YES)
   {
      // c) abort
      BtContinue->Visible = false;    // hide button 'Continue'
      f_Stop = true;                  // set stop flag, this will cause thread to terminate
   }

   BtContinueClick(this);             // continue thread
}