Don't allow drag-extending selection from deselected cell

Dragging in a grid with Ctrl key pressed starting from a previously
selected cell behaved very counterintuitively if not downright wrongly,
as the selection logic assumes that the selection anchor itself is
always selected, which wasn't true in this case.

Solve the problem by just not extending the selection at all when
starting to drag from a deselected cell. This means that Ctrl-dragging
doesn't do anything any more, but it's not a huge loss and to make it
work well while still allowing to use Ctrl-click to toggle the cell
selection, we'd need to implement a whole new and different
drag-deselect mode, as is done in Microsoft Excel 2016 (note that the
previous versions of Excel don't implement Ctrl-dragging neither).
This commit is contained in:
Vadim Zeitlin
2020-04-13 18:46:54 +02:00
parent 6d4df74a03
commit 52d1b86bbd

View File

@@ -4490,11 +4490,24 @@ wxGrid::DoGridCellDrag(wxMouseEvent& event,
// if event is handled by user code, no further processing
return SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) == 0;
}
// When Shift-dragging, we must have already selected the initial
// cell and when Ctrl-dragging we may have either selected or
// deselected it, depending on its previous state. But when
// dragging without any modifiers, we want it to start in the
// selected state even though it's not selected on a simple click.
if ( m_selection )
m_selection->SelectBlock(m_currentCellCoords, coords, event);
}
}
// Edit the current selection block independently of the modifiers state.
if ( m_selection )
// Extend the selection if possible: this is not the case if we're dragging
// from an unselected cell, as can be the case if the drag was started by
// Ctrl-clicking a previously selected cell (notice that the modifier of
// the current event is irrelevant, it's too late to change the behaviour
// by pressing or releasing Ctrl later, only its initial state, as
// indicated by the state of the starting cell, counts).
if ( m_selection && m_selection->IsInSelection(m_currentCellCoords) )
m_selection->ExtendCurrentBlock(m_currentCellCoords, coords, event);
return true;