CPF/CPF.ReoGrid/ReferenceRange.cs
2024-06-24 10:15:59 +08:00

472 lines
9.9 KiB
C#

using System;
using CPF.ReoGrid.Core;
namespace CPF.ReoGrid
{
public class ReferenceRange : IRange, IRowRange, IColumnRange
{
public Worksheet Worksheet { get; internal set; }
public CellPosition StartPos
{
get
{
return this.startCell.Position;
}
set
{
this.startCell = this.Worksheet.CreateAndGetCell(this.Worksheet.FixPos(value));
}
}
public CellPosition EndPos
{
get
{
return this.endCell.Position;
}
set
{
this.endCell = this.Worksheet.CreateAndGetCell(this.Worksheet.FixPos(value));
}
}
public int Row
{
get
{
return this.startCell.Row;
}
set
{
this.startCell = this.Worksheet.CreateAndGetCell(value, this.startCell.Column);
}
}
public int Col
{
get
{
return this.startCell.Column;
}
set
{
this.startCell = this.Worksheet.CreateAndGetCell(this.startCell.Row, value);
}
}
public int Rows
{
get
{
return this.Position.Rows;
}
set
{
this.endCell = this.Worksheet.CreateAndGetCell(this.startCell.Row + value, this.endCell.Column);
}
}
public int Cols
{
get
{
return this.Position.Cols;
}
set
{
this.endCell = this.Worksheet.CreateAndGetCell(this.endCell.Row, this.startCell.Column + value);
}
}
public int EndRow
{
get
{
return this.endCell.Row;
}
set
{
this.endCell = this.Worksheet.CreateAndGetCell(new CellPosition(value, this.endCell.Column));
}
}
public int EndCol
{
get
{
return this.endCell.Column;
}
set
{
this.endCell = this.Worksheet.CreateAndGetCell(this.endCell.Row, value);
}
}
public RangePosition Position
{
get
{
return new RangePosition(this.startCell.Position, this.endCell.Position);
}
set
{
RangePosition rangePosition = this.Worksheet.FixRange(value);
this.startCell = this.Worksheet.CreateAndGetCell(rangePosition.StartPos);
this.endCell = this.Worksheet.CreateAndGetCell(rangePosition.EndPos);
}
}
internal ReferenceRange(Worksheet worksheet, Cell startCell, Cell endCell)
{
bool flag = worksheet == null;
if (flag)
{
throw new ArgumentNullException("worksheet", "cannot create refereced range with null worksheet");
}
this.Worksheet = worksheet;
this.startCell = startCell;
this.endCell = endCell;
}
internal ReferenceRange(Worksheet worksheet, CellPosition startPos, CellPosition endPos) : this(worksheet, worksheet.CreateAndGetCell(startPos), worksheet.CreateAndGetCell(endPos))
{
}
internal ReferenceRange(Worksheet worksheet, string address) : this(worksheet, new RangePosition(address))
{
}
internal ReferenceRange(Worksheet worksheet, RangePosition range) : this(worksheet, worksheet.CreateAndGetCell(range.StartPos), worksheet.CreateAndGetCell(range.EndPos))
{
}
internal ReferenceRange(Worksheet worksheet, CellPosition pos) : this(worksheet, pos, pos)
{
}
public bool Contains(CellPosition pos)
{
CellPosition startPos = this.StartPos;
CellPosition endPos = this.EndPos;
return pos.Row >= startPos.Row && pos.Col >= startPos.Col && pos.Row <= endPos.Row && pos.Col <= endPos.Col;
}
public bool Contains(ReferenceRange range)
{
return this.startCell.InternalRow <= range.startCell.InternalRow && this.startCell.InternalCol <= range.startCell.InternalCol && this.endCell.InternalRow >= range.endCell.InternalRow && this.endCell.InternalCol >= range.endCell.InternalCol;
}
public bool Contains(RangePosition range)
{
return this.startCell.InternalRow <= range.Row && this.startCell.InternalCol <= range.Col && this.endCell.InternalRow >= range.EndRow && this.endCell.InternalCol >= range.EndCol;
}
public bool IntersectWith(RangePosition range)
{
return this.Position.IntersectWith(range);
}
public bool IntersectWith(ReferenceRange range)
{
return this.IntersectWith(range.Position);
}
public static implicit operator RangePosition(ReferenceRange refRange)
{
return refRange.Position;
}
public override string ToString()
{
return this.Position.ToString();
}
public virtual string ToAddress()
{
return this.Position.ToAddress();
}
public virtual string ToAbsoluteAddress()
{
return this.Position.ToAbsoluteAddress();
}
public object Data
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeData(this);
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeData(this, value);
}
}
public void Select()
{
this.CheckForOwnerAssociated();
this.Worksheet.SelectRange(this.Position);
}
public ReferenceRangeStyle Style
{
get
{
this.CheckForOwnerAssociated();
bool flag = this.referenceStyle == null;
if (flag)
{
this.referenceStyle = new ReferenceRangeStyle(this.Worksheet, this);
}
return this.referenceStyle;
}
}
public RangeBorderProperty Border
{
get
{
bool flag = this.borderProperty == null;
if (flag)
{
this.borderProperty = new RangeBorderProperty(this);
}
return this.borderProperty;
}
}
public RangeBorderStyle BorderLeft
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.Left, true).Left;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.Left, value);
}
}
public RangeBorderStyle BorderTop
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.Top, true).Top;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.Top, value);
}
}
public RangeBorderStyle BorderRight
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.Right, true).Right;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.Right, value);
}
}
public RangeBorderStyle BorderBottom
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.Bottom, true).Bottom;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.Bottom, value);
}
}
public RangeBorderStyle BorderInsideAll
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.InsideAll, true).InsideHorizontal;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.InsideAll, value);
}
}
public RangeBorderStyle BorderInsideHorizontal
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.InsideHorizontal, true).InsideHorizontal;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.InsideHorizontal, value);
}
}
public RangeBorderStyle BorderInsideVertical
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.InsideVertical, true).InsideVertical;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.InsideVertical, value);
}
}
public RangeBorderStyle BorderOutside
{
get
{
this.CheckForOwnerAssociated();
return (this.Worksheet == null) ? RangeBorderStyle.Empty : this.Worksheet.GetRangeBorders(this.Position, BorderPositions.Outside, true).Left;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.Outside, value);
}
}
public RangeBorderStyle BorderAll
{
get
{
this.CheckForOwnerAssociated();
return this.Worksheet.GetRangeBorders(this.Position, BorderPositions.All, true).Left;
}
set
{
this.CheckForOwnerAssociated();
this.Worksheet.SetRangeBorders(this.Position, BorderPositions.All, value);
}
}
public void Merge()
{
this.CheckForOwnerAssociated();
this.Worksheet.MergeRange(this.Position);
}
public void Unmerge()
{
this.CheckForOwnerAssociated();
this.Worksheet.UnmergeRange(this.Position);
}
public bool IsMergedCell
{
get
{
this.CheckForOwnerAssociated();
Cell cell = this.Worksheet.GetCell(this.StartPos);
return cell != null && (int)cell.Rowspan == this.Rows && (int)cell.Colspan == this.Cols;
}
}
public void GroupRows()
{
this.CheckForOwnerAssociated();
this.Worksheet.GroupRows(this.Row, this.Rows);
}
public void GroupColumns()
{
this.CheckForOwnerAssociated();
this.Worksheet.GroupColumns(this.Col, this.Cols);
}
public void UngroupRows()
{
this.CheckForOwnerAssociated();
this.Worksheet.UngroupRows(this.Row, this.Rows);
}
public void UngroupColumns()
{
this.CheckForOwnerAssociated();
this.Worksheet.UngroupColumns(this.Row, this.Rows);
}
public bool IsReadonly
{
get
{
bool result = true;
foreach (Cell cell in this.Cells)
{
bool flag = !cell.IsReadOnly;
if (flag)
{
result = false;
break;
}
}
return result;
}
set
{
foreach (Cell cell in this.Cells)
{
cell.IsReadOnly = value;
}
}
}
private void CheckForOwnerAssociated()
{
bool flag = this.Worksheet == null;
if (flag)
{
throw new ReferenceRangeNotAssociatedException(this);
}
}
public Worksheet.CellCollection Cells
{
get
{
bool flag = this.cellsCollection == null;
if (flag)
{
this.cellsCollection = new Worksheet.CellCollection(this.Worksheet, this);
}
return this.cellsCollection;
}
}
private Cell startCell;
private Cell endCell;
private ReferenceRangeStyle referenceStyle = null;
private RangeBorderProperty borderProperty = null;
private Worksheet.CellCollection cellsCollection;
}
}