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

265 lines
5.3 KiB
C#

using System;
using System.Text.RegularExpressions;
using CPF.ReoGrid.Core;
namespace CPF.ReoGrid
{
[Serializable]
public struct CellPosition : ISheetAddress
{
public int Row
{
get
{
return this.row;
}
set
{
this.row = value;
}
}
public int Col
{
get
{
return this.col;
}
set
{
this.col = value;
}
}
public PositionProperty RowProperty
{
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 ColumnProperty
{
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 CellPosition(int row, int col)
{
this.row = row;
this.col = col;
this.positionProperties = 0;
}
internal CellPosition(int row, int col, byte positionProperties)
{
this.row = row;
this.col = col;
this.positionProperties = positionProperties;
}
public CellPosition(string address)
{
Match match = RGUtility.CellReferenceRegex.Match(address);
bool flag = !match.Success;
if (flag)
{
throw new ArgumentException("invalid address for cell: " + address, "id");
}
this.row = 0;
int.TryParse(match.Groups["row"].Value, out this.row);
this.row--;
this.col = RGUtility.GetNumberOfChar(match.Groups["col"].Value);
this.positionProperties = 0;
bool success = match.Groups["abs_row"].Success;
if (success)
{
this.positionProperties |= 1;
}
bool success2 = match.Groups["abs_col"].Success;
if (success2)
{
this.positionProperties |= 2;
}
}
public override string ToString()
{
return this.ToAddress();
}
public string ToAddress()
{
bool flag = this.positionProperties == 3;
string result;
if (flag)
{
result = "$" + RGUtility.GetAlphaChar((long)this.col) + "$" + (this.row + 1).ToString();
}
else
{
bool flag2 = this.positionProperties == 2;
if (flag2)
{
result = "$" + RGUtility.GetAlphaChar((long)this.col) + (this.row + 1).ToString();
}
else
{
bool flag3 = this.positionProperties == 1;
if (flag3)
{
result = RGUtility.GetAlphaChar((long)this.col) + "$" + (this.row + 1).ToString();
}
else
{
result = RGUtility.GetAlphaChar((long)this.col) + (this.row + 1).ToString();
}
}
}
return result;
}
public string ToRelativeAddress()
{
return RGUtility.GetAlphaChar((long)this.col) + (this.row + 1).ToString();
}
public string ToAbsoluteAddress()
{
return string.Format("${0}${1}", RGUtility.GetAlphaChar((long)this.col), this.row + 1);
}
public static bool IsValidAddress(string address)
{
return RGUtility.CellReferenceRegex.IsMatch(address);
}
internal bool IsEmpty
{
get
{
return this.row == CellPosition.Empty.row && this.col == CellPosition.Empty.col;
}
}
public override bool Equals(object obj)
{
bool flag = obj == null;
bool result;
if (flag)
{
result = false;
}
else
{
bool flag2 = !(obj is CellPosition);
if (flag2)
{
result = false;
}
else
{
CellPosition cellPosition = this;
CellPosition cellPosition2 = (CellPosition)obj;
result = (cellPosition.row == cellPosition2.row && cellPosition.col == cellPosition2.col && cellPosition.positionProperties == cellPosition2.positionProperties);
}
}
return result;
}
public bool Equals(int row, int col)
{
return this.row == row && this.col == col;
}
public bool Equals(string address)
{
return !string.IsNullOrEmpty(address) && CellPosition.IsValidAddress(address) && this.Equals(new CellPosition(address));
}
public static bool operator ==(CellPosition r1, CellPosition r2)
{
return r1.Equals(r2);
}
public static bool operator !=(CellPosition r1, CellPosition r2)
{
return !r1.Equals(r2);
}
public static bool Equals(CellPosition pos1, CellPosition pos2)
{
return CellPosition.Equals(pos1, pos2.row, pos2.col);
}
public static bool Equals(CellPosition pos, int row, int col)
{
return pos.row == row && pos.col == col;
}
public override int GetHashCode()
{
return this.row ^ this.col ^ (int)this.positionProperties;
}
public CellPosition Offset(CellPosition pos)
{
return this.Offset(pos.row, pos.col);
}
public CellPosition Offset(int rows, int cols)
{
this.row += rows;
this.col += cols;
return this;
}
public static CellPosition operator +(CellPosition pos1, CellPosition pos2)
{
return pos1.Offset(pos2.row, pos2.col);
}
public static CellPosition operator -(CellPosition pos1, CellPosition pos2)
{
return pos1.Offset(-pos2.row, -pos2.col);
}
private int row;
private int col;
private byte positionProperties;
internal static readonly CellPosition Empty = new CellPosition(-1, -1);
public static CellPosition Zero = new CellPosition(0, 0);
}
}