/*
* Copyright � 2005, Mathew Hall
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using XPTable.Events;
using XPTable.Models;
using XPTable.Renderers;
namespace XPTableDemo.MediaPlayerStyle
{
/// <summary>
/// A CellRenderer that draws Cell contents as rating stars
/// </summary>
public class RatingCellRenderer : CellRenderer
{
#region Constructor
/// <summary>
/// Initializes a new instance of the RatingCellRenderer class with
/// default settings
/// </summary>
public RatingCellRenderer() : base()
{
}
#endregion
#region Methods
/// <summary>
/// Gets the Rectangle that specifies the Size and Location of
/// the Image contained in the current Cell
/// </summary>
/// <param name="image">The Image to be drawn</param>
/// <param name="rowAlignment">The alignment of the current Cell's row</param>
/// <returns>A Rectangle that specifies the Size and Location of
/// the Image contained in the current Cell</returns>
protected Rectangle CalcImageRect(Image image, RowAlignment rowAlignment)
{
Rectangle imageRect = this.ClientRectangle;
if (image.Width < imageRect.Width)
{
imageRect.Width = image.Width;
}
if (image.Height < imageRect.Height)
{
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;
}
return imageRect;
}
#endregion
#region Events
#region Paint
/// <summary>
/// Raises the PaintCell event
/// </summary>
/// <param name="e">A PaintCellEventArgs that contains the event data</param>
public override void OnPaintCell(PaintCellEventArgs e)
{
base.OnPaintCell(e);
}
/// <summary>
/// Raises the Paint event
/// </summary>
/// <param name="e">A PaintCellEventArgs that contains the event data</param>
protected override void OnPaint(PaintCellEventArgs e)
{
base.OnPaint(e);
// don't bother if the Cell is null or doesn't have an image
if (e.Cell == null || e.Cell.Text == null || e.Cell.Image == null)
{
return;
}
int stars = Convert.ToInt32(e.Cell.Text);
// work out the size and location of the image
Rectangle imageRect = this.CalcImageRect(e.Cell.Image, this.LineAlignment);
float alpha;
for (int i=1; i<=5; i++)
{
alpha = (i <= stars) ? 1f : 0.4f;
this.DrawImage(e.Graphics, e.Cell.Image, imageRect, alpha);
imageRect.X += imageRect.Width;
}
if (e.Focused)
{
ControlPaint.DrawFocusRectangle(e.Graphics, this.ClientRectangle);
}
}
/// <summary>
/// Draws the Image contained in the Cell
/// </summary>
/// <param name="g">The Graphics used to paint the Image</param>
/// <param name="image">The Image to be drawn</param>
/// <param name="imageRect">A rectangle that specifies the Size and
/// Location of the Image</param>
/// <param name="alpha">Specifies the opacity of the image</param>
protected void DrawImage(Graphics g, Image image, Rectangle imageRect, float alpha)
{
if (alpha == 1f)
{
g.DrawImage(image, imageRect);
}
else
{
float[][] ptsArray = {new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, alpha, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
}
}
#endregion
#endregion
}
}