+
+// This class gathers the all auto-complete-related stuff we use. It is
+// allocated on demand by wxTextEntry when AutoComplete() is called.
+class wxTextAutoCompleteData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
+{
+public:
+ // The constructor associates us with the given text entry.
+ wxTextAutoCompleteData(wxTextEntry *entry)
+ : m_entry(entry),
+ m_win(entry->GetEditableWindow())
+ {
+ m_autoComplete = NULL;
+ m_autoCompleteDropDown = NULL;
+ m_enumStrings = NULL;
+
+ m_fixedCompleter = NULL;
+ m_customCompleter = NULL;
+
+ m_connectedCharEvent = false;
+
+ // Create an object exposing IAutoComplete interface which we'll later
+ // use to get IAutoComplete2 as the latter can't be created directly,
+ // apparently.
+ HRESULT hr = CoCreateInstance
+ (
+ wxCLSID_AutoComplete,
+ NULL,
+ CLSCTX_INPROC_SERVER,
+ IID_IAutoComplete,
+ reinterpret_cast<void **>(&m_autoComplete)
+ );
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr);
+ return;
+ }
+
+ // Create a string enumerator and initialize the completer with it.
+ m_enumStrings = new wxIEnumString;
+ m_enumStrings->AddRef();
+ hr = m_autoComplete->Init(m_entry->GetEditHWND(), m_enumStrings,
+ NULL, NULL);
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(wxT("IAutoComplete::Init"), hr);
+
+ m_enumStrings->Release();
+ m_enumStrings = NULL;
+
+ return;
+ }
+
+ // As explained in DoRefresh(), we need to call IAutoCompleteDropDown::
+ // ResetEnumerator() if we want to be able to change the completions on
+ // the fly. In principle we could live without it, i.e. return true
+ // from IsOk() even if this QueryInterface() fails, but it doesn't look
+ // like this is ever going to have in practice anyhow as the shell-
+ // provided IAutoComplete always implements IAutoCompleteDropDown too.
+ hr = m_autoComplete->QueryInterface
+ (
+ wxIID_IAutoCompleteDropDown,
+ reinterpret_cast<void **>(&m_autoCompleteDropDown)
+ );
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(wxT("IAutoComplete::QI(IAutoCompleteDropDown)"), hr);
+ return;
+ }
+
+ // Finally set the completion options using IAutoComplete2.
+ IAutoComplete2 *pAutoComplete2 = NULL;
+ hr = m_autoComplete->QueryInterface
+ (
+ IID_IAutoComplete2,
+ reinterpret_cast<void **>(&pAutoComplete2)
+ );
+ if ( SUCCEEDED(hr) )
+ {
+ pAutoComplete2->SetOptions(ACO_AUTOSUGGEST |
+ ACO_AUTOAPPEND |
+ ACO_UPDOWNKEYDROPSLIST);
+ pAutoComplete2->Release();
+ }
+ }
+
+ ~wxTextAutoCompleteData()
+ {
+ delete m_customCompleter;
+ delete m_fixedCompleter;
+
+ if ( m_enumStrings )
+ m_enumStrings->Release();
+ if ( m_autoCompleteDropDown )
+ m_autoCompleteDropDown->Release();
+ if ( m_autoComplete )
+ m_autoComplete->Release();
+ }
+
+ // Must be called after creating this object to verify if initializing it
+ // succeeded.
+ bool IsOk() const
+ {
+ return m_autoComplete && m_autoCompleteDropDown && m_enumStrings;
+ }
+
+ void ChangeStrings(const wxArrayString& strings)
+ {
+ if ( !m_fixedCompleter )
+ m_fixedCompleter = new wxTextCompleterFixed;
+
+ m_fixedCompleter->SetCompletions(strings);
+
+ m_enumStrings->ChangeCompleter(m_fixedCompleter);
+
+ DoRefresh();
+ }
+
+ // Takes ownership of the pointer if it is non-NULL.
+ bool ChangeCustomCompleter(wxTextCompleter *completer)
+ {
+ // Ensure that the old completer is not used any more before deleting
+ // it.
+ m_enumStrings->ChangeCompleter(completer);
+
+ delete m_customCompleter;
+ m_customCompleter = completer;
+
+ if ( m_customCompleter )
+ {
+ // We postpone connecting to this event until we really need to do
+ // it (however we don't disconnect from it when we don't need it
+ // any more because we don't have wxUNBIND_OR_DISCONNECT_HACK...).
+ if ( !m_connectedCharEvent )
+ {
+ m_connectedCharEvent = true;
+
+ // Use the special wxEVT_AFTER_CHAR and not the usual
+ // wxEVT_CHAR here because we need to have the updated value of
+ // the text control in this handler in order to provide
+ // completions for the correct prefix and unfortunately we
+ // don't have any way to let DefWindowProc() run from our
+ // wxEVT_CHAR handler (as we must also let the other handlers
+ // defined at wx level run first).
+ //
+ // Notice that we can't use wxEVT_COMMAND_TEXT_UPDATED here
+ // neither as, due to our use of ACO_AUTOAPPEND, we get
+ // EN_CHANGE notifications from the control every time
+ // IAutoComplete auto-appends something to it.
+ wxBIND_OR_CONNECT_HACK(m_win, wxEVT_AFTER_CHAR,
+ wxKeyEventHandler,
+ wxTextAutoCompleteData::OnAfterChar,
+ this);
+ }
+
+ UpdateStringsFromCustomCompleter();
+ }
+
+ return true;
+ }
+
+ void DisableCompletion()
+ {
+ // We currently simply reset the list of possible strings as this seems
+ // to effectively disable auto-completion just fine. We could (and
+ // probably should) use IAutoComplete::Enable(FALSE) for this too but
+ // then we'd need to call Enable(TRUE) to turn it on back again later.
+ ChangeStrings(wxArrayString());
+ }
+
+private:
+ // Must be called after changing the values to be returned by wxIEnumString
+ // to really make the changes stick.
+ void DoRefresh()
+ {
+ m_enumStrings->Reset();
+
+ // This is completely and utterly not documented and in fact the
+ // current MSDN seems to try to discourage us from using it by saying
+ // that "there is no reason to use this method unless the drop-down
+ // list is currently visible" but actually we absolutely must call it
+ // to force the auto-completer (and not just its drop-down!) to refresh
+ // the list of completions which could have changed now. Without this
+ // call the new choices returned by GetCompletions() that hadn't been
+ // returned by it before are simply silently ignored.
+ m_autoCompleteDropDown->ResetEnumerator();
+ }
+
+ // Update the strings returned by our string enumerator to correspond to
+ // the currently valid choices according to the custom completer.
+ void UpdateStringsFromCustomCompleter()
+ {
+ // As we use ACO_AUTOAPPEND, the selected part of the text is usually
+ // the one appended by us so don't consider it as part of the
+ // user-entered prefix.
+ long from, to;
+ m_entry->GetSelection(&from, &to);
+
+ if ( to == from )
+ from = m_entry->GetLastPosition(); // Take all if no selection.
+
+ const wxString prefix = m_entry->GetRange(0, from);
+
+ m_enumStrings->UpdatePrefix(prefix);
+
+ DoRefresh();
+ }
+
+ void OnAfterChar(wxKeyEvent& event)
+ {
+ // Notice that we must not refresh the completions when the user
+ // presses Backspace as this would result in adding back the just
+ // erased character(s) because of ACO_AUTOAPPEND option we use.
+ if ( m_customCompleter && event.GetKeyCode() != WXK_BACK )
+ UpdateStringsFromCustomCompleter();
+
+ event.Skip();
+ }
+
+
+ // The text entry we're associated with.
+ wxTextEntry * const m_entry;
+
+ // The window of this text entry.
+ wxWindow * const m_win;
+
+ // The auto-completer object itself.
+ IAutoComplete *m_autoComplete;
+
+ // Its IAutoCompleteDropDown interface needed for ResetEnumerator() call.
+ IAutoCompleteDropDown *m_autoCompleteDropDown;
+
+ // Enumerator for strings currently used for auto-completion.
+ wxIEnumString *m_enumStrings;
+
+ // Fixed string completer or NULL if none.
+ wxTextCompleterFixed *m_fixedCompleter;
+
+ // Custom completer or NULL if none.
+ wxTextCompleter *m_customCompleter;
+
+ // Initially false, set to true after connecting OnTextChanged() handler.
+ bool m_connectedCharEvent;
+
+
+ wxDECLARE_NO_COPY_CLASS(wxTextAutoCompleteData);
+};
+