From d2c52078dc8bf1f6f69b9f1693860d6345686767 Mon Sep 17 00:00:00 2001
From: Robin Dunn <robin@alldunn.com>
Date: Fri, 4 May 2001 07:26:30 +0000
Subject: [PATCH] Added mouse wheel support for MSW.  It generates a
 wxMouseEvent with a type of wxEVT_MOUSEWHEEL.

wxMouseEvent has new members and accessors to transport mouse wheel
rotation data.  New members and accessors are documented.

wxGenericScrolledWindow modified to turn mouse wheel events into
scroll actions.

Added wxUSE_MOUSEWHEEL to setup0.h to allow disabling the platform
specific parts.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9990 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/mouseevt.tex    | 66 +++++++++++++++++++++++++++++------
 include/wx/event.h            | 28 ++++++++++++++-
 include/wx/generic/scrolwin.h |  2 ++
 include/wx/msw/setup0.h       |  9 +++--
 include/wx/msw/window.h       |  1 +
 src/common/event.cpp          |  4 +++
 src/generic/scrolwin.cpp      | 28 +++++++++++++--
 src/msw/window.cpp            | 41 ++++++++++++++++++++++
 8 files changed, 162 insertions(+), 17 deletions(-)

diff --git a/docs/latex/wx/mouseevt.tex b/docs/latex/wx/mouseevt.tex
index 423811a297..eff02f8b6d 100644
--- a/docs/latex/wx/mouseevt.tex
+++ b/docs/latex/wx/mouseevt.tex
@@ -9,15 +9,15 @@ drawbacks: the LEAVE\_WINDOW event might be received some time after the mouse
 left the window and the state variables for it may have changed during this
 time.
 
-{\bf NB: } Note the difference between methods like 
-\helpref{LeftDown}{wxmouseeventleftdown} and 
+{\bf NB: } Note the difference between methods like
+\helpref{LeftDown}{wxmouseeventleftdown} and
 \helpref{LeftIsDown}{wxmouseeventleftisdown}: the formet returns {\tt TRUE}
 when the event corresponds to the left mouse button click while the latter
 returns {\tt TRUE} if the left mouse button is currently being pressed. For
-example, when the user is dragging the mouse you can use 
+example, when the user is dragging the mouse you can use
 \helpref{LeftIsDown}{wxmouseeventleftisdown} to test
 whether the left mouse button is (still) depressed. Also, by convention, if
-\helpref{LeftDown}{wxmouseeventleftdown} returns {\tt TRUE}, 
+\helpref{LeftDown}{wxmouseeventleftdown} returns {\tt TRUE},
 \helpref{LeftIsDown}{wxmouseeventleftisdown} will also return {\tt TRUE} in
 wxWindows whatever the underlying GUI behaviour is (which is
 platform-dependent). The same applies, of course, to other mouse buttons as
@@ -50,6 +50,7 @@ functions that take a wxMouseEvent argument.
 \twocolitem{{\bf EVT\_MOTION(func)}}{Process a wxEVT\_MOTION event.}
 \twocolitem{{\bf EVT\_ENTER\_WINDOW(func)}}{Process a wxEVT\_ENTER\_WINDOW event.}
 \twocolitem{{\bf EVT\_LEAVE\_WINDOW(func)}}{Process a wxEVT\_LEAVE\_WINDOW event.}
+\twocolitem{{\bf EVT\_MOUSEWHEEL(func)}}{Process a wxEVT\_MOUSEWHEEL event.}
 \twocolitem{{\bf EVT\_MOUSE\_EVENTS(func)}}{Process all mouse events.}
 \end{twocollist}%
 
@@ -85,12 +86,6 @@ TRUE if the middle mouse button is currently pressed down.
 
 TRUE if the right mouse button is currently pressed down.
 
-\membersection{wxMouseEvent::m\_leftDown}
-
-\member{bool}{m\_leftDown}
-
-TRUE if the left mouse button is currently pressed down.
-
 \membersection{wxMouseEvent::m\_metaDown}
 
 \member{bool}{m\_metaDown}
@@ -115,6 +110,26 @@ X-coordinate of the event.
 
 Y-coordinate of the event.
 
+\membersection{wxMouseEvent::m\_wheelRotation}
+
+\member{int}{m\_wheelRotation}
+
+The distance the mouse wheel is rotated.
+
+\membersection{wxMouseEvent::m\_wheelDelta}
+
+\member{int}{m\_wheelDelta}
+
+The wheel delta, normally 120.
+
+\membersection{wxMouseEvent::m\_linesPerAction}
+
+\member{int}{m\_linesPerAction}
+
+The configured number of lines (or whatever) to be scrolled per wheel
+action.
+
+
 \membersection{wxMouseEvent::wxMouseEvent}
 
 \func{}{wxMouseEvent}{\param{WXTYPE}{ mouseEventType = 0}, \param{int}{ id = 0}}
@@ -135,6 +150,7 @@ Constructor. Valid event types are:
 \item {\bf wxEVT\_RIGHT\_UP}
 \item {\bf wxEVT\_RIGHT\_DCLICK}
 \item {\bf wxEVT\_MOTION}
+\item {\bf wxEVT\_MOUSEWHEEL}
 \end{itemize}
 
 \membersection{wxMouseEvent::AltDown}
@@ -217,6 +233,34 @@ Returns the physical mouse position in pixels.
 Returns the logical mouse position in pixels (i.e. translated according to the
 translation set for the DC, which usually indicates that the window has been scrolled).
 
+
+\membersection{wxMouseEvent::GetLinesPerAction}\label{wxmouseeventgetlinesperaction}
+
+\constfunc{int}{GetLinesPerAction}{\void}
+
+Returns the configured number of lines (or whatever) to be scrolled per
+wheel action.  Defaults to one.
+
+\membersection{wxMouseEvent::GetWheelRotation}\label{wxmouseeventgetwheelrotation}
+
+\constfunc{int}{GetWheelRotation}{\void}
+
+Get wheel rotation, positive or negative indicates direction of
+rotation.  Current devices all send an event when rotation is equal to
++/-WheelDelta, but this allows for finer resolution devices to be
+created in the future.  Because of this you shouldn't assume that one
+event is equal to 1 line or whatever, but you should be able to either
+do partial line scrolling or wait until +/-WheelDelta rotation values
+have been accumulated before scrolling.
+
+\membersection{wxMouseEvent::GetWheelDelta}\label{wxmouseeventgetwheeldelta}
+
+\constfunc{int}{GetWheelDelta}{\void}
+
+Get wheel delta, normally 120.  This is the threshold for action to be
+taken, and one such action (for example, scrolling one increment)
+should occur for each delta.
+
 \membersection{wxMouseEvent::GetX}\label{wxmouseeventgetx}
 
 \constfunc{long}{GetX}{\void}
@@ -263,7 +307,7 @@ Returns TRUE if the left mouse button changed to down.
 Returns TRUE if the left mouse button is currently down, independent
 of the current event type.
 
-Please notice that it is {\bf not} the same as 
+Please notice that it is {\bf not} the same as
 \helpref{LeftDown}{wxmouseeventleftdown} which returns TRUE if the left mouse
 button was just pressed. Rather, it describes the state of the mouse button
 before the event happened.
diff --git a/include/wx/event.h b/include/wx/event.h
index ca452cf849..f7bddba9b7 100644
--- a/include/wx/event.h
+++ b/include/wx/event.h
@@ -151,6 +151,7 @@ BEGIN_DECLARE_EVENT_TYPES()
     DECLARE_EVENT_TYPE(wxEVT_RIGHT_DCLICK, 111)
     DECLARE_EVENT_TYPE(wxEVT_SET_FOCUS, 112)
     DECLARE_EVENT_TYPE(wxEVT_KILL_FOCUS, 113)
+    DECLARE_EVENT_TYPE(wxEVT_MOUSEWHEEL, 114)
 
         // Non-client mouse events
     DECLARE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN, 200)
@@ -669,6 +670,25 @@ public:
     // Get Y position
     wxCoord GetY() const { return m_y; }
 
+    // Get wheel rotation, positive or negative indicates direction of
+    // rotation.  Current devices all send an event when rotation is equal to
+    // +/-WheelDelta, but this allows for finer resolution devices to be
+    // created in the future.  Because of this you shouldn't assume that one
+    // event is equal to 1 line or whatever, but you should be able to either
+    // do partial line scrolling or wait until +/-WheelDelta rotation values
+    // have been accumulated before scrolling.
+    int GetWheelRotation() const { return m_wheelRotation; }
+
+    // Get wheel delta, normally 120.  This is the threshold for action to be
+    // taken, and one such action (for example, scrolling one increment)
+    // should occur for each delta.
+    int GetWheelDelta() const { return m_wheelDelta; }
+
+    // Returns the configured number of lines (or whatever) to be scrolled per
+    // wheel action.  Defaults to one.
+    int GetLinesPerAction() const { return m_linesPerAction; }
+
+
     void CopyObject(wxObject& obj) const;
 
 public:
@@ -682,6 +702,10 @@ public:
     bool          m_shiftDown;
     bool          m_altDown;
     bool          m_metaDown;
+
+    int           m_wheelRotation;
+    int           m_wheelDelta;
+    int           m_linesPerAction;
 };
 
 // Cursor set event
@@ -1756,6 +1780,7 @@ typedef void (wxEvtHandler::*wxHelpEventFunction)(wxHelpEvent&);
 #define EVT_RIGHT_DCLICK(func) DECLARE_EVENT_TABLE_ENTRY( wxEVT_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
 #define EVT_LEAVE_WINDOW(func) DECLARE_EVENT_TABLE_ENTRY( wxEVT_LEAVE_WINDOW, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
 #define EVT_ENTER_WINDOW(func) DECLARE_EVENT_TABLE_ENTRY( wxEVT_ENTER_WINDOW, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
+#define EVT_MOUSEWHEEL(func) DECLARE_EVENT_TABLE_ENTRY( wxEVT_MOUSEWHEEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
 
 // All mouse events
 #define EVT_MOUSE_EVENTS(func) \
@@ -1770,7 +1795,8 @@ typedef void (wxEvtHandler::*wxHelpEventFunction)(wxHelpEvent&);
  DECLARE_EVENT_TABLE_ENTRY( wxEVT_MIDDLE_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),\
  DECLARE_EVENT_TABLE_ENTRY( wxEVT_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),\
  DECLARE_EVENT_TABLE_ENTRY( wxEVT_ENTER_WINDOW, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),\
- DECLARE_EVENT_TABLE_ENTRY( wxEVT_LEAVE_WINDOW, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
+ DECLARE_EVENT_TABLE_ENTRY( wxEVT_LEAVE_WINDOW, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),\
+ DECLARE_EVENT_TABLE_ENTRY( wxEVT_MOUSEWHEEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxMouseEventFunction) & func, (wxObject *) NULL ),
 
 // EVT_COMMAND
 #define EVT_COMMAND(id, event, fn)  DECLARE_EVENT_TABLE_ENTRY( event, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL ),
diff --git a/include/wx/generic/scrolwin.h b/include/wx/generic/scrolwin.h
index 1c9d92168b..0af1fc66c4 100644
--- a/include/wx/generic/scrolwin.h
+++ b/include/wx/generic/scrolwin.h
@@ -126,6 +126,7 @@ public:
     void OnSize(wxSizeEvent& event);
     void OnPaint(wxPaintEvent& event);
     void OnChar(wxKeyEvent& event);
+    void OnMouseWheel(wxMouseEvent& event);
 
     // Calculate scroll increment
     virtual int CalcScrollInc(wxScrollWinEvent& event);
@@ -144,6 +145,7 @@ protected:
     int                   m_yScrollLinesPerPage;
     double                m_scaleX;
     double                m_scaleY;
+    int                   m_wheelRotation;
 
 private:
     DECLARE_EVENT_TABLE()
diff --git a/include/wx/msw/setup0.h b/include/wx/msw/setup0.h
index b88647d7e0..6e1c1de4b6 100644
--- a/include/wx/msw/setup0.h
+++ b/include/wx/msw/setup0.h
@@ -532,6 +532,9 @@
 #define wxUSE_WX_RESOURCES        1
                                 // Use .wxr resource mechanism (requires PrologIO library)
 
+#define wxUSE_MOUSEWHEEL        1
+                                // Include mouse wheel support
+
 // ----------------------------------------------------------------------------
 // postscript support settings
 // ----------------------------------------------------------------------------
@@ -555,10 +558,10 @@
 
 // For backward compatibility reasons, this parameter now only controls the
 // default scrolling method used by cursors.  This default behavior can be
-// overriden by setting the second param of wxDB::wxDbGetConnection() or 
+// overriden by setting the second param of wxDB::wxDbGetConnection() or
 // wxDb() constructor to indicate whether the connection (and any wxDbTable()s
-// that use the connection) should support forward only scrolling of cursors, 
-// or both forward and backward support for backward scrolling cursors is 
+// that use the connection) should support forward only scrolling of cursors,
+// or both forward and backward support for backward scrolling cursors is
 // dependent on the data source as well as the ODBC driver being used.
 #define wxODBC_FWD_ONLY_CURSORS	 1
 
diff --git a/include/wx/msw/window.h b/include/wx/msw/window.h
index a19f12da28..538051007b 100644
--- a/include/wx/msw/window.h
+++ b/include/wx/msw/window.h
@@ -327,6 +327,7 @@ public:
 
     bool HandleMouseEvent(WXUINT msg, int x, int y, WXUINT flags);
     bool HandleMouseMove(int x, int y, WXUINT flags);
+    bool HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam);
 
     bool HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII = FALSE);
     bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam);
diff --git a/src/common/event.cpp b/src/common/event.cpp
index 0939c4dcc2..301ae91082 100644
--- a/src/common/event.cpp
+++ b/src/common/event.cpp
@@ -150,6 +150,7 @@ DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DCLICK)
 DEFINE_EVENT_TYPE(wxEVT_RIGHT_DCLICK)
 DEFINE_EVENT_TYPE(wxEVT_SET_FOCUS)
 DEFINE_EVENT_TYPE(wxEVT_KILL_FOCUS)
+DEFINE_EVENT_TYPE(wxEVT_MOUSEWHEEL)
 
 // Non-client mouse events
 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN)
@@ -414,6 +415,9 @@ wxMouseEvent::wxMouseEvent(wxEventType commandType)
     m_middleDown = FALSE;
     m_x = 0;
     m_y = 0;
+    m_wheelRotation = 0;
+    m_wheelDelta = 0;
+    m_linesPerAction = 0;
 }
 
 void wxMouseEvent::CopyObject(wxObject& obj_d) const
diff --git a/src/generic/scrolwin.cpp b/src/generic/scrolwin.cpp
index d28dc6fb3b..7cd7722062 100644
--- a/src/generic/scrolwin.cpp
+++ b/src/generic/scrolwin.cpp
@@ -68,6 +68,7 @@ BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
     EVT_SIZE(wxGenericScrolledWindow::OnSize)
     EVT_PAINT(wxGenericScrolledWindow::OnPaint)
     EVT_CHAR(wxGenericScrolledWindow::OnChar)
+    EVT_MOUSEWHEEL(wxGenericScrolledWindow::OnMouseWheel)
 END_EVENT_TABLE()
 
 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
@@ -94,6 +95,7 @@ wxGenericScrolledWindow::wxGenericScrolledWindow()
     m_yScrollLinesPerPage = 0;
     m_scaleX = 1.0;
     m_scaleY = 1.0;
+    m_wheelRotation = 0;
     m_targetWindow = (wxWindow*) NULL;
 }
 
@@ -116,6 +118,7 @@ bool wxGenericScrolledWindow::Create(wxWindow *parent,
     m_yScrollLinesPerPage = 0;
     m_scaleX = 1.0;
     m_scaleY = 1.0;
+    m_wheelRotation = 0;
 
     m_targetWindow = this;
 
@@ -151,7 +154,7 @@ void wxGenericScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUn
     bool do_refresh =
     (
       (noUnitsX != 0 && m_xScrollLines == 0) ||
-      (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) || 
+      (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
 
       (noUnitsY != 0 && m_yScrollLines == 0) ||
       (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
@@ -548,7 +551,7 @@ void wxGenericScrolledWindow::Scroll( int x_pos, int y_pos )
         // the visible portion of it or if below zero
         m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
         m_yScrollPosition = wxMax( 0, m_yScrollPosition );
-        
+
         if (old_y != m_yScrollPosition) {
             m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
             m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
@@ -702,3 +705,24 @@ void wxGenericScrolledWindow::OnChar(wxKeyEvent& event)
             event.Skip();
     }
 }
+
+
+void wxGenericScrolledWindow::OnMouseWheel(wxMouseEvent& event)
+{
+    int lines;
+    int vsx, vsy;
+
+    m_wheelRotation += event.GetWheelRotation();
+    lines = m_wheelRotation / event.GetWheelDelta();
+    m_wheelRotation -= lines * event.GetWheelDelta();
+
+    if (lines != 0) {
+        lines *= event.GetLinesPerAction();
+        GetViewStart(&vsx, &vsy);
+        Scroll(-1, vsy - lines);
+    }
+}
+
+
+
+
diff --git a/src/msw/window.cpp b/src/msw/window.cpp
index 3864f654d4..dadffe9b6a 100644
--- a/src/msw/window.cpp
+++ b/src/msw/window.cpp
@@ -108,6 +108,15 @@
 #define SIF_TRACKPOS 16
 #endif
 
+#if wxUSE_MOUSEWHEEL
+    #ifndef WM_MOUSEWHEEL
+        #define WM_MOUSEWHEEL           0x020A
+        #define WHEEL_DELTA             120
+        #define SPI_GETWHEELSCROLLLINES 104
+    #endif
+#endif
+
+
 // ---------------------------------------------------------------------------
 // global variables
 // ---------------------------------------------------------------------------
@@ -1910,6 +1919,12 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
                                         wParam);
             break;
 
+#if wxUSE_MOUSEWHEEL
+        case WM_MOUSEWHEEL:
+            processed = HandleMouseWheel(wParam, lParam);
+            break;
+#endif
+
         case WM_LBUTTONDOWN:
            // set focus to this window
            if (AcceptsFocus())
@@ -3347,6 +3362,31 @@ bool wxWindow::HandleMouseMove(int x, int y, WXUINT flags)
     return HandleMouseEvent(WM_MOUSEMOVE, x, y, flags);
 }
 
+
+bool wxWindow::HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam)
+{
+#if wxUSE_MOUSEWHEEL
+    wxMouseEvent event(wxEVT_MOUSEWHEEL);
+    InitMouseEvent(event,
+                   GET_X_LPARAM(lParam),
+                   GET_Y_LPARAM(lParam),
+                   LOWORD(wParam));
+
+    event.m_wheelRotation = (short)HIWORD(wParam);
+    event.m_wheelDelta = WHEEL_DELTA;
+
+    int linesPer;
+    if (!SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &linesPer, 0))
+        linesPer = 1;
+    event.m_linesPerAction = linesPer;
+
+    return GetEventHandler()->ProcessEvent(event);
+#else
+    return FALSE;
+#endif
+}
+
+
 // ---------------------------------------------------------------------------
 // keyboard handling
 // ---------------------------------------------------------------------------
@@ -4098,6 +4138,7 @@ const char *wxGetMessageName(int message)
         case 0x0207: return "WM_MBUTTONDOWN";
         case 0x0208: return "WM_MBUTTONUP";
         case 0x0209: return "WM_MBUTTONDBLCLK";
+        case 0x020A: return "WM_MOUSEWHEEL";
         case 0x0210: return "WM_PARENTNOTIFY";
         case 0x0211: return "WM_ENTERMENULOOP";
         case 0x0212: return "WM_EXITMENULOOP";
-- 
2.50.0

