Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.8/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/SimpleShapeBase.cs @ 9480

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

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

File size: 10.7 KB
Line 
1using System.ComponentModel;
2using System.Drawing;
3using System.Windows.Forms;
4namespace Netron.Diagramming.Core {
5  // ----------------------------------------------------------------------
6  /// <summary>
7  /// Abstract base class for simple shapes.
8  /// <para>A simple shape does not contain sub-elements (aka shape
9  /// material, see <see cref="IShapeMaterial"/>) but this does
10  /// not mean you cannot paint sub-elements. In fact, this shape type
11  /// corresponds to the old base class in the previous Netron graph
12  /// library (i.e. before version 2.3).
13  /// <seealso cref="ComplexShapeBase"/>
14  /// </para>
15  /// </summary>
16  // ----------------------------------------------------------------------
17  public abstract partial class SimpleShapeBase :
18      ShapeBase,
19      ISimpleShape,
20      ITextProvider {
21    // ------------------------------------------------------------------
22    /// <summary>
23    /// The amount to inflate the text area (mTextRectangle) by.
24    /// </summary>
25    // ------------------------------------------------------------------
26    protected const int TextRectangleInflation = 5;
27
28    // ------------------------------------------------------------------
29    /// <summary>
30    /// Implementation of IVersion - the current version of
31    /// SimpleShapeBase.
32    /// </summary>
33    // ------------------------------------------------------------------
34    protected const double simpleShapeBaseVersion = 1.0;
35
36    #region Fields
37
38    // ------------------------------------------------------------------
39    /// <summary>
40    /// Specifies if this shape is auto-sized to fit the text.
41    /// </summary>
42    // ------------------------------------------------------------------
43    protected bool mAutoSize = false;
44
45    // ------------------------------------------------------------------
46    /// <summary>
47    /// Specifies if this shape allows in-place text editing.  If set to
48    /// true, then the TextEditor is displayed when the shape is
49    /// double-clicked.
50    /// </summary>
51    // ------------------------------------------------------------------
52    protected bool mAllowTextEditing = true;
53
54    // ------------------------------------------------------------------
55    /// <summary>
56    /// Specifies the number of clicks required to launch the in-place
57    /// TextEditor.
58    /// </summary>
59    // ------------------------------------------------------------------
60    protected int mEditTextClicks = 2;
61
62    // ------------------------------------------------------------------
63    /// <summary>
64    /// The text for this shape.
65    /// </summary>
66    // ------------------------------------------------------------------
67    protected string mText = string.Empty;
68
69    // ------------------------------------------------------------------
70    /// <summary>
71    /// The Rectangle inside which the text is drawn.
72    /// </summary>
73    // ------------------------------------------------------------------
74    protected Rectangle mTextArea;
75
76    // ------------------------------------------------------------------
77    /// <summary>
78    /// The style of the text to draw.
79    /// </summary>
80    // ------------------------------------------------------------------
81    protected ITextStyle mTextStyle = new TextStyle(
82        Color.Black,
83        new Font("Arial", 10),
84        StringAlignment.Center,
85        StringAlignment.Center);
86
87    #endregion
88
89    #region Properties
90
91    // ------------------------------------------------------------------
92    /// <summary>
93    /// Gets the current version.
94    /// </summary>
95    // ------------------------------------------------------------------
96    public override double Version {
97      get {
98        return simpleShapeBaseVersion;
99      }
100    }
101
102    // ------------------------------------------------------------------
103    /// <summary>
104    /// Gets or sets the style of the text to use.
105    /// </summary>
106    // ------------------------------------------------------------------
107    public virtual ITextStyle TextStyle {
108      get {
109        return mTextStyle;
110      }
111      set {
112        mTextStyle = value;
113        mTextStyle.TextStyleChanged +=
114            new TextStyleChangedEventHandler(HandleTextStyleChanged);
115        Invalidate();
116      }
117    }
118
119    // ------------------------------------------------------------------
120    /// <summary>
121    /// Returns if text editing is allowed.
122    /// </summary>
123    // ------------------------------------------------------------------
124    public virtual bool AllowTextEditing {
125      get {
126        return this.mAllowTextEditing;
127      }
128    }
129
130    // ------------------------------------------------------------------
131    /// <summary>
132    /// Returns the number of mouse clicks required to launch the in-place
133    /// text editor.
134    /// </summary>
135    // ------------------------------------------------------------------
136    public virtual int EditTextClicks {
137      get {
138        return mEditTextClicks;
139      }
140      set {
141        mEditTextClicks = value;
142      }
143    }
144
145    // ------------------------------------------------------------------
146    /// <summary>
147    /// Gets or sets a value indicating whether to autosize the label in
148    /// function of the string content.
149    /// </summary>
150    /// <value><c>true</c> if [auto size]; otherwise, <c>false</c>.
151    /// </value>
152    // ------------------------------------------------------------------
153    public virtual bool AutoSize {
154      get {
155        return mAutoSize;
156      }
157      set {
158        mAutoSize = value;
159        AutoReSize(mText);
160      }
161    }
162
163    // ------------------------------------------------------------------
164    /// <summary>
165    /// Gets or sets the text to display in the TextArea.
166    /// </summary>
167    // ------------------------------------------------------------------
168    [Browsable(true),
169    Description("The text shown on the shape"),
170    Category("Layout")]
171    public virtual string Text {
172      get {
173        return mText;
174      }
175      set {
176        if (value == null) {
177          throw new InconsistencyException(
178              "The text property cannot be 'null'");
179        }
180
181        mText = value;
182        if (mAutoSize) {
183          AutoReSize(value);
184        }
185        this.Invalidate();
186      }
187    }
188
189    // ------------------------------------------------------------------
190    /// <summary>
191    /// Gets or sets the text rectangle.
192    /// </summary>
193    /// <value>The text rectangle.</value>
194    // ------------------------------------------------------------------
195    public virtual Rectangle TextArea {
196      get {
197        return mTextArea;
198      }
199      set {
200        mTextArea = value;
201      }
202    }
203    #endregion
204
205    #region Constructor
206
207    // ------------------------------------------------------------------
208    ///<summary>
209    ///Default constructor.
210    ///</summary>
211    // ------------------------------------------------------------------
212    public SimpleShapeBase()
213      : base() {
214    }
215
216    // ------------------------------------------------------------------
217    /// <summary>
218    /// Initializes a new instance of the <see cref="T:SimpleShapeBase"/>
219    /// class.
220    /// </summary>
221    /// <param name="model">The <see cref="IModel"/></param>
222    // ------------------------------------------------------------------
223    public SimpleShapeBase(IModel model)
224      : base(model) {
225    }
226
227    #endregion
228
229    #region Methods
230
231    // ------------------------------------------------------------------
232    /// <summary>
233    /// Initializes this instance.
234    /// </summary>
235    // ------------------------------------------------------------------
236    protected override void Initialize() {
237      base.Initialize();
238      mTextArea = Rectangle;
239
240      mTextArea.Inflate(
241          -TextRectangleInflation,
242          -TextRectangleInflation);
243
244      this.mTextStyle = new TextStyle(
245          Color.Black,
246          new Font("Arial", 12),
247          StringAlignment.Center,
248          StringAlignment.Center);
249
250      this.mTextStyle.TextStyleChanged +=
251          new TextStyleChangedEventHandler(HandleTextStyleChanged);
252    }
253
254    // ------------------------------------------------------------------
255    /// <summary>
256    /// Called when the text style is changed.  This shape is invalidated
257    /// so the new text style is reflected.
258    /// </summary>
259    /// <param name="sender">object</param>
260    /// <param name="e">TextStyleChangedEventArgs</param>
261    // ------------------------------------------------------------------
262    protected virtual void HandleTextStyleChanged(
263        object sender,
264        TextStyleChangedEventArgs e) {
265      Invalidate();
266    }
267
268    // ------------------------------------------------------------------
269    /// <summary>
270    /// Moves the entity with the given shift vector
271    /// </summary>
272    /// <param name="p">Represent a shift-vector, not the absolute
273    /// position!</param>
274    // ------------------------------------------------------------------
275    public override void MoveBy(Point p) {
276      base.MoveBy(p);
277      this.mTextArea.X += p.X;
278      this.mTextArea.Y += p.Y;
279    }
280
281    // ------------------------------------------------------------------
282    /// <summary>
283    /// Transforms this shape to the given new rectangle.
284    /// </summary>
285    /// <param name="x">The x-coordinate of the new rectangle.</param>
286    /// <param name="y">The y-coordinate of the new rectangle.</param>
287    /// <param name="width">The width.</param>
288    /// <param name="height">The height.</param>
289    // ------------------------------------------------------------------
290    public override void Transform(
291        int x,
292        int y,
293        int width,
294        int height) {
295      base.Transform(x, y, width, height);
296      mTextArea = Rectangle;
297
298      mTextArea.Inflate(
299          -TextRectangleInflation,
300          -TextRectangleInflation);
301    }
302
303    // ------------------------------------------------------------------
304    /// <summary>
305    /// Re-calculates the bounds of this shape to fit the text specified.
306    /// </summary>
307    /// <param name="value">string: The text to fit to.</param>
308    // ------------------------------------------------------------------
309    private void AutoReSize(string value) {
310      SizeF size = TextRenderer.MeasureText(
311          value,
312          mTextStyle.Font);
313
314      Rectangle rec = new Rectangle(
315          Rectangle.Location,
316          Size.Round(size));
317
318      rec.Inflate(TextRectangleInflation, TextRectangleInflation);
319
320      rec.Offset(TextRectangleInflation, TextRectangleInflation);
321
322      Transform(rec);
323    }
324
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.