You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.3 KiB

  1. // Copyright (c) 2020 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Linq;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Input;
  23. namespace ICSharpCode.TreeView
  24. {
  25. public class SharpTreeViewItem : ListViewItem
  26. {
  27. static SharpTreeViewItem()
  28. {
  29. DefaultStyleKeyProperty.OverrideMetadata(typeof(SharpTreeViewItem),
  30. new FrameworkPropertyMetadata(typeof(SharpTreeViewItem)));
  31. }
  32. public SharpTreeNode Node {
  33. get { return DataContext as SharpTreeNode; }
  34. }
  35. public SharpTreeNodeView NodeView { get; internal set; }
  36. public SharpTreeView ParentTreeView { get; internal set; }
  37. protected override void OnKeyDown(KeyEventArgs e)
  38. {
  39. switch (e.Key)
  40. {
  41. case Key.F2:
  42. if (Node.IsEditable && ParentTreeView != null && ParentTreeView.SelectedItems.Count == 1 && ParentTreeView.SelectedItems[0] == Node)
  43. {
  44. Node.IsEditing = true;
  45. e.Handled = true;
  46. }
  47. break;
  48. case Key.Escape:
  49. if (Node.IsEditing)
  50. {
  51. Node.IsEditing = false;
  52. e.Handled = true;
  53. }
  54. break;
  55. }
  56. }
  57. protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
  58. {
  59. return new SharpTreeViewItemAutomationPeer(this);
  60. }
  61. #region Mouse
  62. Point startPoint;
  63. bool wasSelected;
  64. bool wasDoubleClick;
  65. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  66. {
  67. wasSelected = IsSelected;
  68. if (!IsSelected)
  69. {
  70. base.OnMouseLeftButtonDown(e);
  71. }
  72. if (Mouse.LeftButton == MouseButtonState.Pressed)
  73. {
  74. startPoint = e.GetPosition(null);
  75. CaptureMouse();
  76. if (e.ClickCount == 2)
  77. {
  78. wasDoubleClick = true;
  79. }
  80. }
  81. }
  82. protected override void OnMouseMove(MouseEventArgs e)
  83. {
  84. if (IsMouseCaptured)
  85. {
  86. var currentPoint = e.GetPosition(null);
  87. if (Math.Abs(currentPoint.X - startPoint.X) >= SystemParameters.MinimumHorizontalDragDistance ||
  88. Math.Abs(currentPoint.Y - startPoint.Y) >= SystemParameters.MinimumVerticalDragDistance)
  89. {
  90. var selection = ParentTreeView.GetTopLevelSelection().ToArray();
  91. if (Node.CanDrag(selection))
  92. {
  93. Node.StartDrag(this, selection);
  94. }
  95. }
  96. }
  97. }
  98. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  99. {
  100. if (wasDoubleClick)
  101. {
  102. wasDoubleClick = false;
  103. Node.ActivateItem(e);
  104. if (!e.Handled)
  105. {
  106. if (!Node.IsRoot || ParentTreeView.ShowRootExpander)
  107. {
  108. Node.IsExpanded = !Node.IsExpanded;
  109. }
  110. }
  111. }
  112. ReleaseMouseCapture();
  113. if (wasSelected)
  114. {
  115. base.OnMouseLeftButtonDown(e);
  116. }
  117. }
  118. protected override void OnMouseUp(MouseButtonEventArgs e)
  119. {
  120. if (e.ChangedButton == MouseButton.Middle)
  121. {
  122. Node.ActivateItemSecondary(e);
  123. }
  124. else
  125. {
  126. base.OnMouseUp(e);
  127. }
  128. }
  129. #endregion
  130. #region Drag and Drop
  131. protected override void OnDragEnter(DragEventArgs e)
  132. {
  133. ParentTreeView.HandleDragEnter(this, e);
  134. }
  135. protected override void OnDragOver(DragEventArgs e)
  136. {
  137. ParentTreeView.HandleDragOver(this, e);
  138. }
  139. protected override void OnDrop(DragEventArgs e)
  140. {
  141. ParentTreeView.HandleDrop(this, e);
  142. }
  143. protected override void OnDragLeave(DragEventArgs e)
  144. {
  145. ParentTreeView.HandleDragLeave(this, e);
  146. }
  147. #endregion
  148. }
  149. }