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
|
/*************************************************************************************
Extended WPF Toolkit
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at http://xceed.com/wpf_toolkit
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows;
using System.Diagnostics;
using Xceed.Wpf.AvalonDock.Layout;
using System.Windows.Media;
using System.Windows.Threading;
namespace Xceed.Wpf.AvalonDock.Controls
{
internal static class FocusElementManager
{
#region Member
private static List<DockingManager> _managers = new List<DockingManager>();
private static FullWeakDictionary<ILayoutElement, IInputElement> _modelFocusedElement = new FullWeakDictionary<ILayoutElement, IInputElement>();
private static WeakDictionary<ILayoutElement, IntPtr> _modelFocusedWindowHandle = new WeakDictionary<ILayoutElement, IntPtr>();
private static WeakReference _lastFocusedElement;
private static WindowHookHandler _windowHandler = null;
private static DispatcherOperation _setFocusAsyncOperation;
private static WeakReference _lastFocusedElementBeforeEnterMenuMode = null;
#endregion
#region Internal Methods
internal static void SetupFocusManagement( DockingManager manager )
{
if( _managers.Count == 0 )
{
//InputManager.Current.EnterMenuMode += new EventHandler(InputManager_EnterMenuMode);
//InputManager.Current.LeaveMenuMode += new EventHandler(InputManager_LeaveMenuMode);
_windowHandler = new WindowHookHandler();
_windowHandler.FocusChanged += new EventHandler<FocusChangeEventArgs>( WindowFocusChanging );
//_windowHandler.Activate += new EventHandler<WindowActivateEventArgs>(WindowActivating);
_windowHandler.Attach();
if( Application.Current != null )
Application.Current.Exit += new ExitEventHandler( Current_Exit );
}
manager.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler( manager_PreviewGotKeyboardFocus );
_managers.Add( manager );
}
internal static void FinalizeFocusManagement( DockingManager manager )
{
manager.PreviewGotKeyboardFocus -= new KeyboardFocusChangedEventHandler( manager_PreviewGotKeyboardFocus );
_managers.Remove( manager );
if( _managers.Count == 0 )
{
//InputManager.Current.EnterMenuMode -= new EventHandler(InputManager_EnterMenuMode);
//InputManager.Current.LeaveMenuMode -= new EventHandler(InputManager_LeaveMenuMode);
if( _windowHandler != null )
{
_windowHandler.FocusChanged -= new EventHandler<FocusChangeEventArgs>( WindowFocusChanging );
//_windowHandler.Activate -= new EventHandler<WindowActivateEventArgs>(WindowActivating);
_windowHandler.Detach();
_windowHandler = null;
}
}
}
/// <summary>
/// Get the input element that was focused before user left the layout element
/// </summary>
/// <param name="model">Element to look for</param>
/// <returns>Input element </returns>
internal static IInputElement GetLastFocusedElement( ILayoutElement model )
{
IInputElement objectWithFocus;
if( _modelFocusedElement.GetValue( model, out objectWithFocus ) )
return objectWithFocus;
return null;
}
/// <summary>
/// Get the last window handle focused before user left the element passed as argument
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
internal static IntPtr GetLastWindowHandle( ILayoutElement model )
{
IntPtr handleWithFocus;
if( _modelFocusedWindowHandle.GetValue( model, out handleWithFocus ) )
return handleWithFocus;
return IntPtr.Zero;
}
/// <summary>
/// Given a layout element tries to set the focus of the keyword where it was before user moved to another element
/// </summary>
/// <param name="model"></param>
internal static void SetFocusOnLastElement( ILayoutElement model )
{
bool focused = false;
IInputElement objectToFocus;
if( _modelFocusedElement.GetValue( model, out objectToFocus ) )
{
focused = objectToFocus == Keyboard.Focus( objectToFocus );
}
IntPtr handleToFocus;
if( _modelFocusedWindowHandle.GetValue( model, out handleToFocus ) )
focused = IntPtr.Zero != Win32Helper.SetFocus( handleToFocus );
if( focused )
{
_lastFocusedElement = new WeakReference( model );
}
}
#endregion
#region Private Methods
private static void Current_Exit( object sender, ExitEventArgs e )
{
Application.Current.Exit -= new ExitEventHandler( Current_Exit );
if( _windowHandler != null )
{
_windowHandler.FocusChanged -= new EventHandler<FocusChangeEventArgs>( WindowFocusChanging );
//_windowHandler.Activate -= new EventHandler<WindowActivateEventArgs>(WindowActivating);
_windowHandler.Detach();
_windowHandler = null;
}
}
private static void manager_PreviewGotKeyboardFocus( object sender, KeyboardFocusChangedEventArgs e )
{
var focusedElement = e.NewFocus as Visual;
if( focusedElement != null &&
!( focusedElement is LayoutAnchorableTabItem || focusedElement is LayoutDocumentTabItem ) )
//Avoid tracking focus for elements like this
{
var parentAnchorable = focusedElement.FindVisualAncestor<LayoutAnchorableControl>();
if( parentAnchorable != null )
{
_modelFocusedElement[ parentAnchorable.Model ] = e.NewFocus;
}
else
{
var parentDocument = focusedElement.FindVisualAncestor<LayoutDocumentControl>();
if( parentDocument != null )
{
_modelFocusedElement[ parentDocument.Model ] = e.NewFocus;
}
}
}
}
private static void WindowFocusChanging( object sender, FocusChangeEventArgs e )
{
foreach( var manager in _managers )
{
var hostContainingFocusedHandle = manager.FindLogicalChildren<HwndHost>().FirstOrDefault( hw => Win32Helper.IsChild( hw.Handle, e.GotFocusWinHandle ) );
if( hostContainingFocusedHandle != null )
{
var parentAnchorable = hostContainingFocusedHandle.FindVisualAncestor<LayoutAnchorableControl>();
if( parentAnchorable != null )
{
_modelFocusedWindowHandle[ parentAnchorable.Model ] = e.GotFocusWinHandle;
if( parentAnchorable.Model != null )
parentAnchorable.Model.IsActive = true;
}
else
{
var parentDocument = hostContainingFocusedHandle.FindVisualAncestor<LayoutDocumentControl>();
if( parentDocument != null )
{
_modelFocusedWindowHandle[ parentDocument.Model ] = e.GotFocusWinHandle;
if( parentDocument.Model != null )
parentDocument.Model.IsActive = true;
}
}
}
}
}
private static void WindowActivating( object sender, WindowActivateEventArgs e )
{
if( Keyboard.FocusedElement == null &&
_lastFocusedElement != null &&
_lastFocusedElement.IsAlive )
{
var elementToSetFocus = _lastFocusedElement.Target as ILayoutElement;
if( elementToSetFocus != null )
{
var manager = elementToSetFocus.Root.Manager;
if( manager == null )
return;
IntPtr parentHwnd;
if( !manager.GetParentWindowHandle( out parentHwnd ) )
return;
if( e.HwndActivating != parentHwnd )
return;
_setFocusAsyncOperation = Dispatcher.CurrentDispatcher.BeginInvoke( new Action( () =>
{
try
{
SetFocusOnLastElement( elementToSetFocus );
}
finally
{
_setFocusAsyncOperation = null;
}
} ), DispatcherPriority.Input );
}
}
}
private static void InputManager_EnterMenuMode( object sender, EventArgs e )
{
if( Keyboard.FocusedElement == null )
return;
var lastfocusDepObj = Keyboard.FocusedElement as DependencyObject;
if( lastfocusDepObj.FindLogicalAncestor<DockingManager>() == null )
{
_lastFocusedElementBeforeEnterMenuMode = null;
return;
}
_lastFocusedElementBeforeEnterMenuMode = new WeakReference( Keyboard.FocusedElement );
}
private static void InputManager_LeaveMenuMode( object sender, EventArgs e )
{
if( _lastFocusedElementBeforeEnterMenuMode != null &&
_lastFocusedElementBeforeEnterMenuMode.IsAlive )
{
var lastFocusedInputElement = _lastFocusedElementBeforeEnterMenuMode.GetValueOrDefault<UIElement>();
if( lastFocusedInputElement != null )
{
if( lastFocusedInputElement != Keyboard.Focus( lastFocusedInputElement ) )
Debug.WriteLine( "Unable to activate the element" );
}
}
}
#endregion
}
}
|