compilation fix for stuff missing from mingw32 headers
[wxWidgets.git] / src / msw / textentry.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxMSW
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-26
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/arrstr.h"
28 #include "wx/string.h"
29 #endif // WX_PRECOMP
30
31 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
32
33 #include "wx/textentry.h"
34 #include "wx/dynlib.h"
35
36 #include "wx/msw/private.h"
37
38 #define GetEditHwnd() ((HWND)(GetEditHWND()))
39
40 // ----------------------------------------------------------------------------
41 // wxIEnumString implements IEnumString interface
42 // ----------------------------------------------------------------------------
43
44 #if wxUSE_OLE
45
46 #include "wx/msw/ole/oleutils.h"
47 #include <shldisp.h>
48
49 #if defined(__MINGW32__)
50 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
51 #include <shlguid.h>
52 #endif
53
54 #ifndef ACO_UPDOWNKEYDROPSLIST
55 #define ACO_UPDOWNKEYDROPSLIST 0x20
56 #endif
57
58 #ifndef SHACF_FILESYS_ONLY
59 #define SHACF_FILESYS_ONLY 0x00000010
60 #endif
61
62 DEFINE_GUID(CLSID_AutoComplete,
63 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
64
65 class wxIEnumString : public IEnumString
66 {
67 public:
68 wxIEnumString(const wxArrayString& strings) : m_strings(strings)
69 {
70 m_index = 0;
71 }
72
73 DECLARE_IUNKNOWN_METHODS;
74
75 virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt,
76 LPOLESTR *rgelt,
77 ULONG *pceltFetched)
78 {
79 if ( !rgelt || (!pceltFetched && celt > 1) )
80 return E_POINTER;
81
82 ULONG pceltFetchedDummy;
83 if ( !pceltFetched )
84 pceltFetched = &pceltFetchedDummy;
85
86 *pceltFetched = 0;
87
88 for ( const unsigned count = m_strings.size(); celt--; ++m_index )
89 {
90 if ( m_index == count )
91 return S_FALSE;
92
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);
96 if ( !olestr )
97 return E_OUTOFMEMORY;
98
99 memcpy(olestr, wcbuf, size);
100
101 *rgelt++ = wx_static_cast(LPOLESTR, olestr);
102
103 ++(*pceltFetched);
104 }
105
106 return S_OK;
107 }
108
109 virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
110 {
111 m_index += celt;
112 if ( m_index > m_strings.size() )
113 {
114 m_index = m_strings.size();
115 return S_FALSE;
116 }
117
118 return S_OK;
119 }
120
121 virtual HRESULT STDMETHODCALLTYPE Reset()
122 {
123 m_index = 0;
124
125 return S_OK;
126 }
127
128 virtual HRESULT STDMETHODCALLTYPE Clone(IEnumString **ppEnum)
129 {
130 if ( !ppEnum )
131 return E_POINTER;
132
133 wxIEnumString *e = new wxIEnumString(m_strings);
134 e->m_index = m_index;
135
136 e->AddRef();
137 *ppEnum = e;
138
139 return S_OK;
140 }
141
142 private:
143 const wxArrayString m_strings;
144 unsigned m_index;
145
146 DECLARE_NO_COPY_CLASS(wxIEnumString)
147 };
148
149 BEGIN_IID_TABLE(wxIEnumString)
150 ADD_IID(Unknown)
151 ADD_IID(EnumString)
152 END_IID_TABLE;
153
154 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString)
155
156 #endif // wxUSE_OLE
157
158 // ============================================================================
159 // wxTextEntry implementation
160 // ============================================================================
161
162 // ----------------------------------------------------------------------------
163 // operations on text
164 // ----------------------------------------------------------------------------
165
166 void wxTextEntry::WriteText(const wxString& text)
167 {
168 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
169 }
170
171 wxString wxTextEntry::GetValue() const
172 {
173 return wxGetWindowText(GetEditHWND());
174 }
175
176 void wxTextEntry::Remove(long from, long to)
177 {
178 DoSetSelection(from, to, SetSel_NoScroll);
179 WriteText(wxString());
180 }
181
182 // ----------------------------------------------------------------------------
183 // clipboard operations
184 // ----------------------------------------------------------------------------
185
186 void wxTextEntry::Copy()
187 {
188 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
189 }
190
191 void wxTextEntry::Cut()
192 {
193 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
194 }
195
196 void wxTextEntry::Paste()
197 {
198 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
199 }
200
201 // ----------------------------------------------------------------------------
202 // undo/redo
203 // ----------------------------------------------------------------------------
204
205 void wxTextEntry::Undo()
206 {
207 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
208 }
209
210 void wxTextEntry::Redo()
211 {
212 // same as Undo, since Undo undoes the undo
213 Undo();
214 return;
215 }
216
217 bool wxTextEntry::CanUndo() const
218 {
219 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
220 }
221
222 bool wxTextEntry::CanRedo() const
223 {
224 // see comment in Redo()
225 return CanUndo();
226 }
227
228 // ----------------------------------------------------------------------------
229 // insertion point and selection
230 // ----------------------------------------------------------------------------
231
232 void wxTextEntry::SetInsertionPoint(long pos)
233 {
234 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
235 // and not just SetSelection() here
236 DoSetSelection(pos, pos);
237 }
238
239 long wxTextEntry::GetInsertionPoint() const
240 {
241 long from;
242 GetSelection(&from, NULL);
243 return from;
244 }
245
246 long wxTextEntry::GetLastPosition() const
247 {
248 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
249 }
250
251 void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
252 {
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) )
256 {
257 from = 0;
258 }
259
260 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
261 }
262
263 void wxTextEntry::GetSelection(long *from, long *to) const
264 {
265 DWORD dwStart, dwEnd;
266 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
267
268 if ( from )
269 *from = dwStart;
270 if ( to )
271 *to = dwEnd;
272 }
273
274 // ----------------------------------------------------------------------------
275 // auto-completion
276 // ----------------------------------------------------------------------------
277
278 #if wxUSE_OLE
279
280 bool wxTextEntry::AutoCompleteFileNames()
281 {
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 )
286 {
287 wxLogNull noLog;
288
289 if ( !s_dllShlwapi.Load(_T("shlwapi.dll"), wxDL_VERBATIM) )
290 {
291 s_pfnSHAutoComplete = NULL;
292 }
293 else
294 {
295 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
296 }
297 }
298
299 if ( !s_pfnSHAutoComplete )
300 return false;
301
302 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
303 if ( FAILED(hr) )
304 {
305 wxLogApiError(_T("SHAutoComplete()"), hr);
306
307 return false;
308 }
309
310 return true;
311 }
312
313 bool wxTextEntry::AutoComplete(const wxArrayString& choices)
314 {
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
321 (
322 CLSID_AutoComplete,
323 NULL,
324 CLSCTX_INPROC_SERVER,
325 IID_IAutoComplete,
326 wx_reinterpret_cast(void **, &pAutoComplete)
327 );
328 if ( FAILED(hr) )
329 {
330 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr);
331 return false;
332 }
333
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();
339 if ( FAILED(hr) )
340 {
341 wxLogApiError(_T("IAutoComplete::Init"), hr);
342 return false;
343 }
344
345 // if IAutoComplete2 is available, set more user-friendly options
346 IAutoComplete2 *pAutoComplete2 = NULL;
347 hr = pAutoComplete->QueryInterface
348 (
349 IID_IAutoComplete2,
350 wx_reinterpret_cast(void **, &pAutoComplete2)
351 );
352 if ( SUCCEEDED(hr) )
353 {
354 pAutoComplete2->SetOptions(ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST);
355 pAutoComplete2->Release();
356 }
357
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();
362
363 return true;
364 }
365
366 #endif // wxUSE_OLE
367
368 // ----------------------------------------------------------------------------
369 // editable state
370 // ----------------------------------------------------------------------------
371
372 bool wxTextEntry::IsEditable() const
373 {
374 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
375 }
376
377 void wxTextEntry::SetEditable(bool editable)
378 {
379 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
380 }
381
382 // ----------------------------------------------------------------------------
383 // max length
384 // ----------------------------------------------------------------------------
385
386 void wxTextEntry::SetMaxLength(unsigned long len)
387 {
388 if ( len >= 0xffff )
389 {
390 // this will set it to a platform-dependent maximum (much more
391 // than 64Kb under NT)
392 len = 0;
393 }
394
395 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
396 }
397
398 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX