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