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

#include <vcl.h>
#include <jpeg.hpp>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "WindowMsgF.h"
#include "ITSLangTransF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "PRK0050MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#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 "cxRadioGroup"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDBData"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridDBTableView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxCheckBox"
#pragma link "cxImage"
#pragma link "cxButtonEdit"
#pragma link "cxBlobEdit"
#pragma resource "*.dfm"

TPRK0050M *PRK0050M = NULL;
//---------------------------------------------------------------------------

__fastcall TPRK0050M::TPRK0050M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = Caption;//"VMS 소통정보 배경이미지관리";

	FListSymb = new TList;

    EditMode = false;

#if 0
    TvList->OptionsView->DataRowHeight  = 0;
    TvList->OptionsView->CellAutoHeight = true;
    TvList->OptionsView->Indicator      = false;

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

    TvList->Columns[0]->Width = 60;
    TvList->Columns[0]->Options->HorzSizing = false;
#endif

    //http://docwiki.embarcadero.com/RADStudio/Rio/en/Colors_in_the_VCL
    m_Color[ 0] = Graphics::clLime;
    m_Color[ 1] = (TColor)RGB(0xFF, 0x00, 0x00);
    m_Color[ 2] = (TColor)RGB(0x00, 0x00, 0xFF);
    m_Color[ 3] = (TColor)RGB(0xFF, 0x82, 0x00);
    m_Color[ 4] = (TColor)RGB(0x94, 0x00, 0xD3);
    m_Color[ 5] = Graphics::clPurple;//(TColor)RGB(0x00, 0x00, 0x00);
    m_Color[ 6] = (TColor)RGB(0x82, 0x82, 0x82);
    m_Color[ 7] = (TColor)RGB(0x1E, 0x90, 0xFF);
    m_Color[ 8] = (TColor)RGB(0xB9, 0x06, 0x2F);
    m_Color[ 9] = (TColor)RGB(0x8c, 0x00, 0x8c);
    m_Color[10] = (TColor)RGB(0xFF, 0x82, 0x00);
    m_Color[11] = (TColor)RGB(0x94, 0x00, 0xD3);
    m_Color[12] = (TColor)RGB(0xB0, 0xC4, 0xDE);
    m_Color[13] = (TColor)RGB(0x00, 0xBF, 0xFF);
    m_Color[14] = (TColor)RGB(0x87, 0xCE, 0xEB);
    m_Color[15] = (TColor)RGB(0x87, 0xCE, 0xFA);
    m_Color[16] = (TColor)RGB(0xAD, 0xD8, 0xE6);
    m_Color[17] = (TColor)RGB(0xB0, 0xE0, 0xE6);
    m_Color[18] = (TColor)RGB(0xE6, 0xE6, 0xFA);
    m_Color[19] = (TColor)RGB(0xF0, 0xF8, 0xFF);

    ScrollBox1->Color = Color;
}
//---------------------------------------------------------------------------
TColor TPRK0050M::GetDisplayColor(int ASeq)
{
    //return g_DispColor[ASeq % MAX_DISPCOLOR];
    return m_Color[ASeq % MAX_PARK_CELL_COLOR];
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::FormShow(TObject *Sender)
{
    Refresh();

    LoadPrkPlce();

    FormInit();
    ChangeEditMode(1);
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TPRK0050M::LoadPrkPlce()
{
    CbPrk->Properties->Items->Clear();

    FOR_STL(TPrkPlce*, pObj, PrkPlceManager->FLists)
    {
        if (pObj->DEL_YN != "N") continue;

        CbPrk->Properties->Items->AddObject(" [" + pObj->CTLR_NMBR + "] " + pObj->NAME, (TObject*)pObj);
    }
    CbPrk->ItemIndex = 0;

    //LoadPrkFlr();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::CbPrkPropertiesChange(TObject *Sender)
{
    LoadPrkFlr();
    ChangeEditMode(1);
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::LoadPrkFlr()
{
    try {
        CbFlr->Properties->OnChange = NULL;
        CbFlr->Properties->Items->Clear();
        FormInit();

        TPrkPlce *pObj = (TPrkPlce*)CbPrk->Properties->Items->Objects[CbPrk->ItemIndex];
        if (pObj == NULL) {
            return;
        }

        //if (pObj->FDevs.Size() == 0) {
            PrkPlceManager->LoadFromDbDev(pObj);
        //}
        //if (pObj->FFlrs.Size() == 0) {
            PrkPlceManager->LoadFromDbFlr(pObj);
            PrkPlceManager->LoadFromDbFlrDev(pObj);
        //}

        FOR_STL(TPrkPlceFlr*, pFlr, pObj->FFlrs)
        {
            CbFlr->Properties->Items->AddObject(" " + pFlr->FLR_NO + " ", (TObject*)pFlr);
        }
        CbFlr->ItemIndex = -1;
    }
    __finally {
        CbFlr->Properties->OnChange = CbFlrPropertiesChange;
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::CbFlrPropertiesChange(TObject *Sender)
{
    Application->ProcessMessages();
    LoadPrkDetailInfo();
    Application->ProcessMessages();
    ChangeEditMode(3);
    EdFocus->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TPRK0050M::LoadPrkDetailInfo()
{
    FormInit();

    TPrkPlce *pObj = (TPrkPlce*)CbPrk->Properties->Items->Objects[CbPrk->ItemIndex];
    if (pObj == NULL) {
        return;
    }
    TPrkPlceFlr *pFlr = (TPrkPlceFlr*)CbFlr->Properties->Items->Objects[CbFlr->ItemIndex];
    if (pFlr == NULL) {
        return;
    }
    SeCmprtCo->Value = pFlr->FLR_PRK_CMPRT_CO;

    LoadPrkFlrImage(pObj->CTLR_NMBR, pFlr->FLR_NO);
    LoadPrkFlrDevInfo();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::FormInit()
{
    SeCmprtCo->Value = 1;
    EdImageFileName->Text = "";

    ImgPreView->Picture->Bitmap->FreeImage();
    ImgPreView->Picture->Bitmap = NULL;

    ImgTemp->Picture->Bitmap->FreeImage();
    ImgTemp->Picture->Bitmap = NULL;

    SePosX->Value = 0;
    SePosY->Value = 0;
    SeDevId->Value = 1;

    CMM_ClearGridTableView(TvImageCell);

    FNewDb = false;
}
//---------------------------------------------------------------------------

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

}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    PRK0050M = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------

bool __fastcall TPRK0050M::UpdatePrkFlrInfo(String APrkNmbr, String AFlrNo)
{
    if (!FImageUpdate) {
        return true;
    }

    String sQry;
    TADOQuery *pADO = NULL;

	TMemoryStream *dataStream = new TMemoryStream();
   	ImgTemp->Picture->Bitmap->SaveToStream(dataStream);

    sQry = "UPDATE TB_PRK_PLCE_FLR          \r\n"
           "   SET FLR_IMAGE_TYPE = :p03    \r\n"
           "      ,FLR_IMAGE_DATA = :p04    \r\n"
           " WHERE PRK_PLCE_NMBR  = :p01    \r\n"
           "   AND FLR_NO         = :p02    \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", APrkNmbr);
            ITSDb_SQLBind(pADO, "p02", AFlrNo);
            ITSDb_SQLBind(pADO, "p03", FImageExt);
    		pADO->Parameters->ParamByName("p04")->LoadFromStream(dataStream, ftBlob);
			pADO->ExecSQL();
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0050M::UpdatePrkFlrInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0050M::UpdatePrkFlrInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        if (dataStream) delete dataStream;
        dataStream = NULL;
    }
    FImageUpdate = false;
    return true;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::BtnSaveClick(TObject *Sender)
{
    TPrkPlce *pObj = (TPrkPlce*)CbPrk->Properties->Items->Objects[CbPrk->ItemIndex];
    if (pObj == NULL) {
        return;
    }
    TPrkPlceFlr *pFlr = (TPrkPlceFlr*)CbFlr->Properties->Items->Objects[CbFlr->ItemIndex];
    if (pFlr == NULL) {
        return;
    }

    UpdatePrkFlrInfo(pObj->CTLR_NMBR, pFlr->FLR_NO);

    String sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "UPDATE TB_PRK_PLCE_FLR_DEV      \r\n"
           "   SET X_POS = :p01             \r\n"
           "      ,Y_POS = :p02             \r\n"
           " WHERE PRK_PLCE_NMBR = :p03     \r\n"
           "   AND FLR_NO        = :p04     \r\n"
           "   AND DEVICE_ID     = :p05     \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            TcxGridDataController *pGDC = TvImageCell->DataController;
            int nRows = pGDC->RecordCount;
            for (int ii = 0; ii < nRows; ii++)
            {
                String sDevId = VarToStr(pGDC->Values[ii][ColDevId->Index]);
                String sPosX  = VarToStr(pGDC->Values[ii][ColCellPosX->Index]);
                String sPosY  = VarToStr(pGDC->Values[ii][ColCellPosY->Index]);

                TPrkPlceFlrDev *pDev = pFlr->FFlrDevs.Find(sDevId.ToIntDef(0));
                if (pDev != NULL) {
                    try {
                        ITSDb_SQLBind(pADO, "p01", sPosX);
                        ITSDb_SQLBind(pADO, "p02", sPosY);
                        ITSDb_SQLBind(pADO, "p03", pObj->CTLR_NMBR);
                        ITSDb_SQLBind(pADO, "p04", pFlr->FLR_NO);
                        ITSDb_SQLBind(pADO, "p05", sDevId);

                        ITSDb_SQLExec(pADO);

                        pDev->X_POS = sPosX.ToIntDef(0);
                        pDev->Y_POS = sPosY.ToIntDef(0);
                    }
                    catch(Exception &e)
                    {
                        ITSDb_GetConnection()->RollbackTrans();
                        DBERRORMSG("TPRK0050M::BtnSaveClick", String(e.ClassName()), e.Message, sQry);
                        throw Exception(String(e.ClassName()) + e.Message);
                    }
                }
            }

            ITSDb_GetConnection()->CommitTrans();
            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TPRK0050M::BtnSaveClick", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TPRK0050M::BtnSaveClick", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    if (bResult)
    {
        Application->MessageBox(L"주차장 층별 DEVICE 영역정보 저장\r\n주차장 층별 DEVICE 영역정보를 저장 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FUpdate = true;
        ChangeEditMode(3);
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::EdImageFileNameClick(TObject *Sender)
{
    OpenBmp->Title    = FTitle;
    OpenBmp->FileName = "";
    OpenBmp->Execute();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::OpenBmpCanClose(TObject *Sender, bool &CanClose)
{
    String sFName = OpenBmp->FileName;

    try
    {
        ClearImage();

        EdImageFileName->Text = sFName;

        TMemoryStream *pImgStream = new TMemoryStream();
        pImgStream->LoadFromFile(sFName);

        if (AssignPrkFlrImage(sFName.SubString(sFName.Length()-2, 3), pImgStream)) {
            FImageUpdate = true;
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::ClearImage() {
    try {
        ImgPreView->Picture->Bitmap->FreeImage();
        ImgPreView->Picture->Bitmap = NULL;
    } catch(Exception &e) {}
    try {
        ImgTemp->Picture->Bitmap->FreeImage();
        ImgTemp->Picture->Bitmap = NULL;
    } catch(Exception &e) {}
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0050M::AssignPrkFlrImage(String AExt, TMemoryStream *AImgStream)
{
    if (AImgStream == NULL || AImgStream->Size <= 32) {
        return false;
    }

    AExt = AExt.UpperCase();
    FImageExt = AExt;
    try {
        try {
            if (AExt == "PNG") {
                TdxPNGImage *img = new TdxPNGImage;
                img->LoadFromStream(AImgStream);

                ImgPreView->Picture->Bitmap->Assign(img);
                ImgPreView->Picture->Bitmap->PixelFormat = pf24bit;

                ImgTemp->Picture->Bitmap->Assign(img);
                ImgTemp->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
            }
            else
            if (AExt == "JPG" || AExt == "JPEG") {
                TJPEGImage *img = new TJPEGImage();
                img->LoadFromStream(AImgStream);

                ImgPreView->Picture->Bitmap->Assign(img);
                ImgPreView->Picture->Bitmap->PixelFormat = pf24bit;

                ImgTemp->Picture->Bitmap->Assign(img);
                ImgTemp->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
            }
            else
            if (AExt == "BMP") {
                Graphics::TBitmap *img = new Graphics::TBitmap;
                img->LoadFromStream(AImgStream);

                ImgPreView->Picture->Bitmap->Assign(img);
                ImgTemp->Picture->Bitmap->Assign(img);

                SAFE_DELETE(img);
            }
            else {
                return false;
            }
        } catch(Exception &e) {
            return false;
        }
    } __finally {
        SAFE_DELETE(AImgStream);
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TPRK0050M::LoadPrkFlrDevInfo()
{
    CMM_ClearGridTableView(TvImageCell);

    TPrkPlceFlr *pFlr = (TPrkPlceFlr*)CbFlr->Properties->Items->Objects[CbFlr->ItemIndex];
    if (pFlr == NULL) {
        return;
    }

    try
    {
        int nRow;
        TcxDataController *pGDC = TvImageCell->DataController;
        TvImageCell->BeginUpdate();
        FOR_STL(TPrkPlceFlrDev*, pDev, pFlr->FFlrDevs)
        {
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColDevId->Index]    = pDev->DEVICE_ID;
            pGDC->Values[nRow][ColCellPosX->Index] = pDev->X_POS;
            pGDC->Values[nRow][ColCellPosY->Index] = pDev->Y_POS;
        }
    }
    __finally
    {
        TvImageCell->EndUpdate();
        TvImageCell->DataController->GotoFirst();
        TvImageCell->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::LoadPrkFlrImage(String APrkNmbr, String AFlrNo)
{
    String sQry;
    TADOQuery *pADO = NULL;

    ClearImage();

    sQry = "SELECT FLR_NM,                  \r\n"
           "       FLR_IMAGE_TYPE,          \r\n"
           "       FLR_IMAGE_DATA           \r\n"
           "  FROM TB_PRK_PLCE_FLR          \r\n"
           " WHERE PRK_PLCE_NMBR = :p01     \r\n"
           "   AND FLR_NO        = :p02     \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", APrkNmbr);
            ITSDb_SQLBind(pADO, "p02", AFlrNo);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String FLR_NM = pADO->FieldByName("FLR_NM")->AsString;
                String FLR_IMAGE_TYPE = pADO->FieldByName("FLR_IMAGE_TYPE")->AsString;
                //Variant varImage = pADO->FieldByName("FLR_IMAGE_DATA")->AsVariant;
                if (FLR_IMAGE_TYPE.Trim() != "") {
                    TMemoryStream *pImgStream = new TMemoryStream();
                    pImgStream->LoadFromStream(pADO->CreateBlobStream(pADO->FieldByName("FLR_IMAGE_DATA"), bmRead));

				    AssignPrkFlrImage(FLR_IMAGE_TYPE, pImgStream);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0050M::LoadPrkFlrImage", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0050M::LoadPrkFlrImage", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::BtnCellApplyClick(TObject *Sender)
{
    TcxGridDataController *pGDC = TvImageCell->DataController;

    String sDevId;
    String sPosX;
    String sPosY;

    int nDevId = SeDevId->Value;
    int nPosX  = SePosX->Value;
    int nPosY  = SePosY->Value;

    if (nPosX == 0 || nPosY == 0) {
        return;
    }

    sDevId = String(nDevId);
    sPosX  = String(nPosX);
    sPosY  = String(nPosY);

    try
    {
        int nRows = pGDC->RecordCount;
        TvImageCell->BeginUpdate();
        pGDC->BeginUpdate();
        for (int ii = 0; ii < nRows; ii++)
        {
            String sTmpId = pGDC->Values[ii][ColDevId->Index];
            if (sTmpId == sDevId)
            {
                pGDC->Values[ii][ColCellPosX->Index] = sPosX;
                pGDC->Values[ii][ColCellPosY->Index] = sPosY;
                break;
            }
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvImageCell->EndUpdate();
    }
    UpdateCellColor();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::ImgPreViewMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
    try {
        SePosX->Value = X;
        SePosY->Value = Y;

        if (ChkAuto->Checked) {
            BtnCellApplyClick(NULL);
            int nIndex = TvImageCell->DataController->FocusedRecordIndex;
            if (nIndex < TvImageCell->DataController->RecordCount-1) {
                //TvImageCell->DataController->GotoNext();
                TvImageCell->DataController->FocusedRecordIndex = TvImageCell->DataController->FocusedRecordIndex+1;
            }
        }
        else {
            TColor FOldColor = ImgPreView->Picture->Bitmap->Canvas->Pixels[SePosX->Value][SePosY->Value];
            ImgPreView->Picture->Bitmap->Canvas->Brush->Style = bsSolid;
            ImgPreView->Picture->Bitmap->Canvas->Brush->Color = clRed;//clWhite;
            ImgPreView->Picture->Bitmap->Canvas->FloodFill(SePosX->Value, SePosY->Value, FOldColor, fsSurface);
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::ColAreaCustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    int nIdx = AViewInfo->GridRecord->RecordIndex;
    if (nIdx < 0)
        return;

    try
    {
        String sDevId = VarToStr(TvImageCell->DataController->Values[nIdx][ColDevId->Index]);
        nIdx = sDevId.ToIntDef(0);
        ACanvas->Brush->Color = GetDisplayColor(nIdx-1);
        ACanvas->Font->Color = ACanvas->Brush->Color;
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::TvImageCellFocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    TcxGridDataController *pGDC = TvImageCell->DataController;
    int nSelIdx = pGDC->FocusedRecordIndex;
    if (nSelIdx < 0) return;

    String sDevId = VarToStr(pGDC->Values[nSelIdx][ColDevId->Index]);
    String sPosX  = VarToStr(pGDC->Values[nSelIdx][ColCellPosX->Index]);
    String sPosY  = VarToStr(pGDC->Values[nSelIdx][ColCellPosY->Index]);

    SeDevId->Value = sDevId.ToIntDef(0);
    SePosX->Value  = sPosX.ToIntDef(0);
    SePosY->Value  = sPosY.ToIntDef(0);

    UpdateCellColor();

#if 1
    try {
        if (SePosX->Value != 0 && SePosY->Value != 0) {
            TColor FOldColor = ImgPreView->Picture->Bitmap->Canvas->Pixels[SePosX->Value][SePosY->Value];
            if (FOldColor == clYellow) return;

            ImgPreView->Picture->Bitmap->Canvas->Brush->Color = clYellow;
            ImgPreView->Picture->Bitmap->Canvas->FloodFill(SePosX->Value, SePosY->Value, FOldColor, fsSurface);
        }
    } catch(...) {}
#endif
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::UpdateCellColor()
{
    ImgPreView->Picture->Bitmap->FreeImage();
    ImgPreView->Picture->Bitmap = NULL;
    ImgPreView->Picture->Bitmap->Assign(ImgTemp->Picture->Bitmap);
    ImgPreView->Width  = ImgPreView->Picture->Bitmap->Width;
    ImgPreView->Height = ImgPreView->Picture->Bitmap->Height;

    TcxGridDataController *pGDC = TvImageCell->DataController;
    int nRows = pGDC->RecordCount;
    try
    {
        TvImageCell->BeginUpdate();
        pGDC->BeginUpdate();
        for (int ii = 0; ii < nRows; ii++)
        {
            String sDevId = VarToStr(pGDC->Values[ii][ColDevId->Index]);
            String sPosX  = VarToStr(pGDC->Values[ii][ColCellPosX->Index]);
            String sPosY  = VarToStr(pGDC->Values[ii][ColCellPosY->Index]);

            int X = sPosX.ToIntDef(0);
            int Y = sPosY.ToIntDef(0);
            if (X == 0 || Y == 0) {
                continue;
            }

            TColor FOldColor = ImgPreView->Picture->Bitmap->Canvas->Pixels[X][Y];

            int nIdx = sDevId.ToIntDef(0);
            ImgPreView->Picture->Bitmap->Canvas->Brush->Style = bsSolid;
            ImgPreView->Picture->Bitmap->Canvas->Brush->Color = GetDisplayColor(nIdx-1);
            ImgPreView->Picture->Bitmap->Canvas->FloodFill(X, Y, FOldColor, fsSurface);
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvImageCell->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::BtnEditClick(TObject *Sender)
{
    if (BtnEdit->Caption == FrmLang->lblEdit->Caption)//"편집")
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        ChangeEditMode(1);
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::BtnSaveImgFileClick(TObject *Sender)
{
    SaveDialog->Title      = FTitle;
    SaveDialog->InitialDir = ExtractFilePath(Application->ExeName);
    SaveDialog->Filter     = "Bitmap image Files (*.bmp)|*.bmp";

    if (SaveDialog->Execute())
    {
        try {
            String sFileName = SaveDialog->FileName;

            int iRet = sFileName.Pos(".bmp");
            if (iRet == 0)
            {
                sFileName = SaveDialog->FileName + ".bmp";
            }

            //ImgPreView->Picture->SaveToFile(sFileName);
            ImgTemp->Picture->SaveToFile(sFileName);
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0050M::ChangeEditMode(int AEdit)
{
    //LblWarn->Visible = false;

    FImageUpdate = false;
    bool bEnabled = false;
    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
            //EdImageFileName->Enabled = false;
            EdImageFileName->Enabled = true;
            //LblWarn->Visible = true;
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormInit(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            //bEnabled = true;
            EditMode = false;
            EdImageFileName->Enabled = false;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
            EdImageFileName->Enabled = true;
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
            EditMode = false;
            //EdImageFileName->Enabled = false;
            EdImageFileName->Enabled = true;
        }
        break;
    }

    BtnSaveImgFile->Enabled = false;
    if (ImgPreView->Picture->Bitmap != NULL)
    {
        if (ImgPreView->Picture->Bitmap->Height != 0)
        {
            //BtnSaveImgFile->Enabled = true;
        }
    }
    if (AEdit == 0 || AEdit == 2)
    {
        PnlCmd->Enabled = true;
        PnlHead->Enabled = false;
    }
    else
    {
        PnlCmd->Enabled = false;
        PnlHead->Enabled = true;
    }
}
//---------------------------------------------------------------------------

