/Source/FR_PGRID.PAS

http://github.com/FastReports/FreeReport · Pascal · 235 lines · 184 code · 33 blank · 18 comment · 9 complexity · dc234474ce3602948ae467fd560f4325 MD5 · raw file

  1. {*****************************************}
  2. { }
  3. { FastReport v2.3 }
  4. { Print DBGrid component }
  5. { }
  6. { FR_PGrid.pas: }
  7. { Copyright (c) 1999 by }
  8. { Butov Konstantin <kos@sp.iae.nsk.su> }
  9. { }
  10. { FastReport: }
  11. { Copyright (c) 1998-99 by Tzyganenko A. }
  12. { }
  13. {*****************************************}
  14. unit FR_PGrid;
  15. interface
  16. {$I FR.inc}
  17. uses
  18. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  19. DB, DBGrids, Printers, FR_DSet, FR_DBSet, FR_Class;
  20. type
  21. TfrPrintGrid = class(TComponent)
  22. private
  23. FDBGrid: TDBGrid;
  24. FReport: TfrReport;
  25. FReportDataSet: TfrDBDataSet;
  26. FColumnDataSet: TfrUserDataSet;
  27. FOrientation: TPrinterOrientation;
  28. FFont, FTitleFont: TFont;
  29. FCaption: String;
  30. FShowCaption: Boolean;
  31. FWidth: Integer;
  32. FDataSet: TDataset;
  33. procedure OnEnterRect(Memo: TStringList; View: TfrView);
  34. procedure OnPrintColumn(ColNo: Integer; var Width: Integer);
  35. function RealColumnIndex(Index: Integer; UseDD: Boolean): Integer;
  36. protected
  37. { Protected declarations }
  38. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  39. public
  40. constructor Create(AOwner: TComponent); override;
  41. destructor Destroy; override;
  42. procedure PreviewReport;
  43. published
  44. property DBGrid: TDBGrid read FDBGrid write FDBGrid;
  45. property Orientation: TPrinterOrientation read FOrientation write FOrientation default poPortrait;
  46. property Font: TFont read FFont write FFont;
  47. property Caption: String read FCaption write FCaption;
  48. property ShowCaption: Boolean read FShowCaption write FShowCaption;
  49. end;
  50. procedure Register;
  51. implementation
  52. type
  53. THackDBGrid = class(TDBGrid)
  54. end;
  55. { TfrPrintGrid }
  56. constructor TfrPrintGrid.Create(AOwner: TComponent);
  57. begin
  58. inherited Create(AOwner);
  59. FFont := TFont.Create;
  60. FFont.Name := 'Arial';
  61. {$IFNDEF Delphi2}
  62. FFont.Charset := frCharset;
  63. {$ENDIF}
  64. FFont.Size := 10;
  65. FTitleFont := TFont.Create;
  66. FTitleFont.Assign(FFont);
  67. FTitleFont.Style := [fsBold];
  68. FCaption := 'Grid';
  69. FShowCaption := True;
  70. end;
  71. destructor TfrPrintGrid.Destroy;
  72. begin
  73. FFont.Free;
  74. FTitleFont.Free;
  75. inherited Destroy;
  76. end;
  77. procedure TfrPrintGrid.Notification(AComponent: TComponent; Operation: TOperation);
  78. begin
  79. inherited Notification(AComponent, Operation);
  80. if (Operation = opRemove) and (AComponent = DBGrid) then
  81. DBGrid := nil;
  82. end;
  83. function TfrPrintGrid.RealColumnIndex(Index: Integer; UseDD: Boolean): Integer;
  84. var
  85. Y, I, DD: Integer;
  86. begin
  87. Result := 0;
  88. if (dgIndicator in DBGrid.Options) and UseDD then
  89. DD := 1 else
  90. DD := 0;
  91. Y := -1;
  92. for I := 0 to FDataSet.FieldCount - 1 do
  93. if FDataSet.Fields[I].Visible then
  94. begin
  95. Inc(Y);
  96. if Y = Index then
  97. begin
  98. Result := I + DD;
  99. break;
  100. end;
  101. end;
  102. end;
  103. procedure TfrPrintGrid.PreviewReport;
  104. var
  105. v: TfrView;
  106. b: TfrBandView;
  107. Page: TfrPage;
  108. begin
  109. if (FDBGrid = nil) or (DBGrid.Datasource = nil) or
  110. (DBGrid.Datasource.Dataset = nil) then Exit;
  111. FDataSet := DBGrid.Datasource.Dataset;
  112. FReport := TfrReport.Create(Self);
  113. FReport.OnEnterRect := OnEnterRect;
  114. FReport.OnPrintColumn := OnPrintColumn;
  115. FReportDataSet := TfrDBDataSet.Create(Self);
  116. FReportDataSet.Name := 'frGridDBDataSet1';
  117. FReportDataSet.DataSet := FDataSet;
  118. FColumnDataSet := TfrUserDataSet.Create(Self);
  119. FColumnDataSet.Name := 'frGridUserDataSet1';
  120. FColumnDataSet.RangeEnd := reCount;
  121. FColumnDataSet.RangeEndCount := FDataSet.FieldCount;
  122. try
  123. FReportDataSet.DataSource := DBGrid.DataSource;
  124. FReport.Pages.Add;
  125. Page := FReport.Pages[0];
  126. with Page do
  127. ChangePaper(pgSize, pgWidth, pgHeight, FOrientation);
  128. if FShowCaption then
  129. begin
  130. b := TfrBandView(frCreateObject(gtBand, ''));
  131. b.SetBounds(10, 20, 1000, 25);
  132. b.BandType := btReportTitle;
  133. Page.Objects.Add(b);
  134. v := frCreateObject(gtMemo, '');
  135. v.SetBounds(20, 20, Page.PrnInfo.PgW - 40, 25);
  136. TfrMemoView(v).Adjust:= frtaCenter;
  137. TfrMemoView(v).Font := FTitleFont;
  138. v.Memo.Add(FCaption);
  139. Page.Objects.Add(v);
  140. end;
  141. b := TfrBandView(frCreateObject(gtBand, ''));
  142. b.BandType := btMasterHeader;
  143. b.SetBounds(20, 60, 1000, 20);
  144. Page.Objects.Add(b);
  145. v := frCreateObject(gtMemo, '');
  146. v.SetBounds(20, 60, 20, 20);
  147. TfrMemoView(v).Adjust := frtaCenter;
  148. TfrMemoView(v).FillColor := clGray;
  149. TfrMemoView(v).Font := FTitleFont;
  150. TfrMemoView(v).FrameTyp := 15;
  151. v.Memo.Add('[Header]');
  152. Page.Objects.Add(v);
  153. b := TfrBandView(frCreateObject(gtBand, ''));
  154. b.BandType := btMasterData;
  155. b.Dataset := FReportDataSet.Name;
  156. b.SetBounds(0, 100, 1000, 18);
  157. Page.Objects.Add(b);
  158. b := TfrBandView(frCreateObject(gtBand, ''));
  159. b.BandType := btCrossData;
  160. b.Dataset := FColumnDataSet.Name;
  161. b.SetBounds(20, 0, 20, 1000);
  162. Page.Objects.Add(b);
  163. v := frCreateObject(gtMemo, '');
  164. v.SetBounds(20, 100, 20, 18);
  165. v.Memo.Add('[Cell]');
  166. TfrMemoView(v).FrameTyp := 15;
  167. Page.Objects.Add(v);
  168. FReport.ShowReport;
  169. finally
  170. FReport.Free;
  171. FReportDataSet.Free;
  172. FColumnDataSet.Free;
  173. end;
  174. end;
  175. procedure TfrPrintGrid.OnEnterRect(Memo: TStringList; View: TfrView);
  176. begin
  177. if Memo[0] = '[Cell]' then
  178. begin
  179. Memo[0] := FDataSet.Fields[RealColumnIndex(FColumnDataSet.RecNo, False)].AsString;
  180. View.dx := FWidth;
  181. case FDataSet.Fields[RealColumnIndex(FColumnDataSet.RecNo, False)].Alignment of
  182. taLeftJustify : TfrMemoView(View).Adjust := frtaLeft;
  183. taRightJustify: TfrMemoView(View).Adjust := frtaRight;
  184. taCenter : TfrMemoView(View).Adjust := frtaCenter;
  185. end;
  186. end;
  187. if Memo[0] = '[Header]' then
  188. begin
  189. Memo[0] := FDataSet.Fields[RealColumnIndex(FColumnDataSet.RecNo, False)].FieldName;
  190. View.dx := FWidth;
  191. end;
  192. end;
  193. procedure TfrPrintGrid.OnPrintColumn(ColNo: Integer; var Width: Integer);
  194. begin
  195. Width := THackDBGrid(DBGrid).ColWidths[RealColumnIndex(ColNo - 1, True)];
  196. FWidth := Width;
  197. end;
  198. procedure Register;
  199. begin
  200. RegisterComponents('ButSoft', [TfrPrintGrid]);
  201. end;
  202. end.