//---------------------------------------------------------------------------
/*
Remote Train Control Program for Windows

© Copyright 2024 by Mark DiVecchio

    This file is part of Remote Train Control.

    Remote Train Control is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Remote Train Control is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

*/
// Home Page : http://www.silogic.com/trains/RTC_Running.html
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

// RTC Near Field Communications
#include "RTCMegaMaster.h"
#include "RTC_NFC.h"
#include "Engine_Commands.h"
#include "Train_Control.h"
#include "Comm_Thread.h"
#include "Debug_Controls.h"
#include "Route_Scene.h"
#include "Clipbrd.hpp"
#include "Route_Scene_Thread.h"
#include "RouteScenePopoutList.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "HideButton"
#pragma resource "*.dfm"
TPopoutRouteSceneList *PopoutRouteSceneList;
//---------------------------------------------------------------------------
__fastcall TPopoutRouteSceneList::TPopoutRouteSceneList(TComponent* Owner)
    : TForm(Owner)
{
// Required for multiple monitors and save/restore position/size
Position=poDesigned;
DefaultMonitor=dmDesktop;
//
// read Preferences from registry if they exist
RegFileRootKey =  MainForm->RegFileRootKey;
//
TRegIniFile * const RegFile = new TRegIniFile(RegFileRootKey);
assert(RegFile != NULL);
__try {
    // read window position
    Top = RegFile->ReadInteger("RSList", "Top", Top);
    Height = RegFile->ReadInteger("RSList", "Height", Height);
    Left = RegFile->ReadInteger("RSList", "Left", Left);
    // if using multiple monitors and one was removed, pull the window
    // back onto the remaining monitor
    int MC = Screen->MonitorCount;
    if (MC == 1) {
        if (Left < 0 ) Left = 0;
        int Wth = Screen->Monitors[0]->Width;
		if (Left > Wth) Left -= Wth;
        if (Top < 0 ) Top = 0;
        int Hth = Screen->Monitors[0]->Height;
        if (Top > Hth) Top -= Hth;
        }
	}
__finally {
	// Deallocate RegFile
	delete RegFile;
	}
//
RSData->DefaultColWidth = 30;	// default
RSData->ColWidths[RS_NAME] = RSData->Width - RSData->DefaultColWidth;	// lots of space for the RouteScene name
RSData->ColWidths[RS_LIST] = 100;	// lots of space for the 2nd Parameter but it's not visible
}
//---------------------------------------------------------------------------

__fastcall TPopoutRouteSceneList::~TPopoutRouteSceneList(void)
{
if (MainForm->bSaveRegistryInfo) {
    TRegIniFile * const RegFile = new TRegIniFile(RegFileRootKey);
    assert(RegFile != NULL);
    __try {	    // erase RSList key
        RegFile->EraseSection("RSList");
        // save window position
        RegFile->WriteInteger("RSList", "Top", Top > 0 ? Top : 0);
        RegFile->WriteInteger("RSList", "Height", Height);
        RegFile->WriteInteger("RSList", "Left", Left);

        }
    __finally {
        // Deallocate RegFile
        delete RegFile;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::HideButton1Click(TObject *Sender)
{
Visible = false;     
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::RSDataDblClick(TObject *Sender)
{
if (RSData->Cells[RS_NAME][RSData->Row] != "*") {
    ActivateSelectedRouteSceneClick(Sender);
    }
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::RSDataDrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
// Draw the contents of each cell
RSData->Canvas->Font->Size = 12;
RSData->Canvas->Font->Style = RSData->Canvas->Font->Style >> fsItalic;
// if the cell is selected, hightlight it
if( State.Contains(gdSelected) ) {
    RSData->Canvas->Font->Color = clWhite;
	RSData->Canvas->Brush->Color = clBlue;	// Blue on White
	} else {
	RSData->Canvas->Font->Color = clBlack;
	RSData->Canvas->Brush->Color = clWhite;	// White on Black
	}
// move a little to the right
Rect.Left = Rect.Left + LISTLEFTOFFSET;
// fill the rectangle
RSData->Canvas->FillRect(Rect);
// draw the text
String text = RSData->Cells[ACol][ARow];
RSData->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text);    
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::RSDataClick(TObject *Sender)
{
// this function is called before RouteScene is created because it is auto create
// and RouteScene is manually created when needed
if (RouteScene && (RouteScene->RSData->Row != RSData->Row)) RouteScene->RSData->Row = RSData->Row;
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::RSDataKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
// if ENTER is pressed, Activate the Route/Scene if it's present
if (Key == VK_RETURN) {
	if (RSData->Cells[RS_NAME][RSData->Row] != "*") {
        ActivateSelectedRouteSceneClick(Sender);
   		}
    }
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::ActivateSelectedRouteSceneClick(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int RSNo = RSData->Selection.Top; RSNo <= RSData->Selection.Bottom ; RSNo++) {
    if (RSData->Cells[RS_NAME][RSNo] != "*") {
        RouteScene->RSData->Row = RSNo;
        RouteScene->RSActivateButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TPopoutRouteSceneList::ResetRouteScenetoDefaultPositionClick(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int RSNo = RSData->Selection.Top; RSNo <= RSData->Selection.Bottom ; RSNo++) {
    if (RSData->Cells[RS_NAME][RSNo] != "*") {
        RouteScene->RSData->Row = RSNo;
        RouteScene->RSResetButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopoutRouteSceneList::AllSwitchesinSelectedRouteScenetoNORMALposition1Click(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int RSNo = RSData->Selection.Top; RSNo <= RSData->Selection.Bottom ; RSNo++) {
    if (RSData->Cells[RS_NAME][RSNo] != "*") {
        RouteScene->RSData->Row = RSNo;
        RouteScene->AllSwNormalButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopoutRouteSceneList::AllAccyinSelectedRouteScenetoON1Click(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int RSNo = RSData->Selection.Top; RSNo <= RSData->Selection.Bottom ; RSNo++) {
    if (RSData->Cells[RS_NAME][RSNo] != "*") {
        RouteScene->RSData->Row = RSNo;
        RouteScene->AllAccyOnButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopoutRouteSceneList::AllAccyinSelectedRouteScenetoOFF1Click(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int RSNo = RSData->Selection.Top; RSNo <= RSData->Selection.Bottom ; RSNo++) {
    if (RSData->Cells[RS_NAME][RSNo] != "*") {
        RouteScene->RSData->Row = RSNo;
        RouteScene->AllAccyOffButtonClick(Sender);
        }
    }
}

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

void __fastcall TPopoutRouteSceneList::CopyrightClick(TObject *Sender)
{
// Copy the Form to the clipboard.
// In BCB4.0, there is no SetSize method associated with TBitmap
// so we have to use Width and Height separately.
//
// see:
// https://stackoverflow.com/questions/3190777/how-do-i-copy-a-form-as-an-image-to-the-clipboard
//
Graphics::TBitmap * bm;
bm = new Graphics::TBitmap;
try
	{
    #if __BORLANDC__ >= 0x720
	// Defined for CB10.1 C++ programs that use the VCL.
	bm->SetSize(ClientWidth, ClientHeight);
    #else
    bm->Width = ClientWidth;
    bm->Height = ClientHeight;
    #endif
	BitBlt(bm->Canvas->Handle, 0, 0, ClientWidth, ClientHeight, Canvas->Handle, 0, 0, SRCCOPY);
    Clipboard()->Assign(bm);
    }
__finally
	{
	delete bm;
    }    
}
//---------------------------------------------------------------------------

void __fastcall TPopoutRouteSceneList::RefreshClick(TObject *Sender)
{
// Reload the RSData from RouteScene->RSData
// cannot assign a TStringGrid to a TStringGrid : PopOutEngineList->EngineData->Assign(EngineData);
for (int I = 0; I < RSData->ColCount; I++)
    RSData->Cols[I]->Assign(RouteScene->RSData->Cols[I]);
RSData->Row = RouteScene->RSData->Row;
RSData->Col = RouteScene->RSData->Col;
Route_Scene_List_Label->Caption = RouteScene->Route_Scene_List_Label->Caption;
}
//---------------------------------------------------------------------------

