using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace Netron.Diagramming.Core {
///
/// Icon shape material without service.
///
///
public partial class IconLabelMaterial : ShapeMaterialBase {
///
/// The distance between the icon and the text.
///
public const int constTextShift = 2;
#region Fields
// ------------------------------------------------------------------
///
/// Implementation of IVersion - the current version of
/// IconLabelMaterial.
///
// ------------------------------------------------------------------
protected double iconLabelMaterialVersion = 1.0;
// ------------------------------------------------------------------
///
/// the Text field
///
// ------------------------------------------------------------------
private Bitmap mIcon;
// ------------------------------------------------------------------
///
/// the Text field
///
// ------------------------------------------------------------------
private string mText = string.Empty;
private Rectangle textRectangle = Rectangle.Empty;
#endregion
#region Properties
// ------------------------------------------------------------------
///
/// Gets the current version.
///
// ------------------------------------------------------------------
public override double Version {
get {
return iconLabelMaterialVersion;
}
}
///
/// Gets the bounds of the text.
///
public Rectangle TextArea {
get {
return textRectangle;
}
}
///
/// Gets or sets the Text
///
public string Text {
get {
return mText;
}
set {
mText = value;
}
}
///
/// Gets or sets the Text
///
public Bitmap Icon {
get {
return mIcon;
}
set {
mIcon = value;
}
}
#endregion
#region Constructor
public IconLabelMaterial(string text)
: base() {
mText = text;
}
///
/// Initializes a new instance of the class.
///
/// The resource location.
/// The text.
public IconLabelMaterial(string text, string resourceLocation)
: this(text) {
mIcon = GetBitmap(resourceLocation);
}
///
/// Opens the icon (image) from the location specified and sets it
/// as the image to use.
///
/// string: The resource location.
///
public void SetIcon(string resourceLocation) {
mIcon = GetBitmap(resourceLocation);
}
///
/// Gets the bitmap at the location specified.
///
/// The resource location.
///
protected Bitmap GetBitmap(string resourceLocation) {
if (resourceLocation.Length == 0)
return null;
try {
//first try if it's defined in this assembly somewhere
return new Bitmap(this.GetType(), resourceLocation);
}
catch {
if (File.Exists(resourceLocation)) {
return new Bitmap(resourceLocation);
} else
return null;
}
}
///
/// Initializes a new instance of the class.
///
public IconLabelMaterial()
: base() {
}
#endregion
#region Methods
// ------------------------------------------------------------------
///
/// Calculates the min size needed to fit this material in.
///
/// Graphics
/// Size
// ------------------------------------------------------------------
public override Size CalculateMinSize(Graphics g) {
Size minSizeNeeded = new Size(0, 0);
if (mText != String.Empty) {
minSizeNeeded = Size.Round(g.MeasureString(
this.myTextStyle.GetFormattedText(this.mText),
this.myTextStyle.Font));
}
minSizeNeeded.Width += constTextShift;
if (mIcon != null) {
minSizeNeeded.Width += mIcon.Width;
if (mIcon.Height > minSizeNeeded.Height) {
minSizeNeeded.Height = mIcon.Height;
}
}
return minSizeNeeded;
}
public override void Transform(Rectangle rectangle) {
textRectangle = new Rectangle(
rectangle.X + (mIcon == null ? 0 : mIcon.Width) + constTextShift,
rectangle.Y,
rectangle.Width - (mIcon == null ? 0 : mIcon.Width) - constTextShift,
rectangle.Height);
base.Transform(rectangle);
}
///
/// Paints the entity using the given graphics object
///
///
public override void Paint(Graphics g) {
if (!Visible)
return;
GraphicsContainer cto = g.BeginContainer();
g.SetClip(Shape.Rectangle);
if (mIcon != null) {
g.DrawImage(mIcon, new Rectangle(Rectangle.Location, mIcon.Size));
}
StringFormat stringFormat = myTextStyle.StringFormat;
stringFormat.Trimming = StringTrimming.EllipsisWord;
stringFormat.FormatFlags = StringFormatFlags.LineLimit;
g.DrawString(
myTextStyle.GetFormattedText(mText),
this.myTextStyle.Font,
this.myTextStyle.GetBrush(),
textRectangle,
stringFormat);
g.EndContainer(cto);
}
#endregion
}
}