From 487018f42e9c61069ed55217c5f935b427d7770b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Feb 2023 17:55:42 +0000 Subject: [PATCH] Handle mouse capture loss gracefully in the drawing sample Reset the selection and avoid the assert which happened when the capture was lost unexpectedly (e.g. due to switching to another window using some key combination without releasing the mouse). --- samples/drawing/drawing.cpp | 40 ++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 37a7b4585b..2bb122ffd2 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -107,6 +107,7 @@ public: void OnMouseMove(wxMouseEvent &event); void OnMouseDown(wxMouseEvent &event); void OnMouseUp(wxMouseEvent &event); + void OnMouseCaptureLost(wxMouseCaptureLostEvent &event); void ToShow(int show) { m_show = show; Refresh(); } int GetPage() { return m_show; } @@ -160,6 +161,10 @@ protected: void DrawRegionsHelper(wxDC& dc, wxCoord x, bool firstTime); + // Remove the rubber band if it's currently shown and return true or just + // return false if we're not showing it. + bool StopRubberBanding(); + private: MyFrame *m_owner; @@ -503,6 +508,7 @@ wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) EVT_MOTION (MyCanvas::OnMouseMove) EVT_LEFT_DOWN (MyCanvas::OnMouseDown) EVT_LEFT_UP (MyCanvas::OnMouseUp) + EVT_MOUSE_CAPTURE_LOST (MyCanvas::OnMouseCaptureLost) wxEND_EVENT_TABLE() #include "smile.xpm" @@ -2097,19 +2103,28 @@ void MyCanvas::OnMouseDown(wxMouseEvent &event) CaptureMouse() ; } +bool MyCanvas::StopRubberBanding() +{ + if ( !m_rubberBand ) + return false; + + { + wxClientDC dc( this ); + PrepareDC( dc ); + wxDCOverlay overlaydc( m_overlay, &dc ); + overlaydc.Clear(); + } + m_overlay.Reset(); + m_rubberBand = false; + + return true; +} + void MyCanvas::OnMouseUp(wxMouseEvent &event) { - if ( m_rubberBand ) + if ( StopRubberBanding() ) { ReleaseMouse(); - { - wxClientDC dc( this ); - PrepareDC( dc ); - wxDCOverlay overlaydc( m_overlay, &dc ); - overlaydc.Clear(); - } - m_overlay.Reset(); - m_rubberBand = false; wxPoint endpoint = CalcUnscrolledPosition(event.GetPosition()); @@ -2123,6 +2138,13 @@ void MyCanvas::OnMouseUp(wxMouseEvent &event) } } +void MyCanvas::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) +{ + StopRubberBanding(); + + wxLogStatus(m_owner, "Mouse capture lost"); +} + #if wxUSE_GRAPHICS_CONTEXT void MyCanvas::UseGraphicRenderer(wxGraphicsRenderer* renderer) {