From 36d50301cfbf1b62d1eeda0ea4eebf894f0a25e1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 6 Jun 2007 11:28:44 +0000 Subject: [PATCH] From Olaf Flebbe, "an implementation of GraphicsWindow::setCursor for WIN32." --- .../osgViewer/api/Win32/GraphicsWindowWin32 | 10 ++ src/osgViewer/GraphicsWindowWin32.cpp | 91 ++++++++++++++++++- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/include/osgViewer/api/Win32/GraphicsWindowWin32 b/include/osgViewer/api/Win32/GraphicsWindowWin32 index 946045750..843c2e919 100644 --- a/include/osgViewer/api/Win32/GraphicsWindowWin32 +++ b/include/osgViewer/api/Win32/GraphicsWindowWin32 @@ -78,6 +78,9 @@ class GraphicsWindowWin32 : public osgViewer::GraphicsWindow /** Switch on/off the cursor.*/ virtual void useCursor(bool /*cursorOn*/); + /** Set mouse cursor to a specific shape.*/ + virtual void setCursor(MouseCursor cursor); + /** Handle a native (Win32) windowing event as received from the system */ virtual LRESULT handleNativeWindowingEvent( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); @@ -119,15 +122,20 @@ class GraphicsWindowWin32 : public osgViewer::GraphicsWindow void transformMouseXY(float& x, float& y); + HCURSOR getOrCreateCursor(MouseCursor mouseShape); HWND _hwnd; HDC _hdc; HGLRC _hglrc; + HCURSOR _currentCursor; WNDPROC _windowProcedure; double _timeOfLastCheckEvents; + + + int _screenOriginX; int _screenOriginY; unsigned int _screenWidth; @@ -146,6 +154,8 @@ class GraphicsWindowWin32 : public osgViewer::GraphicsWindow bool _closeWindow; bool _destroyWindow; bool _destroying; + + std::map _mouseCursorMap; }; } diff --git a/src/osgViewer/GraphicsWindowWin32.cpp b/src/osgViewer/GraphicsWindowWin32.cpp index 1c6ebc857..d758f7baa 100644 --- a/src/osgViewer/GraphicsWindowWin32.cpp +++ b/src/osgViewer/GraphicsWindowWin32.cpp @@ -1021,6 +1021,7 @@ GraphicsWindowWin32::GraphicsWindowWin32( osg::GraphicsContext::Traits* traits ) _destroying(false) { _traits = traits; + setCursor( LeftArrowCursor); init(); @@ -1772,6 +1773,89 @@ void GraphicsWindowWin32::useCursor( bool cursorOn ) _traits->useCursor = cursorOn; } +void GraphicsWindowWin32::setCursor( MouseCursor mouseCursor ) +{ + HCURSOR newCursor = getOrCreateCursor( mouseCursor); + if (newCursor == _currentCursor) return; + + _currentCursor = newCursor; + _traits->useCursor = (_currentCursor != NULL); +} + +HCURSOR GraphicsWindowWin32::getOrCreateCursor(MouseCursor mouseCursor) +{ + std::map::iterator i = _mouseCursorMap.find(mouseCursor); + if (i != _mouseCursorMap.end()) return i->second; + + switch (mouseCursor) { + case NoCursor: + _mouseCursorMap[mouseCursor] = NULL; + break; + case RightArrowCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_ARROW); + break; + case LeftArrowCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_ARROW); + break; + case InfoCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZEALL); + break; + case DestroyCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_NO ); + break; + case HelpCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_HELP ); + break; + case CycleCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_NO ); + break; + case SprayCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZEALL ); + break; + case WaitCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_WAIT); + break; + case TextCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_IBEAM ); + break; + case CrosshairCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_CROSS ); + break; + case UpDownCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZENS ); + break; + case LeftRightCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZEWE ); + break; + case TopSideCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_UPARROW ); + break; + case BottomSideCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_UPARROW ); + break; + case LeftSideCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZEWE); + break; + case RightSideCursor: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZEWE ); + break; + case TopLeftCorner: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZENWSE ); + break; + case TopRightCorner: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZENESW ); + break; + case BottomRightCorner: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZENWSE ); + break; + case BottomLeftCorner: + _mouseCursorMap[mouseCursor] = LoadCursor( NULL, IDC_SIZENESW ); + break; + } + + return _mouseCursorMap[mouseCursor]; +} + void GraphicsWindowWin32::adaptKey( WPARAM wParam, LPARAM lParam, int& keySymbol, unsigned int& modifierMask ) { modifierMask = 0; @@ -2044,9 +2128,10 @@ LRESULT GraphicsWindowWin32::handleNativeWindowingEvent( HWND hwnd, UINT uMsg, W /////////////////// case WM_SETCURSOR : /////////////////// - - if (_traits->useCursor) return ::DefWindowProc(hwnd, uMsg, wParam, lParam); - ::SetCursor(NULL); + if (_traits->useCursor) + ::SetCursor( _currentCursor); + else + ::SetCursor(NULL); return TRUE; /////////////////