CPF/CPF.ReoGrid/CellTypes/CheckBoxCell.cs

227 lines
4.7 KiB
C#
Raw Normal View History

2024-06-24 10:15:59 +08:00
using System;
using System.Diagnostics;
using CPF.Drawing;
using CPF.ReoGrid.Events;
using CPF.ReoGrid.Graphics;
using CPF.ReoGrid.Interaction;
using CPF.ReoGrid.Rendering;
namespace CPF.ReoGrid.CellTypes
{
[Serializable]
public class CheckBoxCell : ContentCellBody
{
public CheckBoxCell() : this(false)
{
}
public CheckBoxCell(bool initChecked)
{
this.initChecked = initChecked;
}
public override void OnSetup(Cell cell)
{
bool flag = cell != null;
if (flag)
{
bool flag2 = this.initChecked;
if (flag2)
{
cell.Data = true;
}
cell.Style.HAlign = ReoGridHorAlign.Center;
cell.Style.VAlign = ReoGridVerAlign.Middle;
}
}
protected virtual bool IsPressed { get; set; }
public override bool OnMouseDown(CellMouseEventArgs e)
{
Rect contentBounds = this.ContentBounds;
Point relativePosition = e.RelativePosition;
bool flag = contentBounds.Contains(relativePosition);
bool result;
if (flag)
{
this.IsPressed = true;
result = true;
}
else
{
result = false;
}
return result;
}
public override bool OnMouseUp(CellMouseEventArgs e)
{
bool isPressed = this.IsPressed;
bool result;
if (isPressed)
{
this.IsPressed = false;
Rect contentBounds = this.ContentBounds;
Point relativePosition = e.RelativePosition;
bool flag = contentBounds.Contains(relativePosition);
if (flag)
{
this.ToggleCheckStatus();
this.RaiseClickEvent();
}
result = true;
}
else
{
result = false;
}
return result;
}
//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public event EventHandler Click;
//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public event EventHandler CheckChanged;
protected virtual void RaiseClickEvent()
{
bool flag = this.Click != null;
if (flag)
{
this.Click(this, null);
}
}
protected virtual void RaiseCheckChangedEvent()
{
EventHandler checkChanged = this.CheckChanged;
if (checkChanged != null)
{
checkChanged(this, null);
}
}
protected override void OnContentPaint(CellDrawingContext dc)
{
IGraphics graphics = dc.Graphics;
bool isPressed = this.IsPressed;
if (isPressed)
{
graphics.FillRectangle(this.ContentBounds, StaticResources.SystemColor_Control);
}
graphics.DrawRectangle(this.ContentBounds, StaticResources.SystemColor_ControlDark);
bool flag = this.isChecked;
if (flag)
{
float x = this.ContentBounds.X;
float y = this.ContentBounds.Y;
float width = this.ContentBounds.Width;
float height = this.ContentBounds.Height;
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.BeginFigure(x + width * 0.167f, y + height * 0.546f);
pathGeometry.LineTo(x + width * 0.444f, y + height * 0.712f);
pathGeometry.LineTo(x + width * 0.833f, y + height * 0.157f);
pathGeometry.LineTo(x + width * 0.944f, y + height * 0.323f);
pathGeometry.LineTo(x + width * 0.5f, y + height * 0.934f);
pathGeometry.LineTo(x + width * 0.08f, y + height * 0.712f);
pathGeometry.EndFigure(true);
graphics.FillPath(StaticResources.SystemColor_WindowText, pathGeometry);
}
}
public override bool OnStartEdit()
{
return false;
}
public override object OnSetData(object data)
{
this.IsChecked = (data is bool && (bool)data);
return base.OnSetData(data);
}
public virtual void ToggleCheckStatus()
{
bool flag = base.Cell != null && this.DisableWhenCellReadonly && base.Cell.IsReadOnly;
if (!flag)
{
this.IsChecked = !this.IsChecked;
}
}
public virtual bool IsChecked
{
get
{
return this.isChecked;
}
set
{
bool flag = this.isChecked != value;
if (flag)
{
this.isChecked = value;
bool flag2 = base.Cell != null && (base.Cell.InnerData as bool?).GetValueOrDefault() != value;
if (flag2)
{
base.Cell.Data = value;
}
EventHandler checkChanged = this.CheckChanged;
if (checkChanged != null)
{
checkChanged(this, null);
}
}
}
}
public override bool OnKeyDown(KeyCode keyCode)
{
bool flag = keyCode == KeyCode.Space;
bool result;
if (flag)
{
this.IsPressed = true;
result = true;
}
else
{
result = false;
}
return result;
}
public override bool OnKeyUp(KeyCode keyCode)
{
bool isPressed = this.IsPressed;
bool result;
if (isPressed)
{
this.IsPressed = false;
bool flag = keyCode == KeyCode.Space;
if (flag)
{
this.ToggleCheckStatus();
}
result = true;
}
else
{
result = false;
}
return result;
}
public override ICellBody Clone()
{
return new CheckBoxCell();
}
private bool initChecked = false;
protected bool isChecked;
}
}