123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Globalization;
- using System.Windows.Forms;
- using XPTable.Events;
- using XPTable.Models;
- namespace XPTable.Renderers
- {
-
-
-
- public class ImageCellRenderer : CellRenderer
- {
- #region Class Data
-
-
-
- private bool drawText;
- #endregion
-
-
- #region Constructor
-
-
-
-
-
- public ImageCellRenderer() : base()
- {
- this.drawText = true;
- }
- #endregion
- #region Methods
-
-
-
-
-
-
-
-
-
-
-
- protected Rectangle CalcImageRect(Image image, ImageSizeMode sizeMode, RowAlignment rowAlignment, ColumnAlignment columnAlignment)
- {
- if (this.DrawText)
- {
- sizeMode = ImageSizeMode.ScaledToFit;
- }
- Rectangle imageRect = this.ClientRectangle;
- if (sizeMode == ImageSizeMode.Normal)
- {
- if (image.Width < imageRect.Width)
- {
- imageRect.Width = image.Width;
- }
- if (image.Height < imageRect.Height)
- {
- imageRect.Height = image.Height;
- }
- }
- else if (sizeMode == ImageSizeMode.ScaledToFit)
- {
- if (image.Width >= imageRect.Width || image.Height >= imageRect.Height)
- {
- double hScale = ((double) imageRect.Width) / ((double) image.Width);
- double vScale = ((double) imageRect.Height) / ((double) image.Height);
- double scale = Math.Min(hScale, vScale);
- imageRect.Width = (int) (((double) image.Width) * scale);
- imageRect.Height = (int) (((double) image.Height) * scale);
- }
- else
- {
- imageRect.Width = image.Width;
- imageRect.Height = image.Height;
- }
- }
- if (rowAlignment == RowAlignment.Center)
- {
- imageRect.Y += (this.ClientRectangle.Height - imageRect.Height) / 2;
- }
- else if (rowAlignment == RowAlignment.Bottom)
- {
- imageRect.Y = this.ClientRectangle.Bottom - imageRect.Height;
- }
- if (!this.DrawText)
- {
- if (columnAlignment == ColumnAlignment.Center)
- {
- imageRect.X += (this.ClientRectangle.Width - imageRect.Width) / 2;
- }
- else if (columnAlignment == ColumnAlignment.Right)
- {
- imageRect.X = this.ClientRectangle.Width - imageRect.Width;
- }
- }
- return imageRect;
- }
- #endregion
- #region Properties
-
-
-
- public bool DrawText
- {
- get
- {
- return this.drawText;
- }
- }
- #endregion
-
- #region Events
- #region Paint
-
-
-
-
-
- public override void OnPaintCell(PaintCellEventArgs e)
- {
- if (e.Table.ColumnModel.Columns[e.Column] is ImageColumn)
- {
- this.drawText = ((ImageColumn) e.Table.ColumnModel.Columns[e.Column]).DrawText;
- }
- else
- {
- this.drawText = true;
- }
-
- base.OnPaintCell(e);
- }
-
-
-
-
- protected override void OnPaint(PaintCellEventArgs e)
- {
- base.OnPaint(e);
-
-
- if (e.Cell == null || e.Cell.Image == null)
- {
- return;
- }
-
- Rectangle imageRect = this.CalcImageRect(e.Cell.Image, e.Cell.ImageSizeMode, this.LineAlignment, this.Alignment);
-
-
- bool scaled = (this.DrawText || e.Cell.ImageSizeMode != ImageSizeMode.Normal);
- this.DrawImage(e.Graphics, e.Cell.Image, imageRect, scaled, e.Table.Enabled);
-
- if (this.DrawText)
- {
- if (e.Cell.Text != null && e.Cell.Text.Length != 0)
- {
-
- Rectangle textRect = this.ClientRectangle;
-
-
-
- textRect.X += imageRect.Width;
- textRect.Width -= imageRect.Width;
-
- if (textRect.Width > 0)
- {
-
- if (e.Enabled)
- {
- e.Graphics.DrawString(e.Cell.Text, this.Font, this.ForeBrush, textRect, this.StringFormat);
- }
- else
- {
- e.Graphics.DrawString(e.Cell.Text, this.Font, this.GrayTextBrush, textRect, this.StringFormat);
- }
- }
- }
- }
-
- if (e.Focused && e.Enabled)
- {
- ControlPaint.DrawFocusRectangle(e.Graphics, this.ClientRectangle);
- }
- }
-
-
-
-
-
-
-
-
-
-
- protected void DrawImage(Graphics g, Image image, Rectangle imageRect, bool scaled, bool enabled)
- {
- if (scaled)
- {
- if (enabled)
- {
- g.DrawImage(image, imageRect);
- }
- else
- {
- using (Image im = new Bitmap(image, imageRect.Width, imageRect.Height))
- {
- ControlPaint.DrawImageDisabled(g, im, imageRect.X, imageRect.Y, this.BackBrush.Color);
- }
- }
- }
- else
- {
- if (enabled)
- {
- g.DrawImageUnscaled(image, imageRect);
- }
- else
- {
- ControlPaint.DrawImageDisabled(g, image, imageRect.X, imageRect.Y, this.BackBrush.Color);
- }
- }
- }
- #endregion
- #endregion
- }
- }
|