//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FrmDbPrgressF.h"
#include "WindowMsgF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxProgressBar"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"
//TFrmDbProgress *FrmDbProgress = NULL;

//---------------------------------------------------------------------------
__fastcall TDBThread::TDBThread()
  : TThread(true)
{
    FreeOnTerminate = true;
    //Priority = tpTimeCritical;
	Priority = tpNormal;
}
//---------------------------------------------------------------------------

void __fastcall TDBThread::SetQry(TADOQuery *adoQry, TForm *AForm)
{
    m_ADOQry = adoQry;
    FParentForm = AForm;
    //Resume();
}
//---------------------------------------------------------------------------

void __fastcall TDBThread::Execute()
{
    try
    {
        m_ADOQry->Open();
        POST_MSG(FParentForm->Handle, WM_PARAM_DATABASE, WP_DB_SELECT_OK, 0);
    } catch(...)
    {
        POST_MSG(FParentForm->Handle, WM_PARAM_DATABASE, WP_DB_SELECT_ERROR, 0);
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
__fastcall TFrmDbProgress::TFrmDbProgress(TComponent* Owner)
    : TForm(Owner)
{
    //SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
    m_pThrDb = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TFrmDbProgress::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    Refresh();
    Application->ProcessMessages();
    Refresh();
    Application->ProcessMessages();
}
//---------------------------------------------------------------------------
void __fastcall TFrmDbProgress::btnCancelClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::Init(String sCaption, int nMax, bool bCancel/*=false*/, bool bTimer/*=false*/)
{
    Caption = sCaption;
    //PbProgress->Properties->Text = sCaption;
    PbProgress->Properties->Min = 0;
    PbProgress->Properties->Max = nMax;
    PbProgress->Position = 0;

    if (bCancel)
    {
        btnCancel->Enabled = true;
        Height = 112;//PbProgress->Top + PbProgress->Height + 30;
    }
    else
    {
        btnCancel->Enabled = false;
        Height = 80;//PbProgress->Top + PbProgress->Height + 30;
    }

    FParentForm = (TForm*)this->Owner;
    FParentForm->Enabled = false;
    int nTop = FParentForm->Top + (FParentForm->Height / 2) - Height;
    int nLeft= FParentForm->Left+ ((FParentForm->Width-Width) / 2);
    this->Top = nTop;
    this->Left= nLeft;
    Refresh();
    TmrProgress->Enabled = bTimer;
    Application->ProcessMessages();
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::Step(String sText, int nPos)
{
    Label1->Caption = sText;
    PbProgress->Properties->Text = sText;
    PbProgress->Position = nPos;
    Refresh();
    Application->ProcessMessages();
    Application->ProcessMessages();
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::TmrProgressTimer(TObject *Sender)
{
    int nPos = PbProgress->Position + 1;
    if (nPos > PbProgress->Properties->Max) nPos = 10;
    Step(PbProgress->Properties->Text, nPos);    
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::SetQry(TADOQuery *adoQry)
{
    m_pThrDb = new TDBThread;
    m_pThrDb->SetQry(adoQry, FParentForm);
    m_pThrDb->Resume();
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::FormClose(TObject *Sender, TCloseAction &Action)
{
    TmrProgress->Enabled = false;    
    try {
        if (m_pThrDb)
        {
            m_pThrDb->Terminate();
        }
        m_pThrDb = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmDbProgress::TermThread()
{
    try {
        if (m_pThrDb)
        {
            m_pThrDb->Terminate();
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------

