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

132 lines
2.6 KiB
C#

using System;
using CPF.Drawing;
using CPF.ReoGrid.Graphics;
using CPF.ReoGrid.Rendering;
namespace CPF.ReoGrid.CellTypes
{
public class ImageCell : CellBody
{
public Image Image { get; set; }
public ImageCell()
{
}
public ImageCell(Image image) : this(image, ImageCellViewMode.Stretch)
{
}
public ImageCell(Image image, ImageCellViewMode viewMode)
{
this.Image = image;
this.viewMode = viewMode;
}
public ImageCellViewMode ViewMode
{
get
{
return this.viewMode;
}
set
{
bool flag = this.viewMode != value;
if (flag)
{
this.viewMode = value;
bool flag2 = base.Cell != null && base.Cell.Worksheet != null;
if (flag2)
{
base.Cell.Worksheet.RequestInvalidate();
}
}
}
}
public override void OnPaint(CellDrawingContext dc)
{
bool flag = this.Image != null;
if (flag)
{
float x = this.Bounds.X;
float y = this.Bounds.Y;
bool flag2 = false;
float num;
float num2;
switch (this.viewMode)
{
default:
num = this.Bounds.Width;
num2 = this.Bounds.Height;
break;
case ImageCellViewMode.Zoom:
{
float val = this.Bounds.Width / (float)this.Image.Width;
float val2 = this.Bounds.Height / (float)this.Image.Height;
float num3 = Math.Min(val, val2);
num = num3 * (float)this.Image.Width;
num2 = num3 * (float)this.Image.Height;
break;
}
case ImageCellViewMode.Clip:
{
num = (float)this.Image.Width;
num2 = (float)this.Image.Height;
bool flag3 = num > this.Bounds.Width || num2 > this.Bounds.Height;
if (flag3)
{
flag2 = true;
}
break;
}
}
switch (base.Cell.Style.HAlign)
{
default:
x = this.Bounds.X;
break;
case ReoGridHorAlign.Center:
x = (this.Bounds.Width - num) / 2f;
break;
case ReoGridHorAlign.Right:
x = this.Bounds.Width - num;
break;
}
switch (base.Cell.Style.VAlign)
{
default:
y = this.Bounds.Y;
break;
case ReoGridVerAlign.Middle:
y = (this.Bounds.Height - num2) / 2f;
break;
case ReoGridVerAlign.Bottom:
y = this.Bounds.Height - num2;
break;
}
IGraphics graphics = dc.Graphics;
bool flag4 = flag2;
if (flag4)
{
graphics.PushClip(this.Bounds);
}
graphics.DrawImage(this.Image, x, y, num, num2);
bool flag5 = flag2;
if (flag5)
{
graphics.PopClip();
}
}
dc.DrawCellText();
}
public override ICellBody Clone()
{
return new ImageCell(this.Image);
}
protected ImageCellViewMode viewMode;
}
}