Avoid modifying the text unnecessarily in wxNumValidatorBase

Even if it's not supposed to do anything, avoid calling
wxTextCtrl::ChangeValue() completely if the text contents doesn't
actually change. This should be slightly more efficient and avoids any
chance of bugs such as the one resulting in the insertion point being
still moved to the beginning of the text even if it doesn't change in
wxGTK currently (see https://github.com/wxWidgets/wxWidgets/pull/1270).
This commit is contained in:
Vadim Zeitlin
2019-03-21 02:41:16 +01:00
parent 1cdc0acfbe
commit a1bc4131a6

View File

@@ -178,10 +178,22 @@ void wxNumValidatorBase::OnChar(wxKeyEvent& event)
void wxNumValidatorBase::OnKillFocus(wxFocusEvent& event)
{
event.Skip();
wxTextEntry * const control = GetTextEntry();
if ( !control )
return;
const wxString& valueNorm = NormalizeString(control->GetValue());
if ( control->GetValue() == valueNorm )
{
// Don't do anything at all if the value doesn't really change, even if
// the control optimizes away the calls to ChangeValue() which don't
// actually change it, it's easier to skip all the complications below
// if we don't need to do anything.
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.
@@ -191,12 +203,10 @@ void wxNumValidatorBase::OnKillFocus(wxFocusEvent& event)
wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
const bool wasModified = text ? text->IsModified() : false;
control->ChangeValue(NormalizeString(control->GetValue()));
control->ChangeValue(valueNorm);
if ( wasModified )
text->MarkDirty();
event.Skip();
}
// ============================================================================