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__)
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 // ----------------------------------------------------------------------------
306 bool wxTextEntry::AutoCompleteFileNames()
308 #ifdef HAS_AUTOCOMPLETE
309 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
310 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
311 static wxDynamicLibrary s_dllShlwapi
;
312 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
314 if ( !s_dllShlwapi
.Load(wxT("shlwapi.dll"), wxDL_VERBATIM
| wxDL_QUIET
) )
316 s_pfnSHAutoComplete
= NULL
;
320 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
324 if ( !s_pfnSHAutoComplete
)
327 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
330 wxLogApiError(wxT("SHAutoComplete()"), hr
);
335 #else // !HAS_AUTOCOMPLETE
337 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
340 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
342 #ifdef HAS_AUTOCOMPLETE
343 // if we had an old enumerator we must reuse it as IAutoComplete doesn't
344 // free it if we call Init() again (see #10968) -- and it's also simpler
347 m_enumStrings
->ChangeStrings(choices
);
351 // create an object exposing IAutoComplete interface (don't go for
352 // IAutoComplete2 immediately as, presumably, it might be not available on
353 // older systems as otherwise why do we have both -- although in practice I
354 // don't know when can this happen)
355 IAutoComplete
*pAutoComplete
= NULL
;
356 HRESULT hr
= CoCreateInstance
360 CLSCTX_INPROC_SERVER
,
362 reinterpret_cast<void **>(&pAutoComplete
)
366 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr
);
370 // associate it with our strings
371 m_enumStrings
= new wxIEnumString(choices
);
372 m_enumStrings
->AddRef();
373 hr
= pAutoComplete
->Init(GetEditHwnd(), m_enumStrings
, NULL
, NULL
);
374 m_enumStrings
->Release();
377 wxLogApiError(wxT("IAutoComplete::Init"), hr
);
381 // if IAutoComplete2 is available, set more user-friendly options
382 IAutoComplete2
*pAutoComplete2
= NULL
;
383 hr
= pAutoComplete
->QueryInterface
386 reinterpret_cast<void **>(&pAutoComplete2
)
390 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
391 pAutoComplete2
->Release();
394 // the docs are unclear about when can we release it but it seems safe to
395 // do it immediately, presumably the edit control itself keeps a reference
396 // to the auto completer object
397 pAutoComplete
->Release();
399 #else // !HAS_AUTOCOMPLETE
400 wxUnusedVar(choices
);
403 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 bool wxTextEntry::IsEditable() const
413 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
416 void wxTextEntry::SetEditable(bool editable
)
418 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 void wxTextEntry::SetMaxLength(unsigned long len
)
429 // this will set it to a platform-dependent maximum (much more
430 // than 64Kb under NT)
434 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
443 #ifndef EM_SETCUEBANNER
444 #define EM_SETCUEBANNER 0x1501
445 #define EM_GETCUEBANNER 0x1502
448 bool wxTextEntry::SetHint(const wxString
& hint
)
450 if ( wxUxThemeEngine::GetIfActive() )
452 // notice that this message always works with Unicode strings
453 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER
,
454 0, (LPARAM
)(const wchar_t *)hint
.wc_str()) )
458 return wxTextEntryBase::SetHint(hint
);
461 wxString
wxTextEntry::GetHint() const
463 if ( wxUxThemeEngine::GetIfActive() )
466 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER
,
467 (WPARAM
)buf
, WXSIZEOF(buf
)) )
468 return wxString(buf
);
471 return wxTextEntryBase::GetHint();
475 #endif // wxUSE_UXTHEME
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 bool wxTextEntry::DoSetMargins(const wxPoint
& margins
)
483 #if !defined(__WXWINCE__)
486 if ( margins
.x
!= -1 )
489 ::SendMessage(GetEditHwnd(), EM_SETMARGINS
,
490 EC_LEFTMARGIN
, MAKELONG(margins
.x
, 0));
493 if ( margins
.y
!= -1 )
504 wxPoint
wxTextEntry::DoGetMargins() const
506 #if !defined(__WXWINCE__)
507 LRESULT lResult
= ::SendMessage(GetEditHwnd(), EM_GETMARGINS
,
509 int left
= LOWORD(lResult
);
511 return wxPoint(left
, top
);
513 return wxPoint(-1, -1);
517 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX