Fix mouse event handling while dragging in wxGrid

Ignore all the mouse events other than "left up" while dragging to avoid
releasing the mouse and ending the dragging operation too soon.

This required non-trivial refactoring of the code which hopefully should
also make it slightly more clear by centralizing high level logic in
ProcessGridCellMouseEvent() itself and calling various helper functions
from it instead of spreading this logic over the entire call tree.

The code still remains pretty confusing and rewriting it to use
wxMouseEventsManager (which would need to be generalized first to become
a template class using arbitrary item type instead of just "int", as
now) would undoubtedly do it a lot of good.

Closes #18186.
This commit is contained in:
Vadim Zeitlin
2018-08-17 00:57:04 +02:00
parent 53972b17ee
commit bb00501e77
2 changed files with 82 additions and 67 deletions

View File

@@ -3863,6 +3863,16 @@ void wxGrid::DoAfterDraggingEnd()
m_winCapture = NULL;
}
void wxGrid::EndDraggingIfNecessary()
{
if ( m_winCapture )
{
m_winCapture->ReleaseMouse();
DoAfterDraggingEnd();
}
}
void wxGrid::ChangeCursorMode(CursorMode mode,
wxWindow *win,
bool captureMouse)
@@ -3897,11 +3907,7 @@ void wxGrid::ChangeCursorMode(CursorMode mode,
win = m_gridWin;
}
if ( m_winCapture )
{
m_winCapture->ReleaseMouse();
m_winCapture = NULL;
}
EndDraggingIfNecessary();
m_cursorMode = mode;
@@ -4019,34 +4025,14 @@ void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
}
void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
bool wxGrid::DoGridDragEvent(wxMouseEvent& event,
const wxGridCellCoords& coords,
bool isFirstDrag)
{
if ( !m_isDragging )
{
// Don't start doing anything until the mouse has been dragged far
// enough
const wxPoint& pt = event.GetPosition();
if ( m_startDragPos == wxDefaultPosition )
{
m_startDragPos = pt;
return;
}
if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
return;
}
const bool isFirstDrag = !m_isDragging;
m_isDragging = true;
switch ( m_cursorMode )
{
case WXGRID_CURSOR_SELECT_CELL:
// no further handling if handled by user
if ( DoGridCellDrag(event, coords, isFirstDrag) == false )
return;
break;
return DoGridCellDrag(event, coords, isFirstDrag);
case WXGRID_CURSOR_RESIZE_ROW:
DoGridLineDrag(event, wxGridRowOperations());
@@ -4060,13 +4046,7 @@ void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords
event.Skip();
}
if ( isFirstDrag )
{
wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
m_winCapture = m_gridWin;
m_winCapture->CaptureMouse();
}
return true;
}
void
@@ -4160,12 +4140,6 @@ wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
{
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{
if (m_winCapture)
{
m_winCapture->ReleaseMouse();
m_winCapture = NULL;
}
if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
{
ClearSelection();
@@ -4266,14 +4240,6 @@ wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
{
if ( event.Entering() || event.Leaving() )
{
// we don't care about these events but we must not reset m_isDragging
// if they happen so return before anything else is done
event.Skip();
return;
}
const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
// coordinates of the cell under mouse
@@ -4287,17 +4253,64 @@ void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
coords.SetCol(coords.GetCol() + cell_cols);
}
if ( event.Dragging() )
// Releasing the left mouse button must be processed in any case, so deal
// with it first.
if ( event.LeftUp() )
{
if ( event.LeftIsDown() )
DoGridDragEvent(event, coords);
else
event.Skip();
// Note that we must call this one first, before resetting the
// drag-related data, as it relies on m_cursorMode being still set and
// EndDraggingIfNecessary() resets it.
DoGridCellLeftUp(event, coords);
EndDraggingIfNecessary();
return;
}
m_isDragging = false;
m_startDragPos = wxDefaultPosition;
const bool isDraggingWithLeft = event.Dragging() && event.LeftIsDown();
// While dragging the mouse, only releasing the left mouse button, which
// cancels the drag operation, is processed (above) and any other events
// are just ignored while it's in progress.
if ( m_isDragging )
{
if ( isDraggingWithLeft )
DoGridDragEvent(event, coords, false /* not first drag */);
return;
}
// Now check if we're starting a drag operation (if it had been already
// started, m_isDragging would be true above).
if ( isDraggingWithLeft )
{
// To avoid accidental drags, don't start doing anything until the
// mouse has been dragged far enough.
const wxPoint& pt = event.GetPosition();
if ( m_startDragPos == wxDefaultPosition )
{
m_startDragPos = pt;
return;
}
if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
return;
if ( DoGridDragEvent(event, coords, true /* first drag */) )
{
wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
m_winCapture = m_gridWin;
m_winCapture->CaptureMouse();
m_isDragging = true;
}
return;
}
// If we're not dragging, cancel any dragging operation which could have
// been in progress.
EndDraggingIfNecessary();
// deal with various button presses
if ( event.IsButton() )
@@ -4315,12 +4328,6 @@ void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
else if ( event.RightDClick() )
SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
}
// this one should be called even if we're not over any cell
if ( event.LeftUp() )
{
DoGridCellLeftUp(event, coords);
}
}
else if ( event.Moving() )
{