From aa50c6d8294913ba933f9687aa2bccda5b94769f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 27 Mar 2021 21:55:13 +0100 Subject: [PATCH] Further improve best size of wxDatePickerCtrl with wxDP_ALLOWNONE Compute the best size more precisely to be exactly compatible with the control appearance when not using wxDP_ALLOWNONE. This unfortunately requires hardcoding an arbitrary constant in DoGetBestSize(). Patch used for testing this code for future reference: diff --git a/samples/minimal/minimal.cpp b/samples/minimal/minimal.cpp index 470e765423..a1d5fb9938 100644 --- a/samples/minimal/minimal.cpp +++ b/samples/minimal/minimal.cpp @@ -26,6 +26,8 @@ #include "wx/wx.h" #endif +#include "wx/datectrl.h" + // ---------------------------------------------------------------------------- // resources // ---------------------------------------------------------------------------- @@ -175,6 +177,28 @@ bool MyApp::OnInit() CreateStatusBar(2); SetStatusText("Welcome to wxWidgets!"); #endif // wxUSE_STATUSBAR + + auto sz = new wxBoxSizer(wxVERTICAL); + auto addDPC = [this, sz](int style) + { + sz->Add(new wxDatePickerCtrl(this, wxID_ANY, wxDefaultDateTime, + wxDefaultPosition, wxDefaultSize, style), + wxSizerFlags().Right()); + }; + + addDPC(wxDP_DROPDOWN | wxDP_ALLOWNONE | wxDP_SHOWCENTURY); + addDPC(wxDP_DROPDOWN | wxDP_SHOWCENTURY); + addDPC(wxDP_DROPDOWN | wxDP_ALLOWNONE); + addDPC(wxDP_DROPDOWN); + + sz->AddSpacer(10); + + addDPC(wxDP_SPIN | wxDP_ALLOWNONE | wxDP_SHOWCENTURY); + addDPC(wxDP_SPIN | wxDP_SHOWCENTURY); + addDPC(wxDP_SPIN | wxDP_ALLOWNONE); + addDPC(wxDP_SPIN); + + SetSizerAndFit(sz); } --- src/msw/datetimectrl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/msw/datetimectrl.cpp b/src/msw/datetimectrl.cpp index ee90f3a476..75225f056a 100644 --- a/src/msw/datetimectrl.cpp +++ b/src/msw/datetimectrl.cpp @@ -207,7 +207,12 @@ wxSize wxDateTimePickerCtrl::DoGetBestSize() const // size when using DTS_SHOWNONE than when not using it, which doesn't make // any sense at all). if ( MSWAllowsNone() ) - size.x += 3 * GetCharWidth(); + { + // The extra 10px here was determined heuristically as the value which + // results in the same layout with and without DTS_SHOWNONE under + // Windows 7 and Windows 10 with 100%, 150% and 200% scaling. + size.x += wxGetSystemMetrics(SM_CXMENUCHECK, m_parent) + 10; + } int scrollY = wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_Y, m_parent); size.y = wxMax(size.y, scrollY);