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