1 | using System;
|
---|
2 | using System.Windows;
|
---|
3 | using System.Windows.Input;
|
---|
4 | using System.Windows.Interop;
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace Microsoft.Research.DynamicDataDisplay.Navigation
|
---|
8 | {
|
---|
9 | /// <summary>This class allows convenient navigation around viewport using touchpad on
|
---|
10 | /// some notebooks</summary>
|
---|
11 | public sealed class TouchpadScroll : NavigationBase {
|
---|
12 | public TouchpadScroll() {
|
---|
13 | Loaded += OnLoaded;
|
---|
14 | }
|
---|
15 |
|
---|
16 | private void OnLoaded(object sender, RoutedEventArgs e) {
|
---|
17 | WindowInteropHelper helper = new WindowInteropHelper(Window.GetWindow(this));
|
---|
18 | HwndSource source = HwndSource.FromHwnd(helper.Handle);
|
---|
19 | source.AddHook(OnMessageAppeared);
|
---|
20 | }
|
---|
21 |
|
---|
22 | private IntPtr OnMessageAppeared(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
|
---|
23 | if (msg == WindowsMessages.WM_MOUSEWHEEL) {
|
---|
24 | Point mousePos = MessagesHelper.GetMousePosFromLParam(lParam);
|
---|
25 | mousePos = TranslatePoint(mousePos, this);
|
---|
26 |
|
---|
27 | if (Viewport.Output.Contains(mousePos)) {
|
---|
28 | MouseWheelZoom(MessagesHelper.GetMousePosFromLParam(lParam), MessagesHelper.GetWheelDataFromWParam(wParam));
|
---|
29 | handled = true;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | return IntPtr.Zero;
|
---|
33 | }
|
---|
34 |
|
---|
35 | double wheelZoomSpeed = 1.2;
|
---|
36 | public double WheelZoomSpeed {
|
---|
37 | get { return wheelZoomSpeed; }
|
---|
38 | set { wheelZoomSpeed = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private void MouseWheelZoom(Point mousePos, int wheelRotationDelta) {
|
---|
42 | Point zoomTo = mousePos.ScreenToData(Viewport.Transform);
|
---|
43 |
|
---|
44 | double zoomSpeed = Math.Abs(wheelRotationDelta / Mouse.MouseWheelDeltaForOneLine);
|
---|
45 | zoomSpeed *= wheelZoomSpeed;
|
---|
46 | if (wheelRotationDelta < 0) {
|
---|
47 | zoomSpeed = 1 / zoomSpeed;
|
---|
48 | }
|
---|
49 | Viewport.Visible = Viewport.Visible.Zoom(zoomTo, zoomSpeed);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|