Skip to content

Commit 58b650b

Browse files
committed
Fixes for focus events to catch designer windows. Still needs work in App.cs
1 parent 651ee88 commit 58b650b

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

RetailCoder.VBE/App.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ private void _vbe_FocusChanged(object sender, WindowChangedEventArgs e)
9494
{
9595
if (e.EventType == WindowChangedEventArgs.FocusType.GotFocus)
9696
{
97-
RefreshSelection(e.Window);
97+
switch (e.Window.Type)
98+
{
99+
case WindowKind.Designer:
100+
RefreshSelection(e.Window);
101+
break;
102+
case WindowKind.CodeWindow:
103+
RefreshSelection(e.CodePane);
104+
break;
105+
}
98106
}
99107
}
100108

Rubberduck.VBEEditor/Events/VBEEvents.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ private static void AttachWindow(IntPtr hwnd)
9292
Debug.Assert(!TrackedWindows.ContainsKey(hwnd));
9393
var window = GetWindowFromHwnd(hwnd);
9494
if (window == null) return;
95-
var source = window.Type == WindowKind.CodeWindow ? new CodePaneSubclass(hwnd) as IWindowEventProvider: new DesignerWindowSubclass(hwnd);
95+
var source = window.Type == WindowKind.CodeWindow
96+
? new CodePaneSubclass(hwnd, GetCodePaneFromHwnd(hwnd)) as IWindowEventProvider
97+
: new DesignerWindowSubclass(hwnd);
9698
var info = new WindowInfo(hwnd, window, source);
9799
source.FocusChange += FocusDispatcher;
98100
TrackedWindows.Add(hwnd, info);

Rubberduck.VBEEditor/Events/WindowChangedEventArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public enum FocusType
1313

1414
public IntPtr Hwnd { get; private set; }
1515
public IWindow Window { get; private set; }
16+
public ICodePane CodePane { get; private set; }
1617
public FocusType EventType { get; private set; }
1718

18-
public WindowChangedEventArgs(IntPtr hwnd, IWindow window, FocusType type)
19+
public WindowChangedEventArgs(IntPtr hwnd, IWindow window, ICodePane pane, FocusType type)
1920
{
2021
Hwnd = hwnd;
2122
Window = window;
23+
CodePane = pane;
2224
EventType = type;
2325
}
2426
}
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
using System;
2+
using Rubberduck.VBEditor.Events;
3+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
24

35
namespace Rubberduck.VBEditor.WindowsApi
46
{
7+
//Stub for code pane replacement. :-)
58
internal class CodePaneSubclass : FocusSource
69
{
7-
//Stub for code pane replacement. :-)
8-
internal CodePaneSubclass(IntPtr hwnd) : base(hwnd) { }
10+
private readonly ICodePane _pane;
11+
12+
public ICodePane CodePane { get { return _pane; } }
13+
14+
internal CodePaneSubclass(IntPtr hwnd, ICodePane pane) : base(hwnd)
15+
{
16+
_pane = pane;
17+
}
18+
19+
protected override void DispatchFocusEvent(WindowChangedEventArgs.FocusType type)
20+
{
21+
var window = VBEEvents.GetWindowInfoFromHwnd(Hwnd);
22+
if (window == null)
23+
{
24+
return;
25+
}
26+
OnFocusChange(new WindowChangedEventArgs(window.Value.Hwnd, window.Value.Window, _pane, type));
27+
}
928
}
1029
}

Rubberduck.VBEEditor/WindowsApi/FocusSource.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@ internal abstract class FocusSource : SubclassingWindow, IWindowEventProvider
99
protected FocusSource(IntPtr hwnd) : base(hwnd, hwnd) { }
1010

1111
public event EventHandler<WindowChangedEventArgs> FocusChange;
12-
private void OnFocusChange(WindowChangedEventArgs.FocusType type)
12+
protected void OnFocusChange(WindowChangedEventArgs eventArgs)
1313
{
1414
if (FocusChange != null)
1515
{
16-
var window = VBEEvents.GetWindowInfoFromHwnd(Hwnd);
17-
if (window == null)
18-
{
19-
return;
20-
}
21-
FocusChange.Invoke(this, new WindowChangedEventArgs(window.Value.Hwnd, window.Value.Window, type));
16+
FocusChange.Invoke(this, eventArgs);
2217
}
23-
}
18+
}
19+
20+
protected virtual void DispatchFocusEvent(WindowChangedEventArgs.FocusType type)
21+
{
22+
var window = VBEEvents.GetWindowInfoFromHwnd(Hwnd);
23+
if (window == null)
24+
{
25+
return;
26+
}
27+
OnFocusChange(new WindowChangedEventArgs(Hwnd, window.Value.Window, null, type));
28+
}
2429

2530
public override int SubClassProc(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData)
2631
{
2732
switch ((uint)msg)
2833
{
2934
case (uint)WM.SETFOCUS:
30-
OnFocusChange(WindowChangedEventArgs.FocusType.GotFocus);
35+
36+
DispatchFocusEvent(WindowChangedEventArgs.FocusType.GotFocus);
3137
break;
3238
case (uint)WM.KILLFOCUS:
33-
OnFocusChange(WindowChangedEventArgs.FocusType.LostFocus);
39+
DispatchFocusEvent(WindowChangedEventArgs.FocusType.LostFocus);
3440
break;
3541
}
3642
return base.SubClassProc(hWnd, msg, wParam, lParam, uIdSubclass, dwRefData);

0 commit comments

Comments
 (0)