No changes, just simplify preprocessor checks in wxMSW wxTextEntry.
[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 #if wxUSE_UXTHEME
39 #include "wx/msw/uxtheme.h"
40 #endif
41
42 #define GetEditHwnd() ((HWND)(GetEditHWND()))
43
44 // ----------------------------------------------------------------------------
45 // wxIEnumString implements IEnumString interface
46 // ----------------------------------------------------------------------------
47
48 // standard VC6 SDK (WINVER == 0x0400) does not know about IAutoComplete
49 #if wxUSE_OLE && (WINVER >= 0x0500)
50 #define HAS_AUTOCOMPLETE
51 #endif
52
53 #ifdef HAS_AUTOCOMPLETE
54
55 #include "wx/msw/ole/oleutils.h"
56 #include <shldisp.h>
57
58 #if defined(__MINGW32__) || defined (__WATCOMC__) || defined(__CYGWIN__)
59 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
60 #include <shlguid.h>
61 #endif
62
63 #ifndef ACO_UPDOWNKEYDROPSLIST
64 #define ACO_UPDOWNKEYDROPSLIST 0x20
65 #endif
66
67 #ifndef SHACF_FILESYS_ONLY
68 #define SHACF_FILESYS_ONLY 0x00000010
69 #endif
70
71 DEFINE_GUID(CLSID_AutoComplete,
72 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
73
74 class wxIEnumString : public IEnumString
75 {
76 public:
77 wxIEnumString(const wxArrayString& strings) : m_strings(strings)
78 {
79 m_index = 0;
80 }
81
82 void ChangeStrings(const wxArrayString& strings)
83 {
84 m_strings = strings;
85 Reset();
86 }
87
88 DECLARE_IUNKNOWN_METHODS;
89
90 virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt,
91 LPOLESTR *rgelt,
92 ULONG *pceltFetched)
93 {
94 if ( !rgelt || (!pceltFetched && celt > 1) )
95 return E_POINTER;
96
97 ULONG pceltFetchedDummy;
98 if ( !pceltFetched )
99 pceltFetched = &pceltFetchedDummy;
100
101 *pceltFetched = 0;
102
103 for ( const unsigned count = m_strings.size(); celt--; ++m_index )
104 {
105 if ( m_index == count )
106 return S_FALSE;
107
108 const wxWX2WCbuf wcbuf = m_strings[m_index].wc_str();
109 const size_t size = (wcslen(wcbuf) + 1)*sizeof(wchar_t);
110 void *olestr = CoTaskMemAlloc(size);
111 if ( !olestr )
112 return E_OUTOFMEMORY;
113
114 memcpy(olestr, wcbuf, size);
115
116 *rgelt++ = static_cast<LPOLESTR>(olestr);
117
118 ++(*pceltFetched);
119 }
120
121 return S_OK;
122 }
123
124 virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
125 {
126 m_index += celt;
127 if ( m_index > m_strings.size() )
128 {
129 m_index = m_strings.size();
130 return S_FALSE;
131 }
132
133 return S_OK;
134 }
135
136 virtual HRESULT STDMETHODCALLTYPE Reset()
137 {
138 m_index = 0;
139
140 return S_OK;
141 }
142
143 virtual HRESULT STDMETHODCALLTYPE Clone(IEnumString **ppEnum)
144 {
145 if ( !ppEnum )
146 return E_POINTER;
147
148 wxIEnumString *e = new wxIEnumString(m_strings);
149 e->m_index = m_index;
150
151 e->AddRef();
152 *ppEnum = e;
153
154 return S_OK;
155 }
156
157 private:
158 // dtor doesn't have to be virtual as we're only ever deleted from our own
159 // Release() and are not meant to be derived form anyhow, but making it
160 // virtual silences gcc warnings; making it private makes it impossible to
161 // (mistakenly) delete us directly instead of calling Release()
162 virtual ~wxIEnumString() { }
163
164
165 wxArrayString m_strings;
166 unsigned m_index;
167
168 wxDECLARE_NO_COPY_CLASS(wxIEnumString);
169 };
170
171 BEGIN_IID_TABLE(wxIEnumString)
172 ADD_IID(Unknown)
173 ADD_IID(EnumString)
174 END_IID_TABLE;
175
176 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString)
177
178 #endif // HAS_AUTOCOMPLETE
179
180 // ============================================================================
181 // wxTextEntry implementation
182 // ============================================================================
183
184 // ----------------------------------------------------------------------------
185 // operations on text
186 // ----------------------------------------------------------------------------
187
188 void wxTextEntry::WriteText(const wxString& text)
189 {
190 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
191 }
192
193 wxString wxTextEntry::DoGetValue() const
194 {
195 return wxGetWindowText(GetEditHWND());
196 }
197
198 void wxTextEntry::Remove(long from, long to)
199 {
200 DoSetSelection(from, to, SetSel_NoScroll);
201 WriteText(wxString());
202 }
203
204 // ----------------------------------------------------------------------------
205 // clipboard operations
206 // ----------------------------------------------------------------------------
207
208 void wxTextEntry::Copy()
209 {
210 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
211 }
212
213 void wxTextEntry::Cut()
214 {
215 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
216 }
217
218 void wxTextEntry::Paste()
219 {
220 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
221 }
222
223 // ----------------------------------------------------------------------------
224 // undo/redo
225 // ----------------------------------------------------------------------------
226
227 void wxTextEntry::Undo()
228 {
229 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
230 }
231
232 void wxTextEntry::Redo()
233 {
234 // same as Undo, since Undo undoes the undo
235 Undo();
236 return;
237 }
238
239 bool wxTextEntry::CanUndo() const
240 {
241 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
242 }
243
244 bool wxTextEntry::CanRedo() const
245 {
246 // see comment in Redo()
247 return CanUndo();
248 }
249
250 // ----------------------------------------------------------------------------
251 // insertion point and selection
252 // ----------------------------------------------------------------------------
253
254 void wxTextEntry::SetInsertionPoint(long pos)
255 {
256 // calling DoSetSelection(-1, -1) would select everything which is not what
257 // we want here
258 if ( pos == -1 )
259 pos = GetLastPosition();
260
261 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
262 // and not just SetSelection() here
263 DoSetSelection(pos, pos);
264 }
265
266 long wxTextEntry::GetInsertionPoint() const
267 {
268 long from;
269 GetSelection(&from, NULL);
270 return from;
271 }
272
273 long wxTextEntry::GetLastPosition() const
274 {
275 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
276 }
277
278 void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
279 {
280 // if from and to are both -1, it means (in wxWidgets) that all text should
281 // be selected, translate this into Windows convention
282 if ( (from == -1) && (to == -1) )
283 {
284 from = 0;
285 }
286
287 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
288 }
289
290 void wxTextEntry::GetSelection(long *from, long *to) const
291 {
292 DWORD dwStart, dwEnd;
293 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
294
295 if ( from )
296 *from = dwStart;
297 if ( to )
298 *to = dwEnd;
299 }
300
301 // ----------------------------------------------------------------------------
302 // auto-completion
303 // ----------------------------------------------------------------------------
304
305 #if wxUSE_OLE
306
307 #ifdef HAS_AUTOCOMPLETE
308
309 bool wxTextEntry::DoAutoCompleteFileNames()
310 {
311 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
312 static SHAutoComplete_t s_pfnSHAutoComplete = (SHAutoComplete_t)-1;
313 static wxDynamicLibrary s_dllShlwapi;
314 if ( s_pfnSHAutoComplete == (SHAutoComplete_t)-1 )
315 {
316 if ( !s_dllShlwapi.Load(wxT("shlwapi.dll"), wxDL_VERBATIM | wxDL_QUIET) )
317 {
318 s_pfnSHAutoComplete = NULL;
319 }
320 else
321 {
322 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
323 }
324 }
325
326 if ( !s_pfnSHAutoComplete )
327 return false;
328
329 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
330 if ( FAILED(hr) )
331 {
332 wxLogApiError(wxT("SHAutoComplete()"), hr);
333
334 return false;
335 }
336 return true;
337 }
338
339 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
340 {
341 // if we had an old enumerator we must reuse it as IAutoComplete doesn't
342 // free it if we call Init() again (see #10968) -- and it's also simpler
343 if ( m_enumStrings )
344 {
345 m_enumStrings->ChangeStrings(choices);
346 return true;
347 }
348
349 // create an object exposing IAutoComplete interface (don't go for
350 // IAutoComplete2 immediately as, presumably, it might be not available on
351 // older systems as otherwise why do we have both -- although in practice I
352 // don't know when can this happen)
353 IAutoComplete *pAutoComplete = NULL;
354 HRESULT hr = CoCreateInstance
355 (
356 CLSID_AutoComplete,
357 NULL,
358 CLSCTX_INPROC_SERVER,
359 IID_IAutoComplete,
360 reinterpret_cast<void **>(&pAutoComplete)
361 );
362 if ( FAILED(hr) )
363 {
364 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr);
365 return false;
366 }
367
368 // associate it with our strings
369 m_enumStrings = new wxIEnumString(choices);
370 m_enumStrings->AddRef();
371 hr = pAutoComplete->Init(GetEditHwnd(), m_enumStrings, NULL, NULL);
372 m_enumStrings->Release();
373 if ( FAILED(hr) )
374 {
375 wxLogApiError(wxT("IAutoComplete::Init"), hr);
376 return false;
377 }
378
379 // if IAutoComplete2 is available, set more user-friendly options
380 IAutoComplete2 *pAutoComplete2 = NULL;
381 hr = pAutoComplete->QueryInterface
382 (
383 IID_IAutoComplete2,
384 reinterpret_cast<void **>(&pAutoComplete2)
385 );
386 if ( SUCCEEDED(hr) )
387 {
388 pAutoComplete2->SetOptions(ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST);
389 pAutoComplete2->Release();
390 }
391
392 // the docs are unclear about when can we release it but it seems safe to
393 // do it immediately, presumably the edit control itself keeps a reference
394 // to the auto completer object
395 pAutoComplete->Release();
396 return true;
397 }
398
399 #else // !HAS_AUTOCOMPLETE
400
401 // We still need to define stubs as we declared these overrides in the header.
402
403 bool wxTextEntry::DoAutoCompleteFileNames()
404 {
405 return wxTextEntryBase::DoAutoCompleteFileNames();
406 }
407
408 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
409 {
410 return wxTextEntryBase::DoAutoCompleteStrings(choices);
411 }
412
413 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
414
415 #endif // wxUSE_OLE
416
417 // ----------------------------------------------------------------------------
418 // editable state
419 // ----------------------------------------------------------------------------
420
421 bool wxTextEntry::IsEditable() const
422 {
423 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
424 }
425
426 void wxTextEntry::SetEditable(bool editable)
427 {
428 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
429 }
430
431 // ----------------------------------------------------------------------------
432 // max length
433 // ----------------------------------------------------------------------------
434
435 void wxTextEntry::SetMaxLength(unsigned long len)
436 {
437 if ( len >= 0xffff )
438 {
439 // this will set it to a platform-dependent maximum (much more
440 // than 64Kb under NT)
441 len = 0;
442 }
443
444 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
445 }
446
447 // ----------------------------------------------------------------------------
448 // hints
449 // ----------------------------------------------------------------------------
450
451 #if wxUSE_UXTHEME
452
453 #ifndef EM_SETCUEBANNER
454 #define EM_SETCUEBANNER 0x1501
455 #define EM_GETCUEBANNER 0x1502
456 #endif
457
458 bool wxTextEntry::SetHint(const wxString& hint)
459 {
460 if ( wxUxThemeEngine::GetIfActive() )
461 {
462 // notice that this message always works with Unicode strings
463 //
464 // we always use TRUE for wParam to show the hint even when the window
465 // has focus, otherwise there would be no way to show the hint for the
466 // initially focused window
467 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER,
468 TRUE, (LPARAM)(const wchar_t *)hint.wc_str()) )
469 return true;
470 }
471
472 return wxTextEntryBase::SetHint(hint);
473 }
474
475 wxString wxTextEntry::GetHint() const
476 {
477 if ( wxUxThemeEngine::GetIfActive() )
478 {
479 wchar_t buf[256];
480 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER,
481 (WPARAM)buf, WXSIZEOF(buf)) )
482 return wxString(buf);
483 }
484
485 return wxTextEntryBase::GetHint();
486 }
487
488
489 #endif // wxUSE_UXTHEME
490
491 // ----------------------------------------------------------------------------
492 // margins support
493 // ----------------------------------------------------------------------------
494
495 bool wxTextEntry::DoSetMargins(const wxPoint& margins)
496 {
497 #if !defined(__WXWINCE__)
498 bool res = true;
499
500 if ( margins.x != -1 )
501 {
502 // left margin
503 ::SendMessage(GetEditHwnd(), EM_SETMARGINS,
504 EC_LEFTMARGIN, MAKELONG(margins.x, 0));
505 }
506
507 if ( margins.y != -1 )
508 {
509 res = false;
510 }
511
512 return res;
513 #else
514 return false;
515 #endif
516 }
517
518 wxPoint wxTextEntry::DoGetMargins() const
519 {
520 #if !defined(__WXWINCE__)
521 LRESULT lResult = ::SendMessage(GetEditHwnd(), EM_GETMARGINS,
522 0, 0);
523 int left = LOWORD(lResult);
524 int top = -1;
525 return wxPoint(left, top);
526 #else
527 return wxPoint(-1, -1);
528 #endif
529 }
530
531 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX