Check for correct window in wxNumValidatorBase::SetWindow()

Override SetWindow() to check that the validator is being associated
with the window of the correct type, this allows to trigger an assert
immediately if this is not the case, making it simpler to find the error
as the call to SetValitator() on the wrong window will be in the call
stack when this happens, unlike before when the assert would happen only
at some later time.
This commit is contained in:
Vadim Zeitlin
2018-01-07 01:26:53 +01:00
parent bfcd51cb6a
commit a6d677a0fe
2 changed files with 21 additions and 2 deletions

View File

@@ -50,6 +50,10 @@ public:
// we don't need this as we do our validation on the fly here.
virtual bool Validate(wxWindow * WXUNUSED(parent)) wxOVERRIDE { return true; }
// Override base class method to check that the window is a text control or
// combobox.
virtual void SetWindow(wxWindow *win) wxOVERRIDE;
protected:
wxNumValidatorBase(int style)
{

View File

@@ -52,6 +52,23 @@ int wxNumValidatorBase::GetFormatFlags() const
return flags;
}
void wxNumValidatorBase::SetWindow(wxWindow *win)
{
wxValidator::SetWindow(win);
#if wxUSE_TEXTCTRL
if ( wxDynamicCast(m_validatorWindow, wxTextCtrl) )
return;
#endif // wxUSE_TEXTCTRL
#if wxUSE_COMBOBOX
if ( wxDynamicCast(m_validatorWindow, wxComboBox) )
return;
#endif // wxUSE_COMBOBOX
wxFAIL_MSG("Can only be used with wxTextCtrl or wxComboBox");
}
wxTextEntry *wxNumValidatorBase::GetTextEntry() const
{
#if wxUSE_TEXTCTRL
@@ -64,8 +81,6 @@ wxTextEntry *wxNumValidatorBase::GetTextEntry() const
return combo;
#endif // wxUSE_COMBOBOX
wxFAIL_MSG("Can only be used with wxTextCtrl or wxComboBox");
return NULL;
}