-
Notifications
You must be signed in to change notification settings - Fork 296
/
VBENativeServices.cs
314 lines (281 loc) · 11.2 KB
/
VBENativeServices.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.WindowsApi;
namespace Rubberduck.VBEditor.Events
{
// ReSharper disable once InconsistentNaming
public static class VbeNativeServices
{
private static User32.WinEventProc _eventProc;
private static IntPtr _eventHandle;
private static IVBE _vbe;
private static readonly SubclassManager Subclasses = new SubclassManager();
private static readonly object ThreadLock = new object();
private static uint _threadId;
public static void HookEvents(IVBE vbe)
{
_vbe = vbe;
if (_eventHandle == IntPtr.Zero)
{
_eventProc = VbeEventCallback;
IntPtr mainWindowHwnd;
using (var mainWindow = _vbe.MainWindow)
{
mainWindowHwnd = new IntPtr(mainWindow.HWnd);
}
_threadId = User32.GetWindowThreadProcessId(mainWindowHwnd, IntPtr.Zero);
_eventHandle = User32.SetWinEventHook((uint)WinEvent.Min, (uint)WinEvent.Max, IntPtr.Zero, _eventProc, 0, _threadId, WinEventFlags.OutOfContext);
Subclasses.Subclass(mainWindowHwnd.ChildWindows()
.Where(hwnd => SubclassManager.IsSubclassable(hwnd.ToWindowType())));
}
}
public static void UnhookEvents()
{
lock (ThreadLock)
{
SelectionChanged = delegate { };
IntelliSenseChanged = delegate { };
KeyDown = delegate { };
WindowFocusChange = delegate { };
User32.UnhookWinEvent(_eventHandle);
Subclasses.Dispose();
VbeEvents.Terminate();
_vbe = null;
}
}
private static bool Suspend { get; set; }
private static void Attach(IntPtr hwnd)
{
var subclass = Subclasses.Subclass(hwnd);
if (subclass == null)
{
return;
}
if (subclass is IFocusProvider focusSource)
{
focusSource.FocusChange += FocusDispatcher;
}
if (subclass is IWindowEventProvider keyboardListener)
{
keyboardListener.KeyDown += KeyDownDispatcher;
}
}
public static void VbeEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild,
uint dwEventThread, uint dwmsEventTime)
{
if (Suspend || hwnd == IntPtr.Zero || _vbe.IsWrappingNullReference) { return; }
var windowType = hwnd.ToWindowType();
PeekMessagePump(eventType, hwnd, idObject, idChild);
if (windowType == WindowType.IntelliSense)
{
if (eventType == (uint)WinEvent.ObjectShow)
{
OnIntelliSenseChanged(true);
}
else if (eventType == (uint)WinEvent.ObjectHide)
{
OnIntelliSenseChanged(false);
}
}
else if (windowType == WindowType.CodePane && idObject == (int)ObjId.Caret &&
(eventType == (uint)WinEvent.ObjectLocationChange || eventType == (uint)WinEvent.ObjectCreate))
{
OnSelectionChanged(hwnd);
}
else if (SubclassManager.IsSubclassable(windowType) && (idObject == (int)ObjId.Window && eventType == (uint)WinEvent.ObjectCreate) ||
!Subclasses.IsSubclassed(hwnd))
{
Attach(hwnd);
}
else if (eventType == (uint)WinEvent.ObjectFocus && idObject == (int)ObjId.Client)
{
//Test to see if it was a selection change in the project window.
var parent = User32.GetParent(hwnd);
if (parent != IntPtr.Zero && parent.ToWindowType() == WindowType.Project && hwnd == User32.GetFocus())
{
FocusDispatcher(_vbe, new WindowChangedEventArgs(parent, FocusType.ChildFocus));
}
}
}
private static void KeyDownDispatcher(object sender, KeyPressEventArgs e)
{
OnKeyDown(e);
}
private static void FocusDispatcher(object sender, WindowChangedEventArgs eventArgs)
{
OnWindowFocusChange(sender, eventArgs);
}
public static event EventHandler<SelectionChangedEventArgs> SelectionChanged;
private static void OnSelectionChanged(IntPtr hwnd)
{
using (var pane = GetCodePaneFromHwnd(hwnd))
{
if (pane != null)
{
SelectionChanged?.Invoke(_vbe, new SelectionChangedEventArgs());
}
}
}
public static event EventHandler<IntelliSenseEventArgs> IntelliSenseChanged;
public static void OnIntelliSenseChanged(bool shown)
{
IntelliSenseChanged?.Invoke(_vbe, shown ? IntelliSenseEventArgs.Shown : IntelliSenseEventArgs.Hidden);
}
public static event EventHandler<AutoCompleteEventArgs> KeyDown;
private static void OnKeyDown(KeyPressEventArgs e)
{
using (var pane = GetCodePaneFromHwnd(e.Hwnd))
{
if (pane != null)
{
using (var module = pane.CodeModule)
{
var args = new AutoCompleteEventArgs(module, e);
Suspend = true;
KeyDown?.Invoke(_vbe, args);
Suspend = false;
e.Handled = args.Handled;
}
}
}
}
public static event EventHandler<WindowChangedEventArgs> WindowFocusChange;
private static void OnWindowFocusChange(object sender, WindowChangedEventArgs eventArgs)
{
WindowFocusChange?.Invoke(sender, eventArgs);
}
public static ICodePane GetCodePaneFromHwnd(IntPtr hwnd)
{
if (_vbe == null || _vbe.IsWrappingNullReference)
{
return null;
}
try
{
var caption = hwnd.GetWindowText();
using (var panes = _vbe.CodePanes)
{
if (panes == null || panes.IsWrappingNullReference)
{
return null;
}
var foundIt = false;
foreach (var pane in panes)
{
try
{
using (var window = pane.Window)
{
if (window.Caption.Equals(caption))
{
foundIt = true;
return pane;
}
}
}
finally
{
if (!foundIt)
{
pane.Dispose();
}
}
}
return null;
}
}
catch
{
// This *should* only happen when a code pane window is removed and RD responds faster than
// the VBE removes it from the windows collection. TODO: Find a better method to match code panes
// to windows than testing the captions.
return null;
}
}
public static IWindow GetWindowFromHwnd(IntPtr hwnd)
{
if (!User32.IsWindow(hwnd) || _vbe == null || _vbe.IsWrappingNullReference)
{
return null;
}
var caption = hwnd.GetWindowText();
using (var windows = _vbe.Windows)
{
if (windows == null || windows.IsWrappingNullReference)
{
return null;
}
var foundIt = false;
foreach (var window in windows)
{
try
{
if (window.Caption.Equals(caption))
{
foundIt = true;
return window;
}
}
finally
{
if (!foundIt)
{
window.Dispose();
}
}
}
return null;
}
}
/// <summary>
/// A helper function that returns <c>true</c> when the specified handle is that of the foreground window.
/// </summary>
/// <returns>True if the active thread is on the VBE's thread.</returns>
public static bool IsVbeWindowActive()
{
User32.GetWindowThreadProcessId(User32.GetForegroundWindow(), out var hThread);
return (IntPtr)hThread == (IntPtr)_threadId;
}
public static WindowType ToWindowType(this IntPtr hwnd)
{
var className = hwnd.ToClassName();
if (className.Equals("NameListWndClass"))
{
return WindowType.IntelliSense;
}
var type = Enum.TryParse(className, true, out WindowType id) ? id : WindowType.Indeterminate;
if (type != WindowType.VbaWindow)
{
return type;
}
//A this point we only care about code panes - none of the other 4 types of VbaWindows (Immediate, Object Browser, Locals,
//and Watches) contain a tool bar at the top, so just see if the window has one as a child.
var toolbar = User32.FindWindowEx(hwnd, IntPtr.Zero, "ObtbarWndClass", string.Empty);
return toolbar == IntPtr.Zero ? WindowType.VbaWindow : WindowType.CodePane;
}
private static string ToClassName(this IntPtr hwnd)
{
var name = new StringBuilder(User32.MaxGetClassNameBufferSize);
User32.GetClassName(hwnd, name, name.Capacity);
return name.ToString();
}
[Conditional("THIRSTY_DUCK")]
[Conditional("THIRSTY_DUCK_EVT")]
private static void PeekMessagePump(uint eventType, IntPtr hwnd, int idObject, int idChild)
{
//This is an output window firehose, viewer discretion is advised.
if (idObject != (int)ObjId.Cursor)
{
var windowClassName = hwnd.ToClassName();
if (!WinEventMap.Lookup.TryGetValue((int)eventType, out var eventName))
{
eventName = "Unknown";
}
Debug.WriteLine($"EVT: 0x{eventType:X4} ({eventName}) Hwnd 0x{(int)hwnd:X4} ({windowClassName}), idObject {idObject}, idChild {idChild}");
}
}
}
}