PlotViewBase.Events.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotViewBase.Events.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace OxyPlot.Wpf
  7. {
  8. using System;
  9. using System.Windows.Input;
  10. /// <summary>
  11. /// Base class for WPF PlotView implementations.
  12. /// </summary>
  13. public abstract partial class PlotViewBase
  14. {
  15. /// <summary>
  16. /// Called before the <see cref="E:System.Windows.UIElement.KeyDown" /> event occurs.
  17. /// </summary>
  18. /// <param name="e">The data for the event.</param>
  19. protected override void OnKeyDown(KeyEventArgs e)
  20. {
  21. base.OnKeyDown(e);
  22. if (e.Handled)
  23. {
  24. return;
  25. }
  26. var args = new OxyKeyEventArgs { ModifierKeys = Keyboard.GetModifierKeys(), Key = e.Key.Convert() };
  27. e.Handled = this.ActualController.HandleKeyDown(this, args);
  28. }
  29. /// <summary>
  30. /// Called when the <see cref="E:System.Windows.UIElement.ManipulationStarted" /> event occurs.
  31. /// </summary>
  32. /// <param name="e">The data for the event.</param>
  33. protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
  34. {
  35. base.OnManipulationStarted(e);
  36. if (e.Handled)
  37. {
  38. return;
  39. }
  40. e.Handled = this.ActualController.HandleTouchStarted(this, e.ToTouchEventArgs(this));
  41. }
  42. /// <summary>
  43. /// Called when the <see cref="E:System.Windows.UIElement.ManipulationDelta" /> event occurs.
  44. /// </summary>
  45. /// <param name="e">The data for the event.</param>
  46. protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
  47. {
  48. base.OnManipulationDelta(e);
  49. if (e.Handled)
  50. {
  51. return;
  52. }
  53. e.Handled = this.ActualController.HandleTouchDelta(this, e.ToTouchEventArgs(this));
  54. }
  55. /// <summary>
  56. /// Called when the <see cref="E:System.Windows.UIElement.ManipulationCompleted" /> event occurs.
  57. /// </summary>
  58. /// <param name="e">The data for the event.</param>
  59. protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
  60. {
  61. base.OnManipulationCompleted(e);
  62. if (e.Handled)
  63. {
  64. return;
  65. }
  66. e.Handled = this.ActualController.HandleTouchCompleted(this, e.ToTouchEventArgs(this));
  67. }
  68. /// <summary>
  69. /// Called before the <see cref="E:System.Windows.UIElement.MouseWheel" /> event occurs to provide handling for the event in a derived class without attaching a delegate.
  70. /// </summary>
  71. /// <param name="e">A <see cref="T:System.Windows.Input.MouseWheelEventArgs" /> that contains the event data.</param>
  72. protected override void OnMouseWheel(MouseWheelEventArgs e)
  73. {
  74. base.OnMouseWheel(e);
  75. if (e.Handled || !this.IsMouseWheelEnabled)
  76. {
  77. return;
  78. }
  79. e.Handled = this.ActualController.HandleMouseWheel(this, e.ToMouseWheelEventArgs(this));
  80. }
  81. /// <summary>
  82. /// Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  83. /// </summary>
  84. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. This event data reports details about the mouse button that was pressed and the handled state.</param>
  85. protected override void OnMouseDown(MouseButtonEventArgs e)
  86. {
  87. base.OnMouseDown(e);
  88. if (e.Handled)
  89. {
  90. return;
  91. }
  92. this.Focus();
  93. this.CaptureMouse();
  94. // store the mouse down point, check it when mouse button is released to determine if the context menu should be shown
  95. this.mouseDownPoint = e.GetPosition(this).ToScreenPoint();
  96. e.Handled = this.ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this));
  97. }
  98. /// <summary>
  99. /// Invoked when an unhandled MouseMove attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  100. /// </summary>
  101. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  102. protected override void OnMouseMove(MouseEventArgs e)
  103. {
  104. base.OnMouseMove(e);
  105. if (e.Handled)
  106. {
  107. return;
  108. }
  109. e.Handled = this.ActualController.HandleMouseMove(this, e.ToMouseEventArgs(this));
  110. }
  111. /// <summary>
  112. /// Invoked when an unhandled MouseUp routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  113. /// </summary>
  114. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. The event data reports that the mouse button was released.</param>
  115. protected override void OnMouseUp(MouseButtonEventArgs e)
  116. {
  117. base.OnMouseUp(e);
  118. if (e.Handled)
  119. {
  120. return;
  121. }
  122. this.ReleaseMouseCapture();
  123. e.Handled = this.ActualController.HandleMouseUp(this, e.ToMouseReleasedEventArgs(this));
  124. // Open the context menu
  125. var p = e.GetPosition(this).ToScreenPoint();
  126. var d = p.DistanceTo(this.mouseDownPoint);
  127. if (this.ContextMenu != null)
  128. {
  129. if (Math.Abs(d) < 1e-8 && e.ChangedButton == MouseButton.Right)
  130. {
  131. // TODO: why is the data context not passed to the context menu??
  132. this.ContextMenu.DataContext = this.DataContext;
  133. this.ContextMenu.PlacementTarget = this;
  134. this.ContextMenu.Visibility = System.Windows.Visibility.Visible;
  135. this.ContextMenu.IsOpen = true;
  136. }
  137. else
  138. {
  139. this.ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
  140. this.ContextMenu.IsOpen = false;
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseEnter" /> attached event is raised on this element. Implement this method to add class handling for this event.
  146. /// </summary>
  147. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  148. protected override void OnMouseEnter(MouseEventArgs e)
  149. {
  150. base.OnMouseEnter(e);
  151. if (e.Handled)
  152. {
  153. return;
  154. }
  155. e.Handled = this.ActualController.HandleMouseEnter(this, e.ToMouseEventArgs(this));
  156. }
  157. /// <summary>
  158. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseLeave" /> attached event is raised on this element. Implement this method to add class handling for this event.
  159. /// </summary>
  160. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  161. protected override void OnMouseLeave(MouseEventArgs e)
  162. {
  163. base.OnMouseEnter(e);
  164. if (e.Handled)
  165. {
  166. return;
  167. }
  168. e.Handled = this.ActualController.HandleMouseLeave(this, e.ToMouseEventArgs(this));
  169. }
  170. }
  171. }