550 lines
14 KiB
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Globalization;
using DevComponents.DotNetBar.Charts.Style;
namespace DevComponents.DotNetBar.Charts
{
[TypeConverter(typeof(SymbolDefConvertor))]
[Editor(typeof(SymbolDefEditor), typeof(UITypeEditor))]
public class SymbolDef : INotifyPropertyChanged, IDisposable, IProcessSerialElement
{
#region Private variables
private string _Symbol = "";
private string _SymbolRealized;
private float _SymbolSize = -1f;
private Color _SymbolColor = Color.Empty;
private eSymbolSet _SymbolSet = eSymbolSet.Awesome;
private Font _SymbolFont;
private Size _RealSymbolSize;
#endregion
#region Public properties
#region Symbol
/// <summary>
/// Indicates the displayed Symbol. Symbol setting takes precedence over Image setting.
/// </summary>
[DefaultValue(""), Category("Appearance"), Description("displayed Symbol. Symbol setting takes precedence over Image setting.")]
[Editor("DevComponents.DotNetBar.Design.SymbolTypeEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor))]
public string Symbol
{
get { return (_Symbol); }
set
{
if (value == null)
value = "";
if (value != _Symbol)
{
_Symbol = value;
_SymbolRealized = null;
OnPropertyChangedEx("Symbol");
}
}
}
#endregion
#region SymbolColor
/// <summary>
/// Gets or sets the color of the Symbol.
/// </summary>
[Category("Appearance"), Description("Indicates color of the Symbol.")]
public Color SymbolColor
{
get { return (_SymbolColor); }
set
{
if (_SymbolColor != value)
{
_SymbolColor = value;
OnPropertyChangedEx("SymbolColor", VisualChangeType.Render);
}
}
}
/// <summary>
/// Gets whether property should be serialized.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
private bool ShouldSerializeSymbolColor()
{
return (_SymbolColor.IsEmpty == false);
}
/// <summary>
/// Resets property to its default value.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
private void ResetSymbolColor()
{
SymbolColor = Color.Empty;
}
#endregion
#region SymbolRealized
/// <summary>
/// Gets the realized symbol string.
/// </summary>
[Browsable(false)]
public string SymbolRealized
{
get
{
if (_SymbolRealized == null)
{
if (_Symbol != null)
_SymbolRealized = Symbols.GetSymbol(_Symbol);
}
return (_SymbolRealized);
}
}
#endregion
#region SymbolSet
/// <summary>
/// Gets or sets the symbol set used to represent the Symbol.
/// </summary>
[DefaultValue(eSymbolSet.Awesome)]
public eSymbolSet SymbolSet
{
get { return (_SymbolSet); }
set
{
if (_SymbolSet != value)
{
_SymbolSet = value;
_SymbolRealized = null;
_SymbolFont = null;
_RealSymbolSize = Size.Empty;
OnPropertyChangedEx("SymbolSet");
}
}
}
#endregion
#region SymbolSize
/// <summary>
/// Indicates the size of the symbol in points.
/// </summary>
[DefaultValue(-1f), Category("Appearance")]
[Description("Indicates the size of the symbol in points.")]
public float SymbolSize
{
get { return (_SymbolSize); }
set
{
if (value != _SymbolSize)
{
_SymbolSize = value;
_SymbolFont = null;
_RealSymbolSize = Size.Empty;
OnPropertyChangedEx("SymbolSize");
}
}
}
#endregion
#region IsEmpty
/// <summary>
/// Gets whether SymbolDef is Empty.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsEmpty
{
get
{
return ((string.IsNullOrEmpty(_Symbol) == true) &&
(_SymbolColor.IsEmpty == true) &&
(_SymbolSize == -1f));
}
}
#endregion
#endregion
#region Internal properties
#region SymbolFont
internal Font SymbolFont
{
get
{
if (_SymbolFont == null)
_SymbolFont = Symbols.GetFont(SymbolSize, SymbolSet);
return (_SymbolFont);
}
set
{
_SymbolFont = value;
}
}
#endregion
#region IsValidSymbol
internal bool IsValidSymbol
{
get { return (string.IsNullOrEmpty(SymbolRealized) == false); }
}
#endregion
#endregion
#region GetSymbolSize
internal Size GetSymbolSize(Graphics g)
{
if (_RealSymbolSize == Size.Empty)
{
Font font = SymbolFont;
Size size = g.MeasureString(SymbolRealized, font).ToSize();
size.Width++;
size.Height++;
_RealSymbolSize = size;
}
return (_RealSymbolSize);
}
#endregion
#region Copy
/// <summary>
/// Creates an exact copy of the SymbolDef.
/// </summary>
/// <returns>Copy of the SymbolDef.</returns>
public SymbolDef Copy()
{
SymbolDef style = new SymbolDef();
CopyTo(style);
return (style);
}
#endregion
#region CopyTo
/// <summary>
/// Returns the copy of the style.
/// </summary>
/// <returns>Copy of the style.</returns>
public void CopyTo(SymbolDef style)
{
style.Symbol = _Symbol;
style.SymbolColor = _SymbolColor;
style.SymbolSet = _SymbolSet;
style.SymbolSize = _SymbolSize;
}
#endregion
#region GetSerialData
internal SerialElementCollection GetSerialData(string serialName)
{
if (IsEmpty == false)
{
SerialElementCollection sec = new SerialElementCollection();
if (serialName != null)
{
if (serialName.Equals("") == true)
serialName = "SymbolDef";
sec.AddStartElement(serialName);
}
if (IsValidSymbol == true)
sec.AddValue("Symbol", Symbol);
sec.AddValue("SymbolColor", SymbolColor, Color.Empty);
sec.AddValue("SymbolSet", SymbolSet, eSymbolSet.Awesome);
sec.AddValue("SymbolSize", SymbolSize, -1f);
if (serialName != null)
sec.AddEndElement(serialName);
return (sec);
}
return (null);
}
#endregion
#region PutSerialData
#region ProcessValue
void IProcessSerialElement.ProcessValue(SerialElement se)
{
switch (se.Name)
{
case "Symbol":
Symbol = se.GetValueString();
break;
case "SymbolColor":
SymbolColor = se.GetValueColor();
break;
case "SymbolSet":
SymbolSet = (eSymbolSet)se.GetValueEnum(typeof(eSymbolSet));
break;
case "SymbolSize":
SymbolSize = float.Parse(se.StringValue);
break;
default:
throw new Exception("Unknown Serial Value (" + se.Name + ")");
}
}
#endregion
#region ProcessCollection
void IProcessSerialElement.ProcessCollection(SerialElement se)
{
switch (se.Name)
{
default:
throw new Exception("Unknown Serial Collection (" + se.Name + ")");
}
}
#endregion
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// Occurs when property value has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnPropertyChanged(VisualPropertyChangedEventArgs e)
{
PropertyChangedEventHandler eh = PropertyChanged;
if (eh != null)
eh(this, e);
}
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="s">Event arguments</param>
protected virtual void OnPropertyChangedEx(string s)
{
PropertyChangedEventHandler eh = PropertyChanged;
if (eh != null)
{
VisualPropertyChangedEventArgs e =
new VisualPropertyChangedEventArgs(s);
eh(this, e);
}
}
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="s">Event arguments</param>
protected virtual void OnPropertyChangedEx(string s, VisualChangeType changeType)
{
PropertyChangedEventHandler eh = PropertyChanged;
if (eh != null)
{
VisualPropertyChangedEventArgs e =
new VisualPropertyChangedEventArgs(s, changeType);
eh(this, e);
}
}
#endregion
#region UpdateChangeHandler
private void UpdateChangeHandler(
INotifyPropertyChanged oldValue, INotifyPropertyChanged newValue)
{
if (oldValue != null)
oldValue.PropertyChanged -= StyleChanged;
if (newValue != null)
newValue.PropertyChanged += StyleChanged;
}
#endregion
#region StyleChanged
/// <summary>
/// StyleChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void StyleChanged(
object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged((VisualPropertyChangedEventArgs)e);
}
#endregion
#region IDisposable
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
SymbolFont = null;
}
#endregion
}
#region SymbolDefConvertor
///<summary>
/// SymbolDefConvertor
///</summary>
public class SymbolDefConvertor : ExpandableObjectConverter
{
/// <summary>
/// ConvertTo
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(
ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
SymbolDef sd = value as SymbolDef;
if (sd != null)
return (sd.SymbolSet.ToString());
}
return (base.ConvertTo(context, culture, value, destinationType));
}
}
#endregion
#region SymbolDefEditor
///<summary>
/// SymbolDefEditor
///</summary>
public class SymbolDefEditor : UITypeEditor
{
#region GetPaintValueSupported
/// <summary>
/// GetPaintValueSupported
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return (true);
}
#endregion
#region PaintValue
/// <summary>
/// PaintValue
/// </summary>
/// <param name="e"></param>
public override void PaintValue(PaintValueEventArgs e)
{
SymbolDef sd = e.Value as SymbolDef;
if (sd != null)
{
Font font = Symbols.GetFont(10, sd.SymbolSet);
Color color = sd.SymbolColor;
if (sd.SymbolColor.IsEmpty || sd.SymbolColor == Color.White)
color = Color.Brown;
using (Brush br = new SolidBrush(color))
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(sd.SymbolRealized, font, br, e.Bounds, sf);
}
}
}
}
#endregion
}
#endregion
}