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/string.h"
30 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
32 #include "wx/textentry.h"
33 #include "wx/dynlib.h"
35 #include "wx/msw/private.h"
37 #ifndef SHACF_FILESYS_ONLY
38 #define SHACF_FILESYS_ONLY 0x00000010
41 #define GetEditHwnd() ((HWND)(GetEditHWND()))
43 // ----------------------------------------------------------------------------
44 // wxIEnumString implements IEnumString interface
45 // ----------------------------------------------------------------------------
49 #include "wx/msw/ole/oleutils.h"
52 DEFINE_GUID(CLSID_AutoComplete
,
53 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
55 class wxIEnumString
: public IEnumString
58 wxIEnumString(const wxArrayString
& strings
) : m_strings(strings
)
63 DECLARE_IUNKNOWN_METHODS
;
65 virtual HRESULT STDMETHODCALLTYPE
Next(ULONG celt
,
69 if ( !rgelt
|| (!pceltFetched
&& celt
> 1) )
72 ULONG pceltFetchedDummy
;
74 pceltFetched
= &pceltFetchedDummy
;
78 for ( const unsigned count
= m_strings
.size(); celt
--; ++m_index
)
80 if ( m_index
== count
)
83 const wxWX2WCbuf
wcbuf(m_strings
[m_index
].wc_str());
84 const size_t size
= (wcslen(wcbuf
) + 1)*sizeof(wchar_t);
85 void *olestr
= CoTaskMemAlloc(size
);
89 memcpy(olestr
, wcbuf
, size
);
91 *rgelt
++ = wx_static_cast(LPOLESTR
, olestr
);
99 virtual HRESULT STDMETHODCALLTYPE
Skip(ULONG celt
)
102 if ( m_index
> m_strings
.size() )
104 m_index
= m_strings
.size();
111 virtual HRESULT STDMETHODCALLTYPE
Reset()
118 virtual HRESULT STDMETHODCALLTYPE
Clone(IEnumString
**ppEnum
)
123 wxIEnumString
*e
= new wxIEnumString(m_strings
);
124 e
->m_index
= m_index
;
133 const wxArrayString m_strings
;
136 DECLARE_NO_COPY_CLASS(wxIEnumString
)
139 BEGIN_IID_TABLE(wxIEnumString
)
144 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
148 // ============================================================================
149 // wxTextEntry implementation
150 // ============================================================================
152 // ----------------------------------------------------------------------------
153 // operations on text
154 // ----------------------------------------------------------------------------
156 void wxTextEntry::WriteText(const wxString
& text
)
158 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
161 wxString
wxTextEntry::GetValue() const
163 return wxGetWindowText(GetEditHWND());
166 void wxTextEntry::Remove(long from
, long to
)
168 DoSetSelection(from
, to
, SetSel_NoScroll
);
169 WriteText(wxString());
172 // ----------------------------------------------------------------------------
173 // clipboard operations
174 // ----------------------------------------------------------------------------
176 void wxTextEntry::Copy()
178 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
181 void wxTextEntry::Cut()
183 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
186 void wxTextEntry::Paste()
188 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxTextEntry::Undo()
197 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
200 void wxTextEntry::Redo()
202 // same as Undo, since Undo undoes the undo
207 bool wxTextEntry::CanUndo() const
209 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
212 bool wxTextEntry::CanRedo() const
214 // see comment in Redo()
218 // ----------------------------------------------------------------------------
219 // insertion point and selection
220 // ----------------------------------------------------------------------------
222 void wxTextEntry::SetInsertionPoint(long pos
)
224 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
225 // and not just SetSelection() here
226 DoSetSelection(pos
, pos
);
229 long wxTextEntry::GetInsertionPoint() const
232 GetSelection(&from
, NULL
);
236 long wxTextEntry::GetLastPosition() const
238 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
241 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
243 // if from and to are both -1, it means (in wxWidgets) that all text should
244 // be selected, translate this into Windows convention
245 if ( (from
== -1) && (to
== -1) )
250 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
253 void wxTextEntry::GetSelection(long *from
, long *to
) const
255 DWORD dwStart
, dwEnd
;
256 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
270 bool wxTextEntry::AutoCompleteFileNames()
272 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
273 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
274 static wxDynamicLibrary s_dllShlwapi
;
275 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
279 if ( !s_dllShlwapi
.Load(_T("shlwapi.dll"), wxDL_VERBATIM
) )
281 s_pfnSHAutoComplete
= NULL
;
285 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
289 if ( !s_pfnSHAutoComplete
)
292 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
295 wxLogApiError(_T("SHAutoComplete()"), hr
);
303 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
305 // create an object exposing IAutoComplete interface (don't go for
306 // IAutoComplete2 immediately as, presumably, it might be not available on
307 // older systems as otherwise why do we have both -- although in practice I
308 // don't know when can this happen)
309 IAutoComplete
*pAutoComplete
= NULL
;
310 HRESULT hr
= CoCreateInstance
314 CLSCTX_INPROC_SERVER
,
316 wx_reinterpret_cast(void **, &pAutoComplete
)
320 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr
);
324 // associate it with our strings
325 wxIEnumString
*pEnumString
= new wxIEnumString(choices
);
326 pEnumString
->AddRef();
327 hr
= pAutoComplete
->Init(GetEditHwnd(), pEnumString
, NULL
, NULL
);
328 pEnumString
->Release();
331 wxLogApiError(_T("IAutoComplete::Init"), hr
);
335 // if IAutoComplete2 is available, set more user-friendly options
336 IAutoComplete2
*pAutoComplete2
= NULL
;
337 hr
= pAutoComplete
->QueryInterface
340 wx_reinterpret_cast(void **, &pAutoComplete2
)
344 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
345 pAutoComplete2
->Release();
348 // the docs are unclear about when can we release it but it seems safe to
349 // do it immediately, presumably the edit control itself keeps a reference
350 // to the auto completer object
351 pAutoComplete
->Release();
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 bool wxTextEntry::IsEditable() const
364 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
367 void wxTextEntry::SetEditable(bool editable
)
369 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 void wxTextEntry::SetMaxLength(unsigned long len
)
380 // this will set it to a platform-dependent maximum (much more
381 // than 64Kb under NT)
385 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
388 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX