Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/DiagramEntityBase.cs @ 15682

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

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

File size: 42.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Windows.Forms;
6namespace Netron.Diagramming.Core {
7  // ----------------------------------------------------------------------
8  /// <summary>
9  /// Abstract base class for every diagram entity.
10  /// </summary>
11  // ----------------------------------------------------------------------
12  public abstract partial class DiagramEntityBase :
13      IDiagramEntity,
14      IMouseListener,
15      IHoverListener,
16      IKeyboardListener,
17      IDisposable {
18    #region Events
19
20    // ------------------------------------------------------------------
21    /// <summary>
22    /// Occurs when the user click on the entity.
23    /// </summary>
24    // ------------------------------------------------------------------
25    public event EventHandler<EntityEventArgs> OnClick;
26
27    // ------------------------------------------------------------------
28    /// <summary>
29    /// Occurs when a mouse button is pressed while over the entity.
30    /// </summary>
31    // ------------------------------------------------------------------
32    public event EventHandler<EntityMouseEventArgs> OnMouseDown;
33
34    // ------------------------------------------------------------------
35    /// <summary>
36    /// Occurs when a mouse button is released while over the entity.
37    /// </summary>
38    // ------------------------------------------------------------------
39    public event EventHandler<EntityMouseEventArgs> OnMouseUp;
40
41    // ------------------------------------------------------------------
42    /// <summary>
43    /// Occurs when the mouse is moved while over the entity.
44    /// </summary>
45    // ------------------------------------------------------------------
46    public event EventHandler<EntityMouseEventArgs> OnMouseMove;
47
48    // ------------------------------------------------------------------
49    /// <summary>
50    /// Occurs when the mouse enters the entity.
51    /// </summary>
52    // ------------------------------------------------------------------
53    public event EventHandler<EntityMouseEventArgs> OnMouseEnter;
54
55    // ------------------------------------------------------------------
56    /// <summary>
57    /// Occurs when the mouse hovers over the entity.
58    /// </summary>
59    // ------------------------------------------------------------------
60    public event EventHandler<EntityMouseEventArgs> OnMouseHover;
61
62    // ------------------------------------------------------------------
63    /// <summary>
64    /// Occurs when the mouse leaves the entity.
65    /// </summary>
66    // ------------------------------------------------------------------
67    public event EventHandler<EntityMouseEventArgs> OnMouseLeave;
68
69    // ------------------------------------------------------------------
70    /// <summary>
71    /// Occurs when the entity's properties have changed
72    /// </summary>
73    // ------------------------------------------------------------------
74    public event EventHandler<EntityEventArgs> OnEntityChange;
75
76    // ------------------------------------------------------------------
77    /// <summary>
78    /// Occurs when the entity is selected. This can be different than the
79    /// OnClick because the selector can select and entity without
80    /// clicking on it.
81    /// </summary>
82    // ------------------------------------------------------------------
83    public event EventHandler<EntityEventArgs> OnEntitySelect;
84
85    #endregion
86
87    #region Fields
88
89    // ------------------------------------------------------------------
90    /// <summary>
91    /// Implementation of IVersion - the current version of
92    /// DiagramEntityBase.
93    /// </summary>
94    // ------------------------------------------------------------------
95    protected const double diagramEntityBaseVersion = 1.0;
96
97    // ------------------------------------------------------------------
98    /// <summary>
99    /// The services of this entity.
100    /// </summary>
101    // ------------------------------------------------------------------
102    protected Dictionary<Type, IInteraction> mServices;
103
104    // ------------------------------------------------------------------
105    /// <summary>
106    /// The Rectangle on which any bundle lives.
107    /// </summary>
108    // ------------------------------------------------------------------
109    protected Rectangle mRectangle = Rectangle.Empty;
110
111    // ------------------------------------------------------------------
112    /// <summary>
113    /// General prupose tag
114    /// </summary>
115    // ------------------------------------------------------------------
116    protected object mTag;
117
118    // ------------------------------------------------------------------
119    /// <summary>
120    /// tells whether the current entity is hovered by the mouse
121    /// </summary>
122    // ------------------------------------------------------------------
123    protected bool mHovered;
124
125    // ------------------------------------------------------------------
126    /// <summary>
127    /// The current magnification of the view.
128    /// </summary>
129    // ------------------------------------------------------------------
130    protected SizeF mMagnification = new SizeF(100F, 100F);
131
132    // ------------------------------------------------------------------
133    /// <summary>
134    /// The Model to which the eneity belongs.
135    /// </summary>
136    // ------------------------------------------------------------------
137    protected IModel mModel;
138
139    // ------------------------------------------------------------------
140    /// <summary>
141    /// The layer to which this entity is attached in the Model.
142    /// </summary>
143    // ------------------------------------------------------------------
144    protected ILayer mLayer;
145
146    // ------------------------------------------------------------------
147    /// <summary>
148    /// tells whether the entity is selected
149    /// </summary>
150    // ------------------------------------------------------------------
151    protected bool mIsSelected;
152
153    // ------------------------------------------------------------------
154    /// <summary>
155    /// the current draw style
156    /// </summary>
157    // ------------------------------------------------------------------
158    protected IPenStyle mPenStyle;
159
160    // ------------------------------------------------------------------
161    /// <summary>
162    /// the current paint style
163    /// </summary>
164    // ------------------------------------------------------------------
165    protected IPaintStyle mPaintStyle;
166
167    // ------------------------------------------------------------------
168    /// <summary>
169    /// the default pen to be used by the Paint method
170    /// </summary>
171    // ------------------------------------------------------------------
172    protected Pen mPen;
173
174    // ------------------------------------------------------------------
175    /// <summary>
176    /// the default brush to be used by the Paint method
177    /// </summary>
178    // ------------------------------------------------------------------
179    protected Brush mBrush;
180
181    // ------------------------------------------------------------------
182    /// <summary>
183    /// the name of the entity
184    /// </summary>
185    // ------------------------------------------------------------------
186    protected string mName;
187
188    // ------------------------------------------------------------------
189    /// <summary>
190    /// a weak reference to the parent
191    /// </summary>
192    // ------------------------------------------------------------------
193    protected WeakReference mParent;
194
195    // ------------------------------------------------------------------
196    /// <summary>
197    /// the scene index, i.e. the index of this entity in the scene-graph.
198    /// </summary>
199    // ------------------------------------------------------------------
200    protected int mSceneIndex;
201
202    // ------------------------------------------------------------------
203    /// <summary>
204    /// The top-group to underneath which this entity resides.
205    /// </summary>
206    // ------------------------------------------------------------------
207    protected IGroup mGroup;
208
209    // ------------------------------------------------------------------
210    /// <summary>
211    /// Specifies if the entity can be moved.
212    /// </summary>
213    // ------------------------------------------------------------------
214    protected bool mAllowMove = true;
215
216    // ------------------------------------------------------------------
217    /// <summary>
218    /// Specifies if the entity can be deleted.
219    /// </summary>
220    // ------------------------------------------------------------------
221    protected bool mAllowDelete = true;
222
223    // ------------------------------------------------------------------
224    /// <summary>
225    /// Specifies if the entity can be resized.
226    /// </summary>
227    // ------------------------------------------------------------------
228    protected bool mResizable = true;
229
230    // ------------------------------------------------------------------
231    /// <summary>
232    /// The minimum size this entity can be.  The default value is:
233    /// width = 10, height = 10.  This seems to be the min size that
234    /// keeps the connectors relative position correct during a Transform.
235    /// </summary>
236    // ------------------------------------------------------------------
237    protected Size myMinSize = new Size(10, 10);
238
239    // ------------------------------------------------------------------
240    /// <summary>
241    /// The maximum size this entity can be.  The default value is:
242    /// width = 10000, height = 10000.
243    /// </summary>
244    // ------------------------------------------------------------------
245    protected Size myMaxSize = new Size(10000, 10000);
246
247    // ------------------------------------------------------------------
248    /// <summary>
249    /// The unique identifier of this entity
250    /// </summary>
251    // ------------------------------------------------------------------
252    protected Guid mUid = Guid.NewGuid();
253
254    // ------------------------------------------------------------------
255    /// <summary>
256    /// whether the entity is visible
257    /// </summary>
258    // ------------------------------------------------------------------
259    protected bool mVisible = true;
260
261    // ------------------------------------------------------------------
262    /// <summary>
263    /// The Enabled field.
264    /// </summary>
265    // ------------------------------------------------------------------
266    protected bool mEnabled = true;
267
268    #endregion
269
270    #region Properties
271
272    // ------------------------------------------------------------------
273    /// <summary>
274    /// Gets the current version.
275    /// </summary>
276    // ------------------------------------------------------------------
277    public virtual double Version {
278      get {
279        return diagramEntityBaseVersion;
280      }
281    }
282
283    // ------------------------------------------------------------------
284    /// <summary>
285    /// Gets the services provided by this entity.
286    /// </summary>
287    /// <value>The services.</value>
288    // ------------------------------------------------------------------
289    public Dictionary<Type, IInteraction> Services {
290      get { return mServices; }
291    }
292
293    // ------------------------------------------------------------------
294    /// <summary>
295    /// Gets or sets whether this entity is Enabled.
296    /// </summary>
297    // ------------------------------------------------------------------
298    public virtual bool Enabled {
299      get {
300        return mEnabled;
301      }
302      set {
303        mEnabled = value;
304        RaiseOnChange(this, new EntityEventArgs(this));
305      }
306    }
307
308    // ------------------------------------------------------------------
309    /// <summary>
310    /// Gets or sets a value indicating whether this entity is visible.
311    /// </summary>
312    /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
313    // ------------------------------------------------------------------
314    public virtual bool Visible {
315      get {
316        return mVisible;
317      }
318      set {
319        mVisible = value;
320        RaiseOnChange(this, new EntityEventArgs(this));
321      }
322    }
323
324    // ------------------------------------------------------------------
325    /// <summary>
326    /// Gets or sets the drawing style.
327    /// </summary>
328    /// <value>The draw style.</value>
329    // ------------------------------------------------------------------
330    public virtual IPenStyle PenStyle {
331      get {
332        return mPenStyle;
333      }
334      set {
335        mPenStyle = value;
336        RaiseOnChange(this, new EntityEventArgs(this));
337        UpdatePaintingMaterial();
338      }
339    }
340
341    // ------------------------------------------------------------------
342    /// <summary>
343    /// Gets or sets the paint style.
344    /// </summary>
345    /// <value>The paint style.</value>
346    // ------------------------------------------------------------------
347    public virtual IPaintStyle PaintStyle {
348      get {
349        return mPaintStyle;
350      }
351      set {
352        mPaintStyle = value;
353        RaiseOnChange(this, new EntityEventArgs(this));
354        UpdatePaintingMaterial();
355      }
356    }
357
358    // ------------------------------------------------------------------
359    /// <summary>
360    /// Gets the globally unique identifier of this entity
361    /// </summary>
362    /// <value></value>
363    // ------------------------------------------------------------------
364    public virtual Guid Uid {
365      get {
366        return mUid;
367      }
368
369    }
370
371    // ------------------------------------------------------------------
372    /// <summary>
373    /// Gets or sets a value indicating whether this
374    /// <see cref="IDiagramEntity"/> can be moved.
375    /// </summary>
376    /// <value><c>true</c> if movable; otherwise, <c>false</c>.</value>
377    // ------------------------------------------------------------------
378    public virtual bool AllowMove {
379      get {
380        return mAllowMove;
381      }
382      set {
383        mAllowMove = value;
384      }
385    }
386
387    // ------------------------------------------------------------------
388    /// <summary>
389    /// Gets or sets a value indicating whether this
390    /// <see cref="IDiagramEntity"/> can be deleted.
391    /// </summary>
392    /// <value><c>true</c> if deletable; otherwise, <c>false</c>.</value>
393    // ------------------------------------------------------------------
394    public virtual bool AllowDelete {
395      get {
396        return mAllowDelete;
397      }
398      set {
399        mAllowDelete = value;
400      }
401    }
402
403    // ------------------------------------------------------------------
404    /// <summary>
405    /// Gets or sets the Resizable
406    /// </summary>
407    // ------------------------------------------------------------------
408    public virtual bool Resizable {
409      get {
410        return mResizable;
411      }
412      set {
413        mResizable = value;
414      }
415    }
416
417    // ------------------------------------------------------------------
418    /// <summary>
419    /// Gets the minimum size of the entity.
420    /// </summary>
421    // ------------------------------------------------------------------
422    public virtual Size MinSize {
423      get {
424        return myMinSize;
425      }
426    }
427
428    // ------------------------------------------------------------------
429    /// <summary>
430    /// Gets the maximum size of the entity.
431    /// </summary>
432    // ------------------------------------------------------------------
433    public virtual Size MaxSize {
434      get {
435        return myMaxSize;
436      }
437    }
438
439    // ------------------------------------------------------------------
440    /// <summary>
441    /// Gets the <see cref="Brush"/> to paint this entity.
442    /// </summary>
443    // ------------------------------------------------------------------
444    public virtual Brush Brush {
445      get {
446        return mBrush;
447      }
448    }
449
450    // ------------------------------------------------------------------
451    /// <summary>
452    /// Gets the pen to draw this entity.
453    /// </summary>
454    /// <value>The pen.</value>
455    // ------------------------------------------------------------------
456    public virtual Pen Pen {
457      get {
458        return mPen;
459      }
460    }
461
462    // ------------------------------------------------------------------
463    /// <summary>
464    /// Gets the friendly name of the entity to be displayed in the UI.
465    /// </summary>
466    /// <value></value>
467    // ------------------------------------------------------------------
468    public abstract string EntityName {
469      get;
470    }
471
472    // ------------------------------------------------------------------
473    /// <summary>
474    /// Gets or sets the general purpose tag
475    /// </summary>
476    // ------------------------------------------------------------------
477    public virtual object Tag {
478      get {
479        return mTag;
480      }
481      set {
482        mTag = value;
483      }
484    }
485
486    // ------------------------------------------------------------------
487    /// <summary>
488    /// Gets or sets the index of this entity in the scene-graph.
489    /// </summary>
490    /// <value>The index of the scene.</value>
491    // ------------------------------------------------------------------
492    public virtual int SceneIndex {
493      get {
494        return mSceneIndex;
495      }
496      set {
497        mSceneIndex = value;
498      }
499    }
500
501    // ------------------------------------------------------------------
502    /// <summary>
503    /// Gets or sets the unique top-group to which this entity belongs.
504    /// </summary>
505    /// <value></value>
506    // ------------------------------------------------------------------
507    public virtual IGroup Group {
508      get {
509        return mGroup;
510      }
511      set {
512        mGroup = value;
513        // Propagate downwards if this is a group shape, but not if
514        // the value is 'null' since the group becomes the value of
515        // the Group property.  Note that we could have used a formal
516        // depth-traversal algorithm.
517        if (this is IGroup) {
518          if (value == null)//occurs on an ungroup action
519                    {
520            foreach (IDiagramEntity entity in
521                (this as IGroup).Entities) {
522              entity.Group = this as IGroup;
523            }
524          } else //occurs when grouping
525                    {
526            foreach (IDiagramEntity entity in
527                (this as IGroup).Entities) {
528              entity.Group = value;
529            }
530          }
531        }
532
533      }
534    }
535
536    // ------------------------------------------------------------------
537    /// <summary>
538    /// Gets or sets whether the entity is hovered by the mouse
539    /// </summary>
540    // ------------------------------------------------------------------
541    public virtual bool Hovered {
542      get {
543        return mHovered;
544      }
545      set {
546        mHovered = value;
547        Invalidate();
548      }
549    }
550
551    // ------------------------------------------------------------------
552    /// <summary>
553    /// Gets or sets the parent of the entity
554    /// </summary>
555    // ------------------------------------------------------------------
556    public virtual object Parent {
557      get {
558        if (mParent != null && mParent.IsAlive) {
559          return mParent.Target;
560        } else {
561          return null;
562        }
563      }
564      set {
565        mParent = new WeakReference(value);
566        RaiseOnChange(this, new EntityEventArgs(this));
567      }
568    }
569
570    // ------------------------------------------------------------------
571    /// <summary>
572    /// Gets or sets the name of the entity
573    /// </summary>
574    // ------------------------------------------------------------------
575    public virtual string Name {
576      get {
577        return mName;
578      }
579      set {
580        mName = value;
581        RaiseOnChange(this, new EntityEventArgs(this));
582      }
583    }
584
585    #region Bounds and point calculations
586
587    // ------------------------------------------------------------------
588    /// <summary>
589    /// Gets the bounds of the paintable entity.
590    /// </summary>
591    /// <value></value>
592    // ------------------------------------------------------------------
593    public abstract Rectangle Rectangle {
594      get;
595    }
596
597    // ------------------------------------------------------------------
598    /// <summary>
599    /// Gets the top left corner of this entity, which is the same as
600    /// 'Rectangle.Location'.
601    /// </summary>
602    // ------------------------------------------------------------------
603    public virtual Point TopLeftCorner {
604      get {
605        return this.Rectangle.Location;
606      }
607    }
608
609    // ------------------------------------------------------------------
610    /// <summary>
611    /// Gets the top right corner of this entity.
612    /// </summary>
613    // ------------------------------------------------------------------
614    public virtual Point TopRightCorner {
615      get {
616        return new Point(
617            Rectangle.Right,
618            Rectangle.Top);
619      }
620    }
621
622    // ------------------------------------------------------------------
623    /// <summary>
624    /// Gets the bottom left corner of this entity.
625    /// </summary>
626    // ------------------------------------------------------------------
627    public virtual Point BottomLeftCorner {
628      get {
629        return new Point(
630            Rectangle.Left,
631            Rectangle.Bottom);
632      }
633    }
634
635    // ------------------------------------------------------------------
636    /// <summary>
637    /// Gets the bottom right corner of this entity.
638    /// </summary>
639    // ------------------------------------------------------------------
640    public virtual Point BottomRightCorner {
641      get {
642        return new Point(
643            Rectangle.Right,
644            Rectangle.Bottom);
645      }
646    }
647
648    // ------------------------------------------------------------------
649    /// <summary>
650    /// Gets the center point of the paintable entity (the center of the
651    /// Rectangle).
652    /// </summary>
653    /// <value>Point</value>
654    // ------------------------------------------------------------------
655    public virtual Point Center {
656      get {
657        // Make sure the bounds are legal first.
658        if ((this.mRectangle == null) ||
659            (this.mRectangle == Rectangle.Empty)) {
660          return Point.Empty;
661        }
662
663        int x =
664            (this.mRectangle.Left) +
665            (this.mRectangle.Width / 2);
666
667        int y =
668            (this.mRectangle.Top) +
669            (this.mRectangle.Height / 2);
670        return new Point(x, y);
671      }
672    }
673
674    // ------------------------------------------------------------------
675    /// <summary>
676    /// Gets the center of the bottom edge of the bounding rectangle.
677    /// </summary>
678    // ------------------------------------------------------------------
679    public virtual Point BottomCenter {
680      get {
681        return new Point(
682            BottomLeftCorner.X + (mRectangle.Width / 2),
683            BottomLeftCorner.Y);
684      }
685    }
686
687    // ------------------------------------------------------------------
688    /// <summary>
689    /// Gets the center of the top edge of the bounding rectangle.
690    /// </summary>
691    // ------------------------------------------------------------------
692    public virtual Point TopCenter {
693      get {
694        return new Point(
695            TopLeftCorner.X + (mRectangle.Width / 2),
696            TopLeftCorner.Y);
697      }
698    }
699
700    #endregion
701
702    // ------------------------------------------------------------------
703    /// <summary>
704    /// Gets or sets whether the entity is selected
705    /// </summary>
706    // ------------------------------------------------------------------
707    [Browsable(false)]
708    public virtual bool IsSelected {
709      get {
710        return mIsSelected;
711      }
712      set {
713        mIsSelected = value;
714        if (value == true) {
715          this.RaiseOnSelect(this, new EntityEventArgs(this));
716        }
717      }
718    }
719
720    // ------------------------------------------------------------------
721    /// <summary>
722    /// Gets or sets the current magnification used by the view.
723    /// </summary>
724    // ------------------------------------------------------------------
725    public virtual SizeF Magnification {
726      get {
727        return mMagnification;
728      }
729      set {
730        mMagnification = value;
731      }
732    }
733
734    // ------------------------------------------------------------------
735    /// <summary>
736    /// Gets or sets the canvas to which the entity belongs.
737    /// </summary>
738    // ------------------------------------------------------------------
739    [Browsable(false)]
740    public virtual IModel Model {
741      get {
742        return mModel;
743      }
744      set {
745        mModel = value;
746      }
747    }
748
749    // ------------------------------------------------------------------
750    /// <summary>
751    /// Gets or sets the ILayer this entity is attached to in the IModel.
752    /// </summary>
753    // ------------------------------------------------------------------
754    public virtual ILayer Layer {
755      get {
756        return mLayer;
757      }
758      set {
759        mLayer = value;
760      }
761    }
762
763    #endregion
764
765    #region Constructor
766
767    // ------------------------------------------------------------------
768    /// <summary>
769    /// Constructor with the model of the entity.
770    /// </summary>
771    /// <param mName="model">IModel</param>
772    // ------------------------------------------------------------------
773    protected DiagramEntityBase(IModel model) {
774      this.mModel = model;
775      Initialize();
776    }
777
778    // ------------------------------------------------------------------
779    /// <summary>
780    /// The empty constructor is required to make deserialization work.
781    /// </summary>
782    // ------------------------------------------------------------------
783    protected DiagramEntityBase() {
784      Initialize();
785    }
786
787    #endregion
788
789    #region Methods
790
791    // ------------------------------------------------------------------
792    /// <summary>
793    /// Called after an entity is deleted.
794    /// </summary>
795    /// <param name="deleteCommand">DeleteCommand: The un/redoable command
796    /// that's part of the undo/redo mechanism.</param>
797    // ------------------------------------------------------------------
798    public virtual void OnAfterDelete(DeleteCommand deleteCommand) {
799    }
800
801    // ------------------------------------------------------------------
802    /// <summary>
803    /// Called before an entity is deleted.
804    /// </summary>
805    /// <param name="deleteCommand">DeleteCommand: The un/redoable command
806    /// that's part of the undo/redo mechanism.</param>
807    // ------------------------------------------------------------------
808    public virtual void OnBeforeDelete(DeleteCommand deleteCommand) {
809    }
810
811    // ------------------------------------------------------------------
812    /// <summary>
813    /// Called when a new DiagramEntityBase is instantiated.
814    /// </summary>
815    // ------------------------------------------------------------------
816    protected virtual void Initialize() {
817      PaintStyle = ArtPalette.GetDefaultPaintStyle();
818      PenStyle = ArtPalette.GetDefaultPenStyle();
819
820      mServices = new Dictionary<Type, IInteraction>();
821      mServices[typeof(IMouseListener)] = this;
822      mServices[typeof(IHoverListener)] = this;
823    }
824
825    // ------------------------------------------------------------------
826    /// <summary>
827    /// Generates a new Uid for this entity.
828    /// </summary>
829    /// <param name="recursive">if the Uid has to be changed recursively
830    /// down to the sub-entities, set to true, otherwise false.</param>
831    // ------------------------------------------------------------------
832    public virtual void NewUid(bool recursive) {
833      this.mUid = Guid.NewGuid();
834      RaiseOnChange(this, new EntityEventArgs(this));
835    }
836
837    // ------------------------------------------------------------------
838    /// <summary>
839    /// Defines a mechanism for retrieving a service object; that is, an
840    /// object that provides custom support to other objects.
841    /// </summary>
842    /// <param name="serviceType">An object that specifies the type of
843    /// service object to get.</param>
844    /// <returns>
845    /// A service object of type serviceType.-or- null if there is no
846    /// service object of type serviceType.
847    /// </returns>
848    // ------------------------------------------------------------------
849    public virtual object GetService(Type serviceType) {
850      if (Services.ContainsKey(serviceType)) {
851        return Services[serviceType];
852      } else {
853        return null;
854      }
855    }
856
857    // ------------------------------------------------------------------
858    /// <summary>
859    /// Paints the entity on the control
860    /// </summary>
861    /// <param mName="g">the graphics object to paint on</param>
862    // ------------------------------------------------------------------
863    public abstract void Paint(Graphics g);
864
865    // ------------------------------------------------------------------
866    /// <summary>
867    /// Tests whether the entity is hit by the mouse
868    /// </summary>
869    /// <param>a Point location</param>
870    /// <param name="p"></param>
871    // ------------------------------------------------------------------
872    public abstract bool Hit(Point p);
873
874    // ------------------------------------------------------------------
875    /// <summary>
876    /// Invalidates the entity
877    /// </summary>
878    // ------------------------------------------------------------------
879    public abstract void Invalidate();
880
881    // ------------------------------------------------------------------
882    /// <summary>
883    /// Called when the entity is detached from the canvas (temporarily
884    /// removed but not disposed, like in a cut operation).
885    /// </summary>
886    // ------------------------------------------------------------------
887    public virtual void Detached(ILayer layer) {
888      mLayer = null;
889    }
890
891    // ------------------------------------------------------------------
892    /// <summary>
893    /// Called when the entity is attached to a Layer.
894    /// </summary>
895    // ------------------------------------------------------------------
896    public virtual void Attached(ILayer layer) {
897      mLayer = layer;
898    }
899
900    // ------------------------------------------------------------------
901    /// <summary>
902    /// Invalidates a rectangle of the canvas
903    /// </summary>
904    /// <param name="rectangle"></param>
905    // ------------------------------------------------------------------
906    public virtual void Invalidate(Rectangle rectangle) {
907      if (Model != null)
908        Model.RaiseOnInvalidateRectangle(rectangle);
909    }
910
911    // ------------------------------------------------------------------
912    /// <summary>
913    /// Updates pens and brushes
914    /// </summary>
915    // ------------------------------------------------------------------
916    protected virtual void UpdatePaintingMaterial() {
917      // First make sure we have a valid rectangle.
918      if (mRectangle.Width == 0) {
919        mRectangle.Width = 1;
920      }
921
922      if (mRectangle.Height == 0) {
923        mRectangle.Height = 1;
924      }
925
926      if (mPenStyle != null) {
927        mPen = mPenStyle.DrawingPen();
928      }
929
930      if (mPaintStyle != null) {
931        mBrush = mPaintStyle.GetBrush(this.Rectangle);
932      }
933
934      Invalidate();
935    }
936
937    // ------------------------------------------------------------------
938    /// <summary>
939    /// Moves the entity on the canvas
940    /// </summary>
941    /// <param mName="p">the shifting vector, not an absolute
942    /// position!</param>
943    // ------------------------------------------------------------------
944    public abstract void MoveBy(Point p);
945
946    // ------------------------------------------------------------------
947    /// <summary>
948    /// The custom elements to be added to the menu on a per-entity basis.
949    /// </summary>
950    /// <returns>ToolStripItem[]</returns>
951    // ------------------------------------------------------------------
952    public abstract ToolStripItem[] Menu();
953
954    #region Raisers
955
956    // ------------------------------------------------------------------
957    /// <summary>
958    /// Raises the onclick event.
959    /// </summary>
960    /// <param name="e"></param>
961    // ------------------------------------------------------------------
962    public virtual void RaiseOnClick(EntityEventArgs e) {
963      if (OnClick != null)
964        OnClick(this, e);
965    }
966
967    // ------------------------------------------------------------------
968    /// <summary>
969    /// Raises the OnMouseDown event.
970    /// </summary>
971    /// <param name="e">EntityMouseEventArgs</param>
972    // ------------------------------------------------------------------
973    public virtual void RaiseOnMouseDown(EntityMouseEventArgs e) {
974      if (OnMouseDown != null) {
975        OnMouseDown(this, e);
976      }
977    }
978
979    // ------------------------------------------------------------------
980    /// <summary>
981    /// Raises the OnMouseUp event.
982    /// </summary>
983    /// <param name="e">EntityMouseEventArgs</param>
984    // ------------------------------------------------------------------
985    public virtual void RaiseOnMouseUp(EntityMouseEventArgs e) {
986      if (OnMouseUp != null) {
987        OnMouseUp(this, e);
988      }
989    }
990
991    // ------------------------------------------------------------------
992    /// <summary>
993    /// Raises the OnMouseMove event.
994    /// </summary>
995    /// <param name="e">EntityMouseEventArgs</param>
996    // ------------------------------------------------------------------
997    public virtual void RaiseOnMouseMove(EntityMouseEventArgs e) {
998      if (OnMouseMove != null) {
999        OnMouseMove(this, e);
1000      }
1001    }
1002
1003    // ------------------------------------------------------------------
1004    /// <summary>
1005    /// Raises the OnMouseEnter event.
1006    /// </summary>
1007    /// <param name="e">EntityMouseEventArgs</param>
1008    // ------------------------------------------------------------------
1009    public virtual void RaiseOnMouseEnter(EntityMouseEventArgs e) {
1010      if (OnMouseEnter != null) {
1011        OnMouseEnter(this, e);
1012      }
1013    }
1014
1015    // ------------------------------------------------------------------
1016    /// <summary>
1017    /// Raises the OnMouseEnter event.
1018    /// </summary>
1019    /// <param name="e">EntityMouseEventArgs</param>
1020    // ------------------------------------------------------------------
1021    public virtual void RaiseOnMouseHover(EntityMouseEventArgs e) {
1022      if (OnMouseHover != null) {
1023        OnMouseHover(this, e);
1024      }
1025    }
1026
1027    // ------------------------------------------------------------------
1028    /// <summary>
1029    /// Raises the OnMouseLeave event.
1030    /// </summary>
1031    /// <param name="e">EntityMouseEventArgs</param>
1032    // ------------------------------------------------------------------
1033    public virtual void RaiseOnMouseLeave(EntityMouseEventArgs e) {
1034      if (OnMouseLeave != null) {
1035        OnMouseLeave(this, e);
1036      }
1037    }
1038
1039    // ------------------------------------------------------------------
1040    /// <summary>
1041    /// Raises the OnSelect event.
1042    /// </summary>
1043    /// <param name="sender">The sender.</param>
1044    /// <param name="e">The
1045    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
1046    /// containing the event data.</param>
1047    // ------------------------------------------------------------------
1048    protected virtual void RaiseOnSelect(
1049        object sender,
1050        EntityEventArgs e) {
1051      if (OnEntitySelect != null) {
1052        OnEntitySelect(sender, e);
1053      }
1054    }
1055
1056    // ------------------------------------------------------------------
1057    /// <summary>
1058    /// Raises the OnChange event.
1059    /// </summary>
1060    /// <param name="sender">The sender.</param>
1061    /// <param name="e">The
1062    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
1063    /// containing the event data.</param>
1064    // ------------------------------------------------------------------
1065    protected virtual void RaiseOnChange(object sender, EntityEventArgs e) {
1066      if (OnEntityChange != null)
1067        OnEntityChange(sender, e);
1068    }
1069
1070    #endregion
1071
1072    #endregion
1073
1074    #region Standard IDispose implementation
1075    /// <summary>
1076    /// Disposes the entity.
1077    /// </summary>
1078    public void Dispose() {
1079      Dispose(true);
1080      GC.SuppressFinalize(this);
1081
1082
1083    }
1084
1085    /// <summary>
1086    /// Disposes the entity.
1087    /// </summary>
1088    /// <param name="disposing">if set to <c>true</c> [disposing].</param>
1089    protected virtual void Dispose(bool disposing) {
1090      if (disposing) {
1091        #region free managed resources
1092
1093
1094        if (mPen != null) {
1095          mPen.Dispose();
1096          mPen = null;
1097        }
1098        if (mBrush != null) {
1099          mBrush.Dispose();
1100          mBrush = null;
1101        }
1102        #endregion
1103      }
1104
1105    }
1106
1107    #endregion
1108
1109    #region IMouseListener Members
1110
1111    // ------------------------------------------------------------------
1112    /// <summary>
1113    /// Implementation of the <see cref="IMouseListener"/>.  This is
1114    /// the method called when a mouse button is pressed while over this
1115    /// entity.
1116    /// </summary>
1117    /// <param name="e">The
1118    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1119    /// containing the event data.</param>
1120    /// <returns>bool: Whether or not the mouse down was handled by this
1121    /// entity.  The default here is false.  Sub-entities should override
1122    /// this to provide their own functionality if needed.</returns>
1123    // ------------------------------------------------------------------
1124    public virtual bool MouseDown(MouseEventArgs e) {
1125      this.RaiseOnMouseDown(new EntityMouseEventArgs(this, e));
1126
1127      // By default we're not handling the mouse down event here,
1128      // we're just passing it on.  Let the sub-entities handle it
1129      // by overriding this method.
1130      return false;
1131    }
1132
1133    // ------------------------------------------------------------------
1134    /// <summary>
1135    /// Implementation of the <see cref="IMouseListener"/>.  This is
1136    /// the method called when the mouse is moved while over this
1137    /// entity.
1138    /// </summary>
1139    /// <param name="e">The
1140    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1141    /// containing the event data.</param>
1142    // ------------------------------------------------------------------
1143    public virtual void MouseMove(MouseEventArgs e) {
1144      this.RaiseOnMouseMove(new EntityMouseEventArgs(this, e));
1145    }
1146
1147    // ------------------------------------------------------------------
1148    /// <summary>
1149    /// Implementation of the <see cref="IMouseListener"/>.  This is
1150    /// the method called when a mouse button is released while over this
1151    /// entity.
1152    /// </summary>
1153    /// <param name="e">The
1154    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1155    /// containing the event data.</param>
1156    // ------------------------------------------------------------------
1157    public virtual void MouseUp(MouseEventArgs e) {
1158      this.RaiseOnMouseUp(new EntityMouseEventArgs(this, e));
1159    }
1160
1161    #endregion
1162
1163    #region IHoverListener Members
1164
1165    // ------------------------------------------------------------------
1166    /// <summary>
1167    /// Implementation of the <see cref="IHoverListener"/>.  This is
1168    /// the method called when the mouse hovers over this entity.
1169    /// </summary>
1170    /// <param name="e">The
1171    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1172    /// containing the event data.</param>
1173    // ------------------------------------------------------------------
1174    public virtual void MouseHover(MouseEventArgs e) {
1175      this.RaiseOnMouseHover(new EntityMouseEventArgs(this, e));
1176    }
1177
1178    // ------------------------------------------------------------------
1179    /// <summary>
1180    /// Implementation of the <see cref="IHoverListener"/>.  This is
1181    /// the method called when the mouse enters this entity.
1182    /// </summary>
1183    /// <param name="e">The
1184    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1185    /// containing the event data.</param>
1186    // ------------------------------------------------------------------
1187    public virtual void MouseEnter(MouseEventArgs e) {
1188      this.RaiseOnMouseEnter(new EntityMouseEventArgs(this, e));
1189    }
1190
1191    // ------------------------------------------------------------------
1192    /// <summary>
1193    /// Implementation of the <see cref="IHoverListener"/>.  This is
1194    /// the method called when the mouse leaves this entity.
1195    /// </summary>
1196    /// <param name="e">The
1197    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1198    /// containing the event data.</param>
1199    // ------------------------------------------------------------------
1200    public virtual void MouseLeave(MouseEventArgs e) {
1201      this.RaiseOnMouseLeave(new EntityMouseEventArgs(this, e));
1202    }
1203
1204    #endregion
1205
1206    #region IKeyboardListener Members
1207
1208    // ------------------------------------------------------------------
1209    /// <summary>
1210    /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1211    /// the method called when a key is pressed down.
1212    /// </summary>
1213    /// <param name="e">The
1214    /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> instance
1215    /// containing the event data.</param>
1216    // ------------------------------------------------------------------
1217    public virtual void KeyDown(KeyEventArgs e) {
1218    }
1219
1220    // ------------------------------------------------------------------
1221    /// <summary>
1222    /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1223    /// the method called when a key is released.
1224    /// </summary>
1225    /// <param name="e">The
1226    /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> instance
1227    /// containing the event data.</param>
1228    // ------------------------------------------------------------------
1229    public virtual void KeyUp(KeyEventArgs e) {
1230    }
1231
1232    // ------------------------------------------------------------------
1233    /// <summary>
1234    /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1235    /// the method called when a key is pressed.
1236    /// </summary>
1237    /// <param name="e">The
1238    /// <see cref="T:System.Windows.Forms.KeyPressEventArgs"/> instance
1239    /// containing the event data.</param>
1240    // ------------------------------------------------------------------
1241    public virtual void KeyPress(KeyPressEventArgs e) {
1242    }
1243
1244    #endregion
1245  }
1246}
Note: See TracBrowser for help on using the repository browser.