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

712 lines
16 KiB
C#

using System;
using System.Text;
using System.Text.RegularExpressions;
using CPF.ReoGrid.Core;
namespace CPF.ReoGrid
{
[Serializable]
public struct RangePosition : IRange, IRowRange, IColumnRange, ISheetAddress
{
public int Row
{
get
{
return this.row;
}
set
{
this.row = value;
}
}
public int Col
{
get
{
return this.col;
}
set
{
this.col = value;
}
}
public int Rows
{
get
{
return this.rows;
}
set
{
this.rows = value;
}
}
public int Cols
{
get
{
return this.cols;
}
set
{
this.cols = value;
}
}
public int EndRow
{
get
{
return this.row + this.rows - 1;
}
set
{
this.rows = value - this.row + 1;
}
}
public int EndCol
{
get
{
return this.col + this.cols - 1;
}
set
{
this.cols = value - this.col + 1;
}
}
public CellPosition StartPos
{
get
{
return new CellPosition(this.row, this.col, (byte)(this.positionProperties & 3));
}
set
{
this.row = value.Row;
this.col = value.Col;
this.StartRowProperty = value.RowProperty;
this.StartColumnProperty = value.ColumnProperty;
}
}
public CellPosition EndPos
{
get
{
return new CellPosition((this.rows == RangePosition.EntireRange.rows) ? 1048576 : this.EndRow, (this.cols == RangePosition.EntireRange.cols) ? 32768 : this.EndCol, (byte)((this.positionProperties & 12) >> 2));
}
set
{
this.EndRow = value.Row;
this.EndCol = value.Col;
this.EndRowProperty = value.RowProperty;
this.EndColumnProperty = value.ColumnProperty;
}
}
public PositionProperty StartRowProperty
{
get
{
return ((this.positionProperties & 1) == 1) ? PositionProperty.Absolute : PositionProperty.Relative;
}
set
{
bool flag = value == PositionProperty.Absolute;
if (flag)
{
this.positionProperties |= 1;
}
else
{
this.positionProperties = (byte)((int)this.positionProperties & -2);
}
}
}
public PositionProperty StartColumnProperty
{
get
{
return ((this.positionProperties & 2) == 2) ? PositionProperty.Absolute : PositionProperty.Relative;
}
set
{
bool flag = value == PositionProperty.Absolute;
if (flag)
{
this.positionProperties |= 2;
}
else
{
this.positionProperties = (byte)((int)this.positionProperties & -3);
}
}
}
public PositionProperty EndRowProperty
{
get
{
return ((this.positionProperties & 4) == 4) ? PositionProperty.Absolute : PositionProperty.Relative;
}
set
{
bool flag = value == PositionProperty.Absolute;
if (flag)
{
this.positionProperties |= 4;
}
else
{
this.positionProperties = (byte)((int)this.positionProperties & -5);
}
}
}
public PositionProperty EndColumnProperty
{
get
{
return ((this.positionProperties & 8) == 8) ? PositionProperty.Absolute : PositionProperty.Relative;
}
set
{
bool flag = value == PositionProperty.Absolute;
if (flag)
{
this.positionProperties |= 8;
}
else
{
this.positionProperties = (byte)((int)this.positionProperties & -9);
}
}
}
public RangePosition(CellPosition startPos, CellPosition endPos)
{
this.row = Math.Min(startPos.Row, endPos.Row);
this.col = Math.Min(startPos.Col, endPos.Col);
this.rows = Math.Max(startPos.Row, endPos.Row) - this.row + 1;
this.cols = Math.Max(startPos.Col, endPos.Col) - this.col + 1;
this.positionProperties = 0;
}
public RangePosition(CellPosition singlePos)
{
this.row = singlePos.Row;
this.col = singlePos.Col;
this.rows = 1;
this.cols = 1;
this.positionProperties = 0;
}
public RangePosition(int row, int col, int rows, int cols)
{
this.row = row;
this.col = col;
this.rows = rows;
this.cols = cols;
this.positionProperties = 0;
}
public RangePosition(string startCell, string endCell)
{
this = new RangePosition(new CellPosition(startCell), new CellPosition(endCell));
}
public RangePosition(string address)
{
this.positionProperties = 0;
Match match = RGUtility.RangeReferenceRegex.Match(address);
bool flag = !match.Success;
if (flag)
{
match = RGUtility.SingleAbsoulteRangeRegex.Match(address);
}
bool flag2 = !match.Success && CellPosition.IsValidAddress(address);
if (flag2)
{
CellPosition cellPosition = new CellPosition(address);
this.row = cellPosition.Row;
this.col = cellPosition.Col;
this.rows = 1;
this.cols = 1;
this.StartRowProperty = cellPosition.RowProperty;
this.StartColumnProperty = cellPosition.ColumnProperty;
this.EndRowProperty = cellPosition.RowProperty;
this.EndColumnProperty = cellPosition.ColumnProperty;
}
else
{
bool flag3 = !match.Success;
if (flag3)
{
throw new ArgumentException("range is invalid: " + address);
}
int val = 0;
int num = 0;
int val2 = 0;
int num2 = 0;
bool flag4 = false;
bool flag5 = false;
bool success = match.Groups["from_row"].Success;
if (success)
{
bool flag6 = !int.TryParse(match.Groups["from_row"].Value, out num);
if (flag6)
{
throw new ArgumentException("range is invalid: " + address);
}
num--;
}
else
{
flag4 = true;
}
bool flag7 = !flag4 && match.Groups["to_row"].Success;
if (flag7)
{
bool flag8 = !int.TryParse(match.Groups["to_row"].Value, out num2);
if (flag8)
{
throw new ArgumentException("range is invalid: " + address);
}
num2--;
}
bool success2 = match.Groups["from_col"].Success;
if (success2)
{
val = RGUtility.GetNumberOfChar(match.Groups["from_col"].Value);
}
else
{
flag5 = true;
}
bool flag9 = !flag5 && match.Groups["to_col"].Success;
if (flag9)
{
val2 = RGUtility.GetNumberOfChar(match.Groups["to_col"].Value);
}
bool flag10 = flag5;
if (flag10)
{
this.row = Math.Min(num, num2);
this.rows = Math.Max(num, num2) - this.row + 1;
this.col = 0;
this.cols = -1;
}
else
{
bool flag11 = flag4;
if (flag11)
{
this.row = 0;
this.rows = -1;
this.col = Math.Min(val, val2);
this.cols = Math.Max(val, val2) - this.col + 1;
}
else
{
this.row = Math.Min(num, num2);
this.col = Math.Min(val, val2);
this.rows = Math.Max(num, num2) - this.row + 1;
this.cols = Math.Max(val, val2) - this.col + 1;
}
}
bool success3 = match.Groups["abs_from_row"].Success;
if (success3)
{
this.positionProperties |= 1;
}
bool success4 = match.Groups["abs_from_col"].Success;
if (success4)
{
this.positionProperties |= 2;
}
bool success5 = match.Groups["abs_to_row"].Success;
if (success5)
{
this.positionProperties |= 4;
}
bool success6 = match.Groups["abs_to_col"].Success;
if (success6)
{
this.positionProperties |= 8;
}
}
}
public override bool Equals(object obj)
{
bool flag = obj == null;
bool result;
if (flag)
{
result = false;
}
else
{
bool flag2 = !(obj is RangePosition);
if (flag2)
{
result = false;
}
else
{
RangePosition rangePosition = this;
RangePosition rangePosition2 = (RangePosition)obj;
result = (rangePosition.row == rangePosition2.row && rangePosition.col == rangePosition2.col && rangePosition.rows == rangePosition2.rows && rangePosition.cols == rangePosition2.cols && rangePosition.positionProperties == rangePosition2.positionProperties);
}
}
return result;
}
public static bool operator ==(RangePosition r1, RangePosition r2)
{
return r1.Equals(r2);
}
public static bool operator !=(RangePosition r1, RangePosition r2)
{
return !r1.Equals(r2);
}
public override int GetHashCode()
{
return this.row ^ this.col ^ this.rows ^ this.cols ^ (int)this.positionProperties;
}
public bool Contains(CellPosition pos)
{
return this.Contains(pos.Row, pos.Col);
}
public bool Contains(int row, int col)
{
return row >= this.row && col >= this.col && row <= this.EndRow && col <= this.EndCol;
}
public bool Contains(RangePosition range)
{
return this.row <= range.row && this.col <= range.col && this.EndRow >= range.EndRow && this.EndCol >= range.EndCol;
}
public bool ContainsRow(int row)
{
return row >= this.row && row <= this.EndRow;
}
public bool ContainsColumn(int col)
{
return col >= this.col && col <= this.EndCol;
}
public bool IntersectWith(RangePosition range)
{
int num = this.row + this.rows - 1;
int num2 = this.col + this.cols - 1;
return this.col + this.cols - 1 >= range.col && range.col + range.cols - 1 >= this.col && this.row + this.rows - 1 >= range.row && range.row + range.rows - 1 >= this.row;
}
public bool IsEmpty
{
get
{
return this.rows == 0 || this.cols == 0;
}
}
public bool IsEntire
{
get
{
return this == RangePosition.EntireRange;
}
}
public override string ToString()
{
return this.ToAddress();
}
public string ToAddress()
{
bool flag = this.rows <= -1 && this.cols <= -1;
string result;
if (flag)
{
StringBuilder stringBuilder = new StringBuilder();
bool flag2 = this.StartRowProperty == PositionProperty.Absolute;
if (flag2)
{
stringBuilder.Append('$');
}
stringBuilder.Append("1:");
bool flag3 = this.EndRowProperty == PositionProperty.Absolute;
if (flag3)
{
stringBuilder.Append('$');
}
stringBuilder.Append("1048576");
result = stringBuilder.ToString();
}
else
{
bool flag4 = this.cols <= -1;
if (flag4)
{
StringBuilder stringBuilder2 = new StringBuilder();
bool flag5 = this.StartRowProperty == PositionProperty.Absolute;
if (flag5)
{
stringBuilder2.Append('$');
}
stringBuilder2.Append(this.row + 1);
stringBuilder2.Append(':');
bool flag6 = this.EndRowProperty == PositionProperty.Absolute;
if (flag6)
{
stringBuilder2.Append('$');
}
stringBuilder2.Append(this.EndRow + 1);
result = stringBuilder2.ToString();
}
else
{
bool flag7 = this.rows <= -1;
if (flag7)
{
StringBuilder stringBuilder3 = new StringBuilder();
bool flag8 = this.StartColumnProperty == PositionProperty.Absolute;
if (flag8)
{
stringBuilder3.Append('$');
}
stringBuilder3.Append(RGUtility.GetAlphaChar((long)this.col));
stringBuilder3.Append(':');
bool flag9 = this.EndColumnProperty == PositionProperty.Absolute;
if (flag9)
{
stringBuilder3.Append('$');
}
stringBuilder3.Append(RGUtility.GetAlphaChar((long)this.EndCol));
result = stringBuilder3.ToString();
}
else
{
StringBuilder stringBuilder4 = new StringBuilder();
bool flag10 = this.StartColumnProperty == PositionProperty.Absolute;
if (flag10)
{
stringBuilder4.Append('$');
}
stringBuilder4.Append(RGUtility.GetAlphaChar((long)this.col));
bool flag11 = this.StartRowProperty == PositionProperty.Absolute;
if (flag11)
{
stringBuilder4.Append('$');
}
stringBuilder4.Append(this.row + 1);
stringBuilder4.Append(':');
bool flag12 = this.EndColumnProperty == PositionProperty.Absolute;
if (flag12)
{
stringBuilder4.Append('$');
}
stringBuilder4.Append(RGUtility.GetAlphaChar((long)this.EndCol));
bool flag13 = this.EndRowProperty == PositionProperty.Absolute;
if (flag13)
{
stringBuilder4.Append('$');
}
stringBuilder4.Append(this.EndRow + 1);
result = stringBuilder4.ToString();
}
}
}
return result;
}
public string ToRelativeAddress()
{
bool flag = this.rows <= -1 && this.cols <= -1;
string result;
if (flag)
{
result = "1:1048576";
}
else
{
bool flag2 = this.cols <= -1;
if (flag2)
{
result = string.Format("{0}:{1}", this.row + 1, this.EndRow + 1);
}
else
{
bool flag3 = this.rows <= -1;
if (flag3)
{
result = string.Format("{0}:{1}", RGUtility.GetAlphaChar((long)this.col), RGUtility.GetAlphaChar((long)this.EndCol));
}
else
{
result = string.Format("{0}{1}:{2}{3}", new object[]
{
RGUtility.GetAlphaChar((long)this.col),
this.row + 1,
RGUtility.GetAlphaChar((long)this.EndCol),
this.EndRow + 1
});
}
}
}
return result;
}
public string ToAbsoluteAddress()
{
bool flag = this.rows <= -1 && this.cols <= -1;
string result;
if (flag)
{
result = "$1:$1048576";
}
else
{
bool flag2 = this.cols <= -1;
if (flag2)
{
result = string.Format("${0}:${1}", this.row + 1, this.EndRow + 1);
}
else
{
bool flag3 = this.rows <= -1;
if (flag3)
{
result = string.Format("${0}:${1}", RGUtility.GetAlphaChar((long)this.col), RGUtility.GetAlphaChar((long)this.EndCol));
}
else
{
result = string.Format("${0}${1}:${2}${3}", new object[]
{
RGUtility.GetAlphaChar((long)this.col),
this.row + 1,
RGUtility.GetAlphaChar((long)this.EndCol),
this.EndRow + 1
});
}
}
}
return result;
}
public string ToStringSpans()
{
return string.Format("{0}R x {1}C", this.rows, this.cols);
}
public void Offset(int rows, int cols)
{
this.row += rows;
this.col += cols;
}
public static bool IsValidAddress(string address)
{
return RGUtility.RangeReferenceRegex.IsMatch(address) || RGUtility.CellReferenceRegex.IsMatch(address) || RGUtility.SingleAbsoulteRangeRegex.IsMatch(address);
}
public bool IsSingleCell
{
get
{
return this.rows == 1 && this.cols == 1;
}
}
public static RangePosition FromCellPosition(CellPosition startPosition, CellPosition endPosition)
{
return RangePosition.FromCellPosition(startPosition.Row, startPosition.Col, endPosition.Row, endPosition.Col);
}
public static RangePosition FromCellPosition(int startRow, int startColumn, int endRow, int endColumn)
{
RangePosition rangePosition;
rangePosition.row = Math.Min(startRow, endRow);
rangePosition.col = Math.Min(startColumn, endColumn);
rangePosition.rows = Math.Max(startRow, endRow) - rangePosition.row + 1;
rangePosition.cols = Math.Max(startColumn, endColumn) - rangePosition.col + 1;
rangePosition.positionProperties = 0;
return rangePosition;
}
public static RangePosition Union(RangePosition range1, RangePosition range2)
{
bool flag = range1.IsEmpty && range2.IsEmpty;
RangePosition result;
if (flag)
{
result = RangePosition.Empty;
}
else
{
bool isEmpty = range1.IsEmpty;
if (isEmpty)
{
result = range2;
}
else
{
bool isEmpty2 = range2.IsEmpty;
if (isEmpty2)
{
result = range1;
}
else
{
int num = Math.Min(range1.row, range2.row);
int num2 = Math.Min(range1.col, range2.col);
int num3 = Math.Max(range1.EndRow, range2.EndRow);
int num4 = Math.Max(range1.EndCol, range2.EndCol);
result = new RangePosition(num, num2, num3 - num + 1, num4 - num2 + 1);
}
}
}
return result;
}
public void SetRows(int rows)
{
this.rows = rows;
}
public void SetCols(int cols)
{
this.cols = cols;
}
private int row;
private int col;
private int rows;
private int cols;
private byte positionProperties;
public static readonly RangePosition Empty = new RangePosition(0, 0, 0, 0);
public static readonly RangePosition EntireRange = new RangePosition(0, 0, -1, -1);
}
}