TableColumn.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 column.
  12. /// </summary>
  13. /// <remarks>
  14. /// Use the <see cref="Width"/> property to set the width of a column. If <see cref="AutoSize"/>
  15. /// property is <b>true</b>, the column will calculate its width automatically.
  16. /// <para/>You can also set the <see cref="MinWidth"/> and <see cref="MaxWidth"/> properties
  17. /// to restrict the column's width.
  18. /// </remarks>
  19. public partial class TableColumn : ComponentBase
  20. {
  21. #region Fields
  22. private float minWidth;
  23. private float maxWidth;
  24. private bool autoSize;
  25. private bool pageBreak;
  26. private int keepColumns;
  27. private int index;
  28. private float saveWidth;
  29. private bool saveVisible;
  30. private float minimumBreakWidth;
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// Gets or sets a width of the column, in pixels.
  35. /// </summary>
  36. /// <remarks>
  37. /// The column width cannot exceed the range defined by the <see cref="MinWidth"/>
  38. /// and <see cref="MaxWidth"/> properties.
  39. /// <note>To convert between pixels and report units, use the constants defined
  40. /// in the <see cref="Units"/> class.</note>
  41. /// </remarks>
  42. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  43. public override float Width
  44. {
  45. get { return base.Width; }
  46. set
  47. {
  48. value = Converter.DecreasePrecision(value, 2);
  49. if (value > MaxWidth)
  50. value = MaxWidth;
  51. if (value < MinWidth)
  52. value = MinWidth;
  53. if (FloatDiff(base.Width, value))
  54. {
  55. UpdateLayout(value - base.Width);
  56. base.Width = value;
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the minimal width for this column, in pixels.
  62. /// </summary>
  63. [DefaultValue(0f)]
  64. [Category("Layout")]
  65. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  66. public float MinWidth
  67. {
  68. get { return minWidth; }
  69. set { minWidth = value; }
  70. }
  71. /// <summary>
  72. /// Gets or sets the maximal width for this column, in pixels.
  73. /// </summary>
  74. [DefaultValue(5000f)]
  75. [Category("Layout")]
  76. [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
  77. public float MaxWidth
  78. {
  79. get { return maxWidth; }
  80. set { maxWidth = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets a value determines if the column should calculate its width automatically.
  84. /// </summary>
  85. /// <remarks>
  86. /// The column width cannot exceed the range defined by the <see cref="MinWidth"/>
  87. /// and <see cref="MaxWidth"/> properties.
  88. /// </remarks>
  89. [DefaultValue(false)]
  90. [Category("Behavior")]
  91. public bool AutoSize
  92. {
  93. get { return autoSize; }
  94. set { autoSize = value; }
  95. }
  96. /// <summary>
  97. /// Gets the index of this column.
  98. /// </summary>
  99. [Browsable(false)]
  100. public int Index
  101. {
  102. get { return index; }
  103. }
  104. /// <inheritdoc/>
  105. [Browsable(false)]
  106. public override float Left
  107. {
  108. get
  109. {
  110. TableBase table = Parent as TableBase;
  111. if (table == null)
  112. return 0;
  113. float result = 0;
  114. for (int i = 0; i < Index; i++)
  115. {
  116. result += table.Columns[i].Width;
  117. }
  118. return result;
  119. }
  120. set { base.Left = value; }
  121. }
  122. /// <summary>
  123. /// Gets or sets the page break flag for this column.
  124. /// </summary>
  125. [Browsable(false)]
  126. public bool PageBreak
  127. {
  128. get { return pageBreak; }
  129. set { pageBreak = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets the number of columns to keep on the same page.
  133. /// </summary>
  134. [Browsable(false)]
  135. public int KeepColumns
  136. {
  137. get { return keepColumns; }
  138. set { keepColumns = value; }
  139. }
  140. internal float MinimumBreakWidth
  141. {
  142. get { return minimumBreakWidth; }
  143. set { minimumBreakWidth = value; }
  144. }
  145. internal static float DefaultWidth
  146. {
  147. get { return (int)Math.Round(64 / (0.25f * Units.Centimeters)) * (0.25f * Units.Centimeters); }
  148. }
  149. #endregion
  150. #region Private Methods
  151. private void UpdateLayout(float dx)
  152. {
  153. TableBase table = Parent as TableBase;
  154. if (table == null)
  155. return;
  156. // update this column cells
  157. foreach (TableRow row in table.Rows)
  158. {
  159. row.CellData(Index).UpdateLayout(dx, 0);
  160. }
  161. // update spanned cells that contains this column
  162. List<Rectangle> spanList = table.GetSpanList();
  163. foreach (Rectangle span in spanList)
  164. {
  165. if (Index > span.Left && Index < span.Right)
  166. table[span.Left, span.Top].CellData.UpdateLayout(dx, 0);
  167. }
  168. }
  169. #endregion
  170. #region Public Methods
  171. /// <inheritdoc/>
  172. public override void Assign(Base source)
  173. {
  174. TableColumn src = source as TableColumn;
  175. MinWidth = src.MinWidth;
  176. MaxWidth = src.MaxWidth;
  177. AutoSize = src.AutoSize;
  178. KeepColumns = src.KeepColumns;
  179. base.Assign(source);
  180. }
  181. /// <inheritdoc/>
  182. public override void Serialize(FRWriter writer)
  183. {
  184. TableColumn c = writer.DiffObject as TableColumn;
  185. base.Serialize(writer);
  186. if (FloatDiff(MinWidth, c.MinWidth))
  187. writer.WriteFloat("MinWidth", MinWidth);
  188. if (FloatDiff(MaxWidth, c.MaxWidth))
  189. writer.WriteFloat("MaxWidth", MaxWidth);
  190. if (FloatDiff(Width, c.Width))
  191. writer.WriteFloat("Width", Width);
  192. if (AutoSize != c.AutoSize)
  193. writer.WriteBool("AutoSize", AutoSize);
  194. }
  195. public void SetIndex(int value)
  196. {
  197. index = value;
  198. }
  199. internal void SaveState()
  200. {
  201. saveWidth = Width;
  202. saveVisible = Visible;
  203. }
  204. internal void RestoreState()
  205. {
  206. Width = saveWidth;
  207. Visible = saveVisible;
  208. }
  209. /// <inheritdoc/>
  210. public override void Clear()
  211. {
  212. TableBase grid = Parent as TableBase;
  213. if (grid == null)
  214. return;
  215. int colIndex = grid.Columns.IndexOf(this);
  216. foreach (TableRow row in grid.Rows)
  217. {
  218. row[colIndex].Dispose();
  219. }
  220. base.Clear();
  221. }
  222. #endregion
  223. /// <summary>
  224. /// Initializes a new instance of the <see cref="TableColumn"/> class.
  225. /// </summary>
  226. public TableColumn()
  227. {
  228. maxWidth = 5000;
  229. Width = DefaultWidth;
  230. SetFlags(Flags.CanCopy | Flags.CanDelete | Flags.CanWriteBounds, false);
  231. BaseName = "Column";
  232. }
  233. }
  234. }