{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecPreviewFm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ecPrint, ExtCtrls, ComCtrls, ToolWin, Menus, ActnList,
  {$IFNDEF WIN32}System.ComponentModel, Variants,{$ENDIF}
  ImgList, StdCtrls, ecActns;

type
  TSyntPreviewForm = class(TForm)
    ecSyntPreview1: TecSyntPreview;
    ImageList: TImageList;
    ActionList: TActionList;
    FirstCmd: TAction;
    PrevCmd: TAction;
    NextCmd: TAction;
    LastCmd: TAction;
    ZoomCmd: TAction;
    PrintCmd: TAction;
    CloseCmd: TAction;
    PopupMenu1: TPopupMenu;
    WholePage: TMenuItem;
    Pagewidth1: TMenuItem;
    N1: TMenuItem;
    N251: TMenuItem;
    N501: TMenuItem;
    N1001: TMenuItem;
    N2001: TMenuItem;
    N4001: TMenuItem;
    ToolBar1: TToolBar;
    FirstBtn: TToolButton;
    PrevBtn: TToolButton;
    NextBtn: TToolButton;
    LastBtn: TToolButton;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    PrintBtn: TToolButton;
    ToolButton4: TToolButton;
    CloseBtn: TToolButton;
    N751: TMenuItem;
    Edit1: TEdit;
    StaticText1: TStaticText;
    PageSetup: TAction;
    ToolButton2: TToolButton;
    ToolButton6: TToolButton;
    ecPrintAction1: TecPrintAction;
    procedure WholePageClick(Sender: TObject);
    procedure Pagewidth1Click(Sender: TObject);
    procedure N4001Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ActionListUpdate(Action: TBasicAction; var Handled: Boolean);
    procedure FirstCmdExecute(Sender: TObject);
    procedure CloseCmdExecute(Sender: TObject);
    procedure PrintCmdExecute(Sender: TObject);
    procedure ecSyntPreview1PageChanged(Sender: TObject);
    procedure Edit1Exit(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure PageSetupExecute(Sender: TObject);
    procedure PageSetupUpdate(Sender: TObject);
  private
    { Private declarations }
  public

  end;

procedure Preview(APrinter: TecCustomPrinter);

implementation

{$R *.dfm}

procedure Preview(APrinter: TecCustomPrinter);
begin
  with TSyntPreviewForm.Create(nil) do
    begin
     APrinter.UpdatePages;
     ecSyntPreview1.SyntPrinter := APrinter;
     if APrinter.Title <> '' then
       Caption := Caption + '  (' + APrinter.Title + ')';
     ShowModal;
     Free;
    end;
end;

procedure TSyntPreviewForm.WholePageClick(Sender: TObject);
begin
  ecSyntPreview1.ZoomMode := zmWholePage;
end;

procedure TSyntPreviewForm.Pagewidth1Click(Sender: TObject);
begin
  ecSyntPreview1.ZoomMode := zmPageWidth;
end;

procedure TSyntPreviewForm.N4001Click(Sender: TObject);
begin
  ecSyntPreview1.Zoom := (Sender as TComponent).Tag;
end;

procedure TSyntPreviewForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TSyntPreviewForm.ActionListUpdate(Action: TBasicAction;
  var Handled: Boolean);
begin
  with ecSyntPreview1 do
    begin
      PrintCmd.Enabled := Assigned(ecSyntPreview1.SyntPrinter);
      FirstCmd.Enabled := Page > 1;
      PrevCmd.Enabled := Page > 1;
      NextCmd.Enabled := (PageCount > 1) and (Page < PageCount);
      LastCmd.Enabled := (PageCount > 1) and (Page < PageCount);
    end;
end;

procedure TSyntPreviewForm.FirstCmdExecute(Sender: TObject);
begin
  with ecSyntPreview1 do
  case Integer((Sender as TComponent).Tag) of
    1: Page := 1;
    2: Page := Page - 1;
    3: Page := Page + 1;
    4: Page := PageCount;
  end;
end;

procedure TSyntPreviewForm.CloseCmdExecute(Sender: TObject);
begin
  Close;
end;

procedure TSyntPreviewForm.PrintCmdExecute(Sender: TObject);
begin
  ecSyntPreview1.SyntPrinter.Print;
end;

procedure TSyntPreviewForm.ecSyntPreview1PageChanged(Sender: TObject);
begin
  Edit1.Text := IntToStr(ecSyntPreview1.Page);
  StaticText1.Caption := 'of ' + IntToStr(ecSyntPreview1.PageCount);
  {$IFDEF EC_VCL6_UP}
  StaticText1.BevelKind := bkSoft;
  {$ENDIF}
  ecPrintAction1.SyntPrinter := ecSyntPreview1.SyntPrinter;
end;

procedure TSyntPreviewForm.Edit1Exit(Sender: TObject);
begin
  try
    ecSyntPreview1.Page := StrToInt(Edit1.Text);
  finally
    Edit1.Text := IntToStr(ecSyntPreview1.Page);
  end;
end;

procedure TSyntPreviewForm.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    Edit1Exit(nil);
end;

procedure TSyntPreviewForm.PageSetupExecute(Sender: TObject);
begin
  if ecSyntPreview1.SyntPrinter.PageSetup then
     ecSyntPreview1.UpdatePreview;
end;

procedure TSyntPreviewForm.PageSetupUpdate(Sender: TObject);
begin
  PageSetup.Enabled := ecSyntPreview1.SyntPrinter <> nil;
end;

end.

