unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, RVScroll, RichView, RVEdit, ComCtrls, RVStyle, PreviewFrm,
  PtblRV, StdCtrls, SclRView, SRVPrint;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet4: TTabSheet;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    Print1: TMenuItem;
    Exit1: TMenuItem;
    RVStyle1: TRVStyle;
    RVStyle2: TRVStyle;
    N1: TMenuItem;
    N2: TMenuItem;
    PrintPreview1: TMenuItem;
    OpenDialog1: TOpenDialog;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    txtHeader: TEdit;
    New1: TMenuItem;
    SRichViewEdit1: TSRichViewEdit;
    SRVPrint1: TSRVPrint;
    PrinterSetupDialog1: TPrinterSetupDialog;
    txtFooter: TEdit;
    Label1: TLabel;
    procedure PrintPreview1Click(Sender: TObject);
    procedure Open1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure New1Click(Sender: TObject);
    procedure Print1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure txtHeaderChange(Sender: TObject);
    procedure txtFooterChange(Sender: TObject);
  private
    { Private declarations }
    procedure OpenFile(const FileName: String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.New1Click(Sender: TObject);
begin
  SRichViewEdit1.RichViewEdit.Clear;
  SRichViewEdit1.RVHeader.Clear;
  SRichViewEdit1.RVFooter.Clear;
  SRichViewEdit1.RichViewEdit.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RVHeader.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RVFooter.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RichViewEdit.Format;
  SRichViewEdit1.RVHeader.Format;
  SRichViewEdit1.RVFooter.Format;
end;

procedure TForm1.OpenFile(const FileName: String);
var
     MultiPixelsV : Extended;
begin
  SRichViewEdit1.RichViewEdit.Clear;
  SRichViewEdit1.RVHeader.Clear;
  SRichViewEdit1.RVFooter.Clear;
  SRichViewEdit1.RichViewEdit.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RVHeader.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RVFooter.DeleteUnusedStyles(True,True,True);
  SRichViewEdit1.RichViewEdit.LoadRTF(FileName);
  SRichViewEdit1.SetRVMargins;

  SRichViewEdit1.ViewProperty.ZoomMode := rvzmPageWidth;

  with SRichViewEdit1 do begin
    MultiPixelsV := UnitsPerInchV(UnitsProgram) / UnitsPerInchV(RichViewEdit.DocParameters.Units);
    PageProperty.HeaderY := RichViewEdit.DocParameters.HeaderY * MultiPixelsV;
    PageProperty.FooterY := RichViewEdit.DocParameters.FooterY * MultiPixelsV;
  end;

  txtHeader.Text := IntToStr(SRichViewEdit1.RichViewEdit.RTFReadProperties.HeaderYMM);
  txtFooter.Text := IntToStr(SRichViewEdit1.RichViewEdit.RTFReadProperties.FooterYMM);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenFile(ExtractFilePath(Application.ExeName)+'doc.rtf');
end;

procedure TForm1.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    OpenFile(OpenDialog1.FileName);
end;

procedure TForm1.PrintPreview1Click(Sender: TObject);
var
     frm: TfrmPreview;
begin
  SRVPrint1.Update;
  frm := TfrmPreview.Create(Application);
  frm.Button1Click(nil);
  frm.ShowModal;
  frm.Free;
end;

procedure TForm1.Print1Click(Sender: TObject);
begin
  if PrinterSetupDialog1.Execute then begin
    if SRichViewEdit1.PageCount > 0 then
      SRVPrint1.Print('RichView Edit Demo',1,False);
  end;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.txtHeaderChange(Sender: TObject);
var
     MultiPixelsV : Extended;
begin
  with SRichViewEdit1 do begin
    MultiPixelsV := UnitsPerInchV(UnitsProgram) / UnitsPerInchV(rvuMillimeters);
    PageProperty.HeaderY := StrToIntDef(txtHeader.Text, 0) * MultiPixelsV;
  end;
end;

procedure TForm1.txtFooterChange(Sender: TObject);
var
     MultiPixelsV : Extended;
begin
  with SRichViewEdit1 do begin
    MultiPixelsV := UnitsPerInchV(UnitsProgram) / UnitsPerInchV(rvuMillimeters);
    PageProperty.FooterY := StrToIntDef(txtFooter.Text, 0) * MultiPixelsV;
  end;
end;

end.