示例#1
0
        private void setCell(int col, int row, object content, int colSpan, int rowSpan, bool noWrap, HorizontalTextAlignment?alignment, ConsoleColor?background = null)
        {
            if (col < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(col), col, @"""col"" cannot be negative.");
            }
            if (row < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(row), row, @"""row"" cannot be negative.");
            }
            if (colSpan < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(colSpan), colSpan, @"""colSpan"" cannot be less than 1.");
            }
            if (rowSpan < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(rowSpan), rowSpan, @"""rowSpan"" cannot be less than 1.");
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            // Complain if setting this cell would overlap with another cell due to its colspan or rowspan
            if (row >= _cells.Count || col >= _cells[row].Count || _cells[row][col] == null || _cells[row][col] is surrogateCell)
            {
                for (int x = 0; x < colSpan; x++)
                {
                    for (int y = 0; y < rowSpan; y++)
                    {
                        if (row + y < _cells.Count && col + x < _cells[row + y].Count && _cells[row + y][col + x] is surrogateCell)
                        {
                            var sur  = (surrogateCell)_cells[row][col];
                            var real = (trueCell)_cells[sur.RealRow][sur.RealCol];
                            throw new InvalidOperationException(@"The cell at column {0}, row {1} is already occupied because the cell at column {2}, row {3} has colspan {4} and rowspan {5}.".Fmt(col, row, sur.RealCol, sur.RealRow, real.ColSpan, real.RowSpan));
                        }
                    }
                }
            }

            ensureCell(col, row);

            // If the cell contains a true cell, remove it with all its surrogates
            if (_cells[row][col] is trueCell)
            {
                var tr = (trueCell)_cells[row][col];
                for (int x = 0; x < tr.ColSpan; x++)
                {
                    for (int y = 0; y < tr.RowSpan; y++)
                    {
                        _cells[row + y][col + x] = null;
                    }
                }
            }

            // Insert the cell in the right place
            _cells[row][col] = new trueCell
            {
                ColSpan    = colSpan,
                RowSpan    = rowSpan,
                Value      = content,
                NoWrap     = noWrap,
                Alignment  = alignment,
                Background = background
            };

            // For cells with span, insert the appropriate surrogate cells.
            for (int x = 0; x < colSpan; x++)
            {
                for (int y = x == 0 ? 1 : 0; y < rowSpan; y++)
                {
                    ensureCell(col + x, row + y);
                    _cells[row + y][col + x] = new surrogateCell {
                        RealCol = col, RealRow = row
                    };
                }
            }
        }
示例#2
0
        private void setCell(int col, int row, object content, int colSpan, int rowSpan, bool noWrap, HorizontalTextAlignment? alignment, ConsoleColor? background = null)
        {
            if (col < 0)
                throw new ArgumentOutOfRangeException("col", col, @"""col"" cannot be negative.");
            if (row < 0)
                throw new ArgumentOutOfRangeException("row", row, @"""row"" cannot be negative.");
            if (colSpan < 1)
                throw new ArgumentOutOfRangeException("colSpan", colSpan, @"""colSpan"" cannot be less than 1.");
            if (rowSpan < 1)
                throw new ArgumentOutOfRangeException("rowSpan", rowSpan, @"""rowSpan"" cannot be less than 1.");
            if (content == null)
                throw new ArgumentNullException("content");

            // Complain if setting this cell would overlap with another cell due to its colspan or rowspan
            if (row >= _cells.Count || col >= _cells[row].Count || _cells[row][col] == null || _cells[row][col] is surrogateCell)
            {
                for (int x = 0; x < colSpan; x++)
                    for (int y = 0; y < rowSpan; y++)
                        if (row + y < _cells.Count && col + x < _cells[row + y].Count && _cells[row + y][col + x] is surrogateCell)
                        {
                            var sur = (surrogateCell) _cells[row][col];
                            var real = (trueCell) _cells[sur.RealRow][sur.RealCol];
                            throw new InvalidOperationException(@"The cell at column {0}, row {1} is already occupied because the cell at column {2}, row {3} has colspan {4} and rowspan {5}.".Fmt(col, row, sur.RealCol, sur.RealRow, real.ColSpan, real.RowSpan));
                        }
            }

            ensureCell(col, row);

            // If the cell contains a true cell, remove it with all its surrogates
            if (_cells[row][col] is trueCell)
            {
                var tr = (trueCell) _cells[row][col];
                for (int x = 0; x < tr.ColSpan; x++)
                    for (int y = 0; y < tr.RowSpan; y++)
                        _cells[row + y][col + x] = null;
            }

            // Insert the cell in the right place
            _cells[row][col] = new trueCell
            {
                ColSpan = colSpan,
                RowSpan = rowSpan,
                Value = content,
                NoWrap = noWrap,
                Alignment = alignment,
                Background = background
            };

            // For cells with span, insert the appropriate surrogate cells.
            for (int x = 0; x < colSpan; x++)
                for (int y = x == 0 ? 1 : 0; y < rowSpan; y++)
                {
                    ensureCell(col + x, row + y);
                    _cells[row + y][col + x] = new surrogateCell { RealCol = col, RealRow = row };
                }
        }