From d7351536c1a1f1eb3c46966e7baad386f2965861 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 30 Jul 2018 22:32:29 +0200 Subject: [PATCH] Change the data in generic wxDatePickerCtrl immediately Accept the new date typed into the text control immediately if it's valid. This is more consistent with the native MSW control behaviour and avoids semi-duplicated events on text change and then on focus loss that occurred before. --- src/generic/datectlg.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/generic/datectlg.cpp b/src/generic/datectlg.cpp index f50a1bba65..5ddadc3933 100644 --- a/src/generic/datectlg.cpp +++ b/src/generic/datectlg.cpp @@ -121,6 +121,34 @@ public: return m_combo->GetTextCtrl()->IsEmpty(); } + // This is public because it is used by wxDatePickerCtrlGeneric itself to + // change the date when the text control field changes. The reason it's + // done there and not in this class itself is mostly historic. + void ChangeDateAndNotifyIfValid() + { + wxDateTime dt; + if ( !ParseDateTime(m_combo->GetValue(), &dt) ) + { + // The user must be in the process of updating the date, don't do + // anything -- we'll take care of ensuring it's valid on focus loss + // later. + return; + } + + if ( dt == GetDate() ) + { + // No need to send event if the date hasn't changed. + return; + } + + // We change the date immediately, as it's more consistent with the + // native MSW version and avoids another event on focus loss. + SetDate(dt); + + SendDateEvent(dt); + } + +private: bool ParseDateTime(const wxString& s, wxDateTime* pDt) { wxASSERT(pDt); @@ -144,8 +172,6 @@ public: datePicker->GetEventHandler()->ProcessEvent(event); } -private: - void OnCalKey(wxKeyEvent & ev) { if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) @@ -488,13 +514,8 @@ void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) ev.SetId(GetId()); GetParent()->GetEventHandler()->ProcessEvent(ev); - // We'll create an additional event if the date is valid. - // If the date isn't valid, the user's probably in the middle of typing - wxDateTime dt; - if ( !m_popup || !m_popup->ParseDateTime(m_combo->GetValue(), &dt) ) - return; - - m_popup->SendDateEvent(dt); + if ( m_popup ) + m_popup->ChangeDateAndNotifyIfValid(); } #endif // wxUSE_DATEPICKCTRL