From 6d4ce92c204cd2431aea3fe3a1b417153ed5961c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 4 Nov 2020 23:27:44 +0100 Subject: [PATCH] Fix assert failures with wxDP_ALLOWNONE in the widgets sample DatePickerWidgetsPage::OnDateChanged() must check whether the wxDateTime value received from the control or event is valid before formatting it, as this may not be the case when wxDP_ALLOWNONE is on, and trying to format an invalid date results in assertion failures (and random junk in the output). --- samples/widgets/datepick.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/samples/widgets/datepick.cpp b/samples/widgets/datepick.cpp index 91b8392cdb..3bc561b472 100644 --- a/samples/widgets/datepick.cpp +++ b/samples/widgets/datepick.cpp @@ -327,11 +327,19 @@ void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event)) } } +// Helper function which has to be used here because the controls with +// wxDP_ALLOWNONE style can have invalid date, both in the control itself and +// in the event it generates. +static wxString FormatPossiblyInvalidDate(const wxDateTime& dt) +{ + return dt.IsValid() ? dt.FormatISOCombined() : wxString("[none]"); +} + void DatePickerWidgetsPage::OnDateChanged(wxDateEvent& event) { wxLogMessage("Date changed, now is %s (control value is %s).", - event.GetDate().FormatISOCombined(), - m_datePicker->GetValue().FormatISOCombined()); + FormatPossiblyInvalidDate(event.GetDate()), + FormatPossiblyInvalidDate(m_datePicker->GetValue())); } #endif // wxUSE_DATEPICKCTRL