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 DECLARE_IUNKNOWN_METHODS
;
84 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
88 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
91 ULONG pceltFetchedDummy
;
93 pceltFetched
= &pceltFetchedDummy
;
97 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
99 if ( m_index
== count
)
102 const wxWX2WCbuf wcbuf
= m_strings
[m_index
].wc_str();
103 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
104 void *olestr
= CoTaskMemAlloc(size
);
106 return E_OUTOFMEMORY
;
108 memcpy(olestr
, wcbuf
, size
);
110 *rgelt
++ = static_cast<LPOLESTR
>(olestr
);
118 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
121 if ( m_index
> m_strings
.size() )
123 m_index
= m_strings
.size();
130 virtual HRESULT STDMETHODCALLTYPE
Reset()
137 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
142 wxIEnumString
*e
= new wxIEnumString(m_strings
);
143 e
->m_index
= m_index
;
152 // dtor doesn't have to be virtual as we're only ever deleted from our own
153 // Release() and are not meant to be derived form anyhow, but making it
154 // virtual silences gcc warnings; making it private makes it impossible to
155 // (mistakenly) delete us directly instead of calling Release()
156 virtual ~wxIEnumString() { }
159 const wxArrayString m_strings
;
162 wxDECLARE_NO_COPY_CLASS(wxIEnumString
);
165 BEGIN_IID_TABLE(wxIEnumString
)
170 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
172 #endif // HAS_AUTOCOMPLETE
174 // ============================================================================
175 // wxTextEntry implementation
176 // ============================================================================
178 // ----------------------------------------------------------------------------
179 // operations on text
180 // ----------------------------------------------------------------------------
182 void wxTextEntry::WriteText(const wxString
& text
)
184 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
187 wxString
wxTextEntry::DoGetValue() const
189 return wxGetWindowText(GetEditHWND());
192 void wxTextEntry::Remove(long from
, long to
)
194 DoSetSelection(from
, to
, SetSel_NoScroll
);
195 WriteText(wxString());
198 // ----------------------------------------------------------------------------
199 // clipboard operations
200 // ----------------------------------------------------------------------------
202 void wxTextEntry::Copy()
204 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
207 void wxTextEntry::Cut()
209 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
212 void wxTextEntry::Paste()
214 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 void wxTextEntry::Undo()
223 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
226 void wxTextEntry::Redo()
228 // same as Undo, since Undo undoes the undo
233 bool wxTextEntry::CanUndo() const
235 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
238 bool wxTextEntry::CanRedo() const
240 // see comment in Redo()
244 // ----------------------------------------------------------------------------
245 // insertion point and selection
246 // ----------------------------------------------------------------------------
248 void wxTextEntry::SetInsertionPoint(long pos
)
250 // calling DoSetSelection(-1, -1) would select everything which is not what
253 pos
= GetLastPosition();
255 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
256 // and not just SetSelection() here
257 DoSetSelection(pos
, pos
);
260 long wxTextEntry::GetInsertionPoint() const
263 GetSelection(&from
, NULL
);
267 long wxTextEntry::GetLastPosition() const
269 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
272 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
274 // if from and to are both -1, it means (in wxWidgets) that all text should
275 // be selected, translate this into Windows convention
276 if ( (from
== -1) && (to
== -1) )
281 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
284 void wxTextEntry::GetSelection(long *from
, long *to
) const
286 DWORD dwStart
, dwEnd
;
287 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
300 bool wxTextEntry::AutoCompleteFileNames()
302 #ifdef HAS_AUTOCOMPLETE
303 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
304 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
305 static wxDynamicLibrary s_dllShlwapi
;
306 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
308 if ( !s_dllShlwapi
.Load(_T("shlwapi.dll"), wxDL_VERBATIM
| wxDL_QUIET
) )
310 s_pfnSHAutoComplete
= NULL
;
314 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
318 if ( !s_pfnSHAutoComplete
)
321 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
324 wxLogApiError(_T("SHAutoComplete()"), hr
);
329 #else // !HAS_AUTOCOMPLETE
331 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
334 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
336 #ifdef HAS_AUTOCOMPLETE
337 // create an object exposing IAutoComplete interface (don't go for
338 // IAutoComplete2 immediately as, presumably, it might be not available on
339 // older systems as otherwise why do we have both -- although in practice I
340 // don't know when can this happen)
341 IAutoComplete
*pAutoComplete
= NULL
;
342 HRESULT hr
= CoCreateInstance
346 CLSCTX_INPROC_SERVER
,
348 reinterpret_cast<void **>(&pAutoComplete
)
352 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr
);
356 // associate it with our strings
357 wxIEnumString
*pEnumString
= new wxIEnumString(choices
);
358 pEnumString
->AddRef();
359 hr
= pAutoComplete
->Init(GetEditHwnd(), pEnumString
, NULL
, NULL
);
360 pEnumString
->Release();
363 wxLogApiError(_T("IAutoComplete::Init"), hr
);
367 // if IAutoComplete2 is available, set more user-friendly options
368 IAutoComplete2
*pAutoComplete2
= NULL
;
369 hr
= pAutoComplete
->QueryInterface
372 reinterpret_cast<void **>(&pAutoComplete2
)
376 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
377 pAutoComplete2
->Release();
380 // the docs are unclear about when can we release it but it seems safe to
381 // do it immediately, presumably the edit control itself keeps a reference
382 // to the auto completer object
383 pAutoComplete
->Release();
385 #else // !HAS_AUTOCOMPLETE
386 wxUnusedVar(choices
);
389 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 bool wxTextEntry::IsEditable() const
399 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
402 void wxTextEntry::SetEditable(bool editable
)
404 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 void wxTextEntry::SetMaxLength(unsigned long len
)
415 // this will set it to a platform-dependent maximum (much more
416 // than 64Kb under NT)
420 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
429 #ifndef EM_SETCUEBANNER
430 #define EM_SETCUEBANNER 0x1501
431 #define EM_GETCUEBANNER 0x1502
434 bool wxTextEntry::SetHint(const wxString
& hint
)
436 if ( wxUxThemeEngine::GetIfActive() )
438 // notice that this message always works with Unicode strings
439 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER
,
440 0, (LPARAM
)(const wchar_t *)hint
.wc_str()) )
444 return wxTextEntryBase::SetHint(hint
);
447 wxString
wxTextEntry::GetHint() const
449 if ( wxUxThemeEngine::GetIfActive() )
452 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER
,
453 (WPARAM
)buf
, WXSIZEOF(buf
)) )
457 return wxTextEntryBase::GetHint();
461 #endif // wxUSE_UXTHEME
463 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX