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