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

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "FrmVmsOprMainF.h"
#pragma hdrstop

#include "VMSBACKMF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma resource "*.dfm"

//TVMSBACKM *VMSBACKM = NULL;

//---------------------------------------------------------------------------
__fastcall TVMSBACKM::TVMSBACKM(TComponent* AOwner, HWND AHandle, String ACaption, int AScreenIdx)
    : TForm(AOwner)
{           
    FListForm = new TList();
    FScreenName  = ACaption;
    FScreenIndex = AScreenIdx;
    FPlay        = false;

    FMinWidth  = 1000;
    FMinHeight = 1000;
    FMaxWidth  = 0;
    FMaxHeight = 0;

    FWndParent = AHandle;
    Caption    = ACaption;

    FDisplayMode = 0;
    FViewWidth   = 320;

    FMonitoringType = enMonitoringPhase;
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::FormCreate(TObject *Sender)
{
    ScrollBox->ParentColor = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::FormShow(TObject *Sender)
{
    Refresh();
    ScrollBox->Align = alClient;
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false; 
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::ScrollBoxClick(TObject *Sender)
{
    ScrollBox->SetFocus();    
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::ScrollBoxMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled)
{
    ScrollBox->VertScrollBar->Position -= WheelDelta;    
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::SetPlay(bool APlay)
{
//AnsiString sTrace;
//if (APlay) sTrace = Caption + " TVMSBACKM: SetPlay = Active";
//else       sTrace = Caption + " TVMSBACKM: SetPlay = DeActive";
//ITSUtil_Trace(sTrace.c_str());

    try
    {
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            if (FMonitoringType == enMonitoringPhase)
            {
                TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
                pForm->Play = APlay;
            }
            else
            if (FMonitoringType == enMonitoringModule)
            {
                TVMSMODL0 *pForm = (TVMSMODL0*)FListForm->Items[ii];
                pForm->Play = APlay;
            }
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::FormResize(TObject *Sender)
{
    RecalFormPosition();
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::RecalFormPosition()
{
    int h = 0;
    for (int ii = 0; ii < FpnlForm->ControlCount; ii++)
    {
        if (FpnlForm->Controls[ii]->BoundsRect.Bottom > h)
            h = FpnlForm->Controls[ii]->BoundsRect.Bottom;
    }
    FpnlForm->Height = h+50;
    ScrollBox->HorzScrollBar->Range = 0;
    ScrollBox->VertScrollBar->Range = FpnlForm->Height;
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::RefreshVmsStatus()
{
    try
    {
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            if (FMonitoringType == enMonitoringPhase)
            {
                TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
                pForm->RefreshVmsStatus();
            }
            else
            if (FMonitoringType == enMonitoringModule)
            {
                TVMSMODL0 *pForm = (TVMSMODL0*)FListForm->Items[ii];
                pForm->RefreshVmsStatus();
            }
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::RefreshVmsMessage()
{
    try
    {
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            if (FMonitoringType == enMonitoringPhase)
            {
                TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
                pForm->RefreshVmsMessage();
            }
            else
            if (FMonitoringType == enMonitoringModule)
            {
                TVMSMODL0 *pForm = (TVMSMODL0*)FListForm->Items[ii];
                pForm->RefreshVmsMessage();
            }
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSBACKM::AddVmsForm(String AVmsNmbr)
{
    TVmsCtlr *pObj = VmsCtlrManager->FLists.Find(AVmsNmbr);
    if (!pObj) return false;

    if (pObj->WIDTH  > FMaxWidth)  FMaxWidth  = pObj->WIDTH;
    if (pObj->WIDTH  < FMinWidth)  FMinWidth  = pObj->WIDTH;
    if (pObj->HEIGHT > FMaxHeight) FMaxHeight = pObj->HEIGHT;
    if (pObj->HEIGHT < FMinHeight) FMinHeight = pObj->HEIGHT;

    if (FMaxWidth > 512) FMaxWidth = 512;
    if (FMinWidth < 288) FMinWidth = 288;

    if (FMinHeight <  64) FMinHeight = 64;
    if (FMaxHeight > 384) FMaxHeight = 384;

    FMinWidth = 384;
    if (FMonitoringType == enMonitoringPhase)
    {
        TVMSFORM0 *pForm = new TVMSFORM0(this, pObj);
        //pForm->SetFormSize(pObj->WIDTH);
        pForm->SetFormSize(FMinWidth);
        pForm->Parent     = FpnlForm;
        pForm->FScrollBox = ScrollBox;
        pForm->FWndParent = Handle;
        pForm->Show();
        FListForm->Add(pForm);
    }
    else
    if (FMonitoringType == enMonitoringModule)
    {
        TVMSMODL0 *pForm = new TVMSMODL0(this, pObj);
        pForm->SetFormSize(pObj->WIDTH);
        pForm->Parent     = FpnlForm;
        pForm->FScrollBox = ScrollBox;
        pForm->FWndParent = Handle;
        pForm->Show();
        FListForm->Add(pForm);
    }

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::FpnlFormClick(TObject *Sender)
{
    ScrollBox->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::OnVmsSelectMessage(TMessage &Msg)
{
    if (FWndParent)
    {
        POST_MSG(FWndParent, WM_VMS_SELECT, (WPARAM)Msg.WParam, (LPARAM)Msg.LParam);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::OnVmsSelectDblClickMessage(TMessage &Msg)
{
    if (FWndParent)
    {
        POST_MSG(FWndParent, WM_VMS_SELECT_DBLCLICK, (WPARAM)Msg.WParam, (LPARAM)Msg.LParam);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::FormDestroy(TObject *Sender)
{
    ClearFormList();
    SAFE_DELETE(FListForm);
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::SetFormSize(int AWidth)
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            if (FMonitoringType == enMonitoringPhase)
            {
                TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
                pForm->SetFormSize(AWidth);
                FViewWidth = pForm->Width;
            }
            else
            if (FMonitoringType == enMonitoringModule)
            {
                TVMSMODL0 *pForm = (TVMSMODL0*)FListForm->Items[ii];
                pForm->SetFormSize(AWidth);
                FViewWidth = pForm->Width;
            }
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSBACKM::ClearFormList()
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            if (FMonitoringType == enMonitoringPhase)
            {
                TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
                pForm->Hide();
                SAFE_DELETE(pForm);
            }
            else
            if (FMonitoringType == enMonitoringModule)
            {
                TVMSMODL0 *pForm = (TVMSMODL0*)FListForm->Items[ii];
                pForm->Hide();
                SAFE_DELETE(pForm);
            }
        }
        FListForm->Clear();
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------


