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

#include <vcl.h>
#pragma hdrstop

#include "FormMain.h"
#include "FormAbout.h"
#include "FormDiag.h"
#include "FormDebug.h"
#include "FormOptions.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GR32_Image"
#pragma resource "*.dfm"

using Types::TRect;

TMainForm *MainForm;
TOSCABoard *z80emu;

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
	z80emu = new TOSCABoard(this->Handle);
}

__fastcall TMainForm::~TMainForm()
{
	if (z80emu)
	{
		delete z80emu;
		z80emu = NULL;
	}
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::MenuControlOptionsClick(TObject *Sender)
{
	OptionsForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MenuHelpAboutClick(TObject *Sender)
{
	AboutForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MenuFileExitClick(TObject *Sender)
{
	this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MenuDebugDebuggerClick(TObject *Sender)
{
	if (!DebugForm->Visible)
	{
		DebugForm->Show();
	}
	else
	{
		DebugForm->BringToFront();
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MenuDebugIOMonitorClick(TObject *Sender)
{
	if (DiagForm)
	{
		if (!DiagForm->Visible)
		{
			DiagForm->Show();
		}
		else
		{
			DiagForm->BringToFront();
		}
	}
	else
	{
		// TODO: Reconstruct and show Diagnostic Form here
	}
}
//---------------------------------------------------------------------------



void __fastcall TMainForm::FormShow(TObject *Sender)
{
	DebugForm->BtnRunClick(this);
	fExtraHeight = this->Height - this->ClientHeight;
	fExtraWidth = this->Width - this->ClientWidth;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::IPLColdBoot1Click(TObject *Sender)
{
	z80emu->PowerOnReset();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::NMIWarmBoot1Click(TObject *Sender)
{
	z80emu->NMIRequired = true;
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::FormCanResize(TObject *Sender, int &NewWidth, int &NewHeight,
          bool &Resize)
{
#if 0	// doesn't work yet
	int WfromH = ((NewHeight - fExtraHeight) * 512) / 310;
	int HfromW = ((NewWidth - fExtraHeight) * 310) / 512;
	if ((WfromH - (NewWidth - fExtraWidth)) > (HfromW - (NewHeight - fExtraHeight)))
	{
		NewWidth = WfromH + fExtraWidth;
	}
	else
	{
		NewHeight = HfromW + fExtraHeight;
	}
#endif

	Resize = true;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)

{
	// Key Pressed
	z80emu->KeyPressed(Key, Shift.ToInt());
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)

{
	// Key Released
	z80emu->KeyReleased(Key, Shift.ToInt());
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormMouseMove(TObject *Sender, TShiftState Shift, int X,
			 int Y)
{
	// Handle Mouse Events
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CMDialogKey(TCMDialogKey &Message)
{
	if ((Message.CharCode == VK_TAB) || (Message.CharCode == VK_F10))
	{
		z80emu->KeyPressed(Message.CharCode, 0);
		Message.Result = -1;
	}
	else
	{
		TForm::Dispatch((void *) &Message);
	}
}
void __fastcall TMainForm::MenuFileLoadOSFClick(TObject *Sender)
{
	//	Load a "signed" .OSF Binary Image
	OpenDialog->Title = L"Select OSF Binary to be Loaded";
	OpenDialog->DefaultExt = L"OSF";
	OpenDialog->Filter = L"OSF Binary Images (*.OSF)|*.OSF|All Files (*.*)|*.*";
	OpenDialog->FilterIndex = 0;
	if (OpenDialog->Execute())
	{
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MenuFileLoadRawClick(TObject *Sender)
{
	//	Load an "unsigned" (i.e. RAW) Binary Image
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::StatusBarDrawPanel(TStatusBar *StatusBar, TStatusPanel *Panel,
			 const Types::TRect &Rect)
{
	StatusBar->Canvas->Brush->Color = clBtnFace;

	if (Panel->ID == 5)
	{
		if ((z80emu != NULL) && (z80emu->SDReading()))
		{
			StatusBar->Canvas->Brush->Color = clLime;
		}
	}
	else
	{
		if ((z80emu != NULL) && (z80emu->SDWriting()))
		{
			StatusBar->Canvas->Brush->Color = clRed;
		}
	}

	StatusBar->Canvas->FillRect(Rect);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
	if (z80emu != NULL)
	{
		CanClose = z80emu->CanClose();
	}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
	// Form Closing
	if (z80emu)
	{
		z80emu->ShutdownThreads();

		delete z80emu;
		z80emu = NULL;
	}
}
//---------------------------------------------------------------------------

