/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2014-2016 by Frédéric Coste

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <vcl.h>
#include <Clipbrd.hpp>
#pragma hdrstop
#include "TFormTextCapture.h"
#include "TFormMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


//=================================
// Options

//=================================


TFormTextCapture *FormTextCapture;
//---------------------------------------------------------------------------
__fastcall TFormTextCapture::TFormTextCapture(TComponent* Owner)
  : TForm(Owner)
{
  DoubleBuffered = true;

	// Apply theme
	Font->Name = "Amstrad CPC464 Regular";
	Font->Size = 6;
}
//---------------------------------------------------------------------------

void __fastcall TFormTextCapture::FormShow(TObject *Sender)
{
	// Subscribe to step by step event
	if (FormMain == NULL)
	{
		Close();
		return;
	}

	FormMain->OnTextCaptureFormShow();
	FormMain->SubscribeToCPCEvents(OnStepByStepEvent);

  MemoCapture->Lines->Clear();
  mCurrentLine = "";

  if (mSettingsP)
  {
    mSettingsP->TextCaptureFormVisible = true;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormTextCapture::FormHide(TObject *Sender)
{
	// Unsubscribe to step by step event
	if (FormMain != NULL)
	{
		FormMain->UnsubscribeToCPCEvents(OnStepByStepEvent);
		FormMain->OnTextCaptureFormHide();
	}

  if (mSettingsP)
  {
    mSettingsP->TextCaptureFormVisible = false;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormTextCapture::SetSettings(TSettings* Value)
{
  mSettingsP = Value;

  if (    (mSettingsP->TextCaptureFormLeft != 0)
       || (mSettingsP->TextCaptureFormTop != 0)
       || (mSettingsP->TextCaptureFormWidth != 0)
       || (mSettingsP->TextCaptureFormHeight != 0) )
  {
    Position = poDesigned;
    Left = mSettingsP->TextCaptureFormLeft;
		Top = mSettingsP->TextCaptureFormTop;
    Width = mSettingsP->TextCaptureFormWidth;
    Height = mSettingsP->TextCaptureFormHeight;
  }

  ShowHint = mSettingsP->ShowHints;

  // Apply theme
	Color = mSettingsP->ThemeBackgroundColor;
	Font->Color = mSettingsP->ThemeText1Color;
	for (int Loop=0; Loop < ComponentCount; Loop++)
	{
		if (Components[Loop]->Name.Pos("PanelButton") == 1)
		{
			((TPanel*)Components[Loop])->Color = mSettingsP->ThemeButtonColor;
			((TPanel*)Components[Loop])->Font->Color = mSettingsP->ThemeButtonTextColor;
		}
		else if (Components[Loop]->Name.Pos("PanelCheck") == 1)
		{
			((TPanel*)Components[Loop])->Color = mSettingsP->ThemeButtonColor;
			((TPanel*)Components[Loop])->Font->Color = mSettingsP->ThemeButtonTextColor;
		}

		// Refresh display
		if ( (Visible == true) && (((TControl*)Components[Loop])->Visible == true) )
		{
			((TControl*)Components[Loop])->Invalidate();
		}
	}

	if (mSettingsP->HideAll == false)
  {
    Visible = mSettingsP->TextCaptureFormVisible;
  }
}
//---------------------------------------------------------------------------


void __fastcall TFormTextCapture::FormResize(TObject *Sender)
{
	if ( (Visible == true) && (mSettingsP) )
	{
		mSettingsP->TextCaptureFormWidth = Width;
		mSettingsP->TextCaptureFormHeight = Height;
	}

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormTextCapture::WMMove(TMessage &Message)
{
	if ( (Visible == true) && (mSettingsP) )
  {
    mSettingsP->TextCaptureFormLeft = Left;
    mSettingsP->TextCaptureFormTop = Top;
  }

  NOT_USED(Message);
}
//---------------------------------------------------------------------------


bool __fastcall TFormTextCapture::OnStepByStepEvent(tULong Condition, tNativeCPC* NativeCPC)
{
  bool Pause = NativeCPC->paused;

  if (Condition & EC_TEXT_CAPTURE)
  {
    tZ80* Z80 = NativeCPC->Z80;

    // TXT WRITE CHAR firmware indirection
    if (_PC == 0xbdd3)
    {
			char* CaptureString = (char*)" ";
			*CaptureString = _A;
      mCurrentLine += CaptureString;
    }
    // TXT OUT ACTION firmware indirection
    else if (_PC == 0xbdd9)
    {
      if (_A == 0x0D)
      {
        MemoCapture->Lines->Add(mCurrentLine);
        mCurrentLine = "";
      }
    }

  }

  return Pause;
}
//---------------------------------------------------------------------------

void __fastcall TFormTextCapture::PanelCheckCaptureClick(TObject *Sender)
{
	FormMain->NativeCPC->text_capture = FormMain->NativeCPC->text_capture ? 0 : 1;
  if (FormMain->NativeCPC->text_capture)
  {
    Caption = "Text Capture - [Capturing]";
		PanelCheckCapture->Color = mSettingsP->ThemeButtonTextColor;
		PanelCheckCapture->Font->Color = mSettingsP->ThemeButtonColor;
	}
	else
	{
		Caption = "Text Capture";
		PanelCheckCapture->Color = mSettingsP->ThemeButtonColor;
		PanelCheckCapture->Font->Color = mSettingsP->ThemeButtonTextColor;
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormTextCapture::PanelButtonClearClick(TObject *Sender)
{
  MemoCapture->Lines->Clear();

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormTextCapture::PanelButtonMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
  ((TPanel*)Sender)->Color = mSettingsP->ThemeButtonTextColor;
  ((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonColor;

  NOT_USED(Button);
  NOT_USED(Shift);
  NOT_USED(X);
  NOT_USED(Y);
}
//---------------------------------------------------------------------------

void __fastcall TFormTextCapture::PanelButtonMouseUp(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
  ((TPanel*)Sender)->Color = mSettingsP->ThemeButtonColor;
  ((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonTextColor;

  NOT_USED(Button);
  NOT_USED(Shift);
  NOT_USED(X);
  NOT_USED(Y);
}
//---------------------------------------------------------------------------

void __fastcall TFormTextCapture::Copytoclipboard1Click(TObject *Sender)
{
  Clipboard()->AsText = MemoCapture->Text;

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------

