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

#include <vcl.h>
#include "AppGlobalF.h"
#pragma hdrstop

#include "CDSMonitoringObjF.h"
#include "FrmCameraScreenManagerF.h"
#include "DMDbF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_ObjListF"
#pragma link "cxButtons"
#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 "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TFrmCameraScreenManager *FrmCameraScreenManager = NULL;
//---------------------------------------------------------------------------

__fastcall TFrmCameraScreenManager::TFrmCameraScreenManager(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);

    if (g_AppCfg.sLang != "kr")
    {
        Caption = "Monitoring Screen Management";
        GrpList->Caption = "▶ Monitoring screen list";
        cxGroupBox1->Caption = "▶ Monitoring screen setting";
        Col02->Caption = "Screen Name";
        Col03->Caption = "Count";
        ColCtlr3->Caption = "Name";
        BtnListAdd->Caption = "Regist";
        BtnListDelete->Caption = "Remove";
        BtnUp->Caption = "▲ Up";
        BtnDown->Caption = "▼ Down";

        BtnEdit->Caption = "Edit";
        BtnEdit->Hint = "Edit data";
        BtnInsert->Caption = "Add";
        BtnInsert->Hint = "Add new data";
        BtnDelete->Caption = "Delete";
        BtnDelete->Hint = "Delete selected data";
        BtnSave->Caption = "Save";
        BtnSave->Hint = "Save editing data";
    }
    else
    {
        Caption = "모니터링 화면 관리";
        GrpList->Caption = "▶ 모니터링 화면 목록";
        cxGroupBox1->Caption = "▶ 모니터링 화면 설정";
        Col02->Caption = "화면명칭";
        Col03->Caption = "갯수";
        ColCtlr3->Caption = "명칭";
    }

    FUpdate= false;
    FTitle = Caption; //"모니터링 화면 관리";

    EditMode = false;

    FRAMEObjList1->PnlTop->Visible = false;
    FRAMEObjList1->PnlBottom->Visible = false;
    FRAMEObjList1->ColumnSel->Visible = false;
    FRAMEObjList1->Column04->Visible = true;

    FRAMEObjList1->TvList->OptionsView->ColumnAutoWidth = true;
    FRAMEObjList1->TvList->OptionsSelection->MultiSelect = true;
    FRAMEObjList1->TvList->OptionsSelection->CellSelect = false;
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmCameraScreenManager::FormInit()
{
    //TvList->OptionsView->NoDataToDisplayInfoText = "<등록되어 있는 정보가 없습니다>";
    //TvCtlr->OptionsView->NoDataToDisplayInfoText = "<등록되어 있는 정보가 없습니다>";

    FRAMEObjList1->UpdateList();

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

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

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TFrmCameraScreenManager::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCameraScreenManager::BtnEditClick(TObject *Sender)
{
    String sCaption = "편집";
    if (g_AppCfg.sLang != "kr") sCaption = "Edit";

    if (BtnEdit->Caption == sCaption)
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        String sName = EdName->Text;
        ChangeEditMode(1);

        if (CMM_FindGridData(TvList->DataController, sName, Col02->Index) >= 0)
        {
            CMM_SetGridRow(CxList, TvList, sName, Col02->Index);
            DisplayInfo();
        }
    }
}
//---------------------------------------------------------------------------

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

            if (g_AppCfg.sLang != "kr")
            {
                BtnEdit->Caption = "Cancel";
                BtnEdit->Hint    = "Cancel ediging data";
            }
            else
            {
                BtnEdit->Caption = "취소";
                BtnEdit->Hint    = "데이터 편집 취소";
            }
            EditMode = true;
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormClear(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            if (g_AppCfg.sLang != "kr")
            {
                BtnEdit->Caption = "Edit";
                BtnEdit->Hint    = "Editing data";
            }
            else
            {
                BtnEdit->Caption = "편집";
                BtnEdit->Hint    = "데이터 편집";
            }
            //bEnabled = true;
            EditMode = false;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            if (g_AppCfg.sLang != "kr")
            {
                BtnEdit->Caption = "Cancel";
                BtnEdit->Hint    = "Cancel ediging data";
            }
            else
            {
                BtnEdit->Caption = "취소";
                BtnEdit->Hint    = "데이터 추가 취소";
            }
            EditMode = true;
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            if (g_AppCfg.sLang != "kr")
            {
                BtnEdit->Caption = "Edit";
                BtnEdit->Hint    = "Editing data";
            }
            else
            {
                BtnEdit->Caption = "편집";
                BtnEdit->Hint    = "데이터 수정";
            }
            bEnabled = true;
            EditMode = false;
        }
        break;
    }

    if (AEdit == 0 || AEdit == 2)
    {
        PnlCmd->Enabled = true;
    }
    else
    {
        PnlCmd->Enabled = false;
    }
}
//---------------------------------------------------------------------------

int __fastcall TFrmCameraScreenManager::CheckDupFormName(String ANewName, String AOldName)
{
    int nDupCnt = 0;

    try
    {
        TvList->BeginUpdate();

        TcxGridDataController *pGDC = TvList->DataController;
        int nRows = pGDC->RecordCount;
        for (int ii = 0; ii < nRows; ii++)
        {
            String sFormName = VarToStr(pGDC->Values[ii][Col02->Index]);
            if (sFormName == AOldName)
            {
                continue;
            }
            if (sFormName == ANewName)
            {
                nDupCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nDupCnt;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnSaveClick(TObject *Sender)
{
	if (EdName->Text.Trim().IsEmpty())
	{
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Save monitoring screen\r\nEnter the monitoring screen name.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 저장\r\n모니터링 화면명칭을 입력하세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}
    if (!FNewDb)
    {
        if (EdOrgName->Text.Trim().IsEmpty())
        {
            if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Save monitoring screen\r\nSelect the monitoring screen from the list.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            else                        Application->MessageBox(L"모니터링 화면 저장\r\n모니터링 화면을 목록에서 선택하세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }

    if (TvCtlr->DataController->RecordCount == 0)
    {
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Save monitoring screen\r\nPlease add monitored object.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 저장\r\n모니터링 대상을 추가해 주세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    String sOldName = EdOrgName->Text;
    String sNewName = EdName->Text.Trim();

    int nDupCnt;
    bool bResult = false;
    if (FNewDb)
    {
        nDupCnt = CheckDupFormName(sNewName, "");
    }
    else
    {
        nDupCnt = CheckDupFormName(sNewName, sOldName);
    }
    if (nDupCnt >= 1)
    {
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Save monitoring screen\r\nThis is the name of the monitoring screen that is already in use.\r\nPlease enter an unused name.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 저장\r\n이미 사용중인 모니터링 화면명칭 입니다.\r\n사용하지 않는 명칭을 입력하세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    bResult = DelMonitoring(sOldName);
    if (!bResult)
    {
        return;
    }

    try
    {
        DMDb->GetConnection()->BeginTrans();

        TcxGridDataController *pGDC = TvCtlr->DataController;
        int nRows = pGDC->RecordCount;
        for (int ii = 0; ii < nRows; ii++)
        {
            String sCtlrNmbr = VarToStr(pGDC->Values[ii][ColCtlr2->Index]);
            ObjCtlrManager->InsMonitoringFormFromDb(sNewName, ii+1, sCtlrNmbr, 0, DMDb->GetConnection());
        }
        DMDb->GetConnection()->CommitTrans();
        bResult = true;
    }
    catch(EDatabaseError &E)
    {
        DMDb->GetConnection()->RollbackTrans();
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DMDb->GetConnection()->RollbackTrans();
        throw Exception(String(e.ClassName()) + e.Message);
    }

    if (bResult)
    {
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Save monitoring screen\r\nMonitoring screen information has been saved.", FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 저장\r\n모니터링 화면 정보를 저장 하였습니다.", FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FUpdate = true;

        ObjCtlrManager->LoadMonitoringFormFromDb(DMDb->GetConnection());
        LoadMonitoring();
        CMM_SetGridRow(CxList, TvList, sNewName, Col02->Index);
        DisplayInfo();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnInsertClick(TObject *Sender)
{
    FormClear();

    EdName->Text    = "";
    EdOrgName->Text = "";

    ChangeEditMode(2);
    FNewDb = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnDeleteClick(TObject *Sender)
{
    int nIdx = TvList->DataController->FocusedRecordIndex;
    if (nIdx < 0) return;

    String sFormName = EdOrgName->Text;

    if (sFormName.IsEmpty())
    {
        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Delete monitoring screen\r\nSelect the monitoring screen to delete from the list.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 삭제\r\n삭제할 모니터링 화면을 목록에서 선택하세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    String sMsg;
    if (g_AppCfg.sLang != "kr")
    {
        sMsg = "Monitoring screen: " + sFormName;
        sMsg+= "\r\nAre you sure you want to delete the selected monitoring screen information?";
    }
    else
    {
        sMsg = "모니터링 화면: " + sFormName;
        sMsg+= "\r\n선택하신 모니터링 화면 정보를 삭제하시겠습니까?";
    }

    if (Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    if (DelMonitoring(sFormName))
    {
        FUpdate = true;

        if (g_AppCfg.sLang != "kr") Application->MessageBox(L"Delete monitoring screen\\r\Monitoring screen information has been deleted.", FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        else                        Application->MessageBox(L"모니터링 화면 삭제\r\n모니터링 화면 정보를 삭제 하였습니다.", FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FormClear();
        ChangeEditMode(1);

        ObjCtlrManager->LoadMonitoringFormFromDb(DMDb->GetConnection());
        LoadMonitoring();
	}
}
//---------------------------------------------------------------------------

bool __fastcall TFrmCameraScreenManager::DelMonitoring(String AFormName)
{
    return ObjCtlrManager->DelMonitoringFormFromDb(AFormName, DMDb->GetConnection());
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
	if (!ACellViewInfo) return;

    DisplayInfo();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCameraScreenManager::BtnSearchClick(TObject *Sender)
{
    RefreshData();

    EdFocus->SetFocus();
    ChangeEditMode(1);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    FormClear();
    LoadMonitoring();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::LoadMonitoring()
{
    CMM_ClearGridTableView(TvList);

    try
    {
        int nRow;
        TcxDataController *pGDC = TvList->DataController;
        TvList->BeginUpdate();

        FOR_STL(TMonitoringScreen*, pObj, ObjScreenManager->FLists)
        {
            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][Col01->Index] = "";
            pGDC->Values[nRow][Col02->Index] = pObj->NAME;
            pGDC->Values[nRow][Col03->Index] = String(pObj->FLists.Size());
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::FormClear()
{
    CMM_ClearGridTableView(TvCtlr);

    EdOrgName->Clear();
    EdName->Clear();

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

void __fastcall TFrmCameraScreenManager::DisplayInfo()
{
    FormClear();

	int nSelIdx = TvList->DataController->FocusedRecordIndex;
	if( nSelIdx <= -1 )
		return;

    DisplayFormInfo(nSelIdx);

    ChangeEditMode(3);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::DisplayFormInfo(int AIdx)
{
	TcxGridDataController *pDc = TvList->DataController;

    String sScrName = VarToStr(pDc->Values[AIdx][Col02->Index]);
    EdName->Text    = sScrName;
    EdOrgName->Text = sScrName;

    CMM_ClearGridTableView(TvCtlr);

    try
    {
        int nRow;
        TcxDataController *pGDC = TvCtlr->DataController;
        TvCtlr->BeginUpdate();

        TMonitoringScreen *pObj = ObjScreenManager->FLists.Find(sScrName);
        if (!pObj)
        {
            return;
        }

        FOR_STL(TScreenCtlr*, pScr, pObj->FLists)
        {
            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColCtlr1->Index] = String(pScr->SEQ);
            pGDC->Values[nRow][ColCtlr2->Index] = String(pScr->CTLR_NMBR);
            TMonitoringCtlr *pCtlr = ObjCtlrManager->FLists.Find(pScr->CTLR_NMBR);
            if (pCtlr) pGDC->Values[nRow][ColCtlr3->Index] = pCtlr->NAME;
            else       pGDC->Values[nRow][ColCtlr3->Index] = "--???--";
        }
    }
    __finally
    {
        TvCtlr->EndUpdate();
    }
}
//---------------------------------------------------------------------------

bool __fastcall TFrmCameraScreenManager::FindCtlrInfo(String ACtlrNmbr)
{
    try {
        TvCtlr->BeginUpdate();
        for (int ii = 0; ii < TvCtlr->DataController->RecordCount; ii++)
        {
            String sCtlrNmbr = VarToStr(TvCtlr->DataController->Values[ii][ColCtlr2->Index]);
            if (sCtlrNmbr == ACtlrNmbr) return true;
        }
    }
    __finally
    {
        TvCtlr->EndUpdate();
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::UpdateCtlrSeq()
{
    try {
        TvCtlr->BeginUpdate();
        for (int ii = 0; ii < TvCtlr->DataController->RecordCount; ii++)
        {
            TvCtlr->DataController->Values[ii][ColCtlr1->Index] = String(ii+1);
        }
    }
    __finally
    {
        TvCtlr->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnListAddClick(TObject *Sender)
{
    TcxGridTableView *pSrcList = FRAMEObjList1->TvList;
    TcxGridTableView *pDstList = TvCtlr;
    int nSelRows = pSrcList->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        return;
    }

    try
    {
        TcxGridDataController *pGDC = pDstList->DataController;
        try
        {
            for (int ii = 0; ii < nSelRows; ii++)
            {
                int nSelIdx = pSrcList->Controller->SelectedRows[ii]->RecordIndex;

                String sCtlrNmbr = VarToStr(pSrcList->DataController->Values[nSelIdx][FRAMEObjList1->Column01->Index]);
                String sCtlrName = VarToStr(pSrcList->DataController->Values[nSelIdx][FRAMEObjList1->Column03->Index]);

                if (FindCtlrInfo(sCtlrNmbr)) continue;

                int nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColCtlr2->Index] = sCtlrNmbr;
                pGDC->Values[nRow][ColCtlr3->Index] = sCtlrName;
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        UpdateCtlrSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnListDeleteClick(TObject *Sender)
{
    TcxGridTableView *pSrcList = TvCtlr;
    int nFocused = pSrcList->Controller->FocusedRecordIndex;

    int nSelRows = pSrcList->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        return;
    }

    try
    {
        TcxGridDataController *pGDC = pSrcList->DataController;
        pSrcList->BeginUpdate();
        try
        {
            pSrcList->Controller->DeleteSelection();

            int nDataCnt = pSrcList->DataController->RecordCount;
            if (nDataCnt <= nFocused)
                nFocused = nDataCnt - 1;

            pSrcList->Controller->ClearSelection();
            if (nFocused >= 0)
            {
                pSrcList->Controller->FocusedRowIndex = nFocused;
                pSrcList->ViewData->Records[nFocused]->Focused = true;
                pSrcList->ViewData->Records[nFocused]->Selected = true;
            }
            //CxLink->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        pSrcList->EndUpdate();
        UpdateCtlrSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnUpClick(TObject *Sender)
{
    try
    {
        TvCtlr->BeginUpdate();

        int nCurIdx = TvCtlr->DataController->FocusedRowIndex;//FocusedRecordIndex;
        if (nCurIdx <= 0)
        {
            return;
        }

        int nUpIdx = nCurIdx-1;

        try
        {
            String sVal1 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr1->Index]);
            String sVal2 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr2->Index]);
            String sVal3 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr3->Index]);

            TvCtlr->DataController->Values[nCurIdx][ColCtlr1->Index] = TvCtlr->DataController->Values[nUpIdx][ColCtlr1->Index];
            TvCtlr->DataController->Values[nCurIdx][ColCtlr2->Index] = TvCtlr->DataController->Values[nUpIdx][ColCtlr2->Index];
            TvCtlr->DataController->Values[nCurIdx][ColCtlr3->Index] = TvCtlr->DataController->Values[nUpIdx][ColCtlr3->Index];

            TvCtlr->DataController->Values[nUpIdx][ColCtlr1->Index] = sVal1;
            TvCtlr->DataController->Values[nUpIdx][ColCtlr2->Index] = sVal2;
            TvCtlr->DataController->Values[nUpIdx][ColCtlr3->Index] = sVal3;

            TvCtlr->Controller->ClearSelection();
            TvCtlr->Controller->FocusedRowIndex = nUpIdx;
            TvCtlr->ViewData->Records[nUpIdx]->Focused = true;
            TvCtlr->ViewData->Records[nUpIdx]->Selected = true;
            CxCtlr->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvCtlr->EndUpdate();
        UpdateCtlrSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreenManager::BtnDownClick(TObject *Sender)
{
    try
    {
        TvCtlr->BeginUpdate();

        int nCurIdx = TvCtlr->DataController->FocusedRowIndex;//FocusedRecordIndex;
        if (nCurIdx < 0)
        {
            return;
        }
        if (nCurIdx == TvCtlr->DataController->RecordCount-1)
        {
            return;
        }

        int nDnIdx = nCurIdx+1;

        try
        {
            String sVal1 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr1->Index]);
            String sVal2 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr2->Index]);
            String sVal3 = VarToStr(TvCtlr->DataController->Values[nCurIdx][ColCtlr3->Index]);

            TvCtlr->DataController->Values[nCurIdx][ColCtlr1->Index] = TvCtlr->DataController->Values[nDnIdx][ColCtlr1->Index];
            TvCtlr->DataController->Values[nCurIdx][ColCtlr2->Index] = TvCtlr->DataController->Values[nDnIdx][ColCtlr2->Index];
            TvCtlr->DataController->Values[nCurIdx][ColCtlr3->Index] = TvCtlr->DataController->Values[nDnIdx][ColCtlr3->Index];

            TvCtlr->DataController->Values[nDnIdx][ColCtlr1->Index] = sVal1;
            TvCtlr->DataController->Values[nDnIdx][ColCtlr2->Index] = sVal2;
            TvCtlr->DataController->Values[nDnIdx][ColCtlr3->Index] = sVal3;

            TvCtlr->Controller->ClearSelection();
            TvCtlr->Controller->FocusedRowIndex = nDnIdx;
            TvCtlr->ViewData->Records[nDnIdx]->Focused = true;
            TvCtlr->ViewData->Records[nDnIdx]->Selected = true;
            CxCtlr->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvCtlr->EndUpdate();
        UpdateCtlrSeq();
    }
}
//---------------------------------------------------------------------------

