PtblRV.pas
上传用户:daoqigc
上传日期:2021-04-20
资源大小:2795k
文件大小:26k
源码类别:

RichEdit

开发平台:

Delphi

  1. {*******************************************************}
  2. {                                                       }
  3. {       RichView                                        }
  4. {       TCustomRVPrint: ancestor of TRVPrint and        }
  5. {       TRVReportHelper.                                }
  6. {       TRVPrint: component for printing                }
  7. {       RichView.                                       }
  8. {       (registered on "RichView" page of               }
  9. {       the Component Palette)                          }
  10. {                                                       }
  11. {       Copyright (c) Sergey Tkachenko                  }
  12. {       svt@trichview.com                               }
  13. {       http://www.trichview.com                        }
  14. {                                                       }
  15. {*******************************************************}
  16. unit PtblRV;
  17. interface
  18. uses
  19.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  20.   RVScroll, RichView, RVItem, RVStyle, Printers, CommDlg, DLines, RVFuncs,
  21.   CRVData, CRVFData, RVRVData, PtRVData;
  22. {$I RV_Defs.inc}
  23. type
  24.   TCustomRVPrint = class;
  25.   TRVPrint = class;  
  26.   {------------------------------------------------------------}
  27.   TRVPrintComponentEvent =
  28.     procedure (Sender: TCustomRVPrint;
  29.                PrintMe: TControl;
  30.                var ComponentImage: TBitmap) of object;
  31.   {------------------------------------------------------------}
  32.   TRVPrintingEvent = procedure (Sender: TCustomRichView; PageCompleted: Integer; Step:TRVPrintingStep) of object;
  33.   {------------------------------------------------------------}
  34.   TRVPagePrepaintEvent = procedure (Sender: TRVPrint; PageNo: Integer;
  35.                                Canvas: TCanvas; Preview: Boolean;
  36.                                PageRect, PrintAreaRect: TRect) of object;
  37.   {------------------------------------------------------------}
  38.   TCustomPrintableRV = class(TCustomRichView)
  39.     private
  40.       FRVPrint: TCustomRVPrint;
  41.     public
  42.       constructor Create(AOwner: TComponent); override;
  43.       {$IFNDEF RVDONOTUSERVF}
  44.       procedure ApplyLayoutInfo (Layout: TRVLayoutInfo); override;
  45.       {$ENDIF}
  46.       function CanUseCustomPPI: Boolean; virtual;      
  47.       function FormatPages: Integer;
  48.       procedure DrawPage(pgNo: Integer; Canvas: TCanvas; Preview, Correction: Boolean);
  49.       property RVPrint: TCustomRVPrint read FRVPrint write FRVPrint;
  50.   end;
  51.   {------------------------------------------------------------}
  52.   TPrintableRV = class(TCustomPrintableRV)
  53.     private
  54.       procedure DoOnPrinting(PageCompleted: Integer; Step:TRVPrintingStep);
  55.     protected
  56.       function GetDataClass: TRichViewRVDataClass; override;
  57.     public
  58.       FMirrorMargins: Boolean;
  59.       FLeftMarginMM, FRightMarginMM, FTopMarginMM, FBottomMarginMM: Integer;
  60.       procedure PrintPages(firstPgNo, lastPgNo: Integer;
  61.                          const Title: String;
  62.                          Copies: Integer; Collate: Boolean);
  63.       procedure Print(const Title: String;
  64.                     Copies: Integer; Collate: Boolean);
  65.       procedure ContinuousPrint;
  66.   end;
  67.   {------------------------------------------------------------}
  68.   TCustomRVPrint = class(TComponent)
  69.     private
  70.       { Private declarations }
  71.       FPreviewCorrection: Boolean;
  72.       FOnPrintComponent: TRVPrintComponentEvent;
  73.       function GetPagesCount: Integer;
  74.       function GetTransparentBackground: Boolean;
  75.       procedure SetTransparentBackground(const Value: Boolean);
  76.       function GetPreview100PercentHeight: Integer;
  77.       function GetPreview100PercentWidth: Integer;
  78.       function GetColorMode: TRVColorMode;
  79.       procedure SetColorMode(const Value: TRVColorMode);
  80.       function GetIsDestinationReady: Boolean;
  81.     protected
  82.       { Protected declarations }
  83.       procedure Loaded; override;
  84.       function CreateRichView: TCustomPrintableRV; virtual;
  85.     public
  86.       { Public declarations }
  87.       rv: TCustomPrintableRV;
  88.       Ready: Boolean;
  89.       StartAt,EndAt: Integer;
  90.       FormattingID: Integer;
  91.       constructor Create(AOwner: TComponent); override;
  92.       procedure Clear;
  93.       procedure UpdatePaletteInfo;
  94.       procedure GetFirstItemOnPage(PageNo: Integer; var ItemNo, OffsetInItem: Integer);
  95.       procedure GetFirstItemOnPageEx(PageNo: Integer; var ItemNo, OffsetInItem, ExtraData: Integer);
  96.       procedure DrawPreview(pgNo: Integer; Canvas:  TCanvas; const PageRect: TRect);
  97.       procedure DrawMarginsRect(Canvas:  TCanvas; const PageRect: TRect; PageNo: Integer);
  98.       {$IFNDEF RVDONOTUSERVF}
  99.       function SavePageAsRVF(Stream: TStream; PageNo: Integer): Boolean;
  100.       {$ENDIF}
  101.       property PagesCount: Integer read GetPagesCount;
  102.       property Preview100PercentWidth: Integer read GetPreview100PercentWidth;
  103.       property Preview100PercentHeight: Integer read GetPreview100PercentHeight;
  104.       property IsDestinationReady: Boolean read GetIsDestinationReady;
  105.     published
  106.       { Published declarations }
  107.       property PreviewCorrection: Boolean read FPreviewCorrection  write FPreviewCorrection;
  108.       property OnPrintComponent: TRVPrintComponentEvent read FOnPrintComponent write FOnPrintComponent;
  109.       property TransparentBackground: Boolean read GetTransparentBackground write SetTransparentBackground default False;
  110.       property ColorMode: TRVColorMode read GetColorMode write SetColorMode default rvcmPrinterColor;
  111.   end;
  112.   TRVPrint = class(TCustomRVPrint)
  113.     private
  114.       { Private declarations }
  115.       FOnFormatting, FOnPrinting: TRVPrintingEvent;
  116.       FOnPagePrepaint, FOnPagePostPaint: TRVPagePrepaintEvent;
  117.       FClipMargins: Boolean;
  118.       function GetLM: Integer;
  119.       function GetRM: Integer;
  120.       function GetTM: Integer;
  121.       function GetBM: Integer;
  122.       procedure SetLM(mm: Integer);
  123.       procedure SetRM(mm: Integer);
  124.       procedure SetTM(mm: Integer);
  125.       procedure SetBM(mm: Integer);
  126.       function GetMirrorMargins: Boolean;
  127.       procedure SetMirrorMargins(const Value: Boolean);
  128.       function GetFooterY: Integer;
  129.       function GetHeaderY: Integer;
  130.       procedure SetFooterY(const Value: Integer);
  131.       procedure SetHeaderY(const Value: Integer);
  132.     protected
  133.       { Protected declarations }
  134.       function CreateRichView: TCustomPrintableRV; override;
  135.     public
  136.       { Public declarations }
  137.       constructor Create(AOwner: TComponent); override;
  138.       procedure AssignSource(PrintMe: TCustomRichView);
  139.       procedure SetHeader(RVData: TCustomRVFormattedData);
  140.       procedure SetFooter(RVData: TCustomRVFormattedData);
  141.       function FormatPages(PrintOptions:TRVDisplayOptions): Integer;
  142.       procedure PrintPages(firstPgNo, lastPgNo: Integer; Title: String;
  143.                          Copies: Integer; Collate: Boolean);
  144.       procedure Print(Title: String; Copies: Integer; Collate: Boolean);
  145.       procedure ContinuousPrint;
  146.       procedure MakePreview(pgNo: Integer; bmp: TBitmap);
  147.       procedure MakeScaledPreview(pgNo: Integer; bmp: TBitmap);
  148.       function GetHeaderRect: TRect;
  149.       function GetFooterRect: TRect;
  150.     published
  151.       { Published declarations }
  152.       property ClipMargins:   Boolean read FClipMargins write FClipMargins default False;
  153.       property MirrorMargins: Boolean read GetMirrorMargins write SetMirrorMargins default False;
  154.       property LeftMarginMM:  Integer read GetLM write SetLM;
  155.       property RightMarginMM: Integer read GetRM write SetRM;
  156.       property TopMarginMM:   Integer read GetTM write SetTM;
  157.       property BottomMarginMM:Integer read GetBM write SetBM;
  158.       property FooterYMM:     Integer read GetFooterY write SetFooterY default 10;
  159.       property HeaderYMM:     Integer read GetHeaderY write SetHeaderY default 10;
  160.       property OnFormatting: TRVPrintingEvent read FOnFormatting write FOnFormatting;
  161.       property OnSendingToPrinter: TRVPrintingEvent read FOnPrinting write FOnPrinting;
  162.       property OnPagePrepaint: TRVPagePrepaintEvent read FOnPagePrepaint write FOnPagePrepaint;
  163.       property OnPagePostpaint: TRVPagePrepaintEvent read FOnPagePostpaint write FOnPagePostpaint;
  164.   end;
  165. function RV_GetPrinterDC: HDC;
  166. implementation
  167. {==============================================================================}
  168. type
  169.   TPrinterDevice = class
  170.     Driver, Device, Port: String;
  171.   end;
  172. function RV_GetPrinterDC: HDC;
  173. var ADevice, ADriver, APort: array[0..79] of Char;
  174.     ADeviceMode: THandle;
  175.     DevMode: PDeviceMode;
  176. begin
  177.   Printer.GetPrinter(ADevice,ADriver,APort,ADeviceMode);
  178.   if ADeviceMode<>0 then
  179.     DevMode := PDeviceMode(GlobalLock(ADeviceMode))
  180.   else
  181.     DevMode := nil;
  182.   Result := CreateDC(ADriver, ADevice, APort, DevMode);
  183.   if ADeviceMode<>0 then
  184.     GlobalUnlock(ADeviceMode);
  185. end;
  186. {=============================== TCustomRVPrint ===============================}
  187. constructor TCustomRVPrint.Create(AOwner: TComponent);
  188. begin
  189.   inherited Create(AOwner);
  190.   rv := CreateRichView;
  191.   rv.FRVPrint := Self;
  192.   if not (csDesigning in ComponentState) then
  193.     rv.Parent := TWinControl(Self.Owner);
  194.   PreviewCorrection := True;
  195.   ColorMode := rvcmPrinterColor;
  196. end;
  197. {------------------------------------------------------------------------------}
  198. function TCustomRVPrint.CreateRichView: TCustomPrintableRV;
  199. begin
  200.   Result := nil;
  201. end;
  202. {------------------------------------------------------------------------------}
  203. procedure TCustomRVPrint.Clear;
  204. begin
  205.   rv.Clear;
  206.   Ready := False;
  207. end;
  208. {------------------------------------------------------------------------------}
  209. procedure TCustomRVPrint.GetFirstItemOnPageEx(PageNo: Integer; var ItemNo, OffsetInItem, ExtraData: Integer);
  210. var dino, dioffs, Part: Integer;
  211. begin
  212.   dino := TPrintableRVData(rv.RVData).Pages[PageNo-1].StartDrawItemNo;
  213.   Part := TPrintableRVData(rv.RVData).Pages[PageNo-1].StartPart;
  214.   if Part>0 then
  215.     ExtraData := (rv.RVData.DrawItems[dino] as TRVMultiDrawItemInfo).PartsList[Part-1].GetSoftPageBreakInfo
  216.   else
  217.     ExtraData := -1;
  218.   if rv.GetItemStyle(rv.RVData.DrawItems[dino].ItemNo)<0 then
  219.     dioffs := 0
  220.   else
  221.     dioffs := 1;
  222.   rv.RVData.DrawItem2Item(dino, dioffs, ItemNo, OffsetInItem);
  223. end;
  224. {------------------------------------------------------------------------------}
  225. procedure TCustomRVPrint.GetFirstItemOnPage(PageNo: Integer; var ItemNo,
  226.   OffsetInItem: Integer);
  227. var ExtraData: Integer;
  228. begin
  229.   GetFirstItemOnPageEx(PageNo, ItemNo, OffsetInItem, ExtraData);
  230. end;
  231. {------------------------------------------------------------------------------}
  232. {$IFNDEF RVDONOTUSERVF}
  233. function TCustomRVPrint.SavePageAsRVF(Stream: TStream; PageNo: Integer): Boolean;
  234. var Layout: TRVLayoutInfo;
  235. begin
  236.   Layout := rv.CreateLayoutInfo;
  237.   Result := TCustomMainPtblRVData(rv.RVData).SavePageAsRVF(Stream, PageNo, rv.Color, rv.Background, Layout);
  238.   Layout.Free;
  239. end;
  240. {$ENDIF}
  241. {------------------------------------------------------------------------------}
  242. function TCustomRVPrint.GetPagesCount: Integer;
  243. begin
  244.   Result := TPrintableRVData(rv.RVData).Pages.Count;
  245. end;
  246. {------------------------------------------------------------------------------}
  247. function TCustomRVPrint.GetTransparentBackground: Boolean;
  248. begin
  249.   Result := TPrintableRVData(rv.RVData).Transparent;
  250. end;
  251. {------------------------------------------------------------------------------}
  252. procedure TCustomRVPrint.SetTransparentBackground(const Value: Boolean);
  253. begin
  254.   TPrintableRVData(rv.RVData).Transparent := Value;
  255. end;
  256. {------------------------------------------------------------------------------}
  257. procedure TCustomRVPrint.Loaded;
  258. begin
  259.   inherited Loaded;
  260.   UpdatePaletteInfo;
  261. end;
  262. {------------------------------------------------------------------------------}
  263. procedure TCustomRVPrint.UpdatePaletteInfo;
  264. begin
  265.   rv.UpdatePaletteInfo;
  266. end;
  267. {------------------------------------------------------------------------------}
  268. function TCustomRVPrint.GetIsDestinationReady: Boolean;
  269. begin
  270.   Result := TCustomMainPtblRVData(rv.RVData).FIsDestinationReady;
  271. end;
  272. {------------------------------------------------------------------------------}
  273. function TCustomRVPrint.GetPreview100PercentHeight: Integer;
  274. begin
  275.   with TCustomMainPtblRVData(rv.RVData) do
  276.     Result := MulDiv(rv.ClientHeight+TmpTM+TmpBM,
  277.                      PrnSaD.ppiyScreen, PrnSaD.ppiyDevice);
  278. end;
  279. {------------------------------------------------------------------------------}
  280. function TCustomRVPrint.GetPreview100PercentWidth: Integer;
  281. begin
  282.   with TCustomMainPtblRVData(rv.RVData) do
  283.     Result := MulDiv(rv.ClientWidth+TmpLM+TmpRM,
  284.                    PrnSaD.ppixScreen, PrnSaD.ppixDevice);
  285. end;
  286. {------------------------------------------------------------------------------}
  287. function TCustomRVPrint.GetColorMode: TRVColorMode;
  288. begin
  289.   Result := TCustomMainPtblRVData(rv.RVData).ColorMode;
  290. end;
  291. {------------------------------------------------------------------------------}
  292. procedure TCustomRVPrint.SetColorMode(const Value: TRVColorMode);
  293. begin
  294.   TCustomMainPtblRVData(rv.RVData).ColorMode := Value;
  295. end;
  296. {------------------------------------------------------------------------------}
  297. procedure TCustomRVPrint.DrawPreview(pgNo: Integer; Canvas: TCanvas;
  298.   const PageRect: TRect);
  299. begin
  300.    SetMapMode(Canvas.Handle,MM_TEXT);
  301.    Canvas.Brush.Color := clWhite;
  302.    Canvas.Pen.Color := clWhite;
  303.    Canvas.FillRect(PageRect);
  304.    SetMapMode(Canvas.Handle,MM_ANISOTROPIC);
  305.    with TCustomMainPtblRVData(rv.RVData) do
  306.      SetWindowExtEx(Canvas.Handle,
  307.                     rv.ClientWidth +TmpLM+TmpRM,
  308.                     rv.ClientHeight+TmpTM+TmpBM,nil);
  309.    with PageRect do begin
  310.      SetViewportExtEx(Canvas.Handle, Right-Left, Bottom-Top,nil);
  311.      SetViewportOrgEx(Canvas.Handle,Left,Top,nil);
  312.    end;
  313.    Canvas.Font.PixelsPerInch := TCustomMainPtblRVData(rv.RVData).PrnSaD.ppiyDevice;
  314.    rv.DrawPage(pgNo, Canvas, True, PreviewCorrection);
  315.    SetMapMode(Canvas.Handle,MM_TEXT);
  316.    SetViewportOrgEx(Canvas.Handle,0,0,nil);
  317. end;
  318. {------------------------------------------------------------------------------}
  319. procedure TCustomRVPrint.DrawMarginsRect(Canvas: TCanvas; const PageRect: TRect; PageNo: Integer);
  320. var FullWidth, FullHeight: Integer;
  321.     RectWidth, RectHeight, LM: Integer;
  322. begin
  323.   with TCustomMainPtblRVData(rv.RVData) do begin
  324.      FullWidth  := rv.ClientWidth+TmpLM+TmpRM;
  325.      FullHeight := rv.ClientHeight+TmpTM+TmpBM;
  326.      RectWidth  := PageRect.Right-PageRect.Left;
  327.      RectHeight := PageRect.Bottom-PageRect.Top;
  328.      LM := TCustomPrintableRVData(rv.RVData).GetPrintableAreaLeft(PageNo);
  329.      Canvas.Rectangle(
  330.        PageRect.Left+MulDiv(LM,RectWidth,FullWidth),
  331.        PageRect.Top+MulDiv(TmpTM,RectHeight,FullHeight),
  332.        PageRect.Left+MulDiv(LM+rv.ClientWidth,RectWidth,FullWidth),
  333.        PageRect.Top+MulDiv(TmpTM+rv.ClientHeight,RectHeight,FullHeight)
  334.       );
  335.   end;
  336. end;
  337. {=============================== TRVPrint =====================================}
  338. constructor TRVPrint.Create(AOwner: TComponent);
  339. begin
  340.   inherited Create(AOwner);
  341.   LeftMarginMM   := 20;
  342.   RightMarginMM  := 20;
  343.   TopMarginMM    := 20;
  344.   BottomMarginMM := 20;
  345. end;
  346. {------------------------------------------------------------------------------}
  347. function TRVPrint.CreateRichView: TCustomPrintableRV;
  348. begin
  349.   Result := TPrintableRV.Create(Self);
  350. end;
  351. {------------------------------------------------------------------------------}
  352. function TRVPrint.GetMirrorMargins: Boolean;
  353. begin
  354.   Result := TPrintableRV(rv).FMirrorMargins;
  355. end;
  356. {------------------------------------------------------------------------------}
  357. procedure TRVPrint.SetMirrorMargins(const Value: Boolean);
  358. begin
  359.   TPrintableRV(rv).FMirrorMargins := Value;
  360. end;
  361. {------------------------------------------------------------------------------}
  362. function TRVPrint.GetLM: Integer;
  363. begin
  364.    GetLM := TPrintableRV(rv).FLeftMarginMM;
  365. end;
  366. {------------------------------------------------------------------------------}
  367. function  TRVPrint.GetRM: Integer;
  368. begin
  369.    GetRM := TPrintableRV(rv).FRightMarginMM;
  370. end;
  371. {------------------------------------------------------------------------------}
  372. function  TRVPrint.GetTM: Integer;
  373. begin
  374.    GetTM := TPrintableRV(rv).FTopMarginMM;
  375. end;
  376. {------------------------------------------------------------------------------}
  377. function  TRVPrint.GetBM: Integer;
  378. begin
  379.    GetBM := TPrintableRV(rv).FBottomMarginMM;
  380. end;
  381. {------------------------------------------------------------------------------}
  382. procedure TRVPrint.SetLM(mm: Integer);
  383. begin
  384.    TPrintableRV(rv).FLeftMarginMM := mm;
  385. end;
  386. {------------------------------------------------------------------------------}
  387. procedure TRVPrint.SetRM(mm: Integer);
  388. begin
  389.    TPrintableRV(rv).FRightMarginMM := mm;
  390. end;
  391. {------------------------------------------------------------------------------}
  392. procedure TRVPrint.SetTM(mm: Integer);
  393. begin
  394.    TPrintableRV(rv).FTopMarginMM := mm;
  395. end;
  396. {------------------------------------------------------------------------------}
  397. procedure TRVPrint.SetBM(mm: Integer);
  398. begin
  399.    TPrintableRV(rv).FBottomMarginMM := mm;
  400. end;
  401. {------------------------------------------------------------------------------}
  402. function TRVPrint.GetFooterY: Integer;
  403. begin
  404.   Result := TPrintableRVData(rv.RVData).FooterY;
  405. end;
  406. {------------------------------------------------------------------------------}
  407. function TRVPrint.GetHeaderY: Integer;
  408. begin
  409.   Result := TPrintableRVData(rv.RVData).HeaderY;
  410. end;
  411. {------------------------------------------------------------------------------}
  412. procedure TRVPrint.SetFooterY(const Value: Integer);
  413. begin
  414.   TPrintableRVData(rv.RVData).FooterY := Value;
  415. end;
  416. {------------------------------------------------------------------------------}
  417. procedure TRVPrint.SetHeaderY(const Value: Integer);
  418. begin
  419.   TPrintableRVData(rv.RVData).HeaderY := Value;
  420. end;
  421. {------------------------------------------------------------------------------}
  422. function TRVPrint.FormatPages(PrintOptions:TRVDisplayOptions): Integer;
  423. begin
  424.   inc(FormattingID);
  425.   if FormattingID=10000 then
  426.     FormattingID := 0;
  427.   Result := rv.FormatPages;
  428.   Ready := True;
  429. end;
  430. {------------------------------------------------------------------------------}
  431. procedure TRVPrint.Print(Title: String; Copies: Integer; Collate: Boolean);
  432. begin
  433.   TPrintableRV(rv).Print(Title, Copies, Collate);
  434. end;
  435. {------------------------------------------------------------------------------}
  436. procedure TRVPrint.PrintPages(firstPgNo, lastPgNo: Integer; Title: String;
  437.                               Copies: Integer; Collate: Boolean);
  438. begin
  439.   TPrintableRV(rv).PrintPages(firstPgNo, lastPgNo, Title, Copies, Collate);
  440. end;
  441. {------------------------------------------------------------------------------}
  442. procedure TRVPrint.ContinuousPrint;
  443. begin
  444.   TPrintableRV(rv).ContinuousPrint;
  445. end;
  446. {------------------------------------------------------------------------------}
  447. procedure TRVPrint.AssignSource(PrintMe: TCustomRichView);
  448. begin
  449.   rv.RVData.ShareItemsFrom(PrintMe.RVData);
  450.   rv.RVData.State := rv.RVData.State+[rvstSkipFormatting];
  451.   try
  452.     rv.LeftMargin   := PrintMe.LeftMargin;
  453.     rv.RightMargin  := PrintMe.RightMargin;
  454.     rv.TopMargin    := 0;
  455.     rv.BottomMargin := 0;
  456.     TPrintableRVData(rv.RVData).FTopMarginPix    := PrintMe.TopMargin;
  457.     TPrintableRVData(rv.RVData).FBottomMarginPix := PrintMe.BottomMargin;
  458.     rv.Style := PrintMe.Style;
  459.     rv.DoInPaletteMode := PrintMe.DoInPaletteMode;
  460.     rv.BackgroundBitmap := PrintMe.BackgroundBitmap;
  461.     rv.BackgroundStyle := PrintMe.BackgroundStyle;
  462.     rv.Color := PrintMe.Color;
  463.     rv.BiDiMode := PrintMe.BiDiMode;
  464.   finally
  465.     rv.RVData.State := rv.RVData.State-[rvstSkipFormatting];
  466.   end;
  467. end;
  468. {------------------------------------------------------------------------------}
  469. procedure TRVPrint.SetHeader(RVData: TCustomRVFormattedData);
  470. begin
  471.   TPrintableRVData(rv.RVData).Header.Free;
  472.   TPrintableRVData(rv.RVData).Header := nil;
  473.   if RVData<>nil then
  474.     TPrintableRVData(rv.RVData).Header := TRVHeaderFooterRVData.Create(rv, RVData, rv.RVData as TCustomPrintableRVData);
  475. end;
  476. {------------------------------------------------------------------------------}
  477. procedure TRVPrint.SetFooter(RVData: TCustomRVFormattedData);
  478. begin
  479.   TPrintableRVData(rv.RVData).Footer.Free;
  480.   TPrintableRVData(rv.RVData).Footer := nil;
  481.   if RVData<>nil then
  482.     TPrintableRVData(rv.RVData).Footer := TRVHeaderFooterRVData.Create(rv, RVData, rv.RVData as TCustomPrintableRVData);
  483. end;
  484. {------------------------------------------------------------------------------}
  485. procedure TRVPrint.MakeScaledPreview(pgNo: Integer; bmp: TBitmap);
  486. begin
  487.   DrawPreview(pgNo, bmp.Canvas, Rect(0,0,bmp.Width, bmp.Height));
  488. end;
  489. {------------------------------------------------------------------------------}
  490. procedure TRVPrint.MakePreview(pgNo: Integer; bmp: TBitmap);
  491. var w,h: Integer;
  492. begin
  493.    w := GetPreview100PercentWidth;
  494.    h := GetPreview100PercentHeight;
  495.    if bmp.Width <> w then bmp.Width := w;
  496.    if bmp.Height <> h then bmp.Height := h;
  497.    MakeScaledPreview(pgNo,bmp);
  498. end;
  499. {------------------------------------------------------------------------------}
  500. function TRVPrint.GetFooterRect: TRect;
  501. begin
  502.   if TPrintableRVData(rv.RVData).Footer=nil then
  503.     Result := Rect(0,0,0,0)
  504.   else
  505.     with TPrintableRVData(rv.RVData).Footer do
  506.       Result := Bounds(Left+DX,Top+DY,Width,DocumentHeight);
  507. end;
  508. {------------------------------------------------------------------------------}
  509. function TRVPrint.GetHeaderRect: TRect;
  510. begin
  511.   if TPrintableRVData(rv.RVData).Header=nil then
  512.     Result := Rect(0,0,0,0)
  513.   else
  514.     with TPrintableRVData(rv.RVData).Header do
  515.       Result := Bounds(Left+DX,Top+DY,Width,DocumentHeight);
  516. end;
  517. {======================== TCustomPrintableRV ==================================}
  518. constructor TCustomPrintableRV.Create(AOwner: TComponent);
  519. begin
  520.   inherited Create(AOwner);
  521.   Visible := False;
  522.   Flags := Flags - [rvflUseJumps, rvflCanUseCustomPPI, rvflCanProcessGetText] +
  523.     [rvflPrinting,rvflShareContents,rvflAllowCustomDrawItems];
  524.   TopMargin    := 0;     // do not change!
  525.   BottomMargin := 0;  // do not change!
  526.   BorderStyle  := bsNone;
  527. end;
  528. {------------------------------------------------------------------------------}
  529. function TCustomPrintableRV.CanUseCustomPPI: Boolean;
  530. begin
  531.   Result := False;
  532. end;
  533. {------------------------------------------------------------------------------}
  534. function TCustomPrintableRV.FormatPages: Integer;
  535. begin
  536.   RVData.State := RVData.State+[rvstSkipformatting];
  537.   try
  538.     VScrollVisible := False;
  539.     HScrollVisible := False;
  540.   finally
  541.     RVData.State := RVData.State-[rvstSkipformatting];
  542.   end;
  543.   Result := TPrintableRVData(RVData).FormatPages;
  544. end;
  545. {------------------------------------------------------------------------------}
  546. procedure TCustomPrintableRV.DrawPage(pgNo: Integer; Canvas: TCanvas;
  547.   Preview, Correction: Boolean);
  548. begin
  549.   TPrintableRVData(RVData).DrawPage(pgNo, Canvas, Preview, Correction);
  550. end;
  551. {------------------------------------------------------------------------------}
  552. {$IFNDEF RVDONOTUSERVF}
  553. procedure TCustomPrintableRV.ApplyLayoutInfo(Layout: TRVLayoutInfo);
  554. begin
  555.   inherited;
  556.   if Layout.FirstParaAborted<>0 then begin
  557.     Include(RVData.State, rvstFirstParaAborted);
  558.     TCustomMainPtblRVData(RVData).FFirstParaListNo := Layout.FirstMarkerListNo;
  559.     TCustomMainPtblRVData(RVData).FFirstParaLevel := Layout.FirstMarkerLevel;
  560.     end
  561.   else begin
  562.     Exclude(RVData.State, rvstFirstParaAborted);
  563.     TCustomMainPtblRVData(RVData).FFirstParaListNo := -1;
  564.     TCustomMainPtblRVData(RVData).FFirstParaLevel := -1;
  565.   end;
  566.   if Layout.LastParaAborted<>0 then
  567.     Include(RVData.State, rvstLastParaAborted)
  568.   else
  569.     Exclude(RVData.State, rvstLastParaAborted);
  570. end;
  571. {$ENDIF}
  572. {============================= TPrintableRV ===================================}
  573. function TPrintableRV.GetDataClass: TRichViewRVDataClass;
  574. begin
  575.   Result := TPrintableRVData;
  576. end;
  577. {------------------------------------------------------------------------------}
  578. procedure TPrintableRV.ContinuousPrint;
  579. var i: Integer;
  580. begin
  581.   for i := 1 to TPrintableRVData(RVData).Pages.Count do begin
  582.     if i<>1 then
  583.       Printer.NewPage;
  584.     DrawPage(i, Printer.Canvas,False,False);
  585.   end;
  586. end;
  587. {------------------------------------------------------------------------------}
  588. procedure TPrintableRV.DoOnPrinting(PageCompleted: Integer; Step:TRVPrintingStep);
  589. begin
  590.   if Assigned(TRVPrint(RVPrint).FOnPrinting) then
  591.     TRVPrint(RVPrint).FOnPrinting(Self, PageCompleted, Step);
  592. end;
  593. {------------------------------------------------------------------------------}
  594. procedure TPrintableRV.PrintPages(firstPgNo, lastPgNo: Integer;
  595.                                   const Title: String;
  596.                                   Copies: Integer; Collate: Boolean);
  597. var i,copyno: Integer;
  598.     PrinterCopies: Integer;
  599. begin
  600.    if lastPgNo<firstPgNo then
  601.      exit;
  602.    FRVPrint.StartAt := 0;
  603.    DoOnPrinting(0, rvpsStarting);
  604.    Printer.Title := Title;
  605.    PrinterCopies := Printer.Copies; { storing }
  606.    if (pcCopies in Printer.Capabilities) and
  607.       (pcCollation in Printer.Capabilities) then
  608.      begin
  609.        Printer.Copies := Copies;
  610.                                  // Printer can make copies and collation if needed
  611.        Copies := 1;              // TRichView does not need to support copies and collation itself
  612.      end
  613.    else
  614.      Printer.Copies := 1;        // TRichView will provide copies and collation itself
  615.    Printer.BeginDoc;
  616.    if Collate then
  617.      for copyno:= 1 to Copies do
  618.        for i := firstPgNo to lastPgNo do
  619.        begin
  620.          DrawPage(i, Printer.Canvas,False,False);
  621.          DoOnPrinting(i, rvpsProceeding);
  622.          if not ((i=lastPgNo) and (copyno=Copies)) then Printer.NewPage;
  623.        end
  624.    else
  625.      for i := firstPgNo to lastPgNo do
  626.        for copyno:= 1 to Copies do
  627.        begin
  628.          DrawPage(i, Printer.Canvas,False,False);
  629.          DoOnPrinting(i, rvpsProceeding);
  630.          if not ((i=lastPgNo) and (copyno=Copies)) then Printer.NewPage;
  631.        end;
  632.    Printer.EndDoc;
  633.    Printer.Copies := PrinterCopies; { restoring }
  634.    DoOnPrinting(0, rvpsFinished);
  635. end;
  636. {------------------------------------------------------------------------------}
  637. procedure TPrintableRV.Print(const Title: String; Copies: Integer; Collate: Boolean);
  638. begin
  639.    PrintPages(1, TPrintableRVData(RVData).Pages.Count, Title, Copies, Collate);
  640. end;
  641. end.