From 69a05b034007a0362c25b2419121bb655ce5736c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 6 Apr 2020 02:36:56 +0200 Subject: [PATCH] Use the same selection expansion logic for Shift-Ctrl-cursor Expanding the selection from keyboard with Ctrl pressed should move in the same way Ctrl-cursor does, but use the same selection anchor as Shift-cursor does instead of always using the current cell. This makes the expansion work much more intuitively in the grid, e.g. pressing Shift-Ctrl-Down in 1 2 3 4 grid when 1 and 2 are selected now selects all the cells instead of selecting 1 and 3 as it did before. --- include/wx/generic/grid.h | 6 +++++ src/generic/grid.cpp | 57 ++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index e21baacd6b..946236d23c 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -2767,6 +2767,12 @@ private: wxGridWindow *gridWindow) const; int PosToEdgeOfLine(int pos, const wxGridOperations& oper) const; + // Fill the coords with the cell coordinates to use for the movement + // extending the current selection. Return false if, for whatever reason, + // we can't expand the selection at all. + bool PrepareForSelectionExpansion(wxGridCellCoords& coords, + const wxGridDirectionOperations& diroper); + void DoMoveCursorFromKeyboard(const wxKeyboardState& kbdState, const wxGridDirectionOperations& diroper); bool DoMoveCursor(const wxKeyboardState& kbdState, diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index bff2bbfd27..622795cfa7 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -7914,6 +7914,29 @@ wxKeyboardState DummyKeyboardState(bool expandSelection) } // anonymous namespace +bool +wxGrid::PrepareForSelectionExpansion(wxGridCellCoords& coords, + const wxGridDirectionOperations& diroper) +{ + coords.SetRow(m_selection->GetCurrentBlockCornerRow()); + coords.SetCol(m_selection->GetCurrentBlockCornerCol()); + + if ( coords == wxGridNoCellCoords ) + coords = m_currentCellCoords; + else if ( !diroper.IsValid(coords) ) + { + // The component of the current block corner in our direction + // is not valid. This means we can't change the selection block + // in this direction. + return false; + } + + if ( diroper.IsAtBoundary(coords) ) + return false; + + return true; +} + void wxGrid::DoMoveCursorFromKeyboard(const wxKeyboardState& kbdState, const wxGridDirectionOperations& diroper) @@ -7937,20 +7960,8 @@ wxGrid::DoMoveCursor(const wxKeyboardState& kbdState, if ( !m_selection ) return false; - wxGridCellCoords coords(m_selection->GetCurrentBlockCornerRow(), - m_selection->GetCurrentBlockCornerCol()); - - if ( coords == wxGridNoCellCoords ) - coords = m_currentCellCoords; - else if ( !diroper.IsValid(coords) ) - { - // The component of the current block corner in our direction - // is not valid. This means we can't change the selection block - // in this direction. - return false; - } - - if ( diroper.IsAtBoundary(coords) ) + wxGridCellCoords coords; + if ( !PrepareForSelectionExpansion(coords, diroper) ) return false; diroper.Advance(coords); @@ -8057,13 +8068,22 @@ bool wxGrid::DoMoveCursorByBlock(const wxKeyboardState& kbdState, const wxGridDirectionOperations& diroper) { - if ( !m_table || m_currentCellCoords == wxGridNoCellCoords ) + if ( !m_table ) return false; - if ( diroper.IsAtBoundary(m_currentCellCoords) ) - return false; + wxGridCellCoords coords; + + // Expand selection if Shift is pressed. + if ( kbdState.ShiftDown() ) + { + if ( !PrepareForSelectionExpansion(coords, diroper) ) + return false; + } + else + { + coords = m_currentCellCoords; + } - wxGridCellCoords coords(m_currentCellCoords); if ( m_table->IsEmpty(coords) ) { // we are in an empty cell: find the next block of non-empty cells @@ -8095,7 +8115,6 @@ wxGrid::DoMoveCursorByBlock(const wxKeyboardState& kbdState, if ( kbdState.ShiftDown() ) { - // TODO: Select the next block every time (not the same as now). if ( m_selection ) { if ( m_selection->ExtendOrCreateCurrentBlock(m_currentCellCoords,