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