Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/ComplexShapeBase.cs @ 9503

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

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

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