Refactor selection expansion code to use actual wxKeyboardState

Switch from using just "bool expandSelection" in the grid functions
(possibly) extending the current selection to using the full
wxKeyboardState.

This allows to pass it to ExtendOrCreateCurrentBlock() and slightly
simplify the code by using DoMoveCursorFromKeyboard().
This commit is contained in:
Vadim Zeitlin
2020-04-05 15:53:30 +02:00
parent ddc87d66e8
commit 1d43ae7dc6
2 changed files with 66 additions and 33 deletions

View File

@@ -2767,10 +2767,12 @@ private:
wxGridWindow *gridWindow) const; wxGridWindow *gridWindow) const;
int PosToEdgeOfLine(int pos, const wxGridOperations& oper) const; int PosToEdgeOfLine(int pos, const wxGridOperations& oper) const;
bool DoMoveCursor(bool expandSelection, void DoMoveCursorFromKeyboard(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper);
bool DoMoveCursor(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper); const wxGridDirectionOperations& diroper);
bool DoMoveCursorByPage(const wxGridDirectionOperations& diroper); bool DoMoveCursorByPage(const wxGridDirectionOperations& diroper);
bool DoMoveCursorByBlock(bool expandSelection, bool DoMoveCursorByBlock(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper); const wxGridDirectionOperations& diroper);
void AdvanceToNextNonEmpty(wxGridCellCoords& coords, void AdvanceToNextNonEmpty(wxGridCellCoords& coords,
const wxGridDirectionOperations& diroper); const wxGridDirectionOperations& diroper);

View File

@@ -5650,31 +5650,35 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
switch ( event.GetKeyCode() ) switch ( event.GetKeyCode() )
{ {
case WXK_UP: case WXK_UP:
if ( event.ControlDown() ) DoMoveCursorFromKeyboard
MoveCursorUpBlock( event.ShiftDown() ); (
else event,
MoveCursorUp( event.ShiftDown() ); wxGridBackwardOperations(this, wxGridRowOperations())
);
break; break;
case WXK_DOWN: case WXK_DOWN:
if ( event.ControlDown() ) DoMoveCursorFromKeyboard
MoveCursorDownBlock( event.ShiftDown() ); (
else event,
MoveCursorDown( event.ShiftDown() ); wxGridForwardOperations(this, wxGridRowOperations())
);
break; break;
case WXK_LEFT: case WXK_LEFT:
if ( event.ControlDown() ) DoMoveCursorFromKeyboard
MoveCursorLeftBlock( event.ShiftDown() ); (
else event,
MoveCursorLeft( event.ShiftDown() ); wxGridBackwardOperations(this, wxGridColumnOperations())
);
break; break;
case WXK_RIGHT: case WXK_RIGHT:
if ( event.ControlDown() ) DoMoveCursorFromKeyboard
MoveCursorRightBlock( event.ShiftDown() ); (
else event,
MoveCursorRight( event.ShiftDown() ); wxGridForwardOperations(this, wxGridColumnOperations())
);
break; break;
case WXK_RETURN: case WXK_RETURN:
@@ -7893,14 +7897,42 @@ int wxGrid::GetFirstFullyVisibleColumn() const
// ------ Grid cursor movement functions // ------ Grid cursor movement functions
// //
namespace
{
// Helper function creating dummy wxKeyboardState object corresponding to the
// value of "expandSelection" flag specified to our public MoveCursorXXX().
// This function only exists to avoid breaking compatibility in the public API,
// all internal code should use the real, not dummy, wxKeyboardState.
inline
wxKeyboardState DummyKeyboardState(bool expandSelection)
{
// Normally "expandSelection" is set depending on whether Shift is pressed
// or not, but here we reconstruct the keyboard state from this flag.
return wxKeyboardState(false /* control */, expandSelection /* shift */);
}
} // anonymous namespace
void
wxGrid::DoMoveCursorFromKeyboard(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper)
{
if ( kbdState.ControlDown() )
DoMoveCursorByBlock(kbdState, diroper);
else
DoMoveCursor(kbdState, diroper);
}
bool bool
wxGrid::DoMoveCursor(bool expandSelection, wxGrid::DoMoveCursor(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper) const wxGridDirectionOperations& diroper)
{ {
if ( m_currentCellCoords == wxGridNoCellCoords ) if ( m_currentCellCoords == wxGridNoCellCoords )
return false; return false;
if ( expandSelection ) // Expand selection if Shift is pressed.
if ( kbdState.ShiftDown() )
{ {
if ( !m_selection ) if ( !m_selection )
return false; return false;
@@ -7925,7 +7957,7 @@ wxGrid::DoMoveCursor(bool expandSelection,
if ( m_selection->ExtendOrCreateCurrentBlock(m_currentCellCoords, if ( m_selection->ExtendOrCreateCurrentBlock(m_currentCellCoords,
coords, coords,
wxKeyboardState()) ) kbdState) )
{ {
// We want to show a line (a row or a column), not the end of // We want to show a line (a row or a column), not the end of
// the selection block. And do it only if the selection block // the selection block. And do it only if the selection block
@@ -7951,25 +7983,25 @@ wxGrid::DoMoveCursor(bool expandSelection,
bool wxGrid::MoveCursorUp(bool expandSelection) bool wxGrid::MoveCursorUp(bool expandSelection)
{ {
return DoMoveCursor(expandSelection, return DoMoveCursor(DummyKeyboardState(expandSelection),
wxGridBackwardOperations(this, wxGridRowOperations())); wxGridBackwardOperations(this, wxGridRowOperations()));
} }
bool wxGrid::MoveCursorDown(bool expandSelection) bool wxGrid::MoveCursorDown(bool expandSelection)
{ {
return DoMoveCursor(expandSelection, return DoMoveCursor(DummyKeyboardState(expandSelection),
wxGridForwardOperations(this, wxGridRowOperations())); wxGridForwardOperations(this, wxGridRowOperations()));
} }
bool wxGrid::MoveCursorLeft(bool expandSelection) bool wxGrid::MoveCursorLeft(bool expandSelection)
{ {
return DoMoveCursor(expandSelection, return DoMoveCursor(DummyKeyboardState(expandSelection),
wxGridBackwardOperations(this, wxGridColumnOperations())); wxGridBackwardOperations(this, wxGridColumnOperations()));
} }
bool wxGrid::MoveCursorRight(bool expandSelection) bool wxGrid::MoveCursorRight(bool expandSelection)
{ {
return DoMoveCursor(expandSelection, return DoMoveCursor(DummyKeyboardState(expandSelection),
wxGridForwardOperations(this, wxGridColumnOperations())); wxGridForwardOperations(this, wxGridColumnOperations()));
} }
@@ -8022,7 +8054,7 @@ wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords,
} }
bool bool
wxGrid::DoMoveCursorByBlock(bool expandSelection, wxGrid::DoMoveCursorByBlock(const wxKeyboardState& kbdState,
const wxGridDirectionOperations& diroper) const wxGridDirectionOperations& diroper)
{ {
if ( !m_table || m_currentCellCoords == wxGridNoCellCoords ) if ( !m_table || m_currentCellCoords == wxGridNoCellCoords )
@@ -8061,15 +8093,14 @@ wxGrid::DoMoveCursorByBlock(bool expandSelection,
} }
} }
if ( expandSelection ) if ( kbdState.ShiftDown() )
{ {
// TODO: Select the next block every time (not the same as now). // TODO: Select the next block every time (not the same as now).
// And provide the keyboard state.
if ( m_selection ) if ( m_selection )
{ {
if ( m_selection->ExtendOrCreateCurrentBlock(m_currentCellCoords, if ( m_selection->ExtendOrCreateCurrentBlock(m_currentCellCoords,
coords, coords,
wxKeyboardState()) ) kbdState) )
{ {
// We want to show a line (a row or a column), not the end of // We want to show a line (a row or a column), not the end of
// the selection block. And do it only if the selection block // the selection block. And do it only if the selection block
@@ -8090,7 +8121,7 @@ wxGrid::DoMoveCursorByBlock(bool expandSelection,
bool wxGrid::MoveCursorUpBlock(bool expandSelection) bool wxGrid::MoveCursorUpBlock(bool expandSelection)
{ {
return DoMoveCursorByBlock( return DoMoveCursorByBlock(
expandSelection, DummyKeyboardState(expandSelection),
wxGridBackwardOperations(this, wxGridRowOperations()) wxGridBackwardOperations(this, wxGridRowOperations())
); );
} }
@@ -8098,7 +8129,7 @@ bool wxGrid::MoveCursorUpBlock(bool expandSelection)
bool wxGrid::MoveCursorDownBlock( bool expandSelection ) bool wxGrid::MoveCursorDownBlock( bool expandSelection )
{ {
return DoMoveCursorByBlock( return DoMoveCursorByBlock(
expandSelection, DummyKeyboardState(expandSelection),
wxGridForwardOperations(this, wxGridRowOperations()) wxGridForwardOperations(this, wxGridRowOperations())
); );
} }
@@ -8106,7 +8137,7 @@ bool wxGrid::MoveCursorDownBlock( bool expandSelection )
bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
{ {
return DoMoveCursorByBlock( return DoMoveCursorByBlock(
expandSelection, DummyKeyboardState(expandSelection),
wxGridBackwardOperations(this, wxGridColumnOperations()) wxGridBackwardOperations(this, wxGridColumnOperations())
); );
} }
@@ -8114,7 +8145,7 @@ bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
bool wxGrid::MoveCursorRightBlock( bool expandSelection ) bool wxGrid::MoveCursorRightBlock( bool expandSelection )
{ {
return DoMoveCursorByBlock( return DoMoveCursorByBlock(
expandSelection, DummyKeyboardState(expandSelection),
wxGridForwardOperations(this, wxGridColumnOperations()) wxGridForwardOperations(this, wxGridColumnOperations())
); );
} }