Refactor wxTextEntry to use MSWEnsureHasAutoCompleteData()

The new function has more clear semantics than GetOrCreateCompleter()
which both returned the completer value and set m_autoCompleteData to it
as a side effect. MSWEnsureHasAutoCompleteData() still does the latter,
but returns just a boolean indicating whether it succeeded or failed,
making using it more straightforward.

No real changes.
This commit is contained in:
Vadim Zeitlin
2021-04-07 22:20:05 +01:00
parent 5f33a52f49
commit a6e4cc7eb0
2 changed files with 15 additions and 14 deletions

View File

@@ -100,9 +100,9 @@ private:
// be called and the default implementation asserts if this is not the case.
virtual void MSWProcessSpecialKey(wxKeyEvent& event);
// Get the auto-complete object creating it if necessary. Returns NULL if
// creating it failed.
wxTextAutoCompleteData *GetOrCreateCompleter();
// Check that we have auto-complete data, creating it if necessary. Returns
// false if creating it failed.
bool MSWEnsureHasAutoCompleteData();
// Various auto-completion-related stuff, only used if any of AutoComplete()
// methods are called. Use the function above to access it.

View File

@@ -846,27 +846,29 @@ void wxTextEntry::MSWProcessSpecialKey(wxKeyEvent& WXUNUSED(event))
wxFAIL_MSG(wxS("Must be overridden if can be called"));
}
wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter()
bool wxTextEntry::MSWEnsureHasAutoCompleteData()
{
if ( !m_autoCompleteData )
{
wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this);
if ( ac->IsOk() )
m_autoCompleteData = ac;
else
if ( !ac->IsOk() )
{
delete ac;
return false;
}
return m_autoCompleteData;
m_autoCompleteData = ac;
}
return true;
}
bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
{
wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
if ( !ac )
if ( !MSWEnsureHasAutoCompleteData() )
return false;
ac->ChangeStrings(choices);
m_autoCompleteData->ChangeStrings(choices);
return true;
}
@@ -882,8 +884,7 @@ bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
}
else // Have a valid completer.
{
wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
if ( !ac )
if ( !MSWEnsureHasAutoCompleteData() )
{
// Delete the custom completer for consistency with the case when
// we succeed to avoid memory leaks in user code.
@@ -892,7 +893,7 @@ bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
}
// This gives ownership of the custom completer to m_autoCompleteData.
if ( !ac->ChangeCustomCompleter(completer) )
if ( !m_autoCompleteData->ChangeCustomCompleter(completer) )
return false;
}