From e70be8f82bc65840777d9a60be1f94cdb9525dee Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 3 Jul 2021 15:52:04 +0100 Subject: [PATCH 1/2] Fix indentation in wxGrid mouse processing code Indent "break" statements correctly and remove unnecessary braces. No real changes. This commit is best viewed ignoring whitespace-only changes. --- src/generic/grid.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index a487162a5e..dff8accf66 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -3828,13 +3828,10 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event, wxGridRowLabelWindo switch ( m_cursorMode ) { case WXGRID_CURSOR_RESIZE_ROW: - { DoGridDragResize(event.GetPosition(), wxGridRowOperations(), gridWindow); - } - break; + break; case WXGRID_CURSOR_SELECT_ROW: - { if ( !m_selection || m_numRows == 0 || m_numCols == 0 ) break; @@ -3851,8 +3848,7 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event, wxGridRowLabelWindo event, wxEVT_GRID_RANGE_SELECTING); } - } - break; + break; // default label to suppress warnings about "enumeration value // 'xxx' not handled in switch @@ -4172,10 +4168,9 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event, wxGridColLabelWindo { case WXGRID_CURSOR_RESIZE_COL: DoGridDragResize(event.GetPosition(), wxGridColumnOperations(), gridWindow); - break; + break; case WXGRID_CURSOR_SELECT_COL: - { if ( col != -1 ) { if ( !m_selection || m_numRows == 0 || m_numCols == 0 ) @@ -4193,8 +4188,7 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event, wxGridColLabelWindo event, wxEVT_GRID_RANGE_SELECTING); } - } - break; + break; case WXGRID_CURSOR_MOVE_COL: { From 5cac8a6918d0973ce45de80c1e8daf39a9516386 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 3 Jul 2021 16:31:15 +0100 Subject: [PATCH 2/2] Enforce valid drag column/row index in DoGridDragResize() Add an assert to this function checking that the index is valid before using it with wxGridOperations::GetLineStartPos(), and actually avoid calling the function when this is not the case to avoid assertion failures when wxEVT_GRID_CELL_LEFT_CLICK is handled in user code. Also add comments clarifying the preconditions for calling various drag-related functions. Closes #19218. --- include/wx/generic/grid.h | 4 ++++ src/generic/grid.cpp | 15 +++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index d7a3890764..d92ad689f4 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -2951,6 +2951,8 @@ private: wxGridWindow* gridWindow); // Update the width/height of the column/row being drag-resized. + // Should be only called when m_dragRowOrCol != -1, i.e. dragging is + // actually in progress. void DoGridDragResize(const wxPoint& position, const wxGridOperations& oper, wxGridWindow* gridWindow); @@ -2989,6 +2991,8 @@ private: void DoStartResizeRowOrCol(int col); void DoStartMoveCol(int col); + // These functions should only be called when actually resizing/moving, + // i.e. m_dragRowOrCol and m_dragMoveCol, respectively, are valid. void DoEndDragResizeRow(const wxMouseEvent& event, wxGridWindow *gridWindow); void DoEndDragResizeCol(const wxMouseEvent& event, wxGridWindow *gridWindow); void DoEndMoveCol(int pos); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index dff8accf66..b4b4fcdcc8 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -4707,11 +4707,13 @@ bool wxGrid::DoGridDragEvent(wxMouseEvent& event, return DoGridCellDrag(event, coords, isFirstDrag); case WXGRID_CURSOR_RESIZE_ROW: - DoGridDragResize(event.GetPosition(), wxGridRowOperations(), gridWindow); + if ( m_dragRowOrCol != -1 ) + DoGridDragResize(event.GetPosition(), wxGridRowOperations(), gridWindow); break; case WXGRID_CURSOR_RESIZE_COL: - DoGridDragResize(event.GetPosition(), wxGridColumnOperations(), gridWindow); + if ( m_dragRowOrCol != -1 ) + DoGridDragResize(event.GetPosition(), wxGridColumnOperations(), gridWindow); break; default: @@ -4864,12 +4866,14 @@ wxGrid::DoGridCellLeftUp(wxMouseEvent& event, else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) { ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); - DoEndDragResizeRow(event, gridWindow); + if ( m_dragRowOrCol != -1 ) + DoEndDragResizeRow(event, gridWindow); } else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) { ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL); - DoEndDragResizeCol(event, gridWindow); + if ( m_dragRowOrCol != -1 ) + DoEndDragResizeCol(event, gridWindow); } m_dragLastPos = -1; @@ -5047,6 +5051,9 @@ void wxGrid::DoGridDragResize(const wxPoint& position, const wxGridOperations& oper, wxGridWindow* gridWindow) { + wxCHECK_RET( m_dragRowOrCol != -1, + "shouldn't be called when not drag resizing" ); + // Get the logical position from the physical one we're passed. const wxPoint logicalPos = CalcGridWindowUnscrolledPosition(position, gridWindow);