source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/HeuristicLab.Netron-3.0.2672.12446/View.cs @ 4068

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

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

File size: 7.5 KB
Line 
1#region License Information
2//This end-user license agreement applies to the following software;
3
4//The Netron Diagramming Library
5//Cobalt.IDE
6//Xeon webserver
7//Neon UI Library
8
9//Copyright (C) 2007, Francois M.Vanderseypen, The Netron Project & The Orbifold
10
11//This program is free software; you can redistribute it and/or
12//modify it under the terms of the GNU General Public License
13//as published by the Free Software Foundation; either version 2
14//of the License, or (at your option) any later version.
15
16//This program is distributed in the hope that it will be useful,
17//but WITHOUT ANY WARRANTY; without even the implied warranty of
18//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19//GNU General Public License for more details.
20
21//You should have received a copy of the GNU General Public License
22//along with this program; if not, write to the Free Software
23//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24
25
26//http://www.fsf.org/licensing/licenses/gpl.html
27
28//http://www.fsf.org/licensing/licenses/gpl-faq.html
29#endregion
30
31using System;
32using System.Drawing;
33using Netron.Diagramming.Core;
34
35namespace HeuristicLab.Netron {
36  public class View : ViewBase {
37
38    public View(IDiagramControl control)
39      : base(control) {
40      control.Document.Model.Selection.OnNewSelection += new EventHandler(Selection_OnNewSelection);
41      // this.Model.OnEntityAdded += new EventHandler<EntityEventArgs>(Model_OnEntityAdded);
42      this.HorizontalRuler.Visible = true;
43      this.VerticalRuler.Visible = true;
44      GhostsFactory.View = this;
45    }
46
47    void Selection_OnNewSelection(object sender, EventArgs e) {
48      ShowTracker();
49    }
50
51    public override void Paint(Graphics g) {
52      base.Paint(g);
53      //Rectangle rectangle = WorkArea;
54      //g.SetClip(WorkArea);
55      g.Transform = ViewMatrix;
56      //draw the ghost and ants on top of the diagram
57      if (Ants != null)
58        Ants.Paint(g);
59      if (Ghost != null)
60        Ghost.Paint(g);
61      if (Tracker != null)
62        Tracker.Paint(g);
63
64      g.Transform.Reset();
65      //g.PageUnit = GraphicsUnit.Pixel;
66      //g.PageScale = 1.0F;
67    }
68
69    protected virtual void InvalidateGhostArea() {
70      if (Ghost != null) {
71        Rectangle area = Ghost.Rectangle;
72        // Inflate it a little so we make sure to get everything.
73        area.Inflate(20, 20);
74        this.Invalidate(area);
75      }
76    }
77
78    protected virtual void InvalidateTrackerArea() {
79      if (Tracker != null) {
80        Rectangle area = Tracker.Rectangle;
81        // Inflate it a little so we make sure to get everything.
82        area.Inflate(20, 20);
83        this.Invalidate(area);
84      }
85    }
86
87    protected virtual void InvalidateAntsArea() {
88      if (Ants != null) {
89        Rectangle area = Ants.Rectangle;
90        // Inflate it a little so we make sure to get everything.
91        area.Inflate(20, 20);
92        this.Invalidate(area);
93      }
94    }
95
96    public override void PaintGhostEllipse(
97        Point ltPoint,
98        Point rbPoint) {
99      // Refresh the old ghost area if needed.
100      this.InvalidateGhostArea();
101
102      Ghost = GhostsFactory.GetGhost(
103          new Point[] { ltPoint, rbPoint },
104              GhostTypes.Ellipse);
105    }
106    public override void PaintGhostRectangle(
107        Point ltPoint,
108        Point rbPoint) {
109      // Refresh the old ghost area if needed.
110      this.InvalidateGhostArea();
111
112      Ghost = GhostsFactory.GetGhost(
113          new Point[] { ltPoint, rbPoint },
114          GhostTypes.Rectangle);
115    }
116    public override void PaintAntsRectangle(
117        Point ltPoint,
118        Point rbPoint) {
119      // Refresh the old area if needed.
120      this.InvalidateAntsArea();
121      Ants = AntsFactory.GetAnts(
122          new Point[] { ltPoint, rbPoint },
123          AntTypes.Rectangle);
124    }
125    public override void PaintGhostLine(Point ltPoint, Point rbPoint) {
126      // Refresh the old ghost area if needed.
127      this.InvalidateGhostArea();
128
129      Ghost = GhostsFactory.GetGhost(
130          new Point[] { ltPoint, rbPoint },
131          GhostTypes.Line);
132    }
133    public override void PaintGhostLine(
134        MultiPointType curveType,
135        Point[] points) {
136      // Refresh the old ghost area if needed.
137      this.InvalidateGhostArea();
138
139      switch (curveType) {
140        case MultiPointType.Straight:
141          Ghost = GhostsFactory.GetGhost(points, GhostTypes.MultiLine);
142          break;
143        case MultiPointType.Polygon:
144          Ghost = GhostsFactory.GetGhost(points, GhostTypes.Polygon);
145          break;
146        case MultiPointType.Curve:
147          Ghost = GhostsFactory.GetGhost(points, GhostTypes.CurvedLine);
148          break;
149
150      }
151    }
152
153    public override void PaintTracker(Rectangle rectangle, bool showHandles) {
154      // Refresh the old area if needed.
155      this.InvalidateTrackerArea();
156      Tracker = TrackerFactory.GetTracker(rectangle, TrackerTypes.Default, showHandles);
157      rectangle.Inflate(20, 20);
158      this.Invalidate(rectangle);
159    }
160
161    #region Tracker
162
163    private enum TrackerTypes {
164      Default
165    }
166
167    private class TrackerFactory {
168
169      private static ITracker defTracker;
170
171      public static ITracker GetTracker(Rectangle rectangle, TrackerTypes type, bool showHandles) {
172        switch (type) {
173          case TrackerTypes.Default:
174            if (defTracker == null) defTracker = new DefaultTracker();
175            defTracker.Transform(rectangle);
176            defTracker.ShowHandles = showHandles;
177            return defTracker;
178          default:
179            return null;
180        }
181
182      }
183    }
184
185    private class DefaultTracker : TrackerBase {
186      private const int gripSize = 4;
187      private const int hitSize = 6;
188      float mx, my, sx, sy;
189
190      public DefaultTracker(Rectangle rectangle)
191        : base(rectangle) {
192      }
193
194      public DefaultTracker()
195        : base() { }
196
197      public override void Transform(Rectangle rectangle) {
198        this.Rectangle = rectangle;
199      }
200
201      public override void Paint(Graphics g) {
202        //the main rectangle
203        g.DrawRectangle(ArtPalette.TrackerPen, Rectangle);
204        #region Recalculate the size and location of the grips
205        mx = Rectangle.X + Rectangle.Width / 2;
206        my = Rectangle.Y + Rectangle.Height / 2;
207        sx = Rectangle.Width / 2;
208        sy = Rectangle.Height / 2;
209        #endregion
210        #region draw the grips
211        if (!ShowHandles) return;
212
213        for (int x = -1; x <= 1; x++) {
214          for (int y = -1; y <= 1; y++) {
215            if (x != 0 || y != 0) //not the middle one
216                        {
217              g.FillRectangle(ArtPalette.GripBrush, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
218              g.DrawRectangle(ArtPalette.BlackPen, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
219            }
220          }
221        }
222        #endregion
223      }
224
225      public override Point Hit(Point p) {
226        //no need to test if the handles are not shown
227        if (!ShowHandles) return Point.Empty;
228
229        for (int x = -1; x <= +1; x++)
230          for (int y = -1; y <= +1; y++)
231            if ((x != 0) || (y != 0)) {
232              if (new RectangleF(mx + x * sx - hitSize / 2, my + y * sy - hitSize / 2, hitSize, hitSize).Contains(p))
233                return new Point(x, y);
234            }
235        return Point.Empty;
236      }
237    }
238    #endregion
239  }
240}
Note: See TracBrowser for help on using the repository browser.