//---------------------------------------------------------------------------
/*
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

#include "TIUSetup.h"
#include "Super_TIU_Mode.h"
#include "Message_Popup.h"
#include "Train_Control.h"
#include "Setup_Control.h"
#include "PBFileThread.h"
#include "Clipbrd.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "_GClass"
#pragma link "AbSwitch"
#pragma resource "*.dfm"

extern bool TIUSelection[TIUMAX];                       // Flags to indicate that a TIU is present
TAbRockerSwitch * TIUActiveSwitchptr[TIUMAX];    // pointers to each Active TIU switch

TActiveTIU *ActiveTIU;
//---------------------------------------------------------------------------
__fastcall TActiveTIU::TActiveTIU(TComponent* Owner)
    : TForm(Owner)
{
TIU1->Checked = TIUSelection[0];
TIU2->Checked = TIUSelection[1];
TIU3->Checked = TIUSelection[2];
TIU4->Checked = TIUSelection[3];
TIU5->Checked = TIUSelection[4];

TIUActiveSwitchptr[0] = TIU1;        // Setup pointers
TIUActiveSwitchptr[1] = TIU2;
TIUActiveSwitchptr[2] = TIU3;
TIUActiveSwitchptr[3] = TIU4;
TIUActiveSwitchptr[4] = TIU5;

LocalTransparencyOverride = false;
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::HideButtonClick(TObject *Sender)
{
if (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked) {
    Hide();	      // Same as setting Visible to false
    } else {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    return;
    }
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::TIU1Click(TObject *Sender)
{
TIUSelection[0] = TIU1->Checked;
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::TIU2Click(TObject *Sender)
{
TIUSelection[1] = TIU2->Checked;
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::TIU3Click(TObject *Sender)
{
TIUSelection[2] = TIU3->Checked;
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::TIU4Click(TObject *Sender)
{
TIUSelection[3] = TIU4->Checked;
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
}
//---------------------------------------------------------------------------
void __fastcall TActiveTIU::TIU5Click(TObject *Sender)
{
TIUSelection[4] = TIU5->Checked;
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Workaround for SetLayeredWindowAttributes() to make transparent windows
extern lpfnSetLayeredWindowAttributes m_pSetLayeredWindowAttributes;
//---------------------------------------------------------------------------

void __fastcall TActiveTIU::FormActivate(TObject *Sender)
{
#if __BORLANDC__ >= 0x720      // CB10.1 only
AlphaBlend = false; // Remove Transparency
#else
// Remove WS_EX_LAYERED from this window styles
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) & ~WS_EX_LAYERED);
// Ask the window and its children to repaint
//Repaint();
#endif
}
//---------------------------------------------------------------------------

void __fastcall TActiveTIU::FormDeactivate(TObject *Sender)
{
if (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked) {
    MainForm->DebugMessagePopup("Warning", "At least one Active TIU must be selected", "");
    }
    
if (Setup->TransparentSpin->Value == Setup->TransparentSpin->MaxValue) return;	// Off
if (LocalTransparencyOverride) return;
#if __BORLANDC__ >= 0x720      // CB10.1 only
AlphaBlendValue = (255 * Setup->TransparentSpin->Value) / 100;
AlphaBlend = true;  // Add Transparency
#else
// Set WS_EX_LAYERED on this window
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes
m_pSetLayeredWindowAttributes(Handle, NULL, (255 * Setup->TransparentSpin->Value) / 100, LWA_ALPHA);
#endif
}
void __fastcall TActiveTIU::TIU_LabelClick(TObject *Sender)
{
LocalTransparencyOverride = !LocalTransparencyOverride;	// Toggle transparency locally
}
//---------------------------------------------------------------------------

void __fastcall TActiveTIU::FormShow(TObject *Sender)
{
TIU1->Checked = TIUSelection[0];
TIU2->Checked = TIUSelection[1];
TIU3->Checked = TIUSelection[2];
TIU4->Checked = TIUSelection[3];
TIU5->Checked = TIUSelection[4];
}
//---------------------------------------------------------------------------


void __fastcall TActiveTIU::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;
    }    
}
//---------------------------------------------------------------------------

