//---------------------------------------------------------------------------
/*
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

    This file is part of Remote Train Control.

    Remote Train Control 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.

    Remote Train Control 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 Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

*/
// Home Page : http://www.silogic.com/trains/RTC_Running.html
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Prog_ControlH
#define Prog_ControlH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include "_GClass.hpp"
#include "AbCBitBt.hpp"
#include "HideButton.h"
#include "AbLED.hpp"
#include <ComCtrls.hpp>
#include "AbSwitch.hpp"
#include "QuitButton.h"
#include "AbNumEdit.hpp"
#include "ColorButton.hpp"

#include <Dialogs.hpp>
#include "AbDial.hpp"
#include <Menus.hpp>
#include "LED3Panel.h"
#include "LED2Panel.h"
#include "LED1Panel.h"

//---------------------------------------------------------------------------
class TProgram_Control : public TForm
{
__published:	// IDE-managed Components
	TOpenDialog *OpenDialog;
	TPanel *BackgroundPanel;
	TPanel *MessageListPanel;
    TRichEdit *PCMessages;
	TPanel *Top_Message_Panel;
    TLabel *MessageNote;
	TButton *CopyAllButton;
	TButton *SavePCButton;
	TButton *ClearButton;
	TPanel *HidePanel;
	TAbColBitBtn *ESTOP;
	TQuitButton *CloseButton;
	THideButton *HideButton;
	TImage *Copyright;
	TPopupMenu *FilePopupMenu;
	TMenuItem *EditScript;
	TMenuItem *RemoveScriptfromList1;
	TMenuItem *CleartheList;
	TPanel *UIPanel;
	TLabel *ScriptFileLabel;
	TComboBox *MRUComboBox;
	TButton *BrowseforFileButton;
	TAbLED *PCThreadLED;
	TButton *CompileButton;
	TButton *RunScriptButton;
	TAbLED *RFIDLED;
	TButton *PauseButton;
	TButton *ResetCnt;
	TLabel *Counter01Label;
	TLabel *Counter02Label;
	TLabel *Counter04Label;
	TLabel *Counter05Label;
	TLabel *Counter06Label;
	TLabel *ElapsedTimeLabel;
	TAbNumEdit *ETime;
	TAbNumEdit *Counter06;
	TAbNumEdit *Counter05;
	TAbNumEdit *Counter04;
	TAbNumEdit *Counter03;
	TAbNumEdit *Counter02;
	TAbNumEdit *Counter01;
	TLabel *Counter03Label;
	TSaveDialog *SaveDialog1;
	TAbLED *UserLED1;
	TAbLED *UserLED2;
	TAbLED *UserLED3;
	TAbLED *UserLED4;
	TPanel *LabelPanel;
	TLabel *Title;
	TAbColBitBtn *PreviousPC;
	TAbColBitBtn *NextPC;
	TPanel *InterpreterInputLinePanel;
	TButton *InteractiveButton;
	TComboBox *InteractiveCommandComboBox;
    TLabel *InputLabel;
	TPopupMenu *PCMessagesPopupMenu;
	TMenuItem *Cut1;
	TMenuItem *Copy1;
	TMenuItem *Paste1;
	TMenuItem *SelectAll1;
	TMenuItem *Clear1;
	TMenuItem *SavetoDisk1;
	TMenuItem *ToggleWordWrap;
	TAbNumSpin *EngineSpin;
	TLabel *Label47;
	TButton *ShowDetectedTagsButton;
	TColorButton *StopButtonOpDummy;
	TColorButton *StopButtonOp;
	TPageControl *ButtonControl;
	TTabSheet *ButtonSheet1;
	TPanel *ButtonPanel1;
	TButton *Function01Button;
	TButton *Function02Button;
	TButton *Function03Button;
	TButton *Function04Button;
	TButton *Function05Button;
	TButton *Function06Button;
	TButton *Function07Button;
	TButton *Function08Button;
	TButton *Function09Button;
	TButton *Function10Button;
	TTabSheet *ButtonSheet2;
	TTabSheet *ButtonSheet3;
	TPanel *ButtonPanel2;
	TButton *Function11Button;
	TButton *Function12Button;
	TButton *Function13Button;
	TButton *Function14Button;
	TButton *Function15Button;
	TButton *Function16Button;
	TButton *Function17Button;
	TButton *Function18Button;
	TButton *Function19Button;
	TButton *Function20Button;
	TPanel *ButtonPanel3;
	TButton *Function21Button;
	TButton *Function22Button;
	TButton *Function23Button;
	TButton *Function24Button;
	TButton *Function25Button;
	TButton *Function26Button;
	TButton *Function27Button;
	TButton *Function28Button;
	TButton *Function29Button;
	TButton *Function30Button;
	TButton *ShowLayoutButton;
	TMenuItem *FindButton;
	TFindDialog *FindDialog1;
	TPopupMenu *PCPopupMenu;
	TMenuItem *ShowLayoutWindow1;
	TMenuItem *ShowSignalsWindow1;
	TMenuItem *ShowTagsWindow1;
	TPanel *SignalMast;
	TPopupMenu *WindowPopupMenu;
	TMenuItem *ShowLayout;
	TMenuItem *ShowCabSignals;
	TMenuItem *More_Information;
    TMenuItem *PasteScriptNamefromClipboad1;
    TMenuItem *CopyScriptNametoClipboard1;
    TCheckBox *AutoScroll;
    TMenuItem *ShowTagsWindow2;
    TMenuItem *ViewScriptDir;
	void __fastcall ESTOPClick(TObject *Sender);
	void __fastcall HideButtonClick(TObject *Sender);
    void __fastcall Reset_PC();
	void __fastcall CloseButtonClick(TObject *Sender);
	void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
	void __fastcall PauseButtonClick(TObject *Sender);
	void __fastcall StopButtonOpMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
	void __fastcall ResetCntClick(TObject *Sender);
	void __fastcall FormActivate(TObject *Sender);
	void __fastcall FormDeactivate(TObject *Sender);
	void __fastcall TitleClick(TObject *Sender);
	void __fastcall RunScriptButtonClick(TObject *Sender);
	void __fastcall CompileButtonClick(TObject *Sender);
	void __fastcall BrowseforFileButtonClick(TObject *Sender);
	void __fastcall CopyAllButtonClick(TObject *Sender);
	void __fastcall FunctionButtonClick(TObject *Sender);
	void __fastcall FormShow(TObject *Sender);
	void __fastcall ClearButtonClick(TObject *Sender);
	void __fastcall SavePCButtonClick(TObject *Sender);
	void __fastcall EditScriptClick(TObject *Sender);
	void __fastcall RemoveScriptfromList1Click(TObject *Sender);
	void __fastcall CleartheListClick(TObject *Sender);
	void __fastcall UserLED1Click(TObject *Sender);
	void __fastcall UserLED2Click(TObject *Sender);
	void __fastcall UserLED3Click(TObject *Sender);
	void __fastcall UserLED4Click(TObject *Sender);
	void __fastcall NextPCClick(TObject *Sender);
	void __fastcall PreviousPCClick(TObject *Sender);
	void __fastcall InteractiveButtonClick(TObject *Sender);
	void __fastcall InteractiveCommandComboBoxKeyPress(TObject *Sender,
          char &Key);
	void __fastcall InteractiveCommandComboBoxKeyDown(TObject *Sender,
          WORD &Key, TShiftState Shift);
	void __fastcall InteractiveCommandComboBoxKeyUp(TObject *Sender,
          WORD &Key, TShiftState Shift);
	void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
	void __fastcall CopyrightClick(TObject *Sender);
	void __fastcall Cut1Click(TObject *Sender);
	void __fastcall Copy1Click(TObject *Sender);
	void __fastcall SelectAll1Click(TObject *Sender);
	void __fastcall Paste1Click(TObject *Sender);
	void __fastcall ToggleWordWrapClick(TObject *Sender);
	void __fastcall InteractiveCommandComboBoxChange(TObject *Sender);
	void __fastcall ShowDetectedTagsButtonClick(TObject *Sender);
	void __fastcall ShowLayoutButtonClick(TObject *Sender);
	void __fastcall FindButtonClick(TObject *Sender);
	void __fastcall FindDialog1Find(TObject *Sender);
	void __fastcall MRUComboBoxChange(TObject *Sender);
	void __fastcall MRUComboBoxKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
	void __fastcall ShowLayoutWindow1Click(TObject *Sender);
	void __fastcall ShowLayoutClick(TObject *Sender);
	void __fastcall ShowCabSignalsClick(TObject *Sender);
	void __fastcall More_InformationClick(TObject *Sender);
	void __fastcall FunctionButtonMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
	void __fastcall FunctionButtonMouseUp(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
    void __fastcall CopyScriptNametoClipboard1Click(TObject *Sender);
    void __fastcall PasteScriptNamefromClipboad1Click(TObject *Sender);
    void __fastcall ViewScriptDirClick(TObject *Sender);
    void __fastcall FindDialog1Close(TObject *Sender);
    
protected:

    void __fastcall WMDropFiles(TWMDropFiles &message);
    void __fastcall CMProgCtrlStop(TMessage &Message);
    void __fastcall CMReenable(TMessage &Message);
	void __fastcall WMEnable(TWMEnable &Message);
    void __fastcall CMPCThreadTerm(TMessage &Message);

	BEGIN_MESSAGE_MAP
	    MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDropFiles)
    	MESSAGE_HANDLER(CM_PROGCTRLSTOP, TMessage, CMProgCtrlStop)
        MESSAGE_HANDLER(CM_REENABLE, TMessage, CMReenable)
		MESSAGE_HANDLER(WM_ENABLE, TWMEnable, WMEnable)
		MESSAGE_HANDLER(CM_PCTHREADTERM, TMessage, CMPCThreadTerm)
	END_MESSAGE_MAP(TForm)

private:	// User declarations
	__fastcall TProgram_Control(TComponent* Owner);

	int MyRemoteNo, MyTIUNo, MyEngineNo, MyPCNo;
    bool LoopProgram, PauseProgram;

    bool LocalTransparencyOverride;
    void __fastcall StopPCThread(TObject *Sender);
    String RegFileRootKey;
	void __fastcall ExtractAll_Structure(TWinControl *container, int &indent);
    int RecallIndex;
    bool LuaCommandChange;
    int __fastcall MyFindText(String SearchStr, long StartPos, long FindLength, TSearchTypes Options, bool SearchDown);
    TPoint TopLeftPos;

public:		// User declarations

	__fastcall TProgram_Control(TComponent* Owner, int Engine, int Remote, int TIU, int PCNo);
    __fastcall ~TProgram_Control(void);
    HWND ParentHandle;	// accessed by Train_Control.cpp

   	HANDLE FunctionPCEvent;			// for event communications with Program Control Thread to run Functionxx()
	HANDLE LoopPCEvent;				// for event communications with Program Control Thread to run loop()
	HANDLE SetupPCEvent;			// for event communications with Program Control Thread to run setup()
	HANDLE CleanupPCEvent;			// for event communications with Program Control Thread
	HANDLE EventPCEvent;			// for event communications with Program Control Thread
	HANDLE TerminationPCEvent;		// for event communications with Program Control Thread
	HANDLE InterpreterPCEvent;		// for event communications with Program Control Thread
	HANDLE InputPCEvent;		    // for event communications with Program Control Thread to process typed input

    TProg_Control_Thread * ptrPC_Thread;	// pointer to PC Thread, thread set to free on terminate
    
	enum PROGRAMNO PCMode;		// indicates which mode that the PC Thread is running in

    // the Cab Signals
    void __fastcall SetCabSignal(int HeadNo, int Aspect, String sName, int iNumberPlate, int iType);
    void __fastcall GetCabAspect(int HeadNo, int &Aspect, int &iMode, int &Type);
    void __fastcall CreateCabSignals(int HomeHeadType, int DistantHeadType);

    // Cab Signals
    TPanel * HomeSignal;	// Home Signal
    TPanel * DistantSignal;	// AKA Approach Signal

};
//---------------------------------------------------------------------------
extern PACKAGE TProgram_Control *Program_Control;
//---------------------------------------------------------------------------
#endif
