AnchorablePaneDropTarget.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 Xceed.Wpf.AvalonDock.Layout;
  17. namespace Xceed.Wpf.AvalonDock.Controls
  18. {
  19. internal class AnchorablePaneDropTarget : DropTarget<LayoutAnchorablePaneControl>
  20. {
  21. internal AnchorablePaneDropTarget(LayoutAnchorablePaneControl paneControl, Rect detectionRect, DropTargetType type)
  22. : base(paneControl, detectionRect, type)
  23. {
  24. _targetPane = paneControl;
  25. }
  26. internal AnchorablePaneDropTarget(LayoutAnchorablePaneControl paneControl, Rect detectionRect, DropTargetType type, int tabIndex)
  27. : base(paneControl, detectionRect, type)
  28. {
  29. _targetPane = paneControl;
  30. _tabIndex = tabIndex;
  31. }
  32. LayoutAnchorablePaneControl _targetPane;
  33. int _tabIndex = -1;
  34. protected override void Drop(LayoutAnchorableFloatingWindow floatingWindow)
  35. {
  36. ILayoutAnchorablePane targetModel = _targetPane.Model as ILayoutAnchorablePane;
  37. LayoutAnchorable anchorableActive = floatingWindow.Descendents().OfType<LayoutAnchorable>().FirstOrDefault();
  38. switch (Type)
  39. {
  40. case DropTargetType.AnchorablePaneDockBottom:
  41. #region DropTargetType.AnchorablePaneDockBottom
  42. {
  43. var parentModel = targetModel.Parent as ILayoutGroup;
  44. var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
  45. int insertToIndex = parentModel.IndexOfChild(targetModel);
  46. if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Vertical &&
  47. parentModel.ChildrenCount == 1)
  48. parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Vertical;
  49. if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Vertical)
  50. {
  51. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  52. if (layoutAnchorablePaneGroup != null &&
  53. (layoutAnchorablePaneGroup.Children.Count == 1 ||
  54. layoutAnchorablePaneGroup.Orientation == System.Windows.Controls.Orientation.Vertical))
  55. {
  56. var anchorablesToMove = layoutAnchorablePaneGroup.Children.ToArray();
  57. for (int i = 0; i < anchorablesToMove.Length; i++)
  58. parentModel.InsertChildAt(insertToIndex + 1 + i, anchorablesToMove[i]);
  59. }
  60. else
  61. parentModel.InsertChildAt(insertToIndex + 1, floatingWindow.RootPanel);
  62. }
  63. else
  64. {
  65. var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
  66. var newOrientedPanel = new LayoutAnchorablePaneGroup()
  67. {
  68. Orientation = System.Windows.Controls.Orientation.Vertical,
  69. DockWidth = targetModelAsPositionableElement.DockWidth,
  70. DockHeight = targetModelAsPositionableElement.DockHeight,
  71. };
  72. parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
  73. newOrientedPanel.Children.Add(targetModel);
  74. newOrientedPanel.Children.Add(floatingWindow.RootPanel);
  75. }
  76. }
  77. break;
  78. #endregion
  79. case DropTargetType.AnchorablePaneDockTop:
  80. #region DropTargetType.AnchorablePaneDockTop
  81. {
  82. var parentModel = targetModel.Parent as ILayoutGroup;
  83. var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
  84. int insertToIndex = parentModel.IndexOfChild(targetModel);
  85. if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Vertical &&
  86. parentModel.ChildrenCount == 1)
  87. parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Vertical;
  88. if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Vertical)
  89. {
  90. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  91. if (layoutAnchorablePaneGroup != null &&
  92. (layoutAnchorablePaneGroup.Children.Count == 1 ||
  93. layoutAnchorablePaneGroup.Orientation == System.Windows.Controls.Orientation.Vertical))
  94. {
  95. var anchorablesToMove = layoutAnchorablePaneGroup.Children.ToArray();
  96. for (int i = 0; i < anchorablesToMove.Length; i++)
  97. parentModel.InsertChildAt(insertToIndex + i, anchorablesToMove[i]);
  98. }
  99. else
  100. parentModel.InsertChildAt(insertToIndex, floatingWindow.RootPanel);
  101. }
  102. else
  103. {
  104. var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
  105. var newOrientedPanel = new LayoutAnchorablePaneGroup()
  106. {
  107. Orientation = System.Windows.Controls.Orientation.Vertical,
  108. DockWidth = targetModelAsPositionableElement.DockWidth,
  109. DockHeight = targetModelAsPositionableElement.DockHeight,
  110. };
  111. parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
  112. //the floating window must be added after the target modal as it could be raise a CollectGarbage call
  113. newOrientedPanel.Children.Add(targetModel);
  114. newOrientedPanel.Children.Insert(0, floatingWindow.RootPanel);
  115. }
  116. }
  117. break;
  118. #endregion
  119. case DropTargetType.AnchorablePaneDockLeft:
  120. #region DropTargetType.AnchorablePaneDockLeft
  121. {
  122. var parentModel = targetModel.Parent as ILayoutGroup;
  123. var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
  124. int insertToIndex = parentModel.IndexOfChild(targetModel);
  125. if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Horizontal &&
  126. parentModel.ChildrenCount == 1)
  127. parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Horizontal;
  128. if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Horizontal)
  129. {
  130. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  131. if (layoutAnchorablePaneGroup != null &&
  132. (layoutAnchorablePaneGroup.Children.Count == 1 ||
  133. layoutAnchorablePaneGroup.Orientation == System.Windows.Controls.Orientation.Horizontal))
  134. {
  135. var anchorablesToMove = layoutAnchorablePaneGroup.Children.ToArray();
  136. for (int i = 0; i < anchorablesToMove.Length; i++)
  137. parentModel.InsertChildAt(insertToIndex + i, anchorablesToMove[i]);
  138. }
  139. else
  140. parentModel.InsertChildAt(insertToIndex, floatingWindow.RootPanel);
  141. }
  142. else
  143. {
  144. var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
  145. var newOrientedPanel = new LayoutAnchorablePaneGroup()
  146. {
  147. Orientation = System.Windows.Controls.Orientation.Horizontal,
  148. DockWidth = targetModelAsPositionableElement.DockWidth,
  149. DockHeight = targetModelAsPositionableElement.DockHeight,
  150. };
  151. parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
  152. //the floating window must be added after the target modal as it could be raise a CollectGarbage call
  153. newOrientedPanel.Children.Add(targetModel);
  154. newOrientedPanel.Children.Insert(0, floatingWindow.RootPanel);
  155. }
  156. }
  157. break;
  158. #endregion
  159. case DropTargetType.AnchorablePaneDockRight:
  160. #region DropTargetType.AnchorablePaneDockRight
  161. {
  162. var parentModel = targetModel.Parent as ILayoutGroup;
  163. var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
  164. int insertToIndex = parentModel.IndexOfChild(targetModel);
  165. if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Horizontal &&
  166. parentModel.ChildrenCount == 1)
  167. parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Horizontal;
  168. if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Horizontal)
  169. {
  170. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  171. if (layoutAnchorablePaneGroup != null &&
  172. (layoutAnchorablePaneGroup.Children.Count == 1 ||
  173. layoutAnchorablePaneGroup.Orientation == System.Windows.Controls.Orientation.Horizontal))
  174. {
  175. var anchorablesToMove = layoutAnchorablePaneGroup.Children.ToArray();
  176. for (int i = 0; i < anchorablesToMove.Length; i++)
  177. parentModel.InsertChildAt(insertToIndex + 1 + i, anchorablesToMove[i]);
  178. }
  179. else
  180. parentModel.InsertChildAt(insertToIndex + 1, floatingWindow.RootPanel);
  181. }
  182. else
  183. {
  184. var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
  185. var newOrientedPanel = new LayoutAnchorablePaneGroup()
  186. {
  187. Orientation = System.Windows.Controls.Orientation.Horizontal,
  188. DockWidth = targetModelAsPositionableElement.DockWidth,
  189. DockHeight = targetModelAsPositionableElement.DockHeight,
  190. };
  191. parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
  192. newOrientedPanel.Children.Add(targetModel);
  193. newOrientedPanel.Children.Add(floatingWindow.RootPanel);
  194. }
  195. }
  196. break;
  197. #endregion
  198. case DropTargetType.AnchorablePaneDockInside:
  199. #region DropTargetType.AnchorablePaneDockInside
  200. {
  201. var paneModel = targetModel as LayoutAnchorablePane;
  202. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  203. int i = _tabIndex == -1 ? 0 : _tabIndex;
  204. foreach (var anchorableToImport in
  205. layoutAnchorablePaneGroup.Descendents().OfType<LayoutAnchorable>().ToArray())
  206. {
  207. paneModel.Children.Insert(i, anchorableToImport);
  208. i++;
  209. }
  210. }
  211. break;
  212. #endregion
  213. }
  214. anchorableActive.IsActive = true;
  215. base.Drop(floatingWindow);
  216. }
  217. public override System.Windows.Media.Geometry GetPreviewPath(OverlayWindow overlayWindow, LayoutFloatingWindow floatingWindowModel)
  218. {
  219. //var anchorablePaneDropTarget = target as AnchorablePaneDropTarget;
  220. var anchorableFloatingWindowModel = floatingWindowModel as LayoutAnchorableFloatingWindow;
  221. var layoutAnchorablePane = anchorableFloatingWindowModel.RootPanel as ILayoutPositionableElement;
  222. var layoutAnchorablePaneWithActualSize = anchorableFloatingWindowModel.RootPanel as ILayoutPositionableElementWithActualSize;
  223. switch (Type)
  224. {
  225. case DropTargetType.AnchorablePaneDockBottom:
  226. {
  227. var targetScreenRect = TargetElement.GetScreenArea();
  228. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  229. targetScreenRect.Offset(0.0, targetScreenRect.Height / 2.0);
  230. targetScreenRect.Height /= 2.0;
  231. return new RectangleGeometry(targetScreenRect);
  232. }
  233. case DropTargetType.AnchorablePaneDockTop:
  234. {
  235. var targetScreenRect = TargetElement.GetScreenArea();
  236. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  237. targetScreenRect.Height /= 2.0;
  238. return new RectangleGeometry(targetScreenRect);
  239. }
  240. case DropTargetType.AnchorablePaneDockLeft:
  241. {
  242. var targetScreenRect = TargetElement.GetScreenArea();
  243. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  244. targetScreenRect.Width /= 2.0;
  245. return new RectangleGeometry(targetScreenRect);
  246. }
  247. case DropTargetType.AnchorablePaneDockRight:
  248. {
  249. var targetScreenRect = TargetElement.GetScreenArea();
  250. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  251. targetScreenRect.Offset(targetScreenRect.Width / 2.0, 0.0);
  252. targetScreenRect.Width /= 2.0;
  253. return new RectangleGeometry(targetScreenRect);
  254. }
  255. case DropTargetType.AnchorablePaneDockInside:
  256. {
  257. var targetScreenRect = TargetElement.GetScreenArea();
  258. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  259. if (_tabIndex == -1)
  260. {
  261. return new RectangleGeometry(targetScreenRect);
  262. }
  263. else
  264. {
  265. var translatedDetectionRect = new Rect(DetectionRects[0].TopLeft, DetectionRects[0].BottomRight);
  266. translatedDetectionRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  267. var pathFigure = new PathFigure();
  268. pathFigure.StartPoint = targetScreenRect.TopLeft;
  269. pathFigure.Segments.Add(new LineSegment() { Point = new Point(targetScreenRect.Left, translatedDetectionRect.Top) });
  270. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.TopLeft });
  271. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.BottomLeft });
  272. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.BottomRight });
  273. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.TopRight });
  274. pathFigure.Segments.Add(new LineSegment() { Point = new Point(targetScreenRect.Right, translatedDetectionRect.Top) });
  275. pathFigure.Segments.Add(new LineSegment() { Point = targetScreenRect.TopRight });
  276. pathFigure.IsClosed = true;
  277. pathFigure.IsFilled = true;
  278. pathFigure.Freeze();
  279. return new PathGeometry(new PathFigure[] { pathFigure });
  280. }
  281. }
  282. }
  283. return null;
  284. }
  285. }
  286. }