TableObject.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.Drawing.Drawing2D;
  8. using FastReport.Utils;
  9. using FastReport.Data;
  10. namespace FastReport.Table
  11. {
  12. /// <summary>
  13. /// Represents a table object that consists of several rows and columns.
  14. /// </summary>
  15. /// <remarks>
  16. /// <para/>To add/remove columns, use the <see cref="TableBase.Columns"/> collection. To add/remove
  17. /// rows, use the <see cref="TableBase.Rows"/> collection. To initialize a table with specified number of
  18. /// columns and rows, use <see cref="ColumnCount"/> and <see cref="RowCount"/> properties.
  19. /// <para/>To print a table in code, use the <see cref="ManualBuild"/> event. In the manual build
  20. /// mode, you can use aggregate functions. The following functions available:
  21. /// <list type="table">
  22. /// <listheader>
  23. /// <term>Aggregate function</term>
  24. /// <description>Description</description>
  25. /// </listheader>
  26. /// <item>
  27. /// <term>Sum(cell)</term>
  28. /// <description>Calculates the sum of values in specified table cell.</description>
  29. /// </item>
  30. /// <item>
  31. /// <term>Min(cell)</term>
  32. /// <description>Calculates the minimum of values in specified table cell.</description>
  33. /// </item>
  34. /// <item>
  35. /// <term>Max(cell)</term>
  36. /// <description>Calculates the maximum of values in specified table cell.</description>
  37. /// </item>
  38. /// <item>
  39. /// <term>Avg(cell)</term>
  40. /// <description>Calculates the average of values in specified table cell.</description>
  41. /// </item>
  42. /// <item>
  43. /// <term>Count(cell)</term>
  44. /// <description>Calculates the number of repeats of a specified table cell.</description>
  45. /// </item>
  46. /// </list>
  47. /// <para/>To print aggregate value, place the aggregate function call in the table cell:
  48. /// <c>[Count(Cell2)]</c>.
  49. /// </remarks>
  50. public partial class TableObject : TableBase
  51. {
  52. #region Fields
  53. private string manualBuildEvent;
  54. private TableHelper helper;
  55. private bool saveVisible;
  56. private bool saveStateSkipped;
  57. private bool manualBuildAutoSpans;
  58. #endregion
  59. #region Properties
  60. /// <summary>
  61. /// Allows to print table rows/columns dynamically.
  62. /// </summary>
  63. /// <remarks>
  64. /// This event is used to handle the table print process in a code. Using special methods
  65. /// like <see cref="PrintRow"/>, <see cref="PrintColumn"/> you can print specified rows/columns.
  66. ///
  67. /// <para/>First way is to repeat specified row(s) to get a table that will grow downwards.
  68. /// To do this, you have to call the <b>PrintRow</b> method followed by the <b>PrintColumns</b> method.
  69. ///
  70. /// <para/>Another way is to repeat the specified column(s) to get a table that grows sidewards.
  71. /// To do this, call the <b>PrintColumn</b> method followed by the <b>PrintRows</b> method.
  72. ///
  73. /// <para/>Finally, the third way is to repeat rows and columns. The table will grow downwards and
  74. /// sidewards. To do this, call the <b>PrintRow</b> method followed by the <b>PrintColumn</b>
  75. /// method (or vice versa).
  76. ///
  77. /// <para/>
  78. /// <note type="caution">
  79. /// When you print a table row-by-row, you must call one of the <b>PrintColumn</b>,
  80. /// <b>PrintColumns</b> methods right after the <b>PrintRow</b> method.
  81. /// In the same manner, when you print a table column-by-column, call one of the
  82. /// <b>PrintRow</b>, <b>PrintRows</b> methods right after the <b>PrintColumn</b> method.
  83. /// If you ignore this rule you will get an exception.
  84. /// </note>
  85. /// </remarks>
  86. /// <example>
  87. /// In this example, we will consider all three ways to print a table which has 3 rows and 3 columns.
  88. /// <para/>Case 1: print a table downwards.
  89. /// <code>
  90. /// // print table header (the first row)
  91. /// Table1.PrintRow(0);
  92. /// Table1.PrintColumns();
  93. /// // print table body (the second row)
  94. /// for (int i = 0; i &lt; 10; i++)
  95. /// {
  96. /// Table1.PrintRow(1);
  97. /// Table1.PrintColumns();
  98. /// }
  99. /// // print table footer (the third row)
  100. /// Table1.PrintRow(2);
  101. /// Table1.PrintColumns();
  102. /// </code>
  103. ///
  104. /// <para/>Case 2: print a table sidewards.
  105. /// <code>
  106. /// // print table header (the first column)
  107. /// Table1.PrintColumn(0);
  108. /// Table1.PrintRows();
  109. /// // print table body (the second column)
  110. /// for (int i = 0; i &lt; 10; i++)
  111. /// {
  112. /// Table1.PrintColumn(1);
  113. /// Table1.PrintRows();
  114. /// }
  115. /// // print table footer (the third column)
  116. /// Table1.PrintColumn(2);
  117. /// Table1.PrintRows();
  118. /// </code>
  119. ///
  120. /// <para/>Case 3: print a table downwards and sidewards.
  121. /// <code>
  122. /// // print the first row with all its columns
  123. /// Table1.PrintRow(0);
  124. /// // print header column
  125. /// Table1.PrintColumn(0);
  126. /// // print 10 data columns
  127. /// for (int i = 0; i &lt; 10; i++)
  128. /// {
  129. /// Table1.PrintColumn(1);
  130. /// }
  131. /// // print footer column
  132. /// Table1.PrintColumn(2);
  133. ///
  134. /// // print table body (the second row)
  135. /// for (int i = 0; i &lt; 10; i++)
  136. /// {
  137. /// // print data row with all its columns
  138. /// Table1.PrintRow(1);
  139. /// Table1.PrintColumn(0);
  140. /// for (int j = 0; j &lt; 10; j++)
  141. /// {
  142. /// Table1.PrintColumn(1);
  143. /// }
  144. /// Table1.PrintColumn(2);
  145. /// }
  146. ///
  147. /// // print table footer (the third row)
  148. /// Table1.PrintRow(2);
  149. /// // again print all columns in the table footer
  150. /// Table1.PrintColumn(0);
  151. /// for (int i = 0; i &lt; 10; i++)
  152. /// {
  153. /// Table1.PrintColumn(1);
  154. /// }
  155. /// Table1.PrintColumn(2);
  156. /// </code>
  157. /// </example>
  158. public event EventHandler ManualBuild;
  159. /// <summary>
  160. /// Gets or sets a script method name that will be used to handle the
  161. /// <see cref="ManualBuild"/> event.
  162. /// </summary>
  163. /// <remarks>
  164. /// If you use this event, you must handle the table print process manually.
  165. /// See the <see cref="ManualBuild"/> event for details.
  166. /// </remarks>
  167. [Category("Build")]
  168. public string ManualBuildEvent
  169. {
  170. get { return manualBuildEvent; }
  171. set { manualBuildEvent = value; }
  172. }
  173. /// <summary>
  174. /// Determines whether to manage cell spans automatically during manual build.
  175. /// </summary>
  176. /// <remarks>
  177. /// The default value for this property is <b>true</b>. If you set it to <b>false</b>, you need to manage
  178. /// spans in your ManualBuild event handler.
  179. /// </remarks>
  180. [Category("Build")]
  181. [DefaultValue(true)]
  182. public bool ManualBuildAutoSpans
  183. {
  184. get { return manualBuildAutoSpans; }
  185. set { manualBuildAutoSpans = value; }
  186. }
  187. /// <inheritdoc/>
  188. public override int ColumnCount
  189. {
  190. get { return base.ColumnCount; }
  191. set
  192. {
  193. base.ColumnCount = value;
  194. CreateUniqueNames();
  195. }
  196. }
  197. /// <inheritdoc/>
  198. public override int RowCount
  199. {
  200. get { return base.RowCount; }
  201. set
  202. {
  203. base.RowCount = value;
  204. CreateUniqueNames();
  205. }
  206. }
  207. internal bool IsManualBuild
  208. {
  209. get { return !String.IsNullOrEmpty(ManualBuildEvent) || ManualBuild != null; }
  210. }
  211. #endregion
  212. #region Public Methods
  213. /// <inheritdoc/>
  214. public override void Assign(Base source)
  215. {
  216. base.Assign(source);
  217. TableObject src = source as TableObject;
  218. ManualBuildEvent = src.ManualBuildEvent;
  219. ManualBuildAutoSpans = src.ManualBuildAutoSpans;
  220. }
  221. /// <inheritdoc/>
  222. public override void Serialize(FRWriter writer)
  223. {
  224. TableObject c = writer.DiffObject as TableObject;
  225. base.Serialize(writer);
  226. if (ManualBuildEvent != c.ManualBuildEvent)
  227. writer.WriteStr("ManualBuildEvent", ManualBuildEvent);
  228. if (ManualBuildAutoSpans != c.ManualBuildAutoSpans)
  229. writer.WriteBool("ManualBuildAutoSpans", ManualBuildAutoSpans);
  230. }
  231. #endregion
  232. #region Report Engine
  233. private string GetFunctionCode(string function)
  234. {
  235. string result = "";
  236. switch (Report.ScriptLanguage)
  237. {
  238. case Language.CSharp:
  239. result =
  240. " private object " + function + "(TableCell cell)\r\n" +
  241. " {\r\n" +
  242. " return cell.Table." + function + "(cell);\r\n" +
  243. " }\r\n\r\n";
  244. break;
  245. case Language.Vb:
  246. result =
  247. " Private Function " + function + "(ByVal cell As TableCell) As Object\r\n" +
  248. " Return cell.Table." + function + "(cell)\r\n" +
  249. " End Function\r\n\r\n";
  250. break;
  251. }
  252. return result;
  253. }
  254. /// <inheritdoc/>
  255. public override string GetCustomScript()
  256. {
  257. string result = "";
  258. string[] functions = new string[] { "Sum", "Min", "Max", "Avg", "Count" };
  259. foreach (string function in functions)
  260. {
  261. result += GetFunctionCode(function);
  262. }
  263. return result;
  264. }
  265. /// <inheritdoc/>
  266. public override void SaveState()
  267. {
  268. saveVisible = Visible;
  269. BandBase parent = Parent as BandBase;
  270. saveStateSkipped = !Visible || (parent != null && !parent.Visible);
  271. if (saveStateSkipped)
  272. return;
  273. if (!IsManualBuild)
  274. {
  275. base.SaveState();
  276. }
  277. else
  278. {
  279. // create the result table that will be rendered in the preview
  280. SetResultTable(new TableResult());
  281. ResultTable.Assign(this);
  282. ResultTable.SetReport(Report);
  283. helper = new TableHelper(this, ResultTable);
  284. Visible = false;
  285. if (parent != null && !PrintOnParent)
  286. {
  287. parent.Height = Top;
  288. parent.CanGrow = false;
  289. parent.CanShrink = false;
  290. parent.AfterPrint += ResultTable.GeneratePages;
  291. }
  292. OnManualBuild(EventArgs.Empty);
  293. }
  294. }
  295. /// <inheritdoc/>
  296. public override void RestoreState()
  297. {
  298. BandBase parent = Parent as BandBase;
  299. // SaveState was skipped, there is nothing to restore
  300. if (saveStateSkipped)
  301. return;
  302. if (!IsManualBuild)
  303. {
  304. base.RestoreState();
  305. }
  306. else
  307. {
  308. if (parent != null && !PrintOnParent)
  309. parent.AfterPrint -= ResultTable.GeneratePages;
  310. helper = null;
  311. ResultTable.Dispose();
  312. SetResultTable(null);
  313. Visible = saveVisible;
  314. }
  315. }
  316. /// <inheritdoc/>
  317. public override void GetData()
  318. {
  319. base.GetData();
  320. if (!IsManualBuild)
  321. {
  322. for (int y = 0; y < Rows.Count; y++)
  323. {
  324. for (int x = 0; x < Columns.Count; x++)
  325. {
  326. this[x, y].GetData();
  327. }
  328. }
  329. }
  330. }
  331. /// <inheritdoc/>
  332. public override void OnAfterData(EventArgs e)
  333. {
  334. base.OnAfterData(e);
  335. if (IsManualBuild && PrintOnParent)
  336. ResultTable.AddToParent(Parent);
  337. }
  338. /// <summary>
  339. /// This method fires the <b>ManualBuild</b> event and the script code connected to the <b>ManualBuildEvent</b>.
  340. /// </summary>
  341. /// <param name="e">Event data.</param>
  342. public void OnManualBuild(EventArgs e)
  343. {
  344. if (ManualBuild != null)
  345. ManualBuild(this, e);
  346. InvokeEvent(ManualBuildEvent, e);
  347. }
  348. /// <summary>
  349. /// Prints a row with specified index.
  350. /// </summary>
  351. /// <param name="index">Index of a row to print.</param>
  352. /// <remarks>
  353. /// See the <see cref="ManualBuild"/> event for more details.
  354. /// </remarks>
  355. public void PrintRow(int index)
  356. {
  357. if (!IsManualBuild)
  358. throw new TableManualBuildException();
  359. helper.PrintRow(index);
  360. }
  361. /// <summary>
  362. /// Prints rows with specified indices.
  363. /// </summary>
  364. /// <param name="indices">Indices of rows to print.</param>
  365. /// <remarks>
  366. /// See the <see cref="ManualBuild"/> event for more details.
  367. /// </remarks>
  368. public void PrintRows(int[] indices)
  369. {
  370. foreach (int index in indices)
  371. {
  372. PrintRow(index);
  373. }
  374. }
  375. /// <summary>
  376. /// Prints all rows.
  377. /// </summary>
  378. /// <remarks>
  379. /// See the <see cref="ManualBuild"/> event for more details.
  380. /// </remarks>
  381. public void PrintRows()
  382. {
  383. for (int i = 0; i < Rows.Count; i++)
  384. {
  385. PrintRow(i);
  386. }
  387. }
  388. /// <summary>
  389. /// Prints a column with specified index.
  390. /// </summary>
  391. /// <param name="index">Index of a column to print.</param>
  392. /// <remarks>
  393. /// See the <see cref="ManualBuild"/> event for more details.
  394. /// </remarks>
  395. public void PrintColumn(int index)
  396. {
  397. if (!IsManualBuild)
  398. throw new TableManualBuildException();
  399. helper.PrintColumn(index);
  400. }
  401. /// <summary>
  402. /// Prints columns with specified indices.
  403. /// </summary>
  404. /// <param name="indices">Indices of columns to print.</param>
  405. /// <remarks>
  406. /// See the <see cref="ManualBuild"/> event for more details.
  407. /// </remarks>
  408. public void PrintColumns(int[] indices)
  409. {
  410. foreach (int index in indices)
  411. {
  412. PrintColumn(index);
  413. }
  414. }
  415. /// <summary>
  416. /// Prints all columns.
  417. /// </summary>
  418. /// <remarks>
  419. /// See the <see cref="ManualBuild"/> event for more details.
  420. /// </remarks>
  421. public void PrintColumns()
  422. {
  423. for (int i = 0; i < Columns.Count; i++)
  424. {
  425. PrintColumn(i);
  426. }
  427. }
  428. /// <summary>
  429. /// Adds a page before rows or columns.
  430. /// </summary>
  431. /// <remarks>
  432. /// Call this method to insert a page break before the next row or column that you intend to print
  433. /// using <b>PrintRow(s)</b> or <b>PrintColumn(s)</b> methods.
  434. /// See the <see cref="ManualBuild"/> event for more details.
  435. /// </remarks>
  436. public void PageBreak()
  437. {
  438. if (!IsManualBuild)
  439. throw new TableManualBuildException();
  440. if (!Report.Engine.UnlimitedHeight && !Report.Engine.UnlimitedWidth)
  441. helper.PageBreak();
  442. }
  443. #endregion
  444. /// <summary>
  445. /// Initializes a new instance of the <see cref="TableObject"/> class.
  446. /// </summary>
  447. public TableObject()
  448. {
  449. manualBuildEvent = "";
  450. manualBuildAutoSpans = true;
  451. }
  452. }
  453. }