rptexpressions.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. unit rptexpressions;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. udapp;
  10. type
  11. { TExpressionsDemo }
  12. TExpressionsDemo = class(TReportDemoApp)
  13. private
  14. FReportData: TFPReportUserData;
  15. sl: TStringList;
  16. procedure DoBeforePrint(Sender: TFPReportElement);
  17. procedure GetReportDataFirst(Sender: TObject);
  18. procedure GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  19. procedure GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  20. procedure GetReportFieldNames(Sender: TObject; List: TStrings);
  21. protected
  22. procedure InitialiseData; override;
  23. procedure CreateReportDesign; override;
  24. public
  25. constructor Create(AOwner : TComponent); override;
  26. destructor Destroy; override;
  27. Class function Description : string; override;
  28. end;
  29. implementation
  30. { TExpressionsDemo }
  31. procedure TExpressionsDemo.GetReportDataFirst(Sender: TObject);
  32. begin
  33. {$IFDEF gdebug}
  34. writeln('GetReportDataFirst');
  35. {$ENDIF}
  36. end;
  37. procedure TExpressionsDemo.DoBeforePrint(Sender: TFPReportElement);
  38. begin
  39. With rpt.Variables.FindVariable('isEven') do
  40. AsBoolean:=Not AsBoolean;
  41. end;
  42. procedure TExpressionsDemo.GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  43. begin
  44. {$IFDEF gdebug}
  45. writeln(Format('GetReportDataValue - %d', [lReportData.RecNo]));
  46. {$ENDIF}
  47. if AValueName = 'element' then
  48. begin
  49. AValue := sl[FReportData.RecNo-1];
  50. end;
  51. end;
  52. procedure TExpressionsDemo.GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  53. begin
  54. {$IFDEF gdebug}
  55. writeln(Format('GetReportDataEOF - %d', [lReportData.RecNo]));
  56. {$ENDIF}
  57. if FReportData.RecNo > sl.Count then
  58. IsEOF := True
  59. else
  60. IsEOF := False;
  61. end;
  62. procedure TExpressionsDemo.GetReportFieldNames(Sender: TObject; List: TStrings);
  63. begin
  64. List.Add('element');
  65. end;
  66. procedure TExpressionsDemo.InitialiseData;
  67. var
  68. i: integer;
  69. begin
  70. sl := TStringList.Create;
  71. for i := 1 to 15 do
  72. sl.Add(Format(Char(64+i)+'-Item %d', [i]));
  73. end;
  74. procedure TExpressionsDemo.CreateReportDesign;
  75. var
  76. p: TFPReportPage;
  77. TitleBand: TFPReportTitleBand;
  78. DataBand: TFPReportDataBand;
  79. Memo: TFPReportMemo;
  80. PageFooter: TFPReportPageFooterBand;
  81. DataHeader: TFPReportDataHeaderBand;
  82. begin
  83. Inherited;
  84. rpt.Author := 'Graeme Geldenhuys';
  85. rpt.Title := 'FPReport Demo 2 - Expression Evaluation';
  86. // Line zero : even..
  87. rpt.Variables.AddVariable('isEven').AsBoolean:=True;
  88. p := TFPReportPage.Create(rpt);
  89. p.Orientation := poPortrait;
  90. p.PageSize.PaperName := 'A4';
  91. { page margins }
  92. p.Margins.Left := 20;
  93. p.Margins.Top := 20;
  94. p.Margins.Right := 20;
  95. p.Margins.Bottom := 20;
  96. p.Data := FReportData;
  97. p.Font.Name := 'LiberationSans';
  98. TitleBand := TFPReportTitleBand.Create(p);
  99. TitleBand.Layout.Height := 40;
  100. {$ifdef ColorBands}
  101. TitleBand.Frame.Shape := fsRectangle;
  102. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  103. {$endif}
  104. Memo := TFPReportMemo.Create(TitleBand);
  105. Memo.Layout.Left := 55;
  106. Memo.Layout.Top := 10;
  107. Memo.Layout.Width := 50;
  108. Memo.Layout.Height := 8;
  109. Memo.Text := 'THE REPORT TITLE';
  110. Memo := TFPReportMemo.Create(TitleBand);
  111. Memo.Layout.Left := 125;
  112. Memo.Layout.Top := 20;
  113. Memo.Layout.Width := 50;
  114. Memo.Layout.Height := 8;
  115. Memo.Text := 'Report Date: [TODAY]';
  116. Memo := TFPReportMemo.Create(TitleBand);
  117. Memo.Layout.Left := 0;
  118. Memo.Layout.Top := 0;
  119. Memo.Layout.Width := 30;
  120. Memo.Layout.Height := 5;
  121. Memo.Text := '1 + 2 = [1 + 2].';
  122. Memo.Options := [moDisableExpressions];
  123. DataHeader := TFPReportDataHeaderBand.Create(p);
  124. DataHeader.Layout.Height := 10;
  125. {$ifdef ColorBands}
  126. DataHeader.Frame.Shape := fsRectangle;
  127. DataHeader.Frame.BackgroundColor := clDataHeaderFooter;
  128. {$endif}
  129. DataHeader.UseParentFont := False;
  130. DataHeader.Font.Name := 'LiberationSans-Bold';
  131. Memo := TFPReportMemo.Create(DataHeader);
  132. Memo.Layout.Left := 5;
  133. Memo.Layout.Top := 0;
  134. Memo.Layout.Width := 75;
  135. Memo.Layout.Height := 5;
  136. Memo.Text := 'Complex Example';
  137. Memo.Frame.Lines := [flBottom];
  138. Memo.TextAlignment.Vertical := tlCenter;
  139. Memo := TFPReportMemo.Create(DataHeader);
  140. Memo.Layout.Left := 85;
  141. Memo.Layout.Top := 0;
  142. Memo.Layout.Width := 25;
  143. Memo.Layout.Height := 5;
  144. Memo.Text := 'Single Calc';
  145. Memo.Frame.Lines := [flBottom];
  146. Memo.TextAlignment.Vertical := tlCenter;
  147. Memo := TFPReportMemo.Create(DataHeader);
  148. Memo.Layout.Left := 120;
  149. Memo.Layout.Top := 0;
  150. Memo.Layout.Width := 35;
  151. Memo.Layout.Height := 5;
  152. Memo.Text := 'System Variables';
  153. Memo.StretchMode := smActualHeight;
  154. Memo.Frame.Lines := [flBottom];
  155. Memo.TextAlignment.Vertical := tlCenter;
  156. DataBand := TFPReportDataBand.Create(p);
  157. DataBand.Layout.Height := 10;
  158. DataBand.HeaderBand := DataHeader;
  159. {$ifdef ColorBands}
  160. DataBand.Frame.Shape := fsRectangle;
  161. DataBand.Frame.BackgroundColor := clDataBand;
  162. {$endif}
  163. DataBand.OnBeforePrint:=@DoBeforePrint;
  164. Memo := TFPReportMemo.Create(DataBand);
  165. Memo.Layout.Left := 5;
  166. Memo.Layout.Top := 2;
  167. Memo.Layout.Width := 75;
  168. Memo.Layout.Height := 5;
  169. Memo.Text := 'Even row: [isEven]. Hello world "[element]", and first letter is "[COPY(element, 1, 1)]".';
  170. Memo.Options := [];
  171. Memo := TFPReportMemo.Create(DataBand);
  172. Memo.Layout.Left := 85;
  173. Memo.Layout.Top := 2;
  174. Memo.Layout.Width := 25;
  175. Memo.Layout.Height := 5;
  176. Memo.Text := '[2 + RecNo - 6]';
  177. Memo.Options := [moHideZeros];
  178. Memo := TFPReportMemo.Create(DataBand);
  179. Memo.Layout.Left := 120;
  180. Memo.Layout.Top := 2;
  181. Memo.Layout.Width := 25;
  182. Memo.Layout.Height := 5;
  183. Memo.Text := 'RecNo = [RecNo].';
  184. PageFooter := TFPReportPageFooterBand.Create(p);
  185. PageFooter.Layout.Height := 20;
  186. {$ifdef ColorBands}
  187. PageFooter.Frame.Shape := fsRectangle;
  188. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  189. {$endif}
  190. Memo := TFPReportMemo.Create(PageFooter);
  191. Memo.Layout.Left := 135;
  192. Memo.Layout.Top := 13;
  193. Memo.Layout.Width := 20;
  194. Memo.Layout.Height := 5;
  195. Memo.Text := 'Page [PageNo]';
  196. Memo := TFPReportMemo.Create(PageFooter);
  197. Memo.Layout.Left := 0;
  198. Memo.Layout.Top := 0;
  199. Memo.Layout.Width := 30;
  200. Memo.Layout.Height := 5;
  201. Memo.Text := '1 + 2 = [1 + 2].';
  202. end;
  203. constructor TExpressionsDemo.Create(AOwner: TComponent);
  204. begin
  205. Inherited;
  206. FReportData := TFPReportUserData.Create(self);
  207. FReportData.OnGetValue := @GetReportDataValue;
  208. FReportData.OnGetEOF := @GetReportDataEOF;
  209. FReportData.OnFirst := @GetReportDataFirst;
  210. FReportData.OnGetNames := @GetReportFieldNames;
  211. end;
  212. destructor TExpressionsDemo.Destroy;
  213. begin
  214. FreeAndNil(FReportData);
  215. FreeAndNil(sl);
  216. inherited Destroy;
  217. end;
  218. class function TExpressionsDemo.Description: string;
  219. begin
  220. Result:='Demo with expressions in Memos';
  221. end;
  222. end.