unit uEditorPreview;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ImgList, PngImageList, StdActns, ActnList, SpTBXItem,
  TB2Item, TB2Dock, TB2Toolbar, JvExControls, JvComponent, JvNavigationPane,
  TB2ExtItems,  uViewPreview, frxClass, ComCtrls, frxPreview,
  JvFormAutoSize, JvAppStorage, JvAppRegistryStorage, JvFormPlacement,
  pngimage, frxExportImage, frxExportPDF, frxDCtrl, frxDMPExport,
  frxGradient, frxChBox, frxCross, frxRich, frxOLE, frxBarcode,
  ExtCtrls, JvComponentBase,  JvExComCtrls, JvStatusBar,
  frxExportMail, frxExportXLS, frxExportText, frxExportRTF, uControllerBase,
  uIntegerListUtils, SpTBXEditors, StdCtrls, TntStdCtrls;

type
  IEditorPreview = interface(IEditorBase)
  ['{43934C3E-2776-4F9E-9292-FB0D7DE2E4DA}']
    function GetReport: TfrxReport;
    property Report: TfrxReport read GetReport;

    procedure SetTitle(const Value: string);
    function GetTitle: string;
    property Title: string read GetTitle write SetTitle;

    procedure LoadFromStream(AStream : TStream);
    function ExportToFile : String;
    procedure Print;
    procedure Preview;

    function GetController : IControllerBase;
    procedure SetController (const Value : IControllerBase);
    property Controller : IControllerBase read GetController
      write SetController;

    function GetListaID : TIntegerList;
    procedure SetListaID (const Value : TIntegerList);
    property ListaID : TIntegerList read GetListaID
      write SetListaID;
  end;

  TfEditorPreview = class(TfEditorBase, IEditorPreview)
    TBXToolbar1: TSpTBXToolbar;
    TBXItem33: TSpTBXItem;
    TBXItem34: TSpTBXItem;
    TBXItem39: TSpTBXItem;
    TBXSeparatorItem16: TSpTBXSeparatorItem;
    cbZoom: TSpTBXComboBox;
    PreviewActionList: TActionList;
    PreviewSmallImageList: TPngImageList;
    PreviewLargeImageList: TPngImageList;
    actPrimeraPagina: TAction;
    actUltimaPagina: TAction;
    actPaginaAnterior: TAction;
    actPaginaSiguiente: TAction;
    TBXItem36: TSpTBXItem;
    actZoomIn: TAction;
    actZoomOut: TAction;
    actTodaPagina: TAction;
    actAnchoPagina: TAction;
    TBXSeparatorItem17: TSpTBXSeparatorItem;
    TBXItem37: TSpTBXItem;
    TBXItem40: TSpTBXItem;
    TBXItem41: TSpTBXItem;
    TBXItem42: TSpTBXItem;
    frxReport1: TfrxReport;
    actToolHand: TAction;
    actToolZoom: TAction;
    tbxZoom: TSpTBXItem;
    tbxMano: TSpTBXItem;
    TBXSeparatorItem18: TSpTBXSeparatorItem;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxRichObject1: TfrxRichObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxDialogControls1: TfrxDialogControls;
    frxTIFFExport1: TfrxTIFFExport;
    frxPDFExport1: TfrxPDFExport;
    frxBMPExport1: TfrxBMPExport;
    TBXItem38: TSpTBXItem;
    TBXSeparatorItem19: TSpTBXSeparatorItem;
    TBXItem43: TSpTBXItem;
    TBXItem44: TSpTBXItem;
    TBXItem45: TSpTBXItem;
    TBXItem46: TSpTBXItem;
    TBXItem47: TSpTBXItem;
    TBXSubmenuItem2: TSpTBXSubmenuItem;
    TBXItem48: TSpTBXItem;
    TBXSubmenuItem3: TSpTBXSubmenuItem;
    TBXItem49: TSpTBXItem;
    TBXItem50: TSpTBXItem;
    TBXItem7: TSpTBXItem;
    TBXItem35: TSpTBXItem;
    actMiniaturas: TAction;
    TBXSeparatorItem6: TSpTBXSeparatorItem;
    TBXSeparatorItem14: TSpTBXSeparatorItem;
    TBXItem51: TSpTBXItem;
    TBXItem52: TSpTBXItem;
    actExportarPDF: TAction;
    TBXSubmenuItem8: TSpTBXSubmenuItem;
    TBXItem53: TSpTBXItem;
    TBXSeparatorItem20: TSpTBXSeparatorItem;
    actExportarRTF: TAction;
    frxXLSExport1: TfrxXLSExport;
    frxMailExport1: TfrxMailExport;
    actExportarExcel: TAction;
    actExportarCorreo: TAction;
    frxRTFExport: TfrxRTFExport;
    frxSimpleTextExport1: TfrxSimpleTextExport;
    actExportarTexto: TAction;
    TBXItem54: TSpTBXItem;
    TBXItem55: TSpTBXItem;
    TBXItem56: TSpTBXItem;
    TBXItem57: TSpTBXItem;
    procedure FormShow(Sender: TObject);
    procedure actPrimeraPaginaExecute(Sender: TObject);
    procedure actUltimaPaginaExecute(Sender: TObject);
    procedure actPaginaAnteriorExecute(Sender: TObject);
    procedure actPaginaSiguienteExecute(Sender: TObject);
    procedure actZoomInExecute(Sender: TObject);
    procedure actTodaPaginaExecute(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actAnchoPaginaExecute(Sender: TObject);
    procedure actZoomOutExecute(Sender: TObject);
    procedure cbZoomItemClick(Sender: TObject);
    procedure actToolHandExecute(Sender: TObject);
    procedure actToolZoomExecute(Sender: TObject);
    procedure actPaginaSiguienteUpdate(Sender: TObject);
    procedure actUltimaPaginaUpdate(Sender: TObject);
    procedure actPrimeraPaginaUpdate(Sender: TObject);
    procedure actPaginaAnteriorUpdate(Sender: TObject);
    procedure actMiniaturasExecute(Sender: TObject);
    procedure actExportarPDFExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure actExportarExcelExecute(Sender: TObject);
    procedure actExportarCorreoExecute(Sender: TObject);
    procedure actExportarRTFExecute(Sender: TObject);
    procedure actExportarTextoExecute(Sender: TObject);
  private
    FPreview : TfrViewPreview;
    FTitle : TCaption;
    FController : IControllerBase;
    FListaID : TIntegerList;
    procedure OnPageChanged(Sender: TfrxPreview; PageNo: Integer);
    procedure UpdateZoom;
  protected
    function GetReport: TfrxReport; virtual;
    procedure SetTitle(const Value: string);
    function GetTitle: string;
    procedure PonerTitulos(const ATitulo: String = ''); override;
    function GetController : IControllerBase;
    procedure SetController (const Value : IControllerBase);
    procedure SetListaID (const Value : TIntegerList);
    function GetListaID : TIntegerList;
  public
    constructor Create(AOwner: TComponent); override;
    property ListaID : TIntegerList read GetListaID
      write SetListaID;
    property Controller : IControllerBase read GetController
      write SetController;
    property Title: string read GetTitle write SetTitle;
    property Report: TfrxReport read GetReport;
    procedure Print;
    procedure Preview;
    procedure LoadFromStream(AStream : TStream);
    function ExportToFile : String;
  end;


implementation

uses
  frxRes, frxUtils, frxPrinter, 
  uCustomEditor, uSistemaFunc;

{$R *.dfm}

{ TfEditorBase1 }

function TfEditorPreview.GetController: IControllerBase;
begin
  Result := FController;
end;

function TfEditorPreview.GetListaID: TIntegerList;
begin
  Result := FListaID;
end;

function TfEditorPreview.GetReport: TfrxReport;
begin
  Result := frxReport1;
end;


function TfEditorPreview.GetTitle: string;
begin
  Result := FTitle;
end;

procedure TfEditorPreview.FormShow(Sender: TObject);
begin
  inherited;
  UpdateZoom;
  actPrimeraPagina.Execute;
  FPreview.ShowEmbedded(Self);
  Report.ShowPreparedReport;
  actAnchoPagina.Execute;
end;

procedure TfEditorPreview.actPrimeraPaginaExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.First;
end;

procedure TfEditorPreview.actPrimeraPaginaUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (FPreview.Preview.PageNo > 1);
end;

procedure TfEditorPreview.actUltimaPaginaExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Last;
end;

procedure TfEditorPreview.actUltimaPaginaUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (FPreview.Preview.PageNo < FPreview.Preview.PageCount);
end;

procedure TfEditorPreview.actPaginaAnteriorExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Prior;
end;

procedure TfEditorPreview.actPaginaAnteriorUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (FPreview.Preview.PageNo > 1);
end;

procedure TfEditorPreview.actPaginaSiguienteExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Next;
end;

procedure TfEditorPreview.actPaginaSiguienteUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (FPreview.Preview.PageNo < FPreview.Preview.PageCount);
end;

procedure TfEditorPreview.actZoomInExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Zoom := FPreview.Preview.Zoom + 0.25;
  UpdateZoom;
end;

procedure TfEditorPreview.UpdateZoom;
begin
  cbZoom.Text := IntToStr(Round(FPreview.Preview.Zoom * 100)) + '%';
end;

procedure TfEditorPreview.actTodaPaginaExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.ZoomMode := zmWholePage;
  UpdateZoom;
end;

procedure TfEditorPreview.FormDestroy(Sender: TObject);
begin
  inherited;
  FreeAndNil(FPreview);
end;

procedure TfEditorPreview.FormResize(Sender: TObject);
begin
  inherited;
  UpdateZoom;
end;

procedure TfEditorPreview.actConfPaginaExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.PageSetupDlg;
end;

procedure TfEditorPreview.actExportarCorreoExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Export(frxMailExport1);
end;

procedure TfEditorPreview.actExportarExcelExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Export(frxXLSExport1);
end;

procedure TfEditorPreview.actExportarPDFExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Export(frxPDFExport1);
end;

procedure TfEditorPreview.actExportarRTFExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Export(frxRTFExport);
end;

procedure TfEditorPreview.actExportarTextoExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Export(frxSimpleTextExport1);
end;

procedure TfEditorPreview.actImprimirExecute(Sender: TObject);
begin
  inherited;
  Print;
end;

procedure TfEditorPreview.actMiniaturasExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.ThumbnailVisible := actMiniaturas.Checked;
end;

procedure TfEditorPreview.actAnchoPaginaExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.ZoomMode := zmPageWidth;
  UpdateZoom;
end;

procedure TfEditorPreview.actZoomOutExecute(Sender: TObject);
begin
  inherited;
  FPreview.Preview.Zoom := FPreview.Preview.Zoom - 0.25;
  UpdateZoom;  
end;

procedure TfEditorPreview.cbZoomItemClick(Sender: TObject);
var
  s: String;
begin
  FPreview.Preview.SetFocus;

  if cbZoom.ItemIndex = 6 then
    actAnchoPagina.Execute
  else if cbZoom.ItemIndex = 7 then
    actTodaPagina.Execute
  else
  begin
    s := cbZoom.Text;

    if Pos('%', s) <> 0 then
      s[Pos('%', s)] := ' ';
    while Pos(' ', s) <> 0 do
      Delete(s, Pos(' ', s), 1);

    if s <> '' then
      FPreview.Preview.Zoom := frxStrToFloat(s) / 100;
  end;

  UpdateZoom;
end;

procedure TfEditorPreview.actToolHandExecute(Sender: TObject);
begin
  actToolHand.Checked := True;
  FPreview.Preview.Tool := ptHand
end;

procedure TfEditorPreview.actToolZoomExecute(Sender: TObject);
begin
  actToolZoom.Checked := True;
  FPreview.Preview.Tool := ptZoom;
end;

procedure TfEditorPreview.OnPageChanged(Sender: TfrxPreview;
  PageNo: Integer);
begin
  StatusBar.Panels[0].Text := Format(frxResources.Get('clPageOf'),
    [PageNo, Sender.PageCount]);
end;

constructor TfEditorPreview.Create(AOwner: TComponent);
begin
  inherited;

  with cbZoom.Items do
  begin
    BeginUpdate;
    try
      Clear;
      Add('25%');
      Add('50%');
      Add('75%');
      Add('100%');
      Add('150%');
      Add('200%');
      Add(frxResources.Get('zmPageWidth'));
      Add(frxResources.Get('zmWholePage'));
    finally
      EndUpdate;
    end;
  end;

  FPreview := TfrViewPreview.Create(Self);
  Report.Preview := FPreview.Preview;
  FPreview.Preview.OnPageChanged := OnPageChanged;
end;

procedure TfEditorPreview.PonerTitulos(const ATitulo: String);
var
  AText : String;
begin
  AText := 'Previsualizar';

  if (Length(ATitulo) > 0) then
    AText := AText + ' - ' + ATitulo
  else
    AText := AText + ' - ' + FTitle;

  JvNavPanelHeader.Caption := AText;
  Caption := AText;
end;

procedure TfEditorPreview.Preview;
begin
  Self.ShowModal;
end;

procedure TfEditorPreview.Print;
begin
  if not frxPrinters.HasPhysicalPrinters then
    frxErrorMsg(frxResources.Get('clNoPrinters'))
  else
    FPreview.Preview.Print;
  Enabled := True;
end;

procedure TfEditorPreview.SetController(const Value: IControllerBase);
begin
  FController := Value;
end;

procedure TfEditorPreview.SetListaID(const Value: TIntegerList);
begin
  FListaID := Value;
end;

procedure TfEditorPreview.SetTitle(const Value: string);
begin
  FTitle := Value;
  PonerTitulos(FTitle);
end;

function TfEditorPreview.ExportToFile: String;
var
  AFile : String;
begin
  Result := '';
  AFile := DarFicheroTIFFTemporal;
  frxTIFFExport1.SeparateFiles := False;
  frxTIFFExport1.DefaultPath := ExtractFilePath(AFile);
  frxTIFFExport1.FileName := ExtractFileName(AFile);
  try
    if Report.Export(frxTIFFExport1) then
      Result := AFile;
  finally
    frxTIFFExport1.DefaultPath := '';
    frxTIFFExport1.FileName := '';
  end;
end;

procedure TfEditorPreview.LoadFromStream(AStream: TStream);
begin
  Report.PreviewPages.LoadFromStream(AStream);
end;


initialization
  RegisterClass(TfEditorPreview);

finalization
  UnRegisterClass(TfEditorPreview);

end.

