Skip to content

Commit 97689ee

Browse files
authored
Merge branch 'next' into FixTestMethodCommands
2 parents c9feb2e + 13d2ce7 commit 97689ee

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ public void Detach()
173173

174174
private void hook_MessageReceived(object sender, HookEventArgs e)
175175
{
176-
var active = User32.GetForegroundWindow();
177-
if (active != _mainWindowHandle)
178-
{
179-
return;
180-
}
181-
182176
var hotkey = sender as IHotkey;
183177
if (hotkey != null)
184178
{
@@ -207,6 +201,16 @@ private IntPtr WindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
207201
case WM.SETFOCUS:
208202
Attach();
209203
break;
204+
case WM.RUBBERDUCK_CHILD_FOCUS:
205+
if (lParam == IntPtr.Zero)
206+
{
207+
Detach();
208+
}
209+
else
210+
{
211+
Attach();
212+
}
213+
return IntPtr.Zero;
210214
case WM.NCACTIVATE:
211215
if (wParam == IntPtr.Zero)
212216
{
@@ -235,14 +239,11 @@ private bool HandleHotkeyMessage(IntPtr wParam)
235239
var processed = false;
236240
try
237241
{
238-
if (User32.IsVbeWindowActive(_mainWindowHandle))
242+
var hook = Hooks.OfType<Hotkey>().SingleOrDefault(k => k.HotkeyInfo.HookId == wParam);
243+
if (hook != null)
239244
{
240-
var hook = Hooks.OfType<Hotkey>().SingleOrDefault(k => k.HotkeyInfo.HookId == wParam);
241-
if (hook != null)
242-
{
243-
hook.OnMessageReceived();
244-
processed = true;
245-
}
245+
hook.OnMessageReceived();
246+
processed = true;
246247
}
247248
}
248249
catch (Exception exception)

RetailCoder.VBE/Common/WinAPI/User32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ public static class User32
180180
[DllImport("user32.dll", SetLastError = true)]
181181
internal static extern bool UnregisterDeviceNotification(IntPtr handle);
182182

183+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
184+
internal static extern IntPtr SendMessage(IntPtr hWnd, WM msg, IntPtr wParam, IntPtr lParam);
185+
183186
/// <summary>
184187
/// A helper function that returns <c>true</c> when the specified handle is that of the foreground window.
185188
/// </summary>

RetailCoder.VBE/Common/WinAPI/WM.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,11 @@ public enum WM : uint
922922
/// </summary>
923923
SYSTIMER = 0x118,
924924

925+
/// <summary>
926+
/// Private message to signal focus set/lost for a DockableWindowHost. Set wParam to the DockableWindowHost hWnd, lParam to zero for lost focus, non-zero for gained focus.
927+
/// </summary>
928+
RUBBERDUCK_CHILD_FOCUS = USER + 0x0F00,
929+
925930
/// <summary>
926931
/// The accessibility state has changed.
927932
/// </summary>

RetailCoder.VBE/UI/DockableToolwindowPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private Window CreateToolWindow(IDockableUserControl control)
6262

6363
toolWindow.Visible = false; //hide it again
6464

65-
userControlHost.AddUserControl(control as UserControl);
65+
userControlHost.AddUserControl(control as UserControl, new IntPtr(_vbe.MainWindow.HWnd));
6666
return toolWindow;
6767
}
6868

RetailCoder.VBE/UI/DockableWindowHost.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.Runtime.InteropServices;
66
using System.Windows.Forms;
7+
using Rubberduck.Common.WinAPI;
78
using Rubberduck.VBEditor;
89

910
namespace Rubberduck.UI
@@ -40,10 +41,10 @@ private struct Rect
4041
private IntPtr _parentHandle;
4142
private SubClassingWindow _subClassingWindow;
4243

43-
internal void AddUserControl(UserControl control)
44+
internal void AddUserControl(UserControl control, IntPtr vbeHwnd)
4445
{
4546
_parentHandle = GetParent(Handle);
46-
_subClassingWindow = new SubClassingWindow(_parentHandle);
47+
_subClassingWindow = new SubClassingWindow(_parentHandle, vbeHwnd);
4748
_subClassingWindow.CallBackEvent += OnCallBackEvent;
4849

4950
if (control != null)
@@ -116,27 +117,35 @@ public class SubClassingWindow : NativeWindow
116117
public event SubClassingWindowEventHandler CallBackEvent;
117118
public delegate void SubClassingWindowEventHandler(object sender, SubClassingWindowEventArgs e);
118119

120+
private readonly IntPtr _vbeHwnd;
121+
119122
private void OnCallBackEvent(SubClassingWindowEventArgs e)
120123
{
121124
Debug.Assert(CallBackEvent != null, "CallBackEvent != null");
122125
CallBackEvent(this, e);
123126
}
124127

125-
public SubClassingWindow(IntPtr handle)
128+
public SubClassingWindow(IntPtr handle, IntPtr vbeHwnd)
126129
{
130+
_vbeHwnd = vbeHwnd;
127131
AssignHandle(handle);
128132
}
129133

130134
protected override void WndProc(ref Message msg)
131135
{
132-
const int wmSize = 0x5;
133-
134-
if (msg.Msg == wmSize)
136+
switch ((uint)msg.Msg)
135137
{
136-
var args = new SubClassingWindowEventArgs(msg);
137-
OnCallBackEvent(args);
138+
case (uint)WM.SIZE:
139+
var args = new SubClassingWindowEventArgs(msg);
140+
OnCallBackEvent(args);
141+
break;
142+
case (uint)WM.SETFOCUS:
143+
User32.SendMessage(_vbeHwnd, WM.RUBBERDUCK_CHILD_FOCUS, Handle, Handle);
144+
break;
145+
case (uint)WM.KILLFOCUS:
146+
User32.SendMessage(_vbeHwnd, WM.RUBBERDUCK_CHILD_FOCUS, Handle, IntPtr.Zero);
147+
break;
138148
}
139-
140149
base.WndProc(ref msg);
141150
}
142151

0 commit comments

Comments
 (0)