// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2020 OxyPlot contributors
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.Wpf
{
using System;
using System.Windows.Input;
///
/// Base class for WPF PlotView implementations.
///
public abstract partial class PlotViewBase
{
///
/// Called before the event occurs.
///
/// The data for the event.
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Handled)
{
return;
}
var args = new OxyKeyEventArgs { ModifierKeys = Keyboard.GetModifierKeys(), Key = e.Key.Convert() };
e.Handled = this.ActualController.HandleKeyDown(this, args);
}
///
/// Called when the event occurs.
///
/// The data for the event.
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
base.OnManipulationStarted(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleTouchStarted(this, e.ToTouchEventArgs(this));
}
///
/// Called when the event occurs.
///
/// The data for the event.
protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
{
base.OnManipulationDelta(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleTouchDelta(this, e.ToTouchEventArgs(this));
}
///
/// Called when the event occurs.
///
/// The data for the event.
protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
{
base.OnManipulationCompleted(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleTouchCompleted(this, e.ToTouchEventArgs(this));
}
///
/// Called before the event occurs to provide handling for the event in a derived class without attaching a delegate.
///
/// A that contains the event data.
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (e.Handled || !this.IsMouseWheelEnabled)
{
return;
}
e.Handled = this.ActualController.HandleMouseWheel(this, e.ToMouseWheelEventArgs(this));
}
///
/// 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.
///
/// The that contains the event data. This event data reports details about the mouse button that was pressed and the handled state.
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.Handled)
{
return;
}
this.Focus();
this.CaptureMouse();
// store the mouse down point, check it when mouse button is released to determine if the context menu should be shown
this.mouseDownPoint = e.GetPosition(this).ToScreenPoint();
e.Handled = this.ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this));
}
///
/// 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.
///
/// The that contains the event data.
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleMouseMove(this, e.ToMouseEventArgs(this));
}
///
/// 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.
///
/// The that contains the event data. The event data reports that the mouse button was released.
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
if (e.Handled)
{
return;
}
this.ReleaseMouseCapture();
e.Handled = this.ActualController.HandleMouseUp(this, e.ToMouseReleasedEventArgs(this));
// Open the context menu
var p = e.GetPosition(this).ToScreenPoint();
var d = p.DistanceTo(this.mouseDownPoint);
if (this.ContextMenu != null)
{
if (Math.Abs(d) < 1e-8 && e.ChangedButton == MouseButton.Right)
{
// TODO: why is the data context not passed to the context menu??
this.ContextMenu.DataContext = this.DataContext;
this.ContextMenu.PlacementTarget = this;
this.ContextMenu.Visibility = System.Windows.Visibility.Visible;
this.ContextMenu.IsOpen = true;
}
else
{
this.ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
this.ContextMenu.IsOpen = false;
}
}
}
///
/// Invoked when an unhandled attached event is raised on this element. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleMouseEnter(this, e.ToMouseEventArgs(this));
}
///
/// Invoked when an unhandled attached event is raised on this element. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseEnter(e);
if (e.Handled)
{
return;
}
e.Handled = this.ActualController.HandleMouseLeave(this, e.ToMouseEventArgs(this));
}
}
}