Don't block the main UI thread while generating completions in wxMSW.
[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/textcompleter.h"
35 #include "wx/dynlib.h"
36
37 #include "wx/msw/private.h"
38
39 #if wxUSE_UXTHEME
40 #include "wx/msw/uxtheme.h"
41 #endif
42
43 #define GetEditHwnd() ((HWND)(GetEditHWND()))
44
45 // ----------------------------------------------------------------------------
46 // Classes used by auto-completion implementation.
47 // ----------------------------------------------------------------------------
48
49 // standard VC6 SDK (WINVER == 0x0400) does not know about IAutoComplete
50 #if wxUSE_OLE && (WINVER >= 0x0500)
51 #define HAS_AUTOCOMPLETE
52 #endif
53
54 #ifdef HAS_AUTOCOMPLETE
55
56 #include "wx/msw/ole/oleutils.h"
57 #include <shldisp.h>
58 #include <shobjidl.h>
59
60 #if defined(__MINGW32__) || defined (__WATCOMC__) || defined(__CYGWIN__)
61 // needed for IID_IAutoComplete, IID_IAutoComplete2 and ACO_AUTOSUGGEST
62 #include <shlguid.h>
63 #endif
64
65 #ifndef ACO_UPDOWNKEYDROPSLIST
66 #define ACO_UPDOWNKEYDROPSLIST 0x20
67 #endif
68
69 #ifndef SHACF_FILESYS_ONLY
70 #define SHACF_FILESYS_ONLY 0x00000010
71 #endif
72
73 DEFINE_GUID(CLSID_AutoComplete,
74 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
75
76 namespace
77 {
78
79 // Small helper class which can be used to ensure thread safety even when
80 // wxUSE_THREADS==0 (and hence wxCriticalSection does nothing).
81 class CSLock
82 {
83 public:
84 CSLock(CRITICAL_SECTION& cs) : m_cs(&cs)
85 {
86 ::EnterCriticalSection(m_cs);
87 }
88
89 ~CSLock()
90 {
91 ::LeaveCriticalSection(m_cs);
92 }
93
94 private:
95 CRITICAL_SECTION * const m_cs;
96
97 wxDECLARE_NO_COPY_CLASS(CSLock);
98 };
99
100 } // anonymity namespace
101
102 // Implementation of string enumerator used by wxTextAutoCompleteData. This
103 // class simply iterates over its strings except that it also can be told to
104 // update them from the custom completer if one is used.
105 //
106 // Notice that Next() method of this class is called by IAutoComplete
107 // background thread and so we must care about thread safety here.
108 class wxIEnumString : public IEnumString
109 {
110 public:
111 wxIEnumString()
112 {
113 Init();
114
115 m_index = 0;
116 }
117
118 wxIEnumString(const wxIEnumString& other)
119 : m_strings(other.m_strings),
120 m_index(other.m_index)
121 {
122 Init();
123 }
124
125 void ChangeStrings(const wxArrayString& strings)
126 {
127 CSLock lock(m_csStrings);
128
129 m_strings = strings;
130 m_index = 0;
131 }
132
133 void UpdateStringsFromCompleter(wxTextCompleter *completer,
134 const wxString& prefix)
135 {
136 CSLock lock(m_csUpdate);
137
138 // We simply store the pointer here and will really update during the
139 // next call to our Next() method as we want to call GetCompletions()
140 // from the worker thread to prevent the main UI thread from blocking
141 // while the completions are generated.
142 m_completer = completer;
143 m_prefix = prefix;
144 }
145
146 virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt,
147 LPOLESTR *rgelt,
148 ULONG *pceltFetched)
149 {
150 if ( !rgelt || (!pceltFetched && celt > 1) )
151 return E_POINTER;
152
153 ULONG pceltFetchedDummy;
154 if ( !pceltFetched )
155 pceltFetched = &pceltFetchedDummy;
156
157 *pceltFetched = 0;
158
159 CSLock lock(m_csStrings);
160
161 DoUpdateIfNeeded();
162
163 for ( const unsigned count = m_strings.size(); celt--; ++m_index )
164 {
165 if ( m_index == count )
166 return S_FALSE;
167
168 const wxWX2WCbuf wcbuf = m_strings[m_index].wc_str();
169 const size_t size = (wcslen(wcbuf) + 1)*sizeof(wchar_t);
170 void *olestr = CoTaskMemAlloc(size);
171 if ( !olestr )
172 return E_OUTOFMEMORY;
173
174 memcpy(olestr, wcbuf, size);
175
176 *rgelt++ = static_cast<LPOLESTR>(olestr);
177
178 ++(*pceltFetched);
179 }
180
181 return S_OK;
182 }
183
184 virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
185 {
186 CSLock lock(m_csStrings);
187
188 m_index += celt;
189 if ( m_index > m_strings.size() )
190 {
191 m_index = m_strings.size();
192 return S_FALSE;
193 }
194
195 return S_OK;
196 }
197
198 virtual HRESULT STDMETHODCALLTYPE Reset()
199 {
200 {
201 CSLock lock(m_csUpdate);
202
203 if ( m_completer )
204 {
205 // We will update the string from completer soon (or maybe are
206 // already in process of doing it) so don't do anything now, it
207 // is useless at best and could also result in a deadlock if
208 // m_csStrings is already locked by Next().
209 return S_OK;
210 }
211 }
212
213 CSLock lock(m_csStrings);
214
215 m_index = 0;
216
217 return S_OK;
218 }
219
220 virtual HRESULT STDMETHODCALLTYPE Clone(IEnumString **ppEnum)
221 {
222 if ( !ppEnum )
223 return E_POINTER;
224
225 CSLock lock(m_csStrings);
226
227 wxIEnumString *e = new wxIEnumString(*this);
228
229 e->AddRef();
230 *ppEnum = e;
231
232 return S_OK;
233 }
234
235 DECLARE_IUNKNOWN_METHODS;
236
237 private:
238 // dtor doesn't have to be virtual as we're only ever deleted from our own
239 // Release() and are not meant to be derived form anyhow, but making it
240 // virtual silences gcc warnings; making it private makes it impossible to
241 // (mistakenly) delete us directly instead of calling Release()
242 virtual ~wxIEnumString()
243 {
244 ::DeleteCriticalSection(&m_csStrings);
245 ::DeleteCriticalSection(&m_csUpdate);
246 }
247
248 // Common part of all ctors.
249 void Init()
250 {
251 ::InitializeCriticalSection(&m_csUpdate);
252 ::InitializeCriticalSection(&m_csStrings);
253
254 m_completer = NULL;
255 }
256
257 void DoUpdateIfNeeded()
258 {
259 wxTextCompleter *completer;
260 wxString prefix;
261 {
262 CSLock lock(m_csUpdate);
263
264 completer = m_completer;
265 if ( !completer )
266 return;
267
268 prefix = m_prefix;
269 } // Unlock m_csUpdate to allow the main thread to call our
270 // UpdateStringsFromCompleter() without blocking while we are
271 // generating completions.
272
273 // Notice that m_csStrings is locked by our caller already so no need
274 // to enter it again.
275 m_index = 0;
276 m_strings.clear();
277 completer->GetCompletions(prefix, m_strings);
278
279 {
280 CSLock lock(m_csUpdate);
281
282 if ( m_completer == completer && m_prefix == prefix )
283 {
284 // There were no calls to UpdateStringsFromCompleter() while we
285 // generated the completions, so our completions are still
286 // pertinent.
287 m_completer = NULL;
288 return;
289 }
290 //else: Our completions are already out of date, regenerate them
291 // once again.
292 }
293 }
294
295
296 // Critical section protecting m_strings and m_index.
297 CRITICAL_SECTION m_csStrings;
298
299 // The strings we iterate over and the current iteration index.
300 wxArrayString m_strings;
301 unsigned m_index;
302
303 // This one protects just m_completer, it is different from m_csStrings
304 // because the main thread should be able to update our completer prefix
305 // without blocking (as this would freeze the UI) even while we're inside a
306 // possibly long process of updating m_strings.
307 CRITICAL_SECTION m_csUpdate;
308
309 // If completer pointer is non-NULL, we must use it by calling its
310 // GetCompletions() method with the specified prefix to update the list of
311 // strings we iterate over when our Next() is called the next time.
312 wxTextCompleter *m_completer;
313 wxString m_prefix;
314
315
316 wxDECLARE_NO_ASSIGN_CLASS(wxIEnumString);
317 };
318
319 BEGIN_IID_TABLE(wxIEnumString)
320 ADD_IID(Unknown)
321 ADD_IID(EnumString)
322 END_IID_TABLE;
323
324 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString)
325
326
327 // This class gathers the auto-complete-related we use. It is allocated on
328 // demand by wxTextEntry when AutoComplete() is called.
329 class wxTextAutoCompleteData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
330 {
331 public:
332 // The constructor associates us with the given text entry.
333 wxTextAutoCompleteData(wxTextEntry *entry)
334 : m_entry(entry),
335 m_win(entry->GetEditableWindow())
336 {
337 m_autoComplete = NULL;
338 m_autoCompleteDropDown = NULL;
339 m_enumStrings = NULL;
340 m_customCompleter = NULL;
341
342 m_connectedCharEvent = false;
343
344 // Create an object exposing IAutoComplete interface which we'll later
345 // use to get IAutoComplete2 as the latter can't be created directly,
346 // apparently.
347 HRESULT hr = CoCreateInstance
348 (
349 CLSID_AutoComplete,
350 NULL,
351 CLSCTX_INPROC_SERVER,
352 IID_IAutoComplete,
353 reinterpret_cast<void **>(&m_autoComplete)
354 );
355 if ( FAILED(hr) )
356 {
357 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr);
358 return;
359 }
360
361 // Create a string enumerator and initialize the completer with it.
362 m_enumStrings = new wxIEnumString;
363 m_enumStrings->AddRef();
364 hr = m_autoComplete->Init(m_entry->GetEditHWND(), m_enumStrings,
365 NULL, NULL);
366 if ( FAILED(hr) )
367 {
368 wxLogApiError(wxT("IAutoComplete::Init"), hr);
369
370 m_enumStrings->Release();
371 m_enumStrings = NULL;
372
373 return;
374 }
375
376 // As explained in DoRefresh(), we need to call IAutoCompleteDropDown::
377 // ResetEnumerator() if we want to be able to change the completions on
378 // the fly. In principle we could live without it, i.e. return true
379 // from IsOk() even if this QueryInterface() fails, but it doesn't look
380 // like this is ever going to have in practice anyhow as the shell-
381 // provided IAutoComplete always implements IAutoCompleteDropDown too.
382 hr = m_autoComplete->QueryInterface
383 (
384 IID_IAutoCompleteDropDown,
385 reinterpret_cast<void **>(&m_autoCompleteDropDown)
386 );
387 if ( FAILED(hr) )
388 {
389 wxLogApiError(wxT("IAutoComplete::QI(IAutoCompleteDropDown)"), hr);
390 return;
391 }
392
393 // Finally set the completion options using IAutoComplete2.
394 IAutoComplete2 *pAutoComplete2 = NULL;
395 hr = m_autoComplete->QueryInterface
396 (
397 IID_IAutoComplete2,
398 reinterpret_cast<void **>(&pAutoComplete2)
399 );
400 if ( SUCCEEDED(hr) )
401 {
402 pAutoComplete2->SetOptions(ACO_AUTOSUGGEST |
403 ACO_AUTOAPPEND |
404 ACO_UPDOWNKEYDROPSLIST);
405 pAutoComplete2->Release();
406 }
407 }
408
409 ~wxTextAutoCompleteData()
410 {
411 delete m_customCompleter;
412
413 if ( m_enumStrings )
414 m_enumStrings->Release();
415 if ( m_autoCompleteDropDown )
416 m_autoCompleteDropDown->Release();
417 if ( m_autoComplete )
418 m_autoComplete->Release();
419 }
420
421 // Must be called after creating this object to verify if initializing it
422 // succeeded.
423 bool IsOk() const
424 {
425 return m_autoComplete && m_autoCompleteDropDown && m_enumStrings;
426 }
427
428 void ChangeStrings(const wxArrayString& strings)
429 {
430 m_enumStrings->ChangeStrings(strings);
431
432 DoRefresh();
433 }
434
435 // Takes ownership of the pointer if it is non-NULL.
436 bool ChangeCustomCompleter(wxTextCompleter *completer)
437 {
438 delete m_customCompleter;
439 m_customCompleter = completer;
440
441 if ( m_customCompleter )
442 {
443 // We postpone connecting to this event until we really need to do
444 // it (however we don't disconnect from it when we don't need it
445 // any more because we don't have wxUNBIND_OR_DISCONNECT_HACK...).
446 if ( !m_connectedCharEvent )
447 {
448 m_connectedCharEvent = true;
449
450 // Use the special wxEVT_AFTER_CHAR and not the usual
451 // wxEVT_CHAR here because we need to have the updated value of
452 // the text control in this handler in order to provide
453 // completions for the correct prefix and unfortunately we
454 // don't have any way to let DefWindowProc() run from our
455 // wxEVT_CHAR handler (as we must also let the other handlers
456 // defined at wx level run first).
457 //
458 // Notice that we can't use wxEVT_COMMAND_TEXT_UPDATED here
459 // neither as, due to our use of ACO_AUTOAPPEND, we get
460 // EN_CHANGE notifications from the control every time
461 // IAutoComplete auto-appends something to it.
462 wxBIND_OR_CONNECT_HACK(m_win, wxEVT_AFTER_CHAR,
463 wxKeyEventHandler,
464 wxTextAutoCompleteData::OnAfterChar,
465 this);
466 }
467
468 UpdateStringsFromCustomCompleter();
469 }
470
471 return true;
472 }
473
474 void DisableCompletion()
475 {
476 // We currently simply reset the list of possible strings as this seems
477 // to effectively disable auto-completion just fine. We could (and
478 // probably should) use IAutoComplete::Enable(FALSE) for this too but
479 // then we'd need to call Enable(TRUE) to turn it on back again later.
480
481 m_enumStrings->ChangeStrings(wxArrayString());
482
483 ChangeCustomCompleter(NULL);
484 }
485
486 private:
487 // Must be called after changing wxIEnumString::m_strings to really make
488 // the changes stick.
489 void DoRefresh()
490 {
491 m_enumStrings->Reset();
492
493 // This is completely and utterly not documented and in fact the
494 // current MSDN seems to try to discourage us from using it by saying
495 // that "there is no reason to use this method unless the drop-down
496 // list is currently visible" but actually we absolutely must call it
497 // to force the auto-completer (and not just its drop-down!) to refresh
498 // the list of completions which could have changed now. Without this
499 // call the new choices returned by GetCompletions() that hadn't been
500 // returned by it before are simply silently ignored.
501 m_autoCompleteDropDown->ResetEnumerator();
502 }
503
504 // Update the strings returned by our string enumerator to correspond to
505 // the currently valid choices according to the custom completer.
506 void UpdateStringsFromCustomCompleter()
507 {
508 // As we use ACO_AUTOAPPEND, the selected part of the text is usually
509 // the one appended by us so don't consider it as part of the
510 // user-entered prefix.
511 long from, to;
512 m_entry->GetSelection(&from, &to);
513
514 if ( to == from )
515 from = m_entry->GetLastPosition(); // Take all if no selection.
516
517 const wxString prefix = m_entry->GetRange(0, from);
518
519 m_enumStrings->UpdateStringsFromCompleter(m_customCompleter, prefix);
520
521 DoRefresh();
522 }
523
524 void OnAfterChar(wxKeyEvent& event)
525 {
526 // Notice that we must not refresh the completions when the user
527 // presses Backspace as this would result in adding back the just
528 // erased character(s) because of ACO_AUTOAPPEND option we use.
529 if ( m_customCompleter && event.GetKeyCode() != WXK_BACK )
530 UpdateStringsFromCustomCompleter();
531
532 event.Skip();
533 }
534
535
536 // The text entry we're associated with.
537 wxTextEntry * const m_entry;
538
539 // The window of this text entry.
540 wxWindow * const m_win;
541
542 // The auto-completer object itself.
543 IAutoComplete *m_autoComplete;
544
545 // Its IAutoCompleteDropDown interface needed for ResetEnumerator() call.
546 IAutoCompleteDropDown *m_autoCompleteDropDown;
547
548 // Enumerator for strings currently used for auto-completion.
549 wxIEnumString *m_enumStrings;
550
551 // Custom completer or NULL if none.
552 wxTextCompleter *m_customCompleter;
553
554 // Initially false, set to true after connecting OnTextChanged() handler.
555 bool m_connectedCharEvent;
556
557
558 wxDECLARE_NO_COPY_CLASS(wxTextAutoCompleteData);
559 };
560
561 #endif // HAS_AUTOCOMPLETE
562
563 // ============================================================================
564 // wxTextEntry implementation
565 // ============================================================================
566
567 // ----------------------------------------------------------------------------
568 // initialization and destruction
569 // ----------------------------------------------------------------------------
570
571 wxTextEntry::wxTextEntry()
572 {
573 #ifdef HAS_AUTOCOMPLETE
574 m_autoCompleteData = NULL;
575 #endif // HAS_AUTOCOMPLETE
576 }
577
578 wxTextEntry::~wxTextEntry()
579 {
580 #ifdef HAS_AUTOCOMPLETE
581 delete m_autoCompleteData;
582 #endif // HAS_AUTOCOMPLETE
583 }
584
585 // ----------------------------------------------------------------------------
586 // operations on text
587 // ----------------------------------------------------------------------------
588
589 void wxTextEntry::WriteText(const wxString& text)
590 {
591 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
592 }
593
594 wxString wxTextEntry::DoGetValue() const
595 {
596 return wxGetWindowText(GetEditHWND());
597 }
598
599 void wxTextEntry::Remove(long from, long to)
600 {
601 DoSetSelection(from, to, SetSel_NoScroll);
602 WriteText(wxString());
603 }
604
605 // ----------------------------------------------------------------------------
606 // clipboard operations
607 // ----------------------------------------------------------------------------
608
609 void wxTextEntry::Copy()
610 {
611 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
612 }
613
614 void wxTextEntry::Cut()
615 {
616 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
617 }
618
619 void wxTextEntry::Paste()
620 {
621 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
622 }
623
624 // ----------------------------------------------------------------------------
625 // undo/redo
626 // ----------------------------------------------------------------------------
627
628 void wxTextEntry::Undo()
629 {
630 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
631 }
632
633 void wxTextEntry::Redo()
634 {
635 // same as Undo, since Undo undoes the undo
636 Undo();
637 return;
638 }
639
640 bool wxTextEntry::CanUndo() const
641 {
642 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
643 }
644
645 bool wxTextEntry::CanRedo() const
646 {
647 // see comment in Redo()
648 return CanUndo();
649 }
650
651 // ----------------------------------------------------------------------------
652 // insertion point and selection
653 // ----------------------------------------------------------------------------
654
655 void wxTextEntry::SetInsertionPoint(long pos)
656 {
657 // calling DoSetSelection(-1, -1) would select everything which is not what
658 // we want here
659 if ( pos == -1 )
660 pos = GetLastPosition();
661
662 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
663 // and not just SetSelection() here
664 DoSetSelection(pos, pos);
665 }
666
667 long wxTextEntry::GetInsertionPoint() const
668 {
669 long from;
670 GetSelection(&from, NULL);
671 return from;
672 }
673
674 long wxTextEntry::GetLastPosition() const
675 {
676 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
677 }
678
679 void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
680 {
681 // if from and to are both -1, it means (in wxWidgets) that all text should
682 // be selected, translate this into Windows convention
683 if ( (from == -1) && (to == -1) )
684 {
685 from = 0;
686 }
687
688 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
689 }
690
691 void wxTextEntry::GetSelection(long *from, long *to) const
692 {
693 DWORD dwStart, dwEnd;
694 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
695
696 if ( from )
697 *from = dwStart;
698 if ( to )
699 *to = dwEnd;
700 }
701
702 // ----------------------------------------------------------------------------
703 // auto-completion
704 // ----------------------------------------------------------------------------
705
706 #if wxUSE_OLE
707
708 #ifdef HAS_AUTOCOMPLETE
709
710 bool wxTextEntry::DoAutoCompleteFileNames()
711 {
712 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
713 static SHAutoComplete_t s_pfnSHAutoComplete = (SHAutoComplete_t)-1;
714 static wxDynamicLibrary s_dllShlwapi;
715 if ( s_pfnSHAutoComplete == (SHAutoComplete_t)-1 )
716 {
717 if ( !s_dllShlwapi.Load(wxT("shlwapi.dll"), wxDL_VERBATIM | wxDL_QUIET) )
718 {
719 s_pfnSHAutoComplete = NULL;
720 }
721 else
722 {
723 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
724 }
725 }
726
727 if ( !s_pfnSHAutoComplete )
728 return false;
729
730 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
731 if ( FAILED(hr) )
732 {
733 wxLogApiError(wxT("SHAutoComplete()"), hr);
734
735 return false;
736 }
737
738 // Disable the other kinds of completion now that we use the built-in file
739 // names completion.
740 if ( m_autoCompleteData )
741 m_autoCompleteData->DisableCompletion();
742
743 return true;
744 }
745
746 wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter()
747 {
748 if ( !m_autoCompleteData )
749 {
750 wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this);
751 if ( ac->IsOk() )
752 m_autoCompleteData = ac;
753 else
754 delete ac;
755 }
756
757 return m_autoCompleteData;
758 }
759
760 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
761 {
762 wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
763 if ( !ac )
764 return false;
765
766 ac->ChangeStrings(choices);
767
768 return true;
769 }
770
771 bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
772 {
773 // First deal with the case when we just want to disable auto-completion.
774 if ( !completer )
775 {
776 if ( m_autoCompleteData )
777 m_autoCompleteData->DisableCompletion();
778 //else: Nothing to do, we hadn't used auto-completion even before.
779 }
780 else // Have a valid completer.
781 {
782 wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
783 if ( !ac )
784 {
785 // Delete the custom completer for consistency with the case when
786 // we succeed to avoid memory leaks in user code.
787 delete completer;
788 return false;
789 }
790
791 // This gives ownership of the custom completer to m_autoCompleteData.
792 if ( !ac->ChangeCustomCompleter(completer) )
793 return false;
794 }
795
796 return true;
797 }
798
799 #else // !HAS_AUTOCOMPLETE
800
801 // We still need to define stubs as we declared these overrides in the header.
802
803 bool wxTextEntry::DoAutoCompleteFileNames()
804 {
805 return wxTextEntryBase::DoAutoCompleteFileNames();
806 }
807
808 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
809 {
810 return wxTextEntryBase::DoAutoCompleteStrings(choices);
811 }
812
813 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
814
815 #endif // wxUSE_OLE
816
817 // ----------------------------------------------------------------------------
818 // editable state
819 // ----------------------------------------------------------------------------
820
821 bool wxTextEntry::IsEditable() const
822 {
823 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
824 }
825
826 void wxTextEntry::SetEditable(bool editable)
827 {
828 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
829 }
830
831 // ----------------------------------------------------------------------------
832 // max length
833 // ----------------------------------------------------------------------------
834
835 void wxTextEntry::SetMaxLength(unsigned long len)
836 {
837 if ( len >= 0xffff )
838 {
839 // this will set it to a platform-dependent maximum (much more
840 // than 64Kb under NT)
841 len = 0;
842 }
843
844 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
845 }
846
847 // ----------------------------------------------------------------------------
848 // hints
849 // ----------------------------------------------------------------------------
850
851 #if wxUSE_UXTHEME
852
853 #ifndef EM_SETCUEBANNER
854 #define EM_SETCUEBANNER 0x1501
855 #define EM_GETCUEBANNER 0x1502
856 #endif
857
858 bool wxTextEntry::SetHint(const wxString& hint)
859 {
860 if ( wxUxThemeEngine::GetIfActive() )
861 {
862 // notice that this message always works with Unicode strings
863 //
864 // we always use TRUE for wParam to show the hint even when the window
865 // has focus, otherwise there would be no way to show the hint for the
866 // initially focused window
867 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER,
868 TRUE, (LPARAM)(const wchar_t *)hint.wc_str()) )
869 return true;
870 }
871
872 return wxTextEntryBase::SetHint(hint);
873 }
874
875 wxString wxTextEntry::GetHint() const
876 {
877 if ( wxUxThemeEngine::GetIfActive() )
878 {
879 wchar_t buf[256];
880 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER,
881 (WPARAM)buf, WXSIZEOF(buf)) )
882 return wxString(buf);
883 }
884
885 return wxTextEntryBase::GetHint();
886 }
887
888
889 #endif // wxUSE_UXTHEME
890
891 // ----------------------------------------------------------------------------
892 // margins support
893 // ----------------------------------------------------------------------------
894
895 bool wxTextEntry::DoSetMargins(const wxPoint& margins)
896 {
897 #if !defined(__WXWINCE__)
898 bool res = true;
899
900 if ( margins.x != -1 )
901 {
902 // left margin
903 ::SendMessage(GetEditHwnd(), EM_SETMARGINS,
904 EC_LEFTMARGIN, MAKELONG(margins.x, 0));
905 }
906
907 if ( margins.y != -1 )
908 {
909 res = false;
910 }
911
912 return res;
913 #else
914 return false;
915 #endif
916 }
917
918 wxPoint wxTextEntry::DoGetMargins() const
919 {
920 #if !defined(__WXWINCE__)
921 LRESULT lResult = ::SendMessage(GetEditHwnd(), EM_GETMARGINS,
922 0, 0);
923 int left = LOWORD(lResult);
924 int top = -1;
925 return wxPoint(left, top);
926 #else
927 return wxPoint(-1, -1);
928 #endif
929 }
930
931 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX