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).
This commit is contained in:
Vadim Zeitlin 2023-02-14 17:55:42 +00:00
parent f450241343
commit 487018f42e

View file

@ -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)
{