From f3ff89601f9a56f88c84f8e9e4e98fd5be86f5c4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 11 Apr 2020 15:04:32 +0200 Subject: [PATCH] Slightly clarify ExtendOrCreateCurrentBlock() code Don't use long-lived non-const "block" reference, this makes it difficult to see that it's never changed until the very end of the function, when it's updated. Compare "blockStart" with "block" itself instead of comparing it with "newBlock" which is initially its copy but gets modified later (however these modifications don't affect the comparisons involving it). Add a comment explaining why are we doing what we do here. --- src/generic/gridsel.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/generic/gridsel.cpp b/src/generic/gridsel.cpp index f408b0b36c..e8cfdbf048 100644 --- a/src/generic/gridsel.cpp +++ b/src/generic/gridsel.cpp @@ -475,19 +475,23 @@ bool wxGridSelection::ExtendOrCreateCurrentBlock(const wxGridCellCoords& blockSt return true; } - wxGridBlockCoords& block = *m_selection.rbegin(); + const wxGridBlockCoords& block = *m_selection.rbegin(); wxGridBlockCoords newBlock = block; bool editBlock = false; if ( blockEnd.GetRow() != -1 ) { - if ( newBlock.GetTopRow() == blockStart.GetRow() ) + // If the new block starts at the same top row as the current one, the + // end block coordinates must correspond to the new bottom row -- and + // vice versa, if the new block starts at the bottom, its other end + // must correspond to the top. + if ( blockStart.GetRow() == block.GetTopRow() ) { newBlock.SetBottomRow(blockEnd.GetRow()); editBlock = true; } - else if ( newBlock.GetBottomRow() == blockStart.GetRow() ) + else if ( blockStart.GetRow() == block.GetBottomRow() ) { newBlock.SetTopRow(blockEnd.GetRow()); editBlock = true; @@ -529,8 +533,8 @@ bool wxGridSelection::ExtendOrCreateCurrentBlock(const wxGridCellCoords& blockSt } } - // Edit the current block. - block = newBlock; + // Update the current block in place. + *m_selection.rbegin() = newBlock; // Send Event. wxGridRangeSelectEvent gridEvt(m_grid->GetId(),