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 // ----------------------------------------------------------------------------
46 #include "wx/msw/ole/oleutils.h"
49 #if defined(__MINGW32__)
50 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
54 #ifndef ACO_UPDOWNKEYDROPSLIST
55 #define ACO_UPDOWNKEYDROPSLIST 0x20
58 #ifndef SHACF_FILESYS_ONLY
59 #define SHACF_FILESYS_ONLY 0x00000010
62 DEFINE_GUID(CLSID_AutoComplete
,
63 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
65 class wxIEnumString
: public IEnumString
68 wxIEnumString(const wxArrayString
& strings
) : m_strings(strings
)
73 DECLARE_IUNKNOWN_METHODS
;
75 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
79 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
82 ULONG pceltFetchedDummy
;
84 pceltFetched
= &pceltFetchedDummy
;
88 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
90 if ( m_index
== count
)
93 const wxWX2WCbuf
wcbuf(m_strings
[m_index
].wc_str());
94 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
95 void *olestr
= CoTaskMemAlloc(size
);
99 memcpy(olestr
, wcbuf
, size
);
101 *rgelt
++ = wx_static_cast(LPOLESTR
, olestr
);
109 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
112 if ( m_index
> m_strings
.size() )
114 m_index
= m_strings
.size();
121 virtual HRESULT STDMETHODCALLTYPE
Reset()
128 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
133 wxIEnumString
*e
= new wxIEnumString(m_strings
);
134 e
->m_index
= m_index
;
143 // dtor doesn't have to be virtual as we're only ever deleted from our own
144 // Release() and are not meant to be derived form anyhow, but making it
145 // virtual silences gcc warnings; making it private makes it impossible to
146 // (mistakenly) delete us directly instead of calling Release()
147 virtual ~wxIEnumString() { }
150 const wxArrayString m_strings
;
153 DECLARE_NO_COPY_CLASS(wxIEnumString
)
156 BEGIN_IID_TABLE(wxIEnumString
)
161 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
165 // ============================================================================
166 // wxTextEntry implementation
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // operations on text
171 // ----------------------------------------------------------------------------
173 void wxTextEntry::WriteText(const wxString
& text
)
175 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
178 wxString
wxTextEntry::GetValue() const
180 return wxGetWindowText(GetEditHWND());
183 void wxTextEntry::Remove(long from
, long to
)
185 DoSetSelection(from
, to
, SetSel_NoScroll
);
186 WriteText(wxString());
189 // ----------------------------------------------------------------------------
190 // clipboard operations
191 // ----------------------------------------------------------------------------
193 void wxTextEntry::Copy()
195 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
198 void wxTextEntry::Cut()
200 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
203 void wxTextEntry::Paste()
205 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void wxTextEntry::Undo()
214 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
217 void wxTextEntry::Redo()
219 // same as Undo, since Undo undoes the undo
224 bool wxTextEntry::CanUndo() const
226 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
229 bool wxTextEntry::CanRedo() const
231 // see comment in Redo()
235 // ----------------------------------------------------------------------------
236 // insertion point and selection
237 // ----------------------------------------------------------------------------
239 void wxTextEntry::SetInsertionPoint(long pos
)
241 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
242 // and not just SetSelection() here
243 DoSetSelection(pos
, pos
);
246 long wxTextEntry::GetInsertionPoint() const
249 GetSelection(&from
, NULL
);
253 long wxTextEntry::GetLastPosition() const
255 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
258 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
260 // if from and to are both -1, it means (in wxWidgets) that all text should
261 // be selected, translate this into Windows convention
262 if ( (from
== -1) && (to
== -1) )
267 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
270 void wxTextEntry::GetSelection(long *from
, long *to
) const
272 DWORD dwStart
, dwEnd
;
273 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
287 bool wxTextEntry::AutoCompleteFileNames()
289 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
290 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
291 static wxDynamicLibrary s_dllShlwapi
;
292 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
296 if ( !s_dllShlwapi
.Load(_T("shlwapi.dll"), wxDL_VERBATIM
) )
298 s_pfnSHAutoComplete
= NULL
;
302 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
306 if ( !s_pfnSHAutoComplete
)
309 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
312 wxLogApiError(_T("SHAutoComplete()"), hr
);
320 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
322 // create an object exposing IAutoComplete interface (don't go for
323 // IAutoComplete2 immediately as, presumably, it might be not available on
324 // older systems as otherwise why do we have both -- although in practice I
325 // don't know when can this happen)
326 IAutoComplete
*pAutoComplete
= NULL
;
327 HRESULT hr
= CoCreateInstance
331 CLSCTX_INPROC_SERVER
,
333 wx_reinterpret_cast(void **, &pAutoComplete
)
337 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr
);
341 // associate it with our strings
342 wxIEnumString
*pEnumString
= new wxIEnumString(choices
);
343 pEnumString
->AddRef();
344 hr
= pAutoComplete
->Init(GetEditHwnd(), pEnumString
, NULL
, NULL
);
345 pEnumString
->Release();
348 wxLogApiError(_T("IAutoComplete::Init"), hr
);
352 // if IAutoComplete2 is available, set more user-friendly options
353 IAutoComplete2
*pAutoComplete2
= NULL
;
354 hr
= pAutoComplete
->QueryInterface
357 wx_reinterpret_cast(void **, &pAutoComplete2
)
361 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
362 pAutoComplete2
->Release();
365 // the docs are unclear about when can we release it but it seems safe to
366 // do it immediately, presumably the edit control itself keeps a reference
367 // to the auto completer object
368 pAutoComplete
->Release();
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 bool wxTextEntry::IsEditable() const
381 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
384 void wxTextEntry::SetEditable(bool editable
)
386 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 void wxTextEntry::SetMaxLength(unsigned long len
)
397 // this will set it to a platform-dependent maximum (much more
398 // than 64Kb under NT)
402 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
405 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX