//---------------------------------------------------------------------------
/*
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>
#include <tchar.h>
#pragma hdrstop

#include "Train_Control.h"
#include "Debug_Controls.h"
#include "LashUpListPopOut.h"
#include "Clipbrd.hpp"
#include "LashUp_Selection.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "HideButton"
#pragma resource "*.dfm"

TPopOutLashUpList *PopOutLashUpList;
//---------------------------------------------------------------------------
__fastcall TPopOutLashUpList::TPopOutLashUpList(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("LashUpList", "Top", Top);
    Height = RegFile->ReadInteger("LashUpList", "Height", Height);
    Left = RegFile->ReadInteger("LashUpList", "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;
    }
//
LashUpData->DefaultColWidth = 30;		// default
LashUpData->ColWidths[LASHUP_NAME] = 175;	// lots of space for the LashUp name
LashUpData->ColWidths[LASHUP_LIST] = LashUpData->Width - LashUpData->ColWidths[LASHUP_NAME] - LashUpData->DefaultColWidth;	// lots of space for the Consist

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

void __fastcall TPopOutLashUpList::HideButton1Click(TObject *Sender)
{
Visible = false;    
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::LashUpDataClick(TObject *Sender)
{
// Reflect new row to LashUp->LashUpData
if (LashUp->LashUpData->Row != LashUpData->Row) LashUp->LashUpData->Row = LashUpData->Row;
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::LashUpDataDrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
// Draw the contents of each cell
LashUpData->Canvas->Font->Size = 12;
LashUpData->Canvas->Font->Style = LashUpData->Canvas->Font->Style >> fsItalic;
// if the cell is selected, highlight it
if( State.Contains(gdSelected) ) {
    LashUpData->Canvas->Font->Color = clWhite;
	LashUpData->Canvas->Brush->Color = clBlue;
	} else {
    if ((ARow > 0) && MainForm->IsLashUpRunning(ARow + 100) != NULL) {
		LashUpData->Canvas->Font->Color = clBlack;	// Started up LashUps are Lime
		LashUpData->Canvas->Brush->Color = clLime;
    	} else {
		LashUpData->Canvas->Font->Color = clBlack;
		LashUpData->Canvas->Brush->Color = clWhite;
        }
	}
// move a little to the right
Rect.Left = Rect.Left + LISTLEFTOFFSET;
// fill the rectangle
LashUpData->Canvas->FillRect(Rect);
// draw the text
String text;
if ((ACol == LASHUP_NAME) && (ARow != LASHUP_LABEL_ROW) && (LashUpData->Cells[LASHUP_NAME][ARow] != "*") /* &&
        (MainForm->EngineData->Cells[ENG_STATE][HeadEngine] == ACTIVE)*/) {
    int iHeadEngine;   // get head engine 1 to 99
    String LashupL =  LashUpData->Cells[LASHUP_LIST][ARow];
    _stscanf(LashupL.c_str(), TEXT("%d"), &iHeadEngine);
    iHeadEngine = abs(iHeadEngine);
    text = LashUpData->Cells[ACol][ARow] + "  TIU" + String(MainForm->EngineData->Cells[TIU_NUMBER][iHeadEngine]);
    } else {
    text = LashUpData->Cells[ACol][ARow];
    }
LashUpData->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text);
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::StartupSelectedLashUpClick(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }
//
for (int LashUpNo = LashUpData->Selection.Top; LashUpNo <= LashUpData->Selection.Bottom ; LashUpNo++) {
    if (LashUpData->Cells[LASHUP_NAME][LashUpNo] != "*") {
        LashUp->LashUpData->Row = LashUpNo;
        LashUp->LashUpStartUpButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TPopOutLashUpList::ShutdownSelectedLashUpsClick(
      TObject *Sender)
{
// if TIU Port is not connected fail
if (!MainForm->IsTIUConnected()) {
	MainForm->DebugMessagePopup("Warning", MainForm->ConnectMsg, "");
	return;
    }

for (int LashUpNo = LashUpData->Selection.Top; LashUpNo <= LashUpData->Selection.Bottom ; LashUpNo++) {
    if (LashUpData->Cells[LASHUP_NAME][LashUpNo] != "*") {
        LashUp->LashUpData->Row = LashUpNo;
        LashUp->LashUpShutdownButtonClick(Sender);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::LashUpDataDblClick(TObject *Sender)
{
if (LashUpData->Cells[LASHUP_NAME][LashUpData->Row] != "*") {
    int LashUpNo = LashUpData->Row;
    LashUp->LashUpData->Row = LashUpNo;
    StartupSelectedLashUpClick(Sender);
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::LashUpDataKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
// if ENTER is pressed, Startup the Lashup if it's present
if (Key == VK_RETURN) {
	if (LashUpData->Cells[LASHUP_NAME][LashUpData->Row] != "*") {
		StartupSelectedLashUpClick(Sender);
   		}
    }
}
//---------------------------------------------------------------------------

void __fastcall TPopOutLashUpList::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 TPopOutLashUpList::RefreshClick(TObject *Sender)
{
// Reload LashUpData from LashUp->LashUpData
// cannot assign a TStringGrid to a TStringGrid : PopOutEngineList->EngineData->Assign(EngineData);
for (int I = 0; I < LashUpData->ColCount; I++)
    LashUpData->Cols[I]->Assign(LashUp->LashUpData->Cols[I]);
LashUpData->Row = LashUp->LashUpData->Row;
LashUpData->Col = LashUp->LashUpData->Col;
LashUp_List_Label->Caption = LashUp->LashUp_List_Label->Caption;
}
//---------------------------------------------------------------------------

