Imported existing sources

This commit is contained in:
Daniel Brunner committed 2017-02-04 13:47:58 +01:00
1 parent 14a5be61cc
commit 7c97b9e539
86 files changed
+49665

No files matched your search

+743
View File
@@ -0,0 +1,743 @@
/*
* Adornments - Adornments are the basis for overlays and decorations -- things that can be rendered over the top of a ListView
*
* Author: Phillip Piper
* Date: 16/08/2009 1:02 AM
*
* Change log:
* v2.6
* 2012-08-18 JPP - Correctly dispose of brush and pen resources
* v2.3
* 2009-09-22 JPP - Added Wrap property to TextAdornment, to allow text wrapping to be disabled
* - Added ShrinkToWidth property to ImageAdornment
* 2009-08-17 JPP - Initial version
*
* To do:
* - Use IPointLocator rather than Corners
* - Add RotationCenter property ratherr than always using middle center
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace BrightIdeasSoftware
{
/// <summary>
/// An adorment is the common base for overlays and decorations.
/// </summary>
public class GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the corner of the adornment that will be positioned at the reference corner
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Drawing.ContentAlignment AdornmentCorner {
get { return this.adornmentCorner; }
set { this.adornmentCorner = value; }
}
private System.Drawing.ContentAlignment adornmentCorner = System.Drawing.ContentAlignment.MiddleCenter;
/// <summary>
/// Gets or sets location within the reference rectange where the adornment will be drawn
/// </summary>
/// <remarks>This is a simplied interface to ReferenceCorner and AdornmentCorner </remarks>
[Category("ObjectListView"),
Description("How will the adornment be aligned"),
DefaultValue(System.Drawing.ContentAlignment.BottomRight),
NotifyParentProperty(true)]
public System.Drawing.ContentAlignment Alignment {
get { return this.alignment; }
set {
this.alignment = value;
this.ReferenceCorner = value;
this.AdornmentCorner = value;
}
}
private System.Drawing.ContentAlignment alignment = System.Drawing.ContentAlignment.BottomRight;
/// <summary>
/// Gets or sets the offset by which the position of the adornment will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The offset by which the position of the adornment will be adjusted"),
DefaultValue(typeof(Size), "0,0")]
public Size Offset {
get { return this.offset; }
set { this.offset = value; }
}
private Size offset = new Size();
/// <summary>
/// Gets or sets the point of the reference rectangle to which the adornment will be aligned.
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Drawing.ContentAlignment ReferenceCorner {
get { return this.referenceCorner; }
set { this.referenceCorner = value; }
}
private System.Drawing.ContentAlignment referenceCorner = System.Drawing.ContentAlignment.MiddleCenter;
/// <summary>
/// Gets or sets the degree of rotation by which the adornment will be transformed.
/// The centre of rotation will be the center point of the adornment.
/// </summary>
[Category("ObjectListView"),
Description("The degree of rotation that will be applied to the adornment."),
DefaultValue(0),
NotifyParentProperty(true)]
public int Rotation {
get { return this.rotation; }
set { this.rotation = value; }
}
private int rotation;
/// <summary>
/// Gets or sets the transparency of the overlay.
/// 0 is completely transparent, 255 is completely opaque.
/// </summary>
[Category("ObjectListView"),
Description("The transparency of this adornment. 0 is completely transparent, 255 is completely opaque."),
DefaultValue(128)]
public int Transparency {
get { return this.transparency; }
set { this.transparency = Math.Min(255, Math.Max(0, value)); }
}
private int transparency = 128;
#endregion
#region Calculations
/// <summary>
/// Calculate the location of rectangle of the given size,
/// so that it's indicated corner would be at the given point.
/// </summary>
/// <param name="pt">The point</param>
/// <param name="size"></param>
/// <param name="corner">Which corner will be positioned at the reference point</param>
/// <returns></returns>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.TopLeft) -> Point(50, 100)</example>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.MiddleCenter) -> Point(45, 90)</example>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.BottomRight) -> Point(40, 80)</example>
public virtual Point CalculateAlignedPosition(Point pt, Size size, System.Drawing.ContentAlignment corner) {
switch (corner) {
case System.Drawing.ContentAlignment.TopLeft:
return pt;
case System.Drawing.ContentAlignment.TopCenter:
return new Point(pt.X - (size.Width / 2), pt.Y);
case System.Drawing.ContentAlignment.TopRight:
return new Point(pt.X - size.Width, pt.Y);
case System.Drawing.ContentAlignment.MiddleLeft:
return new Point(pt.X, pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.MiddleCenter:
return new Point(pt.X - (size.Width / 2), pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.MiddleRight:
return new Point(pt.X - size.Width, pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.BottomLeft:
return new Point(pt.X, pt.Y - size.Height);
case System.Drawing.ContentAlignment.BottomCenter:
return new Point(pt.X - (size.Width / 2), pt.Y - size.Height);
case System.Drawing.ContentAlignment.BottomRight:
return new Point(pt.X - size.Width, pt.Y - size.Height);
}
// Should never reach here
return pt;
}
/// <summary>
/// Calculate a rectangle that has the given size which is positioned so that
/// its alignment point is at the reference location of the given rect.
/// </summary>
/// <param name="r"></param>
/// <param name="sz"></param>
/// <returns></returns>
public virtual Rectangle CreateAlignedRectangle(Rectangle r, Size sz) {
return this.CreateAlignedRectangle(r, sz, this.ReferenceCorner, this.AdornmentCorner, this.Offset);
}
/// <summary>
/// Create a rectangle of the given size which is positioned so that
/// its indicated corner is at the indicated corner of the reference rect.
/// </summary>
/// <param name="r"></param>
/// <param name="sz"></param>
/// <param name="corner"></param>
/// <param name="referenceCorner"></param>
/// <param name="offset"></param>
/// <returns></returns>
/// <remarks>
/// <para>Creates a rectangle so that its bottom left is at the centre of the reference:
/// corner=BottomLeft, referenceCorner=MiddleCenter</para>
/// <para>This is a powerful concept that takes some getting used to, but is
/// very neat once you understand it.</para>
/// </remarks>
public virtual Rectangle CreateAlignedRectangle(Rectangle r, Size sz,
System.Drawing.ContentAlignment corner, System.Drawing.ContentAlignment referenceCorner, Size offset) {
Point referencePt = this.CalculateCorner(r, referenceCorner);
Point topLeft = this.CalculateAlignedPosition(referencePt, sz, corner);
return new Rectangle(topLeft + offset, sz);
}
/// <summary>
/// Return the point at the indicated corner of the given rectangle (it doesn't
/// have to be a corner, but a named location)
/// </summary>
/// <param name="r">The reference rectangle</param>
/// <param name="corner">Which point of the rectangle should be returned?</param>
/// <returns>A point</returns>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.TopLeft) -> Point(0, 0)</example>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.MiddleCenter) -> Point(25, 50)</example>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.BottomRight) -> Point(50, 100)</example>
public virtual Point CalculateCorner(Rectangle r, System.Drawing.ContentAlignment corner) {
switch (corner) {
case System.Drawing.ContentAlignment.TopLeft:
return new Point(r.Left, r.Top);
case System.Drawing.ContentAlignment.TopCenter:
return new Point(r.X + (r.Width / 2), r.Top);
case System.Drawing.ContentAlignment.TopRight:
return new Point(r.Right, r.Top);
case System.Drawing.ContentAlignment.MiddleLeft:
return new Point(r.Left, r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.MiddleCenter:
return new Point(r.X + (r.Width / 2), r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.MiddleRight:
return new Point(r.Right, r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.BottomLeft:
return new Point(r.Left, r.Bottom);
case System.Drawing.ContentAlignment.BottomCenter:
return new Point(r.X + (r.Width / 2), r.Bottom);
case System.Drawing.ContentAlignment.BottomRight:
return new Point(r.Right, r.Bottom);
}
// Should never reach here
return r.Location;
}
/// <summary>
/// Given the item and the subitem, calculate its bounds.
/// </summary>
/// <param name="item"></param>
/// <param name="subItem"></param>
/// <returns></returns>
public virtual Rectangle CalculateItemBounds(OLVListItem item, OLVListSubItem subItem) {
if (item == null)
return Rectangle.Empty;
if (subItem == null)
return item.Bounds;
return item.GetSubItemBounds(item.SubItems.IndexOf(subItem));
}
#endregion
#region Commands
/// <summary>
/// Apply any specified rotation to the Graphic content.
/// </summary>
/// <param name="g">The Graphics to be transformed</param>
/// <param name="r">The rotation will be around the centre of this rect</param>
protected virtual void ApplyRotation(Graphics g, Rectangle r) {
if (this.Rotation == 0)
return;
// THINK: Do we want to reset the transform? I think we want to push a new transform
g.ResetTransform();
Matrix m = new Matrix();
m.RotateAt(this.Rotation, new Point(r.Left + r.Width / 2, r.Top + r.Height / 2));
g.Transform = m;
}
/// <summary>
/// Reverse the rotation created by ApplyRotation()
/// </summary>
/// <param name="g"></param>
protected virtual void UnapplyRotation(Graphics g) {
if (this.Rotation != 0)
g.ResetTransform();
}
#endregion
}
/// <summary>
/// An overlay that will draw an image over the top of the ObjectListView
/// </summary>
public class ImageAdornment : GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the image that will be drawn
/// </summary>
[Category("ObjectListView"),
Description("The image that will be drawn"),
DefaultValue(null),
NotifyParentProperty(true)]
public Image Image {
get { return this.image; }
set { this.image = value; }
}
private Image image;
/// <summary>
/// Gets or sets if the image will be shrunk to fit with its horizontal bounds
/// </summary>
[Category("ObjectListView"),
Description("Will the image be shrunk to fit within its width?"),
DefaultValue(false)]
public bool ShrinkToWidth {
get { return this.shrinkToWidth; }
set { this.shrinkToWidth = value; }
}
private bool shrinkToWidth;
#endregion
#region Commands
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void DrawImage(Graphics g, Rectangle r) {
if (this.ShrinkToWidth)
this.DrawScaledImage(g, r, this.Image, this.Transparency);
else
this.DrawImage(g, r, this.Image, this.Transparency);
}
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawImage(Graphics g, Rectangle r, Image image, int transparency) {
if (image != null)
this.DrawImage(g, r, image, image.Size, transparency);
}
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="sz">How big should the image be?</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawImage(Graphics g, Rectangle r, Image image, Size sz, int transparency) {
if (image == null)
return;
Rectangle adornmentBounds = this.CreateAlignedRectangle(r, sz);
try {
this.ApplyRotation(g, adornmentBounds);
this.DrawTransparentBitmap(g, adornmentBounds, image, transparency);
}
finally {
this.UnapplyRotation(g);
}
}
/// <summary>
/// Draw the image in its specified location, scaled so that it is not wider
/// than the given rectangle. Height is scaled proportional to the width.
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawScaledImage(Graphics g, Rectangle r, Image image, int transparency) {
if (image == null)
return;
// If the image is too wide to be drawn in the space provided, proportionally scale it down.
// Too tall images are not scaled.
Size size = image.Size;
if (image.Width > r.Width) {
float scaleRatio = (float)r.Width / (float)image.Width;
size.Height = (int)((float)image.Height * scaleRatio);
size.Width = r.Width - 1;
}
this.DrawImage(g, r, image, size, transparency);
}
/// <summary>
/// Utility to draw a bitmap transparenly.
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="image"></param>
/// <param name="transparency"></param>
protected virtual void DrawTransparentBitmap(Graphics g, Rectangle r, Image image, int transparency) {
ImageAttributes imageAttributes = null;
if (transparency != 255) {
imageAttributes = new ImageAttributes();
float a = (float)transparency / 255.0f;
float[][] colorMatrixElements = {
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, a, 0},
new float[] {0, 0, 0, 0, 1}};
imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrixElements));
}
g.DrawImage(image,
r, // destination rectangle
0, 0, image.Size.Width, image.Size.Height, // source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
#endregion
}
/// <summary>
/// An adornment that will draw text
/// </summary>
public class TextAdornment : GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the background color of the text
/// Set this to Color.Empty to not draw a background
/// </summary>
[Category("ObjectListView"),
Description("The background color of the text"),
DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor = Color.Empty;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Brush BackgroundBrush {
get {
return new SolidBrush(Color.FromArgb(this.workingTransparency, this.BackColor));
}
}
/// <summary>
/// Gets or sets the color of the border around the billboard.
/// Set this to Color.Empty to remove the border
/// </summary>
[Category("ObjectListView"),
Description("The color of the border around the text"),
DefaultValue(typeof(Color), "")]
public Color BorderColor {
get { return this.borderColor; }
set { this.borderColor = value; }
}
private Color borderColor = Color.Empty;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Pen BorderPen {
get {
return new Pen(Color.FromArgb(this.workingTransparency, this.BorderColor), this.BorderWidth);
}
}
/// <summary>
/// Gets or sets the width of the border around the text
/// </summary>
[Category("ObjectListView"),
Description("The width of the border around the text"),
DefaultValue(0.0f)]
public float BorderWidth {
get { return this.borderWidth; }
set { this.borderWidth = value; }
}
private float borderWidth;
/// <summary>
/// How rounded should the corners of the border be? 0 means no rounding.
/// </summary>
/// <remarks>If this value is too large, the edges of the border will appear odd.</remarks>
[Category("ObjectListView"),
Description("How rounded should the corners of the border be? 0 means no rounding."),
DefaultValue(16.0f),
NotifyParentProperty(true)]
public float CornerRounding {
get { return this.cornerRounding; }
set { this.cornerRounding = value; }
}
private float cornerRounding = 16.0f;
/// <summary>
/// Gets or sets the font that will be used to draw the text
/// </summary>
[Category("ObjectListView"),
Description("The font that will be used to draw the text"),
DefaultValue(null),
NotifyParentProperty(true)]
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets the font that will be used to draw the text or a reasonable default
/// </summary>
[Browsable(false)]
public Font FontOrDefault {
get {
return this.Font ?? new Font("Tahoma", 16);
}
}
/// <summary>
/// Does this text have a background?
/// </summary>
[Browsable(false)]
public bool HasBackground {
get {
return this.BackColor != Color.Empty;
}
}
/// <summary>
/// Does this overlay have a border?
/// </summary>
[Browsable(false)]
public bool HasBorder {
get {
return this.BorderColor != Color.Empty && this.BorderWidth > 0;
}
}
/// <summary>
/// Gets or sets the maximum width of the text. Text longer than this will wrap.
/// 0 means no maximum.
/// </summary>
[Category("ObjectListView"),
Description("The maximum width the text (0 means no maximum). Text longer than this will wrap"),
DefaultValue(0)]
public int MaximumTextWidth {
get { return this.maximumTextWidth; }
set { this.maximumTextWidth = value; }
}
private int maximumTextWidth;
/// <summary>
/// Gets or sets the formatting that should be used on the text
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual StringFormat StringFormat {
get {
if (this.stringFormat == null) {
this.stringFormat = new StringFormat();
this.stringFormat.Alignment = StringAlignment.Center;
this.stringFormat.LineAlignment = StringAlignment.Center;
this.stringFormat.Trimming = StringTrimming.EllipsisCharacter;
if (!this.Wrap)
this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
}
return this.stringFormat;
}
set { this.stringFormat = value; }
}
private StringFormat stringFormat;
/// <summary>
/// Gets or sets the text that will be drawn
/// </summary>
[Category("ObjectListView"),
Description("The text that will be drawn over the top of the ListView"),
DefaultValue(null),
NotifyParentProperty(true),
Localizable(true)]
public string Text {
get { return this.text; }
set { this.text = value; }
}
private string text;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Brush TextBrush {
get {
return new SolidBrush(Color.FromArgb(this.workingTransparency, this.TextColor));
}
}
/// <summary>
/// Gets or sets the color of the text
/// </summary>
[Category("ObjectListView"),
Description("The color of the text"),
DefaultValue(typeof(Color), "DarkBlue"),
NotifyParentProperty(true)]
public Color TextColor {
get { return this.textColor; }
set { this.textColor = value; }
}
private Color textColor = Color.DarkBlue;
/// <summary>
/// Gets or sets whether the text will wrap when it exceeds its bounds
/// </summary>
[Category("ObjectListView"),
Description("Will the text wrap?"),
DefaultValue(true)]
public bool Wrap {
get { return this.wrap; }
set { this.wrap = value; }
}
private bool wrap = true;
#endregion
#region Implementation
/// <summary>
/// Draw our text with our stored configuration in relation to the given
/// reference rectangle
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The reference rectangle in relation to which the text will be drawn</param>
public virtual void DrawText(Graphics g, Rectangle r) {
this.DrawText(g, r, this.Text, this.Transparency);
}
/// <summary>
/// Draw the given text with our stored configuration
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The reference rectangle in relation to which the text will be drawn</param>
/// <param name="s">The text to draw</param>
/// <param name="transparency">How opaque should be text be</param>
public virtual void DrawText(Graphics g, Rectangle r, string s, int transparency) {
if (String.IsNullOrEmpty(s))
return;
Rectangle textRect = this.CalculateTextBounds(g, r, s);
this.DrawBorderedText(g, textRect, s, transparency);
}
/// <summary>
/// Draw the text with a border
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="textRect">The bounds within which the text should be drawn</param>
/// <param name="text">The text to draw</param>
/// <param name="transparency">How opaque should be text be</param>
protected virtual void DrawBorderedText(Graphics g, Rectangle textRect, string text, int transparency) {
Rectangle borderRect = textRect;
borderRect.Inflate((int)this.BorderWidth / 2, (int)this.BorderWidth / 2);
borderRect.Y -= 1; // Looker better a little higher
try {
this.ApplyRotation(g, textRect);
using (GraphicsPath path = this.GetRoundedRect(borderRect, this.CornerRounding)) {
this.workingTransparency = transparency;
if (this.HasBackground) {
using (Brush b = this.BackgroundBrush)
g.FillPath(b, path);
}
using (Brush b = this.TextBrush)
g.DrawString(text, this.FontOrDefault, b, textRect, this.StringFormat);
if (this.HasBorder) {
using (Pen p = this.BorderPen)
g.DrawPath(p, path);
}
}
}
finally {
this.UnapplyRotation(g);
}
}
/// <summary>
/// Return the rectangle that will be the precise bounds of the displayed text
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="s"></param>
/// <returns>The bounds of the text</returns>
protected virtual Rectangle CalculateTextBounds(Graphics g, Rectangle r, string s) {
int maxWidth = this.MaximumTextWidth <= 0 ? r.Width : this.MaximumTextWidth;
SizeF sizeF = g.MeasureString(s, this.FontOrDefault, maxWidth, this.StringFormat);
Size size = new Size(1 + (int)sizeF.Width, 1 + (int)sizeF.Height);
return this.CreateAlignedRectangle(r, size);
}
/// <summary>
/// Return a GraphicPath that is a round cornered rectangle
/// </summary>
/// <param name="rect">The rectangle</param>
/// <param name="diameter">The diameter of the corners</param>
/// <returns>A round cornered rectagle path</returns>
/// <remarks>If I could rely on people using C# 3.0+, this should be
/// an extension method of GraphicsPath.</remarks>
protected virtual GraphicsPath GetRoundedRect(Rectangle rect, float diameter) {
GraphicsPath path = new GraphicsPath();
if (diameter > 0) {
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
} else {
path.AddRectangle(rect);
}
return path;
}
#endregion
private int workingTransparency;
}
}
+820
View File
@@ -0,0 +1,820 @@
/*
* Decorations - Images, text or other things that can be rendered onto an ObjectListView
*
* Author: Phillip Piper
* Date: 19/08/2009 10:56 PM
*
* Change log:
* 2011-04-04 JPP - Added ability to have a gradient background on BorderDecoration
* v2.4
* 2010-04-15 JPP - Tweaked LightBoxDecoration a little
* v2.3
* 2009-09-23 JPP - Added LeftColumn and RightColumn to RowBorderDecoration
* 2009-08-23 JPP - Added LightBoxDecoration
* 2009-08-19 JPP - Initial version. Separated from Overlays.cs
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A decoration is an overlay that draws itself in relation to a given row or cell.
/// Decorations scroll when the listview scrolls.
/// </summary>
public interface IDecoration : IOverlay
{
/// <summary>
/// Gets or sets the row that is to be decorated
/// </summary>
OLVListItem ListItem { get; set; }
/// <summary>
/// Gets or sets the subitem that is to be decorated
/// </summary>
OLVListSubItem SubItem { get; set; }
}
/// <summary>
/// An AbstractDecoration is a safe do-nothing implementation of the IDecoration interface
/// </summary>
public class AbstractDecoration : IDecoration
{
#region IDecoration Members
/// <summary>
/// Gets or sets the row that is to be decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the subitem that is to be decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Public properties
/// <summary>
/// Gets the bounds of the decorations row
/// </summary>
public Rectangle RowBounds {
get {
if (this.ListItem == null)
return Rectangle.Empty;
else
return this.ListItem.Bounds;
}
}
/// <summary>
/// Get the bounds of the decorations cell
/// </summary>
public Rectangle CellBounds {
get {
if (this.ListItem == null || this.SubItem == null)
return Rectangle.Empty;
else
return this.ListItem.GetSubItemBounds(this.ListItem.SubItems.IndexOf(this.SubItem));
}
}
#endregion
#region IOverlay Members
/// <summary>
/// Draw the decoration
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
}
#endregion
}
/// <summary>
/// This decoration draws a slight tint over a column of the
/// owning listview. If no column is explicitly set, the selected
/// column in the listview will be used.
/// The selected column is normally the sort column, but does not have to be.
/// </summary>
public class TintedColumnDecoration : AbstractDecoration
{
#region Constructors
/// <summary>
/// Create a TintedColumnDecoration
/// </summary>
public TintedColumnDecoration() {
this.Tint = Color.FromArgb(15, Color.Blue);
}
/// <summary>
/// Create a TintedColumnDecoration
/// </summary>
/// <param name="column"></param>
public TintedColumnDecoration(OLVColumn column)
: this() {
this.ColumnToTint = column;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the column that will be tinted
/// </summary>
public OLVColumn ColumnToTint {
get { return this.columnToTint; }
set { this.columnToTint = value; }
}
private OLVColumn columnToTint;
/// <summary>
/// Gets or sets the color that will be 'tinted' over the selected column
/// </summary>
public Color Tint {
get { return this.tint; }
set {
if (this.tint == value)
return;
if (this.tintBrush != null) {
this.tintBrush.Dispose();
this.tintBrush = null;
}
this.tint = value;
this.tintBrush = new SolidBrush(this.tint);
}
}
private Color tint;
private SolidBrush tintBrush;
#endregion
#region IOverlay Members
/// <summary>
/// Draw a slight colouring over our tinted column
/// </summary>
/// <remarks>
/// This overlay only works when:
/// - the list is in Details view
/// - there is at least one row
/// - there is a selected column (or a specified tint column)
/// </remarks>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (olv.View != System.Windows.Forms.View.Details)
return;
if (olv.GetItemCount() == 0)
return;
OLVColumn column = this.ColumnToTint ?? olv.SelectedColumn;
if (column == null)
return;
Point sides = NativeMethods.GetScrolledColumnSides(olv, column.Index);
if (sides.X == -1)
return;
Rectangle columnBounds = new Rectangle(sides.X, r.Top, sides.Y - sides.X, r.Bottom);
// Find the bottom of the last item. The tinting should extend only to there.
OLVListItem lastItem = olv.GetLastItemInDisplayOrder();
if (lastItem != null) {
Rectangle lastItemBounds = lastItem.Bounds;
if (!lastItemBounds.IsEmpty && lastItemBounds.Bottom < columnBounds.Bottom)
columnBounds.Height = lastItemBounds.Bottom - columnBounds.Top;
}
g.FillRectangle(this.tintBrush, columnBounds);
}
#endregion
}
/// <summary>
/// This decoration draws an optionally filled border around a rectangle.
/// Subclasses must override CalculateBounds().
/// </summary>
public class BorderDecoration : AbstractDecoration
{
#region Constructors
/// <summary>
/// Create a BorderDecoration
/// </summary>
public BorderDecoration()
: this(new Pen(Color.FromArgb(64, Color.Blue), 1)) {
}
/// <summary>
/// Create a BorderDecoration
/// </summary>
/// <param name="borderPen">The pen used to draw the border</param>
public BorderDecoration(Pen borderPen) {
this.BorderPen = borderPen;
}
/// <summary>
/// Create a BorderDecoration
/// </summary>
/// <param name="borderPen">The pen used to draw the border</param>
/// <param name="fill">The brush used to fill the rectangle</param>
public BorderDecoration(Pen borderPen, Brush fill) {
this.BorderPen = borderPen;
this.FillBrush = fill;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the pen that will be used to draw the border
/// </summary>
public Pen BorderPen {
get { return this.borderPen; }
set { this.borderPen = value; }
}
private Pen borderPen;
/// <summary>
/// Gets or sets the padding that will be added to the bounds of the item
/// before drawing the border and fill.
/// </summary>
public Size BoundsPadding {
get { return this.boundsPadding; }
set { this.boundsPadding = value; }
}
private Size boundsPadding = new Size(-1, 2);
/// <summary>
/// How rounded should the corners of the border be? 0 means no rounding.
/// </summary>
/// <remarks>If this value is too large, the edges of the border will appear odd.</remarks>
public float CornerRounding {
get { return this.cornerRounding; }
set { this.cornerRounding = value; }
}
private float cornerRounding = 16.0f;
/// <summary>
/// Gets or sets the brush that will be used to fill the border
/// </summary>
/// <remarks>This value is ignored when using gradient brush</remarks>
public Brush FillBrush {
get { return this.fillBrush; }
set { this.fillBrush = value; }
}
private Brush fillBrush = new SolidBrush(Color.FromArgb(64, Color.Blue));
/// <summary>
/// Gets or sets the color that will be used as the start of a gradient fill.
/// </summary>
/// <remarks>This and FillGradientTo must be given value to show a gradient</remarks>
public Color? FillGradientFrom {
get { return this.fillGradientFrom; }
set { this.fillGradientFrom = value; }
}
private Color? fillGradientFrom;
/// <summary>
/// Gets or sets the color that will be used as the end of a gradient fill.
/// </summary>
/// <remarks>This and FillGradientFrom must be given value to show a gradient</remarks>
public Color? FillGradientTo {
get { return this.fillGradientTo; }
set { this.fillGradientTo = value; }
}
private Color? fillGradientTo;
/// <summary>
/// Gets or sets the fill mode that will be used for the gradient.
/// </summary>
public LinearGradientMode FillGradientMode {
get { return this.fillGradientMode; }
set { this.fillGradientMode = value; }
}
private LinearGradientMode fillGradientMode = LinearGradientMode.Vertical;
#endregion
#region IOverlay Members
/// <summary>
/// Draw a filled border
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
Rectangle bounds = this.CalculateBounds();
if (!bounds.IsEmpty)
this.DrawFilledBorder(g, bounds);
}
#endregion
#region Subclass responsibility
/// <summary>
/// Subclasses should override this to say where the border should be drawn
/// </summary>
/// <returns></returns>
protected virtual Rectangle CalculateBounds() {
return Rectangle.Empty;
}
#endregion
#region Implementation utlities
/// <summary>
/// Do the actual work of drawing the filled border
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
protected void DrawFilledBorder(Graphics g, Rectangle bounds) {
bounds.Inflate(this.BoundsPadding);
GraphicsPath path = this.GetRoundedRect(bounds, this.CornerRounding);
if (this.FillGradientFrom != null && this.FillGradientTo != null) {
if (this.FillBrush != null)
this.FillBrush.Dispose();
this.FillBrush = new LinearGradientBrush(bounds, this.FillGradientFrom.Value, this.FillGradientTo.Value, this.FillGradientMode);
}
if (this.FillBrush != null)
g.FillPath(this.FillBrush, path);
if (this.BorderPen != null)
g.DrawPath(this.BorderPen, path);
}
/// <summary>
/// Create a GraphicsPath that represents a round cornered rectangle.
/// </summary>
/// <param name="rect"></param>
/// <param name="diameter">If this is 0 or less, the rectangle will not be rounded.</param>
/// <returns></returns>
protected GraphicsPath GetRoundedRect(RectangleF rect, float diameter) {
GraphicsPath path = new GraphicsPath();
if (diameter <= 0.0f) {
path.AddRectangle(rect);
} else {
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
}
return path;
}
#endregion
}
/// <summary>
/// Instances of this class draw a border around the decorated row
/// </summary>
public class RowBorderDecoration : BorderDecoration
{
/// <summary>
/// Gets or sets the index of the left most column to be used for the border
/// </summary>
public int LeftColumn {
get { return leftColumn; }
set { leftColumn = value; }
}
private int leftColumn = -1;
/// <summary>
/// Gets or sets the index of the right most column to be used for the border
/// </summary>
public int RightColumn {
get { return rightColumn; }
set { rightColumn = value; }
}
private int rightColumn = -1;
/// <summary>
/// Calculate the boundaries of the border
/// </summary>
/// <returns></returns>
protected override Rectangle CalculateBounds() {
Rectangle bounds = this.RowBounds;
if (this.ListItem == null)
return bounds;
if (this.LeftColumn >= 0) {
Rectangle leftCellBounds = this.ListItem.GetSubItemBounds(this.LeftColumn);
if (!leftCellBounds.IsEmpty) {
bounds.Width = bounds.Right - leftCellBounds.Left;
bounds.X = leftCellBounds.Left;
}
}
if (this.RightColumn >= 0) {
Rectangle rightCellBounds = this.ListItem.GetSubItemBounds(this.RightColumn);
if (!rightCellBounds.IsEmpty) {
bounds.Width = rightCellBounds.Right - bounds.Left;
}
}
return bounds;
}
}
/// <summary>
/// Instances of this class draw a border around the decorated subitem.
/// </summary>
public class CellBorderDecoration : BorderDecoration
{
/// <summary>
/// Calculate the boundaries of the border
/// </summary>
/// <returns></returns>
protected override Rectangle CalculateBounds() {
return this.CellBounds;
}
}
/// <summary>
/// This decoration puts a border around the cell being edited and
/// optionally "lightboxes" the cell (makes the rest of the control dark).
/// </summary>
public class EditingCellBorderDecoration : BorderDecoration
{
#region Life and death
/// <summary>
/// Create a EditingCellBorderDecoration
/// </summary>
public EditingCellBorderDecoration() {
this.FillBrush = null;
this.BorderPen = new Pen(Color.DarkBlue, 2);
this.CornerRounding = 8;
this.BoundsPadding = new Size(10, 8);
}
/// <summary>
/// Create a EditingCellBorderDecoration
/// </summary>
/// <param name="useLightBox">Should the decoration use a lighbox display style?</param>
public EditingCellBorderDecoration(bool useLightBox) : this()
{
this.UseLightbox = useLightbox;
}
#endregion
#region Configuration properties
/// <summary>
/// Gets or set whether the decoration should make the rest of
/// the control dark when a cell is being edited
/// </summary>
/// <remarks>If this is true, FillBrush is used to overpaint
/// the control.</remarks>
public bool UseLightbox {
get { return this.useLightbox; }
set {
if (this.useLightbox == value)
return;
this.useLightbox = value;
if (this.useLightbox) {
if (this.FillBrush == null)
this.FillBrush = new SolidBrush(Color.FromArgb(64, Color.Black));
}
}
}
private bool useLightbox;
#endregion
#region Implementation
/// <summary>
/// Draw the decoration
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (!olv.IsCellEditing)
return;
Rectangle bounds = olv.CellEditor.Bounds;
if (bounds.IsEmpty)
return;
bounds.Inflate(this.BoundsPadding);
GraphicsPath path = this.GetRoundedRect(bounds, this.CornerRounding);
if (this.FillBrush != null) {
if (this.UseLightbox) {
using (Region newClip = new Region(r)) {
newClip.Exclude(path);
Region originalClip = g.Clip;
g.Clip = newClip;
g.FillRectangle(this.FillBrush, r);
g.Clip = originalClip;
}
} else {
g.FillPath(this.FillBrush, path);
}
}
if (this.BorderPen != null)
g.DrawPath(this.BorderPen, path);
}
#endregion
}
/// <summary>
/// This decoration causes everything *except* the row under the mouse to be overpainted
/// with a tint, making the row under the mouse stand out in comparison.
/// The darker and more opaque the fill color, the more obvious the
/// decorated row becomes.
/// </summary>
public class LightBoxDecoration : BorderDecoration
{
/// <summary>
/// Create a LightBoxDecoration
/// </summary>
public LightBoxDecoration() {
this.BoundsPadding = new Size(-1, 4);
this.CornerRounding = 8.0f;
this.FillBrush = new SolidBrush(Color.FromArgb(72, Color.Black));
}
/// <summary>
/// Draw a tint over everything in the ObjectListView except the
/// row under the mouse.
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (!r.Contains(olv.PointToClient(Cursor.Position)))
return;
Rectangle bounds = this.RowBounds;
if (bounds.IsEmpty) {
if (olv.View == View.Tile)
g.FillRectangle(this.FillBrush, r);
return;
}
using (Region newClip = new Region(r)) {
bounds.Inflate(this.BoundsPadding);
newClip.Exclude(this.GetRoundedRect(bounds, this.CornerRounding));
Region originalClip = g.Clip;
g.Clip = newClip;
g.FillRectangle(this.FillBrush, r);
g.Clip = originalClip;
}
}
}
/// <summary>
/// Instances of this class put an Image over the row/cell that it is decorating
/// </summary>
public class ImageDecoration : ImageAdornment, IDecoration
{
#region Constructors
/// <summary>
/// Create an image decoration
/// </summary>
public ImageDecoration() {
this.Alignment = ContentAlignment.MiddleRight;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
public ImageDecoration(Image image)
: this() {
this.Image = image;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="transparency"></param>
public ImageDecoration(Image image, int transparency)
: this() {
this.Image = image;
this.Transparency = transparency;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="alignment"></param>
public ImageDecoration(Image image, ContentAlignment alignment)
: this() {
this.Image = image;
this.Alignment = alignment;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="transparency"></param>
/// <param name="alignment"></param>
public ImageDecoration(Image image, int transparency, ContentAlignment alignment)
: this() {
this.Image = image;
this.Transparency = transparency;
this.Alignment = alignment;
}
#endregion
#region IDecoration Members
/// <summary>
/// Gets or sets the item being decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the sub item being decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Commands
/// <summary>
/// Draw this decoration
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
this.DrawImage(g, this.CalculateItemBounds(this.ListItem, this.SubItem));
}
#endregion
}
/// <summary>
/// Instances of this class draw some text over the row/cell that they are decorating
/// </summary>
public class TextDecoration : TextAdornment, IDecoration
{
#region Constructors
/// <summary>
/// Create a TextDecoration
/// </summary>
public TextDecoration() {
this.Alignment = ContentAlignment.MiddleRight;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
public TextDecoration(string text)
: this() {
this.Text = text;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="transparency"></param>
public TextDecoration(string text, int transparency)
: this() {
this.Text = text;
this.Transparency = transparency;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="alignment"></param>
public TextDecoration(string text, ContentAlignment alignment)
: this() {
this.Text = text;
this.Alignment = alignment;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="transparency"></param>
/// <param name="alignment"></param>
public TextDecoration(string text, int transparency, ContentAlignment alignment)
: this() {
this.Text = text;
this.Transparency = transparency;
this.Alignment = alignment;
}
#endregion
#region IDecoration Members
/// <summary>
/// Gets or sets the item being decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the sub item being decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Commands
/// <summary>
/// Draw this decoration
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
this.DrawText(g, this.CalculateItemBounds(this.ListItem, this.SubItem));
}
#endregion
}
}
+302
View File
@@ -0,0 +1,302 @@
/*
* Overlays - Images, text or other things that can be rendered over the top of a ListView
*
* Author: Phillip Piper
* Date: 14/04/2009 4:36 PM
*
* Change log:
* v2.3
* 2009-08-17 JPP - Overlays now use Adornments
* - Added ITransparentOverlay interface. Overlays can now have separate transparency levels
* 2009-08-10 JPP - Moved decoration related code to new file
* v2.2.1
* 200-07-24 JPP - TintedColumnDecoration now works when last item is a member of a collapsed
* group (well, it no longer crashes).
* v2.2
* 2009-06-01 JPP - Make sure that TintedColumnDecoration reaches to the last item in group view
* 2009-05-05 JPP - Unified BillboardOverlay text rendering with that of TextOverlay
* 2009-04-30 JPP - Added TintedColumnDecoration
* 2009-04-14 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace BrightIdeasSoftware
{
/// <summary>
/// The interface for an object which can draw itself over the top of
/// an ObjectListView.
/// </summary>
public interface IOverlay
{
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView that is being overlaid</param>
/// <param name="g">The Graphics onto the given OLV</param>
/// <param name="r">The content area of the OLV</param>
void Draw(ObjectListView olv, Graphics g, Rectangle r);
}
/// <summary>
/// An interface for an overlay that supports variable levels of transparency
/// </summary>
public interface ITransparentOverlay : IOverlay
{
/// <summary>
/// Gets or sets the transparency of the overlay.
/// 0 is completely transparent, 255 is completely opaque.
/// </summary>
int Transparency { get; set; }
}
/// <summary>
/// A null implementation of the IOverlay interface
/// </summary>
public class AbstractOverlay : ITransparentOverlay
{
#region IOverlay Members
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView that is being overlaid</param>
/// <param name="g">The Graphics onto the given OLV</param>
/// <param name="r">The content area of the OLV</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
}
#endregion
#region ITransparentOverlay Members
/// <summary>
/// How transparent should this overlay be?
/// </summary>
[Category("ObjectListView"),
Description("How transparent should this overlay be"),
DefaultValue(128),
NotifyParentProperty(true)]
public int Transparency {
get { return this.transparency; }
set { this.transparency = Math.Min(255, Math.Max(0, value)); }
}
private int transparency = 128;
#endregion
}
/// <summary>
/// An overlay that will draw an image over the top of the ObjectListView
/// </summary>
[TypeConverter("BrightIdeasSoftware.Design.OverlayConverter")]
public class ImageOverlay : ImageAdornment, ITransparentOverlay
{
/// <summary>
/// Create an ImageOverlay
/// </summary>
public ImageOverlay() {
this.Alignment = System.Drawing.ContentAlignment.BottomRight;
}
#region Public properties
/// <summary>
/// Gets or sets the horizontal inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The horizontal inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetX {
get { return this.insetX; }
set { this.insetX = Math.Max(0, value); }
}
private int insetX = 20;
/// <summary>
/// Gets or sets the vertical inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("Gets or sets the vertical inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetY {
get { return this.insetY; }
set { this.insetY = Math.Max(0, value); }
}
private int insetY = 20;
#endregion
#region Commands
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
Rectangle insetRect = r;
insetRect.Inflate(-this.InsetX, -this.InsetY);
// We hard code a transparency of 255 here since transparency is handled by the glass panel
this.DrawImage(g, insetRect, this.Image, 255);
}
#endregion
}
/// <summary>
/// An overlay that will draw text over the top of the ObjectListView
/// </summary>
[TypeConverter("BrightIdeasSoftware.Design.OverlayConverter")]
public class TextOverlay : TextAdornment, ITransparentOverlay
{
/// <summary>
/// Create a TextOverlay
/// </summary>
public TextOverlay() {
this.Alignment = System.Drawing.ContentAlignment.BottomRight;
}
#region Public properties
/// <summary>
/// Gets or sets the horizontal inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The horizontal inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetX {
get { return this.insetX; }
set { this.insetX = Math.Max(0, value); }
}
private int insetX = 20;
/// <summary>
/// Gets or sets the vertical inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("Gets or sets the vertical inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetY {
get { return this.insetY; }
set { this.insetY = Math.Max(0, value); }
}
private int insetY = 20;
/// <summary>
/// Gets or sets whether the border will be drawn with rounded corners
/// </summary>
[Browsable(false),
Obsolete("Use CornerRounding instead", false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool RoundCorneredBorder {
get { return this.CornerRounding > 0; }
set {
if (value)
this.CornerRounding = 16.0f;
else
this.CornerRounding = 0.0f;
}
}
#endregion
#region Commands
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (String.IsNullOrEmpty(this.Text))
return;
Rectangle insetRect = r;
insetRect.Inflate(-this.InsetX, -this.InsetY);
// We hard code a transparency of 255 here since transparency is handled by the glass panel
this.DrawText(g, insetRect, this.Text, 255);
}
#endregion
}
/// <summary>
/// A Billboard overlay is a TextOverlay positioned at an absolute point
/// </summary>
public class BillboardOverlay : TextOverlay
{
/// <summary>
/// Create a BillboardOverlay
/// </summary>
public BillboardOverlay() {
this.Transparency = 255;
this.BackColor = Color.PeachPuff;
this.TextColor = Color.Black;
this.BorderColor = Color.Empty;
this.Font = new Font("Tahoma", 10);
}
/// <summary>
/// Gets or sets where should the top left of the billboard be placed
/// </summary>
public Point Location {
get { return this.location; }
set { this.location = value; }
}
private Point location;
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (String.IsNullOrEmpty(this.Text))
return;
// Calculate the bounds of the text, and then move it to where it should be
Rectangle textRect = this.CalculateTextBounds(g, r, this.Text);
textRect.Location = this.Location;
// Make sure the billboard is within the bounds of the List, as far as is possible
if (textRect.Right > r.Width)
textRect.X = Math.Max(r.Left, r.Width - textRect.Width);
if (textRect.Bottom > r.Height)
textRect.Y = Math.Max(r.Top, r.Height - textRect.Height);
this.DrawBorderedText(g, textRect, this.Text, 255);
}
}
}
File diff suppressed because it is too large. Load diff
+400
View File
@@ -0,0 +1,400 @@
/*
* Styles - A style is a group of formatting attributes that can be applied to a row or a cell
*
* Author: Phillip Piper
* Date: 29/07/2009 23:09
*
* Change log:
* v2.4
* 2010-03-23 JPP - Added HeaderFormatStyle and support
* v2.3
* 2009-08-15 JPP - Added Decoration and Overlay properties to HotItemStyle
* 2009-07-29 JPP - Initial version
*
* To do:
* - These should be more generally available. It should be possible to do something like this:
* this.olv.GetItem(i).Style = new ItemStyle();
* this.olv.GetItem(i).GetSubItem(j).Style = new CellStyle();
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// The common interface supported by all style objects
/// </summary>
public interface IItemStyle
{
/// <summary>
/// Gets or set the font that will be used by this style
/// </summary>
Font Font { get; set; }
/// <summary>
/// Gets or set the font style
/// </summary>
FontStyle FontStyle { get; set; }
/// <summary>
/// Gets or sets the ForeColor
/// </summary>
Color ForeColor { get; set; }
/// <summary>
/// Gets or sets the BackColor
/// </summary>
Color BackColor { get; set; }
}
/// <summary>
/// Basic implementation of IItemStyle
/// </summary>
public class SimpleItemStyle : System.ComponentModel.Component, IItemStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
[DefaultValue(null)]
public Font Font
{
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the style of font that will be applied by this style
/// </summary>
[DefaultValue(FontStyle.Regular)]
public FontStyle FontStyle
{
get { return this.fontStyle; }
set { this.fontStyle = value; }
}
private FontStyle fontStyle;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof (Color), "")]
public Color ForeColor
{
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof (Color), "")]
public Color BackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
}
/// <summary>
/// Instances of this class specify how should "hot items" (non-selected
/// rows under the cursor) be renderered.
/// </summary>
public class HotItemStyle : SimpleItemStyle
{
/// <summary>
/// Gets or sets the overlay that should be drawn as part of the hot item
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IOverlay Overlay {
get { return this.overlay; }
set { this.overlay = value; }
}
private IOverlay overlay;
/// <summary>
/// Gets or sets the decoration that should be drawn as part of the hot item
/// </summary>
/// <remarks>A decoration is different from an overlay in that an decoration
/// scrolls with the listview contents, whilst an overlay does not.</remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IDecoration Decoration {
get { return this.decoration; }
set { this.decoration = value; }
}
private IDecoration decoration;
}
/// <summary>
/// This class defines how a cell should be formatted
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CellStyle : IItemStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the style of font that will be applied by this style
/// </summary>
[DefaultValue(FontStyle.Regular)]
public FontStyle FontStyle {
get { return this.fontStyle; }
set { this.fontStyle = value; }
}
private FontStyle fontStyle;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color ForeColor {
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
}
/// <summary>
/// Instances of this class describe how hyperlinks will appear
/// </summary>
public class HyperlinkStyle : System.ComponentModel.Component
{
/// <summary>
/// Create a HyperlinkStyle
/// </summary>
public HyperlinkStyle() {
this.Normal = new CellStyle();
this.Normal.ForeColor = Color.Blue;
this.Over = new CellStyle();
this.Over.FontStyle = FontStyle.Underline;
this.Visited = new CellStyle();
this.Visited.ForeColor = Color.Purple;
this.OverCursor = Cursors.Hand;
}
/// <summary>
/// What sort of formatting should be applied to hyperlinks in their normal state?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn")]
public CellStyle Normal {
get { return this.normalStyle; }
set { this.normalStyle = value; }
}
private CellStyle normalStyle;
/// <summary>
/// What sort of formatting should be applied to hyperlinks when the mouse is over them?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn when the mouse is over them?")]
public CellStyle Over {
get { return this.overStyle; }
set { this.overStyle = value; }
}
private CellStyle overStyle;
/// <summary>
/// What sort of formatting should be applied to hyperlinks after they have been clicked?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn after they have been clicked")]
public CellStyle Visited {
get { return this.visitedStyle; }
set { this.visitedStyle = value; }
}
private CellStyle visitedStyle;
/// <summary>
/// Gets or sets the cursor that should be shown when the mouse is over a hyperlink.
/// </summary>
[Category("Appearance"),
Description("What cursor should be shown when the mouse is over a link?")]
public Cursor OverCursor {
get { return this.overCursor; }
set { this.overCursor = value; }
}
private Cursor overCursor;
}
/// <summary>
/// Instances of this class control one the styling of one particular state
/// (normal, hot, pressed) of a header control
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class HeaderStateStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
[DefaultValue(null)]
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color ForeColor {
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
/// <summary>
/// Gets or sets the color in which a frame will be drawn around the header for this column
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color FrameColor {
get { return this.frameColor; }
set { this.frameColor = value; }
}
private Color frameColor;
/// <summary>
/// Gets or sets the width of the frame that will be drawn around the header for this column
/// </summary>
[DefaultValue(0.0f)]
public float FrameWidth {
get { return this.frameWidth; }
set { this.frameWidth = value; }
}
private float frameWidth;
}
/// <summary>
/// This class defines how a header should be formatted in its various states.
/// </summary>
public class HeaderFormatStyle : System.ComponentModel.Component
{
/// <summary>
/// Create a new HeaderFormatStyle
/// </summary>
public HeaderFormatStyle() {
this.Hot = new HeaderStateStyle();
this.Normal = new HeaderStateStyle();
this.Pressed = new HeaderStateStyle();
}
/// <summary>
/// What sort of formatting should be applied to a column header when the mouse is over it?
/// </summary>
[Category("Appearance"),
Description("How should the header be drawn when the mouse is over it?")]
public HeaderStateStyle Hot {
get { return this.hotStyle; }
set { this.hotStyle = value; }
}
private HeaderStateStyle hotStyle;
/// <summary>
/// What sort of formatting should be applied to a column header in its normal state?
/// </summary>
[Category("Appearance"),
Description("How should a column header normally be drawn")]
public HeaderStateStyle Normal {
get { return this.normalStyle; }
set { this.normalStyle = value; }
}
private HeaderStateStyle normalStyle;
/// <summary>
/// What sort of formatting should be applied to a column header when pressed?
/// </summary>
[Category("Appearance"),
Description("How should a column header be drawn when it is pressed")]
public HeaderStateStyle Pressed {
get { return this.pressedStyle; }
set { this.pressedStyle = value; }
}
private HeaderStateStyle pressedStyle;
/// <summary>
/// Set the font for all three states
/// </summary>
/// <param name="font"></param>
public void SetFont(Font font) {
this.Normal.Font = font;
this.Hot.Font = font;
this.Pressed.Font = font;
}
/// <summary>
/// Set the fore color for all three states
/// </summary>
/// <param name="color"></param>
public void SetForeColor(Color color) {
this.Normal.ForeColor = color;
this.Hot.ForeColor = color;
this.Pressed.ForeColor = color;
}
/// <summary>
/// Set the back color for all three states
/// </summary>
/// <param name="color"></param>
public void SetBackColor(Color color) {
this.Normal.BackColor = color;
this.Hot.BackColor = color;
this.Pressed.BackColor = color;
}
}
}
+248
View File
@@ -0,0 +1,248 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
namespace BrightIdeasSoftware {
public partial class TreeListView {
/// <summary>
/// This class handles drawing the tree structure of the primary column.
/// </summary>
public class TreeRenderer : HighlightTextRenderer {
/// <summary>
/// Create a TreeRenderer
/// </summary>
public TreeRenderer() {
this.LinePen = new Pen(Color.Blue, 1.0f);
this.LinePen.DashStyle = DashStyle.Dot;
}
/// <summary>
/// Return the branch that the renderer is currently drawing.
/// </summary>
private Branch Branch {
get {
return this.TreeListView.TreeModel.GetBranch(this.RowObject);
}
}
/// <summary>
/// Return the pen that will be used to draw the lines between branches
/// </summary>
public Pen LinePen {
get { return linePen; }
set { linePen = value; }
}
private Pen linePen;
/// <summary>
/// Return the TreeListView for which the renderer is being used.
/// </summary>
public TreeListView TreeListView {
get {
return (TreeListView)this.ListView;
}
}
/// <summary>
/// Should the renderer draw lines connecting siblings?
/// </summary>
public bool IsShowLines {
get { return isShowLines; }
set { isShowLines = value; }
}
private bool isShowLines = true;
/// <summary>
/// How many pixels will be reserved for each level of indentation?
/// </summary>
public static int PIXELS_PER_LEVEL = 16 + 1;
/// <summary>
/// The real work of drawing the tree is done in this method
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Render(System.Drawing.Graphics g, System.Drawing.Rectangle r) {
this.DrawBackground(g, r);
Branch br = this.Branch;
Rectangle paddedRectangle = this.ApplyCellPadding(r);
Rectangle expandGlyphRectangle = paddedRectangle;
expandGlyphRectangle.Offset((br.Level - 1) * PIXELS_PER_LEVEL, 0);
expandGlyphRectangle.Width = PIXELS_PER_LEVEL;
expandGlyphRectangle.Height = PIXELS_PER_LEVEL;
expandGlyphRectangle.Y = this.AlignVertically(paddedRectangle, expandGlyphRectangle);
int expandGlyphRectangleMidVertical = expandGlyphRectangle.Y + (expandGlyphRectangle.Height/2);
if (this.IsShowLines)
this.DrawLines(g, r, this.LinePen, br, expandGlyphRectangleMidVertical);
if (br.CanExpand)
this.DrawExpansionGlyph(g, expandGlyphRectangle, br.IsExpanded);
int indent = br.Level * PIXELS_PER_LEVEL;
paddedRectangle.Offset(indent, 0);
paddedRectangle.Width -= indent;
this.DrawImageAndText(g, paddedRectangle);
}
/// <summary>
/// Draw the expansion indicator
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyph(Graphics g, Rectangle r, bool isExpanded) {
if (this.UseStyles) {
this.DrawExpansionGlyphStyled(g, r, isExpanded);
} else {
this.DrawExpansionGlyphManual(g, r, isExpanded);
}
}
/// <summary>
/// Gets whether or not we should render using styles
/// </summary>
protected virtual bool UseStyles {
get {
return !this.IsPrinting && Application.RenderWithVisualStyles;
}
}
/// <summary>
/// Draw the expansion indicator using styles
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyphStyled(Graphics g, Rectangle r, bool isExpanded) {
VisualStyleElement element = VisualStyleElement.TreeView.Glyph.Closed;
if (isExpanded)
element = VisualStyleElement.TreeView.Glyph.Opened;
VisualStyleRenderer renderer = new VisualStyleRenderer(element);
renderer.DrawBackground(g, r);
}
/// <summary>
/// Draw the expansion indicator without using styles
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyphManual(Graphics g, Rectangle r, bool isExpanded) {
int h = 8;
int w = 8;
int x = r.X + 4;
int y = r.Y + (r.Height / 2) - 4;
g.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
g.FillRectangle(Brushes.White, x + 1, y + 1, w - 1, h - 1);
g.DrawLine(Pens.Black, x + 2, y + 4, x + w - 2, y + 4);
if (!isExpanded)
g.DrawLine(Pens.Black, x + 4, y + 2, x + 4, y + h - 2);
}
/// <summary>
/// Draw the lines of the tree
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="p"></param>
/// <param name="br"></param>
/// <param name="glyphMidVertical"> </param>
protected virtual void DrawLines(Graphics g, Rectangle r, Pen p, Branch br, int glyphMidVertical) {
Rectangle r2 = r;
r2.Width = PIXELS_PER_LEVEL;
// Vertical lines have to start on even points, otherwise the dotted line looks wrong.
// This is only needed if pen is dotted.
int top = r2.Top;
//if (p.DashStyle == DashStyle.Dot && (top & 1) == 0)
// top += 1;
// Draw lines for ancestors
int midX;
IList<Branch> ancestors = br.Ancestors;
foreach (Branch ancestor in ancestors) {
if (!ancestor.IsLastChild && !ancestor.IsOnlyBranch) {
midX = r2.Left + r2.Width / 2;
g.DrawLine(p, midX, top, midX, r2.Bottom);
}
r2.Offset(PIXELS_PER_LEVEL, 0);
}
// Draw lines for this branch
midX = r2.Left + r2.Width / 2;
// Horizontal line first
g.DrawLine(p, midX, glyphMidVertical, r2.Right, glyphMidVertical);
// Vertical line second
if (br.IsFirstBranch) {
if (!br.IsLastChild && !br.IsOnlyBranch)
g.DrawLine(p, midX, glyphMidVertical, midX, r2.Bottom);
} else {
if (br.IsLastChild)
g.DrawLine(p, midX, top, midX, glyphMidVertical);
else
g.DrawLine(p, midX, top, midX, r2.Bottom);
}
}
/// <summary>
/// Do the hit test
/// </summary>
/// <param name="g"></param>
/// <param name="hti"></param>
/// <param name="x"></param>
/// <param name="y"></param>
protected override void HandleHitTest(Graphics g, OlvListViewHitTestInfo hti, int x, int y) {
Branch br = this.Branch;
Rectangle r = this.Bounds;
if (br.CanExpand) {
r.Offset((br.Level - 1) * PIXELS_PER_LEVEL, 0);
r.Width = PIXELS_PER_LEVEL;
if (r.Contains(x, y)) {
hti.HitTestLocation = HitTestLocation.ExpandButton;
return;
}
}
r = this.Bounds;
int indent = br.Level * PIXELS_PER_LEVEL;
r.X += indent;
r.Width -= indent;
// Ignore events in the indent zone
if (x < r.Left) {
hti.HitTestLocation = HitTestLocation.Nothing;
} else {
this.StandardHitTest(g, hti, r, x, y);
}
}
/// <summary>
/// Calculate the edit rect
/// </summary>
/// <param name="g"></param>
/// <param name="cellBounds"></param>
/// <param name="item"></param>
/// <param name="subItemIndex"></param>
/// <param name="preferredSize"> </param>
/// <returns></returns>
protected override Rectangle HandleGetEditRectangle(Graphics g, Rectangle cellBounds, OLVListItem item, int subItemIndex, Size preferredSize) {
return this.StandardGetEditRectangle(g, cellBounds, preferredSize);
}
}
}
}