From ed43694682e6421802d9479402f5ef49cebacc73 Mon Sep 17 00:00:00 2001 From: Andrew Zonenberg Date: Sun, 12 Jul 2015 12:10:59 -0700 Subject: [PATCH] GAL now hides cursor when mouse is outside the view. No change to legacy view. --- common/view/wx_view_controls.cpp | 33 +++++++++++++++++++++--- include/gal/graphics_abstraction_layer.h | 10 +++++++ include/view/wx_view_controls.h | 13 ++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/common/view/wx_view_controls.cpp b/common/view/wx_view_controls.cpp index 7bb333004a..0f1c734c8d 100644 --- a/common/view/wx_view_controls.cpp +++ b/common/view/wx_view_controls.cpp @@ -36,7 +36,8 @@ using namespace KIGFX; const wxEventType WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE = wxNewEventType(); WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel ) : - VIEW_CONTROLS( aView ), m_state( IDLE ), m_parentPanel( aParentPanel ), m_scrollScale( 1.0, 1.0 ) + VIEW_CONTROLS( aView ), m_state( IDLE ), m_parentPanel( aParentPanel ), m_scrollScale( 1.0, 1.0 ), + m_mouseIsInView(false), m_cursorWasDisplayedOnLeave(false) { m_parentPanel->Connect( wxEVT_MOTION, wxMouseEventHandler( WX_VIEW_CONTROLS::onMotion ), NULL, this ); @@ -54,10 +55,8 @@ WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this ); m_parentPanel->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this ); -#if defined _WIN32 || defined _WIN64 m_parentPanel->Connect( wxEVT_ENTER_WINDOW, wxMouseEventHandler( WX_VIEW_CONTROLS::onEnter ), NULL, this ); -#endif m_parentPanel->Connect( wxEVT_LEAVE_WINDOW, wxMouseEventHandler( WX_VIEW_CONTROLS::onLeave ), NULL, this ); m_parentPanel->Connect( wxEVT_SCROLLWIN_THUMBTRACK, @@ -212,12 +211,28 @@ void WX_VIEW_CONTROLS::onButton( wxMouseEvent& aEvent ) void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent ) { + //If the cursor was enabled when we left, turn it on now + m_mouseIsInView = true; + m_view->GetGAL()->SetCursorEnabled(m_cursorWasDisplayedOnLeave); + +#if defined _WIN32 || defined _WIN64 m_parentPanel->SetFocus(); +#endif } void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent ) { + // Always hide the cursor when the mouse leaves the window to avoid confusing the user + // But we may need to re-enable it, so keep track of whether it was visible + m_mouseIsInView = false; + m_cursorWasDisplayedOnLeave = m_view->GetGAL()->GetCursorEnabled(); + m_view->GetGAL()->SetCursorEnabled(false); + + // If we hid the cursor, redraw the window to get rid of it + if(m_cursorWasDisplayedOnLeave) + m_view->Redraw(); + if( m_cursorCaptured ) { bool warp = false; @@ -434,3 +449,15 @@ void WX_VIEW_CONTROLS::UpdateScrollbars() ( viewport.Centre().x - boundary.GetLeft() ) / boundary.GetWidth() * m_scrollScale.x, ( viewport.Centre().y - boundary.GetTop() ) / boundary.GetHeight() * m_scrollScale.y ); } + +void WX_VIEW_CONTROLS::ShowCursor( bool aEnabled ) +{ + // If the mouse is outside the view (on a toolbar etc), don't show the cursor + // until the mouse enters the view again. Just remember we want to show it now + if(!m_mouseIsInView) + m_cursorWasDisplayedOnLeave = aEnabled; + + // but if it's in the view, we want to show it right now + else + m_view->GetGAL()->SetCursorEnabled( aEnabled ); +} diff --git a/include/gal/graphics_abstraction_layer.h b/include/gal/graphics_abstraction_layer.h index 6c478e8558..281b7d6fc9 100644 --- a/include/gal/graphics_abstraction_layer.h +++ b/include/gal/graphics_abstraction_layer.h @@ -762,6 +762,16 @@ public: { isCursorEnabled = aCursorEnabled; } + + /** + * @brief Returns the cursor enable state + * + * @return True if the cursor is being drawn, else false + */ + inline bool GetCursorEnabled() const + { + return isCursorEnabled; + } /** * @brief Set the cursor color. diff --git a/include/view/wx_view_controls.h b/include/view/wx_view_controls.h index 67ad9b4567..33710be6de 100644 --- a/include/view/wx_view_controls.h +++ b/include/view/wx_view_controls.h @@ -84,6 +84,13 @@ public: if( m_state == AUTO_PANNING ) m_state = IDLE; } + + /** + * Function ShowCursor() + * Enables or disables display of cursor. + * @param aEnabled decides if the cursor should be shown. + */ + virtual void ShowCursor( bool aEnabled ); /// @copydoc VIEW_CONTROLS::GetMousePosition() VECTOR2D GetMousePosition() const; @@ -140,6 +147,12 @@ private: /// Ratio used for scaling world coordinates to scrollbar position. VECTOR2D m_scrollScale; + + /// Indicates whether the mouse is inside the window + bool m_mouseIsInView; + + /// Indicates whether the cursor was being displayed at the time the mouse left the window + bool m_cursorWasDisplayedOnLeave; }; } // namespace KIGFX