FocusElementManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Input;
  15. using System.Windows.Interop;
  16. using System.Windows;
  17. using System.Diagnostics;
  18. using Xceed.Wpf.AvalonDock.Layout;
  19. using System.Windows.Media;
  20. using System.Windows.Threading;
  21. namespace Xceed.Wpf.AvalonDock.Controls
  22. {
  23. internal static class FocusElementManager
  24. {
  25. #region Focus Management
  26. static List<DockingManager> _managers = new List<DockingManager>();
  27. internal static void SetupFocusManagement(DockingManager manager)
  28. {
  29. if (_managers.Count == 0)
  30. {
  31. //InputManager.Current.EnterMenuMode += new EventHandler(InputManager_EnterMenuMode);
  32. //InputManager.Current.LeaveMenuMode += new EventHandler(InputManager_LeaveMenuMode);
  33. _windowHandler = new WindowHookHandler();
  34. _windowHandler.FocusChanged += new EventHandler<FocusChangeEventArgs>(WindowFocusChanging);
  35. //_windowHandler.Activate += new EventHandler<WindowActivateEventArgs>(WindowActivating);
  36. _windowHandler.Attach();
  37. if (Application.Current != null)
  38. Application.Current.Exit += new ExitEventHandler(Current_Exit);
  39. }
  40. manager.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler(manager_PreviewGotKeyboardFocus);
  41. _managers.Add(manager);
  42. }
  43. internal static void FinalizeFocusManagement(DockingManager manager)
  44. {
  45. manager.PreviewGotKeyboardFocus -= new KeyboardFocusChangedEventHandler(manager_PreviewGotKeyboardFocus);
  46. _managers.Remove(manager);
  47. if (_managers.Count == 0)
  48. {
  49. //InputManager.Current.EnterMenuMode -= new EventHandler(InputManager_EnterMenuMode);
  50. //InputManager.Current.LeaveMenuMode -= new EventHandler(InputManager_LeaveMenuMode);
  51. if (_windowHandler != null)
  52. {
  53. _windowHandler.FocusChanged -= new EventHandler<FocusChangeEventArgs>(WindowFocusChanging);
  54. //_windowHandler.Activate -= new EventHandler<WindowActivateEventArgs>(WindowActivating);
  55. _windowHandler.Detach();
  56. _windowHandler = null;
  57. }
  58. }
  59. }
  60. private static void Current_Exit(object sender, ExitEventArgs e)
  61. {
  62. Application.Current.Exit -= new ExitEventHandler(Current_Exit);
  63. if (_windowHandler != null)
  64. {
  65. _windowHandler.FocusChanged -= new EventHandler<FocusChangeEventArgs>(WindowFocusChanging);
  66. //_windowHandler.Activate -= new EventHandler<WindowActivateEventArgs>(WindowActivating);
  67. _windowHandler.Detach();
  68. _windowHandler = null;
  69. }
  70. }
  71. static void manager_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  72. {
  73. var focusedElement = e.NewFocus as Visual;
  74. if (focusedElement != null &&
  75. !(focusedElement is LayoutAnchorableTabItem || focusedElement is LayoutDocumentTabItem))
  76. //Avoid tracking focus for elements like this
  77. {
  78. var parentAnchorable = focusedElement.FindVisualAncestor<LayoutAnchorableControl>();
  79. if (parentAnchorable != null)
  80. {
  81. _modelFocusedElement[parentAnchorable.Model] = e.NewFocus;
  82. }
  83. else
  84. {
  85. var parentDocument = focusedElement.FindVisualAncestor<LayoutDocumentControl>();
  86. if (parentDocument != null)
  87. {
  88. _modelFocusedElement[parentDocument.Model] = e.NewFocus;
  89. }
  90. }
  91. }
  92. }
  93. static FullWeakDictionary<ILayoutElement, IInputElement> _modelFocusedElement = new FullWeakDictionary<ILayoutElement, IInputElement>();
  94. static WeakDictionary<ILayoutElement, IntPtr> _modelFocusedWindowHandle = new WeakDictionary<ILayoutElement, IntPtr>();
  95. /// <summary>
  96. /// Get the input element that was focused before user left the layout element
  97. /// </summary>
  98. /// <param name="model">Element to look for</param>
  99. /// <returns>Input element </returns>
  100. internal static IInputElement GetLastFocusedElement(ILayoutElement model)
  101. {
  102. IInputElement objectWithFocus;
  103. if (_modelFocusedElement.GetValue(model, out objectWithFocus))
  104. return objectWithFocus;
  105. return null;
  106. }
  107. /// <summary>
  108. /// Get the last window handle focused before user left the element passed as argument
  109. /// </summary>
  110. /// <param name="model"></param>
  111. /// <returns></returns>
  112. internal static IntPtr GetLastWindowHandle(ILayoutElement model)
  113. {
  114. IntPtr handleWithFocus;
  115. if (_modelFocusedWindowHandle.GetValue(model, out handleWithFocus))
  116. return handleWithFocus;
  117. return IntPtr.Zero;
  118. }
  119. static WeakReference _lastFocusedElement;
  120. /// <summary>
  121. /// Given a layout element tries to set the focus of the keyword where it was before user moved to another element
  122. /// </summary>
  123. /// <param name="model"></param>
  124. internal static void SetFocusOnLastElement(ILayoutElement model)
  125. {
  126. bool focused = false;
  127. IInputElement objectToFocus;
  128. if (_modelFocusedElement.GetValue(model, out objectToFocus))
  129. {
  130. focused = objectToFocus == Keyboard.Focus(objectToFocus);
  131. }
  132. IntPtr handleToFocus;
  133. if (_modelFocusedWindowHandle.GetValue(model, out handleToFocus))
  134. focused = IntPtr.Zero != Win32Helper.SetFocus(handleToFocus);
  135. Trace.WriteLine( string.Format( "SetFocusOnLastElement(focused={0}, model={1}, element={2})", focused, model, handleToFocus == IntPtr.Zero ? ( objectToFocus == null ? "" : objectToFocus.ToString() ) : handleToFocus.ToString() ) );
  136. if (focused)
  137. {
  138. _lastFocusedElement = new WeakReference(model);
  139. }
  140. }
  141. static WindowHookHandler _windowHandler = null;
  142. static void WindowFocusChanging(object sender, FocusChangeEventArgs e)
  143. {
  144. foreach (var manager in _managers)
  145. {
  146. var hostContainingFocusedHandle = manager.FindLogicalChildren<HwndHost>().FirstOrDefault(hw => Win32Helper.IsChild(hw.Handle, e.GotFocusWinHandle));
  147. if (hostContainingFocusedHandle != null)
  148. {
  149. var parentAnchorable = hostContainingFocusedHandle.FindVisualAncestor<LayoutAnchorableControl>();
  150. if (parentAnchorable != null)
  151. {
  152. _modelFocusedWindowHandle[parentAnchorable.Model] = e.GotFocusWinHandle;
  153. if (parentAnchorable.Model != null)
  154. parentAnchorable.Model.IsActive = true;
  155. }
  156. else
  157. {
  158. var parentDocument = hostContainingFocusedHandle.FindVisualAncestor<LayoutDocumentControl>();
  159. if (parentDocument != null)
  160. {
  161. _modelFocusedWindowHandle[parentDocument.Model] = e.GotFocusWinHandle;
  162. if (parentDocument.Model != null)
  163. parentDocument.Model.IsActive = true;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. static DispatcherOperation _setFocusAsyncOperation;
  170. static void WindowActivating(object sender, WindowActivateEventArgs e)
  171. {
  172. Trace.WriteLine("WindowActivating");
  173. if (Keyboard.FocusedElement == null &&
  174. _lastFocusedElement != null &&
  175. _lastFocusedElement.IsAlive)
  176. {
  177. var elementToSetFocus = _lastFocusedElement.Target as ILayoutElement;
  178. if (elementToSetFocus != null)
  179. {
  180. var manager = elementToSetFocus.Root.Manager;
  181. if (manager == null)
  182. return;
  183. IntPtr parentHwnd;
  184. if (!manager.GetParentWindowHandle(out parentHwnd))
  185. return;
  186. if (e.HwndActivating != parentHwnd)
  187. return;
  188. _setFocusAsyncOperation = Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
  189. {
  190. try
  191. {
  192. SetFocusOnLastElement(elementToSetFocus);
  193. }
  194. finally
  195. {
  196. _setFocusAsyncOperation = null;
  197. }
  198. }), DispatcherPriority.Background);
  199. }
  200. }
  201. }
  202. static WeakReference _lastFocusedElementBeforeEnterMenuMode = null;
  203. static void InputManager_EnterMenuMode(object sender, EventArgs e)
  204. {
  205. if (Keyboard.FocusedElement == null)
  206. return;
  207. var lastfocusDepObj = Keyboard.FocusedElement as DependencyObject;
  208. if (lastfocusDepObj.FindLogicalAncestor<DockingManager>() == null)
  209. {
  210. _lastFocusedElementBeforeEnterMenuMode = null;
  211. return;
  212. }
  213. _lastFocusedElementBeforeEnterMenuMode = new WeakReference(Keyboard.FocusedElement);
  214. }
  215. static void InputManager_LeaveMenuMode(object sender, EventArgs e)
  216. {
  217. if (_lastFocusedElementBeforeEnterMenuMode != null &&
  218. _lastFocusedElementBeforeEnterMenuMode.IsAlive)
  219. {
  220. var lastFocusedInputElement = _lastFocusedElementBeforeEnterMenuMode.GetValueOrDefault<UIElement>();
  221. if (lastFocusedInputElement != null)
  222. {
  223. if (lastFocusedInputElement != Keyboard.Focus(lastFocusedInputElement))
  224. Debug.WriteLine("Unable to activate the element");
  225. }
  226. }
  227. }
  228. #endregion
  229. }
  230. }