//---------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

#include <vcl\vcl.h>
#pragma hdrstop

#include <io.h>

#include "BCrack.h"
#include "AcercaDe.h"
#include "Busc1.h"
//---------------------------------------------------------------------------
#pragma link "TB97"
#pragma link "Grids"
#pragma link "CoolForm"
#pragma link "ActivApp"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton972Click(TObject *Sender)
{
  try {
    Table1 -> Prior();
  } catch(...){
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton974Click(TObject *Sender)
{
  try {
    Table1 -> Next();
  } catch(...){
  }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton971Click(TObject *Sender)
{
  Close();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton977Click(TObject *Sender)
{
  AboutBox -> ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Table1BeforeOpen(TDataSet *DataSet)
{
 if ( (access(Table1 -> TableName.c_str(), 0) != 0) )//! ( Table1 -> CheckOpen(0) ) )
 {
	 // Facilitamos el nombre de la nueva tabla y su tipo
	 Table1 -> TableType = ttParadox;

	 // Usamos la propiedad FielDefs para definir
	 // las columnas que contendrá la tabla
     Table1 -> FieldDefs -> Clear();
	 Table1 -> FieldDefs -> Add("indexed", ftAutoInc, 0, false );
	 Table1 -> FieldDefs -> Add("tCrack", ftString, 10, false );
	 Table1 -> FieldDefs -> Add("nCrack", ftString, 50, false );
	 Table1 -> FieldDefs -> Add("Nombre", ftString, 50, false );
	 Table1 -> FieldDefs -> Add("Fecha",  ftDate,    0, false );
	 Table1 -> FieldDefs -> Add("tProg",  ftString, 15, false );

     Table1 -> IndexDefs-> Clear();

     TIndexOptions Prim, MyIndexOptions;
     Prim << ixPrimary << ixUnique;
     MyIndexOptions << ixCaseInsensitive;
	 Table1->IndexDefs->Add("primary", "indexed", Prim);

	 Table1->IndexDefs->Add("itCrack", "tCrack", MyIndexOptions);
	 Table1->IndexDefs->Add("inCrack", "nCrack", MyIndexOptions);
	 Table1->IndexDefs->Add("iNombre", "Nombre", MyIndexOptions);
	 Table1->IndexDefs->Add("iFecha",  "Fecha",  MyIndexOptions);
	 Table1->IndexDefs->Add("itProg",  "tProg",  MyIndexOptions);

	 // Creamos la base...
	 Table1 -> CreateTable();
 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{

  Table1  -> TableName = "BCrack.db";
  TBuscar -> TableName = "BCrack.db";

  Table1 -> Active     = true;
  TBuscar-> Active     = true;

/*
  TDlgBuscProd *DlgBuscProd;
  DlgBuscProd = new  TDlgBuscProd(this);
*/
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton978Click(TObject *Sender)
{
  DlgBuscProd -> ShowModal();

  if ( DlgBuscProd -> ModalResult == mrOk )
  {
     BusqSig -> Enabled = true;
     BusqAnt -> Enabled = true;
     Table1 -> GotoCurrent(TBuscar);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BusqSigClick(TObject *Sender)
{
  if ( TBuscar -> FindNext() == true )
  {
   Table1 -> GotoCurrent(TBuscar);
   BusqAnt -> Enabled = true;
  } else {
   BusqSig -> Enabled = false;
   MessageBox( 0, "Coincidencia no hayada", "¡ Buscar Siguiente !", MB_OK );
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BusqAntClick(TObject *Sender)
{
  if ( TBuscar -> FindPrior() == true )
  {
   Table1 -> GotoCurrent(TBuscar);
   BusqSig -> Enabled = true;
  } else {
   BusqAnt -> Enabled = false;
   MessageBox( 0, "Coincidencia no hayada", "¡ Buscar Anterior !", MB_OK );
  }
}
void __fastcall TForm1::Table1NewRecord(TDataSet *DataSet)
{
  Table1 -> FieldByName( "Fecha" ) -> AsDateTime = TDateTime::CurrentDate();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DataSource1DataChange(TObject *Sender, TField *Field)
{
  char BarraEstado[80];

  // Indicador de FichaActual / Num.Fichas
  if ( Table1 -> RecNo > 0 && Table1 -> RecordCount > 0 )
  {
   sprintf( BarraEstado, "%d / %d", Table1 -> RecNo, Table1 -> RecordCount );
  } else {
   sprintf( BarraEstado, "¿Nuevo? / %d", Table1 -> RecordCount );
  }

  StatusBar1->Panels->Items[1]->Text = BarraEstado;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolbarButton976Click(TObject *Sender)
{
  bool Success;
  // Run crack...
  ActivApp1 -> ExePath = "start ..\\Cracks\\" + Table1 -> FieldByName("nCrack") -> AsString;
  ActivApp1 -> ExecuteApp( &Success );

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



void __fastcall TForm1::FormDestroy(TObject *Sender)
{
/*
  delete DlgBuscProd;
*/  

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

void __fastcall TForm1::Edit1Change(TObject *Sender)
{

      AnsiString BuscarCota;
      BuscarCota = Edit1->Text + "Z";

      TBuscar->FilterOptions = Form1 -> TBuscar->FilterOptions << foCaseInsensitive;

      TBuscar->Filtered = true;
      TBuscar->Filter = "([Nombre] >= '" + Edit1->Text + "') AND [Nombre] < '" + BuscarCota + "'";

try {
  Table1 -> GotoCurrent(TBuscar);
} catch(...)
{
 //do nothing
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1Enter(TObject *Sender)
{
  DBGrid1 -> Options = DBGrid1 -> Options >> dgRowSelect >> dgAlwaysShowSelection  << dgEditing;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1Exit(TObject *Sender)
{
  DBGrid1 -> Options = DBGrid1 -> Options << dgAlwaysShowSelection << dgRowSelect; 
}
//---------------------------------------------------------------------------

