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