Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/22/10 10:54:33 (14 years ago)
Author:
mkommend
Message:

disabled scrolling via mouse wheel in netron visualization (ticket #867)

Location:
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/HeuristicLab.Netron-3.0.2672.12446/NetronVisualization.cs

    r2909 r3167  
    116116    }
    117117
     118
     119    protected override void OnMouseWheel(MouseEventArgs e) {
     120      //inserted to disable scrolling by the mousewheel
     121      //base.OnMouseWheel(e);
     122    }
     123
    118124    protected override void OnScroll(ScrollEventArgs se) {
    119125      //base.OnScroll(se);
    120126      if (se.ScrollOrientation == ScrollOrientation.HorizontalScroll) {
    121127        Origin = new Point(se.NewValue, Origin.Y);
    122         //System.Diagnostics.Trace.WriteLine(se.NewValue);
    123128      } else {
    124129        Origin = new Point(Origin.X, se.NewValue);
    125         //System.Diagnostics.Trace.WriteLine(se.NewValue);
    126130      }
    127131    }
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Core/ControllerBase.cs

    r2868 r3167  
    66using System.ComponentModel;
    77using System.Diagnostics;
    8 namespace Netron.Diagramming.Core
    9 {
    10     // ----------------------------------------------------------------------
    11     /// <summary>
    12     /// Abstract base implementation of the <see cref="IController"/>
    13     /// interface.
    14     /// </summary>
    15     // ----------------------------------------------------------------------
    16     public abstract class ControllerBase : IUndoSupport, IController
    17     {
    18         #region Events
    19 
    20         // ------------------------------------------------------------------
    21         /// <summary>
    22         /// Occurs when the context menu is shown.
    23         /// </summary>
    24         // ------------------------------------------------------------------
    25         public event EventHandler<EntityMenuEventArgs> OnShowContextMenu;
    26 
    27         // ------------------------------------------------------------------
    28         /// <summary>
    29         /// Occurs when a tool is asked to be deactivated.
    30         /// </summary>
    31         // ------------------------------------------------------------------
    32         public event EventHandler<ToolEventArgs> OnToolDeactivate;
    33 
    34         // ------------------------------------------------------------------
    35         /// <summary>
    36         /// Occurs when a tool is asked to be activated.
    37         /// </summary>
    38         // ------------------------------------------------------------------
    39         public event EventHandler<ToolEventArgs> OnToolActivate;
    40 
    41         // ------------------------------------------------------------------
    42         /// <summary>
    43         /// Occurs when the history has changed in the undo/redo mechanism.
    44         /// </summary>
    45         // ------------------------------------------------------------------
    46         public event EventHandler<HistoryChangeEventArgs> OnHistoryChange;
    47 
    48         // ------------------------------------------------------------------
    49         /// <summary>
    50         /// Occurs when the something got selected and the properties of it
    51         /// can/should be shown.
    52         /// </summary>
    53         // ------------------------------------------------------------------
    54         public event EventHandler<SelectionEventArgs> OnShowSelectionProperties;
    55 
    56         // ------------------------------------------------------------------
    57         /// <summary>
    58         /// Occurs when an entity is added.
    59         /// <remarks>This event usually is bubbled from one of the
    60         /// layers.</remarks>
    61         /// </summary>
    62         // ------------------------------------------------------------------
    63         public event EventHandler<EntityEventArgs> OnEntityAdded;
    64 
    65         // ------------------------------------------------------------------
    66         /// <summary>
    67         /// Occurs when an entity is removed.
    68         /// <remarks>This event usually is bubbled from one of the
    69         /// layers.</remarks>
    70         /// </summary>
    71         // ------------------------------------------------------------------
    72         public event EventHandler<EntityEventArgs> OnEntityRemoved;
    73 
    74         // ------------------------------------------------------------------
    75         /// <summary>
    76         /// Occurs when the controller receives a mouse-down notification of
    77         /// the surface. This event is raised before the
    78         /// event is broadcasted down to the tools.
    79         /// </summary>
    80         // ------------------------------------------------------------------
    81         public event EventHandler<MouseEventArgs> OnMouseDown;
    82 
    83         // ------------------------------------------------------------------
    84         /// <summary>
    85         /// Occurs when the Ambience has changed.
    86         /// </summary>
    87         // ------------------------------------------------------------------
    88         public event EventHandler<AmbienceEventArgs> OnAmbienceChanged;
    89        
    90        
    91         #endregion
    92 
    93         #region Tool Names
    94 
    95         public const string AlignBottomEdgesToolName =
    96             "Align Bottom Edges Tool";
    97 
    98         public const string AlignCentersHorizToolName =
    99             "Align Centers Horizontally";
    100 
    101         public const string AlignCentersVertToolName =
    102             "Align Centers Vertically";
    103 
    104         public const string AlignLeftEdgesToolName =
    105             "Align Left Edges Tool";
    106 
    107         public const string AlignRightEdgesToolName =
    108             "Align Right Edges Tool";
    109 
    110         public const string AlignTopEdgesToolName =
    111             "Align Top Edges Tool";
    112 
    113         public const string ComplexRectangleToolName = "ComplexRectangle Tool";
    114         public const string ConnectionToolName = "Connection Tool";
    115         public const string ConnectorMoverToolName = "Connector Mover Tool";
    116         public const string ContextToolName = "Context Tool";
    117         public const string CopyToolName = "Copy Tool";
    118         public const string CutToolName = "Cut Tool";
    119         public const string DeleteToolName = "Delete Tool";         
    120         public const string DragDropToolName = "DragDrop Tool";
    121         public const string EllipseToolName = "Ellipse Tool";
    122         public const string GroupToolName = "Group Tool";
    123         public const string HitToolName = "Hit Tool";
    124         public const string HoverToolName = "Hover Tool";
    125         public const string ImageExportToolName = "Image Export Tool";
    126         public const string MoveToolName = "Move Tool";
    127         public const string MultiLineToolName = "MultiLine Tool";
    128         public const string PanToolName = "Pan Tool";
    129         public const string PasteToolName = "Paste Tool";
    130         public const string PolygonToolName = "Polygon Tool";
    131         public const string RectangleToolName = "Rectangle Tool";
    132         public const string ScribbleToolName = "Scribble Tool";
    133         public const string SelectionToolName = "Selection Tool";
    134         public const string SendBackwardsToolName = "SendBackwards Tool";
    135         public const string SendForwardsToolName = "SendForwards Tool";
    136         public const string SendToBackToolName = "SendToBack Tool";
    137         public const string SendToFrontToolName = "SendToFront Tool";
    138         public const string TransformToolName = "Transform Tool";       
    139         public const string UngroupToolName = "Ungroup Tool";
    140         public const string ZoomAreaToolName = "Zoom Area Tool";
    141         public const string ZoomInToolName = "Zoom In Tool";
    142         public const string ZoomOutToolName = "Zoom Out Tool";       
    143 
    144         #endregion
    145 
    146         #region Fields
    147 
    148         private bool eventsEnabled = true;
    149         private bool controllerEnabled = true;
    150 
    151         private IModel mModel;
    152         private UndoManager mUndoManager;
    153         ITool activeTool;
    154 
    155         /// <summary>
    156         /// the View field
    157         /// </summary>
    158         private IView mView;
    159         private CollectionBase<IMouseListener> mouseListeners;
    160         private CollectionBase<IKeyboardListener> keyboardListeners;
    161         private CollectionBase<IDragDropListener> dragdropListeners;
    162         private IDiagramControl parentControl;
    163         private CollectionBase<ITool> registeredTools;
    164         private CollectionBase<IActivity> registeredActivity;
    165        
    166         #endregion
    167 
    168         #region Properties
    169 
    170         /// <summary>
    171         /// Gets or sets a value indicating whether this <see cref="T:ControllerBase"/> is enabled.
    172         /// </summary>
    173         /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    174         public bool Enabled
    175         {
    176             get
    177             {
    178                 return controllerEnabled;
    179             }
    180             set
    181             {
    182                 controllerEnabled = value;
    183             }
    184         }
    185 
    186         // ------------------------------------------------------------------
    187         /// <summary>
    188         /// Gets the currently active tool.  This can be 'null'!!!
    189         /// </summary>
    190         // ------------------------------------------------------------------
    191         public ITool ActiveTool
    192         {
    193             get
    194             {
    195                 return this.activeTool;
    196             }
    197         }
    198 
    199         /// <summary>
    200         /// Gets or sets the parent control.
    201         /// </summary>
    202         /// <value>The parent control.</value>
    203          public IDiagramControl ParentControl
    204         {
    205             get { return parentControl; }
    206             internal set { parentControl = value; }
    207         }
    208         /// <summary>
    209         /// Gets the registered tools.
    210         /// </summary>
    211         /// <value>The tools.</value>
    212         public CollectionBase<ITool> Tools
    213         {
    214             get { return registeredTools; }
    215         }
    216 
    217 
    218 
    219         /// <summary>
    220         /// Gets the undo manager.
    221         /// </summary>
    222         /// <value>The undo manager.</value>
    223         public  UndoManager UndoManager
    224         {
    225             get
    226             {
    227                 return mUndoManager;
    228             }
    229 
    230         }
    231 
    232         /// <summary>
    233         /// Gets or sets the model
    234         /// </summary>
    235         /// <value></value>
    236         public IModel Model
    237         {
    238             get
    239             {
    240                 return mModel;
    241             }
    242             set
    243             {
    244                 AttachToModel(value);
    245             }
    246         }
    247 
    248         /// <summary>
    249         /// Gets or sets the view.
    250         /// </summary>
    251         /// <value>The view.</value>
    252         public IView View
    253         {
    254             get
    255             {
    256                 return mView;
    257             }
    258             set
    259             {
    260                 AttachToView(value);
    261             }
    262         }
    263 
    264         /// <summary>
    265         /// Attaches to the given view.
    266         /// </summary>
    267         /// <param name="view">The view.</param>
    268         private void AttachToView(IView view)
    269         {
    270             if (view == null)
    271                 throw new ArgumentNullException();
    272 
    273             mView = view;
    274         }
    275         #endregion
    276 
    277         #region Constructor
    278 
    279         // ------------------------------------------------------------------
    280         /// <summary>
    281         /// Default constructor.
    282         /// </summary>
    283         // ------------------------------------------------------------------
    284         protected ControllerBase(IDiagramControl surface)
    285         {
    286             //doesn't work if you supply a null reference
    287             if (surface == null)
    288             {
    289                 throw new NullReferenceException(
    290                     "The diagram control assigned to the controller " +
    291                     "cannot be 'null'");
    292             }
    293            
    294             //create the undo/redo manager
    295             mUndoManager = new UndoManager(15);
    296             mUndoManager.OnHistoryChange += new EventHandler(
    297                 mUndoManager_OnHistoryChange);
    298 
    299             #region Instantiation of listeners
    300             mouseListeners = new CollectionBase<IMouseListener>();
    301             keyboardListeners = new CollectionBase<IKeyboardListener>();
    302             dragdropListeners = new CollectionBase<IDragDropListener>();
    303             #endregion
    304 
    305             //keep a reference to the parent control
    306             parentControl = surface;
    307 
    308             AttachToSurface(parentControl);
    309            
    310            
    311 
    312             //Initialize the colorscheme
    313             ArtPalette.Init();
    314 
    315             #region Tools: the registration order matters!
    316             /*
     8namespace Netron.Diagramming.Core {
     9  // ----------------------------------------------------------------------
     10  /// <summary>
     11  /// Abstract base implementation of the <see cref="IController"/>
     12  /// interface.
     13  /// </summary>
     14  // ----------------------------------------------------------------------
     15  public abstract class ControllerBase : IUndoSupport, IController {
     16    #region Events
     17
     18    // ------------------------------------------------------------------
     19    /// <summary>
     20    /// Occurs when the context menu is shown.
     21    /// </summary>
     22    // ------------------------------------------------------------------
     23    public event EventHandler<EntityMenuEventArgs> OnShowContextMenu;
     24
     25    // ------------------------------------------------------------------
     26    /// <summary>
     27    /// Occurs when a tool is asked to be deactivated.
     28    /// </summary>
     29    // ------------------------------------------------------------------
     30    public event EventHandler<ToolEventArgs> OnToolDeactivate;
     31
     32    // ------------------------------------------------------------------
     33    /// <summary>
     34    /// Occurs when a tool is asked to be activated.
     35    /// </summary>
     36    // ------------------------------------------------------------------
     37    public event EventHandler<ToolEventArgs> OnToolActivate;
     38
     39    // ------------------------------------------------------------------
     40    /// <summary>
     41    /// Occurs when the history has changed in the undo/redo mechanism.
     42    /// </summary>
     43    // ------------------------------------------------------------------
     44    public event EventHandler<HistoryChangeEventArgs> OnHistoryChange;
     45
     46    // ------------------------------------------------------------------
     47    /// <summary>
     48    /// Occurs when the something got selected and the properties of it
     49    /// can/should be shown.
     50    /// </summary>
     51    // ------------------------------------------------------------------
     52    public event EventHandler<SelectionEventArgs> OnShowSelectionProperties;
     53
     54    // ------------------------------------------------------------------
     55    /// <summary>
     56    /// Occurs when an entity is added.
     57    /// <remarks>This event usually is bubbled from one of the
     58    /// layers.</remarks>
     59    /// </summary>
     60    // ------------------------------------------------------------------
     61    public event EventHandler<EntityEventArgs> OnEntityAdded;
     62
     63    // ------------------------------------------------------------------
     64    /// <summary>
     65    /// Occurs when an entity is removed.
     66    /// <remarks>This event usually is bubbled from one of the
     67    /// layers.</remarks>
     68    /// </summary>
     69    // ------------------------------------------------------------------
     70    public event EventHandler<EntityEventArgs> OnEntityRemoved;
     71
     72    // ------------------------------------------------------------------
     73    /// <summary>
     74    /// Occurs when the controller receives a mouse-down notification of
     75    /// the surface. This event is raised before the
     76    /// event is broadcasted down to the tools.
     77    /// </summary>
     78    // ------------------------------------------------------------------
     79    public event EventHandler<MouseEventArgs> OnMouseDown;
     80
     81    // ------------------------------------------------------------------
     82    /// <summary>
     83    /// Occurs when the Ambience has changed.
     84    /// </summary>
     85    // ------------------------------------------------------------------
     86    public event EventHandler<AmbienceEventArgs> OnAmbienceChanged;
     87
     88
     89    #endregion
     90
     91    #region Tool Names
     92
     93    public const string AlignBottomEdgesToolName =
     94        "Align Bottom Edges Tool";
     95
     96    public const string AlignCentersHorizToolName =
     97        "Align Centers Horizontally";
     98
     99    public const string AlignCentersVertToolName =
     100        "Align Centers Vertically";
     101
     102    public const string AlignLeftEdgesToolName =
     103        "Align Left Edges Tool";
     104
     105    public const string AlignRightEdgesToolName =
     106        "Align Right Edges Tool";
     107
     108    public const string AlignTopEdgesToolName =
     109        "Align Top Edges Tool";
     110
     111    public const string ComplexRectangleToolName = "ComplexRectangle Tool";
     112    public const string ConnectionToolName = "Connection Tool";
     113    public const string ConnectorMoverToolName = "Connector Mover Tool";
     114    public const string ContextToolName = "Context Tool";
     115    public const string CopyToolName = "Copy Tool";
     116    public const string CutToolName = "Cut Tool";
     117    public const string DeleteToolName = "Delete Tool";
     118    public const string DragDropToolName = "DragDrop Tool";
     119    public const string EllipseToolName = "Ellipse Tool";
     120    public const string GroupToolName = "Group Tool";
     121    public const string HitToolName = "Hit Tool";
     122    public const string HoverToolName = "Hover Tool";
     123    public const string ImageExportToolName = "Image Export Tool";
     124    public const string MoveToolName = "Move Tool";
     125    public const string MultiLineToolName = "MultiLine Tool";
     126    public const string PanToolName = "Pan Tool";
     127    public const string PasteToolName = "Paste Tool";
     128    public const string PolygonToolName = "Polygon Tool";
     129    public const string RectangleToolName = "Rectangle Tool";
     130    public const string ScribbleToolName = "Scribble Tool";
     131    public const string SelectionToolName = "Selection Tool";
     132    public const string SendBackwardsToolName = "SendBackwards Tool";
     133    public const string SendForwardsToolName = "SendForwards Tool";
     134    public const string SendToBackToolName = "SendToBack Tool";
     135    public const string SendToFrontToolName = "SendToFront Tool";
     136    public const string TransformToolName = "Transform Tool";
     137    public const string UngroupToolName = "Ungroup Tool";
     138    public const string ZoomAreaToolName = "Zoom Area Tool";
     139    public const string ZoomInToolName = "Zoom In Tool";
     140    public const string ZoomOutToolName = "Zoom Out Tool";
     141
     142    #endregion
     143
     144    #region Fields
     145
     146    private bool eventsEnabled = true;
     147    private bool controllerEnabled = true;
     148
     149    private IModel mModel;
     150    private UndoManager mUndoManager;
     151    ITool activeTool;
     152
     153    /// <summary>
     154    /// the View field
     155    /// </summary>
     156    private IView mView;
     157    private CollectionBase<IMouseListener> mouseListeners;
     158    private CollectionBase<IKeyboardListener> keyboardListeners;
     159    private CollectionBase<IDragDropListener> dragdropListeners;
     160    private IDiagramControl parentControl;
     161    private CollectionBase<ITool> registeredTools;
     162    private CollectionBase<IActivity> registeredActivity;
     163
     164    #endregion
     165
     166    #region Properties
     167
     168    /// <summary>
     169    /// Gets or sets a value indicating whether this <see cref="T:ControllerBase"/> is enabled.
     170    /// </summary>
     171    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
     172    public bool Enabled {
     173      get {
     174        return controllerEnabled;
     175      }
     176      set {
     177        controllerEnabled = value;
     178      }
     179    }
     180
     181    // ------------------------------------------------------------------
     182    /// <summary>
     183    /// Gets the currently active tool.  This can be 'null'!!!
     184    /// </summary>
     185    // ------------------------------------------------------------------
     186    public ITool ActiveTool {
     187      get {
     188        return this.activeTool;
     189      }
     190    }
     191
     192    /// <summary>
     193    /// Gets or sets the parent control.
     194    /// </summary>
     195    /// <value>The parent control.</value>
     196    public IDiagramControl ParentControl {
     197      get { return parentControl; }
     198      internal set { parentControl = value; }
     199    }
     200    /// <summary>
     201    /// Gets the registered tools.
     202    /// </summary>
     203    /// <value>The tools.</value>
     204    public CollectionBase<ITool> Tools {
     205      get { return registeredTools; }
     206    }
     207
     208
     209
     210    /// <summary>
     211    /// Gets the undo manager.
     212    /// </summary>
     213    /// <value>The undo manager.</value>
     214    public UndoManager UndoManager {
     215      get {
     216        return mUndoManager;
     217      }
     218
     219    }
     220
     221    /// <summary>
     222    /// Gets or sets the model
     223    /// </summary>
     224    /// <value></value>
     225    public IModel Model {
     226      get {
     227        return mModel;
     228      }
     229      set {
     230        AttachToModel(value);
     231      }
     232    }
     233
     234    /// <summary>
     235    /// Gets or sets the view.
     236    /// </summary>
     237    /// <value>The view.</value>
     238    public IView View {
     239      get {
     240        return mView;
     241      }
     242      set {
     243        AttachToView(value);
     244      }
     245    }
     246
     247    /// <summary>
     248    /// Attaches to the given view.
     249    /// </summary>
     250    /// <param name="view">The view.</param>
     251    private void AttachToView(IView view) {
     252      if (view == null)
     253        throw new ArgumentNullException();
     254
     255      mView = view;
     256    }
     257    #endregion
     258
     259    #region Constructor
     260
     261    // ------------------------------------------------------------------
     262    /// <summary>
     263    /// Default constructor.
     264    /// </summary>
     265    // ------------------------------------------------------------------
     266    protected ControllerBase(IDiagramControl surface) {
     267      //doesn't work if you supply a null reference
     268      if (surface == null) {
     269        throw new NullReferenceException(
     270            "The diagram control assigned to the controller " +
     271            "cannot be 'null'");
     272      }
     273
     274      //create the undo/redo manager
     275      mUndoManager = new UndoManager(15);
     276      mUndoManager.OnHistoryChange += new EventHandler(
     277          mUndoManager_OnHistoryChange);
     278
     279      #region Instantiation of listeners
     280      mouseListeners = new CollectionBase<IMouseListener>();
     281      keyboardListeners = new CollectionBase<IKeyboardListener>();
     282      dragdropListeners = new CollectionBase<IDragDropListener>();
     283      #endregion
     284
     285      //keep a reference to the parent control
     286      parentControl = surface;
     287
     288      AttachToSurface(parentControl);
     289
     290
     291
     292      //Initialize the colorscheme
     293      ArtPalette.Init();
     294
     295      #region Tools: the registration order matters!
     296      /*
    317297             The order in in which the tools are added matters, at least
    318298             some of them.
     
    324304             */
    325305
    326             registeredTools = new CollectionBase<ITool>();
    327 
    328             this.AddTool(new TransformTool(TransformToolName));
    329 
    330             this.AddTool(new HitTool(HitToolName));
    331 
    332             this.AddTool(new MoveTool(MoveToolName));
    333 
    334             this.AddTool(new RectangleTool(RectangleToolName));
    335 
    336             this.AddTool(new ComplexRectangleTool(ComplexRectangleToolName));
    337 
    338             this.AddTool(new EllipseTool(EllipseToolName));
    339 
    340             this.AddTool(new SelectionTool(SelectionToolName));
    341 
    342             this.AddTool(new DragDropTool(DragDropToolName));
    343 
    344             this.AddTool(new ConnectionTool(ConnectionToolName));
    345 
    346             this.AddTool(new ConnectorMoverTool(ConnectorMoverToolName));
    347 
    348             this.AddTool( new GroupTool(GroupToolName));
    349 
    350             this.AddTool(new UngroupTool(UngroupToolName));
    351 
    352             this.AddTool(new SendToBackTool(SendToBackToolName));
    353 
    354             this.AddTool(new SendBackwardsTool(SendBackwardsToolName));
    355 
    356             this.AddTool(new SendForwardsTool(SendForwardsToolName));
    357 
    358             this.AddTool(new SendToFrontTool(SendToFrontToolName));         
    359 
    360             this.AddTool(new HoverTool(HoverToolName));           
    361 
    362             this.AddTool(new ContextTool(ContextToolName));
    363 
    364             this.AddTool(new CopyTool(CopyToolName));
    365 
    366             this.AddTool(new CutTool(CutToolName));
    367 
    368             this.AddTool(new PasteTool(PasteToolName));
    369 
    370             this.AddTool(new DeleteTool(DeleteToolName));
    371 
    372             this.AddTool(new ScribbleTool(ScribbleToolName));
    373 
    374             this.AddTool(new PolygonTool(PolygonToolName));
    375 
    376             this.AddTool(new MultiLineTool(MultiLineToolName));
    377 
    378             this.AddTool(new AlignBottomEdgesTool(AlignBottomEdgesToolName));
    379 
    380             this.AddTool(
    381                 new AlignCentersHorizontallyTool(AlignCentersHorizToolName));
    382 
    383             this.AddTool(
    384                 new AlignCentersVerticallyTool(AlignCentersVertToolName));
    385 
    386             this.AddTool( new AlignLeftEdgesTool(AlignLeftEdgesToolName));
    387 
    388             this.AddTool(new AlignRightEdgesTool(AlignRightEdgesToolName));
    389 
    390             this.AddTool(new AlignTopEdgesTool(AlignTopEdgesToolName));
    391 
    392             this.AddTool(new ZoomAreaTool(ZoomAreaToolName));
    393 
    394             this.AddTool(new ZoomInTool(ZoomInToolName));
    395 
    396             this.AddTool(new ZoomOutTool(ZoomOutToolName));
    397 
    398             this.AddTool(new PanTool(PanToolName));
    399 
    400             this.AddTool(new ImageExportTool(ImageExportToolName));
    401 
    402             #endregion
    403 
    404             #region Hotkeys
    405             HotKeys keys = new HotKeys(this);
    406             this.keyboardListeners.Add(keys);
    407             #endregion
    408 
    409             #region Activities
    410             // This is in a way a waste of memory; the layouts should not
    411             // necessarily be loaded before they are actually requested.
    412             // You could register only the (string) names instead.
    413             // But for just a few algorithms this is OK and the advantage
    414             // of this registration is that one can register actions from
    415             // outside the library, in the hosting form for example.
    416             registeredActivity = new CollectionBase<IActivity>();
    417             AddActivity(new RandomLayout(this));
    418             AddActivity(new FruchtermanReingoldLayout(this));
    419             AddActivity(new StandardTreeLayout(this));
    420             AddActivity(new RadialTreeLayout(this));
    421             AddActivity(new BalloonTreeLayout(this));
    422             AddActivity(new ForceDirectedLayout(this));
    423             #endregion
     306      registeredTools = new CollectionBase<ITool>();
     307
     308      this.AddTool(new TransformTool(TransformToolName));
     309
     310      this.AddTool(new HitTool(HitToolName));
     311
     312      this.AddTool(new MoveTool(MoveToolName));
     313
     314      this.AddTool(new RectangleTool(RectangleToolName));
     315
     316      this.AddTool(new ComplexRectangleTool(ComplexRectangleToolName));
     317
     318      this.AddTool(new EllipseTool(EllipseToolName));
     319
     320      this.AddTool(new SelectionTool(SelectionToolName));
     321
     322      this.AddTool(new DragDropTool(DragDropToolName));
     323
     324      this.AddTool(new ConnectionTool(ConnectionToolName));
     325
     326      this.AddTool(new ConnectorMoverTool(ConnectorMoverToolName));
     327
     328      this.AddTool(new GroupTool(GroupToolName));
     329
     330      this.AddTool(new UngroupTool(UngroupToolName));
     331
     332      this.AddTool(new SendToBackTool(SendToBackToolName));
     333
     334      this.AddTool(new SendBackwardsTool(SendBackwardsToolName));
     335
     336      this.AddTool(new SendForwardsTool(SendForwardsToolName));
     337
     338      this.AddTool(new SendToFrontTool(SendToFrontToolName));
     339
     340      this.AddTool(new HoverTool(HoverToolName));
     341
     342      this.AddTool(new ContextTool(ContextToolName));
     343
     344      this.AddTool(new CopyTool(CopyToolName));
     345
     346      this.AddTool(new CutTool(CutToolName));
     347
     348      this.AddTool(new PasteTool(PasteToolName));
     349
     350      this.AddTool(new DeleteTool(DeleteToolName));
     351
     352      this.AddTool(new ScribbleTool(ScribbleToolName));
     353
     354      this.AddTool(new PolygonTool(PolygonToolName));
     355
     356      this.AddTool(new MultiLineTool(MultiLineToolName));
     357
     358      this.AddTool(new AlignBottomEdgesTool(AlignBottomEdgesToolName));
     359
     360      this.AddTool(
     361          new AlignCentersHorizontallyTool(AlignCentersHorizToolName));
     362
     363      this.AddTool(
     364          new AlignCentersVerticallyTool(AlignCentersVertToolName));
     365
     366      this.AddTool(new AlignLeftEdgesTool(AlignLeftEdgesToolName));
     367
     368      this.AddTool(new AlignRightEdgesTool(AlignRightEdgesToolName));
     369
     370      this.AddTool(new AlignTopEdgesTool(AlignTopEdgesToolName));
     371
     372      this.AddTool(new ZoomAreaTool(ZoomAreaToolName));
     373
     374      this.AddTool(new ZoomInTool(ZoomInToolName));
     375
     376      this.AddTool(new ZoomOutTool(ZoomOutToolName));
     377
     378      this.AddTool(new PanTool(PanToolName));
     379
     380      this.AddTool(new ImageExportTool(ImageExportToolName));
     381
     382      #endregion
     383
     384      #region Hotkeys
     385      HotKeys keys = new HotKeys(this);
     386      this.keyboardListeners.Add(keys);
     387      #endregion
     388
     389      #region Activities
     390      // This is in a way a waste of memory; the layouts should not
     391      // necessarily be loaded before they are actually requested.
     392      // You could register only the (string) names instead.
     393      // But for just a few algorithms this is OK and the advantage
     394      // of this registration is that one can register actions from
     395      // outside the library, in the hosting form for example.
     396      registeredActivity = new CollectionBase<IActivity>();
     397      AddActivity(new RandomLayout(this));
     398      AddActivity(new FruchtermanReingoldLayout(this));
     399      AddActivity(new StandardTreeLayout(this));
     400      AddActivity(new RadialTreeLayout(this));
     401      AddActivity(new BalloonTreeLayout(this));
     402      AddActivity(new ForceDirectedLayout(this));
     403      #endregion
     404    }
     405
     406    #endregion
     407
     408    // ------------------------------------------------------------------
     409    /// <summary>
     410    /// Attaches the given model to the controller.
     411    /// </summary>
     412    /// <param name="model">IModel</param>
     413    // ------------------------------------------------------------------
     414    protected virtual void AttachToModel(IModel model) {
     415      if (model == null)
     416        throw new ArgumentNullException();
     417
     418      mModel = model;
     419      mModel.OnEntityAdded +=
     420          new EventHandler<EntityEventArgs>(mModel_OnEntityAdded);
     421      mModel.OnEntityRemoved +=
     422          new EventHandler<EntityEventArgs>(mModel_OnEntityRemoved);
     423      mModel.OnAmbienceChanged +=
     424          new EventHandler<AmbienceEventArgs>(mModel_OnAmbienceChanged);
     425    }
     426
     427    // ------------------------------------------------------------------
     428    /// <summary>
     429    /// Passes the OnAmbienceChanged event on.
     430    /// </summary>
     431    /// <param name="sender">object</param>
     432    /// <param name="e">AmbienceEventArgs</param>
     433    // ------------------------------------------------------------------
     434    void mModel_OnAmbienceChanged(
     435        object sender,
     436        AmbienceEventArgs e) {
     437      RaiseOnAmbienceChanged(e);
     438    }
     439
     440    // ------------------------------------------------------------------
     441    /// <summary>
     442    /// Passes the OnEntityRemoved event on.
     443    /// </summary>
     444    /// <param name="sender">object</param>
     445    /// <param name="e">EntityEventArgs</param>
     446    // ------------------------------------------------------------------
     447    void mModel_OnEntityRemoved(
     448        object sender,
     449        EntityEventArgs e) {
     450      RaiseOnEntityRemoved(e);
     451    }
     452
     453    // ------------------------------------------------------------------
     454    /// <summary>
     455    /// Passes the OnEntityAdded event on.
     456    /// </summary>
     457    /// <param name="sender">object</param>
     458    /// <param name="e">EntityEventArgs</param>
     459    // ------------------------------------------------------------------
     460    void mModel_OnEntityAdded(
     461        object sender,
     462        EntityEventArgs e) {
     463      RaiseOnEntityAdded(e);
     464    }
     465
     466    // ------------------------------------------------------------------
     467    /// <summary>
     468    /// Bubbles the OnHistoryChange event.
     469    /// </summary>
     470    /// <param name="sender">The source of the event.</param>
     471    /// <param name="e">The <see cref="T:System.EventArgs"/> instance
     472    /// containing the event data.</param>
     473    // ------------------------------------------------------------------
     474    void mUndoManager_OnHistoryChange(object sender, EventArgs e) {
     475      RaiseOnHistoryChange();
     476    }
     477
     478    #region Methods
     479
     480    // ------------------------------------------------------------------
     481    /// <summary>
     482    /// Activates the text editor for the given text provider.
     483    /// </summary>
     484    /// <param name="textProvider">ITextProvider</param>
     485    /// <returns>bool: True if sucessful, false if not.</returns>
     486    // ------------------------------------------------------------------
     487    public abstract bool ActivateTextEditor(ITextProvider textProvider);
     488
     489    // ------------------------------------------------------------------
     490    /// <summary>
     491    /// Changes the paint style of the selected entities.
     492    /// </summary>
     493    /// <param name="paintStyle">IPaintStyle</param>
     494    // ------------------------------------------------------------------
     495    public void ChangeStyle(IPaintStyle paintStyle) {
     496
     497      // Note that you need a copy of the selected item otherwise the
     498      // undo/redo will fail once the selection has changed
     499      FillStyleCommand cmd = new FillStyleCommand(
     500          this,
     501          this.Model.Selection.SelectedItems.Copy(),
     502          paintStyle);
     503
     504      this.UndoManager.AddUndoCommand(cmd);
     505      cmd.Redo();
     506    }
     507
     508    // ------------------------------------------------------------------
     509    /// <summary>
     510    /// Changes the pen style  of the selected entities.
     511    /// </summary>
     512    /// <param name="penStyle">The pen style.</param>
     513    // ------------------------------------------------------------------
     514    public void ChangeStyle(IPenStyle penStyle) {
     515      PenStyleCommand cmd = new PenStyleCommand(
     516          this,
     517          this.Model.Selection.SelectedItems.Copy(),
     518          penStyle);
     519
     520      this.UndoManager.AddUndoCommand(cmd);
     521
     522      cmd.Redo();
     523    }
     524
     525    #region Event Raisers
     526
     527    // ------------------------------------------------------------------
     528    /// <summary>
     529    /// Raises the OnShowContextMenu event
     530    /// </summary>
     531    /// <param name="e">EntityMenuEventArgs</param>
     532    // ------------------------------------------------------------------
     533    public virtual void RaiseOnShowContextMenu(EntityMenuEventArgs e) {
     534      EventHandler<EntityMenuEventArgs> handler = OnShowContextMenu;
     535      if (handler != null) {
     536        handler(this, e);
     537      }
     538    }
     539
     540    // ------------------------------------------------------------------
     541    /// <summary>
     542    /// Raises the OnHistory change.
     543    /// </summary>
     544    // ------------------------------------------------------------------
     545    public virtual void RaiseOnHistoryChange() {
     546      EventHandler<HistoryChangeEventArgs> handler = OnHistoryChange;
     547      if (handler != null) {
     548        handler(this, new HistoryChangeEventArgs(
     549            this.UndoManager.RedoText,
     550            this.UndoManager.UndoText));
     551      }
     552    }
     553
     554    // ------------------------------------------------------------------
     555    /// <summary>
     556    /// Raises the <see cref="OnToolDeactivate"/> event.
     557    /// </summary>
     558    /// <param name="e">ConnectionCollection event argument</param>
     559    // ------------------------------------------------------------------
     560    public virtual void RaiseOnToolDeactivate(ToolEventArgs e) {
     561      EventHandler<ToolEventArgs> handler = OnToolDeactivate;
     562      if (handler != null) {
     563        handler(this, e);
     564      }
     565    }
     566
     567    // ------------------------------------------------------------------
     568    /// <summary>
     569    /// Raises the <see cref="OnToolActivate"/> event
     570    /// </summary>
     571    /// <param name="e">ConnectionCollection event argument</param>
     572    // ------------------------------------------------------------------
     573    public virtual void RaiseOnToolActivate(ToolEventArgs e) {
     574      EventHandler<ToolEventArgs> handler = OnToolActivate;
     575      if (handler != null) {
     576        handler(this, e);
     577      }
     578    }
     579
     580    // ------------------------------------------------------------------
     581    /// <summary>
     582    /// Raises the OnShowSelectionProperties event.
     583    /// </summary>
     584    /// <param name="e">The
     585    /// <see cref="T:Netron.Diagramming.Core.SelectionEventArgs"/>
     586    /// instance containing the event data.</param>
     587    // ------------------------------------------------------------------
     588    public virtual void RaiseOnShowSelectionProperties(SelectionEventArgs e) {
     589      EventHandler<SelectionEventArgs> handler = OnShowSelectionProperties;
     590      if (handler != null) {
     591        handler(this, e);
     592      }
     593    }
     594
     595    // ------------------------------------------------------------------
     596    /// <summary>
     597    /// Raises the <see cref="OnMouseDown "/> event.
     598    /// </summary>
     599    /// <param name="e">The
     600    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
     601    /// containing the event data.</param>
     602    // ------------------------------------------------------------------
     603    protected virtual void RaiseOnMouseDown(MouseEventArgs e) {
     604      if (OnMouseDown != null)
     605        OnMouseDown(this, e);
     606    }
     607
     608    // ------------------------------------------------------------------
     609    /// <summary>
     610    /// Raises the <see cref="OnEntityAdded"/> event.
     611    /// </summary>
     612    /// <param name="e">The
     613    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
     614    /// containing the event data.</param>
     615    // ------------------------------------------------------------------
     616    protected virtual void RaiseOnEntityAdded(EntityEventArgs e) {
     617      EventHandler<EntityEventArgs> handler = OnEntityAdded;
     618      if (handler != null) {
     619        handler(this, e);
     620      }
     621    }
     622
     623    // ------------------------------------------------------------------
     624    /// <summary>
     625    /// Raises the <see cref="OnEntityRemoved"/> event.
     626    /// </summary>
     627    /// <param name="e">The
     628    /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
     629    /// containing the event data.</param>
     630    // ------------------------------------------------------------------
     631    protected virtual void RaiseOnEntityRemoved(EntityEventArgs e) {
     632      EventHandler<EntityEventArgs> handler = OnEntityRemoved;
     633      if (handler != null) {
     634        handler(this, e);
     635      }
     636    }
     637
     638    // ------------------------------------------------------------------
     639    /// <summary>
     640    /// Raises the <see cref="OnAmbienceChanged"/> event.
     641    /// </summary>
     642    /// <param name="e">AmbienceEventArgs</param>
     643    // ------------------------------------------------------------------
     644    protected virtual void RaiseOnAmbienceChanged(AmbienceEventArgs e) {
     645      EventHandler<AmbienceEventArgs> handler = OnAmbienceChanged;
     646      if (handler != null) {
     647        handler(this, e);
     648      }
     649    }
     650
     651    #endregion
     652
     653    #region Tool (de)activation methods
     654
     655    // ------------------------------------------------------------------
     656    /// <summary>
     657    /// Deactivates the given tool.
     658    /// </summary>
     659    /// <param name="tool">a registered ITool</param>
     660    /// <returns>bool: True if successful.</returns>
     661    // ------------------------------------------------------------------
     662    public bool DeactivateTool(ITool tool) {
     663      bool flag = false;
     664      if (tool != null && tool.Enabled && tool.IsActive) {
     665        //IEnumerator iEnumerator = tools.GetEnumerator();
     666        //Tool tool2 = null;
     667        //while (iEnumerator.MoveNext())
     668        //{
     669        //    tool2 = iEnumerator.Current is Tool;
     670        //    if (tool2 != null && tool2 != tool)
     671        //    {
     672        //        tool2.ToolDeactivating(tool);
     673        //    }
     674        //}
     675        flag = tool.DeactivateTool();
     676        if (flag && eventsEnabled) {
     677          RaiseOnToolDeactivate(new ToolEventArgs(tool));
    424678        }
     679      }
     680      return flag;
     681    }
     682
     683    // ------------------------------------------------------------------
     684    /// <summary>
     685    /// Deactivates all tools.
     686    /// </summary>
     687    /// <returns>bool: True if successful.</returns>
     688    // ------------------------------------------------------------------
     689    public bool DeactivateAllTools() {
     690      bool successful = true;
     691
     692      // If the deactivation of any tool returns false, then we will
     693      // return false.
     694      foreach (ITool tool in this.Tools) {
     695        successful = successful & this.DeactivateTool(tool);
     696      }
     697      this.activeTool = null;
     698      return successful;
     699    }
     700
     701    // ------------------------------------------------------------------
     702    /// <summary>
     703    /// Activates the tool with the given name
     704    /// </summary>
     705    /// <param name="toolName"></param>
     706    // ------------------------------------------------------------------
     707    public void ActivateTool(string toolName) {
     708      if (!controllerEnabled)
     709        return;
     710
     711      //using anonymous method here
     712      Predicate<ITool> predicate = delegate(ITool tool) {
     713        if (tool.Name.ToLower() == toolName.ToLower())//not case sensitive
     714          return true;
     715        else
     716          return false;
     717      };
     718
     719      // First deactivate the current tool.
     720      if (this.activeTool != null) {
     721        this.activeTool.DeactivateTool();
     722      }
     723      ITool foundTool = this.registeredTools.Find(predicate);
     724      ActivateTool(foundTool);
     725    }
     726
     727    // ------------------------------------------------------------------
     728    /// <summary>
     729    /// Suspends all tools
     730    /// </summary>
     731    // ------------------------------------------------------------------
     732    public void SuspendAllTools() {
     733      foreach (ITool tool in this.Tools) {
     734        tool.IsSuspended = true;
     735      }
     736    }
     737
     738    // ------------------------------------------------------------------
     739    /// <summary>
     740    /// Unsuspends all tools.
     741    /// </summary>
     742    // ------------------------------------------------------------------
     743    public void UnsuspendAllTools() {
     744      foreach (ITool tool in this.Tools) {
     745        tool.IsSuspended = false;
     746      }
     747    }
     748
     749    // ------------------------------------------------------------------
     750    /// <summary>
     751    /// Activates a registered tool
     752    /// </summary>
     753    /// <param name="tool">a registered ITool</param>
     754    /// <returns>bool: Returns if the activation was successful.  True is
     755    /// returned if it was, false if not.</returns>
     756    // ------------------------------------------------------------------
     757    private bool ActivateTool(ITool tool) {
     758      if (!controllerEnabled)
     759        return false;
     760      bool flag = false;
     761      if (tool != null && tool.CanActivate) {
     762        flag = tool.ActivateTool();
     763        this.activeTool = tool;
     764        if (flag && eventsEnabled) {
     765          RaiseOnToolActivate(new ToolEventArgs(tool));
     766        }
     767      }
     768      return flag;
     769    }
     770
     771    #endregion
     772
     773    // ------------------------------------------------------------------
     774    /// <summary>
     775    /// Adds the given tool.
     776    /// </summary>
     777    /// <param name="tool">The tool.</param>
     778    // ------------------------------------------------------------------
     779    public void AddTool(ITool tool) {
     780      tool.Controller = this;
     781      // Add the tool to the collection even if it doesn't attach to
     782      // anything (yet)
     783      registeredTools.Add(tool);
     784
     785      IMouseListener mouseTool = null;
     786      if ((mouseTool = tool as IMouseListener) != null)
     787        mouseListeners.Add(mouseTool);
     788
     789      IKeyboardListener keyboardTool = null;
     790      if ((keyboardTool = tool as IKeyboardListener) != null)
     791        keyboardListeners.Add(keyboardTool);
     792
     793      IDragDropListener dragdropTool = null;
     794      if ((dragdropTool = tool as IDragDropListener) != null)
     795        dragdropListeners.Add(dragdropTool);
     796
     797      // Watch when the tool is (de)activated so we can pass it on.
     798      tool.OnToolActivate +=
     799          new EventHandler<ToolEventArgs>(AddedTool_OnToolActivate);
     800
     801      tool.OnToolDeactivate +=
     802          new EventHandler<ToolEventArgs>(AddedTool_OnToolDeactivate);
     803    }
     804
     805    // ------------------------------------------------------------------
     806    /// <summary>
     807    /// Called when an added tool is deactivated.  The event is passed on
     808    /// by calling RaiseOnToolDeactivate.
     809    /// </summary>
     810    /// <param name="sender">object</param>
     811    /// <param name="e">ToolEventArgs</param>
     812    // ------------------------------------------------------------------
     813    void AddedTool_OnToolDeactivate(object sender, ToolEventArgs e) {
     814      ITool nextActiveToolInList = null;
     815      if (this.activeTool == e.Properties) {
     816        foreach (ITool tool in this.Tools) {
     817          if (tool.IsActive) {
     818            nextActiveToolInList = tool;
     819            break;
     820          }
     821        }
     822        activeTool = nextActiveToolInList;
     823      }
     824      this.RaiseOnToolDeactivate(e);
     825    }
     826
     827    // ------------------------------------------------------------------
     828    /// <summary>
     829    /// Called when an added tool is activated.  The event is passed on
     830    /// by calling RaiseOnToolActivate.
     831    /// </summary>
     832    /// <param name="sender">object</param>
     833    /// <param name="e">ToolEventArgs</param>
     834    // ------------------------------------------------------------------
     835    void AddedTool_OnToolActivate(object sender, ToolEventArgs e) {
     836      this.RaiseOnToolActivate(e);
     837    }
     838
     839    #region Activity
     840
     841    // ------------------------------------------------------------------
     842    /// <summary>
     843    /// Adds the given activity to the controller.
     844    /// </summary>
     845    /// <param name="activity">The activity.</param>
     846    // ------------------------------------------------------------------
     847    public void AddActivity(IActivity activity) {
     848
     849      registeredActivity.Add(activity);
     850    }
     851
     852    // ------------------------------------------------------------------
     853    /// <summary>
     854    /// Runs the given activity.
     855    /// </summary>
     856    /// <param name="activity">The activity.</param>
     857    // ------------------------------------------------------------------
     858    protected void RunActivity(IActivity activity) {
     859      if (activity == null) return;
     860      PrepareActivity(activity);
     861
     862      activity.Run();
     863
     864    }
     865
     866    // ------------------------------------------------------------------
     867    /// <summary>
     868    /// Runs the given activity.
     869    /// </summary>
     870    /// <param name="activity">The activity.</param>
     871    /// <param name="milliseconds">The milliseconds.</param>
     872    // ------------------------------------------------------------------
     873    protected void RunActivity(IActivity activity, int milliseconds) {
     874      if (activity == null) return;
     875      PrepareActivity(activity);
     876      activity.Run(milliseconds);
     877    }
     878
     879    // ------------------------------------------------------------------
     880    /// <summary>
     881    /// Prepares the activity.
     882    /// </summary>
     883    /// <param name="activity">The activity.</param>
     884    // ------------------------------------------------------------------
     885    private void PrepareActivity(IActivity activity) {
     886      if (activity is IAction)
     887        (activity as IAction).Model = this.Model;
     888      if (activity is ILayout) {
     889        (activity as ILayout).Bounds = parentControl.ClientRectangle;
     890        (activity as ILayout).Center = new PointF(parentControl.ClientRectangle.Width / 2, parentControl.ClientRectangle.Height / 2);
     891      }
     892    }
     893
     894    // ------------------------------------------------------------------
     895    /// <summary>
     896    /// Runs the activity with the name specified.  If no activity could
     897    /// by found with the given name then an exception is thrown.
     898    /// </summary>
     899    /// <param name="activityName">string: The name of the
     900    /// activity to run.</param>
     901    // ------------------------------------------------------------------
     902    public void RunActivity(string activityName) {
     903      if (!controllerEnabled)
     904        return;
     905
     906      this.View.CurrentCursor = Cursors.WaitCursor;
     907      controllerEnabled = false;
     908
     909      IActivity foundActivity = FindActivity(activityName);
     910      if (foundActivity != null) {
     911        RunActivity(foundActivity);
     912      }
     913
     914      controllerEnabled = true;
     915      this.View.CurrentCursor = Cursors.Default;
     916
     917      // After returning the canvas back to "normal", if the activity
     918      // wasn't found throw an exception (as specified by IController).
     919      if (foundActivity == null) {
     920        throw new Exception("Activity '" + activityName +
     921            "' could not be found.");
     922      }
     923    }
     924
     925    // ------------------------------------------------------------------
     926    /// <summary>
     927    /// Finds the activity with the given name.
     928    /// </summary>
     929    /// <param name="name">The name.</param>
     930    /// <returns>IActivity</returns>
     931    // ------------------------------------------------------------------
     932    protected IActivity FindActivity(string name) {
     933      //using anonymous method here
     934      Predicate<IActivity> predicate = delegate(IActivity activity) {
     935        if (activity.Name.ToLower() == name.ToLower())//not case sensitive
     936          return true;
     937        else
     938          return false;
     939      };
     940      return this.registeredActivity.Find(predicate);
     941
     942    }
     943
     944    // ------------------------------------------------------------------
     945    /// <summary>
     946    /// Runs the given activity for the specified time span.
     947    /// </summary>
     948    /// <param name="name">The name.</param>
     949    /// <param name="milliseconds">The milliseconds.</param>
     950    // ------------------------------------------------------------------
     951    public void RunActivity(string name, int milliseconds) {
     952      if (!controllerEnabled)
     953        return;
     954
     955      IActivity foundActivity = FindActivity(name);
     956      if (foundActivity != null)
     957        RunActivity(foundActivity, milliseconds);
     958    }
     959
     960    #endregion
     961
     962    // ------------------------------------------------------------------
     963    /// <summary>
     964    /// Attaches this controller to the surface.
     965    /// </summary>
     966    /// <param name="surface">The surface.</param>
     967    // ------------------------------------------------------------------
     968    protected virtual void AttachToSurface(IDiagramControl surface) {
     969      #region Mouse events
     970      surface.MouseDown += new MouseEventHandler(OnSurfaceMouseDown);
     971      surface.MouseUp += new MouseEventHandler(OnSurfaceMouseUp);
     972      surface.MouseMove += new MouseEventHandler(OnSurfaceMouseMove);
     973      surface.MouseHover += new EventHandler(OnSurfaceMouseHover);
     974      surface.MouseWheel += new MouseEventHandler(surface_MouseWheel);
     975      #endregion
     976
     977      #region Keyboard events
     978      surface.KeyDown += new KeyEventHandler(surface_KeyDown);
     979      surface.KeyUp += new KeyEventHandler(surface_KeyUp);
     980      surface.KeyPress += new KeyPressEventHandler(surface_KeyPress);
     981      #endregion
     982
     983      #region Dragdrop events
     984      surface.DragDrop += new DragEventHandler(surface_DragDrop);
     985      surface.DragEnter += new DragEventHandler(surface_DragEnter);
     986      surface.DragLeave += new EventHandler(surface_DragLeave);
     987      surface.DragOver += new DragEventHandler(surface_DragOver);
     988      surface.GiveFeedback +=
     989          new GiveFeedbackEventHandler(surface_GiveFeedback);
     990      #endregion
     991    }
     992
     993    // ------------------------------------------------------------------
     994    /// <summary>
     995    /// Handles the MouseWheel event of the surface control.
     996    /// <remarks>In the WinForm implementation this routine is not called
     997    /// because it gives some flickering effects; the hotkeys are
     998    /// implemented in the overriden OnMouseWheel method instead.</remarks>
     999    /// </summary>
     1000    /// <param name="sender">The source of the event.</param>
     1001    /// <param name="e">The
     1002    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
     1003    /// containing the event data.</param>
     1004    // ------------------------------------------------------------------
     1005    void surface_MouseWheel(object sender, MouseEventArgs e) {
     1006      Point p = View.Origin;
     1007      SizeF magnification = View.Magnification;
     1008      int newValue = 0;
     1009
     1010      if ((Control.ModifierKeys & Keys.Control) == Keys.Control) {
     1011        #region Zooming
     1012
     1013        SizeF s = magnification;
     1014
     1015        // If zooming in, e.Delta is < 0 so a value of 1.1 is used to
     1016        // offset the current magnification by.  One mouse wheel
     1017        // position on my PC corresponds to a delta of 120 (positive
     1018        // for zooming out, neg for zooming in).
     1019        float alpha = e.Delta > 0 ? 1.1F : 0.9F;
     1020        View.Magnification = new SizeF(
     1021            s.Width * alpha,
     1022            s.Height * alpha);
     1023
     1024        float w = (float)parentControl.AutoScrollPosition.X /
     1025            (float)parentControl.AutoScrollMinSize.Width;
     1026
     1027        float h = (float)parentControl.AutoScrollPosition.Y /
     1028            (float)parentControl.AutoScrollMinSize.Height;
     1029
     1030        // Resize the scrollbars proportionally to keep the actual
     1031        // canvas constant.
     1032        //s = new SizeF(
     1033        //    parentControl.AutoScrollMinSize.Width * alpha,
     1034        //    parentControl.AutoScrollMinSize.Height * alpha);
     1035
     1036        //parentControl.AutoScrollMinSize = Size.Round(s);
     1037        RectangleF pageBounds = Model.CurrentPage.Bounds;
     1038        pageBounds.Inflate(s);
     1039        SizeF deltaSize = new SizeF(
     1040            pageBounds.Width - parentControl.ClientRectangle.Width,
     1041            pageBounds.Height - parentControl.ClientRectangle.Height);
     1042
     1043        if ((deltaSize.Width > 0) && (deltaSize.Height > 0)) {
     1044          parentControl.AutoScrollMinSize = Size.Round(deltaSize);
     1045        }
     1046
     1047        //Point v = Origin;
     1048        //v.Offset(
     1049        //    Convert.ToInt32((alpha - 1) * v.X),
     1050        //    Convert.ToInt32((alpha - 1) * v.Y));
     1051        //v.X = (int)Math.Round((double)(v.X - alpha));
     1052        //v.Y = (int)Math.Round((double)(v.Y - alpha));
     1053        //Origin = v;
    4251054
    4261055        #endregion
    427 
    428         // ------------------------------------------------------------------
    429         /// <summary>
    430         /// Attaches the given model to the controller.
    431         /// </summary>
    432         /// <param name="model">IModel</param>
    433         // ------------------------------------------------------------------
    434         protected virtual void AttachToModel(IModel model)
    435         {
    436             if (model == null)
    437                 throw new ArgumentNullException();
    438 
    439             mModel = model;
    440             mModel.OnEntityAdded +=
    441                 new EventHandler<EntityEventArgs>(mModel_OnEntityAdded);
    442             mModel.OnEntityRemoved +=
    443                 new EventHandler<EntityEventArgs>(mModel_OnEntityRemoved);
    444             mModel.OnAmbienceChanged +=
    445                 new EventHandler<AmbienceEventArgs>(mModel_OnAmbienceChanged);
     1056      } else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) {
     1057        #region Pan horizontal
     1058        newValue = p.X - Math.Sign(e.Delta) * 20;
     1059        if (newValue > 0)
     1060          View.Origin = new Point(newValue, p.Y);
     1061        else
     1062          View.Origin = new Point(0, p.Y);
     1063
     1064        #endregion
     1065      } else {
     1066        #region Default vertical scroll
     1067        newValue = View.Origin.Y -
     1068            Math.Sign(e.Delta) * 20;
     1069
     1070        if (newValue > 0)
     1071          View.Origin = new Point(
     1072              View.Origin.X,
     1073              newValue);
     1074        else
     1075          View.Origin = new Point(
     1076              View.Origin.X,
     1077              0);
     1078        #endregion
     1079      }
     1080
     1081      this.parentControl.AutoScrollPosition = View.Origin;
     1082      HandledMouseEventArgs eventargs = e as HandledMouseEventArgs;
     1083      if (eventargs != null)
     1084        eventargs.Handled = true;
     1085      View.Invalidate();
     1086    }
     1087
     1088    #region DragDrop event handlers
     1089
     1090    /// <summary>
     1091    /// Handles the GiveFeedback event of the surface control.
     1092    /// </summary>
     1093    /// <param name="sender">The source of the event.</param>
     1094    /// <param name="e">The <see cref="T:System.Windows.Forms.GiveFeedbackEventArgs"/> instance containing the event data.</param>
     1095    void surface_GiveFeedback(object sender, GiveFeedbackEventArgs e) {
     1096      if (!controllerEnabled) {
     1097        return;
     1098      }
     1099
     1100      foreach (IDragDropListener listener in dragdropListeners) {
     1101        listener.GiveFeedback(e);
     1102      }
     1103    }
     1104    /// <summary>
     1105    /// Handles the DragOver event of the surface control.
     1106    /// </summary>
     1107    /// <param name="sender">The source of the event.</param>
     1108    /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
     1109    void surface_DragOver(object sender, DragEventArgs e) {
     1110      if (!controllerEnabled) {
     1111        return;
     1112      }
     1113
     1114      foreach (IDragDropListener listener in dragdropListeners) {
     1115        listener.OnDragOver(e);
     1116      }
     1117    }
     1118
     1119    /// <summary>
     1120    /// Handles the DragLeave event of the surface control.
     1121    /// </summary>
     1122    /// <param name="sender">The source of the event.</param>
     1123    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
     1124    void surface_DragLeave(object sender, EventArgs e) {
     1125      if (!controllerEnabled) {
     1126        return;
     1127      }
     1128
     1129      foreach (IDragDropListener listener in dragdropListeners) {
     1130        listener.OnDragLeave(e);
     1131      }
     1132    }
     1133
     1134    /// <summary>
     1135    /// Handles the DragEnter event of the surface control.
     1136    /// </summary>
     1137    /// <param name="sender">The source of the event.</param>
     1138    /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
     1139    void surface_DragEnter(object sender, DragEventArgs e) {
     1140      if (!controllerEnabled) {
     1141        return;
     1142      }
     1143
     1144      foreach (IDragDropListener listener in dragdropListeners) {
     1145        listener.OnDragEnter(e);
     1146      }
     1147    }
     1148
     1149    /// <summary>
     1150    /// Handles the DragDrop event of the surface control.
     1151    /// </summary>
     1152    /// <param name="sender">The source of the event.</param>
     1153    /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
     1154    void surface_DragDrop(object sender, DragEventArgs e) {
     1155      if (!controllerEnabled) {
     1156        return;
     1157      }
     1158
     1159      foreach (IDragDropListener listener in dragdropListeners) {
     1160        listener.OnDragDrop(e);
     1161      }
     1162    }
     1163    #endregion
     1164
     1165    #region Keyboard event handlers
     1166    void surface_KeyPress(object sender, KeyPressEventArgs e) {
     1167
     1168      foreach (IKeyboardListener listener in keyboardListeners) {
     1169        listener.KeyPress(e);
     1170      }
     1171
     1172      foreach (IDiagramEntity entity in this.Model.Selection.SelectedItems) {
     1173        if (entity is IKeyboardListener) {
     1174          (entity as IKeyboardListener).KeyPress(e);
    4461175        }
    447 
    448         // ------------------------------------------------------------------
    449         /// <summary>
    450         /// Passes the OnAmbienceChanged event on.
    451         /// </summary>
    452         /// <param name="sender">object</param>
    453         /// <param name="e">AmbienceEventArgs</param>
    454         // ------------------------------------------------------------------
    455         void mModel_OnAmbienceChanged(
    456             object sender,
    457             AmbienceEventArgs e)
    458         {
    459             RaiseOnAmbienceChanged(e);
     1176      }
     1177    }
     1178
     1179    void surface_KeyUp(object sender, KeyEventArgs e) {
     1180
     1181      foreach (IKeyboardListener listener in keyboardListeners) {
     1182        listener.KeyUp(e);
     1183      }
     1184
     1185      foreach (IDiagramEntity entity in this.Model.Selection.SelectedItems) {
     1186        if (entity is IKeyboardListener) {
     1187          (entity as IKeyboardListener).KeyUp(e);
    4601188        }
    461 
    462         // ------------------------------------------------------------------
    463         /// <summary>
    464         /// Passes the OnEntityRemoved event on.
    465         /// </summary>
    466         /// <param name="sender">object</param>
    467         /// <param name="e">EntityEventArgs</param>
    468         // ------------------------------------------------------------------
    469         void mModel_OnEntityRemoved(
    470             object sender,
    471             EntityEventArgs e)
    472         {
    473             RaiseOnEntityRemoved(e);
     1189      }
     1190    }
     1191
     1192    void surface_KeyDown(object sender, KeyEventArgs e) {
     1193      foreach (IKeyboardListener listener in keyboardListeners) {
     1194        listener.KeyDown(e);
     1195      }
     1196
     1197      foreach (IDiagramEntity entity in this.Model.Selection.SelectedItems) {
     1198        if (entity is IKeyboardListener) {
     1199          (entity as IKeyboardListener).KeyDown(e);
    4741200        }
    475 
    476         // ------------------------------------------------------------------
    477         /// <summary>
    478         /// Passes the OnEntityAdded event on.
    479         /// </summary>
    480         /// <param name="sender">object</param>
    481         /// <param name="e">EntityEventArgs</param>
    482         // ------------------------------------------------------------------
    483         void mModel_OnEntityAdded(
    484             object sender,
    485             EntityEventArgs e)
    486         {
    487             RaiseOnEntityAdded(e);
     1201      }
     1202    }
     1203
     1204    #endregion
     1205
     1206    #region Mouse event handlers
     1207    /// <summary>
     1208    /// Implements the observer pattern for the mouse hover event,
     1209    /// communicating the event to all listeners implementing the
     1210    /// necessary interface.
     1211    /// </summary>
     1212    /// <param name="sender">The source of the event.</param>
     1213    /// <param name="e">The <see cref="T:System.EventArgs"/> instance
     1214    /// containing the event data.</param>
     1215    protected virtual void OnSurfaceMouseHover(object sender, EventArgs e) {
     1216      //if (eventsEnabled)
     1217      //    RaiseOnMouseHover(e);
     1218      //if (!controllerEnabled)
     1219      //    return; 
     1220    }
     1221    /// <summary>
     1222    /// Implements the observer pattern for the mouse down event,
     1223    /// communicating the event to all listeners implementing the
     1224    /// necessary interface.
     1225    /// </summary>
     1226    /// <param name="sender">The source of the event.</param>
     1227    /// <param name="e">The
     1228    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
     1229    /// containing the event data.</param>
     1230    protected virtual void OnSurfaceMouseDown(
     1231        object sender,
     1232        MouseEventArgs e) {
     1233      #region Coordinates logic
     1234      // Get a point adjusted by the current scroll position and zoom factor           
     1235      Point p = Point.Round(
     1236          this.View.ViewToWorld(this.View.DeviceToView(e.Location)));
     1237
     1238      MouseEventArgs ce =
     1239          new MouseEventArgs(
     1240          e.Button,
     1241          e.Clicks,
     1242          p.X,
     1243          p.Y,
     1244          e.Delta);
     1245      #endregion
     1246
     1247      if (eventsEnabled)
     1248        RaiseOnMouseDown(ce);
     1249      if (!controllerEnabled)
     1250        return;
     1251      this.parentControl.Focus();
     1252
     1253      //(parentControl as Win.DiagramControl).toolTip.Show("Yihaaa", parentControl as Win.DiagramControl, ce.Location);
     1254
     1255      //this selection process will work independently of the tools because
     1256      //some tools need the current selection or hit entity
     1257      //On the other hand, when drawing a simple rectangle for example the selection
     1258      //should be off, so there is an overhead.
     1259      //Selection.CollectEntitiesAt(e.Location);
     1260
     1261      //raise the event to give the host the opportunity to show the properties of the selected item(s)
     1262      //Note that if the selection is empty the property grid will show 'nothing'.
     1263      RaiseOnShowSelectionProperties(new SelectionEventArgs(this.Model.Selection.SelectedItems.ToArray()));
     1264
     1265      foreach (IMouseListener listener in mouseListeners) {
     1266        if (listener.MouseDown(ce))
     1267          break;
     1268      }
     1269    }
     1270    /// <summary>
     1271    /// Handles the MouseMove event of the surface control.
     1272    /// </summary>
     1273    /// <param name="sender">The source of the event.</param>
     1274    /// <param name="e">The
     1275    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
     1276    /// containing the event data.</param>
     1277    protected virtual void OnSurfaceMouseMove(object sender, MouseEventArgs e) {
     1278      if (!controllerEnabled)
     1279        return;
     1280
     1281      #region Coordinates logic
     1282      // Get a point adjusted by the current scroll position and zoom
     1283      // factor.
     1284      //Point p = new Point(e.X - parentControl.AutoScrollPosition.X, e.Y - parentControl.AutoScrollPosition.Y);
     1285      Point p = Point.Round(this.View.ViewToWorld(
     1286          this.View.DeviceToView(e.Location)));
     1287
     1288      MouseEventArgs ce = new MouseEventArgs(
     1289          e.Button,
     1290          e.Clicks,
     1291          p.X,
     1292          p.Y,
     1293          e.Delta);
     1294
     1295      #endregion
     1296      foreach (IMouseListener listener in mouseListeners) {
     1297        listener.MouseMove(ce);
     1298      }
     1299    }
     1300
     1301    /// <summary>
     1302    /// Handles the MouseUp event of the surface control.
     1303    /// </summary>
     1304    /// <param name="sender">The source of the event.</param>
     1305    /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
     1306    protected virtual void OnSurfaceMouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
     1307      if (!controllerEnabled)
     1308        return;
     1309      #region Coordinates logic
     1310      // Get a point adjusted by the current scroll position and zoom factor
     1311      //Point p = new Point(e.X - parentControl.AutoScrollPosition.X, e.Y - parentControl.AutoScrollPosition.Y);
     1312      Point p = Point.Round(this.View.ViewToWorld(this.View.DeviceToView(e.Location)));
     1313      MouseEventArgs ce = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta);
     1314      #endregion
     1315      foreach (IMouseListener listener in mouseListeners) {
     1316        listener.MouseUp(ce);
     1317      }
     1318    }
     1319
     1320
     1321    #endregion
     1322
     1323    // ------------------------------------------------------------------
     1324    /// <summary>
     1325    /// Undo of the last action
     1326    /// </summary>
     1327    /// <remarks>Calling this on a class level will call the Undo method
     1328    /// of the last ICommand in the stack.</remarks>
     1329    // ------------------------------------------------------------------
     1330    public void Undo() {
     1331      // Reset the tracker or show the tracker after the undo operation
     1332      // since the undo does not take care of it
     1333      this.View.ResetTracker();
     1334      mUndoManager.Undo();
     1335      this.View.ShowTracker();
     1336      this.View.Invalidate();
     1337    }
     1338
     1339    // ------------------------------------------------------------------
     1340    /// <summary>
     1341    /// Performs the actual action or redo in case the actions was undone
     1342    /// before.
     1343    /// </summary>
     1344    /// <remarks>Calling this on a class level will call the Redo method
     1345    /// of the last ICommand in the stack.</remarks>
     1346    // ------------------------------------------------------------------
     1347    public void Redo() {
     1348      mUndoManager.Redo();
     1349      this.View.ShowTracker();
     1350    }
     1351
     1352    // ------------------------------------------------------------------
     1353    /// <summary>
     1354    /// Selects all entities on the current page.  The selection is
     1355    /// cleared first.
     1356    /// </summary>
     1357    // ------------------------------------------------------------------
     1358    public void SelectAll() {
     1359      this.View.ResetTracker();
     1360      this.Model.Selection.Clear();
     1361      this.Model.Selection.SelectedItems = this.Model.CurrentPage.Entities;
     1362      this.View.ShowTracker();
     1363      this.View.Invalidate();
     1364    }
     1365
     1366    // ------------------------------------------------------------------
     1367    /// <summary>
     1368    /// Navigates to the next page.  Nothing is performed if the last page
     1369    /// is currently selected and 'wrap' is false.  If 'wrap' is true,
     1370    /// then the first page is selected.
     1371    /// </summary>
     1372    /// <param name="wrap">bool: Specifies if the collection is wrapped
     1373    /// when the end is reached.</param>
     1374    // ------------------------------------------------------------------
     1375    public void GoForward(bool wrap) {
     1376      // We can't go anywhere if there's only one page!
     1377      if (Model.Pages.Count == 1) {
     1378        return;
     1379      }
     1380
     1381      int index = Model.Pages.IndexOf(Model.CurrentPage);
     1382
     1383      int newIndex = 0;  // The index of the page to select.
     1384
     1385      if (index >= (Model.Pages.Count - 1)) {
     1386        // The last page is currently active, so if 'wrap' is
     1387        // false then just return.
     1388        if (wrap == false) {
     1389          return;
    4881390        }
    4891391
    490         // ------------------------------------------------------------------
    491         /// <summary>
    492         /// Bubbles the OnHistoryChange event.
    493         /// </summary>
    494         /// <param name="sender">The source of the event.</param>
    495         /// <param name="e">The <see cref="T:System.EventArgs"/> instance
    496         /// containing the event data.</param>
    497         // ------------------------------------------------------------------
    498         void mUndoManager_OnHistoryChange(object sender, EventArgs e)
    499         {
    500             RaiseOnHistoryChange();
     1392        // Otherwise, if 'wrap' is true then we want the first page
     1393        // in the collection to be active.
     1394        newIndex = 0;
     1395      } else {
     1396        newIndex = index + 1;
     1397      }
     1398
     1399      DeactivateAllTools();
     1400      View.HideTracker();  // Just in case there are selected items.
     1401      Model.SetCurrentPage(Model.Pages[newIndex]);
     1402      View.Invalidate();
     1403    }
     1404
     1405    // ------------------------------------------------------------------
     1406    /// <summary>
     1407    /// Navigates to the previous page.  Nothing is performed if the first
     1408    /// page is currently selected and 'wrap' is false.  If 'wrap' is
     1409    /// true, then the last page is selected if the current page is the
     1410    /// first page.
     1411    /// </summary>
     1412    /// <param name="wrap">bool: Specifies if the collection is wrapped
     1413    /// when the start is reached.</param>
     1414    // ------------------------------------------------------------------
     1415    public void GoBack(bool wrap) {
     1416      // We can't go anywhere if there's only one page!
     1417      if (Model.Pages.Count == 1) {
     1418        return;
     1419      }
     1420
     1421      int index = Model.Pages.IndexOf(Model.CurrentPage);
     1422
     1423      int newIndex = 0;  // The index of the page to select.
     1424
     1425      if (index == 0) {
     1426        // The first page is currently active, so if 'wrap' is
     1427        // false then just return.
     1428        if (wrap == false) {
     1429          return;
    5011430        }
    5021431
    503         #region Methods
    504 
    505         // ------------------------------------------------------------------
    506         /// <summary>
    507         /// Activates the text editor for the given text provider.
    508         /// </summary>
    509         /// <param name="textProvider">ITextProvider</param>
    510         /// <returns>bool: True if sucessful, false if not.</returns>
    511         // ------------------------------------------------------------------
    512         public abstract bool ActivateTextEditor(ITextProvider textProvider);
    513 
    514         // ------------------------------------------------------------------
    515         /// <summary>
    516         /// Changes the paint style of the selected entities.
    517         /// </summary>
    518         /// <param name="paintStyle">IPaintStyle</param>
    519         // ------------------------------------------------------------------
    520         public void ChangeStyle(IPaintStyle paintStyle)
    521         {
    522 
    523             // Note that you need a copy of the selected item otherwise the
    524             // undo/redo will fail once the selection has changed
    525             FillStyleCommand cmd = new FillStyleCommand(
    526                 this,
    527                 this.Model.Selection.SelectedItems.Copy(),
    528                 paintStyle);           
    529 
    530             this.UndoManager.AddUndoCommand(cmd);
    531             cmd.Redo();
    532         }
    533 
    534         // ------------------------------------------------------------------
    535         /// <summary>
    536         /// Changes the pen style  of the selected entities.
    537         /// </summary>
    538         /// <param name="penStyle">The pen style.</param>
    539         // ------------------------------------------------------------------
    540         public void ChangeStyle(IPenStyle penStyle)
    541         {
    542             PenStyleCommand cmd = new PenStyleCommand(
    543                 this,
    544                 this.Model.Selection.SelectedItems.Copy(),
    545                 penStyle);
    546 
    547             this.UndoManager.AddUndoCommand(cmd);
    548 
    549             cmd.Redo();
    550         }
    551 
    552         #region Event Raisers
    553 
    554         // ------------------------------------------------------------------
    555         /// <summary>
    556         /// Raises the OnShowContextMenu event
    557         /// </summary>
    558         /// <param name="e">EntityMenuEventArgs</param>
    559         // ------------------------------------------------------------------
    560         public virtual void RaiseOnShowContextMenu(EntityMenuEventArgs e)
    561         {
    562             EventHandler<EntityMenuEventArgs> handler = OnShowContextMenu;
    563             if (handler != null)
    564             {
    565                 handler(this, e);
    566             }
    567         }
    568 
    569         // ------------------------------------------------------------------
    570         /// <summary>
    571         /// Raises the OnHistory change.
    572         /// </summary>
    573         // ------------------------------------------------------------------
    574         public virtual void RaiseOnHistoryChange()
    575         {
    576             EventHandler<HistoryChangeEventArgs> handler = OnHistoryChange;
    577             if (handler != null)
    578             {
    579                 handler(this, new HistoryChangeEventArgs(
    580                     this.UndoManager.RedoText,
    581                     this.UndoManager.UndoText));
    582             }
    583         }
    584 
    585         // ------------------------------------------------------------------
    586         /// <summary>
    587         /// Raises the <see cref="OnToolDeactivate"/> event.
    588         /// </summary>
    589         /// <param name="e">ConnectionCollection event argument</param>
    590         // ------------------------------------------------------------------
    591         public virtual void RaiseOnToolDeactivate(ToolEventArgs e)
    592         {
    593             EventHandler<ToolEventArgs> handler = OnToolDeactivate;
    594             if (handler != null)
    595             {
    596                 handler(this, e);
    597             }
    598         }
    599 
    600         // ------------------------------------------------------------------
    601         /// <summary>
    602         /// Raises the <see cref="OnToolActivate"/> event
    603         /// </summary>
    604         /// <param name="e">ConnectionCollection event argument</param>
    605         // ------------------------------------------------------------------
    606         public virtual void RaiseOnToolActivate(ToolEventArgs e)
    607         {
    608             EventHandler<ToolEventArgs> handler = OnToolActivate;
    609             if (handler != null)
    610             {
    611                 handler(this, e);
    612             }
    613         }
    614 
    615         // ------------------------------------------------------------------
    616         /// <summary>
    617         /// Raises the OnShowSelectionProperties event.
    618         /// </summary>
    619         /// <param name="e">The
    620         /// <see cref="T:Netron.Diagramming.Core.SelectionEventArgs"/>
    621         /// instance containing the event data.</param>
    622         // ------------------------------------------------------------------
    623         public virtual void RaiseOnShowSelectionProperties(SelectionEventArgs e)
    624         {
    625             EventHandler<SelectionEventArgs> handler = OnShowSelectionProperties;
    626             if(handler != null)
    627             {
    628                 handler(this, e);
    629             }
    630         }
    631 
    632         // ------------------------------------------------------------------
    633         /// <summary>
    634         /// Raises the <see cref="OnMouseDown "/> event.
    635         /// </summary>
    636         /// <param name="e">The
    637         /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
    638         /// containing the event data.</param>
    639         // ------------------------------------------------------------------
    640         protected virtual void RaiseOnMouseDown(MouseEventArgs e)
    641         {
    642             if(OnMouseDown != null)
    643                 OnMouseDown(this, e);
    644         }
    645 
    646         // ------------------------------------------------------------------
    647         /// <summary>
    648         /// Raises the <see cref="OnEntityAdded"/> event.
    649         /// </summary>
    650         /// <param name="e">The
    651         /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
    652         /// containing the event data.</param>
    653         // ------------------------------------------------------------------
    654         protected virtual void RaiseOnEntityAdded(EntityEventArgs e)
    655         {
    656             EventHandler<EntityEventArgs> handler = OnEntityAdded;
    657             if (handler != null)
    658             {
    659                 handler(this, e);
    660             }
    661         }
    662 
    663         // ------------------------------------------------------------------
    664         /// <summary>
    665         /// Raises the <see cref="OnEntityRemoved"/> event.
    666         /// </summary>
    667         /// <param name="e">The
    668         /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
    669         /// containing the event data.</param>
    670         // ------------------------------------------------------------------
    671         protected virtual void RaiseOnEntityRemoved(EntityEventArgs e)
    672         {
    673             EventHandler<EntityEventArgs> handler = OnEntityRemoved;
    674             if (handler != null)
    675             {
    676                 handler(this, e);
    677             }
    678         }
    679 
    680         // ------------------------------------------------------------------
    681         /// <summary>
    682         /// Raises the <see cref="OnAmbienceChanged"/> event.
    683         /// </summary>
    684         /// <param name="e">AmbienceEventArgs</param>
    685         // ------------------------------------------------------------------
    686         protected virtual void RaiseOnAmbienceChanged(AmbienceEventArgs e)
    687         {
    688             EventHandler<AmbienceEventArgs> handler = OnAmbienceChanged;
    689             if(handler != null)
    690             {
    691                 handler(this, e);
    692             }
    693         }
    694 
    695         #endregion
    696 
    697         #region Tool (de)activation methods
    698 
    699         // ------------------------------------------------------------------
    700         /// <summary>
    701         /// Deactivates the given tool.
    702         /// </summary>
    703         /// <param name="tool">a registered ITool</param>
    704         /// <returns>bool: True if successful.</returns>
    705         // ------------------------------------------------------------------
    706         public bool DeactivateTool(ITool tool)
    707         {
    708             bool flag = false;
    709             if (tool != null && tool.Enabled && tool.IsActive)
    710             {
    711                 //IEnumerator iEnumerator = tools.GetEnumerator();
    712                 //Tool tool2 = null;
    713                 //while (iEnumerator.MoveNext())
    714                 //{
    715                 //    tool2 = iEnumerator.Current is Tool;
    716                 //    if (tool2 != null && tool2 != tool)
    717                 //    {
    718                 //        tool2.ToolDeactivating(tool);
    719                 //    }
    720                 //}
    721                 flag = tool.DeactivateTool();
    722                 if (flag && eventsEnabled)
    723                 {
    724                     RaiseOnToolDeactivate(new ToolEventArgs(tool));
    725                 }
    726             }
    727             return flag;
    728         }
    729 
    730         // ------------------------------------------------------------------
    731         /// <summary>
    732         /// Deactivates all tools.
    733         /// </summary>
    734         /// <returns>bool: True if successful.</returns>
    735         // ------------------------------------------------------------------
    736         public bool DeactivateAllTools()
    737         {
    738             bool successful = true;
    739 
    740             // If the deactivation of any tool returns false, then we will
    741             // return false.
    742             foreach (ITool tool in this.Tools)
    743             {
    744                 successful = successful & this.DeactivateTool(tool);
    745             }
    746             this.activeTool = null;
    747             return successful;
    748         }
    749 
    750         // ------------------------------------------------------------------
    751         /// <summary>
    752         /// Activates the tool with the given name
    753         /// </summary>
    754         /// <param name="toolName"></param>
    755         // ------------------------------------------------------------------
    756         public void ActivateTool(string toolName)
    757         {
    758             if(!controllerEnabled)
    759                 return;
    760 
    761             //using anonymous method here
    762             Predicate<ITool> predicate = delegate(ITool tool)
    763             {
    764                 if (tool.Name.ToLower() == toolName.ToLower())//not case sensitive
    765                     return true;
    766                 else
    767                     return false;
    768             };
    769 
    770             // First deactivate the current tool.
    771             if (this.activeTool != null)
    772             {
    773                 this.activeTool.DeactivateTool();
    774             }
    775             ITool foundTool= this.registeredTools.Find(predicate);
    776             ActivateTool(foundTool);
    777         }
    778 
    779         // ------------------------------------------------------------------
    780         /// <summary>
    781         /// Suspends all tools
    782         /// </summary>
    783         // ------------------------------------------------------------------
    784         public void SuspendAllTools()
    785         {
    786             foreach(ITool tool in this.Tools)
    787             {
    788                 tool.IsSuspended = true;
    789             }
    790         }
    791 
    792         // ------------------------------------------------------------------
    793         /// <summary>
    794         /// Unsuspends all tools.
    795         /// </summary>
    796         // ------------------------------------------------------------------
    797         public void UnsuspendAllTools()
    798         {
    799             foreach(ITool tool in this.Tools)
    800             {
    801                 tool.IsSuspended = false;
    802             }
    803         }
    804 
    805         // ------------------------------------------------------------------
    806         /// <summary>
    807         /// Activates a registered tool
    808         /// </summary>
    809         /// <param name="tool">a registered ITool</param>
    810         /// <returns>bool: Returns if the activation was successful.  True is
    811         /// returned if it was, false if not.</returns>
    812         // ------------------------------------------------------------------
    813         private bool ActivateTool(ITool tool)
    814     {
    815             if(!controllerEnabled)
    816                 return false;
    817       bool flag = false;
    818       if (tool != null && tool.CanActivate)
    819       {                 
    820         flag = tool.ActivateTool();
    821                 this.activeTool = tool;
    822         if (flag && eventsEnabled )
    823         {
    824           RaiseOnToolActivate(new ToolEventArgs(tool));
    825         }
    826       }
    827       return flag;
    828     }
    829 
    830         #endregion
    831 
    832         // ------------------------------------------------------------------
    833         /// <summary>
    834         /// Adds the given tool.
    835         /// </summary>
    836         /// <param name="tool">The tool.</param>
    837         // ------------------------------------------------------------------
    838         public void AddTool(ITool tool)
    839         {
    840             tool.Controller = this;
    841             // Add the tool to the collection even if it doesn't attach to
    842             // anything (yet)
    843             registeredTools.Add(tool);
    844 
    845             IMouseListener mouseTool = null;
    846             if ((mouseTool = tool as IMouseListener) != null)
    847                 mouseListeners.Add(mouseTool);                   
    848 
    849             IKeyboardListener keyboardTool = null;
    850             if ((keyboardTool = tool as IKeyboardListener) != null)
    851                 keyboardListeners.Add(keyboardTool);
    852 
    853             IDragDropListener dragdropTool = null;
    854             if ((dragdropTool = tool as IDragDropListener) != null)
    855                 dragdropListeners.Add(dragdropTool);
    856 
    857             // Watch when the tool is (de)activated so we can pass it on.
    858             tool.OnToolActivate +=
    859                 new EventHandler<ToolEventArgs>(AddedTool_OnToolActivate);
    860 
    861             tool.OnToolDeactivate +=
    862                 new EventHandler<ToolEventArgs>(AddedTool_OnToolDeactivate);
    863         }
    864 
    865         // ------------------------------------------------------------------
    866         /// <summary>
    867         /// Called when an added tool is deactivated.  The event is passed on
    868         /// by calling RaiseOnToolDeactivate.
    869         /// </summary>
    870         /// <param name="sender">object</param>
    871         /// <param name="e">ToolEventArgs</param>
    872         // ------------------------------------------------------------------
    873         void AddedTool_OnToolDeactivate(object sender, ToolEventArgs e)
    874         {
    875             ITool nextActiveToolInList = null;
    876             if (this.activeTool == e.Properties)
    877             {
    878                 foreach (ITool tool in this.Tools)
    879                 {
    880                     if (tool.IsActive)
    881                     {
    882                         nextActiveToolInList = tool;
    883                         break;
    884                     }
    885                 }
    886                 activeTool = nextActiveToolInList;
    887             }
    888             this.RaiseOnToolDeactivate(e);
    889         }
    890 
    891         // ------------------------------------------------------------------
    892         /// <summary>
    893         /// Called when an added tool is activated.  The event is passed on
    894         /// by calling RaiseOnToolActivate.
    895         /// </summary>
    896         /// <param name="sender">object</param>
    897         /// <param name="e">ToolEventArgs</param>
    898         // ------------------------------------------------------------------
    899         void AddedTool_OnToolActivate(object sender, ToolEventArgs e)
    900         {
    901             this.RaiseOnToolActivate(e);
    902         }
    903 
    904         #region Activity
    905 
    906         // ------------------------------------------------------------------
    907         /// <summary>
    908         /// Adds the given activity to the controller.
    909         /// </summary>
    910         /// <param name="activity">The activity.</param>
    911         // ------------------------------------------------------------------
    912         public void AddActivity(IActivity activity)
    913         {
    914            
    915             registeredActivity.Add(activity);
    916         }
    917 
    918         // ------------------------------------------------------------------
    919         /// <summary>
    920         /// Runs the given activity.
    921         /// </summary>
    922         /// <param name="activity">The activity.</param>
    923         // ------------------------------------------------------------------
    924         protected void RunActivity(IActivity activity)
    925         {
    926             if (activity == null) return;
    927             PrepareActivity(activity);
    928            
    929             activity.Run();
    930            
    931         }
    932 
    933         // ------------------------------------------------------------------
    934         /// <summary>
    935         /// Runs the given activity.
    936         /// </summary>
    937         /// <param name="activity">The activity.</param>
    938         /// <param name="milliseconds">The milliseconds.</param>
    939         // ------------------------------------------------------------------
    940         protected void RunActivity(IActivity activity, int milliseconds)
    941         {
    942             if (activity == null) return;
    943             PrepareActivity(activity);
    944             activity.Run(milliseconds);
    945         }
    946 
    947         // ------------------------------------------------------------------
    948         /// <summary>
    949         /// Prepares the activity.
    950         /// </summary>
    951         /// <param name="activity">The activity.</param>
    952         // ------------------------------------------------------------------
    953         private void PrepareActivity(IActivity activity)
    954         {
    955             if (activity is IAction)
    956                 (activity as IAction).Model = this.Model;
    957             if (activity is ILayout)
    958             {
    959                 (activity as ILayout).Bounds = parentControl.ClientRectangle;
    960                 (activity as ILayout).Center = new PointF(parentControl.ClientRectangle.Width / 2, parentControl.ClientRectangle.Height / 2);
    961             }
    962         }
    963 
    964         // ------------------------------------------------------------------
    965         /// <summary>
    966         /// Runs the activity with the name specified.  If no activity could
    967         /// by found with the given name then an exception is thrown.
    968         /// </summary>
    969         /// <param name="activityName">string: The name of the
    970         /// activity to run.</param>
    971         // ------------------------------------------------------------------
    972         public void RunActivity(string activityName)
    973         {
    974             if (!controllerEnabled)
    975                 return;
    976 
    977             this.View.CurrentCursor = Cursors.WaitCursor;
    978             controllerEnabled = false;
    979            
    980             IActivity foundActivity = FindActivity(activityName);
    981             if (foundActivity != null)
    982             {
    983                 RunActivity(foundActivity);
    984             }
    985 
    986             controllerEnabled = true;
    987             this.View.CurrentCursor = Cursors.Default;
    988 
    989             // After returning the canvas back to "normal", if the activity
    990             // wasn't found throw an exception (as specified by IController).
    991             if (foundActivity == null)
    992             {
    993                 throw new Exception("Activity '" + activityName +
    994                     "' could not be found.");
    995             }
    996         }
    997 
    998         // ------------------------------------------------------------------
    999         /// <summary>
    1000         /// Finds the activity with the given name.
    1001         /// </summary>
    1002         /// <param name="name">The name.</param>
    1003         /// <returns>IActivity</returns>
    1004         // ------------------------------------------------------------------
    1005         protected IActivity FindActivity(string name)
    1006         {
    1007             //using anonymous method here
    1008             Predicate<IActivity> predicate = delegate(IActivity activity)
    1009             {
    1010                 if (activity.Name.ToLower() == name.ToLower())//not case sensitive
    1011                     return true;
    1012                 else
    1013                     return false;
    1014             };
    1015             return this.registeredActivity.Find(predicate);
    1016 
    1017         }
    1018 
    1019         // ------------------------------------------------------------------
    1020         /// <summary>
    1021         /// Runs the given activity for the specified time span.
    1022         /// </summary>
    1023         /// <param name="name">The name.</param>
    1024         /// <param name="milliseconds">The milliseconds.</param>
    1025         // ------------------------------------------------------------------
    1026         public void RunActivity(string name, int milliseconds)
    1027         {
    1028             if (!controllerEnabled)
    1029                 return;
    1030 
    1031             IActivity foundActivity = FindActivity(name);
    1032             if (foundActivity != null)
    1033                 RunActivity(foundActivity, milliseconds);
    1034         }
    1035 
    1036         #endregion
    1037 
    1038         // ------------------------------------------------------------------
    1039         /// <summary>
    1040         /// Attaches this controller to the surface.
    1041         /// </summary>
    1042         /// <param name="surface">The surface.</param>
    1043         // ------------------------------------------------------------------
    1044         protected virtual void AttachToSurface(IDiagramControl surface)
    1045         {
    1046             #region Mouse events
    1047             surface.MouseDown += new MouseEventHandler(OnSurfaceMouseDown);
    1048             surface.MouseUp += new MouseEventHandler(OnSurfaceMouseUp);
    1049             surface.MouseMove += new MouseEventHandler(OnSurfaceMouseMove);
    1050             surface.MouseHover += new EventHandler(OnSurfaceMouseHover);
    1051             surface.MouseWheel += new MouseEventHandler(surface_MouseWheel);
    1052             #endregion
    1053 
    1054             #region Keyboard events
    1055             surface.KeyDown += new KeyEventHandler(surface_KeyDown);
    1056             surface.KeyUp += new KeyEventHandler(surface_KeyUp);
    1057             surface.KeyPress += new KeyPressEventHandler(surface_KeyPress);
    1058             #endregion
    1059 
    1060             #region Dragdrop events
    1061             surface.DragDrop += new DragEventHandler(surface_DragDrop);
    1062             surface.DragEnter += new DragEventHandler(surface_DragEnter);
    1063             surface.DragLeave += new EventHandler(surface_DragLeave);
    1064             surface.DragOver += new DragEventHandler(surface_DragOver);
    1065             surface.GiveFeedback +=
    1066                 new GiveFeedbackEventHandler(surface_GiveFeedback);
    1067             #endregion
    1068         }
    1069 
    1070         // ------------------------------------------------------------------
    1071         /// <summary>
    1072         /// Handles the MouseWheel event of the surface control.
    1073         /// <remarks>In the WinForm implementation this routine is not called
    1074         /// because it gives some flickering effects; the hotkeys are
    1075         /// implemented in the overriden OnMouseWheel method instead.</remarks>
    1076         /// </summary>
    1077         /// <param name="sender">The source of the event.</param>
    1078         /// <param name="e">The
    1079         /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
    1080         /// containing the event data.</param>
    1081         // ------------------------------------------------------------------
    1082         void surface_MouseWheel(object sender, MouseEventArgs e)
    1083         {
    1084             Point p = View.Origin;
    1085             SizeF magnification = View.Magnification;
    1086             int newValue = 0;
    1087 
    1088 
    1089             if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
    1090             {
    1091                 #region Zooming
    1092 
    1093                 SizeF s = magnification;
    1094 
    1095                 // If zooming in, e.Delta is < 0 so a value of 1.1 is used to
    1096                 // offset the current magnification by.  One mouse wheel
    1097                 // position on my PC corresponds to a delta of 120 (positive
    1098                 // for zooming out, neg for zooming in).
    1099                 float alpha = e.Delta > 0 ? 1.1F : 0.9F;
    1100                 View.Magnification = new SizeF(
    1101                     s.Width * alpha,
    1102                     s.Height * alpha);
    1103 
    1104                 float w = (float)parentControl.AutoScrollPosition.X /
    1105                     (float)parentControl.AutoScrollMinSize.Width;
    1106 
    1107                 float h = (float)parentControl.AutoScrollPosition.Y /
    1108                     (float)parentControl.AutoScrollMinSize.Height;
    1109 
    1110                 // Resize the scrollbars proportionally to keep the actual
    1111                 // canvas constant.
    1112                 //s = new SizeF(
    1113                 //    parentControl.AutoScrollMinSize.Width * alpha,
    1114                 //    parentControl.AutoScrollMinSize.Height * alpha);
    1115 
    1116                 //parentControl.AutoScrollMinSize = Size.Round(s);
    1117                 RectangleF pageBounds = Model.CurrentPage.Bounds;
    1118                 pageBounds.Inflate(s);
    1119                 SizeF deltaSize = new SizeF(
    1120                     pageBounds.Width - parentControl.ClientRectangle.Width,
    1121                     pageBounds.Height - parentControl.ClientRectangle.Height);
    1122 
    1123                 if ((deltaSize.Width > 0) && (deltaSize.Height > 0))
    1124                 {
    1125                     parentControl.AutoScrollMinSize = Size.Round(deltaSize);
    1126                 }
    1127 
    1128                 //Point v = Origin;
    1129                 //v.Offset(
    1130                 //    Convert.ToInt32((alpha - 1) * v.X),
    1131                 //    Convert.ToInt32((alpha - 1) * v.Y));
    1132                 //v.X = (int)Math.Round((double)(v.X - alpha));
    1133                 //v.Y = (int)Math.Round((double)(v.Y - alpha));
    1134                 //Origin = v;
    1135 
    1136                 #endregion
    1137             }
    1138             else if ( (Control.ModifierKeys & Keys.Shift) == Keys.Shift)
    1139                
    1140             {
    1141                 #region Pan horizontal
    1142                 newValue = p.X - Math.Sign(e.Delta) * 20;
    1143                 if (newValue > 0)
    1144                     View.Origin = new Point(newValue, p.Y);
    1145                 else
    1146                     View.Origin = new Point(
    1147                         0,
    1148                         View.Origin.Y);
    1149 
    1150                 #endregion
    1151             }
    1152             else
    1153             {
    1154                 #region Default vertical scroll
    1155                 newValue = View.Origin.Y -
    1156                     Math.Sign(e.Delta) * 20;
    1157 
    1158                 if (newValue > 0)
    1159                     View.Origin = new Point(
    1160                         View.Origin.X,
    1161                         newValue);
    1162                 else
    1163                     View.Origin = new Point(
    1164                         View.Origin.X,
    1165                         0);
    1166                 #endregion
    1167             }
    1168 
    1169             this.parentControl.AutoScrollPosition = View.Origin;
    1170             //View.Invalidate();
    1171              
    1172         }       
    1173      
    1174         #region DragDrop event handlers
    1175 
    1176         /// <summary>
    1177         /// Handles the GiveFeedback event of the surface control.
    1178         /// </summary>
    1179         /// <param name="sender">The source of the event.</param>
    1180         /// <param name="e">The <see cref="T:System.Windows.Forms.GiveFeedbackEventArgs"/> instance containing the event data.</param>
    1181         void surface_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    1182         {
    1183             if (!controllerEnabled)
    1184             {
    1185                 return;
    1186             }
    1187 
    1188             foreach (IDragDropListener listener in dragdropListeners)
    1189             {
    1190                 listener.GiveFeedback(e);
    1191             }           
    1192         }
    1193         /// <summary>
    1194         /// Handles the DragOver event of the surface control.
    1195         /// </summary>
    1196         /// <param name="sender">The source of the event.</param>
    1197         /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
    1198         void surface_DragOver(object sender, DragEventArgs e)
    1199         {
    1200             if (!controllerEnabled)
    1201             {
    1202                 return;
    1203             }
    1204 
    1205             foreach (IDragDropListener listener in dragdropListeners)
    1206             {
    1207                 listener.OnDragOver(e);
    1208             }           
    1209         }
    1210 
    1211         /// <summary>
    1212         /// Handles the DragLeave event of the surface control.
    1213         /// </summary>
    1214         /// <param name="sender">The source of the event.</param>
    1215         /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    1216         void surface_DragLeave(object sender, EventArgs e)
    1217         {
    1218             if (!controllerEnabled)
    1219             {
    1220                 return;
    1221             }
    1222 
    1223             foreach (IDragDropListener listener in dragdropListeners)
    1224             {
    1225                 listener.OnDragLeave(e);
    1226             }           
    1227         }
    1228 
    1229         /// <summary>
    1230         /// Handles the DragEnter event of the surface control.
    1231         /// </summary>
    1232         /// <param name="sender">The source of the event.</param>
    1233         /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
    1234         void surface_DragEnter(object sender, DragEventArgs e)
    1235         {
    1236             if (!controllerEnabled)
    1237             {
    1238                 return;
    1239             }
    1240 
    1241             foreach (IDragDropListener listener in dragdropListeners)
    1242             {
    1243                 listener.OnDragEnter(e);
    1244             }           
    1245         }
    1246 
    1247         /// <summary>
    1248         /// Handles the DragDrop event of the surface control.
    1249         /// </summary>
    1250         /// <param name="sender">The source of the event.</param>
    1251         /// <param name="e">The <see cref="T:System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
    1252         void surface_DragDrop(object sender, DragEventArgs e)
    1253         {
    1254             if (!controllerEnabled)
    1255             {
    1256                 return;
    1257             }
    1258 
    1259             foreach (IDragDropListener listener in dragdropListeners)
    1260             {
    1261                 listener.OnDragDrop(e);
    1262             }           
    1263         }
    1264         #endregion
    1265 
    1266         #region Keyboard event handlers
    1267         void surface_KeyPress(object sender, KeyPressEventArgs e)
    1268         {
    1269            
    1270             foreach (IKeyboardListener listener in keyboardListeners)
    1271             {
    1272                 listener.KeyPress(e);
    1273             }
    1274 
    1275             foreach (IDiagramEntity entity in this.Model.Selection.SelectedItems)
    1276             {
    1277                 if (entity is IKeyboardListener)
    1278                 {
    1279                     (entity as IKeyboardListener).KeyPress(e);
    1280                 }
    1281             }
    1282         }
    1283 
    1284         void surface_KeyUp(object sender, KeyEventArgs e)
    1285         {
    1286            
    1287             foreach (IKeyboardListener listener in keyboardListeners)
    1288             {
    1289                 listener.KeyUp(e);
    1290             }
    1291 
    1292             foreach (IDiagramEntity entity in  this.Model.Selection.SelectedItems)
    1293             {
    1294                 if (entity is IKeyboardListener)
    1295                 {
    1296                     (entity as IKeyboardListener).KeyUp(e);
    1297                 }
    1298             }
    1299         }
    1300 
    1301         void surface_KeyDown(object sender, KeyEventArgs e)
    1302         {
    1303              foreach (IKeyboardListener listener in keyboardListeners)
    1304             {
    1305                 listener.KeyDown(e);
    1306             }
    1307 
    1308              foreach (IDiagramEntity entity in this.Model.Selection.SelectedItems)
    1309             {
    1310                 if (entity is IKeyboardListener)
    1311                 {
    1312                     (entity as IKeyboardListener).KeyDown(e);
    1313                 }
    1314             }
    1315         }
    1316        
    1317         #endregion
    1318 
    1319         #region Mouse event handlers
    1320         /// <summary>
    1321         /// Implements the observer pattern for the mouse hover event,
    1322         /// communicating the event to all listeners implementing the
    1323         /// necessary interface.
    1324         /// </summary>
    1325         /// <param name="sender">The source of the event.</param>
    1326         /// <param name="e">The <see cref="T:System.EventArgs"/> instance
    1327         /// containing the event data.</param>
    1328         protected virtual void OnSurfaceMouseHover(object sender, EventArgs e)
    1329         {
    1330             //if (eventsEnabled)
    1331             //    RaiseOnMouseHover(e);
    1332             //if (!controllerEnabled)
    1333             //    return; 
    1334         }
    1335         /// <summary>
    1336         /// Implements the observer pattern for the mouse down event,
    1337         /// communicating the event to all listeners implementing the
    1338         /// necessary interface.
    1339         /// </summary>
    1340         /// <param name="sender">The source of the event.</param>
    1341         /// <param name="e">The
    1342         /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
    1343         /// containing the event data.</param>
    1344         protected virtual void OnSurfaceMouseDown(
    1345             object sender,
    1346             MouseEventArgs e)
    1347         {
    1348             #region Coordinates logic
    1349             // Get a point adjusted by the current scroll position and zoom factor           
    1350             Point p = Point.Round(
    1351                 this.View.ViewToWorld(this.View.DeviceToView(e.Location)));
    1352 
    1353             MouseEventArgs ce =
    1354                 new MouseEventArgs(
    1355                 e.Button,
    1356                 e.Clicks,
    1357                 p.X,
    1358                 p.Y,
    1359                 e.Delta);           
    1360             #endregion
    1361 
    1362             if (eventsEnabled)
    1363                 RaiseOnMouseDown(ce);
    1364             if(!controllerEnabled)
    1365                 return;           
    1366             this.parentControl.Focus();
    1367 
    1368             //(parentControl as Win.DiagramControl).toolTip.Show("Yihaaa", parentControl as Win.DiagramControl, ce.Location);
    1369 
    1370             //this selection process will work independently of the tools because
    1371             //some tools need the current selection or hit entity
    1372             //On the other hand, when drawing a simple rectangle for example the selection
    1373             //should be off, so there is an overhead.
    1374             //Selection.CollectEntitiesAt(e.Location);
    1375 
    1376             //raise the event to give the host the opportunity to show the properties of the selected item(s)
    1377             //Note that if the selection is empty the property grid will show 'nothing'.
    1378             RaiseOnShowSelectionProperties(new SelectionEventArgs(this.Model.Selection.SelectedItems.ToArray()));
    1379 
    1380             foreach(IMouseListener listener in mouseListeners)
    1381             {
    1382                 if (listener.MouseDown(ce))
    1383                     break;
    1384             }
    1385         }
    1386         /// <summary>
    1387         /// Handles the MouseMove event of the surface control.
    1388         /// </summary>
    1389         /// <param name="sender">The source of the event.</param>
    1390         /// <param name="e">The
    1391         /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
    1392         /// containing the event data.</param>
    1393         protected virtual void OnSurfaceMouseMove(object sender, MouseEventArgs e)
    1394         {
    1395             if(!controllerEnabled)
    1396                 return;
    1397 
    1398             #region Coordinates logic
    1399             // Get a point adjusted by the current scroll position and zoom
    1400             // factor.
    1401             //Point p = new Point(e.X - parentControl.AutoScrollPosition.X, e.Y - parentControl.AutoScrollPosition.Y);
    1402             Point p = Point.Round(this.View.ViewToWorld(
    1403                 this.View.DeviceToView(e.Location)));
    1404 
    1405             MouseEventArgs ce = new MouseEventArgs(
    1406                 e.Button,
    1407                 e.Clicks,
    1408                 p.X,
    1409                 p.Y,
    1410                 e.Delta);
    1411                    
    1412             #endregion
    1413             foreach (IMouseListener listener in mouseListeners)
    1414             {
    1415                 listener.MouseMove(ce);
    1416             }
    1417         }
    1418 
    1419         /// <summary>
    1420         /// Handles the MouseUp event of the surface control.
    1421         /// </summary>
    1422         /// <param name="sender">The source of the event.</param>
    1423         /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
    1424         protected virtual void OnSurfaceMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    1425         {
    1426             if(!controllerEnabled)
    1427                 return;
    1428             #region Coordinates logic
    1429             // Get a point adjusted by the current scroll position and zoom factor
    1430             //Point p = new Point(e.X - parentControl.AutoScrollPosition.X, e.Y - parentControl.AutoScrollPosition.Y);
    1431             Point p = Point.Round(this.View.ViewToWorld(this.View.DeviceToView(e.Location)));
    1432             MouseEventArgs ce = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta);
    1433             #endregion
    1434             foreach (IMouseListener listener in mouseListeners)
    1435             {
    1436                 listener.MouseUp(ce);
    1437             }
    1438         }
    1439 
    1440        
    1441         #endregion
    1442 
    1443         // ------------------------------------------------------------------
    1444         /// <summary>
    1445         /// Undo of the last action
    1446         /// </summary>
    1447         /// <remarks>Calling this on a class level will call the Undo method
    1448         /// of the last ICommand in the stack.</remarks>
    1449         // ------------------------------------------------------------------
    1450         public void Undo()
    1451         {
    1452             // Reset the tracker or show the tracker after the undo operation
    1453             // since the undo does not take care of it
    1454             this.View.ResetTracker();
    1455             mUndoManager.Undo();
    1456             this.View.ShowTracker();
    1457             this.View.Invalidate();
    1458         }
    1459 
    1460         // ------------------------------------------------------------------
    1461         /// <summary>
    1462         /// Performs the actual action or redo in case the actions was undone
    1463         /// before.
    1464         /// </summary>
    1465         /// <remarks>Calling this on a class level will call the Redo method
    1466         /// of the last ICommand in the stack.</remarks>
    1467         // ------------------------------------------------------------------
    1468         public void Redo()
    1469         {
    1470             mUndoManager.Redo();
    1471             this.View.ShowTracker();
    1472         }
    1473 
    1474         // ------------------------------------------------------------------
    1475         /// <summary>
    1476         /// Selects all entities on the current page.  The selection is
    1477         /// cleared first.
    1478         /// </summary>
    1479         // ------------------------------------------------------------------
    1480         public void SelectAll()
    1481         {
    1482             this.View.ResetTracker();
    1483             this.Model.Selection.Clear();
    1484             this.Model.Selection.SelectedItems = this.Model.CurrentPage.Entities;
    1485             this.View.ShowTracker();
    1486             this.View.Invalidate();
    1487         }
    1488 
    1489         // ------------------------------------------------------------------
    1490         /// <summary>
    1491         /// Navigates to the next page.  Nothing is performed if the last page
    1492         /// is currently selected and 'wrap' is false.  If 'wrap' is true,
    1493         /// then the first page is selected.
    1494         /// </summary>
    1495         /// <param name="wrap">bool: Specifies if the collection is wrapped
    1496         /// when the end is reached.</param>
    1497         // ------------------------------------------------------------------
    1498         public void GoForward(bool wrap)
    1499         {
    1500             // We can't go anywhere if there's only one page!
    1501             if (Model.Pages.Count == 1)
    1502             {
    1503                 return;
    1504             }
    1505 
    1506             int index = Model.Pages.IndexOf(Model.CurrentPage);
    1507 
    1508             int newIndex = 0;  // The index of the page to select.
    1509 
    1510             if (index >= (Model.Pages.Count - 1))
    1511             {
    1512                 // The last page is currently active, so if 'wrap' is
    1513                 // false then just return.
    1514                 if (wrap == false)
    1515                 {
    1516                     return;
    1517                 }
    1518 
    1519                 // Otherwise, if 'wrap' is true then we want the first page
    1520                 // in the collection to be active.
    1521                 newIndex = 0;
    1522             }
    1523             else
    1524             {
    1525                 newIndex = index + 1;
    1526             }
    1527 
    1528             DeactivateAllTools();
    1529             View.HideTracker();  // Just in case there are selected items.
    1530             Model.SetCurrentPage(Model.Pages[newIndex]);
    1531             View.Invalidate();
    1532         }
    1533 
    1534         // ------------------------------------------------------------------
    1535         /// <summary>
    1536         /// Navigates to the previous page.  Nothing is performed if the first
    1537         /// page is currently selected and 'wrap' is false.  If 'wrap' is
    1538         /// true, then the last page is selected if the current page is the
    1539         /// first page.
    1540         /// </summary>
    1541         /// <param name="wrap">bool: Specifies if the collection is wrapped
    1542         /// when the start is reached.</param>
    1543         // ------------------------------------------------------------------
    1544         public void GoBack(bool wrap)
    1545         {
    1546             // We can't go anywhere if there's only one page!
    1547             if (Model.Pages.Count == 1)
    1548             {
    1549                 return;
    1550             }
    1551 
    1552             int index = Model.Pages.IndexOf(Model.CurrentPage);
    1553 
    1554             int newIndex = 0;  // The index of the page to select.
    1555 
    1556             if (index == 0)
    1557             {
    1558                 // The first page is currently active, so if 'wrap' is
    1559                 // false then just return.
    1560                 if (wrap == false)
    1561                 {
    1562                     return;
    1563                 }
    1564 
    1565                 // Otherwise, since 'wrap' is true then we want the last page
    1566                 // in the collection to be active.
    1567                 newIndex = Model.Pages.Count - 1;
    1568             }
    1569             else
    1570             {
    1571                 newIndex = index - 1;
    1572             }
    1573 
    1574             DeactivateAllTools();
    1575             View.HideTracker();  // Just in case there are selected items.
    1576             Model.SetCurrentPage(Model.Pages[newIndex]);
    1577             View.Invalidate();
    1578         }
    1579         #endregion
    1580              
    1581     }
     1432        // Otherwise, since 'wrap' is true then we want the last page
     1433        // in the collection to be active.
     1434        newIndex = Model.Pages.Count - 1;
     1435      } else {
     1436        newIndex = index - 1;
     1437      }
     1438
     1439      DeactivateAllTools();
     1440      View.HideTracker();  // Just in case there are selected items.
     1441      Model.SetCurrentPage(Model.Pages[newIndex]);
     1442      View.Invalidate();
     1443    }
     1444    #endregion
     1445
     1446  }
    15821447}
Note: See TracChangeset for help on using the changeset viewer.