Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Styling/TextStyle.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 13.1 KB
Line 
1using System;
2using System.ComponentModel;
3using System.Drawing;
4using ToolBox.Formatting;
5
6namespace Netron.Diagramming.Core {
7  // ----------------------------------------------------------------------
8  /// <summary>
9  /// Specifies formatting for text to be drawn (such as color, alignment,
10  /// font size, etc.).  The event 'TextStyleChanged' is raised when a
11  /// property is changed.
12  /// </summary>
13  // ----------------------------------------------------------------------
14  public partial class TextStyle : ITextStyle {
15    // ------------------------------------------------------------------
16    /// <summary>
17    /// Event raised when this TextStyle is changed.
18    /// </summary>
19    // ------------------------------------------------------------------
20    [field: NonSerialized()]
21    public event TextStyleChangedEventHandler TextStyleChanged;
22
23    // ------------------------------------------------------------------
24    /// <summary>
25    /// Implementation of IVersion - the current version of
26    /// TextStyle.
27    /// </summary>
28    // ------------------------------------------------------------------
29    protected const double textStyleVersion = 1.0;
30
31    // ------------------------------------------------------------------
32    /// <summary>
33    /// Specifies the number of decimal places to use when a TextFormat
34    /// other than 'String' is specified.
35    /// </summary>
36    // ------------------------------------------------------------------
37    protected int mDecimalPlaces = 2;
38
39    // ------------------------------------------------------------------
40    /// <summary>
41    /// The formatting to apply to the text.
42    /// </summary>
43    // ------------------------------------------------------------------
44    protected TextFormat mTextFormat = TextFormat.String;
45
46    // ------------------------------------------------------------------
47    /// <summary>
48    /// The font.
49    /// </summary>
50    // ------------------------------------------------------------------
51    protected Font mFont = new Font("Arial", 10);
52
53    // ------------------------------------------------------------------
54    /// <summary>
55    /// The fontstyle to use (regular, bold, italics, etc.).
56    /// </summary>
57    // ------------------------------------------------------------------
58    protected FontStyle mFontStyle = FontStyle.Regular;
59
60    // ------------------------------------------------------------------
61    /// <summary>
62    /// The color for the text;
63    /// </summary>
64    // ------------------------------------------------------------------
65    protected Color mFontColor = Color.Black;
66
67    // ------------------------------------------------------------------
68    /// <summary>
69    /// The horizontal alignment of the text.
70    /// </summary>
71    // ------------------------------------------------------------------
72    protected StringAlignment mHorizontalAlignment =
73        StringAlignment.Center;
74
75    // ------------------------------------------------------------------
76    /// <summary>
77    /// The vertical alignment of the text.
78    /// </summary>
79    // ------------------------------------------------------------------
80    protected StringAlignment mVerticalAlignment =
81        StringAlignment.Center;
82
83    // ------------------------------------------------------------------
84    /// <summary>
85    /// Specifies if the font is bold.
86    /// </summary>
87    // ------------------------------------------------------------------
88    protected bool mIsBold = false;
89
90    // ------------------------------------------------------------------
91    /// <summary>
92    /// Specifies if the font is italic.
93    /// </summary>
94    // ------------------------------------------------------------------
95    protected bool mIsItalic = false;
96
97    // ------------------------------------------------------------------
98    /// <summary>
99    /// Specifies if the font is underlined.
100    /// </summary>
101    // ------------------------------------------------------------------
102    protected bool mIsUnderline = false;
103
104    #region ITextStyle Members
105
106    // ------------------------------------------------------------------
107    /// <summary>
108    /// Gets the current version.
109    /// </summary>
110    // ------------------------------------------------------------------
111    public virtual double Version {
112      get {
113        return textStyleVersion;
114      }
115    }
116
117    // ------------------------------------------------------------------
118    /// <summary>
119    /// Gets or sets the number of decimal places to use when a TextFormat
120    /// other than 'String' is specified.
121    /// </summary>
122    // ------------------------------------------------------------------
123    public virtual int DecimalPlaces {
124      get {
125        return mDecimalPlaces;
126      }
127      set {
128        mDecimalPlaces = value;
129        RaiseTextStyleChanged();
130      }
131    }
132
133    // ------------------------------------------------------------------
134    /// <summary>
135    /// Gets or sets the formatting to apply to the text.
136    /// </summary>
137    // ------------------------------------------------------------------
138    public virtual TextFormat TextFormat {
139      get {
140        return mTextFormat;
141      }
142      set {
143        mTextFormat = value;
144        RaiseTextStyleChanged();
145      }
146    }
147
148    // ------------------------------------------------------------------
149    /// <summary>
150    /// Gets or sets the text's font.
151    /// </summary>
152    // ------------------------------------------------------------------
153    public virtual Font Font {
154      get {
155        return this.mFont;
156      }
157      set {
158        this.mFont = value;
159        RaiseTextStyleChanged();
160      }
161    }
162
163    // ------------------------------------------------------------------
164    /// <summary>
165    /// Gets or sets the text's font size.
166    /// </summary>
167    // ------------------------------------------------------------------
168    public virtual float FontSize {
169      get {
170        return this.mFont.Size;
171      }
172      set {
173        this.mFont = new Font(
174            mFont.FontFamily,
175            value,
176            mFont.Style);
177        RaiseTextStyleChanged();
178      }
179    }
180
181    // ------------------------------------------------------------------
182    /// <summary>
183    /// Gets or sets the text's color.
184    /// </summary>
185    // ------------------------------------------------------------------
186    public virtual Color FontColor {
187      get {
188        return mFontColor;
189      }
190      set {
191        mFontColor = value;
192        RaiseTextStyleChanged();
193      }
194    }
195
196    // ------------------------------------------------------------------
197    /// <summary>
198    /// Gets or sets if the text is underlined.
199    /// </summary>
200    // ------------------------------------------------------------------
201    public virtual bool IsUnderlined {
202      get {
203        return mFont.Underline;
204      }
205      set {
206        mIsUnderline = value;
207        SetFontStyle();
208        RaiseTextStyleChanged();
209      }
210    }
211
212    // ------------------------------------------------------------------
213    /// <summary>
214    /// Gets or sets if the text is bold.
215    /// </summary>
216    // ------------------------------------------------------------------
217    public virtual bool IsBold {
218      get {
219        return mFont.Bold;
220      }
221      set {
222        mIsBold = value;
223        SetFontStyle();
224        RaiseTextStyleChanged();
225      }
226    }
227
228    // ------------------------------------------------------------------
229    /// <summary>
230    /// Gets or sets if the text is italic.
231    /// </summary>
232    // ------------------------------------------------------------------
233    public virtual bool IsItalic {
234      get {
235        return mFont.Italic;
236      }
237      set {
238        mIsItalic = value;
239        SetFontStyle();
240        RaiseTextStyleChanged();
241      }
242    }
243
244    // ------------------------------------------------------------------
245    /// <summary>
246    /// Gets or sets the horizontal alignment of the text.
247    /// </summary>
248    // ------------------------------------------------------------------
249    [Browsable(true),
250    Description("The horizontal alignment of the text."),
251    Category("Layout")]
252    public virtual StringAlignment HorizontalAlignment {
253      get {
254        return mHorizontalAlignment;
255      }
256      set {
257        mHorizontalAlignment = value;
258        RaiseTextStyleChanged();
259      }
260    }
261
262    // ------------------------------------------------------------------
263    /// <summary>
264    /// Gets or sets the vertical alignment of the text.
265    /// </summary>
266    // ------------------------------------------------------------------
267    [Browsable(true),
268    Description("The vertical alignment of the text."),
269    Category("Layout")]
270    public virtual StringAlignment VerticalAlignment {
271      get {
272        return mVerticalAlignment;
273      }
274      set {
275        mVerticalAlignment = value;
276        RaiseTextStyleChanged();
277      }
278    }
279
280    // ------------------------------------------------------------------
281    /// <summary>
282    /// Gets the format of the string.
283    /// </summary>
284    // ------------------------------------------------------------------
285    public virtual StringFormat StringFormat {
286      get {
287        StringFormat format = new StringFormat();
288        format.Alignment = this.VerticalAlignment;
289        format.LineAlignment = this.HorizontalAlignment;
290        return format;
291      }
292    }
293
294    #endregion
295
296    #region Constructors
297
298    // ------------------------------------------------------------------
299    /// <summary>
300    /// Default constructor.
301    /// </summary>
302    // ------------------------------------------------------------------
303    public TextStyle() {
304    }
305
306    // ------------------------------------------------------------------
307    /// <summary>
308    /// Constructor that receives the color of the font.
309    /// </summary>
310    /// <param name="color">Color</param>
311    // ------------------------------------------------------------------
312    public TextStyle(Color color) {
313      mFontColor = color;
314    }
315
316    // ------------------------------------------------------------------
317    /// <summary>
318    /// Constructor that receives the font color, font, horizontal
319    /// alignment, and vertical alignment.
320    /// </summary>
321    /// <param name="color">Color</param>
322    /// <param name="font">Font</param>
323    /// <param name="horizontalAlignment">StringAlignment</param>
324    /// <param name="verticalAlignment">StringAlignment</param>
325    // ------------------------------------------------------------------
326    public TextStyle(
327        Color color,
328        Font font,
329        StringAlignment horizontalAlignment,
330        StringAlignment verticalAlignment) {
331      mFontColor = color;
332      mFont = font;
333      mHorizontalAlignment = horizontalAlignment;
334      mVerticalAlignment = verticalAlignment;
335    }
336
337    #endregion
338
339    // ------------------------------------------------------------------
340    /// <summary>
341    /// Creates a new font style using the current IsBold, IsItalic,
342    /// and IsUnderline properties.
343    /// </summary>
344    // ------------------------------------------------------------------
345    protected virtual void SetFontStyle() {
346      FontStyle style = FontStyle.Regular;
347
348      if (mIsBold) {
349        style = style | FontStyle.Bold;
350      }
351
352      if (mIsItalic) {
353        style = style | FontStyle.Italic;
354      }
355
356      if (mIsUnderline) {
357        style = style | FontStyle.Underline;
358      }
359
360      this.mFont = new Font(mFont, style);
361    }
362
363    // ------------------------------------------------------------------
364    /// <summary>
365    /// Gets the brush that's used to draw our text to a GDI+ graphics
366    /// surface.
367    /// </summary>
368    /// <returns>Brush</returns>
369    // ------------------------------------------------------------------
370    public virtual Brush GetBrush() {
371      return new SolidBrush(mFontColor);
372    }
373
374    // ------------------------------------------------------------------
375    /// <summary>
376    /// Applies the formatting specified by TextFormatting to the text
377    /// specified.
378    /// </summary>
379    /// <param name="text">string: The un-formatted text.</param>
380    /// <returns>string: The formatted text.</returns>
381    // ------------------------------------------------------------------
382    public virtual string GetFormattedText(string text) {
383      return TextFormatter.Format(
384          mTextFormat,
385          text,
386          mDecimalPlaces);
387    }
388
389    // ------------------------------------------------------------------
390    /// <summary>
391    /// Raises the TextStyleChanged event.
392    /// </summary>
393    // ------------------------------------------------------------------
394    protected virtual void RaiseTextStyleChanged() {
395      if (this.TextStyleChanged != null) {
396        // Raise the event
397        this.TextStyleChanged(
398            this,
399            new TextStyleChangedEventArgs(this));
400      }
401    }
402  }
403}
Note: See TracBrowser for help on using the repository browser.