Remove the selecting mode flag from wxGridSelection

Remove m_isSelecting and StartSelecting() from wxGridSelection to avoid
inconsistency of wxGridSelection selecting mode state with wxGrid state,
as wxGrid already has m_isDragging field which tells it if the selection
state is final or not.

Instead, just allow wxGrid to specify the event to send from
ExtendCurrentBlock().

We still need a separate EndSelecting() for sending the final
wxEVT_GRID_RANGE_SELECTED event, but send it only for the last selection
block, and not all the selected blocks, as this makes more sense (there
should be one SELECTED event for each block and it was already sent for
the other blocks before) and is consistent with the events generated
when performing the same actions from keyboard.
This commit is contained in:
Ilya Sinitsyn
2020-08-18 00:30:00 +07:00
committed by Vadim Zeitlin
parent decc255846
commit 0a2c62fc5c
3 changed files with 34 additions and 52 deletions

View File

@@ -82,10 +82,16 @@ public:
// //
// Both components of both blockStart and blockEnd must be valid. // Both components of both blockStart and blockEnd must be valid.
// //
// This function sends an event notifying about the selection change using
// the provided event type, which is wxEVT_GRID_RANGE_SELECTED by default,
// but may also be wxEVT_GRID_RANGE_SELECTING, when the selection is not
// final yet.
//
// Return true if the current block was actually changed. // Return true if the current block was actually changed.
bool ExtendCurrentBlock(const wxGridCellCoords& blockStart, bool ExtendCurrentBlock(const wxGridCellCoords& blockStart,
const wxGridCellCoords& blockEnd, const wxGridCellCoords& blockEnd,
const wxKeyboardState& kbd); const wxKeyboardState& kbd,
wxEventType eventType = wxEVT_GRID_RANGE_SELECTED);
// Return the coordinates of the cell from which the selection should // Return the coordinates of the cell from which the selection should
@@ -103,9 +109,8 @@ public:
wxVectorGridBlockCoords& GetBlocks() { return m_selection; } wxVectorGridBlockCoords& GetBlocks() { return m_selection; }
void StartSelecting();
void EndSelecting(); void EndSelecting();
private: private:
void SelectBlockNoEvent(const wxGridBlockCoords& block) void SelectBlockNoEvent(const wxGridBlockCoords& block)
{ {
@@ -141,7 +146,6 @@ private:
wxGrid *m_grid; wxGrid *m_grid;
wxGrid::wxGridSelectionModes m_selectionMode; wxGrid::wxGridSelectionModes m_selectionMode;
bool m_isSelecting;
wxDECLARE_NO_COPY_CLASS(wxGridSelection); wxDECLARE_NO_COPY_CLASS(wxGridSelection);
}; };

View File

@@ -4384,7 +4384,7 @@ void wxGrid::DoAfterDraggingEnd()
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
m_winCapture->SetCursor( *wxSTANDARD_CURSOR ); m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
m_winCapture = NULL; m_winCapture = NULL;
m_selection->EndSelecting(); m_selection->EndSelecting();
} }
@@ -4514,11 +4514,10 @@ wxGrid::DoGridCellDrag(wxMouseEvent& event,
// of the modifier keys matters. // of the modifier keys matters.
if ( m_selection ) if ( m_selection )
{ {
if (isFirstDrag) m_selection->ExtendCurrentBlock(m_currentCellCoords,
{ coords,
m_selection->StartSelecting(); event,
} wxEVT_GRID_RANGE_SELECTING);
m_selection->ExtendCurrentBlock(m_currentCellCoords, coords, event);
} }
return true; return true;
@@ -4823,7 +4822,6 @@ void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event, wxGridWindow *eventG
m_winCapture->CaptureMouse(); m_winCapture->CaptureMouse();
m_isDragging = true; m_isDragging = true;
m_selection->StartSelecting();
} }
return; return;

View File

@@ -48,7 +48,6 @@ wxGridSelection::wxGridSelection( wxGrid * grid,
{ {
m_grid = grid; m_grid = grid;
m_selectionMode = sel; m_selectionMode = sel;
m_isSelecting = false;
} }
bool wxGridSelection::IsSelection() bool wxGridSelection::IsSelection()
@@ -56,46 +55,26 @@ bool wxGridSelection::IsSelection()
return !m_selection.empty(); return !m_selection.empty();
} }
void wxGridSelection::StartSelecting()
{
m_isSelecting = true;
}
void wxGridSelection::EndSelecting() void wxGridSelection::EndSelecting()
{ {
m_isSelecting = false; // It's possible that nothing was selected finally, e.g. the mouse could
//send the RANGE_SELECTED events // have been dragged around only to return to the starting cell, just don't
if (IsSelection()) // do anything in this case.
{ if ( !IsSelection() )
wxRect r; return;
wxGridCellCoords coords1, coords2;
// Send selection events for all the selected blocks. // Send RANGE_SELECTED event for the last modified block.
const size_t count = m_selection.size(); const wxGridBlockCoords& block = m_selection.back();
for ( size_t n = 0; n < count; n++ ) wxGridRangeSelectEvent gridEvt(m_grid->GetId(),
{ wxEVT_GRID_RANGE_SELECTED,
const wxGridBlockCoords& block = m_selection[n]; m_grid,
coords1 = block.GetTopLeft(); block.GetTopLeft(),
coords2 = block.GetBottomRight(); block.GetBottomRight(),
if ( !m_grid->GetBatchCount() ) true);
{
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), m_grid->GetEventHandler()->ProcessEvent(gridEvt);
wxEVT_GRID_RANGE_SELECTED,
m_grid,
coords1,
coords2,
true );
m_grid->GetEventHandler()->ProcessEvent(gridEvt);
}
}
}
else
{
ClearSelection();
}
} }
bool wxGridSelection::IsInSelection( int row, int col ) const bool wxGridSelection::IsInSelection( int row, int col ) const
{ {
// Check whether the given cell is contained in one of the selected blocks. // Check whether the given cell is contained in one of the selected blocks.
@@ -385,7 +364,7 @@ wxGridSelection::DeselectBlock(const wxGridBlockCoords& block,
if ( sendEvent ) if ( sendEvent )
{ {
wxGridRangeSelectEvent gridEvt(m_grid->GetId(), wxGridRangeSelectEvent gridEvt(m_grid->GetId(),
m_isSelecting ? wxEVT_GRID_RANGE_SELECTING : wxEVT_GRID_RANGE_SELECTED, wxEVT_GRID_RANGE_SELECTED,
m_grid, m_grid,
refBlock.GetTopLeft(), refBlock.GetTopLeft(),
refBlock.GetBottomRight(), refBlock.GetBottomRight(),
@@ -424,7 +403,7 @@ void wxGridSelection::ClearSelection()
// (No finer grained events for each of the smaller regions // (No finer grained events for each of the smaller regions
// deselected above!) // deselected above!)
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
m_isSelecting ? wxEVT_GRID_RANGE_SELECTING : wxEVT_GRID_RANGE_SELECTED, wxEVT_GRID_RANGE_SELECTED,
m_grid, m_grid,
wxGridCellCoords( 0, 0 ), wxGridCellCoords( 0, 0 ),
wxGridCellCoords( wxGridCellCoords(
@@ -535,7 +514,8 @@ void wxGridSelection::UpdateCols( size_t pos, int numCols )
bool wxGridSelection::ExtendCurrentBlock(const wxGridCellCoords& blockStart, bool wxGridSelection::ExtendCurrentBlock(const wxGridCellCoords& blockStart,
const wxGridCellCoords& blockEnd, const wxGridCellCoords& blockEnd,
const wxKeyboardState& kbd) const wxKeyboardState& kbd,
wxEventType eventType)
{ {
wxASSERT( blockStart.GetRow() != -1 && blockStart.GetCol() != -1 && wxASSERT( blockStart.GetRow() != -1 && blockStart.GetCol() != -1 &&
blockEnd.GetRow() != -1 && blockEnd.GetCol() != -1 ); blockEnd.GetRow() != -1 && blockEnd.GetCol() != -1 );
@@ -682,7 +662,7 @@ bool wxGridSelection::ExtendCurrentBlock(const wxGridCellCoords& blockStart,
// Send Event. // Send Event.
wxGridRangeSelectEvent gridEvt(m_grid->GetId(), wxGridRangeSelectEvent gridEvt(m_grid->GetId(),
m_isSelecting ? wxEVT_GRID_RANGE_SELECTING : wxEVT_GRID_RANGE_SELECTED, eventType,
m_grid, m_grid,
newBlock.GetTopLeft(), newBlock.GetTopLeft(),
newBlock.GetBottomRight(), newBlock.GetBottomRight(),
@@ -855,7 +835,7 @@ wxGridSelection::Select(const wxGridBlockCoords& block,
if ( sendEvent ) if ( sendEvent )
{ {
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
m_isSelecting ? wxEVT_GRID_RANGE_SELECTING : wxEVT_GRID_RANGE_SELECTED, wxEVT_GRID_RANGE_SELECTED,
m_grid, m_grid,
block.GetTopLeft(), block.GetTopLeft(),
block.GetBottomRight(), block.GetBottomRight(),