unit BaseForm;

interface
{$I AnyLib.Inc}
uses
  Windows, Messages, SysUtils,{$IFDEF Delphi6} Variants,{$ENDIF} Classes,
  Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, AnyPrint, ComCtrls, AnyGrids,AnyConsts;

type
  TfrmBase = class(TForm)
    pnlToolBar1: TPanel;
    Image1: TImage;
    btnImport: TSpeedButton;
    btnExport: TSpeedButton;
    btnPrint: TSpeedButton;
    btnExit: TSpeedButton;
    Report: TAnyPrint;
    ExportDialog: TSaveDialog;
    ImportDialog: TOpenDialog;
    AnyGrid: TAnyGrid;
    ProgressBar: TProgressBar;
    btnSave: TSpeedButton;
    SaveDialog: TSaveDialog;
    procedure btnImportClick(Sender: TObject);
    procedure btnExportClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnPrintClick(Sender: TObject);
    procedure AnyGridResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    FLayoutSection: string;
    FLayoutKey: string;
    AppPath:string;
    { Private declarations }
  protected
    procedure InitReport(Report:TAnyPrint);virtual;
  public
    property LayoutSection:string Read  FLayoutSection write FLayoutSection;
  end;

var
  frmBase: TfrmBase;

implementation

{$R *.dfm}
function SameText(const S1, S2: string): Boolean; assembler;
asm
        CMP     EAX,EDX
        JZ      @1
        OR      EAX,EAX
        JZ      @2
        OR      EDX,EDX
        JZ      @3
        MOV     ECX,[EAX-4]
        CMP     ECX,[EDX-4]
        JNE     @3
        CALL    CompareText
        TEST    EAX,EAX
        JNZ     @3
@1:     MOV     AL,1
@2:     RET
@3:     XOR     EAX,EAX
end;
function Max(A1, A2: integer): integer;
begin
  Result := A1;
  if A2 > A1 then
    Result := A2;
end;
procedure TfrmBase.btnImportClick(Sender: TObject);
begin
  if not ImportDialog.Execute then Exit;
  ProgressBar.Visible:=True;
  try
    AnyGrid.ImportFromExcel(ImportDialog.FileName,0,0,-1,-1,0,ProgressBar);
  finally
    ProgressBar.Position:=0;
    ProgressBar.Visible:=False;
  end;
end;
function MessageBox(Text, Caption: PChar; Flags: Longint): Integer;
begin
  Result:=Application.MessageBox(Text,Caption,Flags);
end;
procedure TfrmBase.btnExportClick(Sender: TObject);
var
  FileName:string;
begin
  if not ExportDialog.Execute then Exit;
  FileName:=ExportDialog.FileName;
  if (FileName<>'') then
  begin
    if not ((Pos('.',FileName)>0) and (FileName[Length(FileName)]<>'.')) then
      FileName:=FileName+'.xls';
    if FileExists(FileName) then
    begin
      if MessageBox(PChar(Format(SFileAlreadyExist,[FileName])),PChar(SErrorCaption),MB_YESNO+MB_ICONWARNING)=IDYES then
        AnyGrid.ExportToExcel(FileName,ProgressBar);
    end
    else
      AnyGrid.ExportToExcel(FileName,ProgressBar);
  end;
end;

procedure TfrmBase.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmBase.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //Save Layout
  //if (FLayoutKey<>'') and (FLayoutSection<>'') then
  //  AnyGrid.SaveLayout(FLayoutKey,FLayoutSection,slIniFile);
  Action:=caFree;
end;

procedure TfrmBase.InitReport(Report: TAnyPrint);
begin
  Report.TitleRows:=AnyGrid.FixRowCount;
  Report.PageHeader.Text:='AnyLib Demo';
  {$IFDEF Warning}
  Report.PageHeader.LeftText:=DateToStr(Date);
  {$ELSE}
  Report.PageHeader.LeftText:=DateToStr(Date)+#13#10'http://www.anylib.com';
  {$ENDIF}
  Report.PageFooter.RightText:='[PAGE#] of [TOTALPAGES]';//Display PageNo at RightBottom
end;

procedure TfrmBase.btnPrintClick(Sender: TObject);
begin
  InitReport(Report);
  Report.ShowReport;
end;

procedure TfrmBase.AnyGridResize(Sender: TObject);
begin
  if AnyGrid.GridType=gtInvoice then
    //Auto center
    AnyGrid.LeftSpace:=Max(10,(AnyGrid.Width-AnyGrid.InnerWidth-
      AnyGrid.LeftMargin-AnyGrid.RightMargin) div 2);
end;

procedure TfrmBase.FormCreate(Sender: TObject);
begin
  WindowState:=wsMaximized;
  ImportDialog.InitialDir:=ExtractFilePath(Application.ExeName);
  ExportDialog.InitialDir:=ImportDialog.InitialDir;
  AppPath:=ExtractFilePath(Application.ExeName);
  FLayoutKey:=AppPath+'AnyLibLayout.ini';
  FLayoutSection:='';
end;

procedure TfrmBase.FormShow(Sender: TObject);
begin
  //Load Layout
  //if (FLayoutKey<>'') and (FLayoutSection<>'') then
  //  AnyGrid.loadLayout(FLayoutKey,FLayoutSection,slIniFile)
end;

procedure TfrmBase.btnSaveClick(Sender: TObject);
var
  FileName:string;
begin
  if not SaveDialog.Execute then Exit;
  FileName:=SaveDialog.FileName;
  if (FileName<>'') then
  begin
    if not ((Pos('.',FileName)>0) and (FileName[Length(FileName)]<>'.')) then
      FileName:=FileName+'.ald';
    if FileExists(FileName) then
    begin
      if MessageBox(PChar(Format(SFileAlreadyExist,[FileName])),PChar(SErrorCaption),MB_YESNO+MB_ICONWARNING)=IDYES then
        AnyGrid.SaveToFile(FileName);
    end
    else
      AnyGrid.SaveToFile(FileName);
  end;
end;

end.
