{$DEFINE USE_ADO}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtDlgs, ExtCtrls, sgConsts,
  CADImage, DXF, DWG, DWF, HPGL2, SVG, CGM,  sgDrawing4,  frxDrawingView,
  frxClass, frxDesgn, sgDrawingNavigator, DB, DBTables,
  DBCtrls, frxDBSet, Mask
  {$IFDEF USE_ADO}
  ,ADODB
  {$ENDIF}
  ;

type
  TForm1 = class(TForm)
    OD1: TOpenPictureDialog;
    Label1: TLabel;
    dsDrawings: TDataSource;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    DBNavigator1: TDBNavigator;
    dbedtFileName: TDBEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure tblDrawingsAfterScroll(DataSet: TDataSet);
  private
    FLoading: Boolean;
    FPrepareReport: Boolean;
    FDN: TsgDrawingNavigator;
    FfrxReport: TfrxReport;
    FfrxDesigner: TfrxDesigner;
    FsgDrawingView: TsgDrawing4;
    FfrxDBDataset: TfrxDBDataset;
  {$IFDEF USE_ADO}
    FTable: TADOTable;
  {$ELSE}
    FTable: TTable;
  {$ENDIF}
  public
    property frxReport: TfrxReport read FfrxReport;
    property frxDesigner: TfrxDesigner read FfrxDesigner;
    property sgDrawingView: TsgDrawing4 read FsgDrawingView;
    property frxDBDataset: TfrxDBDataset read FfrxDBDataset;
  end;

var
  Form1: TForm1;

implementation

{$IFDEF DEMO}
uses
  fInfo;
{$ENDIF}

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  MS: TMemoryStream;
begin
  if od1.Execute then
  begin
    FLoading := True;
    try
      MS := TMemoryStream.Create;
      try
        MS.LoadFromFile(od1.FileName);
        MS.Position := 0;
        if FTable.IsEmpty then
          FTable.Insert;
        if (FTable.State <> dsEdit) or (FTable.State <> dsInsert) then
          FTable.Insert;
        FTable.FieldByName('FILE_NAME').AsString := ExtractFileName(od1.FileName);
        (FTable.FieldByName('DATA') as TBlobField).LoadFromStream(MS);
        FTable.Post;
      finally
        MS.Free;
      end;
    finally
      FLoading := False;
      FTable.AfterScroll(FTable);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  {$IFDEF USE_ADO}
  FTable := TADOTable.Create(Self);
  FTable.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=Drawings.mdb;';
  {$ELSE}
  FTable := TTable.Create(Self);
  FTable.TableType := ttParadox;
  {$ENDIF}
  FTable.TableName := 'Drawings';
  dsDrawings.DataSet := FTable;
  FTable.AfterScroll := tblDrawingsAfterScroll;
  KeepOriginal := True;
  FDN := TsgDrawingNavigator.Create(Self);
  FDN.Parent := Self;
  FDN.SetBounds(0, 0, Width - 8, dbedtFileName.Top - 8);
  FDN.Color := clWhite;
  FDN.Align := alClient;
  OD1.Filter := GraphicFilter(TGraphic);
  FfrxDBDataset := TfrxDBDataset.Create(Self);
  FfrxDBDataset.UserName := 'frxDBDataset';
  FfrxDBDataset.DataSource := dsDrawings;
  FsgDrawingView := TsgDrawing4.Create(Self);
  FfrxReport := TfrxReport.Create(Self);
  FfrxReport.DataSet := FfrxDBDataset;
  FfrxReport.DataSetName := 'frxDBDataset';
  FfrxDesigner := TfrxDesigner.Create(Self);
  frxReport.LoadFromFile('TestR.fr3');
  FTable.Open;
//  try
//    FTable.Open;
//  except
//    FTable.CreateTable;
//  end;
  Caption := 'FastReport ' +  FfrxReport.Version;
{$IFDEF DEMO}
  fInfo.CreateInfoFrame(FDN.Handle, Point(1, 1));
{$ENDIF}
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  frxReport.DesignReport;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  FPrepareReport := True;
  try
    if frxReport.PrepareReport then
      frxReport.ShowReport;
  finally
    FPrepareReport := False;
    FTable.AfterScroll(FTable);
  end;
end;

procedure TForm1.tblDrawingsAfterScroll(DataSet: TDataSet);
var
  vBS: TMemoryStream;
  vGr: TGraphic;
  vGrClass: TGraphicClass;
  vPicView: TfrxPictureView;

  function DoLoad(S: TStream; AGraphicClass: TGraphicClass): TGraphic;
  begin
    Result := AGraphicClass.Create;
    Result.LoadFromStream(S);
  end;

begin
  if not (FLoading or FPrepareReport) then
  begin
    vBS := TMemoryStream.Create;
    try
      vGr := nil;
      try
        if FTable.FieldByName('DATA') is TBlobField then
          TBlobField(FTable.FieldByName('DATA')).SaveToStream(vBS);
        vBS.Position := 0;
        if vBS.Size > 0 then
        begin
          vGrClass := TGraphicClass(GetCADClassByContent(vBS));
          if Assigned(vGrClass) then
          begin
            vGr := vGrClass.Create;
            vGr.LoadFromStream(vBS);
          end;
          if Assigned(vGr) then
            FDN.Picture.Graphic := vGr
          else
          begin
            vPicView := TfrxPictureView.Create(Self);
            try
              vPicView.LoadPictureFromStream(vBS);
              FDN.Picture := vPicView.Picture;
            finally
              vPicView.Free;
            end;
          end;
        end
        else
          FDN.Picture.Graphic := nil;
      finally
        vGr.Free;
      end;
    finally
      vBS.Free;
    end;
  end;
end;

end.
