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

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
#include "DataModule.h"
#include "ProcessThread.h"
#include "SaveToXlsThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PC_MainDrawTab(TCustomTabControl *Control,
      int TabIndex, const TRect &Rect, bool Active)
{
    TCanvas *PCCanvas = Control->Canvas;
    TRect MyRect;
    AnsiString AppTitle;

     if(Active == true) {
        PCCanvas->Brush->Color = clBlack;
        PCCanvas->Font->Color = clWindow;
        PCCanvas->Font->Style = PCCanvas->Font->Style << fsBold;
        MyRect.bottom = Rect.Bottom;
     }else {
            PCCanvas->Brush->Color = clAppWorkSpace;
            PCCanvas->Font->Color = clBlack;
            PCCanvas->Font->Style = PCCanvas->Font->Style >> fsBold;
            MyRect.bottom = Rect.Bottom + 2;
     }

     TPageControl *MyPC;
     if(Control->Name == "PC_Main")
     {
        switch(TabIndex){
          case 0: AppTitle =  TabSheet1->Caption; break;
          case 1: AppTitle =  TabSheet2->Caption; break;
          case 2: AppTitle =  TabSheet3->Caption; break;
          case 3: AppTitle =  TabSheet4->Caption; break;
        }

        Form1->Caption = App_Caption + AppTitle;
        MyPC = PC_Main;
        MyRect.left = Rect.left;
        MyRect.top = Rect.top;
        MyRect.right = Rect.right;
        //MyRect.bottom = Rect.Bottom + 2;
      }else if(Control->Name == "PC_F1") MyPC = PC_F1, MyRect = Rect;
       else if(Control->Name == "PC_F2") MyPC = PC_F2, MyRect = Rect;
       else if(Control->Name == "PC_F3") MyPC = PC_F3, MyRect = Rect;
       else if(Control->Name == "PC_F4") MyPC = PC_F4, MyRect = Rect;

     PCCanvas->FillRect(MyRect);

     AnsiString text(MyPC->Pages[TabIndex]->Caption);
     RECT R = static_cast<RECT>(Rect);
     DrawText(PCCanvas->Handle, text.c_str(), text.Length(),&R, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_ClearClick(TObject *Sender)
{
    DModule->F1_Timer->Enabled = false;
    DModule->F1_DataCount = 0;
    DModule->F1_Timer->Tag = 0;
    F1_ListView->Clear();
    Form1->F1_Chart->Series[0]->Clear();
    Form1->F1_Chart->Series[1]->Clear();
    Form1->F1_Chart->Series[2]->Clear();
    Form1->F1_Chart->Series[3]->Clear();
    Form1->F1_Chart->Series[4]->Clear();
    Form1->F1_Chart->Series[5]->Clear();
    Form1->F1_Chart->Series[6]->Clear();
    Form1->F1_Chart->Series[7]->Clear();
    Form1->F1_Chart->Series[8]->Clear();
    DModule->F1_Timer->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_ListViewChange(TObject *Sender, TListItem *Item,
      TItemChange Change)
{
  TListView *MyLV = (TListView*)Sender;
  TUpDown *MyUD;

  switch(MyLV->Tag){
    case 1:
           MyUD = F1_UpDown;
           break;
    case 2:
           MyUD = F2_UpDown;
           break;
    case 3:
           MyUD = F3_UpDown;
           break;
    case 4:
           MyUD = F4_UpDown;
           break;
  }

  if(!MyUD->Position)
      SendMessage(MyLV->Handle, WM_VSCROLL, SB_BOTTOM, 0 );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
   App_Dir = GetCurrentDir();
   App_Caption = Caption;

   CreateDir(App_Dir+"\\temp");
   CreateDir(App_Dir+"\\AutoSave");

  
   ThClose = false;
   F1_ExcelSaveThread = NULL;
   F2_ExcelSaveThread = NULL;
   F3_ExcelSaveThread = NULL;
   F4_ExcelSaveThread = NULL;

   PThread = new ProcessThread(false);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_Save2ExcelClick(TObject *Sender)
{
   TSpeedButton *MySB = (TSpeedButton*)Sender;

   switch(MySB->Tag){
     case 1:
            if(!F1_Autosave_CB->Tag){
              if(DModule->SaveDialog1->Execute()){
                F1_SaveFileName = DModule->SaveDialog1->FileName;
                if(!F1_ExcelSaveThread) F1_ExcelSaveThread = new SaveToXlsThread(false,1);
              }
            } else if(!F1_ExcelSaveThread) F1_ExcelSaveThread = new SaveToXlsThread(false,1);
            break;

     case 2:
            if(!F2_Autosave_CB->Tag){
              if(DModule->SaveDialog1->Execute()) {
                F2_SaveFileName = DModule->SaveDialog1->FileName;
                if(!F2_ExcelSaveThread) F2_ExcelSaveThread = new SaveToXlsThread(false,2);
              }
            } else if(!F2_ExcelSaveThread) F2_ExcelSaveThread = new SaveToXlsThread(false,2);
            break;

     case 3:
            if(!F3_Autosave_CB->Tag){
              if(DModule->SaveDialog1->Execute()) {
                F3_SaveFileName = DModule->SaveDialog1->FileName;
                if(!F3_ExcelSaveThread) F3_ExcelSaveThread = new SaveToXlsThread(false,3);
              }
            } else if(!F3_ExcelSaveThread) F3_ExcelSaveThread = new SaveToXlsThread(false,3);
            break;

     case 4:
            if(!F4_Autosave_CB->Tag){
              if(DModule->SaveDialog1->Execute()) {
                F4_SaveFileName = DModule->SaveDialog1->FileName;
                if(!F4_ExcelSaveThread) F4_ExcelSaveThread = new SaveToXlsThread(false,4);
              }
            } else if(!F4_ExcelSaveThread) F4_ExcelSaveThread = new SaveToXlsThread(false,4);
            break;
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  ThClose = true;        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_CBChange(TObject *Sender)
{
    TComboBox * MyCB = (TComboBox*)Sender;
    TTimer * MyT[] = {DModule->F1_Timer,DModule->F2_Timer,DModule->F3_Timer,DModule->F4_Timer};

    switch(MyCB->ItemIndex){
      case 0:
              MyT[MyCB->Tag-1]->Interval = 10000;
              break;
      case 1:
              MyT[MyCB->Tag-1]->Interval = 30000;
              break;
      case 2:
              MyT[MyCB->Tag-1]->Interval = 60000;
              break;
      case 3:
              MyT[MyCB->Tag-1]->Interval = 300000;
              break;
      case 4:
              MyT[MyCB->Tag-1]->Interval = 600000;
              break;
      case 5:
              MyT[MyCB->Tag-1]->Enabled = false;
              break;
    }
    if(!MyT[MyCB->Tag-1]->Enabled && (MyCB->ItemIndex < 5)) MyT[MyCB->Tag-1]->Enabled = true;
    StatusBar1->Panels->Items[MyCB->Tag+2]->Text = MyCB->Items->Strings[MyCB->ItemIndex];
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_Reset_SBClick(TObject *Sender)
{
   TSpeedButton * MySB = (TSpeedButton*)Sender;

   switch(MySB->Tag){
    case 0:
           DModule->Tss[0] = 0;
           DModule->Tmm[0] = 0;
           DModule->Thh[0] = 0;
           F1_SS_ST->Caption = F1_SS_ST->Caption.sprintf("%02d",DModule->Tss[0]);
           F1_MM_ST->Caption = F1_MM_ST->Caption.sprintf("%02d",DModule->Tmm[0]);
           F1_HH_ST->Caption = F1_HH_ST->Caption.sprintf("%02d",DModule->Thh[0]);
           break;
    case 1:
           DModule->Tss[1] = 0;
           DModule->Tmm[1] = 0;
           DModule->Thh[1] = 0;
           F2_SS_ST->Caption = F2_SS_ST->Caption.sprintf("%02d",DModule->Tss[1]);
           F2_MM_ST->Caption = F2_MM_ST->Caption.sprintf("%02d",DModule->Tmm[1]);
           F2_HH_ST->Caption = F2_HH_ST->Caption.sprintf("%02d",DModule->Thh[1]);
           break;
    case 2:
           DModule->Tss[2] = 0;
           DModule->Tmm[2] = 0;
           DModule->Thh[2] = 0;
           F3_SS_ST->Caption = F3_SS_ST->Caption.sprintf("%02d",DModule->Tss[2]);
           F3_MM_ST->Caption = F3_MM_ST->Caption.sprintf("%02d",DModule->Tmm[2]);
           F3_HH_ST->Caption = F3_HH_ST->Caption.sprintf("%02d",DModule->Thh[2]);
           break;
    case 3:
           DModule->Tss[3] = 0;
           DModule->Tmm[3] = 0;
           DModule->Thh[3] = 0;
           F4_SS_ST->Caption = F4_SS_ST->Caption.sprintf("%02d",DModule->Tss[3]);
           F4_MM_ST->Caption = F4_MM_ST->Caption.sprintf("%02d",DModule->Tmm[3]);
           F4_HH_ST->Caption = F4_HH_ST->Caption.sprintf("%02d",DModule->Thh[3]);
           break;
    }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::F1_Timer_SBClick(TObject *Sender)
{
  if(F1_Timer_SB->Tag == 1){
     F1_Timer_SB->Tag = 0;
     F1_SS_ST->Font->Color = clGreen;
     F1_MM_ST->Font->Color = clGreen;
     F1_HH_ST->Font->Color = clGreen;
     F1_Timer_SB->Font->Color = clActiveCaption;
     F1_Timer_SB->Font->Size = 9;
     if(!F2_Timer_SB->Tag &&  !F3_Timer_SB->Tag && !F4_Timer_SB->Tag)
        DModule->Timer1->Enabled = false;
  }else {
     F1_Timer_SB->Tag = 1;
     F1_SS_ST->Font->Color = clLime;
     F1_MM_ST->Font->Color = clLime;
     F1_HH_ST->Font->Color = clLime;
     F1_Timer_SB->Font->Color = (TColor) RGB(255,255,0);
     F1_Timer_SB->Font->Size = 10;
     DModule->Timer1->Enabled = true;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F2_Timer_SBClick(TObject *Sender)
{
  if(F2_Timer_SB->Tag == 1){
     F2_Timer_SB->Tag = 0;
     F2_SS_ST->Font->Color = clGreen;
     F2_MM_ST->Font->Color = clGreen;
     F2_HH_ST->Font->Color = clGreen;
     F2_Timer_SB->Font->Color = clActiveCaption;
     F2_Timer_SB->Font->Size = 9;
     if(!F1_Timer_SB->Tag &&  !F3_Timer_SB->Tag && !F4_Timer_SB->Tag)
        DModule->Timer1->Enabled = false;
  }else {
     F2_Timer_SB->Tag = 1;
     F2_SS_ST->Font->Color = clLime;
     F2_MM_ST->Font->Color = clLime;
     F2_HH_ST->Font->Color = clLime;
     F2_Timer_SB->Font->Color = (TColor) RGB(255,255,0);
     F2_Timer_SB->Font->Size = 10;
     DModule->Timer1->Enabled = true;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F3_Timer_SBClick(TObject *Sender)
{
  if(F3_Timer_SB->Tag == 1){
     F3_Timer_SB->Tag = 0;
     F3_SS_ST->Font->Color = clGreen;
     F3_MM_ST->Font->Color = clGreen;
     F3_HH_ST->Font->Color = clGreen;
     F3_Timer_SB->Font->Color = clActiveCaption;
     F3_Timer_SB->Font->Size = 9;
     if(!F1_Timer_SB->Tag &&  !F2_Timer_SB->Tag && !F4_Timer_SB->Tag)
        DModule->Timer1->Enabled = false;
  }else {
     F3_Timer_SB->Tag = 1;
     F3_SS_ST->Font->Color = clLime;
     F3_MM_ST->Font->Color = clLime;
     F3_HH_ST->Font->Color = clLime;
     F3_Timer_SB->Font->Color = (TColor) RGB(255,255,0);
     F3_Timer_SB->Font->Size = 10;
     DModule->Timer1->Enabled = true;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F4_Timer_SBClick(TObject *Sender)
{
  if(F4_Timer_SB->Tag == 1){
     F4_Timer_SB->Tag = 0;
     F4_SS_ST->Font->Color = clGreen;
     F4_MM_ST->Font->Color = clGreen;
     F4_HH_ST->Font->Color = clGreen;
     F4_Timer_SB->Font->Size = 9;
     F4_Timer_SB->Font->Color = clActiveCaption;
     if(!F1_Timer_SB->Tag &&  !F2_Timer_SB->Tag && !F3_Timer_SB->Tag)
        DModule->Timer1->Enabled = false;
  }else {
     F4_Timer_SB->Tag = 1;
     F4_SS_ST->Font->Color = clLime;
     F4_MM_ST->Font->Color = clLime;
     F4_HH_ST->Font->Color = clLime;
     F4_Timer_SB->Font->Color = (TColor) RGB(255,255,0);
     F4_Timer_SB->Font->Size = 10;
     DModule->Timer1->Enabled = true;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F2_ClearClick(TObject *Sender)
{
    DModule->F2_Timer->Enabled = false;
    DModule->F2_DataCount = 0;
    DModule->F2_Timer->Tag = 0;
    F2_ListView->Clear();
    Form1->F2_Chart->Series[0]->Clear();
    Form1->F2_Chart->Series[1]->Clear();
    Form1->F2_Chart->Series[2]->Clear();
    Form1->F2_Chart->Series[3]->Clear();
    Form1->F2_Chart->Series[4]->Clear();
    Form1->F2_Chart->Series[5]->Clear();
    Form1->F2_Chart->Series[6]->Clear();
    Form1->F2_Chart->Series[7]->Clear();
    Form1->F2_Chart->Series[8]->Clear();
    DModule->F2_Timer->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F3_ClearClick(TObject *Sender)
{
    DModule->F3_Timer->Enabled = false;
    DModule->F3_DataCount = 0;
    DModule->F3_Timer->Tag = 0;
    F3_ListView->Clear();
    Form1->F3_Chart->Series[0]->Clear();
    Form1->F3_Chart->Series[1]->Clear();
    Form1->F3_Chart->Series[2]->Clear();
    Form1->F3_Chart->Series[3]->Clear();
    Form1->F3_Chart->Series[4]->Clear();
    Form1->F3_Chart->Series[5]->Clear();
    Form1->F3_Chart->Series[6]->Clear();
    Form1->F3_Chart->Series[7]->Clear();
    Form1->F3_Chart->Series[8]->Clear();
    DModule->F3_Timer->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F4_ClearClick(TObject *Sender)
{
    DModule->F4_Timer->Enabled = false;
    DModule->F4_DataCount = 0;
    DModule->F4_Timer->Tag = 0;
    F4_ListView->Clear();
    Form1->F4_Chart->Series[0]->Clear();
    Form1->F4_Chart->Series[1]->Clear();
    Form1->F4_Chart->Series[2]->Clear();
    Form1->F4_Chart->Series[3]->Clear();
    Form1->F4_Chart->Series[4]->Clear();
    Form1->F4_Chart->Series[5]->Clear();
    Form1->F4_Chart->Series[6]->Clear();
    Form1->F4_Chart->Series[7]->Clear();
    Form1->F4_Chart->Series[8]->Clear();
    DModule->F4_Timer->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
  DModule->SaveDialog1->FilterIndex = 1;
  if(DModule->SaveDialog1->Execute())
     F1_Chart->SaveToBitmapFile(DModule->SaveDialog1->FileName+".bmp");
}
//---------------------------------------------------------------------------



