Don't normalize the value of empty text control on focus loss

This is unexpected, as just TAB-bing in and out of the control changes
its value since the recent changes to wxNumValidator.

(cherry picked from commit 67f634dfc40fcf4b2a7c481c8c5b223c1494d1ba)
This commit is contained in:
Vadim Zeitlin 2023-07-02 22:40:42 +02:00
parent cf93b63ca1
commit 49cb0550dc

View File

@ -199,7 +199,20 @@ void wxNumValidatorBase::OnKillFocus(wxFocusEvent& event)
if ( !control )
return;
const wxString& valueNorm = NormalizeString(control->GetValue());
// Notice that only wxTextCtrl (and not wxTextEntry) has
// IsModified()/MarkDirty() methods hence the need for dynamic cast.
wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
const bool wasModified = text ? text->IsModified() : false;
const wxString& value = control->GetValue();
// If the control is currently empty and it hasn't been modified by the
// user at all, leave it empty because just giving it focus and taking it
// away again shouldn't change the value.
if ( value.empty() && !wasModified )
return;
const wxString& valueNorm = NormalizeString(value);
if ( control->GetValue() == valueNorm )
{
// Don't do anything at all if the value doesn't really change, even if
@ -209,17 +222,11 @@ void wxNumValidatorBase::OnKillFocus(wxFocusEvent& event)
return;
}
// When we change the control value below, its "modified" status is reset
// so we need to explicitly keep it marked as modified if it was so in the
// first place.
//
// Notice that only wxTextCtrl (and not wxTextEntry) has
// IsModified()/MarkDirty() methods hence the need for dynamic cast.
wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
const bool wasModified = text ? text->IsModified() : false;
control->ChangeValue(valueNorm);
// When we changed the control value above, its "modified" status was reset
// so we need to explicitly keep it marked as modified if it was so in the
// first place.
if ( wasModified )
text->MarkDirty();
}