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/dynlib.h"
36 #include "wx/msw/private.h"
39 #include "wx/msw/uxtheme.h"
42 #define GetEditHwnd() ((HWND)(GetEditHWND()))
44 // ----------------------------------------------------------------------------
45 // wxIEnumString implements IEnumString interface
46 // ----------------------------------------------------------------------------
48 // standard VC6 SDK (WINVER == 0x0400) does not know about IAutoComplete
49 #if wxUSE_OLE && (WINVER >= 0x0500)
50 #define HAS_AUTOCOMPLETE
53 #ifdef HAS_AUTOCOMPLETE
55 #include "wx/msw/ole/oleutils.h"
58 #if defined(__MINGW32__) || defined (__WATCOMC__) || defined(__CYGWIN__)
59 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
63 #ifndef ACO_UPDOWNKEYDROPSLIST
64 #define ACO_UPDOWNKEYDROPSLIST 0x20
67 #ifndef SHACF_FILESYS_ONLY
68 #define SHACF_FILESYS_ONLY 0x00000010
71 DEFINE_GUID(CLSID_AutoComplete
,
72 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
74 class wxIEnumString
: public IEnumString
77 wxIEnumString(const wxArrayString
& strings
) : m_strings(strings
)
82 void ChangeStrings(const wxArrayString
& strings
)
88 DECLARE_IUNKNOWN_METHODS
;
90 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
94 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
97 ULONG pceltFetchedDummy
;
99 pceltFetched
= &pceltFetchedDummy
;
103 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
105 if ( m_index
== count
)
108 const wxWX2WCbuf wcbuf
= m_strings
[m_index
].wc_str();
109 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
110 void *olestr
= CoTaskMemAlloc(size
);
112 return E_OUTOFMEMORY
;
114 memcpy(olestr
, wcbuf
, size
);
116 *rgelt
++ = static_cast<LPOLESTR
>(olestr
);
124 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
127 if ( m_index
> m_strings
.size() )
129 m_index
= m_strings
.size();
136 virtual HRESULT STDMETHODCALLTYPE
Reset()
143 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
148 wxIEnumString
*e
= new wxIEnumString(m_strings
);
149 e
->m_index
= m_index
;
158 // dtor doesn't have to be virtual as we're only ever deleted from our own
159 // Release() and are not meant to be derived form anyhow, but making it
160 // virtual silences gcc warnings; making it private makes it impossible to
161 // (mistakenly) delete us directly instead of calling Release()
162 virtual ~wxIEnumString() { }
165 wxArrayString m_strings
;
168 wxDECLARE_NO_COPY_CLASS(wxIEnumString
);
171 BEGIN_IID_TABLE(wxIEnumString
)
176 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
178 #endif // HAS_AUTOCOMPLETE
180 // ============================================================================
181 // wxTextEntry implementation
182 // ============================================================================
184 // ----------------------------------------------------------------------------
185 // operations on text
186 // ----------------------------------------------------------------------------
188 void wxTextEntry::WriteText(const wxString
& text
)
190 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
193 wxString
wxTextEntry::DoGetValue() const
195 return wxGetWindowText(GetEditHWND());
198 void wxTextEntry::Remove(long from
, long to
)
200 DoSetSelection(from
, to
, SetSel_NoScroll
);
201 WriteText(wxString());
204 // ----------------------------------------------------------------------------
205 // clipboard operations
206 // ----------------------------------------------------------------------------
208 void wxTextEntry::Copy()
210 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
213 void wxTextEntry::Cut()
215 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
218 void wxTextEntry::Paste()
220 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void wxTextEntry::Undo()
229 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
232 void wxTextEntry::Redo()
234 // same as Undo, since Undo undoes the undo
239 bool wxTextEntry::CanUndo() const
241 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
244 bool wxTextEntry::CanRedo() const
246 // see comment in Redo()
250 // ----------------------------------------------------------------------------
251 // insertion point and selection
252 // ----------------------------------------------------------------------------
254 void wxTextEntry::SetInsertionPoint(long pos
)
256 // calling DoSetSelection(-1, -1) would select everything which is not what
259 pos
= GetLastPosition();
261 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
262 // and not just SetSelection() here
263 DoSetSelection(pos
, pos
);
266 long wxTextEntry::GetInsertionPoint() const
269 GetSelection(&from
, NULL
);
273 long wxTextEntry::GetLastPosition() const
275 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
278 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
280 // if from and to are both -1, it means (in wxWidgets) that all text should
281 // be selected, translate this into Windows convention
282 if ( (from
== -1) && (to
== -1) )
287 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
290 void wxTextEntry::GetSelection(long *from
, long *to
) const
292 DWORD dwStart
, dwEnd
;
293 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
307 #ifdef HAS_AUTOCOMPLETE
309 bool wxTextEntry::DoAutoCompleteFileNames()
311 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
312 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
313 static wxDynamicLibrary s_dllShlwapi
;
314 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
316 if ( !s_dllShlwapi
.Load(wxT("shlwapi.dll"), wxDL_VERBATIM
| wxDL_QUIET
) )
318 s_pfnSHAutoComplete
= NULL
;
322 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
326 if ( !s_pfnSHAutoComplete
)
329 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
332 wxLogApiError(wxT("SHAutoComplete()"), hr
);
339 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
341 // if we had an old enumerator we must reuse it as IAutoComplete doesn't
342 // free it if we call Init() again (see #10968) -- and it's also simpler
345 m_enumStrings
->ChangeStrings(choices
);
349 // create an object exposing IAutoComplete interface (don't go for
350 // IAutoComplete2 immediately as, presumably, it might be not available on
351 // older systems as otherwise why do we have both -- although in practice I
352 // don't know when can this happen)
353 IAutoComplete
*pAutoComplete
= NULL
;
354 HRESULT hr
= CoCreateInstance
358 CLSCTX_INPROC_SERVER
,
360 reinterpret_cast<void **>(&pAutoComplete
)
364 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr
);
368 // associate it with our strings
369 m_enumStrings
= new wxIEnumString(choices
);
370 m_enumStrings
->AddRef();
371 hr
= pAutoComplete
->Init(GetEditHwnd(), m_enumStrings
, NULL
, NULL
);
372 m_enumStrings
->Release();
375 wxLogApiError(wxT("IAutoComplete::Init"), hr
);
379 // if IAutoComplete2 is available, set more user-friendly options
380 IAutoComplete2
*pAutoComplete2
= NULL
;
381 hr
= pAutoComplete
->QueryInterface
384 reinterpret_cast<void **>(&pAutoComplete2
)
388 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
389 pAutoComplete2
->Release();
392 // the docs are unclear about when can we release it but it seems safe to
393 // do it immediately, presumably the edit control itself keeps a reference
394 // to the auto completer object
395 pAutoComplete
->Release();
399 #else // !HAS_AUTOCOMPLETE
401 // We still need to define stubs as we declared these overrides in the header.
403 bool wxTextEntry::DoAutoCompleteFileNames()
405 return wxTextEntryBase::DoAutoCompleteFileNames();
408 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString
& choices
)
410 return wxTextEntryBase::DoAutoCompleteStrings(choices
);
413 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 bool wxTextEntry::IsEditable() const
423 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
426 void wxTextEntry::SetEditable(bool editable
)
428 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 void wxTextEntry::SetMaxLength(unsigned long len
)
439 // this will set it to a platform-dependent maximum (much more
440 // than 64Kb under NT)
444 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
453 #ifndef EM_SETCUEBANNER
454 #define EM_SETCUEBANNER 0x1501
455 #define EM_GETCUEBANNER 0x1502
458 bool wxTextEntry::SetHint(const wxString
& hint
)
460 if ( wxUxThemeEngine::GetIfActive() )
462 // notice that this message always works with Unicode strings
464 // we always use TRUE for wParam to show the hint even when the window
465 // has focus, otherwise there would be no way to show the hint for the
466 // initially focused window
467 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER
,
468 TRUE
, (LPARAM
)(const wchar_t *)hint
.wc_str()) )
472 return wxTextEntryBase::SetHint(hint
);
475 wxString
wxTextEntry::GetHint() const
477 if ( wxUxThemeEngine::GetIfActive() )
480 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER
,
481 (WPARAM
)buf
, WXSIZEOF(buf
)) )
482 return wxString(buf
);
485 return wxTextEntryBase::GetHint();
489 #endif // wxUSE_UXTHEME
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 bool wxTextEntry::DoSetMargins(const wxPoint
& margins
)
497 #if !defined(__WXWINCE__)
500 if ( margins
.x
!= -1 )
503 ::SendMessage(GetEditHwnd(), EM_SETMARGINS
,
504 EC_LEFTMARGIN
, MAKELONG(margins
.x
, 0));
507 if ( margins
.y
!= -1 )
518 wxPoint
wxTextEntry::DoGetMargins() const
520 #if !defined(__WXWINCE__)
521 LRESULT lResult
= ::SendMessage(GetEditHwnd(), EM_GETMARGINS
,
523 int left
= LOWORD(lResult
);
525 return wxPoint(left
, top
);
527 return wxPoint(-1, -1);
531 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX