1 | using System.Drawing;
|
---|
2 |
|
---|
3 | namespace Netron.Diagramming.Core {
|
---|
4 | // ----------------------------------------------------------------------
|
---|
5 | /// <summary>
|
---|
6 | /// The base class for zooming tools. Zoom tools allow the user to
|
---|
7 | /// incrementally adjust (increase or decrease) the View's magnification.
|
---|
8 | /// </summary>
|
---|
9 | // ----------------------------------------------------------------------
|
---|
10 | public class ZoomToolBase : AbstractTool {
|
---|
11 | // ------------------------------------------------------------------
|
---|
12 | /// <summary>
|
---|
13 | /// Used to multiply the current magnification by.
|
---|
14 | /// </summary>
|
---|
15 | // ------------------------------------------------------------------
|
---|
16 | protected float myZoomFactor = 0.9F;
|
---|
17 |
|
---|
18 | // ------------------------------------------------------------------
|
---|
19 | /// <summary>
|
---|
20 | /// Gets or sets the multiplier used to adjust the View's
|
---|
21 | /// Magnification by. The default is 0.9.
|
---|
22 | /// </summary>
|
---|
23 | // ------------------------------------------------------------------
|
---|
24 | public float ZoomFactor {
|
---|
25 | get {
|
---|
26 | return myZoomFactor;
|
---|
27 | }
|
---|
28 | set {
|
---|
29 | myZoomFactor = value;
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | // ------------------------------------------------------------------
|
---|
34 | /// <summary>
|
---|
35 | /// Constructor.
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="toolName">string: The name of the tool.</param>
|
---|
38 | // ------------------------------------------------------------------
|
---|
39 | public ZoomToolBase(string toolName)
|
---|
40 | : base(toolName) {
|
---|
41 | }
|
---|
42 |
|
---|
43 | // ------------------------------------------------------------------
|
---|
44 | /// <summary>
|
---|
45 | /// Activates this tool - the View's current magnification is
|
---|
46 | /// mulitiplied by 'ZoomFactor'.
|
---|
47 | /// </summary>
|
---|
48 | // ------------------------------------------------------------------
|
---|
49 | protected override void OnActivateTool() {
|
---|
50 | base.OnActivateTool();
|
---|
51 |
|
---|
52 | SizeF size = Controller.View.Magnification;
|
---|
53 | SizeF autoScrollMinSize =
|
---|
54 | Controller.ParentControl.AutoScrollMinSize;
|
---|
55 | Point origin = Controller.View.Origin;
|
---|
56 | Point parentAutoScrollPosition =
|
---|
57 | Controller.ParentControl.AutoScrollPosition;
|
---|
58 |
|
---|
59 | Controller.View.Magnification = new SizeF(
|
---|
60 | size.Width * myZoomFactor,
|
---|
61 | size.Height * myZoomFactor);
|
---|
62 |
|
---|
63 | // Remember to also adjust the diagram's scroll bars.
|
---|
64 | size = new SizeF(
|
---|
65 | autoScrollMinSize.Width * myZoomFactor,
|
---|
66 | autoScrollMinSize.Height * myZoomFactor);
|
---|
67 | Controller.ParentControl.AutoScrollMinSize = Size.Round(size);
|
---|
68 |
|
---|
69 | // Should we set the Origin to the location of the selected items
|
---|
70 | // if there are any? This will allow the user to zoom in on
|
---|
71 | // a selection.
|
---|
72 | if (this.Controller.Model.Selection.SelectedItems.Count > 0) {
|
---|
73 | Bundle bundle = new Bundle(this.Controller.Model.Selection.SelectedItems);
|
---|
74 | Point bundleLocation = bundle.Rectangle.Location;
|
---|
75 |
|
---|
76 | // Don't move the origin *exactly* to the bundle's location.
|
---|
77 | // Offset it a little so the bundle is butted-up with the
|
---|
78 | // upper-right hand corner of the screen.
|
---|
79 | bundleLocation.Offset(-20, -20);
|
---|
80 | origin.Offset(Point.Round(Controller.View.WorldToView(
|
---|
81 | bundleLocation)));
|
---|
82 |
|
---|
83 | Controller.View.Origin = origin;
|
---|
84 | }
|
---|
85 |
|
---|
86 | Controller.ParentControl.AutoScrollPosition =
|
---|
87 | Controller.View.Origin;
|
---|
88 |
|
---|
89 | DeactivateTool();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|