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

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "ITSLogF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "FrmVmsLogDsplSubF.h"
#include "CDSFontF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#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 "dxSkinMcSkin"
#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 "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxLabel"
#pragma link "cxGroupBox"
#pragma link "cxDropDownEdit"
#pragma link "cxImage"
#pragma link "cxSpinEdit"
#pragma resource "*.dfm"

//TFrmVmsLogMsgSub *FrmVmsLogMsgSub = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmVmsLogDsplSub::TFrmVmsLogDsplSub(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;

    TvVmsMsg->OptionsView->DataRowHeight  = 0;
    TvVmsMsg->OptionsView->CellAutoHeight = true;
    TvVmsMsg->OptionsView->Indicator      = false;

    TvVmsMsg->OptionsCustomize->ColumnMoving  = false;
    TvVmsMsg->OptionsCustomize->ColumnSorting = false;

    TvVmsMsg->Columns[0]->Width = 40;
    TvVmsMsg->Columns[0]->Options->HorzSizing = false;

    FVmsNmbr = "";
    FDsplDt  = "";
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = Caption + ":  " + lblText1->Caption;//조회결과가 없습니다.";
    LblSearch->Caption = lblText2->Caption + FStDateTime.SubString(1, 10) + " ~ " + FEdDateTime.SubString(1, 10);
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::FormShow(TObject *Sender)
{
    FormInit();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::CommClose()
{
    //CMM_SaveForm(g_sFormsDir, this);
    //FrmVmsLogSub = NULL;
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsLogDsplSub::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT A.*, B.VMS_CTLR_ID, B.VMS_NM,                     \r\n"
           "       B.FORMW, B.FORMH                                  \r\n"
           "  FROM TB_VMS_DSPL_HS  A,                                \r\n"
           "       (SELECT A.*,                                      \r\n"
           "               B.VMS_TYPE_NM AS VMS_TYPE_NM,             \r\n"
           "               B.VMS_WDTH    AS FORMW,                   \r\n"
           "               B.VMS_HGHT    AS FORMH                    \r\n"
           "          FROM TB_VMS_CTLR A,                            \r\n"
           "               TB_VMS_TYPE B                             \r\n"
           "         WHERE A.VMS_TYPE_CD = B.VMS_TYPE_CD) B          \r\n"
           " WHERE B.VMS_CTLR_NMBR IN(" + FIdList + ")               \r\n"
           "   AND A.DSPL_DT BETWEEN :p01 AND :p02                   \r\n"
           "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                 \r\n"
           " ORDER BY A.VMS_CTLR_NMBR, A.DSPL_DT, A.PHASE            \r\n";

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("TFrmVmsLogDsplSub::SelHistory", String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("TFrmVmsLogDsplSub::SelHistory", String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
    DspHistory();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
    	TcxGridChartSeries *pChart = NULL;

        CMM_ClearGridTableView(TvList);

        int nRow = 0;
        int nDataCnt = 0;
        try {
            TvList->BeginUpdate(lsimImmediate);

            nDataCnt = pADO->RecordCount;
            m_pGDC->RecordCount = nDataCnt;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                m_pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                m_pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_NM")->AsString;
                m_pGDC->Values[nRow][Col04->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("DSPL_DT")->AsString).FormatString(STR_DATETIME);
                m_pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("PHASE")->AsString;
                m_pGDC->Values[nRow][Col06->Index] = pADO->FieldByName("VMS_DSPL_MSG_TXT")->AsString;
                m_pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("DNLD_YN")->AsString == "Y" ? lblSucc->Caption : lblFai->Caption;//"성공" : "실패";

                //m_pGDC->Values[nRow][Col11->Index] = pADO->FieldByName("FORMSIZE")->AsString+" ";
                m_pGDC->Values[nRow][Col12->Index] = pADO->FieldByName("FORMW")->AsString.ToIntDef(384);
                m_pGDC->Values[nRow][Col13->Index] = pADO->FieldByName("FORMH")->AsString.ToIntDef(64);

                m_pGDC->Values[nRow][Col14->Index] = pADO->FieldByName("DSPL_DT")->AsString;

                int nFormType = pADO->FieldByName("VMS_SCH_FORM_TYPE")->AsString.ToIntDef(0);
                String sFormType = "";

                //VMS 폼 스케쥴 유형(0:교통,1:돌발,2:공사행사,3:홍보,4:우회,5:정체,6:안전,7:동영상,8:스트리밍영상,9:대기환경,10:주차정보)
                switch(nFormType)
                {
                case  0: sFormType = lblForm11->Caption; break;//"교통정보;
                case  1: sFormType = lblForm12->Caption; break;//"돌발";
                case  2: sFormType = lblForm13->Caption; break;//"공사행사";
                case  3: sFormType = lblForm14->Caption; break;//"홍보";
                case  4: sFormType = lblForm15->Caption; break;//"도형식배경소통정보";
                case  5: sFormType = lblForm20->Caption; break;//"우회";
                case  6: sFormType = lblForm30->Caption; break;//"정체";
                case  7: sFormType = lblForm40->Caption; break;//"동영상";
                case  8: sFormType = lblForm50->Caption; break;//"스트리밍영상";
                case  9: sFormType = lblForm90->Caption; break;//"대기환경";
                case 10: sFormType = lblForm91->Caption; break;//"주차정보";
                }

                m_pGDC->Values[nRow][Col15->Index] = sFormType;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + lblCnt->Caption;//" 건";
        }
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("TFrmVmsLogDsplSub::DspHistory", String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("TFrmVmsLogDsplSub::DspHistory", String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;

    CMM_ExportToExcelFile(sTitle, pGrid, pView, this);
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::FormClose(TObject *Sender, TCloseAction &Action)
{
    POST_MSG(FParent, WM_SUBFORM_CLOSE, 0, 0);
    CommClose();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::FormDestroy(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::Col07CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;
        if (sVmsMode == lblFai->Caption)//"실패")
            tColor = clRed;
        //else                    tColor = clLime;

        ACanvas->SetBrushColor(tColor);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::TvListFocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    if (!AFocusedRecord) return;
    DisplayVmsMsg();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::DisplayVmsMsg()
{
    TcxGridDataController *pGDC = TvList->DataController;
    int nSelIdx = CMM_GetRecordIndex(TvList);
    if (nSelIdx < 0) return;

    String sVmsNmbr = VarToStr(pGDC->Values[nSelIdx][Col00->Index]);
    String sDsplDt  = VarToStr(pGDC->Values[nSelIdx][Col14->Index]);

    EdId->Text    = VarToStr(pGDC->Values[nSelIdx][Col01->Index]);
    EdName->Text  = VarToStr(pGDC->Values[nSelIdx][Col02->Index]);
    EdSvcDt->Text = VarToStr(pGDC->Values[nSelIdx][Col04->Index]);

    FWidth  = pGDC->Values[nSelIdx][Col12->Index];
    FHeight = pGDC->Values[nSelIdx][Col13->Index];

    if (FVmsNmbr != sVmsNmbr || FDsplDt != sDsplDt)
    {
        LoadVmsMsg(sVmsNmbr, sDsplDt);
    }
}
//---------------------------------------------------------------------------

TColor CodeToColor(int ACode)
{
	TColor clColor = clBlack;
	switch(ACode)
	{
	case 0: clColor = clBlack;  break;
	case 1: clColor = clRed;    break;
	case 2: clColor = clLime;   break;
	case 3: clColor = clYellow; break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

TColor CodeToFontColor(int ACode)
{
	TColor clColor = clBlack;
    TCDSFontColor *pObj = CDSFontManager->FColors.Find(ACode);
    if (pObj) clColor = (TColor)pObj->VMS_FONT_COLR_VAL;

	return clColor;
}
//---------------------------------------------------------------------------

String CodeToFontName(int ACode)
{
    AnsiString sFont = "맑은 고딕";
    if (g_AppCfg.sLang == "en")
    {
        sFont = "Tahoma";
    }

    TCDSFontName *pObj = CDSFontManager->FNames.Find(ACode);
    if (pObj) sFont = (String)pObj->VMS_FONT_NAME_NM;

	return sFont;
}
//---------------------------------------------------------------------------

TColor GradeToFillColor(int AGrade)
{
	TColor clColor = clSilver;
	switch(AGrade)
	{
	case 1: clColor = clLime;   break;
	case 2: clColor = clYellow; break;
	case 3: clColor = clRed;    break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDsplSub::LoadVmsMsg(String AVmsNmbr, String ADsplDt)
{
    FVmsNmbr = AVmsNmbr;
    FDsplDt  = ADsplDt;

    CMM_ClearGridTableView(TvVmsMsg);
    EdMsgCnt->Text = "";

    String     sQry;
    TADOQuery *pADO = NULL;
	TImage    *pImage = NULL;

#if 1
    //FOR Tibero
    sQry = "SELECT A.PHASE, A.VMS_DSPL_MSG_DATA, DNLD_YN,           \r\n"
           "       SUBSTR(A.VMS_DSPL_MSG_DATA,    1, 400) AS MSG1,  \r\n"
           "       SUBSTR(A.VMS_DSPL_MSG_DATA,  401, 400) AS MSG2,  \r\n"
           "       SUBSTR(A.VMS_DSPL_MSG_DATA,  801, 400) AS MSG3,  \r\n"
           "       SUBSTR(A.VMS_DSPL_MSG_DATA, 1201, 300) AS MSG4   \r\n"
           "  FROM TB_VMS_DSPL_HS A                                 \r\n"
           " WHERE A.VMS_CTLR_NMBR = :p01                           \r\n"
           "   AND A.DSPL_DT       = :p02                           \r\n"
           " ORDER BY A.PHASE                                       \r\n";
#else
    sQry = "SELECT A.PHASE, A.VMS_DSPL_MSG_DATA, DNLD_YN          \r\n"
           "  FROM TB_VMS_DSPL_HS A                               \r\n"
           " WHERE A.VMS_CTLR_NMBR = :p01                         \r\n"
           "   AND A.DSPL_DT       = :p02                         \r\n"
           " ORDER BY A.PHASE                                     \r\n";
#endif

typedef enum en_vms_object_type
{
	vms_obj_none,       /* 0:정보없음 */
	vms_obj_text,       /* 1:문자열 */
	vms_obj_bitmap,     /* 2:Bitmap ID */
	vms_obj_draw,       /* 3:Draw */
} EN_VMS_OBJECT_TYPE;

    TcxDataController *pGDC = TvVmsMsg->DataController;
    try
    {
        int nRow;
        TvVmsMsg->BeginUpdate(lsimImmediate);
        pGDC->BeginUpdate();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AVmsNmbr);
            ITSDb_SQLBind(pADO, "p02", ADsplDt);
            ITSDb_SQLOpen(pADO);

            EdMsgCnt->Text = "Forms: " + String(pADO->RecordCount);
            for( ; !pADO->Eof; pADO->Next())
            {
#if 1
                AnsiString sMsg1 = pADO->FieldByName("MSG1")->AsString;
                AnsiString sMsg2 = pADO->FieldByName("MSG2")->AsString;
                AnsiString sMsg3 = pADO->FieldByName("MSG3")->AsString;
                AnsiString sMsg4 = pADO->FieldByName("MSG4")->AsString;
                AnsiString sData = sMsg1+sMsg2+sMsg3+sMsg4;
#else
                String sData = pADO->FieldByName("VMS_DSPL_MSG_DATA")->AsString;
#endif

                    //폼종류/표출시간/배경색상
                    //sCont.printf("%02d%02X%02X|", pForm->FormKind, pForm->DisplayTime, pForm->BkColor);
                int nFormKind = StrToInt("0x" + sData.SubString(1,2));
                int nDispTime = StrToInt("0x" + sData.SubString(3,2));
                int nBkColor  = StrToInt("0x" + sData.SubString(5,2));
                int nObjType, nObjLen;
                try
                {
                    TRect rc;
                    pImage = new TImage(NULL);
        			pImage->Canvas->Lock();
                    pImage->AutoSize = true;
                    pImage->Picture->Bitmap->PixelFormat = pf24bit;
                    pImage->Picture->Bitmap->Height = FHeight;
                    pImage->Picture->Bitmap->Width  = FWidth;
                    SetStretchBltMode(pImage->Picture->Bitmap->Canvas->Handle, MAXSTRETCHBLTMODE);//HALFTONE);//MAXSTRETCHBLTMODE)//STRETCH_HALFTONE;
                    pImage->Canvas->Brush->Color    = CodeToColor(nBkColor);
                    pImage->Canvas->FloodFill(0, 0, pImage->Picture->Bitmap->Canvas->Pixels[0][0], fsSurface);

                    TStringList *pList = new TStringList;
                    pList->Delimiter = '|';
                    pList->StrictDelimiter = true;
                    pList->DelimitedText = sData;

                    int nObjCnt = pList->Count;
                    if (nObjCnt > 1)
                    {
                        for (int ii = 1; ii < nObjCnt-1; ii++)
                        {
                            String sTmp = pList->Strings[ii];
                            nObjType = StrToInt("0x" + sTmp.SubString(1, 2));
                            nObjLen  = StrToInt("0x" + sTmp.SubString(3, 2));

                            int nBlink   = StrToInt("0x" + sTmp.SubString( 5, 2));
                            int nLeft    = StrToInt("0x" + sTmp.SubString( 7, 4));
                            int nTop     = StrToInt("0x" + sTmp.SubString(11, 4));
                            int nBkColor = StrToInt("0x" + sTmp.SubString(15, 2));

                            if (nObjType == vms_obj_text)  //text
                            {
                                int nFontSize  = StrToInt("0x" + sTmp.SubString(17, 2));
                                int nFontColor = StrToInt("0x" + sTmp.SubString(19, 2));
                                int nFontName  = StrToInt("0x" + sTmp.SubString(21, 2));
                                int nFontBold  = StrToInt("0x" + sTmp.SubString(23, 2));
                                int nTextSize  = StrToInt("0x" + sTmp.SubString(25, 2));
                                String sText   =                 sTmp.SubString(27, nTextSize);

                                pImage->Canvas->Brush->Color = CodeToColor(nBkColor);
                                pImage->Canvas->Font->Color  = CodeToFontColor(nFontColor);
                                pImage->Canvas->Font->Name   = CodeToFontName(nFontName);
                                pImage->Canvas->Font->Size   = nFontSize;
                                if (nFontBold)
                                    pImage->Canvas->Font->Style = TFontStyles() << fsBold;
                                else
                                    pImage->Canvas->Font->Style = TFontStyles();

                                pImage->Canvas->TextOut(nLeft, nTop, sText);
                            }
                            else
                            if (nObjType == vms_obj_bitmap)  //symbol id
                            {
                                int nWidth  = StrToInt("0x" + sTmp.SubString(17, 4));
                                int nHeight = StrToInt("0x" + sTmp.SubString(21, 4));
                                String sBmpId = sTmp.SubString(25, sTmp.Length()-16);
                                TImage *pTmpImage = GetSymbolImage(sBmpId);
                                if (pImage && pTmpImage)
                                {
                                    rc.Top    = nTop;
                                    rc.Bottom = nTop + nHeight;
                                    rc.Left   = nLeft;
                                    rc.Right  = nLeft + nWidth;

                                    pImage->Picture->Bitmap->Canvas->StretchDraw(rc, pTmpImage->Picture->Graphic);
                                    //pImage->Canvas->Draw(nLeft, nTop, pTmpImage->Picture->Graphic);
                                    pTmpImage->Picture->Assign(NULL);
                                    SAFE_DELETE(pTmpImage);
                                }
                            }
                            else
                            if (nObjType == vms_obj_draw)  //draw traffic
                            {
                                TStringList *pTmpList = new TStringList;
                                pTmpList->Delimiter = '+';
                                pTmpList->StrictDelimiter = true;
                                pTmpList->DelimitedText = sTmp;

                                if (pTmpList->Count == 2)
                                {
                                    sTmp = pTmpList->Strings[0];
                                    int nWidth  = StrToInt("0x" + sTmp.SubString(17, 4));
                                    int nHeight = StrToInt("0x" + sTmp.SubString(21, 4));
                                    String sBmpId = sTmp.SubString(25, sTmp.Length()-16);
                                    TImage *pTmpImage = GetSymbolImage(sBmpId);
                                    if (pImage && pTmpImage)
                                    {
                                        sTmp = pTmpList->Strings[1];
                                        if (sTmp != "X")
                                        {
                                            int nCellX, nCellY, nTraff;
                                            int nVertex = StrToInt("0x" + sTmp.SubString(1, 2));
                                            for (int jj = 0; jj < nVertex; jj++)
                                            {
                                                nCellX = sTmp.SubString((jj*7)+3,3).ToIntDef(0);
                                                nCellY = sTmp.SubString((jj*7)+6,3).ToIntDef(0);
                                                nTraff = sTmp.SubString((jj*7)+9,1).ToIntDef(0);
                                                pTmpImage->Picture->Bitmap->Canvas->Brush->Color = GradeToFillColor(nTraff);
                                                pTmpImage->Picture->Bitmap->Canvas->FloodFill(nCellX,
                                                                                              nCellY,
                                                                                              pTmpImage->Picture->Bitmap->Canvas->Pixels[nCellX][nCellY],
                                                                                              fsSurface);
                                            }
                                            rc.Top    = nTop;
                                            rc.Bottom = nTop + nHeight;
                                            rc.Left   = nLeft;
                                            rc.Right  = nLeft + nWidth;

                                            pImage->Picture->Bitmap->Canvas->StretchDraw(rc, pTmpImage->Picture->Graphic);
                                            //pImage->Canvas->Draw(nLeft, nTop, pTmpImage->Picture->Graphic);
                                            pTmpImage->Picture->Assign(NULL);
                                        }
                                        SAFE_DELETE(pTmpImage);
                                    }
                                }
                                SAFE_DELETE(pTmpList);
                            }
                        }
                    }
                    SAFE_DELETE(pList);

                    Graphics::TBitmap *pBitmap = pImage->Picture->Bitmap;
                    TStream	*pImgStream = new TMemoryStream();
                    try
                    {
                        nRow = pGDC->AppendRecord();
                        pGDC->Values[nRow][ColSch01->Index] = String(nRow+1);

                        pBitmap->SaveToStream(pImgStream);
                        void    *p;
                        Variant  v;
                        int aBound[]={0, pImgStream->Size - 1};
                        v = VarArrayCreate(aBound, 1, varByte);
                        p = VarArrayLock(v);
                        pImgStream->Position = 0;
                        pImgStream->Read(p, pImgStream->Size);
                        VarArrayUnlock(v);
                        pGDC->Values[nRow][ColSch02->Index] = v;
                    } catch(Exception &e) {}
                    SAFE_DELETE(pImgStream);
                }
                __finally
                {
                    pImage->Picture->Assign(NULL);
                    pImage->Canvas->Unlock();
        			SAFE_DELETE(pImage);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TFrmVmsLogDsplSub::LoadVmsMsg", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TFrmVmsLogDsplSub::LoadVmsMsg", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        pGDC->EndUpdate();
        TvVmsMsg->EndUpdate();
        //TvVmsMsg->ApplyBestFit(NULL, false, false);
    }
}
//---------------------------------------------------------------------------

TImage * __fastcall TFrmVmsLogDsplSub::GetSymbolImage(String ASymbolId)
{
	String     sQry;
    TADOQuery *pADO   = NULL;
    TImage    *pImage = NULL;

    sQry = "SELECT IMAG_DATA            \r\n"
           "  FROM TB_VMS_SYMB_LIB      \r\n"
           " WHERE SYMB_LIB_NMBR = :p01 \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ASymbolId.SubString(1,ASymbolId.Length()-1));
            ITSDb_SQLOpen(pADO);
            pADO->First();
            if (!pADO->IsEmpty() || !pADO->Eof)
            {
                TStream *pStream = NULL;
                pStream = pADO->CreateBlobStream(pADO->FieldByName("IMAG_DATA"), bmRead);
                try
                {
                    try {
                        pImage = new TImage(NULL);
                        pImage->Canvas->Lock();
                        pStream->Position = 0;
                        pImage->Picture->Bitmap->LoadFromStream(pStream);
                    } catch(...) {}
                }__finally{
                    pImage->Canvas->Unlock();
                    if (pStream)
                        delete pStream;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TFrmVmsLogDsplSub::GetSymbolImage", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TFrmVmsLogDsplSub::GetSymbolImage", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return pImage;
}
//---------------------------------------------------------------------------

