//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#include "FileManager.h"
#include "ListFile.h"
#include "NextScreen.h"
#include "LastScreen.h"
#include "EndingScreen.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFileScreen *FileScreen;
bool MLBUTTON = false;
String MainDir;
//---------------------------------------------------------------------------
__fastcall TFileScreen::TFileScreen(TComponent* Owner)
	: TForm(Owner)
{
    MainDir = FileScreen->DirectoryListBox1->Directory + "\\";
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::FileScreenExitClick(TObject *Sender)
{
    FileScreen->Close();
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::FileListBox1MouseDown(TObject *Sender,
	TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if (Button != mbLeft)
    {	return; }
    if (MLBUTTON == true)
    {	MLBUTTON = false;
    	return;
    }
    if (FileListBox1->ItemAtPos (Point (X,Y), true) >= 0)
    {	FileListBox1->BeginDrag (false, 1);
    	return;
    }

}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::ListBox1DragOver(TObject *Sender,
	TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
// Only accept a drop from the file list box
    if (Source == FileListBox1)
    {   Accept = true;}
    else
    {	Accept = false;
     	return;
    }
// Don't accept same file twice....
    int limit = NumberOfFiles(ListBox1->Items->Count);
    if (limit == 0) { Accept = true; return;}
    for (int i=0; i < limit; i++)
    {	if (ListBox1->Items->Strings[i] ==
    			FileListBox1->Items->Strings[FileListBox1->ItemIndex])
    	{   Accept = false;
        	return;
        }
    }
	Accept = true;
    return;
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::ListBox1DragDrop(TObject *Sender, TObject *Source,
	int X, int Y)
{
    ListBox1->Items->Add(FileListBox1->Items->Strings[FileListBox1->ItemIndex]);
    FileCounter();
    if (ListBox1->Items->Count > 0)
    { Start->Enabled = true; }
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::ClearLISTClick(TObject *Sender)
{
    TListBox &temp = *ListBox1;
    temp.Clear();
    FileCounter();
    Start->Enabled = false;
}
//*************************************
int TFileScreen::NumberOfFiles(String buff)
{
    int FileNum;
    FileNum = buff.ToInt();
    return FileNum;
}
//*****************************************
void TFileScreen::FileCounter()
{
	Counter->Text = AnsiString("    ") + ListBox1->Items->Count;
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::ClearLASTClick(TObject *Sender)
{
    int last = (NumberOfFiles(ListBox1->Items->Count) - 1);
    ListBox1->Items->Delete(last);
    FileCounter();
    if (last == 0) { Start->Enabled = false;}
}
//-----------------------------------------------------------
void TFileScreen::DisplayFile()
{
 String filename;
 filename = FileListBox1->Items->Strings[FileListBox1->ItemIndex];
 ListFileForm->FileName->Text = filename;
 ListFileForm->OutBox->Lines->LoadFromFile(filename);
 ListFileForm->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::StartClick(TObject *Sender)
{
        // Advance to Analysis Input Screen
        AnalysisInput->Show();
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::FormShow(TObject *Sender)
{
	// . . . . . . .
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::FormMouseUp(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y)
{
    // function clears mouse double-click
    FileCounter();	
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::FileListBox1DblClick(TObject *Sender)
{
    DisplayFile();
    MLBUTTON = true;
}
//---------------------------------------------------------------------------
void __fastcall TFileScreen::Button1Click(TObject *Sender)
{
     int FileLimit = NumberOfFiles(FileListBox1->Items->Count);
     for (int i=0; i < FileLimit; i++)
     {   ListBox1->Items->Strings[i] = FileListBox1->Items->Strings[i]; }
     FileCounter();
     if (ListBox1->Items->Count > 1)
     { Start->Enabled = true; }
}
//---------------------------------------------------------------------------

void __fastcall TFileScreen::FormClose(TObject *Sender,
      TCloseAction &Action)
{
   Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TAnalysisInput::BackClick(TObject *Sender)
{
    AnalysisInput->Close();
}
//---------------------------------------------------------------------------
void __fastcall TProgressScreen::STARTOVERClick(TObject *Sender)
{
    ProgressScreen->Close();
    ReviewScreen->Close();
    AnalysisInput->Close();
}

//---------------------------------------------------------------------------
void __fastcall TProgressScreen::EXITClick(TObject *Sender)
{
    ProgressScreen->Close();
    ReviewScreen->Close();
    AnalysisInput->Close();
    FileScreen->Close();
}
//---------------------------------------------------------------------------



