From 8030bd727a79d0493e7a2870c661522a217ecb2d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 27 Jan 2018 00:16:53 +0100 Subject: [PATCH] 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. --- src/gtk/textentry.cpp | 46 +++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/gtk/textentry.cpp b/src/gtk/textentry.cpp index 91b47840b6..c6e80cccc6 100644 --- a/src/gtk/textentry.cpp +++ b/src/gtk/textentry.cpp @@ -205,22 +205,17 @@ wx_gtk_paste_clipboard_callback( GtkWidget *widget, wxWindow *win ) class wxTextAutoCompleteData { public: - // The constructor associates us with the given text entry. - wxEXPLICIT wxTextAutoCompleteData(wxTextEntry *entry) - : m_entry(entry) + // Factory function, may return NULL if entry is invalid. + static wxTextAutoCompleteData* New(wxTextEntry *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; - - 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" ); + return new wxTextAutoCompleteData(entry); } ~wxTextAutoCompleteData() @@ -228,13 +223,6 @@ public: 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) { wxDELETE( m_completer ); @@ -305,6 +293,16 @@ public: } 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() { @@ -676,11 +674,7 @@ wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter() { if ( !m_autoCompleteData ) { - wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this); - if ( ac->IsOk() ) - m_autoCompleteData = ac; - else - delete ac; + m_autoCompleteData = wxTextAutoCompleteData::New(this); } return m_autoCompleteData;