//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Aspect.h"
#include "Train_Control.h"
#include "Layout.h"
#include "Clipbrd.hpp"
#include "assert.h"
#include "Debug_Controls.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TAspects *Aspects;
//---------------------------------------------------------------------------
__fastcall TAspects::TAspects(TComponent* Owner)
	: TForm(Owner)
{
assert(false);	// never called since I used the overloaded constructor below
}
//---------------------------------------------------------------------------
__fastcall TAspects::TAspects(TComponent* Owner, int Aspect, int iMode, int iType, String Line1, String Line2, String Line3)
	: TForm(Owner)     // overloaded constructors get run time error if exactly 2 parameters are used, always use more
{
FinalAspect = Aspect;
switch (Aspect) {
	case AsDark:
    	DarkButton->Checked = true;
        break;

	case AsStopSignal:
    	StopSignalButton->Checked = true;
        break;

	case AsClear:
    	ClearButton->Checked = true;
        break;

    case AsStopandProceed:
    	StopandProceedButton->Checked = true;
        break;

	case AsApproach:
    	ApproachButton->Checked = true;
        break;

	case AsMediumClear:
    	MediumClearButton->Checked = true;
        break;

	case AsMediumApproach:
    	MediumApproachButton->Checked = true;
        break;

	case AsSlowClear:
    	SlowClearButton->Checked = true;
        break;

	case AsSlowApproach:
    	SlowApproachButton->Checked = true;
        break;

	case AsApproachMedium:
    	ApproachMediumButton->Checked = true;
        break;

	case AsApproachSlow:
    	ApproachSlowButton->Checked = true;
        break;

	case AsRestricting:
    	RestrictingButton->Checked = true;
        break;

	case AsApproachLimited:
    	ApproachLimitedButton->Checked = true;
        break;

	case AsLimitedClear:
    	LimitedClearButton->Checked = true;
        break;

	case AsLimitedApproach:
    	LimitedApproachButton->Checked = true;
        break;

	case AsMediumApproachSlow:
    	MediumSlowApproachButton->Checked = true;
        break;

    case AsTest:
        TestButton->Checked = true;
        break;
	}
//
// Enable the radio buttons differently depending on the number of lights
//
switch(iMode) {
	case Hd1LED:	// one light signals have the fewest aspects
	    	//DarkButton->Enabled = false;
            if (iType != ABSOLUTE_HEAD_BLOCK) {
		    	StopSignalButton->Enabled = false;
                } else {
				StopandProceedButton->Enabled = false;
                }
	    	//ClearButton->Enabled = false;
	    	//ApproachButton->Enabled = false;
	    	MediumClearButton->Enabled = false;
	    	MediumApproachButton->Enabled = false;
	    	SlowClearButton->Enabled = false;
	    	SlowApproachButton->Enabled = false;
	    	ApproachMediumButton->Enabled = false;
	    	ApproachSlowButton->Enabled = false;
	    	RestrictingButton->Enabled = false;
	    	ApproachLimitedButton->Enabled = false;
	    	LimitedClearButton->Enabled = false;
	    	LimitedApproachButton->Enabled = false;
	    	MediumSlowApproachButton->Enabled = false;
    	break;
    case Hd2LED:
	    	//DarkButton->Enabled = false;
            if (iType != ABSOLUTE_HEAD_BLOCK) {
		    	StopSignalButton->Enabled = false;
                } else {
				StopandProceedButton->Enabled = false;
                }
	    	//ClearButton->Enabled = false;
	    	//ApproachButton->Enabled = false;
	    	//MediumClearButton->Enabled = false;
	    	//MediumApproachButton->Enabled = false;
	    	//SlowClearButton->Enabled = false;
	    	//SlowApproachButton->Enabled = false;
	    	//ApproachMediumButton->Enabled = false;
	    	ApproachSlowButton->Enabled = false;
	    	//RestrictingButton->Enabled = false;
	    	//ApproachLimitedButton->Enabled = false;
	    	//LimitedClearButton->Enabled = false;
	    	//LimitedApproachButton->Enabled = false;
	    	MediumSlowApproachButton->Enabled = false;
    	break;
    case Hd3LED:	// three light signals have all possible aspects
	    	//DarkButton->Enabled = false;
	    	if (iType != ABSOLUTE_HEAD_BLOCK) {
		    	StopSignalButton->Enabled = false;
                } else {
				StopandProceedButton->Enabled = false;
                }
	    	//ClearButton->Enabled = false;
	    	//ApproachButton->Enabled = false;
	    	//MediumClearButton->Enabled = false;
	    	//MediumApproachButton->Enabled = false;
	    	//SlowClearButton->Enabled = false;
	    	//SlowApproachButton->Enabled = false;
	    	//ApproachMediumButton->Enabled = false;
	    	//ApproachSlowButton->Enabled = false;
	    	//RestrictingButton->Enabled = false;
	    	//ApproachLimitedButton->Enabled = false;
	    	//LimitedClearButton->Enabled = false;
	    	//LimitedApproachButton->Enabled = false;
	    	//MediumSlowApproachButton->Enabled = false;
    	break;
	case HdMLED:	// three light signals with three LED have the fewest aspects
	    	//DarkButton->Enabled = false;
            if (iType != ABSOLUTE_HEAD_BLOCK) {
		    	StopSignalButton->Enabled = false;
                } else {
				StopandProceedButton->Enabled = false;
                }
	    	//ClearButton->Enabled = false;
	    	//ApproachButton->Enabled = false;
	    	MediumClearButton->Enabled = false;
	    	MediumApproachButton->Enabled = false;
	    	SlowClearButton->Enabled = false;
	    	SlowApproachButton->Enabled = false;
	    	ApproachMediumButton->Enabled = false;
	    	ApproachSlowButton->Enabled = false;
	    	RestrictingButton->Enabled = false;
	    	ApproachLimitedButton->Enabled = false;
	    	LimitedClearButton->Enabled = false;
	    	LimitedApproachButton->Enabled = false;
	    	MediumSlowApproachButton->Enabled = false;
    	break;
    }
Caption = Line1;
Info->Caption = Line2 + (iType == ABSOLUTE_HEAD_BLOCK ? " (Absolute)" : " (Permissive)");
Hint = Line3;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::DarkButtonClick(TObject *Sender)
{
// Dark
FinalAspect = AsDark;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::StopSignalButtonClick(TObject *Sender)
{
// Stop Signal
FinalAspect = AsStopSignal;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::ClearButtonClick(TObject *Sender)
{
// Clear
FinalAspect = AsClear;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::StopandProceedButtonClick(TObject *Sender)
{
// Stop and Proceed
FinalAspect = AsStopandProceed;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::ApproachButtonClick(TObject *Sender)
{
// Approach
FinalAspect = AsApproach;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::MediumClearButtonClick(TObject *Sender)
{
// Medium Clear
FinalAspect = AsMediumClear;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::MediumApproachButtonClick(TObject *Sender)
{
// Medium Approach
FinalAspect = AsMediumApproach;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::SlowClearButtonClick(TObject *Sender)
{
// Slow Clear
FinalAspect = AsSlowClear;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::SlowApproachButtonClick(TObject *Sender)
{
//Slow Approach
FinalAspect = AsSlowApproach;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::ApproachMediumButtonClick(TObject *Sender)
{
// Approach Medium
FinalAspect = AsApproachMedium;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::ApproachSlowButtonClick(TObject *Sender)
{
// Approach Slow
FinalAspect = AsApproachSlow;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::RestrictingButtonClick(TObject *Sender)
{
// Restricting
FinalAspect = AsRestricting;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::ApproachLimitedButtonClick(TObject *Sender)
{
// Approach Limited
FinalAspect = AsApproachLimited;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::LimitedClearButtonClick(TObject *Sender)
{
// Limited Clear
FinalAspect = AsLimitedClear;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::LimitedApproachButtonClick(TObject *Sender)
{
// Limited Approach
FinalAspect = AsLimitedApproach;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::MediumSlowApproachButtonClick(TObject *Sender)
{
// Medium Slow Approach
FinalAspect = AsMediumApproachSlow;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::TestButtonClick(TObject *Sender)
{
// Test
FinalAspect = AsTest;
}
//---------------------------------------------------------------------------

void __fastcall TAspects::CancelButtonClick(TObject *Sender)
{
FinalAspect = AsNone;
ModalResult = mrCancel;
}
//---------------------------------------------------------------------------
void __fastcall TAspects::OKButtonClick(TObject *Sender)
{
// FinalAspect = selected radio button
ModalResult = mrOk;
}
//---------------------------------------------------------------------------

void __fastcall TAspects::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 TAspects::MoreInformation1Click(TObject *Sender)
{
HINSTANCE retv = ShellExecute(NULL, TEXT("open"), TEXT("http://www.silogic.com/trains/RTC_Control_Language%20-%20Signaling.html"), NULL, NULL, SW_SHOWNORMAL);
if ((int)retv <= 32) {
	// error
    if (DEBUGGING) DB->Debug_Print("Shell Execute error - return value = " + String((int) retv));
    }
}
//---------------------------------------------------------------------------


