DocumentPaneDropTarget.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 DocumentPaneDropTarget : DropTarget<LayoutDocumentPaneControl>
  21. {
  22. internal DocumentPaneDropTarget(LayoutDocumentPaneControl paneControl, Rect detectionRect, DropTargetType type)
  23. : base(paneControl, detectionRect, type)
  24. {
  25. _targetPane = paneControl;
  26. }
  27. internal DocumentPaneDropTarget(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(LayoutDocumentFloatingWindow floatingWindow)
  36. {
  37. ILayoutDocumentPane targetModel = _targetPane.Model as ILayoutDocumentPane;
  38. switch (Type)
  39. {
  40. case DropTargetType.DocumentPaneDockBottom:
  41. #region DropTargetType.DocumentPaneDockBottom
  42. {
  43. var newLayoutDocumentPane = new LayoutDocumentPane(floatingWindow.RootDocument);
  44. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  45. if (parentModel == null)
  46. {
  47. var parentContainer = targetModel.Parent as ILayoutContainer;
  48. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Vertical};
  49. parentContainer.ReplaceChild(targetModel, newParentModel);
  50. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  51. newParentModel.Children.Add(newLayoutDocumentPane);
  52. }
  53. else
  54. {
  55. var manager = parentModel.Root.Manager;
  56. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Vertical)
  57. {
  58. parentModel.Orientation = System.Windows.Controls.Orientation.Vertical;
  59. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  60. parentModel.Children.Insert(targetPaneIndex + 1, newLayoutDocumentPane);
  61. }
  62. else
  63. {
  64. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  65. newChildGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
  66. parentModel.ReplaceChild(targetModel, newChildGroup);
  67. newChildGroup.Children.Add(targetModel);
  68. newChildGroup.Children.Add(newLayoutDocumentPane);
  69. }
  70. }
  71. }
  72. break;
  73. #endregion
  74. case DropTargetType.DocumentPaneDockTop:
  75. #region DropTargetType.DocumentPaneDockTop
  76. {
  77. var newLayoutDocumentPane = new LayoutDocumentPane(floatingWindow.RootDocument);
  78. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  79. if (parentModel == null)
  80. {
  81. var parentContainer = targetModel.Parent as ILayoutContainer;
  82. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Vertical };
  83. parentContainer.ReplaceChild(targetModel, newParentModel);
  84. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  85. newParentModel.Children.Insert(0, newLayoutDocumentPane);
  86. }
  87. else
  88. {
  89. var manager = parentModel.Root.Manager;
  90. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Vertical)
  91. {
  92. parentModel.Orientation = System.Windows.Controls.Orientation.Vertical;
  93. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  94. parentModel.Children.Insert(targetPaneIndex, newLayoutDocumentPane);
  95. }
  96. else
  97. {
  98. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  99. newChildGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
  100. parentModel.ReplaceChild(targetModel, newChildGroup);
  101. newChildGroup.Children.Add(newLayoutDocumentPane);
  102. newChildGroup.Children.Add(targetModel);
  103. }
  104. }
  105. }
  106. break;
  107. #endregion
  108. case DropTargetType.DocumentPaneDockLeft:
  109. #region DropTargetType.DocumentPaneDockLeft
  110. {
  111. var newLayoutDocumentPane = new LayoutDocumentPane(floatingWindow.RootDocument);
  112. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  113. if (parentModel == null)
  114. {
  115. var parentContainer = targetModel.Parent as ILayoutContainer;
  116. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  117. parentContainer.ReplaceChild(targetModel, newParentModel);
  118. newParentModel.Children.Add(targetModel);
  119. newParentModel.Children.Insert(0, newLayoutDocumentPane);
  120. }
  121. else
  122. {
  123. var manager = parentModel.Root.Manager;
  124. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  125. {
  126. parentModel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  127. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  128. parentModel.Children.Insert(targetPaneIndex, newLayoutDocumentPane);
  129. }
  130. else
  131. {
  132. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  133. newChildGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
  134. parentModel.ReplaceChild(targetModel, newChildGroup);
  135. newChildGroup.Children.Add(newLayoutDocumentPane);
  136. newChildGroup.Children.Add(targetModel);
  137. }
  138. }
  139. }
  140. break;
  141. #endregion
  142. case DropTargetType.DocumentPaneDockRight:
  143. #region DropTargetType.DocumentPaneDockRight
  144. {
  145. var newLayoutDocumentPane = new LayoutDocumentPane(floatingWindow.RootDocument);
  146. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  147. if (parentModel == null)
  148. {
  149. var parentContainer = targetModel.Parent as ILayoutContainer;
  150. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  151. parentContainer.ReplaceChild(targetModel, newParentModel);
  152. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  153. newParentModel.Children.Add(newLayoutDocumentPane);
  154. }
  155. else
  156. {
  157. var manager = parentModel.Root.Manager;
  158. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  159. {
  160. parentModel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  161. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  162. parentModel.Children.Insert(targetPaneIndex + 1, newLayoutDocumentPane);
  163. }
  164. else
  165. {
  166. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  167. newChildGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
  168. parentModel.ReplaceChild(targetModel, newChildGroup);
  169. newChildGroup.Children.Add(targetModel);
  170. newChildGroup.Children.Add(newLayoutDocumentPane);
  171. }
  172. }
  173. }
  174. break;
  175. #endregion
  176. case DropTargetType.DocumentPaneDockInside:
  177. #region DropTargetType.DocumentPaneDockInside
  178. {
  179. var paneModel = targetModel as LayoutDocumentPane;
  180. var sourceModel = floatingWindow.RootDocument;
  181. int i = _tabIndex == -1 ? 0 : _tabIndex;
  182. sourceModel.IsActive = false;
  183. paneModel.Children.Insert(i, sourceModel);
  184. sourceModel.IsActive = true;
  185. }
  186. break;
  187. #endregion
  188. }
  189. base.Drop(floatingWindow);
  190. }
  191. protected override void Drop(LayoutAnchorableFloatingWindow floatingWindow)
  192. {
  193. ILayoutDocumentPane targetModel = _targetPane.Model as ILayoutDocumentPane;
  194. switch (Type)
  195. {
  196. case DropTargetType.DocumentPaneDockBottom:
  197. #region DropTargetType.DocumentPaneDockBottom
  198. {
  199. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  200. var newLayoutDocumentPane = new LayoutDocumentPane();
  201. if (parentModel == null)
  202. {
  203. var parentContainer = targetModel.Parent as ILayoutContainer;
  204. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Vertical };
  205. parentContainer.ReplaceChild(targetModel, newParentModel);
  206. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  207. newParentModel.Children.Add(newLayoutDocumentPane);
  208. }
  209. else
  210. {
  211. var manager = parentModel.Root.Manager;
  212. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Vertical)
  213. {
  214. parentModel.Orientation = System.Windows.Controls.Orientation.Vertical;
  215. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  216. parentModel.Children.Insert(targetPaneIndex + 1, newLayoutDocumentPane);
  217. }
  218. else
  219. {
  220. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  221. newChildGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
  222. parentModel.ReplaceChild(targetModel, newChildGroup);
  223. newChildGroup.Children.Add(targetModel);
  224. newChildGroup.Children.Add(newLayoutDocumentPane);
  225. }
  226. }
  227. foreach (var cntToTransfer in floatingWindow.RootPanel.Descendents().OfType<LayoutAnchorable>().ToArray())
  228. newLayoutDocumentPane.Children.Add(cntToTransfer);
  229. }
  230. break;
  231. #endregion
  232. case DropTargetType.DocumentPaneDockTop:
  233. #region DropTargetType.DocumentPaneDockTop
  234. {
  235. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  236. var newLayoutDocumentPane = new LayoutDocumentPane();
  237. if (parentModel == null)
  238. {
  239. var parentContainer = targetModel.Parent as ILayoutContainer;
  240. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Vertical };
  241. parentContainer.ReplaceChild(targetModel, newParentModel);
  242. newParentModel.Children.Add(newLayoutDocumentPane);
  243. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  244. }
  245. else
  246. {
  247. var manager = parentModel.Root.Manager;
  248. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Vertical)
  249. {
  250. parentModel.Orientation = System.Windows.Controls.Orientation.Vertical;
  251. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  252. parentModel.Children.Insert(targetPaneIndex, newLayoutDocumentPane);
  253. }
  254. else
  255. {
  256. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  257. newChildGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
  258. parentModel.ReplaceChild(targetModel, newChildGroup);
  259. newChildGroup.Children.Add(newLayoutDocumentPane);
  260. newChildGroup.Children.Add(targetModel);
  261. }
  262. }
  263. foreach (var cntToTransfer in floatingWindow.RootPanel.Descendents().OfType<LayoutAnchorable>().ToArray())
  264. newLayoutDocumentPane.Children.Add(cntToTransfer);
  265. }
  266. break;
  267. #endregion
  268. case DropTargetType.DocumentPaneDockLeft:
  269. #region DropTargetType.DocumentPaneDockLeft
  270. {
  271. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  272. var newLayoutDocumentPane = new LayoutDocumentPane();
  273. if (parentModel == null)
  274. {
  275. var parentContainer = targetModel.Parent as ILayoutContainer;
  276. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  277. parentContainer.ReplaceChild(targetModel, newParentModel);
  278. newParentModel.Children.Add(newLayoutDocumentPane);
  279. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  280. }
  281. else
  282. {
  283. var manager = parentModel.Root.Manager;
  284. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  285. {
  286. parentModel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  287. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  288. parentModel.Children.Insert(targetPaneIndex, newLayoutDocumentPane);
  289. }
  290. else
  291. {
  292. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  293. newChildGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
  294. parentModel.ReplaceChild(targetModel, newChildGroup);
  295. newChildGroup.Children.Add(newLayoutDocumentPane);
  296. newChildGroup.Children.Add(targetModel);
  297. }
  298. }
  299. foreach (var cntToTransfer in floatingWindow.RootPanel.Descendents().OfType<LayoutAnchorable>().ToArray())
  300. newLayoutDocumentPane.Children.Add(cntToTransfer);
  301. }
  302. break;
  303. #endregion
  304. case DropTargetType.DocumentPaneDockRight:
  305. #region DropTargetType.DocumentPaneDockRight
  306. {
  307. var parentModel = targetModel.Parent as LayoutDocumentPaneGroup;
  308. var newLayoutDocumentPane = new LayoutDocumentPane();
  309. if (parentModel == null)
  310. {
  311. var parentContainer = targetModel.Parent as ILayoutContainer;
  312. var newParentModel = new LayoutDocumentPaneGroup() { Orientation = System.Windows.Controls.Orientation.Horizontal };
  313. parentContainer.ReplaceChild(targetModel, newParentModel);
  314. newParentModel.Children.Add(targetModel as LayoutDocumentPane);
  315. newParentModel.Children.Add(newLayoutDocumentPane);
  316. }
  317. else
  318. {
  319. var manager = parentModel.Root.Manager;
  320. if (!manager.AllowMixedOrientation || parentModel.Orientation == System.Windows.Controls.Orientation.Horizontal)
  321. {
  322. parentModel.Orientation = System.Windows.Controls.Orientation.Horizontal;
  323. int targetPaneIndex = parentModel.IndexOfChild(targetModel);
  324. parentModel.Children.Insert(targetPaneIndex + 1, newLayoutDocumentPane);
  325. }
  326. else
  327. {
  328. LayoutDocumentPaneGroup newChildGroup = new LayoutDocumentPaneGroup();
  329. newChildGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
  330. parentModel.ReplaceChild(targetModel, newChildGroup);
  331. newChildGroup.Children.Add(targetModel);
  332. newChildGroup.Children.Add(newLayoutDocumentPane);
  333. }
  334. }
  335. foreach (var cntToTransfer in floatingWindow.RootPanel.Descendents().OfType<LayoutAnchorable>().ToArray())
  336. newLayoutDocumentPane.Children.Add(cntToTransfer);
  337. }
  338. break;
  339. #endregion
  340. case DropTargetType.DocumentPaneDockInside:
  341. #region DropTargetType.DocumentPaneDockInside
  342. {
  343. var paneModel = targetModel as LayoutDocumentPane;
  344. var layoutAnchorablePaneGroup = floatingWindow.RootPanel as LayoutAnchorablePaneGroup;
  345. int i = _tabIndex == -1 ? 0 : _tabIndex;
  346. LayoutAnchorable anchorableToActivate = null;
  347. foreach (var anchorableToImport in layoutAnchorablePaneGroup.Descendents().OfType<LayoutAnchorable>().ToArray())
  348. {
  349. paneModel.Children.Insert(i, anchorableToImport);
  350. i++;
  351. anchorableToActivate = anchorableToImport;
  352. }
  353. anchorableToActivate.IsActive = true;
  354. }
  355. break;
  356. #endregion
  357. }
  358. base.Drop(floatingWindow);
  359. }
  360. public override System.Windows.Media.Geometry GetPreviewPath(OverlayWindow overlayWindow, LayoutFloatingWindow floatingWindowModel)
  361. {
  362. switch (Type)
  363. {
  364. case DropTargetType.DocumentPaneDockInside:
  365. {
  366. var targetScreenRect = TargetElement.GetScreenArea();
  367. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  368. if (_tabIndex == -1)
  369. {
  370. return new RectangleGeometry(targetScreenRect);
  371. }
  372. else
  373. {
  374. var translatedDetectionRect = new Rect(DetectionRects[0].TopLeft, DetectionRects[0].BottomRight);
  375. translatedDetectionRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  376. var pathFigure = new PathFigure();
  377. pathFigure.StartPoint = targetScreenRect.BottomRight;
  378. pathFigure.Segments.Add(new LineSegment() { Point = new Point(targetScreenRect.Right, translatedDetectionRect.Bottom) });
  379. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.BottomRight });
  380. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.TopRight });
  381. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.TopLeft });
  382. pathFigure.Segments.Add(new LineSegment() { Point = translatedDetectionRect.BottomLeft });
  383. pathFigure.Segments.Add(new LineSegment() { Point = new Point(targetScreenRect.Left, translatedDetectionRect.Bottom) });
  384. pathFigure.Segments.Add(new LineSegment() { Point = targetScreenRect.BottomLeft });
  385. pathFigure.IsClosed = true;
  386. pathFigure.IsFilled = true;
  387. pathFigure.Freeze();
  388. return new PathGeometry(new PathFigure[] { pathFigure });
  389. }
  390. }
  391. case DropTargetType.DocumentPaneDockBottom:
  392. {
  393. var targetScreenRect = TargetElement.GetScreenArea();
  394. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  395. targetScreenRect.Offset(0.0, targetScreenRect.Height / 2.0);
  396. targetScreenRect.Height /= 2.0;
  397. return new RectangleGeometry(targetScreenRect);
  398. }
  399. case DropTargetType.DocumentPaneDockTop:
  400. {
  401. var targetScreenRect = TargetElement.GetScreenArea();
  402. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  403. targetScreenRect.Height /= 2.0;
  404. return new RectangleGeometry(targetScreenRect);
  405. }
  406. case DropTargetType.DocumentPaneDockLeft:
  407. {
  408. var targetScreenRect = TargetElement.GetScreenArea();
  409. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  410. targetScreenRect.Width /= 2.0;
  411. return new RectangleGeometry(targetScreenRect);
  412. }
  413. case DropTargetType.DocumentPaneDockRight:
  414. {
  415. var targetScreenRect = TargetElement.GetScreenArea();
  416. targetScreenRect.Offset(-overlayWindow.Left, -overlayWindow.Top);
  417. targetScreenRect.Offset(targetScreenRect.Width / 2.0, 0.0);
  418. targetScreenRect.Width /= 2.0;
  419. return new RectangleGeometry(targetScreenRect);
  420. }
  421. }
  422. return null;
  423. }
  424. }
  425. }