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


#pragma hdrstop

#include "TVmsFormF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TVmsFormObj::TVmsFormObj()
{
    pImageData   = NULL;
    pImageStream = NULL;
    IFSC_ID = NULL;
}
//---------------------------------------------------------------------------

TVmsFormObj::~TVmsFormObj()
{
    if (pImageData)
    {
        pImageData->Picture->Bitmap->FreeImage();
        pImageData->Picture->Assign(NULL);
        delete pImageData;
        pImageData = NULL;
    }
    if (pImageStream)
    {
        pImageStream->Clear();
        pImageStream->Position = 0;
        delete pImageStream;
        pImageStream = NULL;
    }
}
//---------------------------------------------------------------------------
void TVmsFormObj::Clear()
{
}
//---------------------------------------------------------------------------
TImage *TVmsFormObj::NewImage()
{
    bool bUnLock = false;
    try
    {
        if (pImageData)
        {
            pImageData->Picture->Bitmap->Canvas->Lock();
            bUnLock = true;
        }
        try
        {
            if (pImageStream)
            {
                pImageStream->Clear();
                pImageStream->Position = 0;
            }
            else
            {
                pImageStream = new TMemoryStream();
            }

            if (pImageData)
            {
                pImageData->Picture->Bitmap->FreeImage();
                pImageData->Picture->Assign(NULL);
            }
            else
            {
                pImageData = new TImage(NULL);
            }
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        if (bUnLock)
        {
            pImageData->Picture->Bitmap->Canvas->Unlock();
        }
    }

    return pImageData;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TVmsForm::TVmsForm()
{
    FTotal  = 0;

    SvcRes  = false;
    FtpRes  = false;
    FObjects= 0;

    TrfBakImgId = "";

    pCriticalSection = new TCriticalSection();

	pBitmap = new Graphics::TBitmap;
	pStream = new TMemoryStream();
}
//---------------------------------------------------------------------------
TVmsForm::~TVmsForm()
{
    FLists.RemoveAll();

    if (pBitmap)
    {
        pBitmap->FreeImage();
        pBitmap->Assign(NULL);
        delete pBitmap;
        pBitmap = NULL;
    }
    if (pStream)
    {
        pStream->Clear();
        pStream->Position = 0;
        delete pStream;
        pStream = NULL;
    }

    if (pCriticalSection)
    {
        delete pCriticalSection;
        pCriticalSection = NULL;
    }
}
//---------------------------------------------------------------------------
void TVmsForm::Clear()
{
    //FLists.RemoveAll();
    SvcRes  = false;
    FtpRes  = false;
    FObjects = 0;
}
//---------------------------------------------------------------------------
void TVmsForm::Init(int AWidth, int AHeight)
{
    try
    {
        pBitmap->PixelFormat = pf4bit;
        pBitmap->Width = AWidth;
        pBitmap->Height= AHeight;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------
void TVmsForm::FormClear(TColor ABkColor)
{
    try
    {
        pBitmap->FreeImage();
        pBitmap->Assign(NULL);

        pBitmap->PixelFormat = pf4bit;
        pBitmap->Width = Width;
        pBitmap->Height= Height;

        pBitmap->Canvas->Pen->Color = clWhite;
        pBitmap->Canvas->Pen->Width = 0;
        pBitmap->Canvas->Pen->Style = psClear;
        pBitmap->Canvas->Brush->Style = bsSolid;
        pBitmap->Canvas->Brush->Color = ABkColor;
        pBitmap->Canvas->Rectangle(0, 0, Width+1, Height+1);

        pStream->Clear();
        pStream->Position = 0;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

TVmsFormObj *TVmsForm::GetItem(int AIdx)
{
    TVmsFormObj *pFormObj = FLists.Find(AIdx);
    return pFormObj;
}
//---------------------------------------------------------------------------
int TVmsForm::AddFormObject(TVmsFormObj *AFormObj)
{
    FLists.Push(FObjects++, AFormObj);
    return FObjects;
}
//---------------------------------------------------------------------------
TVmsFormObj *TVmsForm::AddFormObject()
{
    TVmsFormObj *pVmsFormObj = NULL;

    if (FObjects < FTotal)
    {
        pVmsFormObj = GetItem(FObjects);
        FObjects++;
    }
    else
    {
        pVmsFormObj = new TVmsFormObj();
        FLists.Push(FTotal++, pVmsFormObj);
        FObjects++;
    }
    return pVmsFormObj;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//extern TVmsFormManager *VmsFormManager = NULL;
//---------------------------------------------------------------------------

TVmsFormManager::TVmsFormManager()
{
    FTotal   = 0;
    FObjects = 0;
}
//---------------------------------------------------------------------------
TVmsFormManager::~TVmsFormManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsFormManager::Clear()
{
#ifdef VMS_CLEAR
    FLists.RemoveAll();
#else
    FObjects = 0;
    FOR_STL(TVmsForm*, pForm, FLists)
    {
        pForm->Clear();
    }
#endif
}
//---------------------------------------------------------------------------
TVmsForm *TVmsFormManager::GetItem(int AIdx)
{
    TVmsForm *pForm = FLists.Find(AIdx);
    return pForm;
}
//---------------------------------------------------------------------------
int TVmsFormManager::AddForm(TVmsForm *AForm)
{
    FLists.Push(FObjects++, AForm);
    return FObjects;
}
//---------------------------------------------------------------------------
TVmsForm *TVmsFormManager::AddForm()
{
    TVmsForm *pVmsForm = NULL;

    if (FObjects < FTotal)
    {
        pVmsForm = GetItem(FObjects);
        FObjects++;
    }
    else
    {
        pVmsForm = new TVmsForm();
        pVmsForm->Init(FormWidth, FormHeight);
        FLists.Push(FTotal++, pVmsForm);
        FObjects++;
    }
    return pVmsForm;
}
//---------------------------------------------------------------------------


