Define wxTextEntry::DoAutoCompleteStrings() stub in wxMSW code too.
[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 // Trivial wxTextCompleter implementation which always returns the same
498 // fixed array of completions.
499 class wxTextCompleterFixed : public wxTextCompleterSimple
500 {
501 public:
502 void SetCompletions(const wxArrayString& strings)
503 {
504 m_strings = strings;
505 }
506
507 virtual void GetCompletions(const wxString& WXUNUSED(prefix),
508 wxArrayString& res)
509 {
510 res = m_strings;
511 }
512
513 private:
514 wxArrayString m_strings;
515 };
516
517
518 // Must be called after changing the values to be returned by wxIEnumString
519 // to really make the changes stick.
520 void DoRefresh()
521 {
522 m_enumStrings->Reset();
523
524 // This is completely and utterly not documented and in fact the
525 // current MSDN seems to try to discourage us from using it by saying
526 // that "there is no reason to use this method unless the drop-down
527 // list is currently visible" but actually we absolutely must call it
528 // to force the auto-completer (and not just its drop-down!) to refresh
529 // the list of completions which could have changed now. Without this
530 // call the new choices returned by GetCompletions() that hadn't been
531 // returned by it before are simply silently ignored.
532 m_autoCompleteDropDown->ResetEnumerator();
533 }
534
535 // Update the strings returned by our string enumerator to correspond to
536 // the currently valid choices according to the custom completer.
537 void UpdateStringsFromCustomCompleter()
538 {
539 // As we use ACO_AUTOAPPEND, the selected part of the text is usually
540 // the one appended by us so don't consider it as part of the
541 // user-entered prefix.
542 long from, to;
543 m_entry->GetSelection(&from, &to);
544
545 if ( to == from )
546 from = m_entry->GetLastPosition(); // Take all if no selection.
547
548 const wxString prefix = m_entry->GetRange(0, from);
549
550 m_enumStrings->UpdatePrefix(prefix);
551
552 DoRefresh();
553 }
554
555 void OnAfterChar(wxKeyEvent& event)
556 {
557 // Notice that we must not refresh the completions when the user
558 // presses Backspace as this would result in adding back the just
559 // erased character(s) because of ACO_AUTOAPPEND option we use.
560 if ( m_customCompleter && event.GetKeyCode() != WXK_BACK )
561 UpdateStringsFromCustomCompleter();
562
563 event.Skip();
564 }
565
566
567 // The text entry we're associated with.
568 wxTextEntry * const m_entry;
569
570 // The window of this text entry.
571 wxWindow * const m_win;
572
573 // The auto-completer object itself.
574 IAutoComplete *m_autoComplete;
575
576 // Its IAutoCompleteDropDown interface needed for ResetEnumerator() call.
577 IAutoCompleteDropDown *m_autoCompleteDropDown;
578
579 // Enumerator for strings currently used for auto-completion.
580 wxIEnumString *m_enumStrings;
581
582 // Fixed string completer or NULL if none.
583 wxTextCompleterFixed *m_fixedCompleter;
584
585 // Custom completer or NULL if none.
586 wxTextCompleter *m_customCompleter;
587
588 // Initially false, set to true after connecting OnTextChanged() handler.
589 bool m_connectedCharEvent;
590
591
592 wxDECLARE_NO_COPY_CLASS(wxTextAutoCompleteData);
593 };
594
595 #endif // HAS_AUTOCOMPLETE
596
597 // ============================================================================
598 // wxTextEntry implementation
599 // ============================================================================
600
601 // ----------------------------------------------------------------------------
602 // initialization and destruction
603 // ----------------------------------------------------------------------------
604
605 wxTextEntry::wxTextEntry()
606 {
607 #ifdef HAS_AUTOCOMPLETE
608 m_autoCompleteData = NULL;
609 #endif // HAS_AUTOCOMPLETE
610 }
611
612 wxTextEntry::~wxTextEntry()
613 {
614 #ifdef HAS_AUTOCOMPLETE
615 delete m_autoCompleteData;
616 #endif // HAS_AUTOCOMPLETE
617 }
618
619 // ----------------------------------------------------------------------------
620 // operations on text
621 // ----------------------------------------------------------------------------
622
623 void wxTextEntry::WriteText(const wxString& text)
624 {
625 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
626 }
627
628 wxString wxTextEntry::DoGetValue() const
629 {
630 return wxGetWindowText(GetEditHWND());
631 }
632
633 void wxTextEntry::Remove(long from, long to)
634 {
635 DoSetSelection(from, to, SetSel_NoScroll);
636 WriteText(wxString());
637 }
638
639 // ----------------------------------------------------------------------------
640 // clipboard operations
641 // ----------------------------------------------------------------------------
642
643 void wxTextEntry::Copy()
644 {
645 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
646 }
647
648 void wxTextEntry::Cut()
649 {
650 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
651 }
652
653 void wxTextEntry::Paste()
654 {
655 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
656 }
657
658 // ----------------------------------------------------------------------------
659 // undo/redo
660 // ----------------------------------------------------------------------------
661
662 void wxTextEntry::Undo()
663 {
664 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
665 }
666
667 void wxTextEntry::Redo()
668 {
669 // same as Undo, since Undo undoes the undo
670 Undo();
671 return;
672 }
673
674 bool wxTextEntry::CanUndo() const
675 {
676 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
677 }
678
679 bool wxTextEntry::CanRedo() const
680 {
681 // see comment in Redo()
682 return CanUndo();
683 }
684
685 // ----------------------------------------------------------------------------
686 // insertion point and selection
687 // ----------------------------------------------------------------------------
688
689 void wxTextEntry::SetInsertionPoint(long pos)
690 {
691 // calling DoSetSelection(-1, -1) would select everything which is not what
692 // we want here
693 if ( pos == -1 )
694 pos = GetLastPosition();
695
696 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
697 // and not just SetSelection() here
698 DoSetSelection(pos, pos);
699 }
700
701 long wxTextEntry::GetInsertionPoint() const
702 {
703 long from;
704 GetSelection(&from, NULL);
705 return from;
706 }
707
708 long wxTextEntry::GetLastPosition() const
709 {
710 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
711 }
712
713 void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
714 {
715 // if from and to are both -1, it means (in wxWidgets) that all text should
716 // be selected, translate this into Windows convention
717 if ( (from == -1) && (to == -1) )
718 {
719 from = 0;
720 }
721
722 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
723 }
724
725 void wxTextEntry::GetSelection(long *from, long *to) const
726 {
727 DWORD dwStart, dwEnd;
728 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
729
730 if ( from )
731 *from = dwStart;
732 if ( to )
733 *to = dwEnd;
734 }
735
736 // ----------------------------------------------------------------------------
737 // auto-completion
738 // ----------------------------------------------------------------------------
739
740 #if wxUSE_OLE
741
742 #ifdef HAS_AUTOCOMPLETE
743
744 bool wxTextEntry::DoAutoCompleteFileNames()
745 {
746 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
747 static SHAutoComplete_t s_pfnSHAutoComplete = (SHAutoComplete_t)-1;
748 static wxDynamicLibrary s_dllShlwapi;
749 if ( s_pfnSHAutoComplete == (SHAutoComplete_t)-1 )
750 {
751 if ( !s_dllShlwapi.Load(wxT("shlwapi.dll"), wxDL_VERBATIM | wxDL_QUIET) )
752 {
753 s_pfnSHAutoComplete = NULL;
754 }
755 else
756 {
757 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
758 }
759 }
760
761 if ( !s_pfnSHAutoComplete )
762 return false;
763
764 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
765 if ( FAILED(hr) )
766 {
767 wxLogApiError(wxT("SHAutoComplete()"), hr);
768
769 return false;
770 }
771
772 // Disable the other kinds of completion now that we use the built-in file
773 // names completion.
774 if ( m_autoCompleteData )
775 m_autoCompleteData->DisableCompletion();
776
777 return true;
778 }
779
780 wxTextAutoCompleteData *wxTextEntry::GetOrCreateCompleter()
781 {
782 if ( !m_autoCompleteData )
783 {
784 wxTextAutoCompleteData * const ac = new wxTextAutoCompleteData(this);
785 if ( ac->IsOk() )
786 m_autoCompleteData = ac;
787 else
788 delete ac;
789 }
790
791 return m_autoCompleteData;
792 }
793
794 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
795 {
796 wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
797 if ( !ac )
798 return false;
799
800 ac->ChangeStrings(choices);
801
802 return true;
803 }
804
805 bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
806 {
807 // First deal with the case when we just want to disable auto-completion.
808 if ( !completer )
809 {
810 if ( m_autoCompleteData )
811 m_autoCompleteData->DisableCompletion();
812 //else: Nothing to do, we hadn't used auto-completion even before.
813 }
814 else // Have a valid completer.
815 {
816 wxTextAutoCompleteData * const ac = GetOrCreateCompleter();
817 if ( !ac )
818 {
819 // Delete the custom completer for consistency with the case when
820 // we succeed to avoid memory leaks in user code.
821 delete completer;
822 return false;
823 }
824
825 // This gives ownership of the custom completer to m_autoCompleteData.
826 if ( !ac->ChangeCustomCompleter(completer) )
827 return false;
828 }
829
830 return true;
831 }
832
833 #else // !HAS_AUTOCOMPLETE
834
835 // We still need to define stubs as we declared these overrides in the header.
836
837 bool wxTextEntry::DoAutoCompleteFileNames()
838 {
839 return wxTextEntryBase::DoAutoCompleteFileNames();
840 }
841
842 bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices)
843 {
844 return wxTextEntryBase::DoAutoCompleteStrings(choices);
845 }
846
847 bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer)
848 {
849 return wxTextCompleter::DoAutoCompleteCustom(completer);
850 }
851
852 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
853
854 #endif // wxUSE_OLE
855
856 // ----------------------------------------------------------------------------
857 // editable state
858 // ----------------------------------------------------------------------------
859
860 bool wxTextEntry::IsEditable() const
861 {
862 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
863 }
864
865 void wxTextEntry::SetEditable(bool editable)
866 {
867 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
868 }
869
870 // ----------------------------------------------------------------------------
871 // max length
872 // ----------------------------------------------------------------------------
873
874 void wxTextEntry::SetMaxLength(unsigned long len)
875 {
876 if ( len >= 0xffff )
877 {
878 // this will set it to a platform-dependent maximum (much more
879 // than 64Kb under NT)
880 len = 0;
881 }
882
883 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
884 }
885
886 // ----------------------------------------------------------------------------
887 // hints
888 // ----------------------------------------------------------------------------
889
890 #if wxUSE_UXTHEME
891
892 #ifndef EM_SETCUEBANNER
893 #define EM_SETCUEBANNER 0x1501
894 #define EM_GETCUEBANNER 0x1502
895 #endif
896
897 bool wxTextEntry::SetHint(const wxString& hint)
898 {
899 if ( wxUxThemeEngine::GetIfActive() )
900 {
901 // notice that this message always works with Unicode strings
902 //
903 // we always use TRUE for wParam to show the hint even when the window
904 // has focus, otherwise there would be no way to show the hint for the
905 // initially focused window
906 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER,
907 TRUE, (LPARAM)(const wchar_t *)hint.wc_str()) )
908 return true;
909 }
910
911 return wxTextEntryBase::SetHint(hint);
912 }
913
914 wxString wxTextEntry::GetHint() const
915 {
916 if ( wxUxThemeEngine::GetIfActive() )
917 {
918 wchar_t buf[256];
919 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER,
920 (WPARAM)buf, WXSIZEOF(buf)) )
921 return wxString(buf);
922 }
923
924 return wxTextEntryBase::GetHint();
925 }
926
927
928 #endif // wxUSE_UXTHEME
929
930 // ----------------------------------------------------------------------------
931 // margins support
932 // ----------------------------------------------------------------------------
933
934 bool wxTextEntry::DoSetMargins(const wxPoint& margins)
935 {
936 #if !defined(__WXWINCE__)
937 bool res = true;
938
939 if ( margins.x != -1 )
940 {
941 // left margin
942 ::SendMessage(GetEditHwnd(), EM_SETMARGINS,
943 EC_LEFTMARGIN, MAKELONG(margins.x, 0));
944 }
945
946 if ( margins.y != -1 )
947 {
948 res = false;
949 }
950
951 return res;
952 #else
953 return false;
954 #endif
955 }
956
957 wxPoint wxTextEntry::DoGetMargins() const
958 {
959 #if !defined(__WXWINCE__)
960 LRESULT lResult = ::SendMessage(GetEditHwnd(), EM_GETMARGINS,
961 0, 0);
962 int left = LOWORD(lResult);
963 int top = -1;
964 return wxPoint(left, top);
965 #else
966 return wxPoint(-1, -1);
967 #endif
968 }
969
970 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX