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

RichEdit

开发平台:

Delphi

  1. {*******************************************************}
  2. {                                                       }
  3. {       RichView                                        }
  4. {       TRVReportHelper: contains RichView document     }
  5. {       and draws/prints in onto the specified          }
  6. {       Canvas                                          }
  7. {       (registered on "RichView" page of               }
  8. {       the Component Palette)                          }
  9. {                                                       }
  10. {       Copyright (c) Sergey Tkachenko                  }
  11. {       svt@trichview.com                               }
  12. {       http://www.trichview.com                        }
  13. {                                                       }
  14. {*******************************************************}
  15. unit RVReport;
  16. interface
  17. {$I RV_Defs.inc}
  18. uses Windows, Graphics, Classes, RVStyle, RVClasses, RVItem,
  19.      CRVData, CRVFData, RVRVData, RichView, PtRVData, PtblRV, RVFuncs;
  20. type
  21.   TRVReportHelper = class;
  22.   TRVDrawHyperlinkEvent = procedure (Sender: TRVReportHelper;
  23.     RVData: TCustomRVData; ItemNo: Integer; R: TRect) of object;
  24.   TRVDrawCheckpointEvent = procedure (Sender: TRVReportHelper;
  25.     RVData: TCustomRVData; ItemNo: Integer; X, Y: Integer) of object;
  26.   { ----------------------------------------------------------------------------
  27.     TReportRVData: main data object for TReportRichView
  28.   }
  29.   TReportRVData = class(TCustomMainPtblRVData)
  30.     private
  31.       StartY,       StartAt,       Y,       DrawItemNo,   CurHeight: Integer;
  32.       StoredStartY, StoredStartAt, StoredY, StoredDrawItemNo, StoredMaxHeight: Integer;
  33.       Splitting: Boolean;
  34.       StoredSplitting: Boolean;
  35.       procedure StoreMargins;
  36.       procedure RestoreMargins;
  37.       procedure Init(ACanvas: TCanvas; APageWidth: Integer);
  38.       function FormatNextPage(AMaxHeight: Integer): Boolean;
  39.       procedure UnformatLastPage;
  40.       function Finished: Boolean;
  41.       procedure Reset;
  42.     protected
  43.       function ShareItems: Boolean; override;
  44.       procedure DoOnHyperlink(RVData: TCustomRVData; ItemNo: Integer;
  45.         const R: TRect); override;
  46.       procedure DoOnCheckpoint(RVData: TCustomRVData; ItemNo, X, Y: Integer); override;
  47.     public
  48.       function GetHeight: Integer; override;
  49.   end;
  50.   { ----------------------------------------------------------------------------
  51.     TReportRichView: class of hidden TRichView inside TRVReportHelper
  52.   }
  53.   TReportRichView = class(TCustomPrintableRV)
  54.     private
  55.       function GetHeight: Integer;
  56.       function GetWidth: Integer;
  57.       function GetLeft: Integer;
  58.       function GetTop: Integer;
  59.     protected
  60.       function GetDataClass: TRichViewRVDataClass; override;
  61.     public
  62.       constructor Create(AOwner: TComponent); override;
  63.       function CanUseCustomPPI: Boolean; override;      
  64.     published
  65.       { Published standard properties }
  66.       property Color default clNone;
  67.       { Published RichView properties }
  68.       property BackgroundBitmap;
  69.       property BackgroundStyle;
  70.       property BiDiMode;
  71.       property BottomMargin;
  72.       property Delimiters;
  73.       property LeftMargin;
  74.       property Options;
  75.       property RightMargin;
  76.       property RTFOptions;
  77.       property RTFReadProperties;
  78.       property RVFOptions;
  79.       property RVFParaStylesReadMode;
  80.       property RVFTextStylesReadMode;
  81.       property Style;
  82.       property TopMargin;
  83.       { Published RichView events }
  84.       property OnControlAction;
  85.       property OnHTMLSaveImage;
  86.       property OnRVFImageListNeeded;
  87.       property OnRVFControlNeeded;
  88.       property OnRVFPictureNeeded;
  89.       property OnSaveComponentToFile;
  90.       property OnURLNeeded;
  91.       property OnReadHyperlink;
  92.       property OnWriteHyperlink;
  93.       property Width: Integer read GetWidth;
  94.       property Height: Integer read GetHeight;
  95.       property Left: Integer read GetLeft;
  96.       property Top: Integer read GetTop;
  97.   end;
  98.   TRVReportHelper = class (TCustomRVPrint)
  99.     private
  100.       FOnDrawHyperlink: TRVDrawHyperlinkEvent;
  101.       FOnDrawCheckpoint: TRVDrawCheckpointEvent;
  102.       function GetRichView: TReportRichView;
  103.     protected
  104.       function CreateRichView: TCustomPrintableRV; override;
  105.     public
  106.       { Create & Destroy }
  107.       constructor Create(AOwner: TComponent); override;
  108.       { Formatting }
  109.       procedure Init(ACanvas: TCanvas; APageWidth: Integer);
  110.       function FormatNextPage(AMaxHeight: Integer): Boolean;
  111.       procedure UnformatLastPage;
  112.       procedure Reset;
  113.       { Drawing }
  114.       procedure DrawPage(APageNo: Integer; ACanvas: TCanvas; APreview: Boolean;
  115.         AHeight: Integer);
  116.       procedure DrawPageAt(Left, Top, APageNo: Integer; ACanvas: TCanvas;
  117.         APreview: Boolean; AHeight: Integer);
  118.       { Information }
  119.       function Finished: Boolean;
  120.       function GetLastPageHeight: Integer;
  121.     {$IFDEF RICHVIEWDEF6}
  122.     published
  123.     {$ENDIF}
  124.       property RichView: TReportRichView read GetRichView;
  125.     published
  126.       property ColorMode default rvcmColor;
  127.       property OnDrawHyperlink: TRVDrawHyperlinkEvent
  128.         read FOnDrawHyperlink write FOnDrawHyperlink;
  129.       property OnDrawCheckpoint: TRVDrawCheckpointEvent
  130.         read FOnDrawCheckpoint write FOnDrawCheckpoint;
  131.   end;
  132. implementation
  133. uses DLines, RVCtrlData;
  134. {============================ TRVReportData ===================================}
  135. function TReportRVData.ShareItems: Boolean;
  136. begin
  137.   Result := False;
  138. end;
  139. {------------------------------------------------------------------------------}
  140. procedure TReportRVData.Reset;
  141. var i: Integer;
  142.     dli: TRVDrawLineInfo;
  143. begin
  144.   Pages.Clear;
  145.   DrawItemNo := 0;
  146.   Splitting  := False;
  147.   for i := 0 to DrawItems.Count-1 do begin
  148.     dli := TRVDrawLineInfo(DrawItems[i]);
  149.     if dli is TRVMultiDrawItemInfo then
  150.       TRVMultiDrawItemInfo(dli).PartsList.Clear;
  151.   end;
  152. end;
  153. {------------------------------------------------------------------------------}
  154. procedure TReportRVData.Init(ACanvas: TCanvas; APageWidth: Integer);
  155. begin
  156.   Reset;
  157.   PrinterCanvas := ACanvas;
  158.   TmpLM := 0;
  159.   TmpTM := 0;
  160.   StoreMargins;
  161.   Prepare;
  162.   State := State+[rvstSkipformatting];
  163.   try
  164.     TCustomRichView(FRichView).HandleNeeded;
  165.     TCustomRichView(FRichView).VScrollVisible := False;
  166.     TCustomRichView(FRichView).HScrollVisible := False;
  167.     FRichView.ClientWidth := APageWidth;
  168.     FRichView.ClientHeight:= APageWidth;
  169.   finally
  170.     State := State-[rvstSkipformatting];
  171.   end;
  172.   TCustomRichView(FRichView).MaxTextWidth := RV_XToScreen(FRichView.Width, PrnSaD)-
  173.     TCustomRichView(FRichView).LeftMargin-TCustomRichView(FRichView).RightMargin;
  174.   Format_(False, True, False, 0, PrinterCanvas, False, False, False);
  175.   RestoreMargins;
  176.   FIsDestinationReady := True;
  177. end;
  178. {------------------------------------------------------------------------------}
  179. procedure TReportRVData.StoreMargins;
  180. begin
  181.   FTopMarginPix    := TCustomRichView(FRichView).TopMargin;
  182.   FBottomMarginPix := TCustomRichView(FRichView).BottomMargin;
  183.   TCustomRichView(FRichView).TopMargin := 0;
  184.   TCustomRichView(FRichView).BottomMargin := 0;
  185. end;
  186. {------------------------------------------------------------------------------}
  187. procedure TReportRVData.RestoreMargins;
  188. begin
  189.   TCustomRichView(FRichView).TopMargin    := FTopMarginPix;
  190.   TCustomRichView(FRichView).BottomMargin := FBottomMarginPix;
  191. end;
  192. {------------------------------------------------------------------------------}
  193. function TReportRVData.Finished: Boolean;
  194. begin
  195.   Result := (DrawItems.Count=0) or (DrawItemNo>=DrawItems.Count);
  196. end;
  197. {------------------------------------------------------------------------------}
  198. function TReportRVData.FormatNextPage(AMaxHeight: Integer): Boolean;
  199. begin
  200.   Result := not Finished;
  201.   if Result then begin
  202.     StoredDrawItemNo := DrawItemNo;
  203.     StoredStartAt    := StartAt;
  204.     StoredStartY     := StartY;
  205.     StoredY          := Y;
  206.     StoredSplitting  := Splitting;
  207.     if Splitting then begin
  208.       dec(Y, StoredMaxHeight);
  209.       inc(Y, AMaxHeight);
  210.     end;
  211.     StoredMaxHeight  :=  AMaxHeight;
  212.     StoreMargins;
  213.     dec(AMaxHeight, TmpTMPix+TmpBMPix);
  214.     inherited FormatNextPage(DrawItemNo, StartAt, StartY, Y, Splitting, AMaxHeight);
  215.     RestoreMargins;
  216.   end;
  217. end;
  218. {------------------------------------------------------------------------------}
  219. procedure TReportRVData.UnformatLastPage;
  220. begin
  221.   Pages[Pages.Count-1].Free;
  222.   DrawItemNo := StoredDrawItemNo;
  223.   StartAt    := StoredStartAt;
  224.   StartY     := StoredStartY;
  225.   Y          := StoredY;
  226.   Splitting  := StoredSplitting;
  227. end;
  228. {------------------------------------------------------------------------------}
  229. function TReportRVData.GetHeight: Integer;
  230. begin
  231.   Result := CurHeight;
  232. end;
  233. {------------------------------------------------------------------------------}
  234. procedure TReportRVData.DoOnHyperlink(RVData: TCustomRVData;
  235.   ItemNo: Integer; const R: TRect);
  236. var Helper: TRVReportHelper;
  237. begin
  238.   Helper := TRVReportHelper(TReportRichView(FRichView).RVPrint);
  239.   if Assigned(Helper.FOnDrawHyperlink) then
  240.     Helper.FOnDrawHyperlink(Helper, RVData, ItemNo, R);
  241. end;
  242. {------------------------------------------------------------------------------}
  243. procedure TReportRVData.DoOnCheckpoint(RVData: TCustomRVData; ItemNo, X,
  244.   Y: Integer);
  245. var Helper: TRVReportHelper;
  246. begin
  247.   Helper := TRVReportHelper(TReportRichView(FRichView).RVPrint);
  248.   if Assigned(Helper.FOnDrawCheckpoint) then
  249.     Helper.FOnDrawCheckpoint(Helper, RVData, ItemNo, X, Y);
  250. end;
  251. {================================ TReportRichView =============================}
  252. constructor TReportRichView.Create(AOwner: TComponent);
  253. begin
  254.   inherited Create(AOwner);
  255.   Flags := Flags - [rvflShareContents]+[rvflCanUseCustomPPI];  
  256.   Name := 'RichView';
  257.   {$IFDEF RICHVIEWDEF6}
  258.   SetSubComponent(True);
  259.   {$ENDIF}
  260. end;
  261. {------------------------------------------------------------------------------}
  262. function TReportRichView.GetDataClass: TRichViewRVDataClass;
  263. begin
  264.   Result := TReportRVData;
  265. end;
  266. {------------------------------------------------------------------------------}
  267. function TReportRichView.CanUseCustomPPI: Boolean;
  268. begin
  269.   Result := True;
  270. end;
  271. {------------------------------------------------------------------------------}
  272. function TReportRichView.GetHeight: Integer;
  273. begin
  274.   Result := inherited Height;
  275. end;
  276. {------------------------------------------------------------------------------}
  277. function TReportRichView.GetLeft: Integer;
  278. begin
  279.   Result := inherited Left;
  280. end;
  281. {------------------------------------------------------------------------------}
  282. function TReportRichView.GetTop: Integer;
  283. begin
  284.   Result := inherited Top;
  285. end;
  286. {------------------------------------------------------------------------------}
  287. function TReportRichView.GetWidth: Integer;
  288. begin
  289.   Result := inherited Width;
  290. end;
  291. {================================ TRVReportHelper =============================}
  292. { Constructor.                                                                 }
  293. constructor TRVReportHelper.Create(AOwner: TComponent);
  294. begin
  295.   inherited;
  296.   ColorMode := rvcmColor;
  297. end;
  298. {------------------------------------------------------------------------------}
  299. function TRVReportHelper.CreateRichView: TCustomPrintableRV;
  300. begin
  301.   Result := TReportRichView.Create(Self);
  302. end;
  303. {------------------------------------------------------------------------------}
  304. procedure TRVReportHelper.Init(ACanvas: TCanvas; APageWidth: Integer);
  305. begin
  306.   inc(FormattingID);
  307.   if FormattingID=10000 then
  308.     FormattingID := 0;
  309.   TReportRVData(TReportRichView(rv).RVData).Init(ACanvas,APageWidth);
  310. end;
  311. {------------------------------------------------------------------------------}
  312. function TRVReportHelper.Finished: Boolean;
  313. begin
  314.   Result := TReportRVData(TReportRichView(rv).RVData).Finished;
  315. end;
  316. {------------------------------------------------------------------------------}
  317. function TRVReportHelper.FormatNextPage(AMaxHeight: Integer): Boolean;
  318. begin
  319.   Result := TReportRVData(TReportRichView(rv).RVData).FormatNextPage(AMaxHeight);
  320. end;
  321. {------------------------------------------------------------------------------}
  322. procedure TRVReportHelper.UnformatLastPage;
  323. begin
  324.   TReportRVData(TReportRichView(rv).RVData).UnformatLastPage;
  325. end;
  326. {------------------------------------------------------------------------------}
  327. function TRVReportHelper.GetRichView: TReportRichView;
  328. begin
  329.   Result := TReportRichView(rv);
  330. end;
  331. {------------------------------------------------------------------------------}
  332. procedure TRVReportHelper.DrawPage(APageNo: Integer; ACanvas: TCanvas;
  333.   APreview: Boolean; AHeight: Integer);
  334. begin
  335.   TReportRVData(TReportRichView(rv).RVData).CurHeight := AHeight;
  336.   rv.DrawPage(APageNo, ACanvas, APreview, PreviewCorrection);
  337. end;
  338. {------------------------------------------------------------------------------}
  339. procedure TRVReportHelper.DrawPageAt(Left, Top, APageNo: Integer;
  340.   ACanvas: TCanvas; APreview: Boolean; AHeight: Integer);
  341. var pt: TPoint;
  342. begin
  343.   SetWindowOrgEx(ACanvas.Handle, -Left, -Top, @pt);
  344.   try
  345.     DrawPage(APageNo, ACanvas, APreview, AHeight);
  346.   finally
  347.     SetWindowOrgEx(ACanvas.Handle, pt.x, pt.y, nil);
  348.   end;
  349. end;
  350. {------------------------------------------------------------------------------}
  351. function TRVReportHelper.GetLastPageHeight: Integer;
  352. begin
  353.   Result := EndAt+TReportRVData(RichView.RVData).TmpTMPix+
  354.     TReportRVData(RichView.RVData).TmpBMPix;
  355. end;
  356. {------------------------------------------------------------------------------}
  357. procedure TRVReportHelper.Reset;
  358. begin
  359.   TReportRVData(TReportRichView(rv).RVData).Reset;
  360. end;
  361. end.