From 1cb2ec06ee69fc0457c5afdab2019f03840671b7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 11 Apr 2020 23:59:44 +0200 Subject: [PATCH] Fix off-by-1 bugs in wxGrid::GetFirstFullyVisible{Row,Column}() These functions always returned the first partially visible line, not the first fully visible one as intended because IsXXXShown() always returned true when it applied to that line and we need to increment the index first, before calling it -- even if this requires writing the loop in a slightly uglier way. This fixes bugs in commits 0920a1646b (Make wxGrid row selecting more user friendly, 2020-03-04) and 89dd47edee (Make wxGrid column selecting more user friendly, 2020-03-04). --- src/generic/grid.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 622795cfa7..fe821cd87a 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -7848,9 +7848,13 @@ int wxGrid::GetFirstFullyVisibleRow() const if ( GetRowTop(row) - 2 < y ) { // Use the next visible row. - for ( ; row < m_numRows; ++row ) + for ( ;; ) { - if ( IsRowShown(row) ) + // But we can't go beyond the last row anyhow. + if ( row == m_numRows - 1 ) + break; + + if ( IsRowShown(++row) ) break; } } @@ -7882,9 +7886,12 @@ int wxGrid::GetFirstFullyVisibleColumn() const if ( GetColLeft(col) < x ) { // Use the next visible column. - for ( ; col < m_numCols; ++col ) + for ( ;; ) { - if ( IsColShown(GetColAt(col)) ) + if ( col == m_numCols - 1 ) + break; + + if ( IsColShown(GetColAt(++col)) ) break; } }