TableObject.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 Private Methods
  213. partial void InitTag();
  214. #endregion
  215. #region Public Methods
  216. /// <inheritdoc/>
  217. public override void Assign(Base source)
  218. {
  219. base.Assign(source);
  220. TableObject src = source as TableObject;
  221. ManualBuildEvent = src.ManualBuildEvent;
  222. ManualBuildAutoSpans = src.ManualBuildAutoSpans;
  223. }
  224. /// <inheritdoc/>
  225. public override void Serialize(FRWriter writer)
  226. {
  227. TableObject c = writer.DiffObject as TableObject;
  228. base.Serialize(writer);
  229. if (ManualBuildEvent != c.ManualBuildEvent)
  230. writer.WriteStr("ManualBuildEvent", ManualBuildEvent);
  231. if (ManualBuildAutoSpans != c.ManualBuildAutoSpans)
  232. writer.WriteBool("ManualBuildAutoSpans", ManualBuildAutoSpans);
  233. }
  234. #endregion
  235. #region Report Engine
  236. private string GetFunctionCode(string function)
  237. {
  238. string result = "";
  239. switch (Report.ScriptLanguage)
  240. {
  241. case Language.CSharp:
  242. result =
  243. " private object " + function + "(TableCell cell)\r\n" +
  244. " {\r\n" +
  245. " return cell.Table." + function + "(cell);\r\n" +
  246. " }\r\n\r\n";
  247. break;
  248. case Language.Vb:
  249. result =
  250. " Private Function " + function + "(ByVal cell As TableCell) As Object\r\n" +
  251. " Return cell.Table." + function + "(cell)\r\n" +
  252. " End Function\r\n\r\n";
  253. break;
  254. }
  255. return result;
  256. }
  257. /// <inheritdoc/>
  258. public override string GetCustomScript()
  259. {
  260. string result = "";
  261. string[] functions = new string[] { "Sum", "Min", "Max", "Avg", "Count" };
  262. foreach (string function in functions)
  263. {
  264. result += GetFunctionCode(function);
  265. }
  266. return result;
  267. }
  268. /// <inheritdoc/>
  269. public override void SaveState()
  270. {
  271. saveVisible = Visible;
  272. BandBase parent = Parent as BandBase;
  273. saveStateSkipped = !Visible || (parent != null && !parent.Visible);
  274. if (saveStateSkipped)
  275. return;
  276. if (!IsManualBuild)
  277. {
  278. base.SaveState();
  279. }
  280. else
  281. {
  282. // create the result table that will be rendered in the preview
  283. SetResultTable(new TableResult());
  284. ResultTable.Assign(this);
  285. ResultTable.SetReport(Report);
  286. helper = new TableHelper(this, ResultTable);
  287. Visible = false;
  288. if (parent != null && !PrintOnParent)
  289. {
  290. parent.Height = Top;
  291. parent.CanGrow = false;
  292. parent.CanShrink = false;
  293. parent.AfterPrint += ResultTable.GeneratePages;
  294. }
  295. OnManualBuild(EventArgs.Empty);
  296. }
  297. }
  298. /// <inheritdoc/>
  299. public override void RestoreState()
  300. {
  301. BandBase parent = Parent as BandBase;
  302. // SaveState was skipped, there is nothing to restore
  303. if (saveStateSkipped)
  304. return;
  305. if (!IsManualBuild)
  306. {
  307. base.RestoreState();
  308. }
  309. else
  310. {
  311. if (parent != null && !PrintOnParent)
  312. parent.AfterPrint -= ResultTable.GeneratePages;
  313. helper = null;
  314. ResultTable.Dispose();
  315. SetResultTable(null);
  316. Visible = saveVisible;
  317. }
  318. }
  319. /// <inheritdoc/>
  320. public override void GetData()
  321. {
  322. base.GetData();
  323. if (!IsManualBuild)
  324. {
  325. for (int y = 0; y < Rows.Count; y++)
  326. {
  327. for (int x = 0; x < Columns.Count; x++)
  328. {
  329. this[x, y].GetData();
  330. }
  331. }
  332. }
  333. }
  334. /// <inheritdoc/>
  335. public override void OnAfterData(EventArgs e)
  336. {
  337. base.OnAfterData(e);
  338. if (IsManualBuild && PrintOnParent)
  339. ResultTable.AddToParent(Parent);
  340. }
  341. /// <summary>
  342. /// This method fires the <b>ManualBuild</b> event and the script code connected to the <b>ManualBuildEvent</b>.
  343. /// </summary>
  344. /// <param name="e">Event data.</param>
  345. public void OnManualBuild(EventArgs e)
  346. {
  347. if (ManualBuild != null)
  348. ManualBuild(this, e);
  349. InvokeEvent(ManualBuildEvent, e);
  350. }
  351. /// <summary>
  352. /// Prints a row with specified index.
  353. /// </summary>
  354. /// <param name="index">Index of a row to print.</param>
  355. /// <remarks>
  356. /// See the <see cref="ManualBuild"/> event for more details.
  357. /// </remarks>
  358. public void PrintRow(int index)
  359. {
  360. if (!IsManualBuild)
  361. throw new TableManualBuildException();
  362. helper.PrintRow(index);
  363. }
  364. /// <summary>
  365. /// Prints rows with specified indices.
  366. /// </summary>
  367. /// <param name="indices">Indices of rows to print.</param>
  368. /// <remarks>
  369. /// See the <see cref="ManualBuild"/> event for more details.
  370. /// </remarks>
  371. public void PrintRows(int[] indices)
  372. {
  373. foreach (int index in indices)
  374. {
  375. PrintRow(index);
  376. }
  377. }
  378. /// <summary>
  379. /// Prints all rows.
  380. /// </summary>
  381. /// <remarks>
  382. /// See the <see cref="ManualBuild"/> event for more details.
  383. /// </remarks>
  384. public void PrintRows()
  385. {
  386. for (int i = 0; i < Rows.Count; i++)
  387. {
  388. PrintRow(i);
  389. }
  390. }
  391. /// <summary>
  392. /// Prints a column with specified index.
  393. /// </summary>
  394. /// <param name="index">Index of a column to print.</param>
  395. /// <remarks>
  396. /// See the <see cref="ManualBuild"/> event for more details.
  397. /// </remarks>
  398. public void PrintColumn(int index)
  399. {
  400. if (!IsManualBuild)
  401. throw new TableManualBuildException();
  402. helper.PrintColumn(index);
  403. }
  404. /// <summary>
  405. /// Prints columns with specified indices.
  406. /// </summary>
  407. /// <param name="indices">Indices of columns to print.</param>
  408. /// <remarks>
  409. /// See the <see cref="ManualBuild"/> event for more details.
  410. /// </remarks>
  411. public void PrintColumns(int[] indices)
  412. {
  413. foreach (int index in indices)
  414. {
  415. PrintColumn(index);
  416. }
  417. }
  418. /// <summary>
  419. /// Prints all columns.
  420. /// </summary>
  421. /// <remarks>
  422. /// See the <see cref="ManualBuild"/> event for more details.
  423. /// </remarks>
  424. public void PrintColumns()
  425. {
  426. for (int i = 0; i < Columns.Count; i++)
  427. {
  428. PrintColumn(i);
  429. }
  430. }
  431. /// <summary>
  432. /// Adds a page before rows or columns.
  433. /// </summary>
  434. /// <remarks>
  435. /// Call this method to insert a page break before the next row or column that you intend to print
  436. /// using <b>PrintRow(s)</b> or <b>PrintColumn(s)</b> methods.
  437. /// See the <see cref="ManualBuild"/> event for more details.
  438. /// </remarks>
  439. public void PageBreak()
  440. {
  441. if (!IsManualBuild)
  442. throw new TableManualBuildException();
  443. if (!Report.Engine.UnlimitedHeight && !Report.Engine.UnlimitedWidth)
  444. helper.PageBreak();
  445. }
  446. #endregion
  447. /// <summary>
  448. /// Initializes a new instance of the <see cref="TableObject"/> class.
  449. /// </summary>
  450. public TableObject()
  451. {
  452. manualBuildEvent = "";
  453. manualBuildAutoSpans = true;
  454. InitTag();
  455. }
  456. }
  457. }