using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Drawing; using FastReport.Data; using FastReport.Utils; namespace FastReport.Table { /// /// Represents a table column. /// /// /// Use the property to set the width of a column. If /// property is true, the column will calculate its width automatically. /// You can also set the and properties /// to restrict the column's width. /// public partial class TableColumn : ComponentBase { #region Fields private float minWidth; private float maxWidth; private bool autoSize; private bool pageBreak; private int keepColumns; private int index; private float saveWidth; private bool saveVisible; private float minimumBreakWidth; #endregion #region Properties /// /// Gets or sets a width of the column, in pixels. /// /// /// The column width cannot exceed the range defined by the /// and properties. /// To convert between pixels and report units, use the constants defined /// in the class. /// [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")] public override float Width { get { return base.Width; } set { value = Converter.DecreasePrecision(value, 2); if (value > MaxWidth) value = MaxWidth; if (value < MinWidth) value = MinWidth; if (FloatDiff(base.Width, value)) { UpdateLayout(value - base.Width); base.Width = value; } } } /// /// Gets or sets the minimal width for this column, in pixels. /// [DefaultValue(0f)] [Category("Layout")] [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")] public float MinWidth { get { return minWidth; } set { minWidth = value; } } /// /// Gets or sets the maximal width for this column, in pixels. /// [DefaultValue(5000f)] [Category("Layout")] [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")] public float MaxWidth { get { return maxWidth; } set { maxWidth = value; } } /// /// Gets or sets a value determines if the column should calculate its width automatically. /// /// /// The column width cannot exceed the range defined by the /// and properties. /// [DefaultValue(false)] [Category("Behavior")] public bool AutoSize { get { return autoSize; } set { autoSize = value; } } /// /// Gets the index of this column. /// [Browsable(false)] public int Index { get { return index; } } /// [Browsable(false)] public override float Left { get { TableBase table = Parent as TableBase; if (table == null) return 0; float result = 0; for (int i = 0; i < Index; i++) { result += table.Columns[i].Width; } return result; } set { base.Left = value; } } /// /// Gets or sets the page break flag for this column. /// [Browsable(false)] public bool PageBreak { get { return pageBreak; } set { pageBreak = value; } } /// /// Gets or sets the number of columns to keep on the same page. /// [Browsable(false)] public int KeepColumns { get { return keepColumns; } set { keepColumns = value; } } internal float MinimumBreakWidth { get { return minimumBreakWidth; } set { minimumBreakWidth = value; } } internal static float DefaultWidth { get { return (int)Math.Round(64 / (0.25f * Units.Centimeters)) * (0.25f * Units.Centimeters); } } #endregion #region Private Methods private void UpdateLayout(float dx) { TableBase table = Parent as TableBase; if (table == null) return; // update this column cells foreach (TableRow row in table.Rows) { row.CellData(Index).UpdateLayout(dx, 0); } // update spanned cells that contains this column List spanList = table.GetSpanList(); foreach (Rectangle span in spanList) { if (Index > span.Left && Index < span.Right) table[span.Left, span.Top].CellData.UpdateLayout(dx, 0); } } #endregion #region Public Methods /// public override void Assign(Base source) { TableColumn src = source as TableColumn; MinWidth = src.MinWidth; MaxWidth = src.MaxWidth; AutoSize = src.AutoSize; KeepColumns = src.KeepColumns; base.Assign(source); } /// public override void Serialize(FRWriter writer) { TableColumn c = writer.DiffObject as TableColumn; base.Serialize(writer); if (FloatDiff(MinWidth, c.MinWidth)) writer.WriteFloat("MinWidth", MinWidth); if (FloatDiff(MaxWidth, c.MaxWidth)) writer.WriteFloat("MaxWidth", MaxWidth); if (FloatDiff(Width, c.Width)) writer.WriteFloat("Width", Width); if (AutoSize != c.AutoSize) writer.WriteBool("AutoSize", AutoSize); } public void SetIndex(int value) { index = value; } internal void SaveState() { saveWidth = Width; saveVisible = Visible; } internal void RestoreState() { Width = saveWidth; Visible = saveVisible; } /// public override void Clear() { TableBase grid = Parent as TableBase; if (grid == null) return; int colIndex = grid.Columns.IndexOf(this); foreach (TableRow row in grid.Rows) { row[colIndex].Dispose(); } base.Clear(); } #endregion /// /// Initializes a new instance of the class. /// public TableColumn() { maxWidth = 5000; Width = DefaultWidth; SetFlags(Flags.CanCopy | Flags.CanDelete | Flags.CanWriteBounds, false); BaseName = "Column"; } } }