Refactor wxTextAutoCompleteData creation

Use factory function instead of ctor and IsOk() check, as this
simplifies the code using this class: if factory function fails, it can
just return NULL, which is what the caller used to do explicitly after
freeing the new object before.

Also don't assert if there is no associated GtkEntry, AutoComplete() is
supposed to just return false if using it with the given control is not
implemented under the current platform.
This commit is contained in:
Vadim Zeitlin
2018-01-27 00:16:53 +01:00
parent a8c19c7bd2
commit 8030bd727a

View File

@@ -205,22 +205,17 @@ wx_gtk_paste_clipboard_callback( GtkWidget *widget, wxWindow *win )
class wxTextAutoCompleteData class wxTextAutoCompleteData
{ {
public: public:
// The constructor associates us with the given text entry. // Factory function, may return NULL if entry is invalid.
wxEXPLICIT wxTextAutoCompleteData(wxTextEntry *entry) static wxTextAutoCompleteData* New(wxTextEntry *entry)
: m_entry(entry)
{ {
m_completer = NULL; if ( !GTK_IS_ENTRY(entry->GetEntry()) )
{
// This is probably a multiline wxTextCtrl which doesn't have any
// associated GtkEntry.
return NULL;
}
m_isDynamicCompleter = false; return new wxTextAutoCompleteData(entry);
m_newCompletionsNeeded = m_entry->IsEmpty();
// this asserts if entry is multiline.
// because multiline is actually a GtkTextView not a GtkEntry.
// even GetEditable() will return NULL if entry is multiline.
wxCHECK_RET( GTK_IS_ENTRY(GetGtkEntry()),
"auto completion doesn't work with this control" );
} }
~wxTextAutoCompleteData() ~wxTextAutoCompleteData()
@@ -228,13 +223,6 @@ public:
delete m_completer; delete m_completer;
} }
// Must be called after creating this object to verify if initializing it
// succeeded.
bool IsOk() const
{
return GTK_IS_ENTRY( GetGtkEntry() );
}
void ChangeStrings(const wxArrayString& strings) void ChangeStrings(const wxArrayString& strings)
{ {
wxDELETE( m_completer ); wxDELETE( m_completer );
@@ -305,6 +293,16 @@ public:
} }
private: private:
// Ctor is private, use New() to create objects of this type.
explicit wxTextAutoCompleteData(wxTextEntry *entry)
: m_entry(entry)
{
m_completer = NULL;
m_isDynamicCompleter = false;
m_newCompletionsNeeded = m_entry->IsEmpty();
}
void DoEnableCompletion() void DoEnableCompletion()
{ {
@@ -676,11 +674,7 @@ wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter()
{ {
if ( !m_autoCompleteData ) if ( !m_autoCompleteData )
{ {
wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this); m_autoCompleteData = wxTextAutoCompleteData::New(this);
if ( ac->IsOk() )
m_autoCompleteData = ac;
else
delete ac;
} }
return m_autoCompleteData; return m_autoCompleteData;