From 25e46d0a4d9270d24c7d0d50de037a9aedd3123d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 28 Apr 2022 23:31:32 +0100 Subject: [PATCH] Improve wxListCtrl::MSWGetBestViewRect() fit It looks like ListView_ApproximateViewRect() doesn't actually take the borders into account and doesn't even take margins into account correctly. With this change, vertical wxListbook in the notebook sample doesn't show any horizontal scrollbar and has symmetric margins around its contents (whether it shows a vertical scrollbar or not) under both Windows 7 and 10. --- src/msw/listctrl.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 7c8369e5ed..8e2dc51f8d 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -1611,10 +1611,16 @@ wxSize wxListCtrl::MSWGetBestViewRect(int x, int y) const if ( mswStyle & WS_VSCROLL ) size.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X, m_parent); - // OTOH we have to subtract the size of our borders because the base class - // public method already adds them, but ListView_ApproximateViewRect() - // already takes the borders into account, so this would be superfluous. - return size - GetWindowBorderSize(); + // This is a dirty hack, but while the size returned by the control does + // fit its contents, it results in asymmetric horizontal margins around it, + // with 3px on one side and just 1px on the other one. Adding these 2px + // makes it looks nicely symmetrical, at least under Windows 7 and 10. + // Vertical margins are even more asymmetric, but they're too big and not + // too small and it might be a bad idea to allocate size smaller than what + // the control thinks it needs, so leave them be. + size.IncBy(2, 0); + + return size; } // ----------------------------------------------------------------------------