using System;
using System.ComponentModel;
using System.Drawing;
using ToolBox.Formatting;
namespace Netron.Diagramming.Core {
// ----------------------------------------------------------------------
///
/// Specifies formatting for text to be drawn (such as color, alignment,
/// font size, etc.). The event 'TextStyleChanged' is raised when a
/// property is changed.
///
// ----------------------------------------------------------------------
public partial class TextStyle : ITextStyle {
// ------------------------------------------------------------------
///
/// Event raised when this TextStyle is changed.
///
// ------------------------------------------------------------------
[field: NonSerialized()]
public event TextStyleChangedEventHandler TextStyleChanged;
// ------------------------------------------------------------------
///
/// Implementation of IVersion - the current version of
/// TextStyle.
///
// ------------------------------------------------------------------
protected const double textStyleVersion = 1.0;
// ------------------------------------------------------------------
///
/// Specifies the number of decimal places to use when a TextFormat
/// other than 'String' is specified.
///
// ------------------------------------------------------------------
protected int mDecimalPlaces = 2;
// ------------------------------------------------------------------
///
/// The formatting to apply to the text.
///
// ------------------------------------------------------------------
protected TextFormat mTextFormat = TextFormat.String;
// ------------------------------------------------------------------
///
/// The font.
///
// ------------------------------------------------------------------
protected Font mFont = new Font("Arial", 10);
// ------------------------------------------------------------------
///
/// The fontstyle to use (regular, bold, italics, etc.).
///
// ------------------------------------------------------------------
protected FontStyle mFontStyle = FontStyle.Regular;
// ------------------------------------------------------------------
///
/// The color for the text;
///
// ------------------------------------------------------------------
protected Color mFontColor = Color.Black;
// ------------------------------------------------------------------
///
/// The horizontal alignment of the text.
///
// ------------------------------------------------------------------
protected StringAlignment mHorizontalAlignment =
StringAlignment.Center;
// ------------------------------------------------------------------
///
/// The vertical alignment of the text.
///
// ------------------------------------------------------------------
protected StringAlignment mVerticalAlignment =
StringAlignment.Center;
// ------------------------------------------------------------------
///
/// Specifies if the font is bold.
///
// ------------------------------------------------------------------
protected bool mIsBold = false;
// ------------------------------------------------------------------
///
/// Specifies if the font is italic.
///
// ------------------------------------------------------------------
protected bool mIsItalic = false;
// ------------------------------------------------------------------
///
/// Specifies if the font is underlined.
///
// ------------------------------------------------------------------
protected bool mIsUnderline = false;
#region ITextStyle Members
// ------------------------------------------------------------------
///
/// Gets the current version.
///
// ------------------------------------------------------------------
public virtual double Version {
get {
return textStyleVersion;
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the number of decimal places to use when a TextFormat
/// other than 'String' is specified.
///
// ------------------------------------------------------------------
public virtual int DecimalPlaces {
get {
return mDecimalPlaces;
}
set {
mDecimalPlaces = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the formatting to apply to the text.
///
// ------------------------------------------------------------------
public virtual TextFormat TextFormat {
get {
return mTextFormat;
}
set {
mTextFormat = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the text's font.
///
// ------------------------------------------------------------------
public virtual Font Font {
get {
return this.mFont;
}
set {
this.mFont = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the text's font size.
///
// ------------------------------------------------------------------
public virtual float FontSize {
get {
return this.mFont.Size;
}
set {
this.mFont = new Font(
mFont.FontFamily,
value,
mFont.Style);
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the text's color.
///
// ------------------------------------------------------------------
public virtual Color FontColor {
get {
return mFontColor;
}
set {
mFontColor = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets if the text is underlined.
///
// ------------------------------------------------------------------
public virtual bool IsUnderlined {
get {
return mFont.Underline;
}
set {
mIsUnderline = value;
SetFontStyle();
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets if the text is bold.
///
// ------------------------------------------------------------------
public virtual bool IsBold {
get {
return mFont.Bold;
}
set {
mIsBold = value;
SetFontStyle();
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets if the text is italic.
///
// ------------------------------------------------------------------
public virtual bool IsItalic {
get {
return mFont.Italic;
}
set {
mIsItalic = value;
SetFontStyle();
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the horizontal alignment of the text.
///
// ------------------------------------------------------------------
[Browsable(true),
Description("The horizontal alignment of the text."),
Category("Layout")]
public virtual StringAlignment HorizontalAlignment {
get {
return mHorizontalAlignment;
}
set {
mHorizontalAlignment = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the vertical alignment of the text.
///
// ------------------------------------------------------------------
[Browsable(true),
Description("The vertical alignment of the text."),
Category("Layout")]
public virtual StringAlignment VerticalAlignment {
get {
return mVerticalAlignment;
}
set {
mVerticalAlignment = value;
RaiseTextStyleChanged();
}
}
// ------------------------------------------------------------------
///
/// Gets the format of the string.
///
// ------------------------------------------------------------------
public virtual StringFormat StringFormat {
get {
StringFormat format = new StringFormat();
format.Alignment = this.VerticalAlignment;
format.LineAlignment = this.HorizontalAlignment;
return format;
}
}
#endregion
#region Constructors
// ------------------------------------------------------------------
///
/// Default constructor.
///
// ------------------------------------------------------------------
public TextStyle() {
}
// ------------------------------------------------------------------
///
/// Constructor that receives the color of the font.
///
/// Color
// ------------------------------------------------------------------
public TextStyle(Color color) {
mFontColor = color;
}
// ------------------------------------------------------------------
///
/// Constructor that receives the font color, font, horizontal
/// alignment, and vertical alignment.
///
/// Color
/// Font
/// StringAlignment
/// StringAlignment
// ------------------------------------------------------------------
public TextStyle(
Color color,
Font font,
StringAlignment horizontalAlignment,
StringAlignment verticalAlignment) {
mFontColor = color;
mFont = font;
mHorizontalAlignment = horizontalAlignment;
mVerticalAlignment = verticalAlignment;
}
#endregion
// ------------------------------------------------------------------
///
/// Creates a new font style using the current IsBold, IsItalic,
/// and IsUnderline properties.
///
// ------------------------------------------------------------------
protected virtual void SetFontStyle() {
FontStyle style = FontStyle.Regular;
if (mIsBold) {
style = style | FontStyle.Bold;
}
if (mIsItalic) {
style = style | FontStyle.Italic;
}
if (mIsUnderline) {
style = style | FontStyle.Underline;
}
this.mFont = new Font(mFont, style);
}
// ------------------------------------------------------------------
///
/// Gets the brush that's used to draw our text to a GDI+ graphics
/// surface.
///
/// Brush
// ------------------------------------------------------------------
public virtual Brush GetBrush() {
return new SolidBrush(mFontColor);
}
// ------------------------------------------------------------------
///
/// Applies the formatting specified by TextFormatting to the text
/// specified.
///
/// string: The un-formatted text.
/// string: The formatted text.
// ------------------------------------------------------------------
public virtual string GetFormattedText(string text) {
return TextFormatter.Format(
mTextFormat,
text,
mDecimalPlaces);
}
// ------------------------------------------------------------------
///
/// Raises the TextStyleChanged event.
///
// ------------------------------------------------------------------
protected virtual void RaiseTextStyleChanged() {
if (this.TextStyleChanged != null) {
// Raise the event
this.TextStyleChanged(
this,
new TextStyleChangedEventArgs(this));
}
}
}
}