PageColumns.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using FastReport.Utils;
  2. using System;
  3. using System.ComponentModel;
  4. namespace FastReport
  5. {
  6. /// <summary>
  7. /// This class contains the page columns settings.
  8. /// It is used in the <see cref="ReportPage.Columns"/> property.
  9. /// </summary>
  10. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  11. public class PageColumns
  12. {
  13. private int count;
  14. private float width;
  15. private FloatCollection positions;
  16. private ReportPage page;
  17. /// <summary>
  18. /// Gets or sets the number of columns.
  19. /// </summary>
  20. /// <remarks>
  21. /// Set this property to 0 or 1 if you don't want to use columns.
  22. /// </remarks>
  23. [DefaultValue(1)]
  24. public int Count
  25. {
  26. get { return count; }
  27. set
  28. {
  29. if (value <= 0)
  30. throw new ArgumentOutOfRangeException("Count", "Value must be greather than 0");
  31. count = value;
  32. width = (page.PaperWidth - page.LeftMargin - page.RightMargin) / count;
  33. positions.Clear();
  34. for (int i = 0; i < count; i++)
  35. {
  36. positions.Add(i * Width);
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// Gets or sets the column width.
  42. /// </summary>
  43. [TypeConverter("FastReport.TypeConverters.PaperConverter, FastReport")]
  44. public float Width
  45. {
  46. get { return width; }
  47. set { width = value; }
  48. }
  49. /// <summary>
  50. /// Gets or sets a list of column starting positions.
  51. /// </summary>
  52. /// <remarks>
  53. /// Each value represents a column starting position measured in the millimeters.
  54. /// </remarks>
  55. public FloatCollection Positions
  56. {
  57. get { return positions; }
  58. set
  59. {
  60. if (value.Count == count)
  61. {
  62. positions = value;
  63. }
  64. else
  65. {
  66. positions.Clear();
  67. for (int i = 0; i < count; i++)
  68. {
  69. positions.Add(i * Width);
  70. }
  71. }
  72. }
  73. }
  74. private bool ShouldSerializeWidth()
  75. {
  76. return Count > 1;
  77. }
  78. private bool ShouldSerializePositions()
  79. {
  80. return Count > 1;
  81. }
  82. /// <summary>
  83. /// Assigns values from another source.
  84. /// </summary>
  85. /// <param name="source">Source to assign from.</param>
  86. public void Assign(PageColumns source)
  87. {
  88. Count = source.Count;
  89. Width = source.Width;
  90. Positions.Assign(source.Positions);
  91. }
  92. internal void Serialize(FRWriter writer, PageColumns c)
  93. {
  94. if (Count != c.Count)
  95. writer.WriteInt("Columns.Count", Count);
  96. if (Count > 1)
  97. {
  98. writer.WriteFloat("Columns.Width", Width);
  99. Positions = Positions; // avoid bug when number of positions is not equal number of columns
  100. writer.WriteValue("Columns.Positions", Positions);
  101. }
  102. }
  103. internal PageColumns(ReportPage page)
  104. {
  105. this.page = page;
  106. positions = new FloatCollection();
  107. Count = 1;
  108. }
  109. }
  110. }