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"
38 #define GetEditHwnd() ((HWND)(GetEditHWND()))
40 // ----------------------------------------------------------------------------
41 // wxIEnumString implements IEnumString interface
42 // ----------------------------------------------------------------------------
44 // standard VC6 SDK (WINVER == 0x0400) does not know about IAutoComplete
45 #if wxUSE_OLE && (WINVER >= 0x0500)
46 #define HAS_AUTOCOMPLETE
49 #ifdef HAS_AUTOCOMPLETE
51 #include "wx/msw/ole/oleutils.h"
54 #if defined(__MINGW32__)
55 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
59 #ifndef ACO_UPDOWNKEYDROPSLIST
60 #define ACO_UPDOWNKEYDROPSLIST 0x20
63 #ifndef SHACF_FILESYS_ONLY
64 #define SHACF_FILESYS_ONLY 0x00000010
67 DEFINE_GUID(CLSID_AutoComplete
,
68 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
70 class wxIEnumString
: public IEnumString
73 wxIEnumString(const wxArrayString
& strings
) : m_strings(strings
)
78 DECLARE_IUNKNOWN_METHODS
;
80 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
84 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
87 ULONG pceltFetchedDummy
;
89 pceltFetched
= &pceltFetchedDummy
;
93 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
95 if ( m_index
== count
)
98 const wxWX2WCbuf wcbuf
= m_strings
[m_index
].wc_str();
99 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
100 void *olestr
= CoTaskMemAlloc(size
);
102 return E_OUTOFMEMORY
;
104 memcpy(olestr
, wcbuf
, size
);
106 *rgelt
++ = wx_static_cast(LPOLESTR
, olestr
);
114 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
117 if ( m_index
> m_strings
.size() )
119 m_index
= m_strings
.size();
126 virtual HRESULT STDMETHODCALLTYPE
Reset()
133 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
138 wxIEnumString
*e
= new wxIEnumString(m_strings
);
139 e
->m_index
= m_index
;
148 // dtor doesn't have to be virtual as we're only ever deleted from our own
149 // Release() and are not meant to be derived form anyhow, but making it
150 // virtual silences gcc warnings; making it private makes it impossible to
151 // (mistakenly) delete us directly instead of calling Release()
152 virtual ~wxIEnumString() { }
155 const wxArrayString m_strings
;
158 DECLARE_NO_COPY_CLASS(wxIEnumString
)
161 BEGIN_IID_TABLE(wxIEnumString
)
166 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
168 #endif // HAS_AUTOCOMPLETE
170 // ============================================================================
171 // wxTextEntry implementation
172 // ============================================================================
174 // ----------------------------------------------------------------------------
175 // operations on text
176 // ----------------------------------------------------------------------------
178 void wxTextEntry::WriteText(const wxString
& text
)
180 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
183 wxString
wxTextEntry::GetValue() const
185 return wxGetWindowText(GetEditHWND());
188 void wxTextEntry::Remove(long from
, long to
)
190 DoSetSelection(from
, to
, SetSel_NoScroll
);
191 WriteText(wxString());
194 // ----------------------------------------------------------------------------
195 // clipboard operations
196 // ----------------------------------------------------------------------------
198 void wxTextEntry::Copy()
200 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
203 void wxTextEntry::Cut()
205 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
208 void wxTextEntry::Paste()
210 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 void wxTextEntry::Undo()
219 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
222 void wxTextEntry::Redo()
224 // same as Undo, since Undo undoes the undo
229 bool wxTextEntry::CanUndo() const
231 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
234 bool wxTextEntry::CanRedo() const
236 // see comment in Redo()
240 // ----------------------------------------------------------------------------
241 // insertion point and selection
242 // ----------------------------------------------------------------------------
244 void wxTextEntry::SetInsertionPoint(long pos
)
246 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
247 // and not just SetSelection() here
248 DoSetSelection(pos
, pos
);
251 long wxTextEntry::GetInsertionPoint() const
254 GetSelection(&from
, NULL
);
258 long wxTextEntry::GetLastPosition() const
260 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
263 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
265 // if from and to are both -1, it means (in wxWidgets) that all text should
266 // be selected, translate this into Windows convention
267 if ( (from
== -1) && (to
== -1) )
272 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
275 void wxTextEntry::GetSelection(long *from
, long *to
) const
277 DWORD dwStart
, dwEnd
;
278 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 bool wxTextEntry::AutoCompleteFileNames()
292 #ifdef HAS_AUTOCOMPLETE
293 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
294 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
295 static wxDynamicLibrary s_dllShlwapi
;
296 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
300 if ( !s_dllShlwapi
.Load(_T("shlwapi.dll"), wxDL_VERBATIM
) )
302 s_pfnSHAutoComplete
= NULL
;
306 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
310 if ( !s_pfnSHAutoComplete
)
313 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
316 wxLogApiError(_T("SHAutoComplete()"), hr
);
321 #else // !HAS_AUTOCOMPLETE
323 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
326 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
328 #ifdef HAS_AUTOCOMPLETE
329 // create an object exposing IAutoComplete interface (don't go for
330 // IAutoComplete2 immediately as, presumably, it might be not available on
331 // older systems as otherwise why do we have both -- although in practice I
332 // don't know when can this happen)
333 IAutoComplete
*pAutoComplete
= NULL
;
334 HRESULT hr
= CoCreateInstance
338 CLSCTX_INPROC_SERVER
,
340 wx_reinterpret_cast(void **, &pAutoComplete
)
344 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr
);
348 // associate it with our strings
349 wxIEnumString
*pEnumString
= new wxIEnumString(choices
);
350 pEnumString
->AddRef();
351 hr
= pAutoComplete
->Init(GetEditHwnd(), pEnumString
, NULL
, NULL
);
352 pEnumString
->Release();
355 wxLogApiError(_T("IAutoComplete::Init"), hr
);
359 // if IAutoComplete2 is available, set more user-friendly options
360 IAutoComplete2
*pAutoComplete2
= NULL
;
361 hr
= pAutoComplete
->QueryInterface
364 wx_reinterpret_cast(void **, &pAutoComplete2
)
368 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
369 pAutoComplete2
->Release();
372 // the docs are unclear about when can we release it but it seems safe to
373 // do it immediately, presumably the edit control itself keeps a reference
374 // to the auto completer object
375 pAutoComplete
->Release();
377 #else // !HAS_AUTOCOMPLETE
379 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 bool wxTextEntry::IsEditable() const
388 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
391 void wxTextEntry::SetEditable(bool editable
)
393 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 void wxTextEntry::SetMaxLength(unsigned long len
)
404 // this will set it to a platform-dependent maximum (much more
405 // than 64Kb under NT)
409 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
412 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX