DocumentPaneDropAsAnchorableTarget.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 System.Windows.Media;
  16. using System.Diagnostics;
  17. using Xceed.Wpf.AvalonDock.Layout;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. internal class DocumentPaneDropAsAnchorableTarget : DropTarget<LayoutDocumentPaneControl>
  21. {
  22. internal DocumentPaneDropAsAnchorableTarget(LayoutDocumentPaneControl paneControl, Rect detectionRect, DropTargetType type)
  23. : base(paneControl, detectionRect, type)
  24. {
  25. _targetPane = paneControl;
  26. }
  27. internal DocumentPaneDropAsAnchorableTarget(LayoutDocumentPaneControl paneControl, Rect detectionRect, DropTargetType type, int tabIndex)
  28. : base(paneControl, detectionRect, type)
  29. {
  30. _targetPane = paneControl;
  31. _tabIndex = tabIndex;
  32. }
  33. LayoutDocumentPaneControl _targetPane;
  34. int _tabIndex = -1;
  35. protected override void Drop(LayoutAnchorableFloatingWindow floatingWindow)
  36. {
  37. ILayoutDocumentPane targetModel = _targetPane.Model as ILayoutDocumentPane;
  38. LayoutDocumentPaneGroup parentGroup;
  39. LayoutPanel parentGroupPanel;
  40. FindParentLayoutDocumentPane(targetModel, out parentGroup, out parentGroupPanel);
  41. switch (Type)
  42. {
  43. case DropTargetType.DocumentPaneDockAsAnchorableBottom:
  44. #region DropTargetType.DocumentPaneDockAsAnchorableBottom
  45. {
  46. if (parentGroupPanel != null &&
  47. parentGroupPanel.ChildrenCount == 1)
  48. parentGroupPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
  49. if (parentGroupPanel != null &&
  50. parentGroupPanel.Orientation == System.Windows.Controls.Orientation.Vertical)
  51. {
  52. parentGroupPanel.Children.Insert(
  53. parentGroupPanel.IndexOfChild(parentGroup != null ? parentGroup : targetModel) + 1,
  54. floatingWindow.RootPanel);
  55. }
  56. else if (parentGroupPanel != null)
  57. {
  58. var newParentPanel = new LayoutPanel() { Orientation = System.Windows.Controls.Orientation.Vertical };
  59. parentGroupPanel.ReplaceChild(parentGroup != null ? parentGroup : targetModel, newParentPanel);
  60. newParentPanel.Children.Add(parentGroup != null ? parentGroup : targetModel);
  61. newParentPanel.Children.Add(floatingWindow.RootPanel);
  62. }
  63. else
  64. {
  65. throw new NotImplementedException();
  66. }
  67. }
  68. break;
  69. #endregion
  70. case DropTargetType.DocumentPaneDockAsAnchorableTop:
  71. #region DropTargetType.DocumentPaneDockAsAnchorableTop
  72. {
  73. if (parentGroupPanel != null &&
  74. parentGroupPanel.ChildrenCount == 1)
  75. parentGroupPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
  76. if (parentGroupPanel != null &&
  77. parentGroupPanel.Orientation == System.Windows.Controls.Orientation.Vertical)
  78. {
  79. parentGroupPanel.Children.Insert(
  80. parentGroupPanel.IndexOfChild(parentGroup != null ? parentGroup : targetModel),
  81. floatingWindow.RootPanel);
  82. }
  83. else if (parentGroupPanel != null)
  84. {
  85. var newParentPanel = new LayoutPanel() { Orientation = System.Windows.Controls.Orientation.Vertical };
  86. parentGroupPanel.ReplaceChild(parentGroup != null ? parentGroup : targetModel, newParentPanel);
  87. newParentPanel.Children.Add(parentGroup != null ? parentGroup : targetModel);
  88. newParentPanel.Children.Insert(0, floatingWindow.RootPanel);
  89. }
  90. else
  91. {
  92. throw new NotImplementedException();
  93. }
  94. }
  95. break;
  96. #endregion
  97. case DropTargetType.DocumentPaneDockAsAnchorableLeft:
  98. #region DropTargetType.DocumentPaneDockAsAnchorableLeft
  99. {
  100. if (parentGroupPanel != null &&
  101. parentGroupPanel.ChildrenCount == 1)
  102. parentGroupPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  103. if (parentGroupPanel != null &&
  104. parentGroupPanel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  105. {
  106. parentGroupPanel.Children.Insert(
  107. parentGroupPanel.IndexOfChild(parentGroup != null ? parentGroup : targetModel),
  108. floatingWindow.RootPanel);
  109. }
  110. else if (parentGroupPanel != null)
  111. {
  112. var newParentPanel = new LayoutPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  113. parentGroupPanel.ReplaceChild(parentGroup != null ? parentGroup : targetModel, newParentPanel);
  114. newParentPanel.Children.Add(parentGroup != null ? parentGroup : targetModel);
  115. newParentPanel.Children.Insert(0, floatingWindow.RootPanel);
  116. }
  117. else
  118. {
  119. throw new NotImplementedException();
  120. }
  121. }
  122. break;
  123. #endregion
  124. case DropTargetType.DocumentPaneDockAsAnchorableRight:
  125. #region DropTargetType.DocumentPaneDockAsAnchorableRight
  126. {
  127. if (parentGroupPanel != null &&
  128. parentGroupPanel.ChildrenCount == 1)
  129. parentGroupPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  130. if (parentGroupPanel != null &&
  131. parentGroupPanel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  132. {
  133. parentGroupPanel.Children.Insert(
  134. parentGroupPanel.IndexOfChild(parentGroup != null ? parentGroup : targetModel) + 1,
  135. floatingWindow.RootPanel);
  136. }
  137. else if (parentGroupPanel != null)
  138. {
  139. var newParentPanel = new LayoutPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  140. parentGroupPanel.ReplaceChild(parentGroup != null ? parentGroup : targetModel, newParentPanel);
  141. newParentPanel.Children.Add(parentGroup != null ? parentGroup : targetModel);
  142. newParentPanel.Children.Add(floatingWindow.RootPanel);
  143. }
  144. else
  145. {
  146. throw new NotImplementedException();
  147. }
  148. }
  149. break;
  150. #endregion
  151. }
  152. base.Drop(floatingWindow);
  153. }
  154. public override System.Windows.Media.Geometry GetPreviewPath(OverlayWindow overlayWindow, LayoutFloatingWindow floatingWindowModel)
  155. {
  156. Rect targetScreenRect;
  157. ILayoutDocumentPane targetModel = _targetPane.Model as ILayoutDocumentPane;
  158. var manager = targetModel.Root.Manager;
  159. //ILayoutDocumentPane targetModel = _targetPane.Model as ILayoutDocumentPane;
  160. LayoutDocumentPaneGroup parentGroup;
  161. LayoutPanel parentGroupPanel;
  162. if (!FindParentLayoutDocumentPane(targetModel, out parentGroup, out parentGroupPanel))
  163. return null;
  164. //if (targetModel.Parent is LayoutDocumentPaneGroup)
  165. //{
  166. // var parentGroup = targetModel.Parent as LayoutDocumentPaneGroup;
  167. // var documentPaneGroupControl = manager.FindLogicalChildren<LayoutDocumentPaneGroupControl>().First(d => d.Model == parentGroup);
  168. // targetScreenRect = documentPaneGroupControl.GetScreenArea();
  169. //}
  170. //else
  171. //{
  172. // var documentPaneControl = manager.FindLogicalChildren<LayoutDocumentPaneControl>().First(d => d.Model == targetModel);
  173. // targetScreenRect = documentPaneControl.GetScreenArea();
  174. //}
  175. //var parentPanel = targetModel.FindParent<LayoutPanel>();
  176. var documentPaneControl = manager.FindLogicalChildren<FrameworkElement>().OfType<ILayoutControl>().First(d => parentGroup != null ? d.Model == parentGroup : d.Model == parentGroupPanel) as FrameworkElement;
  177. targetScreenRect = documentPaneControl.GetScreenArea();
  178. switch (Type)
  179. {
  180. case DropTargetType.DocumentPaneDockAsAnchorableBottom:
  181. {
  182. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  183. targetScreenRect.Offset(0.0, targetScreenRect.Height - targetScreenRect.Height / 3.0);
  184. targetScreenRect.Height /= 3.0;
  185. return new RectangleGeometry(targetScreenRect);
  186. }
  187. case DropTargetType.DocumentPaneDockAsAnchorableTop:
  188. {
  189. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  190. targetScreenRect.Height /= 3.0;
  191. return new RectangleGeometry(targetScreenRect);
  192. }
  193. case DropTargetType.DocumentPaneDockAsAnchorableRight:
  194. {
  195. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  196. targetScreenRect.Offset(targetScreenRect.Width - targetScreenRect.Width / 3.0, 0.0);
  197. targetScreenRect.Width /= 3.0;
  198. return new RectangleGeometry(targetScreenRect);
  199. }
  200. case DropTargetType.DocumentPaneDockAsAnchorableLeft:
  201. {
  202. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  203. targetScreenRect.Width /= 3.0;
  204. return new RectangleGeometry(targetScreenRect);
  205. }
  206. }
  207. return null;
  208. }
  209. bool FindParentLayoutDocumentPane(ILayoutDocumentPane documentPane, out LayoutDocumentPaneGroup containerPaneGroup, out LayoutPanel containerPanel)
  210. {
  211. containerPaneGroup = null;
  212. containerPanel = null;
  213. if (documentPane.Parent is LayoutPanel)
  214. {
  215. containerPaneGroup = null;
  216. containerPanel = documentPane.Parent as LayoutPanel;
  217. return true;
  218. }
  219. else if (documentPane.Parent is LayoutDocumentPaneGroup)
  220. {
  221. var currentDocumentPaneGroup = documentPane.Parent as LayoutDocumentPaneGroup;
  222. while (!(currentDocumentPaneGroup.Parent is LayoutPanel))
  223. {
  224. currentDocumentPaneGroup = currentDocumentPaneGroup.Parent as LayoutDocumentPaneGroup;
  225. if (currentDocumentPaneGroup == null)
  226. break;
  227. }
  228. if (currentDocumentPaneGroup == null)
  229. return false;
  230. containerPaneGroup = currentDocumentPaneGroup;
  231. containerPanel = currentDocumentPaneGroup.Parent as LayoutPanel;
  232. return true;
  233. }
  234. return false;
  235. }
  236. }
  237. }