/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2014-2018 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 "TFormEditValue.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


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

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


TFormEditValue *FormEditValue;
//---------------------------------------------------------------------------
__fastcall TFormEditValue::TFormEditValue(TComponent* Owner)
  : TForm(Owner)
{
  mSettingsP = NULL;
  mCurrentValue = 0;
  mNewValue = 0;
  mHexadecimal = false;
  mNbDigitsDecimal = 0;
  mNbDigitsHexa = 0;
  mMinValue = -1;
  mMaxValue = -1;
  mIncrement = 1;

  // Apply theme
  Font->Name = "Amstrad CPC464 Regular";
  Font->Size = 6;
  LabelCurrentValue->Font->Size = 12;
  LabelCurrentValue->Height = 16;
  PanelFormatNew->Font->Size = 12;
  EditNew->Font->Size = 12;
  EditNew->Height = 16;
  EditIncrement->Font->Size = 12;
  EditIncrement->Height = 16;

  //LabelCurrentValue->Top = ((PanelCurrent->Height - LabelCurrentValue->Height) / 2) - 1;
  EditNew->Top = ((PanelNew->Height - EditNew->Height) / 2) - 1;
  EditIncrement->Top = ((PanelIncrement->Height - EditIncrement->Height) / 2) - 1;
}
//---------------------------------------------------------------------------


void __fastcall TFormEditValue::FormShow(TObject *Sender)
{
  mNewValue = mCurrentValue;

  // Get NB Digit in Decimal
  mNbDigitsDecimal = 0;
  int Temp = mMaxValue;
  while (Temp != 0)
  {
    Temp = Temp / 10;
    mNbDigitsDecimal++;
  }

  // Get NB Digit in Dexadecimal
  mNbDigitsHexa = 0;
  Temp = mMaxValue;
  while (Temp != 0)
  {
    Temp = Temp / 16;
    mNbDigitsHexa++;
  }

  if (mHexadecimal == true)
  {
    LabelCurrentValue->Caption = AnsiString::IntToHex(mCurrentValue, mNbDigitsHexa);
    PanelFormatNew->Caption = "&&";
    EditNew->Text = AnsiString::IntToHex(mNewValue, mNbDigitsHexa);
    EditIncrement->Text = AnsiString::IntToHex(mIncrement, 0);
    EditNew->MaxLength = mNbDigitsHexa;
  }
  else
  {
    LabelCurrentValue->Caption = mCurrentValue;
    PanelFormatNew->Caption = "";
    EditNew->Text = mNewValue;
    EditIncrement->Text = mIncrement;
    EditNew->MaxLength = mNbDigitsDecimal;
  }


  // Apply theme
  if (mSettingsP)
  {
    Color = mSettingsP->ThemeBackgroundColor;
    Font->Color = mSettingsP->ThemeText1Color;
    LabelCurrent->Font->Color = mSettingsP->ThemeText2Color;
    LabelCurrentValue->Font->Color = mSettingsP->ThemeText1Color;
    LabelNew->Font->Color = mSettingsP->ThemeText2Color;
    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;
      }
    }
  }

  EditNew->SetFocus();
  EditNew->SelectAll();

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


void __fastcall TFormEditValue::EditNewChange(TObject *Sender)
{
  if ( (EditNew->Focused() == true)
       && (EditNew->Text.Trim().Length() > 0) )
  {
    // Presence of char '&'
    bool Hexa = mHexadecimal;
    AnsiString StringNewValue = EditNew->Text.Trim();
    if (StringNewValue.Pos("&") == 1)
    {
      StringNewValue = StringNewValue.SubString(2, StringNewValue.Length());
      Hexa = true;
    }

    try
    {
      if (Hexa == true)
      {
        mNewValue = StrToInt("0x" + StringNewValue);
      }
      else
      {
        mNewValue = StringNewValue.ToInt();
      }

      // Check valid value
      if ( (mMaxValue != -1) && (mNewValue > mMaxValue) )
      {
        PanelButtonOK->Visible = false;
      }
      else if ( (mMinValue != -1) && (mNewValue < mMinValue) )
      {
        PanelButtonOK->Visible = false;
      }
      else
      {
        PanelButtonOK->Visible = true;
      }
    }
    catch(...)
    {
      mNewValue = mCurrentValue;
      PanelButtonOK->Visible = false;
    }
  }

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

void __fastcall TFormEditValue::PanelFormatNewClick(TObject *Sender)
{
  mHexadecimal = (mHexadecimal == true) ? false : true;
  if (mHexadecimal == true)
  {
    PanelFormatNew->Caption = "&&";
    EditNew->Text = AnsiString::IntToHex(mNewValue, mNbDigitsHexa);
    EditIncrement->Text = AnsiString::IntToHex(mIncrement, 0);
    LabelCurrentValue->Caption = AnsiString::IntToHex(mCurrentValue, mNbDigitsHexa);
    EditNew->MaxLength = mNbDigitsHexa;
  }
  else
  {
    PanelFormatNew->Caption = "";
    EditNew->Text = mNewValue;
    EditIncrement->Text = mIncrement;
    LabelCurrentValue->Caption = mCurrentValue;
    EditNew->MaxLength = mNbDigitsDecimal;
  }

  EditNew->SetFocus();
  EditNew->SelectAll();

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


void __fastcall TFormEditValue::EditIncrementChange(TObject *Sender)
{
  if ( (EditIncrement->Focused() == true)
       && (EditIncrement->Text.Trim().Length() > 0) )
  {
    // Presence of char '&'
    bool Hexa = mHexadecimal;
    AnsiString StringNewValue = EditIncrement->Text.Trim();
    if (StringNewValue.Pos("&") == 1)
    {
      StringNewValue = StringNewValue.SubString(2, StringNewValue.Length());
      Hexa = true;
    }

    try
    {
      if (Hexa == true)
      {
        mIncrement = StrToInt("0x" + StringNewValue);
      }
      else
      {
        mIncrement = StringNewValue.ToInt();
      }

      if (mIncrement < 1)
      {
        mIncrement = 1;
      }
    }
    catch(...)
    {
      mIncrement = 1;
    }
  }

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


void __fastcall TFormEditValue::PanelButtonIncrementClick(TObject *Sender)
{
  try
  {
    if (mHexadecimal == true)
    {
      mNewValue = StrToInt("0x" + EditNew->Text);
    }
    else
    {
      mNewValue = EditNew->Text.ToInt();
    }

    if ( (mMaxValue != -1) && (mMinValue != -1) )
    {
      if ( (mMaxValue - mNewValue) >= mIncrement)
      {
        mNewValue += mIncrement;
      }
      else
      {
        mNewValue = mMinValue + (mIncrement - ((mMaxValue+1) - mNewValue));
      }
    }
    else
    {
      mNewValue += mIncrement;
    }

    if (mHexadecimal == true)
    {
      EditNew->Text = AnsiString::IntToHex(mNewValue, mNbDigitsHexa);
    }
    else
    {
      EditNew->Text = mNewValue;
    }

    PanelButtonOK->Visible = true;
  }
  catch(...)
  {
    mNewValue = mCurrentValue;
    PanelButtonOK->Visible = false;
  }

  ActiveControl = EditNew;

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


void __fastcall TFormEditValue::PanelButtonDecrementClick(TObject *Sender)
{
  try
  {
    if (mHexadecimal == true)
    {
      mNewValue = StrToInt("0x" + EditNew->Text);
    }
    else
    {
      mNewValue = EditNew->Text.ToInt();
    }

    if ( (mMaxValue != -1) && (mMinValue != -1) )
    {
      if ( (mNewValue - mMinValue) >= mIncrement)
      {
        mNewValue -= mIncrement;
      }
      else
      {
        mNewValue = mMaxValue - (mIncrement - ((mNewValue+1) - mMinValue));
      }
    }
    else
    {
      mNewValue -= mIncrement;
    }

    if (mHexadecimal == true)
    {
      EditNew->Text = AnsiString::IntToHex(mNewValue, mNbDigitsHexa);
    }
    else
    {
      EditNew->Text = mNewValue;
    }

    PanelButtonOK->Visible = true;
  }
  catch(...)
  {
    mNewValue = mCurrentValue;
    PanelButtonOK->Visible = false;
  }

  ActiveControl = EditNew;

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


void __fastcall TFormEditValue::FormKeyPress(TObject *Sender, System::WideChar &Key)
{
  if (Key == VK_RETURN)
  {
    PanelButtonOK->OnClick(PanelButtonOK);
		Key = 0; // Stop key press propagation to focused control
  }
  else if (Key == VK_ESCAPE)
  {
    PanelButtonCancel->OnClick(PanelButtonCancel);
		Key = 0; // Stop key press propagation to focused control
  }
	else if (Key == '+')
	{
		PanelButtonIncrementClick(PanelButtonIncrement);
		Key = 0; // Stop key press propagation to focused control
	}
	else if (Key == '-')
	{
		PanelButtonDecrementClick(PanelButtonDecrement);
		Key = 0; // Stop key press propagation to focused control
	}

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


void __fastcall TFormEditValue::CopyMenuClick(TObject *Sender)
{
  Clipboard()->AsText = EditNew->Text;
  ActiveControl = EditNew;

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

void __fastcall TFormEditValue::PasteMenuClick(TObject *Sender)
{
  AnsiString NewValue = Clipboard()->AsText;
  int Length = NewValue.Length();
  if (Length > EditNew->MaxLength)
  {
    NewValue = NewValue.SubString(Length - EditNew->MaxLength + 1, EditNew->MaxLength);
  }

  ActiveControl = EditNew;
  EditNew->Text = NewValue;
  EditNew->SelectAll();

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


void __fastcall TFormEditValue::PanelButtonOKClick(TObject *Sender)
{
  TPanel* Panel = (TPanel*)Sender;
  Panel->Color = mSettingsP->ThemeButtonColor;
  Panel->Font->Color = mSettingsP->ThemeButtonTextColor;
  Panel->Refresh();

  ModalResult = mrOk;

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

void __fastcall TFormEditValue::PanelButtonCancelClick(TObject *Sender)
{
  TPanel* Panel = (TPanel*)Sender;
  Panel->Color = mSettingsP->ThemeButtonColor;
  Panel->Font->Color = mSettingsP->ThemeButtonTextColor;
  Panel->Refresh();

  ModalResult = mrCancel;

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


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

