#ifndef PhCnt_MainH
#define PhCnt_MainH

#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <ToolWin.hpp>

#include <vcl\Registry.hpp>
#include <Buttons.hpp>
#include <ActnList.hpp>
#include <ImgList.hpp>
#include <Dialogs.hpp>
#include <Grids.hpp>

#include "ThemeMgr.hpp"
#include "WakePort.h"
#include "FloatEdit.hpp"

#define SOFTNAME "Reel"
#define VERSION 1.25

#define LOGN   18001 //максимальное количество точек графика
#define ROUND(x) ((x > 0)? (x + 0.5) : (x - 0.5))
#define GRAPHS 6

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

class TMainForm : public TForm
{
__published:	// IDE-managed Components
  TMainMenu *MainMenu;
  TMenuItem *mnFile;
  TMenuItem *mnNew;
  TMenuItem *mnOpen;
  TMenuItem *mnSaveAs;
  TMenuItem *mnSep1;
  TMenuItem *mnExit;
  TMenuItem *mnDevice;
  TMenuItem *mnStart;
  TMenuItem *mnPause;
  TMenuItem *mnStop;
  TMenuItem *mnView;
  TMenuItem *mnCursor;
  TMenuItem *mnSep3;
  TMenuItem *mnColours;
  TMenuItem *mnHelp;
  TMenuItem *mnAbout;
	TActionList *ActionList1;
	TAction *acNew;
  TAction *acOpen;
	TAction *acSaveas;
  TAction *acExit;
	TAction *acStart;
  TAction *acPause;
	TAction *acStop;
	TAction *acCursor;
  TAction *acColours;
  TAction *acAbout;
  TToolBar *ToolBar;
	TToolButton *tbNew;
  TToolButton *tbOpen;
	TToolButton *tbSaveAs;
  TToolButton *tbSep1;
	TToolButton *tbStart;
  TToolButton *tbPause;
	TToolButton *tbStop;
  TToolButton *tbSep2;
	TToolButton *tbCursor;
  TToolButton *tbSep4;
  TToolButton *tbColours;
	TImageList *ImageList1;
  TOpenDialog *OpenDialog;
	TSaveDialog *SaveDialog;
	TTimer *Timer1;
  TStatusBar *StatusBar;
  TAction *acSettings;
  TToolButton *ToolButton2;
  TToolButton *ToolButton3;
  TMenuItem *N1;
  TMenuItem *Settings1;
  TPanel *CtrlPanel;
  TPanel *UnderLogPanel;
  TScrollBar *LogScrollBar;
  TAction *acConnect;
  TAction *acDisconnect;
  TMenuItem *Connect1;
  TMenuItem *Disconnect1;
  TMenuItem *N2;
  TToolButton *ToolButton1;
  TToolButton *ToolButton4;
  TToolButton *ToolButton5;
  TPanel *TrsPanel;
  TLabel *lbTraces;
  TCheckBox *cbGr1;
  TLabel *lbGr1;
  TCheckBox *cbGr2;
  TLabel *lbGr2;
  TCheckBox *cbGr3;
  TLabel *lbGr3;
  TCheckBox *cbGr4;
  TLabel *lbGr4;
  TCheckBox *cbGr5;
  TLabel *lbGr5;
  TAction *acRead;
  TAction *acWrite;
  TToolButton *ToolButton6;
  TToolButton *ToolButton7;
  TMenuItem *ReadParams1;
  TMenuItem *WriteParams1;
  TMenuItem *N3;
  TToolButton *ToolButton8;
  TThemeManager *ThemeManager1;
  TPanel *LogPanel;
  TPaintBox *LogPaintBox;
  TAction *acCutL;
  TAction *acCutR;
  TMenuItem *CutLeft1;
  TMenuItem *CutRight1;
  TMenuItem *N6;
  TAction *acFreeze;
  TToolButton *ToolButton9;
  TMenuItem *Freeze1;
  TScrollBar *sbScale;
  TScrollBar *sbShift;
  TComboBox *cbGrs;
  TLabel *Label9;
  TFloatEdit *feScale;
  TLabel *Label10;
  TFloatEdit *feShift;
  TButton *btRes;
  TAction *acTest;
  TMenuItem *N4;
  TMenuItem *TestData1;
  TLabel *Label21;
  TLabel *Label22;
  TCheckBox *cbGr6;
  TLabel *lbGr6;
  TAction *acParOpen;
  TAction *acParSave;
  TMenuItem *OpenPrams1;
  TMenuItem *SaveParams1;
  TMenuItem *N5;
  TPanel *TlPanel;
  TGroupBox *GroupBox1;
  TLabel *Label37;
  TLabel *Label38;
  TLabel *Label39;
  TLabel *Label40;
  TLabel *Label49;
  TLabel *Label50;
  TCheckBox *cbCapStart;
  TCheckBox *cbCapReverse;
  TCheckBox *cbCapReady;
  TFloatEdit *feFref;
  TFloatEdit *feSleep;
  TComboBox *cbSpeed;
  TFloatEdit *feCapStart;
  TGroupBox *GroupBox2;
  TLabel *Label1;
  TLabel *Label7;
  TLabel *Label2;
  TLabel *lbTen1;
  TLabel *lbTen6;
  TLabel *Label13;
  TLabel *Label6;
  TLabel *Label14;
  TLabel *Label15;
  TLabel *Label16;
  TLabel *Label17;
  TLabel *lbTen3;
  TLabel *Label19;
  TLabel *Label20;
  TLabel *Label5;
  TLabel *lbTen7;
  TLabel *Label25;
  TLabel *Label26;
  TLabel *Label28;
  TLabel *Label29;
  TLabel *Label30;
  TLabel *Label33;
  TLabel *Label34;
  TLabel *Label35;
  TLabel *Label36;
  TLabel *lbTen4;
  TLabel *lbTen5;
  TLabel *Label43;
  TLabel *Label44;
  TLabel *Label45;
  TLabel *Label46;
  TLabel *lbTen2;
  TLabel *Label55;
  TLabel *Label56;
  TLabel *Label31;
  TLabel *Label24;
  TFloatEdit *feP1;
  TFloatEdit *feI1;
  TFloatEdit *feD1;
  TFloatEdit *feTbrake1;
  TFloatEdit *feTbrake2;
  TFloatEdit *feMot1;
  TFloatEdit *feMot2;
  TFloatEdit *feT1get;
  TFloatEdit *feT2get;
  TComboBox *cbSpool;
  TFloatEdit *feTplayR1;
  TFloatEdit *feTffd1;
  TFloatEdit *feTarew1;
  TFloatEdit *feTplayR2;
  TFloatEdit *feTffd2;
  TFloatEdit *feTarew2;
  TFloatEdit *feTmin2;
  TFloatEdit *feTrew1;
  TFloatEdit *feTaffd1;
  TFloatEdit *feTrew2;
  TFloatEdit *feTaffd2;
  TFloatEdit *feTplayF1;
  TFloatEdit *feTplayF2;
  TFloatEdit *feTmin1;
  TGroupBox *gbControl;
  TSpeedButton *sbPlayF;
  TSpeedButton *sbPlayR;
  TSpeedButton *sbPause;
  TSpeedButton *sbRec;
  TSpeedButton *sbRew;
  TSpeedButton *sbFfd;
  TSpeedButton *sbStop;
  TShape *shRew;
  TShape *shStop;
  TShape *shFfd;
  TShape *shRec;
  TShape *shPause;
  TShape *shPlayR;
  TShape *shPlayF;
  TSpeedButton *sbRollBack;
  TGroupBox *GroupBox4;
  TLabel *Label47;
  TLabel *Label48;
  TCheckBox *cbBreak;
  TCheckBox *cbPress;
  TCheckBox *cbLift;
  TFloatEdit *feEmForce;
  TGroupBox *GroupBox5;
  TCheckBox *cbMove;
  TCheckBox *cbSlow;
  TGroupBox *GroupBox6;
  TLabel *Label57;
  TLabel *Label58;
  TLabel *Label59;
  TLabel *Label61;
  TLabel *Label62;
  TLabel *Label63;
  TCheckBox *cbAsLowten;
  TCheckBox *cbAsBrake;
  TCheckBox *cbAsMove;
  TFloatEdit *feAsBrkDel;
  TFloatEdit *feAsPreDel;
  TFloatEdit *feAsTenDel;
  TCheckBox *cbAsPreten;
  TGroupBox *GroupBox7;
  TCheckBox *cbAudioMute;
  TCheckBox *cbAudioReverse;
  TCheckBox *cbAudioRecord;
  TGroupBox *GroupBox8;
  TCheckBox *cbOpPauseBlink;
  TCheckBox *cbOpRecOnPause;
  TCheckBox *cbOpShowDir;
  TCheckBox *cbOpArchive;
  TCheckBox *cbOpRollCue;
  TCheckBox *cbOpWindCue;
  TCheckBox *cbOpNoSound;
  TCheckBox *cbOpAutoReverse;
  TCheckBox *cbOpProg;
  TCheckBox *cbOpProgBlink;
  TGroupBox *GroupBox9;
  TLabel *Label4;
  TLabel *Label3;
  TFloatEdit *feTau;
  TCheckBox *cbTape;
  TBitBtn *BitBtn2;
  TBitBtn *BitBtn1;
  TAction *acEeprom;
  TAction *acDefault;
  TMenuItem *SavetoEeprom1;
  TMenuItem *LoadDefaults1;
  TMenuItem *N7;
  TBitBtn *BitBtn3;
  TBitBtn *BitBtn4;
  TFloatEdit *feP2;
  TFloatEdit *feI2;
  TFloatEdit *feD2;
  TCheckBox *cbOpMotorBrk;
  TCheckBox *cbOpMutePause;
  TCheckBox *cbOpPlayRecEn;
  TCheckBox *cbOpRecPlayEn;
  TCheckBox *cbOpPlayExPause;
  TGroupBox *GroupBox3;
  TLabel *Label8;
  TEdit *edSys;
  TBitBtn *BitBtn5;
  TLabel *Label11;
	void __fastcall FormCreate(TObject *Sender);
	void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
	void __fastcall acNewExecute(TObject *Sender);
  void __fastcall acOpenExecute(TObject *Sender);
	void __fastcall acSaveasExecute(TObject *Sender);
  void __fastcall acExitExecute(TObject *Sender);
	void __fastcall acStartExecute(TObject *Sender);
  void __fastcall acPauseExecute(TObject *Sender);
	void __fastcall acStopExecute(TObject *Sender);
	void __fastcall acCursorExecute(TObject *Sender);
  void __fastcall acColoursExecute(TObject *Sender);
  void __fastcall acAboutExecute(TObject *Sender);
  void __fastcall acSettingsExecute(TObject *Sender);
  void __fastcall acConnectExecute(TObject *Sender);
  void __fastcall acDisconnectExecute(TObject *Sender);
  void __fastcall acReadExecute(TObject *Sender);
  void __fastcall acWriteExecute(TObject *Sender);
  void __fastcall acCutLExecute(TObject *Sender);
  void __fastcall acCutRExecute(TObject *Sender);
  void __fastcall acFreezeExecute(TObject *Sender);
  void __fastcall LogPaintBoxMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
  void __fastcall LogPaintBoxMouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y);
  void __fastcall LogPaintBoxPaint(TObject *Sender);
	void __fastcall Timer1Timer(TObject *Sender);
  void __fastcall LogScrollBarChange(TObject *Sender);
  void __fastcall cbGr1Click(TObject *Sender);
  void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
  void __fastcall feP1Changed(TObject *Sender);
  void __fastcall feScaleChanged(TObject *Sender);
  void __fastcall feShiftChanged(TObject *Sender);
  void __fastcall cbGrsChange(TObject *Sender);
  void __fastcall sbScaleChange(TObject *Sender);
  void __fastcall sbShiftChange(TObject *Sender);
  void __fastcall feMot1Changed(TObject *Sender);
  void __fastcall feTbrake1Changed(TObject *Sender);
  void __fastcall btResClick(TObject *Sender);
  void __fastcall sbStopClick(TObject *Sender);
  void __fastcall cbPressClick(TObject *Sender);
  void __fastcall cbSpoolChange(TObject *Sender);
  void __fastcall cbSlowClick(TObject *Sender);
  void __fastcall cbAudioMuteClick(TObject *Sender);
  void __fastcall acTestExecute(TObject *Sender);
  void __fastcall sbRollBackClick(TObject *Sender);
  void __fastcall cbCapReverseClick(TObject *Sender);
  void __fastcall feCapStartChanged(TObject *Sender);
  void __fastcall acParOpenExecute(TObject *Sender);
  void __fastcall acParSaveExecute(TObject *Sender);
  void __fastcall acEepromExecute(TObject *Sender);
  void __fastcall acDefaultExecute(TObject *Sender);
  void __fastcall BitBtn5Click(TObject *Sender);
private:	// User declarations
  bool Lock;                  //флаг запрещения обработчиков контролов
  AnsiString FPortName;       //имя порта
  AnsiString FDeviceName;     //имя устройства
  AnsiString FDataDir;        //путь для сохранения файлов данных
  AnsiString FFileName;       //имя файла данных
  bool FModified;             //признак изменения данных
  double Data1[LOGN];         //массив входных данных 1
  double Data2[LOGN];         //массив входных данных 2
  double Data3[LOGN];         //массив входных данных 3
  double Data4[LOGN];         //массив входных данных 4
  double Data5[LOGN];         //массив входных данных 5
  double Data6[LOGN];         //массив входных данных 6
  double Scale[GRAPHS];
  int BaseShift[GRAPHS];
  int Shift[GRAPHS];
  int FPointer;               //указатель данных
  int FMode;                  //режим (stop, pause, start)
  enum { MD_STOP, MD_PAUSE, MD_START }; //коды режима графика
  bool FCursor;               //режим курсора (on/off)
  bool FCurOn;                //признак прорисовки курсора
  int FCursorX;               //текущая координата курсора X
  Graphics::TBitmap *Bm;
	void __fastcall WmDropFiles(TWMDropFiles& Message);
	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WmDropFiles)
	END_MESSAGE_MAP(TForm);
  AnsiString __fastcall MakeCaption(void);    //формирование заголовка окна
  void __fastcall Disconnect(void);           //отключение устройства
	void __fastcall ReadConfig(void);           //чтение конфигурации
	void __fastcall SaveConfig(void);           //сохранение конфигурации
  void __fastcall Clear(void);                //очистка данных
  void __fastcall ClearScale(void);           //очистка шкал
  void __fastcall ReadData(AnsiString Name);  //чтение данных
  void __fastcall SaveData(AnsiString Name);  //сохранение данных
  void __fastcall DrawLogGrid(TCanvas *cv);   //прорисовка сетки
  void __fastcall DrawSegment(TCanvas *cv, TColor cl, double *data, //рисование 
                    double range, int shift, int x0, int x, int dy); //сегмента
  void __fastcall DrawLog(TCanvas *cv, int x1, int x2);   //прорисовка графиков
	void __fastcall DrawLogCur(TCanvas *cv, bool e, int x); //прорисовка курсора

  void __fastcall Tagging(void);
  void __fastcall SetLeds(int leds);
  void __fastcall HighlightLabel(int i);
  void __fastcall SetTension(int m);
  void __fastcall GetTension(int m);
  void __fastcall SetParams(void);
  void __fastcall GetParams(void);
  void __fastcall GetState(void);
  state_t State;
  int HexToInt(AnsiString txt);

public:		// User declarations
	__fastcall TMainForm(TComponent* Owner);
  TWakePort *WakePort;        //объект порта
  bool FConnected;            //признак соединения
  int FPortNumber;            //номер порта
  void __fastcall Connect(void);              //подключение устройства
  TColor Col_Bg, Col_Ax, Col_Tx, Col_Cr,
   Col_G1, Col_G2, Col_G3, Col_G4, Col_G5, Col_G6; //цвета
  //bool FConnected;            //флаг соединения с устройством
  bool FAutoConnect;          //флаг автоматического подключения
  bool FAutoLoad;             //флаг автоматической загрузки параметров
  int FSample;                //период опроса
  bool FAutoScroll;           //признак автоматического скроллирования
  void __fastcall SetDefColours(void); //установка цветов по умолчанию
  void __fastcall RedrawAll(void);     //перерисовка всего
};

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

extern PACKAGE TMainForm *MainForm;

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

#endif
