1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.ComponentModel;
|
---|
6 | using System.Drawing.Drawing2D;
|
---|
7 | using System.Windows.Forms;
|
---|
8 |
|
---|
9 | namespace Netron.Diagramming.Core
|
---|
10 | {
|
---|
11 | // ----------------------------------------------------------------------
|
---|
12 | /// <summary>
|
---|
13 | /// Abstract base class for complex shapes <see cref="ComplexShapeBase"/>.
|
---|
14 | /// </summary>
|
---|
15 | // ----------------------------------------------------------------------
|
---|
16 | public abstract partial class ComplexShapeBase :
|
---|
17 | ShapeBase,
|
---|
18 | IComplexShape,
|
---|
19 | IMouseListener,
|
---|
20 | IHoverListener
|
---|
21 | {
|
---|
22 | #region Fields
|
---|
23 |
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | /// <summary>
|
---|
26 | /// Implementation of IVersion - the current version of
|
---|
27 | /// ComplexShapeBase.
|
---|
28 | /// </summary>
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | protected const double complexShapeVersion = 1.0;
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// the text on the bundle
|
---|
34 | /// </summary>
|
---|
35 | protected string mText = string.Empty;
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// the shape children or sub-controls
|
---|
39 | /// </summary>
|
---|
40 | protected CollectionBase<IShapeMaterial> mChildren;
|
---|
41 |
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Properties
|
---|
45 |
|
---|
46 | // ------------------------------------------------------------------
|
---|
47 | /// <summary>
|
---|
48 | /// Gets the current version.
|
---|
49 | /// </summary>
|
---|
50 | // ------------------------------------------------------------------
|
---|
51 | public override double Version
|
---|
52 | {
|
---|
53 | get
|
---|
54 | {
|
---|
55 | return complexShapeVersion;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // ------------------------------------------------------------------
|
---|
60 | /// <summary>
|
---|
61 | /// Gets or sets the text of the bundle
|
---|
62 | /// </summary>
|
---|
63 | // ------------------------------------------------------------------
|
---|
64 | [Browsable(true),
|
---|
65 | Description("The text shown on the shape"), Category("Layout")]
|
---|
66 | public virtual string Text
|
---|
67 | {
|
---|
68 | get
|
---|
69 | {
|
---|
70 | return mText;
|
---|
71 | }
|
---|
72 | set
|
---|
73 | {
|
---|
74 | mText = value;
|
---|
75 | this.Invalidate();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | #region Constructor
|
---|
82 |
|
---|
83 | // ------------------------------------------------------------------
|
---|
84 | ///<summary>
|
---|
85 | ///Default constructor
|
---|
86 | ///</summary>
|
---|
87 | // ------------------------------------------------------------------
|
---|
88 | public ComplexShapeBase() : base()
|
---|
89 | {
|
---|
90 | }
|
---|
91 |
|
---|
92 | // ------------------------------------------------------------------
|
---|
93 | /// <summary>
|
---|
94 | /// Initializes a new instance of the <see cref="ComplexShapeBase"/>
|
---|
95 | /// class.
|
---|
96 | /// </summary>
|
---|
97 | /// <param name="model"></param>
|
---|
98 | // ------------------------------------------------------------------
|
---|
99 | public ComplexShapeBase(IModel model)
|
---|
100 | : base(model)
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | // ------------------------------------------------------------------
|
---|
105 | /// <summary>
|
---|
106 | /// Initializes this instance.
|
---|
107 | /// </summary>
|
---|
108 | // ------------------------------------------------------------------
|
---|
109 | protected override void Initialize()
|
---|
110 | {
|
---|
111 | base.Initialize();
|
---|
112 | mChildren = new CollectionBase<IShapeMaterial>();
|
---|
113 | mChildren.OnItemAdded +=
|
---|
114 | new EventHandler<CollectionEventArgs<IShapeMaterial>>
|
---|
115 | (mChildren_OnItemAdded);
|
---|
116 | }
|
---|
117 |
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Methods
|
---|
121 | // ------------------------------------------------------------------
|
---|
122 |
|
---|
123 | /// <summary>
|
---|
124 | /// Sets some contextual properties of the <see cref="Children"/>
|
---|
125 | /// when a new item is added.
|
---|
126 | /// </summary>
|
---|
127 | /// <param name="sender">The sender.</param>
|
---|
128 | /// <param name="e">The e.</param>
|
---|
129 | // ------------------------------------------------------------------
|
---|
130 | void mChildren_OnItemAdded(object sender, CollectionEventArgs<IShapeMaterial> e)
|
---|
131 | {
|
---|
132 | e.Item.Shape = this;
|
---|
133 | }
|
---|
134 |
|
---|
135 | // ------------------------------------------------------------------
|
---|
136 | /// <summary>
|
---|
137 | /// Overrides the abstract paint method
|
---|
138 | /// </summary>
|
---|
139 | /// <param name="g">a graphics object onto which to paint</param>
|
---|
140 | // ------------------------------------------------------------------
|
---|
141 | public override void Paint(Graphics g)
|
---|
142 | {
|
---|
143 |
|
---|
144 | base.Paint(g);
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | foreach (IPaintable material in Children)
|
---|
149 | {
|
---|
150 | material.Paint(g);
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 | // ------------------------------------------------------------------
|
---|
156 | /// <summary>
|
---|
157 | /// Moves the entity with the given shift
|
---|
158 | /// </summary>
|
---|
159 | /// <param name="p">represent a shift-vector, not the absolute
|
---|
160 | /// position!</param>
|
---|
161 | // ------------------------------------------------------------------
|
---|
162 | public override void MoveBy(Point p)
|
---|
163 | {
|
---|
164 | base.MoveBy(p);
|
---|
165 | Rectangle rec;
|
---|
166 | foreach (IShapeMaterial material in Children)
|
---|
167 | {
|
---|
168 | rec = material.Rectangle;
|
---|
169 |
|
---|
170 | rec.Offset(p.X, p.Y);
|
---|
171 | material.Transform(rec);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | // ------------------------------------------------------------------
|
---|
176 | /// <summary>
|
---|
177 | /// Transforms the entity to the given new rectangle.
|
---|
178 | /// </summary>
|
---|
179 | /// <param name="x">The x-coordinate of the new rectangle.</param>
|
---|
180 | /// <param name="y">The y-coordinate of the new rectangle.</param>
|
---|
181 | /// <param name="width">The width.</param>
|
---|
182 | /// <param name="height">The height.</param>
|
---|
183 | // ------------------------------------------------------------------
|
---|
184 | public override void Transform(int x, int y, int width, int height)
|
---|
185 | {
|
---|
186 | if (Children != null && Children.Count > 0)
|
---|
187 | {
|
---|
188 |
|
---|
189 | int a, b, w, h;
|
---|
190 | foreach (IShapeMaterial material in Children)
|
---|
191 | {
|
---|
192 |
|
---|
193 | if (material.Gliding)
|
---|
194 | {
|
---|
195 | a = Convert.ToInt32(Math.Round(((double)material.Rectangle.X - (double)Rectangle.X) / (double)Rectangle.Width * width, 1) + x);
|
---|
196 | b = Convert.ToInt32(Math.Round(((double)material.Rectangle.Y - (double)Rectangle.Y) / (double)Rectangle.Height * height, 1) + y);
|
---|
197 | }
|
---|
198 | else //shift the material, do not scale the position with respect to the sizing of the shape
|
---|
199 | {
|
---|
200 | a = material.Rectangle.X - Rectangle.X + x;
|
---|
201 | b = material.Rectangle.Y - Rectangle.Y + y;
|
---|
202 | }
|
---|
203 | if (material.Resizable)
|
---|
204 | {
|
---|
205 | w = Convert.ToInt32(Math.Round(((double)material.Rectangle.Width) / ((double)Rectangle.Width), 1) * width);
|
---|
206 | h = Convert.ToInt32(Math.Round(((double)material.Rectangle.Height) / ((double)Rectangle.Height), 1) * height);
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | w = material.Rectangle.Width;
|
---|
211 | h = material.Rectangle.Height;
|
---|
212 | }
|
---|
213 |
|
---|
214 | material.Transform(new Rectangle(a, b, w, h));
|
---|
215 | }
|
---|
216 | }
|
---|
217 | base.Transform(x, y, width, height);
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 |
|
---|
221 | // ------------------------------------------------------------------
|
---|
222 | /// <summary>
|
---|
223 | /// Gets or sets the children.
|
---|
224 | /// </summary>
|
---|
225 | /// <value>The children.</value>
|
---|
226 | // ------------------------------------------------------------------
|
---|
227 | public virtual CollectionBase<IShapeMaterial> Children
|
---|
228 | {
|
---|
229 | get
|
---|
230 | {
|
---|
231 | return mChildren;
|
---|
232 | }
|
---|
233 | set
|
---|
234 | {
|
---|
235 | throw new InconsistencyException("You cannot set the children, use the existing collection and the clear() method.");
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | // ------------------------------------------------------------------
|
---|
240 | /// <summary>
|
---|
241 | /// Defines a mechanism for retrieving a service object; that is, an
|
---|
242 | /// object that provides custom support to other objects.
|
---|
243 | /// </summary>
|
---|
244 | /// <param name="serviceType">An object that specifies the type of
|
---|
245 | /// service object to get.</param>
|
---|
246 | /// <returns>
|
---|
247 | /// A service object of type serviceType.-or- null if there is no
|
---|
248 | /// service object of type serviceType.
|
---|
249 | /// </returns>
|
---|
250 | // ------------------------------------------------------------------
|
---|
251 | public override object GetService(Type serviceType)
|
---|
252 | {
|
---|
253 | if (Services.ContainsKey(serviceType))
|
---|
254 | return Services[serviceType];
|
---|
255 | else
|
---|
256 | {
|
---|
257 | return null;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | // ------------------------------------------------------------------
|
---|
262 | /// <summary>
|
---|
263 | /// <see cref="IMouseListener"/> implementation.
|
---|
264 | /// </summary>
|
---|
265 | /// <param name="e">The
|
---|
266 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
267 | /// containing the event data.</param>
|
---|
268 | // ------------------------------------------------------------------
|
---|
269 | public override bool MouseDown(MouseEventArgs e)
|
---|
270 | {
|
---|
271 | base.MouseDown(e);
|
---|
272 | IMouseListener listener ;
|
---|
273 | foreach (IShapeMaterial material in mChildren)
|
---|
274 | {
|
---|
275 | if(material.Rectangle.Contains(e.Location) && material.Visible)
|
---|
276 | {
|
---|
277 | listener = material.GetService(typeof(
|
---|
278 | IMouseListener)) as IMouseListener;
|
---|
279 | if (listener != null)
|
---|
280 | if (listener.MouseDown(e))
|
---|
281 | return true;
|
---|
282 | }
|
---|
283 |
|
---|
284 | }
|
---|
285 | return false;
|
---|
286 | }
|
---|
287 |
|
---|
288 | // ------------------------------------------------------------------
|
---|
289 | /// <summary>
|
---|
290 | /// <see cref="IMouseListener"/> implementation.
|
---|
291 | /// </summary>
|
---|
292 | /// <param name="e">The
|
---|
293 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
294 | /// containing the event data.</param>
|
---|
295 | // ------------------------------------------------------------------
|
---|
296 | public override void MouseMove(MouseEventArgs e)
|
---|
297 | {
|
---|
298 | base.MouseMove(e);
|
---|
299 | IMouseListener listener;
|
---|
300 | foreach (IShapeMaterial material in mChildren)
|
---|
301 | {
|
---|
302 | if ( (material.Rectangle.Contains(e.Location)) &&
|
---|
303 | (material.Visible) )
|
---|
304 | {
|
---|
305 | listener = material.GetService(typeof
|
---|
306 | (IMouseListener)) as IMouseListener;
|
---|
307 |
|
---|
308 | if (listener != null)
|
---|
309 | {
|
---|
310 | listener.MouseMove(e);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | // ------------------------------------------------------------------
|
---|
317 | /// <summary>
|
---|
318 | /// <see cref="IMouseListener"/> implementation.
|
---|
319 | /// </summary>
|
---|
320 | /// <param name="e">The
|
---|
321 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
322 | /// containing the event data.</param>
|
---|
323 | // ------------------------------------------------------------------
|
---|
324 | public override void MouseUp(MouseEventArgs e)
|
---|
325 | {
|
---|
326 | base.MouseUp(e);
|
---|
327 | IMouseListener listener;
|
---|
328 | foreach (IShapeMaterial material in mChildren)
|
---|
329 | {
|
---|
330 | if(material.Rectangle.Contains(e.Location) && material.Visible)
|
---|
331 | {
|
---|
332 | listener = material.GetService(typeof(IMouseListener)) as IMouseListener;
|
---|
333 | if (listener != null) listener.MouseUp(e);
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 |
|
---|
340 | #region IHoverListener Members
|
---|
341 |
|
---|
342 | private IHoverListener currentHoveredMaterial;
|
---|
343 |
|
---|
344 | // ------------------------------------------------------------------
|
---|
345 | /// <summary>
|
---|
346 | /// Handles the MouseHover event.
|
---|
347 | /// </summary>
|
---|
348 | /// <param name="e">The
|
---|
349 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
350 | /// containing the event data.</param>
|
---|
351 | // ------------------------------------------------------------------
|
---|
352 | public override void MouseHover(MouseEventArgs e)
|
---|
353 | {
|
---|
354 | base.MouseHover(e);
|
---|
355 | IHoverListener listener;
|
---|
356 | foreach(IShapeMaterial material in this.Children)
|
---|
357 | {
|
---|
358 | if(material.Rectangle.Contains(e.Location) && material.Visible) //we caught an material and it's visible
|
---|
359 | {
|
---|
360 | listener = material.GetService(typeof(IHoverListener)) as IHoverListener;
|
---|
361 | if(listener != null) //the caught material does listen
|
---|
362 | {
|
---|
363 | if(currentHoveredMaterial == listener) //it's the same as the previous time
|
---|
364 | listener.MouseHover(e);
|
---|
365 | else //we moved from one material to another listening material
|
---|
366 | {
|
---|
367 | if(currentHoveredMaterial != null) //tell the previous material we are leaving
|
---|
368 | currentHoveredMaterial.MouseLeave(e);
|
---|
369 | listener.MouseEnter(e); //tell the current one we enter
|
---|
370 | currentHoveredMaterial = listener;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | else //the caught material does not listen
|
---|
374 | {
|
---|
375 | if(currentHoveredMaterial != null)
|
---|
376 | {
|
---|
377 | currentHoveredMaterial.MouseLeave(e);
|
---|
378 | currentHoveredMaterial = null;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | return; //only one material at a time
|
---|
382 | }
|
---|
383 |
|
---|
384 | }
|
---|
385 | if(currentHoveredMaterial != null)
|
---|
386 | {
|
---|
387 | currentHoveredMaterial.MouseLeave(e);
|
---|
388 | currentHoveredMaterial = null;
|
---|
389 | }
|
---|
390 |
|
---|
391 | }
|
---|
392 |
|
---|
393 | // ------------------------------------------------------------------
|
---|
394 | /// <summary>
|
---|
395 | /// Handles the OnMouseEnter event.
|
---|
396 | /// </summary>
|
---|
397 | /// <param name="e">The
|
---|
398 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
399 | /// containing the event data.</param>
|
---|
400 | // ------------------------------------------------------------------
|
---|
401 | public override void MouseEnter(MouseEventArgs e)
|
---|
402 | {
|
---|
403 | base.MouseEnter(e);
|
---|
404 | }
|
---|
405 |
|
---|
406 | // ------------------------------------------------------------------
|
---|
407 | /// <summary>
|
---|
408 | /// Handles the OnMouseLeave event.
|
---|
409 | /// </summary>
|
---|
410 | /// <param name="e">The
|
---|
411 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
412 | /// containing the event data.</param>
|
---|
413 | // ------------------------------------------------------------------
|
---|
414 | public override void MouseLeave(MouseEventArgs e)
|
---|
415 | {
|
---|
416 | base.MouseLeave(e);
|
---|
417 | }
|
---|
418 |
|
---|
419 | #endregion
|
---|
420 | }
|
---|
421 | }
|
---|