From d6aec06150a96554f670d54fe4ed6af032ad3868 Mon Sep 17 00:00:00 2001 From: Ilya Sinitsyn Date: Fri, 14 Aug 2020 20:10:26 +0700 Subject: [PATCH 1/3] Process the wxGrid mouse click depending on the current mode Simplify the code of wxGrid::DoGridCellLeftDown() and make it more correct by using m_cursorMode instead of duplicating (not 100% correctly) the logic used to set it in DoGridMouseMoveEvent(). If the assumption that m_cursorMode is already set by the time DoGridCellLeftDown() is called turns out to be wrong, e.g. when wxGrid is used on touch screens, we would need to refactor these functions to extract the code determining m_cursorMode from the mouse location into a separate function that would be called from both. Note that this commit is best viewed with "git diff -w" to see how little has really changed. --- src/generic/grid.cpp | 159 ++++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 76 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 464f3d2c7e..72c948572a 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -4551,91 +4551,98 @@ wxGrid::DoGridCellLeftDown(wxMouseEvent& event, return; } - if ( event.ShiftDown() && !event.CmdDown() ) + // Process the mouse down event depending on the current cursor mode. Note + // that this assumes m_cursorMode was set in the mouse move event hendler. + switch ( m_cursorMode ) { - if ( m_selection ) - { - m_selection->ExtendCurrentBlock(m_currentCellCoords, coords, event); - MakeCellVisible(coords); - } - } - else - { - // Clicking on (or very near) the separating lines shouldn't change the - // selection when it's used for resizing -- but should still do it if - // resizing is disabled (notice that we intentionally don't check for - // it being disabled for a particular row/column as it would be - // surprising to have different mouse behaviour in different parts of - // the same grid, so we only check for it being globally disabled). - int dragRowOrCol = wxNOT_FOUND; - if ( CanDragGridColEdges() ) - dragRowOrCol = XToEdgeOfCol(pos.x); - - if ( dragRowOrCol == wxNOT_FOUND && CanDragGridRowEdges() ) - dragRowOrCol = YToEdgeOfRow(pos.y); - - if ( dragRowOrCol != wxNOT_FOUND ) - { - DoStartResizeRowOrCol(dragRowOrCol); - return; - } - - DisableCellEditControl(); - MakeCellVisible( coords ); - - if ( event.CmdDown() && !event.ShiftDown() ) - { - if ( m_selection ) + case WXGRID_CURSOR_RESIZE_ROW: + case WXGRID_CURSOR_RESIZE_COL: { - if ( !m_selection->IsInSelection(coords) ) + int dragRowOrCol = wxNOT_FOUND; + if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) + dragRowOrCol = XToEdgeOfCol(pos.x); + else + dragRowOrCol = YToEdgeOfRow(pos.y); + wxCHECK_RET( dragRowOrCol != -1, "Can't determine row or column in resizing mode" ); + + DoStartResizeRowOrCol(dragRowOrCol); + } + break; + + case WXGRID_CURSOR_SELECT_CELL: + case WXGRID_CURSOR_SELECT_ROW: + case WXGRID_CURSOR_SELECT_COL: + DisableCellEditControl(); + MakeCellVisible(coords); + + if (event.ShiftDown() && !event.CmdDown()) + { + if (m_selection) { - // If the cell is not selected, select it. - m_selection->SelectBlock(coords.GetRow(), coords.GetCol(), - coords.GetRow(), coords.GetCol(), - event); + m_selection->ExtendCurrentBlock(m_currentCellCoords, coords, event); + } + } + else + { + if ( event.CmdDown() && !event.ShiftDown() ) + { + if ( m_selection ) + { + if ( !m_selection->IsInSelection(coords) ) + { + // If the cell is not selected, select it. + m_selection->SelectBlock(coords.GetRow(), coords.GetCol(), + coords.GetRow(), coords.GetCol(), + event); + } + else + { + // Otherwise deselect it. + m_selection->DeselectBlock( + wxGridBlockCoords(coords.GetRow(), coords.GetCol(), + coords.GetRow(), coords.GetCol()), + event); + } + } } else { - // Otherwise deselect it. - m_selection->DeselectBlock( - wxGridBlockCoords(coords.GetRow(), coords.GetCol(), - coords.GetRow(), coords.GetCol()), - event); + ClearSelection(); + + if ( m_selection ) + { + // In row or column selection mode just clicking on the cell + // should select the row or column containing it: this is more + // convenient for the kinds of controls that use such selection + // mode and is compatible with 2.8 behaviour (see #12062). + switch ( m_selection->GetSelectionMode() ) + { + case wxGridSelectCells: + case wxGridSelectRowsOrColumns: + // nothing to do in these cases + break; + + case wxGridSelectRows: + m_selection->SelectRow(coords.GetRow()); + break; + + case wxGridSelectColumns: + m_selection->SelectCol(coords.GetCol()); + break; + } + } + + m_waitForSlowClick = m_currentCellCoords == coords && + coords != wxGridNoCellCoords; } + + SetCurrentCell(coords); } - } - else - { - ClearSelection(); + break; - if ( m_selection ) - { - // In row or column selection mode just clicking on the cell - // should select the row or column containing it: this is more - // convenient for the kinds of controls that use such selection - // mode and is compatible with 2.8 behaviour (see #12062). - switch ( m_selection->GetSelectionMode() ) - { - case wxGridSelectCells: - case wxGridSelectRowsOrColumns: - // nothing to do in these cases - break; - - case wxGridSelectRows: - m_selection->SelectRow(coords.GetRow()); - break; - - case wxGridSelectColumns: - m_selection->SelectCol(coords.GetCol()); - break; - } - } - - m_waitForSlowClick = m_currentCellCoords == coords && - coords != wxGridNoCellCoords; - } - - SetCurrentCell(coords); + case WXGRID_CURSOR_MOVE_COL: + // Nothing to do here for this case. + break; } } From 5d3d376766b3f0bd42730693ed6b63674c5a1cf3 Mon Sep 17 00:00:00 2001 From: Ilya Sinitsyn Date: Sat, 15 Aug 2020 02:49:06 +0700 Subject: [PATCH 2/3] Remove unused wxGrid function --- include/wx/generic/grid.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 935180d8cf..bc8d9b2a50 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -3003,7 +3003,6 @@ private: void DoGridProcessTab(wxKeyboardState& kbdState); // common implementations of methods defined for both rows and columns - bool DoEndDragResizeLine(const wxGridOperations& oper, wxGridWindow *gridWindow); int PosToLinePos(int pos, bool clipToMinMax, const wxGridOperations& oper, wxGridWindow *gridWindow) const; From 0e8ca0c8f932c34167fae827c5807a2337963d94 Mon Sep 17 00:00:00 2001 From: Ilya Sinitsyn Date: Mon, 17 Aug 2020 21:12:02 +0700 Subject: [PATCH 3/3] Fix the cursor mode switching in wxGrid mouse move event handler Allow switching between row and column resizing modes in mouse move event handler. --- src/generic/grid.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 72c948572a..051c7cc7d6 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -4714,14 +4714,14 @@ wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event), // cell corner, as this is a more common operation. if ( dragCol >= 0 && CanDragGridColEdges() && CanDragColSize(dragCol) ) { - if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) + if ( m_cursorMode != WXGRID_CURSOR_RESIZE_COL ) { ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, gridWindow, false); } } else if ( dragRow >= 0 && CanDragGridRowEdges() && CanDragRowSize(dragRow) ) { - if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) + if ( m_cursorMode != WXGRID_CURSOR_RESIZE_ROW) { ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, gridWindow, false); }