/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2014-2015 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/>.
*/


#ifndef TFormZ80EditorH
#define TFormZ80EditorH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Grids.hpp>
#include <Vcl.StdCtrls.hpp>
#include "TFormEditValue.h"
#include "..\Emulator\TEmulator.h"
#include "..\Settings\TSettings.h"
#include "..\..\Common\Z80DASM\z80dasm.h"
#include <Vcl.Menus.hpp>

//---------------------------------------------------------------------------
class TFormZ80Editor : public TForm
{
__published:	// Composants gérés par l'EDI
  TGroupBox *GroupBoxRegisters;
  TLabel *LabelRegAF;
  TPanel *PanelFormatRegAF;
  TPanel *PanelFormatRegBC;
  TPanel *PanelRegA;
  TPanel *PanelRegB;
  TPanel *PanelRegF;
  TPanel *PanelRegC;
  TPanel *PanelFormatRegAFAlt;
  TPanel *PanelRegAAlt;
  TPanel *PanelRegFAlt;
  TPanel *PanelFormatRegBCAlt;
  TPanel *PanelRegBAlt;
  TPanel *PanelRegCAlt;
  TPanel *PanelFormatRegIX;
  TPanel *PanelFormatRegIY;
  TPanel *PanelFormatRegDE;
  TPanel *PanelRegD;
  TPanel *PanelRegE;
  TPanel *PanelFormatRegHL;
  TPanel *PanelRegH;
  TPanel *PanelRegL;
  TPanel *PanelFormatRegI;
  TPanel *PanelRegI;
  TPanel *PanelFormatRegR;
  TPanel *PanelRegR;
  TLabel *LabelRegBC;
  TLabel *LabelRegDE;
  TLabel *LabelRegHL;
  TLabel *LabelRegAFAlt;
  TLabel *LabelRegBCAlt;
  TLabel *LabelRegIX;
  TLabel *LabelRegIY;
  TLabel *LabelRegI;
  TLabel *LabelRegR;
  TLabel *LabelRegDEAlt;
  TPanel *PanelFormatRegDEAlt;
  TPanel *PanelRegDAlt;
  TPanel *PanelRegEAlt;
  TLabel *LabelRegHLAlt;
  TPanel *PanelFormatRegHLAlt;
  TPanel *PanelRegHAlt;
  TPanel *PanelRegLAlt;
  TLabel *LabelRegPC;
  TPanel *PanelFormatRegPC;
  TPanel *PanelRegPC;
  TPanel *PanelRegIY;
  TPanel *PanelRegIX;
  TLabel *LabelRegSP;
  TPanel *PanelFormatRegSP;
  TPanel *PanelRegSP;
  TGroupBox *GroupBoxInstructions;
  TGroupBox *GroupBoxStack;
  TGroupBox *GroupBoxFlags;
  TStringGrid *StringGridInstructions;
  TStringGrid *StringGridStack;
  TComboBox *ComboBoxFlag7;
  TComboBox *ComboBoxFlag6;
  TComboBox *ComboBoxFlag5;
  TComboBox *ComboBoxFlag4;
  TComboBox *ComboBoxFlag3;
  TComboBox *ComboBoxFlag2;
  TComboBox *ComboBoxFlag1;
  TComboBox *ComboBoxFlag0;
  TGroupBox *GroupBoxInterrupt;
  TPanel *PanelIFF1;
  TLabel *LabelIFF2;
  TPanel *PanelIFF2;
  TComboBox *ComboBoxITMode;
  TLabel *LabelITMode;
  TPanel *PanelPrevPC;
  TLabel *LabelPrevPC;
	TScrollBar *ScrollBarAddress;
	TLabel *LabelTopAddress;
	TPanel *PanelTopAddress;
	TPanel *PanelButtonGotoPC;
	TPanel *PanelButtonGotoPPC;
	TPopupMenu *PopupMenuInstructions;
	TMenuItem *AddPCBreakpointMenu;
	TPanel *PanelButtonAddBreakpoint;
	TPanel *PanelButtonShowMemory;
	TLabel *LabelIFF1;
	TMenuItem *ShowMemoryMenu;
  void __fastcall FormShow(TObject *Sender);
  void __fastcall FormHide(TObject *Sender);
  void __fastcall FormResize(TObject *Sender);
  void __fastcall PanelFormatRegPCClick(TObject *Sender);
  void __fastcall PanelFormatRegAFClick(TObject *Sender);
  void __fastcall PanelFormatRegBCClick(TObject *Sender);
  void __fastcall PanelFormatRegDEClick(TObject *Sender);
  void __fastcall PanelFormatRegHLClick(TObject *Sender);
  void __fastcall PanelFormatRegAFAltClick(TObject *Sender);
  void __fastcall PanelFormatRegBCAltClick(TObject *Sender);
  void __fastcall PanelFormatRegDEAltClick(TObject *Sender);
  void __fastcall PanelFormatRegHLAltClick(TObject *Sender);
  void __fastcall PanelFormatRegIXClick(TObject *Sender);
  void __fastcall PanelFormatRegIYClick(TObject *Sender);
  void __fastcall PanelFormatRegSPClick(TObject *Sender);
  void __fastcall PanelFormatRegIClick(TObject *Sender);
  void __fastcall PanelFormatRegRClick(TObject *Sender);
  void __fastcall ComboBoxFlagChange(TObject *Sender);
  void __fastcall PanelRegAClick(TObject *Sender);
  void __fastcall PanelRegFClick(TObject *Sender);
  void __fastcall PanelRegBClick(TObject *Sender);
  void __fastcall PanelRegCClick(TObject *Sender);
  void __fastcall PanelRegDClick(TObject *Sender);
  void __fastcall PanelRegEClick(TObject *Sender);
  void __fastcall PanelRegHClick(TObject *Sender);
  void __fastcall PanelRegLClick(TObject *Sender);
  void __fastcall PanelRegAAltClick(TObject *Sender);
  void __fastcall PanelRegFAltClick(TObject *Sender);
  void __fastcall PanelRegBAltClick(TObject *Sender);
  void __fastcall PanelRegCAltClick(TObject *Sender);
  void __fastcall PanelRegDAltClick(TObject *Sender);
  void __fastcall PanelRegEAltClick(TObject *Sender);
  void __fastcall PanelRegHAltClick(TObject *Sender);
  void __fastcall PanelRegLAltClick(TObject *Sender);
  void __fastcall PanelRegIXClick(TObject *Sender);
  void __fastcall PanelRegIYClick(TObject *Sender);
  void __fastcall PanelRegSPClick(TObject *Sender);
  void __fastcall PanelRegPCClick(TObject *Sender);
  void __fastcall PanelRegIClick(TObject *Sender);
  void __fastcall PanelRegRClick(TObject *Sender);
  void __fastcall ComboBoxITModeChange(TObject *Sender);
  void __fastcall ComboBoxITModeDrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag7DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag6DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag5DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag4DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag3DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag2DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag1DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall ComboBoxFlag0DrawItem(TWinControl *Control, int Index, TRect &Rect,
          TOwnerDrawState State);
  void __fastcall StringGridInstructionsDrawCell(TObject *Sender, int ACol, int ARow,
          TRect &Rect, TGridDrawState State);
  void __fastcall StringGridStackDrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State);
  void __fastcall FormDestroy(TObject *Sender);
	void __fastcall ScrollBarAddressChange(TObject *Sender);
	void __fastcall PanelButtonGotoPCClick(TObject *Sender);
	void __fastcall PanelButtonGotoPPCClick(TObject *Sender);
	void __fastcall StringGridInstructionsDblClick(TObject *Sender);
	void __fastcall StringGridInstructionsMouseWheelDown(TObject *Sender, TShiftState Shift,
          TPoint &MousePos, bool &Handled);
	void __fastcall StringGridInstructionsMouseWheelUp(TObject *Sender, TShiftState Shift,
          TPoint &MousePos, bool &Handled);
	void __fastcall StringGridInstructionsKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
	void __fastcall PanelTopAddressClick(TObject *Sender);
	void __fastcall PanelButtonMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
	void __fastcall PanelButtonMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y);
	void __fastcall AddPCBreakpointMenuClick(TObject *Sender);
	void __fastcall StringGridInstructionsMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
	void __fastcall ShowMemoryMenuClick(TObject *Sender);
	void __fastcall StringGridInstructionsSelectCell(TObject *Sender, int ACol, int ARow,
          bool &CanSelect);


private:	// Déclarations utilisateur
  //
  // Members
  //
  TSettings* mSettingsP;
  tULong* mNextInstructions;
	tULong mTopAddress;
  tULong mMinAddress;
	tULong mMaxAddress;
  bool mPaused;
  TFormEditValue* mFormEditValue;
  TZ80dasmParam mZ80dasmParam;

  //
  // Methods
  //
  AnsiString __fastcall GetTitle(void);
  void __fastcall WMMove(TMessage &Message);
  void __fastcall SetSettings(TSettings* Value);
	bool __fastcall OnStepByStepEvent(tULong Condition, tNativeCPC* NativeCPC);
	void __fastcall UpdateRegisters(tNativeCPC* NativeCPC);
  void __fastcall UpdateFlags(tNativeCPC* NativeCPC);
  void __fastcall UpdateInstructions(tNativeCPC* NativeCPC, bool SelectPC);
  void __fastcall UpdateStack(tNativeCPC* NativeCPC);
	void __fastcall UpdateInterrupt(tNativeCPC* NativeCPC);
  void __fastcall UpdateRegistersEdition(bool Allow);
  void __fastcall UpdateTopAddress(tNativeCPC* NativeCPC);
	inline tULong __fastcall GetTopAddress(void) { return mTopAddress; };
	void __fastcall SetTopAddress(tULong Value);
	tULong __fastcall GetSelectedAddress(void);

	//
	// Private Properties
	//

public:		// Déclarations utilisateur
	//
	// Properties
	//
	__property AnsiString Title = { read=GetTitle };
	__property TSettings* Settings = { write=SetSettings };
	__property tULong TopAddress = { read=GetTopAddress, write=SetTopAddress };
	__property tULong SelectedAddress = { read=GetSelectedAddress };

  //
  // API
  //
	__fastcall TFormZ80Editor(TComponent* Owner);


#pragma option push -vi-
BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(WM_MOVE, TMessage, WMMove);
END_MESSAGE_MAP(TForm)
#pragma option pop
};
//---------------------------------------------------------------------------
extern PACKAGE TFormZ80Editor *FormZ80Editor;
//---------------------------------------------------------------------------
#endif
