DropTarget.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. using System.Windows.Threading;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. internal abstract class DropTarget<T> : DropTargetBase, IDropTarget where T : FrameworkElement
  21. {
  22. protected DropTarget(T targetElement, Rect detectionRect, DropTargetType type)
  23. {
  24. _targetElement = targetElement;
  25. _detectionRect = new Rect[] { detectionRect };
  26. _type = type;
  27. }
  28. protected DropTarget(T targetElement, IEnumerable<Rect> detectionRects, DropTargetType type)
  29. {
  30. _targetElement = targetElement;
  31. _detectionRect = detectionRects.ToArray();
  32. _type = type;
  33. }
  34. Rect[] _detectionRect;
  35. public Rect[] DetectionRects
  36. {
  37. get { return _detectionRect; }
  38. }
  39. T _targetElement;
  40. public T TargetElement
  41. {
  42. get { return _targetElement; }
  43. }
  44. DropTargetType _type;
  45. public DropTargetType Type
  46. {
  47. get { return _type; }
  48. }
  49. protected virtual void Drop(LayoutAnchorableFloatingWindow floatingWindow)
  50. { }
  51. protected virtual void Drop(LayoutDocumentFloatingWindow floatingWindow)
  52. { }
  53. public void Drop(LayoutFloatingWindow floatingWindow)
  54. {
  55. var root = floatingWindow.Root;
  56. var currentActiveContent = floatingWindow.Root.ActiveContent;
  57. var fwAsAnchorable = floatingWindow as LayoutAnchorableFloatingWindow;
  58. if (fwAsAnchorable != null)
  59. {
  60. this.Drop(fwAsAnchorable);
  61. }
  62. else
  63. {
  64. var fwAsDocument = floatingWindow as LayoutDocumentFloatingWindow;
  65. this.Drop(fwAsDocument);
  66. }
  67. Dispatcher.BeginInvoke(new Action(() =>
  68. {
  69. currentActiveContent.IsSelected = false;
  70. currentActiveContent.IsActive = false;
  71. currentActiveContent.IsActive = true;
  72. }), DispatcherPriority.Background);
  73. }
  74. public virtual bool HitTest(Point dragPoint)
  75. {
  76. return _detectionRect.Any(dr => dr.Contains(dragPoint));
  77. }
  78. public abstract Geometry GetPreviewPath(OverlayWindow overlayWindow, LayoutFloatingWindow floatingWindow);
  79. public void DragEnter()
  80. {
  81. SetIsDraggingOver(TargetElement, true);
  82. }
  83. public void DragLeave()
  84. {
  85. SetIsDraggingOver(TargetElement, false);
  86. }
  87. }
  88. }