Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9181 was 9181, checked in by mkommend, 11 years ago

#2006: Changed GhostFactory to not hold a reference to the View object and completely removed the TextEditor tool as it was not in use.

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    }
45
46    void Selection_OnNewSelection(object sender, EventArgs e) {
47      ShowTracker();
48    }
49
50    public override void Paint(Graphics g) {
51      base.Paint(g);
52      //Rectangle rectangle = WorkArea;
53      //g.SetClip(WorkArea);
54      g.Transform = ViewMatrix;
55      //draw the ghost and ants on top of the diagram
56      if (Ants != null)
57        Ants.Paint(g);
58      if (Ghost != null)
59        Ghost.Paint(g);
60      if (Tracker != null)
61        Tracker.Paint(g);
62
63      g.Transform.Reset();
64      //g.PageUnit = GraphicsUnit.Pixel;
65      //g.PageScale = 1.0F;
66    }
67
68    protected virtual void InvalidateGhostArea() {
69      if (Ghost != null) {
70        Rectangle area = Ghost.Rectangle;
71        // Inflate it a little so we make sure to get everything.
72        area.Inflate(20, 20);
73        this.Invalidate(area);
74      }
75    }
76
77    protected virtual void InvalidateTrackerArea() {
78      if (Tracker != null) {
79        Rectangle area = Tracker.Rectangle;
80        // Inflate it a little so we make sure to get everything.
81        area.Inflate(20, 20);
82        this.Invalidate(area);
83      }
84    }
85
86    protected virtual void InvalidateAntsArea() {
87      if (Ants != null) {
88        Rectangle area = Ants.Rectangle;
89        // Inflate it a little so we make sure to get everything.
90        area.Inflate(20, 20);
91        this.Invalidate(area);
92      }
93    }
94
95    public override void PaintGhostEllipse(
96        Point ltPoint,
97        Point rbPoint) {
98      // Refresh the old ghost area if needed.
99      this.InvalidateGhostArea();
100
101      Ghost = GhostsFactory.GetGhost(
102          new Point[] { ltPoint, rbPoint },
103              GhostTypes.Ellipse, this);
104    }
105    public override void PaintGhostRectangle(
106        Point ltPoint,
107        Point rbPoint) {
108      // Refresh the old ghost area if needed.
109      this.InvalidateGhostArea();
110
111      Ghost = GhostsFactory.GetGhost(
112          new Point[] { ltPoint, rbPoint },
113          GhostTypes.Rectangle, this);
114    }
115    public override void PaintAntsRectangle(
116        Point ltPoint,
117        Point rbPoint) {
118      // Refresh the old area if needed.
119      this.InvalidateAntsArea();
120      Ants = AntsFactory.GetAnts(
121          new Point[] { ltPoint, rbPoint },
122          AntTypes.Rectangle);
123    }
124    public override void PaintGhostLine(Point ltPoint, Point rbPoint) {
125      // Refresh the old ghost area if needed.
126      this.InvalidateGhostArea();
127
128      Ghost = GhostsFactory.GetGhost(
129          new Point[] { ltPoint, rbPoint },
130          GhostTypes.Line, this);
131    }
132    public override void PaintGhostLine(
133        MultiPointType curveType,
134        Point[] points) {
135      // Refresh the old ghost area if needed.
136      this.InvalidateGhostArea();
137
138      switch (curveType) {
139        case MultiPointType.Straight:
140          Ghost = GhostsFactory.GetGhost(points, GhostTypes.MultiLine, this);
141          break;
142        case MultiPointType.Polygon:
143          Ghost = GhostsFactory.GetGhost(points, GhostTypes.Polygon, this);
144          break;
145        case MultiPointType.Curve:
146          Ghost = GhostsFactory.GetGhost(points, GhostTypes.CurvedLine, this);
147          break;
148
149      }
150    }
151
152    public override void PaintTracker(Rectangle rectangle, bool showHandles) {
153      // Refresh the old area if needed.
154      this.InvalidateTrackerArea();
155      Tracker = TrackerFactory.GetTracker(rectangle, TrackerTypes.Default, showHandles);
156      rectangle.Inflate(20, 20);
157      this.Invalidate(rectangle);
158    }
159
160    #region Tracker
161
162    private enum TrackerTypes {
163      Default
164    }
165
166    private class TrackerFactory {
167
168      private static ITracker defTracker;
169
170      public static ITracker GetTracker(Rectangle rectangle, TrackerTypes type, bool showHandles) {
171        switch (type) {
172          case TrackerTypes.Default:
173            if (defTracker == null) defTracker = new DefaultTracker();
174            defTracker.Transform(rectangle);
175            defTracker.ShowHandles = showHandles;
176            return defTracker;
177          default:
178            return null;
179        }
180
181      }
182    }
183
184    private class DefaultTracker : TrackerBase {
185      private const int gripSize = 4;
186      private const int hitSize = 6;
187      float mx, my, sx, sy;
188
189      public DefaultTracker(Rectangle rectangle)
190        : base(rectangle) {
191      }
192
193      public DefaultTracker()
194        : base() { }
195
196      public override void Transform(Rectangle rectangle) {
197        this.Rectangle = rectangle;
198      }
199
200      public override void Paint(Graphics g) {
201        //the main rectangle
202        g.DrawRectangle(ArtPalette.TrackerPen, Rectangle);
203        #region Recalculate the size and location of the grips
204        mx = Rectangle.X + Rectangle.Width / 2;
205        my = Rectangle.Y + Rectangle.Height / 2;
206        sx = Rectangle.Width / 2;
207        sy = Rectangle.Height / 2;
208        #endregion
209        #region draw the grips
210        if (!ShowHandles) return;
211
212        for (int x = -1; x <= 1; x++) {
213          for (int y = -1; y <= 1; y++) {
214            if (x != 0 || y != 0) //not the middle one
215                        {
216              g.FillRectangle(ArtPalette.GripBrush, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
217              g.DrawRectangle(ArtPalette.BlackPen, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
218            }
219          }
220        }
221        #endregion
222      }
223
224      public override Point Hit(Point p) {
225        //no need to test if the handles are not shown
226        if (!ShowHandles) return Point.Empty;
227
228        for (int x = -1; x <= +1; x++)
229          for (int y = -1; y <= +1; y++)
230            if ((x != 0) || (y != 0)) {
231              if (new RectangleF(mx + x * sx - hitSize / 2, my + y * sy - hitSize / 2, hitSize, hitSize).Contains(p))
232                return new Point(x, y);
233            }
234        return Point.Empty;
235      }
236    }
237    #endregion
238  }
239}
Note: See TracBrowser for help on using the repository browser.