From 340d7f67b6100c7b6fa7ef2af4053676977ef7c8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Dec 2014 22:17:17 +0000 Subject: [PATCH] Use DTM_GETIDEALSIZE to implement wxDateTimePickerCtrl::DoGetBestSize(). If possible, i.e. when running under Vista or later, just ask the control for its best size instead of trying to approximate it ourselves. Notice that we still use our own height, to ensure that it's the same as for the text controls, but it's the width that really counts. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78221 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/datetimectrl.cpp | 52 +++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/msw/datetimectrl.cpp b/src/msw/datetimectrl.cpp index 83cca78f1f..37407cf1ec 100644 --- a/src/msw/datetimectrl.cpp +++ b/src/msw/datetimectrl.cpp @@ -44,6 +44,10 @@ #define DateTime_SetSystemtime DateTime_SetSystemTime #endif +#ifndef DTM_GETIDEALSIZE + #define DTM_GETIDEALSIZE 0x100f +#endif + // ============================================================================ // wxDateTimePickerCtrl implementation // ============================================================================ @@ -108,32 +112,48 @@ wxDateTime wxDateTimePickerCtrl::GetValue() const wxSize wxDateTimePickerCtrl::DoGetBestSize() const { - wxClientDC dc(const_cast(this)); + // Since Vista, the control can compute its best size itself, just ask it. + wxSize size; + if ( wxGetWinVersion() >= wxWinVersion_Vista ) + { + SIZE idealSize; + ::SendMessage(m_hWnd, DTM_GETIDEALSIZE, 0, (LPARAM)&idealSize); - // Use the same native format as the underlying native control. + size = wxSize(idealSize.cx, idealSize.cy); + } + else // Windows XP + { + wxClientDC dc(const_cast(this)); + + // Use the same native format as the underlying native control. #if wxUSE_INTL - wxString s = wxDateTime::Now().Format(wxLocale::GetOSInfo(MSWGetFormat())); + wxString s = wxDateTime::Now().Format(wxLocale::GetOSInfo(MSWGetFormat())); #else // !wxUSE_INTL - wxString s("XXX-YYY-ZZZZ"); + wxString s("XXX-YYY-ZZZZ"); #endif // wxUSE_INTL/!wxUSE_INTL - // the best size for the control is bigger than just the string - // representation of the current value because the control must accommodate - // any date and while the widths of all digits are usually about the same, - // the width of the month string varies a lot, so try to account for it - s += wxT("WW"); + // the best size for the control is bigger than just the string + // representation of the current value because the control must accommodate + // any date and while the widths of all digits are usually about the same, + // the width of the month string varies a lot, so try to account for it + s += wxT("WW"); - int x, y; - dc.GetTextExtent(s, &x, &y); + size = dc.GetTextExtent(s); - // account for the drop-down arrow or spin arrows - x += wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_X); + // account for the drop-down arrow or spin arrows + size.x += wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_X); + } - // and for the checkbox if we have it + // We need to account for the checkbox, if we have one, ourselves as + // DTM_GETIDEALSIZE doesn't seem to take it into account, at least under + // Windows 7. if ( MSWAllowsNone() ) - x += 3*GetCharWidth(); + size.x += 3*GetCharWidth(); - return wxSize(x, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y)); + // In any case, adjust the height to be the same as for the text controls. + size.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(size.y); + + return size; } bool