1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxMSW
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/arrstr.h"
28 #include "wx/string.h"
31 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
33 #include "wx/textentry.h"
34 #include "wx/textcompleter.h"
35 #include "wx/dynlib.h"
37 #include "wx/msw/private.h"
40 #include "wx/msw/uxtheme.h"
43 #define GetEditHwnd() ((HWND)(GetEditHWND()))
45 // ----------------------------------------------------------------------------
46 // wxIEnumString implements IEnumString interface
47 // ----------------------------------------------------------------------------
49 // standard VC6 SDK (WINVER == 0x0400) does not know about IAutoComplete
50 #if wxUSE_OLE && (WINVER >= 0x0500)
51 #define HAS_AUTOCOMPLETE
54 #ifdef HAS_AUTOCOMPLETE
56 #include "wx/msw/ole/oleutils.h"
60 #if defined(__MINGW32__) || defined (__WATCOMC__) || defined(__CYGWIN__)
61 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
65 #ifndef ACO_UPDOWNKEYDROPSLIST
66 #define ACO_UPDOWNKEYDROPSLIST 0x20
69 #ifndef SHACF_FILESYS_ONLY
70 #define SHACF_FILESYS_ONLY 0x00000010
73 DEFINE_GUID(CLSID_AutoComplete
,
74 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
76 class wxIEnumString
: public IEnumString
84 wxIEnumString(const wxIEnumString
& other
)
85 : m_strings(other
.m_strings
),
86 m_index(other
.m_index
)
90 DECLARE_IUNKNOWN_METHODS
;
92 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
96 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
99 ULONG pceltFetchedDummy
;
101 pceltFetched
= &pceltFetchedDummy
;
105 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
107 if ( m_index
== count
)
110 const wxWX2WCbuf wcbuf
= m_strings
[m_index
].wc_str();
111 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
112 void *olestr
= CoTaskMemAlloc(size
);
114 return E_OUTOFMEMORY
;
116 memcpy(olestr
, wcbuf
, size
);
118 *rgelt
++ = static_cast<LPOLESTR
>(olestr
);
126 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
129 if ( m_index
> m_strings
.size() )
131 m_index
= m_strings
.size();
138 virtual HRESULT STDMETHODCALLTYPE
Reset()
145 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
150 wxIEnumString
*e
= new wxIEnumString(*this);
159 // dtor doesn't have to be virtual as we're only ever deleted from our own
160 // Release() and are not meant to be derived form anyhow, but making it
161 // virtual silences gcc warnings; making it private makes it impossible to
162 // (mistakenly) delete us directly instead of calling Release()
163 virtual ~wxIEnumString() { }
166 wxArrayString m_strings
;
169 friend class wxTextAutoCompleteData
;
171 wxDECLARE_NO_ASSIGN_CLASS(wxIEnumString
);
174 BEGIN_IID_TABLE(wxIEnumString
)
179 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
182 // This class gathers the auto-complete-related we use. It is allocated on
183 // demand by wxTextEntry when AutoComplete() is called.
184 class wxTextAutoCompleteData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
187 // The constructor associates us with the given text entry.
188 wxTextAutoCompleteData(wxTextEntry
*entry
)
190 m_win(entry
->GetEditableWindow())
192 m_autoComplete
= NULL
;
193 m_autoCompleteDropDown
= NULL
;
194 m_enumStrings
= NULL
;
195 m_customCompleter
= NULL
;
197 m_connectedTextChangedEvent
= false;
199 // Create an object exposing IAutoComplete interface which we'll later
200 // use to get IAutoComplete2 as the latter can't be created directly,
202 HRESULT hr
= CoCreateInstance
206 CLSCTX_INPROC_SERVER
,
208 reinterpret_cast<void **>(&m_autoComplete
)
212 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr
);
216 // Create a string enumerator and initialize the completer with it.
217 m_enumStrings
= new wxIEnumString
;
218 m_enumStrings
->AddRef();
219 hr
= m_autoComplete
->Init(m_entry
->GetEditHWND(), m_enumStrings
,
223 wxLogApiError(wxT("IAutoComplete::Init"), hr
);
225 m_enumStrings
->Release();
226 m_enumStrings
= NULL
;
231 // As explained in DoRefresh(), we need to call IAutoCompleteDropDown::
232 // ResetEnumerator() if we want to be able to change the completions on
233 // the fly. In principle we could live without it, i.e. return true
234 // from IsOk() even if this QueryInterface() fails, but it doesn't look
235 // like this is ever going to have in practice anyhow as the shell-
236 // provided IAutoComplete always implements IAutoCompleteDropDown too.
237 hr
= m_autoComplete
->QueryInterface
239 IID_IAutoCompleteDropDown
,
240 reinterpret_cast<void **>(&m_autoCompleteDropDown
)
244 wxLogApiError(wxT("IAutoComplete::QI(IAutoCompleteDropDown)"), hr
);
248 // Finally set the completion options using IAutoComplete2.
249 IAutoComplete2
*pAutoComplete2
= NULL
;
250 hr
= m_autoComplete
->QueryInterface
253 reinterpret_cast<void **>(&pAutoComplete2
)
257 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
|
258 ACO_UPDOWNKEYDROPSLIST
);
259 pAutoComplete2
->Release();
263 ~wxTextAutoCompleteData()
265 delete m_customCompleter
;
268 m_enumStrings
->Release();
269 if ( m_autoCompleteDropDown
)
270 m_autoCompleteDropDown
->Release();
271 if ( m_autoComplete
)
272 m_autoComplete
->Release();
275 // Must be called after creating this object to verify if initializing it
279 return m_autoComplete
&& m_autoCompleteDropDown
&& m_enumStrings
;
282 void ChangeStrings(const wxArrayString
& strings
)
284 m_enumStrings
->m_strings
= strings
;
289 // Takes ownership of the pointer if it is non-NULL.
290 bool ChangeCustomCompleter(wxTextCompleter
*completer
)
292 delete m_customCompleter
;
293 m_customCompleter
= completer
;
295 if ( m_customCompleter
)
297 // We postpone connecting to this event until we really need to do
298 // it (however we don't disconnect from it when we don't need it
299 // any more because we don't have wxUNBIND_OR_DISCONNECT_HACK...).
300 if ( !m_connectedTextChangedEvent
)
302 m_connectedTextChangedEvent
= true;
304 wxBIND_OR_CONNECT_HACK(m_win
, wxEVT_COMMAND_TEXT_UPDATED
,
305 wxCommandEventHandler
,
306 wxTextAutoCompleteData::OnTextChanged
,
310 UpdateStringsFromCustomCompleter();
316 void DisableCompletion()
318 // We currently simply reset the list of possible strings as this seems
319 // to effectively disable auto-completion just fine. We could (and
320 // probably should) use IAutoComplete::Enable(FALSE) for this too but
321 // then we'd need to call Enable(TRUE) to turn it on back again later.
323 m_enumStrings
->m_strings
.clear();
324 m_enumStrings
->Reset();
326 ChangeCustomCompleter(NULL
);
330 // Must be called after changing wxIEnumString::m_strings to really make
331 // the changes stick.
334 m_enumStrings
->Reset();
336 // This is completely and utterly not documented and in fact the
337 // current MSDN seems to try to discourage us from using it by saying
338 // that "there is no reason to use this method unless the drop-down
339 // list is currently visible" but actually we absolutely must call it
340 // to force the auto-completer (and not just its drop-down!) to refresh
341 // the list of completions which could have changed now. Without this
342 // call the new choices returned by GetCompletions() that hadn't been
343 // returned by it before are simply silently ignored.
344 m_autoCompleteDropDown
->ResetEnumerator();
347 // Update the strings returned by our string enumerator to correspond to
348 // the currently valid choices according to the custom completer.
349 void UpdateStringsFromCustomCompleter()
351 // For efficiency we access m_strings directly instead of creating
352 // another wxArrayString, normally this should save us an unnecessary
353 // memory allocation on the subsequent calls.
354 m_enumStrings
->m_strings
.clear();
355 m_customCompleter
->GetCompletions(m_entry
->GetValue(),
356 m_enumStrings
->m_strings
);
361 void OnTextChanged(wxCommandEvent
& event
)
363 if ( m_customCompleter
)
364 UpdateStringsFromCustomCompleter();
370 // The text entry we're associated with.
371 wxTextEntry
* const m_entry
;
373 // The window of this text entry.
374 wxWindow
* const m_win
;
376 // The auto-completer object itself.
377 IAutoComplete
*m_autoComplete
;
379 // Its IAutoCompleteDropDown interface needed for ResetEnumerator() call.
380 IAutoCompleteDropDown
*m_autoCompleteDropDown
;
382 // Enumerator for strings currently used for auto-completion.
383 wxIEnumString
*m_enumStrings
;
385 // Custom completer or NULL if none.
386 wxTextCompleter
*m_customCompleter
;
388 // Initially false, set to true after connecting OnTextChanged() handler.
389 bool m_connectedTextChangedEvent
;
392 wxDECLARE_NO_COPY_CLASS(wxTextAutoCompleteData
);
395 #endif // HAS_AUTOCOMPLETE
397 // ============================================================================
398 // wxTextEntry implementation
399 // ============================================================================
401 // ----------------------------------------------------------------------------
402 // initialization and destruction
403 // ----------------------------------------------------------------------------
405 wxTextEntry::wxTextEntry()
407 #ifdef HAS_AUTOCOMPLETE
408 m_autoCompleteData
= NULL
;
409 #endif // HAS_AUTOCOMPLETE
412 wxTextEntry::~wxTextEntry()
414 #ifdef HAS_AUTOCOMPLETE
415 delete m_autoCompleteData
;
416 #endif // HAS_AUTOCOMPLETE
419 // ----------------------------------------------------------------------------
420 // operations on text
421 // ----------------------------------------------------------------------------
423 void wxTextEntry::WriteText(const wxString
& text
)
425 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
428 wxString
wxTextEntry::DoGetValue() const
430 return wxGetWindowText(GetEditHWND());
433 void wxTextEntry::Remove(long from
, long to
)
435 DoSetSelection(from
, to
, SetSel_NoScroll
);
436 WriteText(wxString());
439 // ----------------------------------------------------------------------------
440 // clipboard operations
441 // ----------------------------------------------------------------------------
443 void wxTextEntry::Copy()
445 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
448 void wxTextEntry::Cut()
450 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
453 void wxTextEntry::Paste()
455 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 void wxTextEntry::Undo()
464 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
467 void wxTextEntry::Redo()
469 // same as Undo, since Undo undoes the undo
474 bool wxTextEntry::CanUndo() const
476 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
479 bool wxTextEntry::CanRedo() const
481 // see comment in Redo()
485 // ----------------------------------------------------------------------------
486 // insertion point and selection
487 // ----------------------------------------------------------------------------
489 void wxTextEntry::SetInsertionPoint(long pos
)
491 // calling DoSetSelection(-1, -1) would select everything which is not what
494 pos
= GetLastPosition();
496 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
497 // and not just SetSelection() here
498 DoSetSelection(pos
, pos
);
501 long wxTextEntry::GetInsertionPoint() const
504 GetSelection(&from
, NULL
);
508 long wxTextEntry::GetLastPosition() const
510 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
513 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
515 // if from and to are both -1, it means (in wxWidgets) that all text should
516 // be selected, translate this into Windows convention
517 if ( (from
== -1) && (to
== -1) )
522 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
525 void wxTextEntry::GetSelection(long *from
, long *to
) const
527 DWORD dwStart
, dwEnd
;
528 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
542 #ifdef HAS_AUTOCOMPLETE
544 bool wxTextEntry::DoAutoCompleteFileNames()
546 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
547 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
548 static wxDynamicLibrary s_dllShlwapi
;
549 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
551 if ( !s_dllShlwapi
.Load(wxT("shlwapi.dll"), wxDL_VERBATIM
| wxDL_QUIET
) )
553 s_pfnSHAutoComplete
= NULL
;
557 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
561 if ( !s_pfnSHAutoComplete
)
564 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
567 wxLogApiError(wxT("SHAutoComplete()"), hr
);
572 // Disable the other kinds of completion now that we use the built-in file
574 if ( m_autoCompleteData
)
575 m_autoCompleteData
->DisableCompletion();
580 wxTextAutoCompleteData
*wxTextEntry::GetOrCreateCompleter()
582 if ( !m_autoCompleteData
)
584 wxTextAutoCompleteData
* const ac
= new wxTextAutoCompleteData(this);
586 m_autoCompleteData
= ac
;
591 return m_autoCompleteData
;
594 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
596 wxTextAutoCompleteData
* const ac
= GetOrCreateCompleter();
600 ac
->ChangeStrings(choices
);
605 bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter
*completer
)
607 // First deal with the case when we just want to disable auto-completion.
610 if ( m_autoCompleteData
)
611 m_autoCompleteData
->DisableCompletion();
612 //else: Nothing to do, we hadn't used auto-completion even before.
614 else // Have a valid completer.
616 wxTextAutoCompleteData
* const ac
= GetOrCreateCompleter();
619 // Delete the custom completer for consistency with the case when
620 // we succeed to avoid memory leaks in user code.
625 // This gives ownership of the custom completer to m_autoCompleteData.
626 if ( !ac
->ChangeCustomCompleter(completer
) )
633 #else // !HAS_AUTOCOMPLETE
635 // We still need to define stubs as we declared these overrides in the header.
637 bool wxTextEntry::DoAutoCompleteFileNames()
639 return wxTextEntryBase::DoAutoCompleteFileNames();
642 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
644 return wxTextEntryBase::DoAutoCompleteStrings(choices
);
647 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
651 // ----------------------------------------------------------------------------
653 // ----------------------------------------------------------------------------
655 bool wxTextEntry::IsEditable() const
657 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
660 void wxTextEntry::SetEditable(bool editable
)
662 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 void wxTextEntry::SetMaxLength(unsigned long len
)
673 // this will set it to a platform-dependent maximum (much more
674 // than 64Kb under NT)
678 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
681 // ----------------------------------------------------------------------------
683 // ----------------------------------------------------------------------------
687 #ifndef EM_SETCUEBANNER
688 #define EM_SETCUEBANNER 0x1501
689 #define EM_GETCUEBANNER 0x1502
692 bool wxTextEntry::SetHint(const wxString
& hint
)
694 if ( wxUxThemeEngine::GetIfActive() )
696 // notice that this message always works with Unicode strings
698 // we always use TRUE for wParam to show the hint even when the window
699 // has focus, otherwise there would be no way to show the hint for the
700 // initially focused window
701 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER
,
702 TRUE
, (LPARAM
)(const wchar_t *)hint
.wc_str()) )
706 return wxTextEntryBase::SetHint(hint
);
709 wxString
wxTextEntry::GetHint() const
711 if ( wxUxThemeEngine::GetIfActive() )
714 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER
,
715 (WPARAM
)buf
, WXSIZEOF(buf
)) )
716 return wxString(buf
);
719 return wxTextEntryBase::GetHint();
723 #endif // wxUSE_UXTHEME
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 bool wxTextEntry::DoSetMargins(const wxPoint
& margins
)
731 #if !defined(__WXWINCE__)
734 if ( margins
.x
!= -1 )
737 ::SendMessage(GetEditHwnd(), EM_SETMARGINS
,
738 EC_LEFTMARGIN
, MAKELONG(margins
.x
, 0));
741 if ( margins
.y
!= -1 )
752 wxPoint
wxTextEntry::DoGetMargins() const
754 #if !defined(__WXWINCE__)
755 LRESULT lResult
= ::SendMessage(GetEditHwnd(), EM_GETMARGINS
,
757 int left
= LOWORD(lResult
);
759 return wxPoint(left
, top
);
761 return wxPoint(-1, -1);
765 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX