NavigatorWindow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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;
  15. using Xceed.Wpf.AvalonDock.Layout;
  16. using System.Windows.Interop;
  17. using System.Windows.Threading;
  18. using Xceed.Wpf.AvalonDock.Themes;
  19. namespace Xceed.Wpf.AvalonDock.Controls
  20. {
  21. public class NavigatorWindow : Window
  22. {
  23. private ResourceDictionary currentThemeResourceDictionary; // = null
  24. static NavigatorWindow()
  25. {
  26. DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigatorWindow), new FrameworkPropertyMetadata(typeof(NavigatorWindow)));
  27. ShowActivatedProperty.OverrideMetadata(typeof(NavigatorWindow), new FrameworkPropertyMetadata(false));
  28. ShowInTaskbarProperty.OverrideMetadata(typeof(NavigatorWindow), new FrameworkPropertyMetadata(false));
  29. }
  30. DockingManager _manager;
  31. internal NavigatorWindow(DockingManager manager)
  32. {
  33. _manager = manager;
  34. _internalSetSelectedDocument = true;
  35. SetAnchorables(_manager.Layout.Descendents().OfType<LayoutAnchorable>().Where(a => a.IsVisible).Select(d => (LayoutAnchorableItem)_manager.GetLayoutItemFromModel(d)).ToArray());
  36. SetDocuments(_manager.Layout.Descendents().OfType<LayoutDocument>().OrderByDescending(d => d.LastActivationTimeStamp.GetValueOrDefault()).Select(d => (LayoutDocumentItem)_manager.GetLayoutItemFromModel(d)).ToArray());
  37. _internalSetSelectedDocument = false;
  38. if (Documents.Length > 1)
  39. InternalSetSelectedDocument(Documents[1]);
  40. this.DataContext = this;
  41. this.Loaded += new RoutedEventHandler(OnLoaded);
  42. this.Unloaded += new RoutedEventHandler(OnUnloaded);
  43. UpdateThemeResources();
  44. }
  45. internal void UpdateThemeResources(Theme oldTheme = null)
  46. {
  47. if (oldTheme != null)
  48. {
  49. if( oldTheme is DictionaryTheme )
  50. {
  51. if( currentThemeResourceDictionary != null )
  52. {
  53. Resources.MergedDictionaries.Remove( currentThemeResourceDictionary );
  54. currentThemeResourceDictionary = null;
  55. }
  56. }
  57. else
  58. {
  59. var resourceDictionaryToRemove =
  60. Resources.MergedDictionaries.FirstOrDefault( r => r.Source == oldTheme.GetResourceUri() );
  61. if( resourceDictionaryToRemove != null )
  62. Resources.MergedDictionaries.Remove(
  63. resourceDictionaryToRemove );
  64. }
  65. }
  66. if (_manager.Theme != null)
  67. {
  68. if( _manager.Theme is DictionaryTheme )
  69. {
  70. currentThemeResourceDictionary = ( ( DictionaryTheme )_manager.Theme ).ThemeResourceDictionary;
  71. Resources.MergedDictionaries.Add( currentThemeResourceDictionary );
  72. }
  73. else
  74. {
  75. Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = _manager.Theme.GetResourceUri() });
  76. }
  77. }
  78. }
  79. void OnLoaded(object sender, RoutedEventArgs e)
  80. {
  81. this.Loaded -= new RoutedEventHandler(OnLoaded);
  82. this.Focus();
  83. //this.SetParentToMainWindowOf(_manager);
  84. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  85. }
  86. void OnUnloaded(object sender, RoutedEventArgs e)
  87. {
  88. this.Unloaded -= new RoutedEventHandler(OnUnloaded);
  89. //_hwndSrc.RemoveHook(_hwndSrcHook);
  90. //_hwndSrc.Dispose();
  91. //_hwndSrc = null;
  92. }
  93. //protected virtual IntPtr FilterMessage(
  94. // IntPtr hwnd,
  95. // int msg,
  96. // IntPtr wParam,
  97. // IntPtr lParam,
  98. // ref bool handled
  99. // )
  100. //{
  101. // handled = false;
  102. // switch (msg)
  103. // {
  104. // case Win32Helper.WM_ACTIVATE:
  105. // if (((int)wParam & 0xFFFF) == Win32Helper.WA_INACTIVE)
  106. // {
  107. // if (lParam == new WindowInteropHelper(this.Owner).Handle)
  108. // {
  109. // Win32Helper.SetActiveWindow(_hwndSrc.Handle);
  110. // handled = true;
  111. // }
  112. // }
  113. // break;
  114. // }
  115. // return IntPtr.Zero;
  116. //}
  117. #region Documents
  118. /// <summary>
  119. /// Documents Read-Only Dependency Property
  120. /// </summary>
  121. private static readonly DependencyPropertyKey DocumentsPropertyKey
  122. = DependencyProperty.RegisterReadOnly("Documents", typeof(IEnumerable<LayoutDocumentItem>), typeof(NavigatorWindow),
  123. new FrameworkPropertyMetadata(null));
  124. public static readonly DependencyProperty DocumentsProperty
  125. = DocumentsPropertyKey.DependencyProperty;
  126. /// <summary>
  127. /// Gets the Documents property. This dependency property
  128. /// indicates the list of documents.
  129. /// </summary>
  130. public LayoutDocumentItem[] Documents
  131. {
  132. get { return (LayoutDocumentItem[])GetValue(DocumentsProperty); }
  133. }
  134. /// <summary>
  135. /// Provides a secure method for setting the Documents property.
  136. /// This dependency property indicates the list of documents.
  137. /// </summary>
  138. /// <param name="value">The new value for the property.</param>
  139. protected void SetDocuments(LayoutDocumentItem[] value)
  140. {
  141. SetValue(DocumentsPropertyKey, value);
  142. }
  143. #endregion
  144. #region Anchorables
  145. /// <summary>
  146. /// Anchorables Read-Only Dependency Property
  147. /// </summary>
  148. private static readonly DependencyPropertyKey AnchorablesPropertyKey
  149. = DependencyProperty.RegisterReadOnly("Anchorables", typeof(IEnumerable<LayoutAnchorableItem>), typeof(NavigatorWindow),
  150. new FrameworkPropertyMetadata((IEnumerable<LayoutAnchorableItem>)null));
  151. public static readonly DependencyProperty AnchorablesProperty
  152. = AnchorablesPropertyKey.DependencyProperty;
  153. /// <summary>
  154. /// Gets the Anchorables property. This dependency property
  155. /// indicates the list of anchorables.
  156. /// </summary>
  157. public IEnumerable<LayoutAnchorableItem> Anchorables
  158. {
  159. get { return (IEnumerable<LayoutAnchorableItem>)GetValue(AnchorablesProperty); }
  160. }
  161. /// <summary>
  162. /// Provides a secure method for setting the Anchorables property.
  163. /// This dependency property indicates the list of anchorables.
  164. /// </summary>
  165. /// <param name="value">The new value for the property.</param>
  166. protected void SetAnchorables(IEnumerable<LayoutAnchorableItem> value)
  167. {
  168. SetValue(AnchorablesPropertyKey, value);
  169. }
  170. #endregion
  171. #region SelectedDocument
  172. /// <summary>
  173. /// SelectedDocument Dependency Property
  174. /// </summary>
  175. public static readonly DependencyProperty SelectedDocumentProperty =
  176. DependencyProperty.Register("SelectedDocument", typeof(LayoutDocumentItem), typeof(NavigatorWindow),
  177. new FrameworkPropertyMetadata((LayoutDocumentItem)null,
  178. new PropertyChangedCallback(OnSelectedDocumentChanged)));
  179. /// <summary>
  180. /// Gets or sets the SelectedDocument property. This dependency property
  181. /// indicates the selected document.
  182. /// </summary>
  183. public LayoutDocumentItem SelectedDocument
  184. {
  185. get { return (LayoutDocumentItem)GetValue(SelectedDocumentProperty); }
  186. set { SetValue(SelectedDocumentProperty, value); }
  187. }
  188. /// <summary>
  189. /// Handles changes to the SelectedDocument property.
  190. /// </summary>
  191. private static void OnSelectedDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  192. {
  193. ((NavigatorWindow)d).OnSelectedDocumentChanged(e);
  194. }
  195. /// <summary>
  196. /// Provides derived classes an opportunity to handle changes to the SelectedDocument property.
  197. /// </summary>
  198. protected virtual void OnSelectedDocumentChanged(DependencyPropertyChangedEventArgs e)
  199. {
  200. if (_internalSetSelectedDocument)
  201. return;
  202. if (SelectedDocument != null &&
  203. SelectedDocument.ActivateCommand.CanExecute(null))
  204. {
  205. System.Diagnostics.Trace.WriteLine( "OnSelectedDocumentChanged()" );
  206. SelectedDocument.ActivateCommand.Execute(null);
  207. Hide();
  208. }
  209. }
  210. bool _internalSetSelectedDocument = false;
  211. void InternalSetSelectedDocument(LayoutDocumentItem documentToSelect)
  212. {
  213. _internalSetSelectedDocument = true;
  214. SelectedDocument = documentToSelect;
  215. _internalSetSelectedDocument = false;
  216. }
  217. #endregion
  218. #region SelectedAnchorable
  219. /// <summary>
  220. /// SelectedAnchorable Dependency Property
  221. /// </summary>
  222. public static readonly DependencyProperty SelectedAnchorableProperty =
  223. DependencyProperty.Register("SelectedAnchorable", typeof(LayoutAnchorableItem), typeof(NavigatorWindow),
  224. new FrameworkPropertyMetadata((LayoutAnchorableItem)null,
  225. new PropertyChangedCallback(OnSelectedAnchorableChanged)));
  226. /// <summary>
  227. /// Gets or sets the SelectedAnchorable property. This dependency property
  228. /// indicates the selected anchorable.
  229. /// </summary>
  230. public LayoutAnchorableItem SelectedAnchorable
  231. {
  232. get { return (LayoutAnchorableItem)GetValue(SelectedAnchorableProperty); }
  233. set { SetValue(SelectedAnchorableProperty, value); }
  234. }
  235. /// <summary>
  236. /// Handles changes to the SelectedAnchorable property.
  237. /// </summary>
  238. private static void OnSelectedAnchorableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  239. {
  240. ((NavigatorWindow)d).OnSelectedAnchorableChanged(e);
  241. }
  242. /// <summary>
  243. /// Provides derived classes an opportunity to handle changes to the SelectedAnchorable property.
  244. /// </summary>
  245. protected virtual void OnSelectedAnchorableChanged(DependencyPropertyChangedEventArgs e)
  246. {
  247. var selectedAnchorable = e.NewValue as LayoutAnchorableItem;
  248. if (SelectedAnchorable != null &&
  249. SelectedAnchorable.ActivateCommand.CanExecute(null))
  250. {
  251. SelectedAnchorable.ActivateCommand.Execute(null);
  252. Close();
  253. }
  254. }
  255. #endregion
  256. internal void SelectNextDocument()
  257. {
  258. if (SelectedDocument != null)
  259. {
  260. int docIndex = Documents.IndexOf<LayoutDocumentItem>(SelectedDocument);
  261. docIndex++;
  262. if (docIndex == Documents.Length)
  263. docIndex = 0;
  264. InternalSetSelectedDocument(Documents[docIndex]);
  265. }
  266. }
  267. protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
  268. {
  269. base.OnKeyDown(e);
  270. }
  271. protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
  272. {
  273. if (e.Key == System.Windows.Input.Key.Tab)
  274. {
  275. SelectNextDocument();
  276. e.Handled = true;
  277. }
  278. base.OnPreviewKeyDown(e);
  279. }
  280. protected override void OnPreviewKeyUp(System.Windows.Input.KeyEventArgs e)
  281. {
  282. if (e.Key != System.Windows.Input.Key.Tab)
  283. {
  284. if (SelectedAnchorable != null &&
  285. SelectedAnchorable.ActivateCommand.CanExecute(null))
  286. SelectedAnchorable.ActivateCommand.Execute(null);
  287. if (SelectedAnchorable == null &&
  288. SelectedDocument != null &&
  289. SelectedDocument.ActivateCommand.CanExecute(null))
  290. SelectedDocument.ActivateCommand.Execute(null);
  291. Close();
  292. e.Handled = true;
  293. }
  294. base.OnPreviewKeyUp(e);
  295. }
  296. }
  297. }