From 9dc0bfc755b5ba074b9e7c23a311c132e6926f37 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Sun, 5 Jan 2014 17:21:59 +0200 Subject: [PATCH 1/7] Small fix for extracting mouse coordinates on Windows, in a way that will always work with multiple monitors. This is the way recommended by Microsoft. --- modules/highgui/src/window_w32.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index 6a5355c53..222f0b0ab 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -1474,8 +1474,8 @@ static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM if( uMsg == WM_LBUTTONUP || uMsg == WM_RBUTTONUP || uMsg == WM_MBUTTONUP ) ReleaseCapture(); - pt.x = LOWORD( lParam ); - pt.y = HIWORD( lParam ); + pt.x = GET_X_LPARAM( lParam ); + pt.y = GET_Y_LPARAM( lParam ); GetClientRect( window->hwnd, &rect ); icvGetBitmapData( window, &size, 0, 0 ); From fecd5c994b79f0480b5d893fd53e062edccef0ad Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Tue, 7 Jan 2014 21:17:57 +0200 Subject: [PATCH 2/7] Added support for mouse-wheel events on Windows. To be used in the mouse callback like this: if (CV_EVENT_MOUSEWHEEL == CV_GET_MOUSEWHEEL_EVENT(event)) { int delta= CV_GET_WHEEL_DELTA(event); // use delta... } --- .../include/opencv2/highgui/highgui_c.h | 9 ++++- modules/highgui/src/window_w32.cpp | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index 1a42e5804..d1aa93b02 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -170,7 +170,9 @@ enum CV_EVENT_MBUTTONUP =6, CV_EVENT_LBUTTONDBLCLK =7, CV_EVENT_RBUTTONDBLCLK =8, - CV_EVENT_MBUTTONDBLCLK =9 + CV_EVENT_MBUTTONDBLCLK =9, + CV_EVENT_MOUSEWHEEL =10, + CV_EVENT_MOUSEHWHEEL =11 }; enum @@ -183,6 +185,11 @@ enum CV_EVENT_FLAG_ALTKEY =32 }; + +#define CV_GET_WHEEL_DELTA(event) ((short)((event >> 16) & 0xffff)) // upper 16 bits +#define CV_GET_MOUSEWHEEL_EVENT(event) (event & 0xffff) // lower 16 bits + + typedef void (CV_CDECL *CvMouseCallback )(int event, int x, int y, int flags, void* param); /* assign callback for mouse events */ diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index 222f0b0ab..70a29d402 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -40,6 +40,7 @@ //M*/ #include "precomp.hpp" +#include // required for GET_X_LPARAM() and GET_Y_LPARAM() marco #if defined WIN32 || defined _WIN32 @@ -1377,6 +1378,43 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) SetFocus(window->hwnd); break; + case WM_MOUSEWHEEL: + case WM_MOUSEHWHEEL: + if( window->on_mouse ) + { + int flags = (wParam & MK_LBUTTON ? CV_EVENT_FLAG_LBUTTON : 0)| + (wParam & MK_RBUTTON ? CV_EVENT_FLAG_RBUTTON : 0)| + (wParam & MK_MBUTTON ? CV_EVENT_FLAG_MBUTTON : 0)| + (wParam & MK_CONTROL ? CV_EVENT_FLAG_CTRLKEY : 0)| + (wParam & MK_SHIFT ? CV_EVENT_FLAG_SHIFTKEY : 0)| + (GetKeyState(VK_MENU) < 0 ? CV_EVENT_FLAG_ALTKEY : 0); + int event = (uMsg == WM_MOUSEWHEEL ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL); + + // Set the wheel delta of mouse wheel to be in the upper word of 'event' + int delta = GET_WHEEL_DELTA_WPARAM(wParam); + event |= (delta << 16); + + POINT pt; + { + // since The coordinates are relative to screen so get screen size. + RECT windowRect; + ::GetWindowRect( window->hwnd, &windowRect ); + pt.x = GET_X_LPARAM( lParam ) - windowRect.left; + pt.y = GET_Y_LPARAM( lParam ) - windowRect.top; + } + + RECT rect; + GetClientRect( window->hwnd, &rect ); + + SIZE size = {0,0}; + icvGetBitmapData( window, &size, 0, 0 ); + + window->on_mouse( event, pt.x*size.cx/MAX(rect.right - rect.left,1), + pt.y*size.cy/MAX(rect.bottom - rect.top,1), flags, + window->on_mouse_param ); + } + break; + case WM_ERASEBKGND: { RECT cr, tr, wrc; From e45cf2173f28c51eadfe799d18f342b7163f9590 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Sun, 12 Jan 2014 09:33:08 +0200 Subject: [PATCH 3/7] [HighGUI] Updated mouse wheel delta to reside within the 'flags' variable. Updated C++ API. --- modules/highgui/include/opencv2/highgui.hpp | 6 +++++- modules/highgui/include/opencv2/highgui/highgui_c.h | 4 +--- modules/highgui/src/window.cpp | 5 +++++ modules/highgui/src/window_w32.cpp | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/highgui/include/opencv2/highgui.hpp b/modules/highgui/include/opencv2/highgui.hpp index eb4ee8c03..00e1ce427 100644 --- a/modules/highgui/include/opencv2/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui.hpp @@ -76,7 +76,9 @@ enum { EVENT_MOUSEMOVE = 0, EVENT_MBUTTONUP = 6, EVENT_LBUTTONDBLCLK = 7, EVENT_RBUTTONDBLCLK = 8, - EVENT_MBUTTONDBLCLK = 9 + EVENT_MBUTTONDBLCLK =9, + EVENT_MOUSEWHEEL =10, + EVENT_MOUSEHWHEEL =11 }; enum { EVENT_FLAG_LBUTTON = 1, @@ -137,6 +139,8 @@ CV_EXPORTS_W double getWindowProperty(const String& winname, int prop_id); //! assigns callback for mouse events CV_EXPORTS void setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0); +CV_EXPORTS int getMouseWheelDelta(int flags); + CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname, int* value, int count, TrackbarCallback onChange = 0, diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index d1aa93b02..ac1262585 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -186,9 +186,7 @@ enum }; -#define CV_GET_WHEEL_DELTA(event) ((short)((event >> 16) & 0xffff)) // upper 16 bits -#define CV_GET_MOUSEWHEEL_EVENT(event) (event & 0xffff) // lower 16 bits - +#define CV_GET_WHEEL_DELTA(flags) ((short)((flags >> 16) & 0xffff)) // upper 16 bits typedef void (CV_CDECL *CvMouseCallback )(int event, int x, int y, int flags, void* param); diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index 428ef51ef..03ff988d7 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -216,6 +216,11 @@ void cv::setMouseCallback( const String& windowName, MouseCallback onMouse, void cvSetMouseCallback(windowName.c_str(), onMouse, param); } +int cv::getMouseWheelDelta( int flags ) +{ + return CV_GET_WHEEL_DELTA(flags); +} + int cv::startWindowThread() { return cvStartWindowThread(); diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index 70a29d402..bb4704795 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -1392,7 +1392,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) // Set the wheel delta of mouse wheel to be in the upper word of 'event' int delta = GET_WHEEL_DELTA_WPARAM(wParam); - event |= (delta << 16); + flags |= (delta << 16); POINT pt; { From 24333569aa8096f863b115814c10f0917630040c Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Thu, 16 Jan 2014 11:09:25 +0200 Subject: [PATCH 4/7] Small fixes: typo, spacing and using ::ScreenToClient() instead of ::GetWindowRect() --- modules/highgui/include/opencv2/highgui.hpp | 6 +++--- modules/highgui/src/window_w32.cpp | 12 ++++-------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/modules/highgui/include/opencv2/highgui.hpp b/modules/highgui/include/opencv2/highgui.hpp index 00e1ce427..068747068 100644 --- a/modules/highgui/include/opencv2/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui.hpp @@ -76,9 +76,9 @@ enum { EVENT_MOUSEMOVE = 0, EVENT_MBUTTONUP = 6, EVENT_LBUTTONDBLCLK = 7, EVENT_RBUTTONDBLCLK = 8, - EVENT_MBUTTONDBLCLK =9, - EVENT_MOUSEWHEEL =10, - EVENT_MOUSEHWHEEL =11 + EVENT_MBUTTONDBLCLK = 9, + EVENT_MOUSEWHEEL = 10, + EVENT_MOUSEHWHEEL = 11 }; enum { EVENT_FLAG_LBUTTON = 1, diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index bb4704795..1e665ed02 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -40,7 +40,7 @@ //M*/ #include "precomp.hpp" -#include // required for GET_X_LPARAM() and GET_Y_LPARAM() marco +#include // required for GET_X_LPARAM() and GET_Y_LPARAM() macros #if defined WIN32 || defined _WIN32 @@ -1395,13 +1395,9 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) flags |= (delta << 16); POINT pt; - { - // since The coordinates are relative to screen so get screen size. - RECT windowRect; - ::GetWindowRect( window->hwnd, &windowRect ); - pt.x = GET_X_LPARAM( lParam ) - windowRect.left; - pt.y = GET_Y_LPARAM( lParam ) - windowRect.top; - } + pt.x = GET_X_LPARAM( lParam ); + pt.y = GET_Y_LPARAM( lParam ); + ::ScreenToClient(hwnd, &pt); // Convert screen coordinates to client coordinates. RECT rect; GetClientRect( window->hwnd, &rect ); From 8802d62cc5afd16987947ff4c0334e528141a4a4 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Thu, 16 Jan 2014 11:25:57 +0200 Subject: [PATCH 5/7] Updated docs. --- modules/highgui/doc/user_interface.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/highgui/doc/user_interface.rst b/modules/highgui/doc/user_interface.rst index 8b655a1c8..58b040b3f 100644 --- a/modules/highgui/doc/user_interface.rst +++ b/modules/highgui/doc/user_interface.rst @@ -208,6 +208,24 @@ Sets mouse handler for the specified window :param userdata: The optional parameter passed to the callback. +getMouseWheelDelta +------------------ +Gets the mouse-wheel motion delta, when handling mouse-wheel events EVENT_MOUSEWHEEL and EVENT_MOUSEHWHEEL. + +.. ocv:function:: int getMouseWheelDelta(int flags) + +.. ocv:cfunction:: CV_GET_WHEEL_DELTA(flags) + + :param flags: The mouse callback flags parameter. + + +For EVENT_MOUSEWHEEL positive and negative values mean forward and backward scrolling, respectively. +For EVENT_MOUSEHWHEEL, where available, positive and negative values mean right and left scrolling, respectively. + +.. note:: + + Mouse-wheel event are currently available only on Windows. On Windows Units are usually multiples of 120. + setTrackbarPos ------------------ Sets the trackbar position. From 46f06d82c9775b8572ded9cefd02c68cce61be00 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Thu, 16 Jan 2014 12:51:14 +0200 Subject: [PATCH 6/7] Update docs. --- modules/highgui/doc/user_interface.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/highgui/doc/user_interface.rst b/modules/highgui/doc/user_interface.rst index 58b040b3f..2be7a67b3 100644 --- a/modules/highgui/doc/user_interface.rst +++ b/modules/highgui/doc/user_interface.rst @@ -218,13 +218,15 @@ Gets the mouse-wheel motion delta, when handling mouse-wheel events EVENT_MOUSEW :param flags: The mouse callback flags parameter. +For regular mice with a scroll-wheel, delta will be a multiple of 120. The value 120 corresponds to a one notch rotation of the wheel or the threshold for action to be taken and one such action should occur for each delta. +Some high-precision mice with higher-resolution freely-rotating wheels may generate smaller values. For EVENT_MOUSEWHEEL positive and negative values mean forward and backward scrolling, respectively. For EVENT_MOUSEHWHEEL, where available, positive and negative values mean right and left scrolling, respectively. .. note:: - Mouse-wheel event are currently available only on Windows. On Windows Units are usually multiples of 120. + Mouse-wheel events are currently supported only on Windows. setTrackbarPos ------------------ From 8fd0fd538d567828bfd12861493c69f407b9dc23 Mon Sep 17 00:00:00 2001 From: Adi Shavit Date: Thu, 16 Jan 2014 16:30:54 +0200 Subject: [PATCH 7/7] Doc fix. --- modules/highgui/doc/user_interface.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/highgui/doc/user_interface.rst b/modules/highgui/doc/user_interface.rst index 2be7a67b3..0d0ccde94 100644 --- a/modules/highgui/doc/user_interface.rst +++ b/modules/highgui/doc/user_interface.rst @@ -214,8 +214,6 @@ Gets the mouse-wheel motion delta, when handling mouse-wheel events EVENT_MOUSEW .. ocv:function:: int getMouseWheelDelta(int flags) -.. ocv:cfunction:: CV_GET_WHEEL_DELTA(flags) - :param flags: The mouse callback flags parameter. For regular mice with a scroll-wheel, delta will be a multiple of 120. The value 120 corresponds to a one notch rotation of the wheel or the threshold for action to be taken and one such action should occur for each delta. @@ -224,6 +222,8 @@ Some high-precision mice with higher-resolution freely-rotating wheels may gener For EVENT_MOUSEWHEEL positive and negative values mean forward and backward scrolling, respectively. For EVENT_MOUSEHWHEEL, where available, positive and negative values mean right and left scrolling, respectively. +With the C API, the macro CV_GET_WHEEL_DELTA(flags) can be used alternatively. + .. note:: Mouse-wheel events are currently supported only on Windows.