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. // be called and the default implementation asserts if this is not the case.
virtual void MSWProcessSpecialKey(wxKeyEvent& event); virtual void MSWProcessSpecialKey(wxKeyEvent& event);
// Get the auto-complete object creating it if necessary. Returns NULL if // Check that we have auto-complete data, creating it if necessary. Returns
// creating it failed. // false if creating it failed.
wxTextAutoCompleteData *GetOrCreateCompleter(); bool MSWEnsureHasAutoCompleteData();
// Various auto-completion-related stuff, only used if any of AutoComplete() // Various auto-completion-related stuff, only used if any of AutoComplete()
// methods are called. Use the function above to access it. // 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")); wxFAIL_MSG(wxS("Must be overridden if can be called"));
} }
wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter() bool wxTextEntry::MSWEnsureHasAutoCompleteData()
{ {
if ( !m_autoCompleteData ) if ( !m_autoCompleteData )
{ {
wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this); wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this);
if ( ac->IsOk() ) if ( !ac->IsOk() )
m_autoCompleteData = ac; {
else
delete ac; delete ac;
return false;
}
m_autoCompleteData = ac;
} }
return m_autoCompleteData; return true;
} }
bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices) bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
{ {
wxTextAutoCompleteData * const ac = GetOrCreateCompleter(); if ( !MSWEnsureHasAutoCompleteData() )
if ( !ac )
return false; return false;
ac->ChangeStrings(choices); m_autoCompleteData->ChangeStrings(choices);
return true; return true;
} }
@@ -882,8 +884,7 @@ bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
} }
else // Have a valid completer. else // Have a valid completer.
{ {
wxTextAutoCompleteData * const ac = GetOrCreateCompleter(); if ( !MSWEnsureHasAutoCompleteData() )
if ( !ac )
{ {
// Delete the custom completer for consistency with the case when // Delete the custom completer for consistency with the case when
// we succeed to avoid memory leaks in user code. // 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. // This gives ownership of the custom completer to m_autoCompleteData.
if ( !ac->ChangeCustomCompleter(completer) ) if ( !m_autoCompleteData->ChangeCustomCompleter(completer) )
return false; return false;
} }