TableRow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using FastReport.Data;
  7. using FastReport.Utils;
  8. namespace FastReport.Table
  9. {
  10. /// <summary>
  11. /// Represents a table row.
  12. /// </summary>
  13. /// <remarks>
  14. /// Use the <see cref="Height"/> property to set the height of a row. If <see cref="AutoSize"/>
  15. /// property is <b>true</b>, the row will calculate its height automatically.
  16. /// <para/>You can also set the <see cref="MinHeight"/> and <see cref="MaxHeight"/> properties
  17. /// to restrict the row's height.
  18. /// </remarks>
  19. public partial class TableRow : ComponentBase, IParent
  20. {
  21. #region Fields
  22. private List<TableCellData> cells;
  23. private float minHeight;
  24. private float maxHeight;
  25. private bool autoSize;
  26. private bool canBreak;
  27. private bool pageBreak;
  28. private int keepRows;
  29. private int index;
  30. private float saveHeight;
  31. private bool saveVisible;
  32. private bool serializingToPreview;
  33. #endregion
  34. #region Properties
  35. /// <summary>
  36. /// Gets or sets a height of the row, in pixels.
  37. /// </summary>
  38. /// <remarks>
  39. /// The row height cannot exceed the range defined by the <see cref="MinHeight"/>
  40. /// and <see cref="MaxHeight"/> properties.
  41. /// <note>To convert between pixels and report units, use the constants defined
  42. /// in the <see cref="Units"/> class.</note>
  43. /// </remarks>
  44. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  45. public override float Height
  46. {
  47. get { return base.Height; }
  48. set
  49. {
  50. value = Converter.DecreasePrecision(value, 2);
  51. if (value > MaxHeight && !canBreak)
  52. value = MaxHeight;
  53. if (value < MinHeight)
  54. value = MinHeight;
  55. base.Height = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets or sets the minimal height for this row, in pixels.
  60. /// </summary>
  61. [DefaultValue(0f)]
  62. [Category("Layout")]
  63. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  64. public float MinHeight
  65. {
  66. get { return minHeight; }
  67. set { minHeight = value; }
  68. }
  69. /// <summary>
  70. /// Gets or sets the maximal height for this row, in pixels.
  71. /// </summary>
  72. [DefaultValue(1000f)]
  73. [Category("Layout")]
  74. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  75. public float MaxHeight
  76. {
  77. get { return maxHeight; }
  78. set { maxHeight = value; }
  79. }
  80. /// <summary>
  81. /// Gets or sets a value determines if the row should calculate its height automatically.
  82. /// </summary>
  83. /// <remarks>
  84. /// The row height cannot exceed the range defined by the <see cref="MinHeight"/>
  85. /// and <see cref="MaxHeight"/> properties.
  86. /// </remarks>
  87. [DefaultValue(false)]
  88. [Category("Behavior")]
  89. public bool AutoSize
  90. {
  91. get { return autoSize; }
  92. set { autoSize = value; }
  93. }
  94. /// <summary>
  95. /// Gets or sets a value that determines if the component can break its contents across pages.
  96. /// </summary>
  97. [DefaultValue(false)]
  98. [Category("Behavior")]
  99. public bool CanBreak
  100. {
  101. get { return canBreak; }
  102. set { canBreak = value; }
  103. }
  104. /// <summary>
  105. /// Gets the index of this row.
  106. /// </summary>
  107. [Browsable(false)]
  108. public int Index
  109. {
  110. get { return index; }
  111. }
  112. /// <inheritdoc/>
  113. [Browsable(false)]
  114. public override float Top
  115. {
  116. get
  117. {
  118. TableBase table = Parent as TableBase;
  119. if (table == null)
  120. return 0;
  121. float result = 0;
  122. for (int i = 0; i < Index; i++)
  123. {
  124. result += table.Rows[i].Height;
  125. }
  126. return result;
  127. }
  128. set { base.Top = value; }
  129. }
  130. /// <summary>
  131. /// Gets or sets the cell with specified index.
  132. /// </summary>
  133. /// <param name="col">Column index.</param>
  134. /// <returns>The <b>TableCell</b> object.</returns>
  135. [Browsable(false)]
  136. public TableCell this[int col]
  137. {
  138. get
  139. {
  140. TableCellData cellData = CellData(col);
  141. TableCell cell = cellData.Cell;
  142. cell.SetParent(this);
  143. cell.SetValue(cellData.Value);
  144. return cell;
  145. }
  146. set
  147. {
  148. TableCellData cellData = CellData(col);
  149. cellData.AttachCell(value);
  150. }
  151. }
  152. /// <summary>
  153. /// Gets or sets the page break flag for this row.
  154. /// </summary>
  155. [Browsable(false)]
  156. public bool PageBreak
  157. {
  158. get { return pageBreak; }
  159. set { pageBreak = value; }
  160. }
  161. /// <summary>
  162. /// Gets or sets the number of rows to keep on the same page.
  163. /// </summary>
  164. [Browsable(false)]
  165. public int KeepRows
  166. {
  167. get { return keepRows; }
  168. set { keepRows = value; }
  169. }
  170. internal static float DefaultHeight
  171. {
  172. get { return (int)Math.Round(18 / (0.25f * Units.Centimeters)) * (0.25f * Units.Centimeters); }
  173. }
  174. #endregion
  175. #region IParent Members
  176. /// <inheritdoc/>
  177. public bool CanContain(Base child)
  178. {
  179. return child is TableCell;
  180. }
  181. /// <inheritdoc/>
  182. public void GetChildObjects(ObjectCollection list)
  183. {
  184. TableBase table = Parent as TableBase;
  185. if (table == null)
  186. return;
  187. for (int i = 0; i < table.Columns.Count; i++)
  188. {
  189. if (!serializingToPreview || table.Columns[i].Visible)
  190. list.Add(this[i]);
  191. }
  192. }
  193. /// <inheritdoc/>
  194. public void AddChild(Base child)
  195. {
  196. // support deserializing the cells
  197. if (child is TableCell)
  198. {
  199. this[cells.Count] = child as TableCell;
  200. child.SetParent(this);
  201. }
  202. }
  203. /// <inheritdoc/>
  204. public void RemoveChild(Base child)
  205. {
  206. }
  207. private TableCellData FindCellData(TableCell cell)
  208. {
  209. foreach (TableCellData cellData in cells)
  210. {
  211. if (cellData.Cell == cell)
  212. return cellData;
  213. }
  214. return null;
  215. }
  216. /// <inheritdoc/>
  217. public int GetChildOrder(Base child)
  218. {
  219. TableCellData cellData = FindCellData(child as TableCell);
  220. return cellData == null ? 0 : cells.IndexOf(cellData);
  221. }
  222. /// <inheritdoc/>
  223. public void SetChildOrder(Base child, int order)
  224. {
  225. TableCellData cellData = FindCellData(child as TableCell);
  226. if (cellData == null)
  227. return;
  228. int oldOrder = child.ZOrder;
  229. if (oldOrder != -1 && order != -1 && oldOrder != order)
  230. {
  231. if (order > cells.Count)
  232. order = cells.Count;
  233. if (oldOrder <= order)
  234. order--;
  235. cells.Remove(cellData);
  236. cells.Insert(order, cellData);
  237. }
  238. }
  239. /// <inheritdoc/>
  240. public void UpdateLayout(float dx, float dy)
  241. {
  242. TableBase table = Parent as TableBase;
  243. if (table == null)
  244. return;
  245. // update this row cells
  246. for (int i = 0; i < table.Columns.Count; i++)
  247. {
  248. this.CellData(i).UpdateLayout(dx, dy);
  249. }
  250. // update spanned cells that contains this row
  251. List<Rectangle> spanList = table.GetSpanList();
  252. foreach (Rectangle span in spanList)
  253. {
  254. if (Index > span.Top && Index < span.Bottom)
  255. table[span.Left, span.Top].CellData.UpdateLayout(dx, dy);
  256. }
  257. }
  258. #endregion
  259. #region Public Methods
  260. /// <inheritdoc/>
  261. public override void Assign(Base source)
  262. {
  263. TableRow src = source as TableRow;
  264. MinHeight = src.MinHeight;
  265. MaxHeight = src.MaxHeight;
  266. AutoSize = src.AutoSize;
  267. KeepRows = src.KeepRows;
  268. CanBreak = src.CanBreak;
  269. base.Assign(source);
  270. }
  271. internal TableCellData CellData(int col)
  272. {
  273. while (col >= cells.Count)
  274. {
  275. cells.Add(new TableCellData());
  276. }
  277. TableCellData cellData = cells[col];
  278. cellData.Table = Parent as TableBase;
  279. cellData.Address = new Point(col, Index);
  280. return cellData;
  281. }
  282. internal void CorrectCellsOnColumnChange(int index, int correct)
  283. {
  284. if (correct == 1)
  285. cells.Insert(index, new TableCellData());
  286. else if (index < cells.Count)
  287. cells.RemoveAt(index);
  288. }
  289. internal void SetIndex(int value)
  290. {
  291. index = value;
  292. }
  293. /// <inheritdoc/>
  294. public override void Serialize(FRWriter writer)
  295. {
  296. TableRow c = writer.DiffObject as TableRow;
  297. serializingToPreview = writer.SerializeTo == SerializeTo.Preview;
  298. base.Serialize(writer);
  299. if (FloatDiff(MinHeight, c.MinHeight))
  300. writer.WriteFloat("MinHeight", MinHeight);
  301. if (FloatDiff(MaxHeight, c.MaxHeight))
  302. writer.WriteFloat("MaxHeight", MaxHeight);
  303. if (FloatDiff(Height, c.Height))
  304. writer.WriteFloat("Height", Height);
  305. if (AutoSize != c.AutoSize)
  306. writer.WriteBool("AutoSize", AutoSize);
  307. if (CanBreak != c.CanBreak)
  308. writer.WriteBool("CanBreak", CanBreak);
  309. if (Parent is TableResult)
  310. {
  311. // write children by itself
  312. SetFlags(Flags.CanWriteChildren, true);
  313. writer.SaveChildren = true;
  314. TableResult table = Parent as TableResult;
  315. foreach (TableColumn column in table.ColumnsToSerialize)
  316. {
  317. TableCell cell = this[column.Index];
  318. writer.Write(cell);
  319. }
  320. }
  321. }
  322. /// <inheritdoc/>
  323. public override void Clear()
  324. {
  325. base.Clear();
  326. foreach (TableCellData cell in cells)
  327. {
  328. cell.Dispose();
  329. }
  330. cells.Clear();
  331. }
  332. internal void SaveState()
  333. {
  334. saveHeight = Height;
  335. saveVisible = Visible;
  336. }
  337. internal void RestoreState()
  338. {
  339. Height = saveHeight;
  340. Visible = saveVisible;
  341. }
  342. #endregion
  343. /// <summary>
  344. /// Initializes a new instance of the <see cref="TableRow"/> class.
  345. /// </summary>
  346. public TableRow()
  347. {
  348. cells = new List<TableCellData>();
  349. maxHeight = 1000;
  350. Height = DefaultHeight;
  351. SetFlags(Flags.CanCopy | Flags.CanDelete | Flags.CanWriteBounds, false);
  352. BaseName = "Row";
  353. }
  354. }
  355. }