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 const wxArrayString m_strings
;
146 DECLARE_NO_COPY_CLASS(wxIEnumString
)
149 BEGIN_IID_TABLE(wxIEnumString
)
154 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString
)
158 // ============================================================================
159 // wxTextEntry implementation
160 // ============================================================================
162 // ----------------------------------------------------------------------------
163 // operations on text
164 // ----------------------------------------------------------------------------
166 void wxTextEntry::WriteText(const wxString
& text
)
168 ::SendMessage(GetEditHwnd(), EM_REPLACESEL
, 0, (LPARAM
)text
.wx_str());
171 wxString
wxTextEntry::GetValue() const
173 return wxGetWindowText(GetEditHWND());
176 void wxTextEntry::Remove(long from
, long to
)
178 DoSetSelection(from
, to
, SetSel_NoScroll
);
179 WriteText(wxString());
182 // ----------------------------------------------------------------------------
183 // clipboard operations
184 // ----------------------------------------------------------------------------
186 void wxTextEntry::Copy()
188 ::SendMessage(GetEditHwnd(), WM_COPY
, 0, 0);
191 void wxTextEntry::Cut()
193 ::SendMessage(GetEditHwnd(), WM_CUT
, 0, 0);
196 void wxTextEntry::Paste()
198 ::SendMessage(GetEditHwnd(), WM_PASTE
, 0, 0);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 void wxTextEntry::Undo()
207 ::SendMessage(GetEditHwnd(), EM_UNDO
, 0, 0);
210 void wxTextEntry::Redo()
212 // same as Undo, since Undo undoes the undo
217 bool wxTextEntry::CanUndo() const
219 return ::SendMessage(GetEditHwnd(), EM_CANUNDO
, 0, 0) != 0;
222 bool wxTextEntry::CanRedo() const
224 // see comment in Redo()
228 // ----------------------------------------------------------------------------
229 // insertion point and selection
230 // ----------------------------------------------------------------------------
232 void wxTextEntry::SetInsertionPoint(long pos
)
234 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
235 // and not just SetSelection() here
236 DoSetSelection(pos
, pos
);
239 long wxTextEntry::GetInsertionPoint() const
242 GetSelection(&from
, NULL
);
246 long wxTextEntry::GetLastPosition() const
248 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH
, 0, 0);
251 void wxTextEntry::DoSetSelection(long from
, long to
, int WXUNUSED(flags
))
253 // if from and to are both -1, it means (in wxWidgets) that all text should
254 // be selected, translate this into Windows convention
255 if ( (from
== -1) && (to
== -1) )
260 ::SendMessage(GetEditHwnd(), EM_SETSEL
, from
, to
);
263 void wxTextEntry::GetSelection(long *from
, long *to
) const
265 DWORD dwStart
, dwEnd
;
266 ::SendMessage(GetEditHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
280 bool wxTextEntry::AutoCompleteFileNames()
282 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
283 static SHAutoComplete_t s_pfnSHAutoComplete
= (SHAutoComplete_t
)-1;
284 static wxDynamicLibrary s_dllShlwapi
;
285 if ( s_pfnSHAutoComplete
== (SHAutoComplete_t
)-1 )
289 if ( !s_dllShlwapi
.Load(_T("shlwapi.dll"), wxDL_VERBATIM
) )
291 s_pfnSHAutoComplete
= NULL
;
295 wxDL_INIT_FUNC(s_pfn
, SHAutoComplete
, s_dllShlwapi
);
299 if ( !s_pfnSHAutoComplete
)
302 HRESULT hr
= (*s_pfnSHAutoComplete
)(GetEditHwnd(), SHACF_FILESYS_ONLY
);
305 wxLogApiError(_T("SHAutoComplete()"), hr
);
313 bool wxTextEntry::AutoComplete(const wxArrayString
& choices
)
315 // create an object exposing IAutoComplete interface (don't go for
316 // IAutoComplete2 immediately as, presumably, it might be not available on
317 // older systems as otherwise why do we have both -- although in practice I
318 // don't know when can this happen)
319 IAutoComplete
*pAutoComplete
= NULL
;
320 HRESULT hr
= CoCreateInstance
324 CLSCTX_INPROC_SERVER
,
326 wx_reinterpret_cast(void **, &pAutoComplete
)
330 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr
);
334 // associate it with our strings
335 wxIEnumString
*pEnumString
= new wxIEnumString(choices
);
336 pEnumString
->AddRef();
337 hr
= pAutoComplete
->Init(GetEditHwnd(), pEnumString
, NULL
, NULL
);
338 pEnumString
->Release();
341 wxLogApiError(_T("IAutoComplete::Init"), hr
);
345 // if IAutoComplete2 is available, set more user-friendly options
346 IAutoComplete2
*pAutoComplete2
= NULL
;
347 hr
= pAutoComplete
->QueryInterface
350 wx_reinterpret_cast(void **, &pAutoComplete2
)
354 pAutoComplete2
->SetOptions(ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
);
355 pAutoComplete2
->Release();
358 // the docs are unclear about when can we release it but it seems safe to
359 // do it immediately, presumably the edit control itself keeps a reference
360 // to the auto completer object
361 pAutoComplete
->Release();
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 bool wxTextEntry::IsEditable() const
374 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE
) & ES_READONLY
);
377 void wxTextEntry::SetEditable(bool editable
)
379 ::SendMessage(GetEditHwnd(), EM_SETREADONLY
, !editable
, 0);
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void wxTextEntry::SetMaxLength(unsigned long len
)
390 // this will set it to a platform-dependent maximum (much more
391 // than 64Kb under NT)
395 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT
, len
, 0);
398 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX