-
Notifications
You must be signed in to change notification settings - Fork 10
/
CanvasViewportControl.cs
144 lines (131 loc) · 5.5 KB
/
CanvasViewportControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace GhCanvasViewport
{
class CanvasViewportControl : RhinoWindows.Forms.Controls.ViewportControl
{
public CanvasViewportControl()
{
// stupid hack to get GH to draw preview geometry in this control
this.Viewport.Name = "GH_HACK";
}
System.Drawing.Point RightMouseDownLocation { get; set; }
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
RightMouseDownLocation = e.Location;
else
RightMouseDownLocation = System.Drawing.Point.Empty;
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var vec = new Rhino.Geometry.Vector2d(e.X - RightMouseDownLocation.X, e.Y - RightMouseDownLocation.Y);
if (vec.Length > 10)
RightMouseDownLocation = System.Drawing.Point.Empty;
}
else
{
RightMouseDownLocation = System.Drawing.Point.Empty;
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Right)
{
var vec = new Rhino.Geometry.Vector2d(e.X - RightMouseDownLocation.X, e.Y - RightMouseDownLocation.Y);
if (vec.Length < 10)
{
ShowContextMenu(e.Location);
}
}
RightMouseDownLocation = System.Drawing.Point.Empty;
}
void ShowContextMenu(System.Drawing.Point location)
{
var contextMenu = new ContextMenuStrip();
var displayModeMenu = new ToolStripMenuItem("Display Mode");
var modes = Rhino.Display.DisplayModeDescription.GetDisplayModes();
var currentModeId = Guid.Empty;
if (Viewport.DisplayMode != null)
currentModeId = Viewport.DisplayMode.Id;
foreach (var mode in modes)
{
var modeMenuItem = new ToolStripMenuItem(mode.LocalName);
modeMenuItem.Checked = true;
modeMenuItem.Checked = (currentModeId == mode.Id);
modeMenuItem.Click += (s, e) =>
{
Viewport.DisplayMode = mode;
Invalidate();
};
displayModeMenu.DropDownItems.Add(modeMenuItem);
displayModeMenu.Tag = mode.Id;
}
contextMenu.Items.Add(displayModeMenu);
var dockMenu = new ToolStripMenuItem("Dock");
var mnu = new ToolStripRadioMenuItem("Top Left");
mnu.Checked = true;
mnu.Click += (s, args) => CanvasViewport.DockPanel(Parent, AnchorStyles.Top | AnchorStyles.Left);
dockMenu.DropDownItems.Add(mnu);
mnu = new ToolStripRadioMenuItem("Top Right");
mnu.Checked = true;
mnu.Click += (s, args) => CanvasViewport.DockPanel(Parent, AnchorStyles.Top | AnchorStyles.Right);
dockMenu.DropDownItems.Add(mnu);
mnu = new ToolStripRadioMenuItem("Bottom Left");
mnu.Checked = true;
mnu.Click += (s, args) => CanvasViewport.DockPanel(Parent, AnchorStyles.Bottom | AnchorStyles.Left);
dockMenu.DropDownItems.Add(mnu);
mnu = new ToolStripRadioMenuItem("Bottom Right");
mnu.Checked = true;
mnu.Click += (s, args) => CanvasViewport.DockPanel(Parent, AnchorStyles.Bottom | AnchorStyles.Right);
dockMenu.DropDownItems.Add(mnu);
contextMenu.Items.Add(dockMenu);
dockMenu.DropDownOpening += (s, args) =>
{
var anchor = this.Parent.Anchor;
((ToolStripMenuItem)dockMenu.DropDownItems[0]).Checked = (anchor == (AnchorStyles.Top | AnchorStyles.Left));
((ToolStripMenuItem)dockMenu.DropDownItems[1]).Checked = (anchor == (AnchorStyles.Bottom | AnchorStyles.Left));
((ToolStripMenuItem)dockMenu.DropDownItems[2]).Checked = (anchor == (AnchorStyles.Top | AnchorStyles.Right));
((ToolStripMenuItem)dockMenu.DropDownItems[3]).Checked = (anchor == (AnchorStyles.Bottom | AnchorStyles.Right));
};
contextMenu.Items.Add("Zoom Extents", null, (s, e) =>
{
Viewport.Camera35mmLensLength = 50;
Viewport.ZoomExtents();
Refresh();
});
contextMenu.Items.Add("Hide", null, (s, e) =>
{
this.Parent.Hide();
});
contextMenu.Show(this, location);
}
}
class ToolStripRadioMenuItem : ToolStripMenuItem
{
public ToolStripRadioMenuItem(string text) : base(text)
{
}
protected override void OnClick(EventArgs e)
{
Checked = true;
if (OwnerItem is ToolStripMenuItem parent)
{
foreach (var item in parent.DropDownItems.OfType<ToolStripRadioMenuItem>())
{
if (ReferenceEquals(this, item))
continue;
item.Checked = false;
}
}
base.OnClick(e);
}
}
}