{**********************************************}
{   PrintPreview Form                          }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeePrevi;
{$I TeeDefs.inc}

interface

uses {$IFNDEF LINUX}
     {$IFDEF LCL}
     LCLIntf,
     {$ELSE}
     Windows, Messages,
     {$ENDIF}
     {$ENDIF}
     SysUtils, Classes,

     Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Printers,
     Buttons,

     {$IFDEF MSWINDOWS}
     TeeGDIPlus,
     {$ENDIF}

     {$IFDEF D6}
     Types,
     {$ENDIF}

     TeeProcs, TeCanvas, TeePreviewPanel, TeeNavigator, TeEngine, Chart;

type
  {$IFDEF LCL}
  TPrinterSetupDialog=class(TComponent)
  public
    procedure Execute;
  end;
  {$ENDIF}

  TChartPreview = class(TForm)
    Panel1: TPanel;
    CBPrinters: TComboFlat;
    Label1: TLabel;
    Panel2: TPanel;
    Orientation: TRadioGroup;
    GBMargins: TGroupBox;
    SETopMa: TEdit;
    SELeftMa: TEdit;
    SEBotMa: TEdit;
    SERightMa: TEdit;
    ChangeDetailGroup: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    UDLeftMa: TUpDown;
    UDTopMa: TUpDown;
    UDRightMa: TUpDown;
    UDBotMa: TUpDown;
    TeePreviewPanel1: TTeePreviewPanel;
    Resolution: TTrackBar;
    PanelMargins: TPanel;
    BReset: TButton;
    ShowMargins: TCheckBox;
    PanelOptions: TPanel;
    CBProp: TCheckBox;
    CBBitmap: TCheckBox;
    PanelClose: TPanel;
    BClose: TButton;
    Panel3: TPanel;
    BSetupPrinter: TButton;
    BPrint: TButton;
    CBPrintPanel: TCheckBox;
    Button1: TButton;
    MarginsExpand: TPanel;
    Label4: TLabel;
    TriDown: TImage;
    TriUp: TImage;
    procedure FormShow(Sender: TObject);
    procedure BSetupPrinterClick(Sender: TObject);
    procedure CBPrintersChange(Sender: TObject);
    procedure OrientationClick(Sender: TObject);
    procedure SETopMaChange(Sender: TObject);
    procedure SERightMaChange(Sender: TObject);
    procedure SEBotMaChange(Sender: TObject);
    procedure SELeftMaChange(Sender: TObject);
    procedure ShowMarginsClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BResetClick(Sender: TObject);
    procedure BPrintClick(Sender: TObject);
    procedure BCloseClick(Sender: TObject);
    procedure CBPropClick(Sender: TObject);
    procedure TeePreviewPanel1ChangeMargins(Sender: TObject;
      DisableProportional: Boolean; const NewMargins: TRect);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBBitmapClick(Sender: TObject);
    procedure Panel1Resize(Sender: TObject);
    procedure CBPrintPanelClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Label4Click(Sender: TObject);
    procedure TriDownClick(Sender: TObject);
  private
    { Private declarations }
    ChangingMargins : Boolean;
    ChangingProp    : Boolean;
    OldMargins      : TRect;
    PageNavigator1  : TCustomTeeNavigator;

    procedure Navigator1ButtonClicked(Index: TTeeNavigateBtn);
    Procedure ResetMargins;
    Procedure CheckOrientation;
    procedure ChangeMargin(UpDown:TUpDown; Var APos:Integer; OtherSide:Integer);
  public
    PageNavigatorClass    : TTeePageNavigatorClass;
    
    PrinterSetupDialog1   : TPrinterSetupDialog;

    procedure HideCloseButton;
    Function PreviewPanel : TTeePreviewPanel;
  end;

var
  TeeChangePaperOrientation:Boolean=True;

Procedure TeePreview(AOwner:TComponent; APanel:TCustomTeePanel);

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

uses
  TeePreviewPanelEditor;
  
const
  TeeMsg_PreviewKey='SOFTWARE\Steema Software\TeeChart Pro\PrintPreview'; // Do not localize

Procedure TeePreview(AOwner:TComponent; APanel:TCustomTeePanel);
begin
  with TChartPreview.Create(AOwner) do
  try
    TeePreviewPanel1.Panel:=APanel;
    ShowModal;
  finally
    Free;
    APanel.Repaint;
  end;
end;

{$IFDEF LCL}
procedure TPrinterSetupDialog.Execute;
begin
  raise Exception.Create('Printer setup dialog is not implemented.');
end;
{$ENDIF}

{ TChartPreview }

Procedure TChartPreview.ResetMargins;
begin
  With TeePreviewPanel1 do
  if Assigned(Panel) then
  begin
    if Panel.PrintProportional and (Printer.Printers.Count>0) then
       Panel.PrintMargins:=Panel.CalcProportionalMargins;

    TeePreviewPanel1ChangeMargins(Self,False,Panel.PrintMargins);
    Invalidate;
  end;
end;

procedure TChartPreview.FormShow(Sender: TObject);
var tmpClass: TTeePageNavigatorClass;
begin
  tmpClass:=PageNavigatorClass;
  if Assigned(tmpClass) then
  begin
    PageNavigator1:=tmpClass.Create(Self);
    PageNavigator1.Parent:=Panel2;
    PageNavigator1.Align:=alBottom;
    PageNavigator1.HelpContext:=1488;
    PageNavigator1.OnButtonClicked:=Navigator1ButtonClicked;
  end;

  Screen.Cursor:=crDefault;

  if Tag<>0 then
     TeePreviewPanel1.Panel:=TCustomTeePanel(Tag);

  {$IFDEF LCL}
  if not Assigned(Printer) then
  begin
    EnableControls(False,[BPrint,BSetupPrinter,Orientation]);
    Exit;
  end;
  {$ENDIF}

  CBPrinters.Items:=Printer.Printers;

  if Printer.Printers.Count>0 then
  begin
    CBPrinters.ItemIndex:=Printer.PrinterIndex;

    {$IFNDEF TEEOCX}
    if TeeChangePaperOrientation then
       Printer.Orientation:=poLandscape;
    {$ENDIF}

    CheckOrientation;
  end
  else
  begin
    EnableControls(False,[BPrint,BSetupPrinter,Orientation]);
  end;

  With TeePreviewPanel1 do
  if Assigned(Panel) then
  begin
    CBProp.Checked:=Panel.PrintProportional;
    Resolution.Position:=Panel.PrintResolution+100;
    OldMargins:=Panel.PrintMargins;
    ResetMargins;

    if Assigned(PageNavigator1) then
       PageNavigator1.Panel:=Panel;
  end
  else
  begin
    CBProp.Enabled:=False;
    Resolution.Enabled:=False;
    GBMargins.Enabled:=False;
    BPrint.Enabled:=False;
  end;

  BReset.Enabled:=False;
  CBBitmap.Checked:=TeePreviewPanel1.AsBitmap;
  CBPrintPanel.Checked:=PrintTeePanel;
  
  TeeTranslateControl(Self);
end;

procedure TChartPreview.BSetupPrinterClick(Sender: TObject);
begin
  PrinterSetupDialog1.Execute;

  CBPrinters.Items:=Printer.Printers;
  CBPrinters.ItemIndex:=Printer.PrinterIndex;

  CheckOrientation;
  TeePreviewPanel1.Invalidate;
end;

procedure TChartPreview.CBPrintersChange(Sender: TObject);
begin
  Printer.PrinterIndex:=CBPrinters.ItemIndex;

  CheckOrientation;
  OrientationClick(Self);
end;

procedure TChartPreview.OrientationClick(Sender: TObject);
begin
  Printer.Orientation:=TPrinterOrientation(Orientation.ItemIndex);
  ResetMargins;
  TeePreviewPanel1.Invalidate;
end;

procedure TChartPreview.ChangeMargin(UpDown:TUpDown; Var APos:Integer; OtherSide:Integer);
var tmpR : TRect;
begin
  if Showing then
  begin
    if UpDown.Position+OtherSide<100 then
    begin
      APos:=UpDown.Position;
      
      if not ChangingMargins then
      With TeePreviewPanel1 do
      Begin
        Invalidate;

        tmpR:=Panel.PrintMargins;
        CBProp.Checked:=False;
      end;

      BReset.Enabled:=not EqualRect(tmpR,OldMargins);
    end
    else UpDown.Position:=APos;
  end;
end;

procedure TChartPreview.SETopMaChange(Sender: TObject);
var R: TRect;
begin
  if Showing then
  begin
    R:=TeePreviewPanel1.Panel.PrintMargins;
    ChangeMargin(UDTopMa,R.Top,R.Bottom);
    TeePreviewPanel1.Panel.PrintMargins:=R;
  end;
end;

procedure TChartPreview.SERightMaChange(Sender: TObject);
var R: TRect;
begin
  if Showing then
  begin
    R:=TeePreviewPanel1.Panel.PrintMargins;
    ChangeMargin(UDRightMa,R.Right,R.Left);
    TeePreviewPanel1.Panel.PrintMargins:=R;
  end;
end;

procedure TChartPreview.SEBotMaChange(Sender: TObject);
var R: TRect;
begin
  if Showing then
  begin
    R:=TeePreviewPanel1.Panel.PrintMargins;
    ChangeMargin(UDBotMa,R.Bottom,R.Top);
    TeePreviewPanel1.Panel.PrintMargins:=R;
  end;
end;

procedure TChartPreview.SELeftMaChange(Sender: TObject);
var R: TRect;
begin
  if Showing then
  begin
    R:=TeePreviewPanel1.Panel.PrintMargins;
    ChangeMargin(UDLeftMa,R.Left,R.Right);
    TeePreviewPanel1.Panel.PrintMargins:=R;
  end;
end;

procedure TChartPreview.ShowMarginsClick(Sender: TObject);
begin
  TeePreviewPanel1.Margins.Visible:=ShowMargins.Checked;
end;

procedure TChartPreview.FormCreate(Sender: TObject);
const TeeMsg_PreviewKey_old='PrintPreview';
begin
  ChangingMargins:=True;
  ChangingProp:=False;
  PrinterSetupDialog1:=TPrinterSetupDialog.Create(Self);

  TriDown.Left:=TriUp.Left;
  TriDownClick(Self);

  TeeFixRegistry(TeeMsg_PreviewKey_old, TeeMsg_PreviewKey);
  TeeLoadFormPosition(Self,TeeMsg_PreviewKey,True,True);
end;

procedure TChartPreview.BResetClick(Sender: TObject);
begin
  With TeePreviewPanel1 do
  Begin
    Panel.PrintMargins:=OldMargins;
    TeePreviewPanel1ChangeMargins(Self,False,Panel.PrintMargins);
    Invalidate;
  end;
  
  BReset.Enabled:=False;
end;

Procedure TChartPreview.CheckOrientation;
Begin
  Orientation.ItemIndex:=Ord(Printer.Orientation);
End;

procedure TChartPreview.BPrintClick(Sender: TObject);
begin
  Screen.Cursor:=crHourGlass;
  try
    if Assigned(PageNavigator1) and (PageNavigator1.PageCount>1) then
       PageNavigator1.Print
    else
       TeePreviewPanel1.Print;
  finally
    Screen.Cursor:=crDefault;
  end;
end;

procedure TChartPreview.BCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TChartPreview.CBPropClick(Sender: TObject);
begin
  if not ChangingProp then
  begin
    TeePreviewPanel1.Panel.PrintProportional:=CBProp.Checked;
    ResetMargins;
    BReset.Enabled:=not CBProp.Checked;
  end;
end;

procedure TChartPreview.TeePreviewPanel1ChangeMargins(Sender: TObject;
  DisableProportional: Boolean; const NewMargins: TRect);
begin
  ChangingMargins:=True;
  try
    UDLeftMa.Position :=NewMargins.Left;
    UDRightMa.Position:=NewMargins.Right;
    UDTopMa.Position  :=NewMargins.Top;
    UDBotMa.Position  :=NewMargins.Bottom;

    if DisableProportional then
    begin
      TeePreviewPanel1.Panel.PrintProportional:=False;
      ChangingProp:=True;
      CBProp.Checked:=False;
      ChangingProp:=False;
    end;
  finally
    ChangingMargins:=False;
  end;
end;

procedure TChartPreview.Navigator1ButtonClicked(Index: TTeeNavigateBtn);
begin
  TeePreviewPanel1.Invalidate;
end;

procedure TChartPreview.TrackBar1Change(Sender: TObject);
begin
  With TeePreviewPanel1 do
  Begin
    Panel.PrintResolution:=Resolution.Position-100;
    Invalidate;
  end;
end;

function TChartPreview.PreviewPanel: TTeePreviewPanel;
begin
  result:=TeePreviewPanel1;
end;

procedure TChartPreview.FormDestroy(Sender: TObject);
begin
  PrinterSetupDialog1.Free;
  PageNavigator1.Free;
end;

procedure TChartPreview.CBBitmapClick(Sender: TObject);
begin
  TeePreviewPanel1.AsBitmap:=CBBitmap.Checked;
end;

procedure TChartPreview.HideCloseButton;
begin
  PanelClose.Hide;
end;

procedure TChartPreview.Panel1Resize(Sender: TObject);
begin
  CBPrinters.Width:=Panel3.Left-CBPrinters.Left;
end;

procedure TChartPreview.CBPrintPanelClick(Sender: TObject);
begin
  PrintTeePanel:=CBPrintPanel.Checked;
  TeePreviewPanel1.Invalidate;
end;

procedure TChartPreview.Button1Click(Sender: TObject);
begin
  with TFormPreviewPanelEditor.CreatePanel(Self,TeePreviewPanel1) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

procedure TChartPreview.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  TeeSaveFormPosition(Self,TeeMsg_PreviewKey,True,True);
end;

const
  TeeDefMarginsPanelHeight=168;

procedure TChartPreview.Label4Click(Sender: TObject);
begin
  TriDownClick(Self);
end;

procedure TChartPreview.TriDownClick(Sender: TObject);
begin
  if MarginsExpand.Height=TeeDefMarginsPanelHeight then
  begin
    MarginsExpand.Height:=ShowMargins.Top-2;
    TriDown.Visible:=True;
  end
  else
  begin
    MarginsExpand.Height:=TeeDefMarginsPanelHeight;
    TriDown.Visible:=False;
  end;

  TriUp.Visible:=not TriDown.Visible;
end;

end.
