]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textentry.cpp
(dirty) fix rendering upon startup of the surface
[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
6c49d634 58#if defined(__MINGW32__) || defined (__WATCOMC__)
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
82 DECLARE_IUNKNOWN_METHODS;
83
84 virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt,
85 LPOLESTR *rgelt,
86 ULONG *pceltFetched)
87 {
88 if ( !rgelt || (!pceltFetched && celt > 1) )
89 return E_POINTER;
90
91 ULONG pceltFetchedDummy;
92 if ( !pceltFetched )
93 pceltFetched = &pceltFetchedDummy;
94
95 *pceltFetched = 0;
96
97 for ( const unsigned count = m_strings.size(); celt--; ++m_index )
98 {
99 if ( m_index == count )
100 return S_FALSE;
101
46eb9b54 102 const wxWX2WCbuf wcbuf = m_strings[m_index].wc_str();
0847dca6
VZ
103 const size_t size = (wcslen(wcbuf) + 1)*sizeof(wchar_t);
104 void *olestr = CoTaskMemAlloc(size);
105 if ( !olestr )
106 return E_OUTOFMEMORY;
107
108 memcpy(olestr, wcbuf, size);
109
5c33522f 110 *rgelt++ = static_cast<LPOLESTR>(olestr);
0847dca6
VZ
111
112 ++(*pceltFetched);
113 }
114
115 return S_OK;
116 }
117
118 virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
119 {
120 m_index += celt;
121 if ( m_index > m_strings.size() )
122 {
123 m_index = m_strings.size();
124 return S_FALSE;
125 }
126
127 return S_OK;
128 }
129
130 virtual HRESULT STDMETHODCALLTYPE Reset()
131 {
132 m_index = 0;
133
134 return S_OK;
135 }
136
137 virtual HRESULT STDMETHODCALLTYPE Clone(IEnumString **ppEnum)
138 {
139 if ( !ppEnum )
140 return E_POINTER;
141
142 wxIEnumString *e = new wxIEnumString(m_strings);
143 e->m_index = m_index;
144
145 e->AddRef();
146 *ppEnum = e;
147
148 return S_OK;
149 }
150
151private:
3eeefdf9
VZ
152 // dtor doesn't have to be virtual as we're only ever deleted from our own
153 // Release() and are not meant to be derived form anyhow, but making it
154 // virtual silences gcc warnings; making it private makes it impossible to
155 // (mistakenly) delete us directly instead of calling Release()
156 virtual ~wxIEnumString() { }
157
158
0847dca6
VZ
159 const wxArrayString m_strings;
160 unsigned m_index;
161
c0c133e1 162 wxDECLARE_NO_COPY_CLASS(wxIEnumString);
0847dca6
VZ
163};
164
165BEGIN_IID_TABLE(wxIEnumString)
166 ADD_IID(Unknown)
167 ADD_IID(EnumString)
168END_IID_TABLE;
169
170IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString)
171
fd873451 172#endif // HAS_AUTOCOMPLETE
0847dca6 173
69a05ef6
VZ
174// ============================================================================
175// wxTextEntry implementation
176// ============================================================================
177
0847dca6
VZ
178// ----------------------------------------------------------------------------
179// operations on text
180// ----------------------------------------------------------------------------
181
69a05ef6
VZ
182void wxTextEntry::WriteText(const wxString& text)
183{
184 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
185}
186
135b23b2 187wxString wxTextEntry::DoGetValue() const
69a05ef6
VZ
188{
189 return wxGetWindowText(GetEditHWND());
190}
191
192void wxTextEntry::Remove(long from, long to)
193{
194 DoSetSelection(from, to, SetSel_NoScroll);
195 WriteText(wxString());
196}
197
0847dca6
VZ
198// ----------------------------------------------------------------------------
199// clipboard operations
200// ----------------------------------------------------------------------------
201
69a05ef6
VZ
202void wxTextEntry::Copy()
203{
204 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
205}
206
207void wxTextEntry::Cut()
208{
209 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
210}
211
212void wxTextEntry::Paste()
213{
214 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
215}
216
0847dca6
VZ
217// ----------------------------------------------------------------------------
218// undo/redo
219// ----------------------------------------------------------------------------
220
69a05ef6
VZ
221void wxTextEntry::Undo()
222{
223 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
224}
225
226void wxTextEntry::Redo()
227{
3cb6eaec 228 // same as Undo, since Undo undoes the undo
69a05ef6
VZ
229 Undo();
230 return;
231}
232
233bool wxTextEntry::CanUndo() const
234{
235 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
236}
237
238bool wxTextEntry::CanRedo() const
239{
240 // see comment in Redo()
241 return CanUndo();
242}
243
0847dca6
VZ
244// ----------------------------------------------------------------------------
245// insertion point and selection
246// ----------------------------------------------------------------------------
247
69a05ef6
VZ
248void wxTextEntry::SetInsertionPoint(long pos)
249{
2851cf25
VZ
250 // calling DoSetSelection(-1, -1) would select everything which is not what
251 // we want here
252 if ( pos == -1 )
253 pos = GetLastPosition();
254
69a05ef6
VZ
255 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
256 // and not just SetSelection() here
257 DoSetSelection(pos, pos);
258}
259
260long wxTextEntry::GetInsertionPoint() const
261{
262 long from;
263 GetSelection(&from, NULL);
264 return from;
265}
266
267long wxTextEntry::GetLastPosition() const
268{
269 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
270}
271
272void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
273{
274 // if from and to are both -1, it means (in wxWidgets) that all text should
275 // be selected, translate this into Windows convention
276 if ( (from == -1) && (to == -1) )
277 {
278 from = 0;
279 }
280
281 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
282}
283
284void wxTextEntry::GetSelection(long *from, long *to) const
285{
286 DWORD dwStart, dwEnd;
287 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
288
289 if ( from )
290 *from = dwStart;
291 if ( to )
292 *to = dwEnd;
293}
294
0847dca6
VZ
295// ----------------------------------------------------------------------------
296// auto-completion
297// ----------------------------------------------------------------------------
298
6502dc68 299#if wxUSE_OLE
59396417
VZ
300bool wxTextEntry::AutoCompleteFileNames()
301{
fd873451 302#ifdef HAS_AUTOCOMPLETE
59396417
VZ
303 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
304 static SHAutoComplete_t s_pfnSHAutoComplete = (SHAutoComplete_t)-1;
305 static wxDynamicLibrary s_dllShlwapi;
306 if ( s_pfnSHAutoComplete == (SHAutoComplete_t)-1 )
307 {
e2fc2bd5 308 if ( !s_dllShlwapi.Load(_T("shlwapi.dll"), wxDL_VERBATIM | wxDL_QUIET) )
59396417
VZ
309 {
310 s_pfnSHAutoComplete = NULL;
311 }
312 else
313 {
314 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
315 }
316 }
317
318 if ( !s_pfnSHAutoComplete )
319 return false;
320
321 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
322 if ( FAILED(hr) )
323 {
324 wxLogApiError(_T("SHAutoComplete()"), hr);
325
326 return false;
327 }
59396417 328 return true;
fd873451
VZ
329#else // !HAS_AUTOCOMPLETE
330 return false;
331#endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
59396417
VZ
332}
333
0847dca6
VZ
334bool wxTextEntry::AutoComplete(const wxArrayString& choices)
335{
fd873451 336#ifdef HAS_AUTOCOMPLETE
0847dca6
VZ
337 // create an object exposing IAutoComplete interface (don't go for
338 // IAutoComplete2 immediately as, presumably, it might be not available on
339 // older systems as otherwise why do we have both -- although in practice I
340 // don't know when can this happen)
341 IAutoComplete *pAutoComplete = NULL;
342 HRESULT hr = CoCreateInstance
343 (
344 CLSID_AutoComplete,
345 NULL,
346 CLSCTX_INPROC_SERVER,
347 IID_IAutoComplete,
5c33522f 348 reinterpret_cast<void **>(&pAutoComplete)
0847dca6
VZ
349 );
350 if ( FAILED(hr) )
351 {
352 wxLogApiError(_T("CoCreateInstance(CLSID_AutoComplete)"), hr);
353 return false;
354 }
355
356 // associate it with our strings
357 wxIEnumString *pEnumString = new wxIEnumString(choices);
358 pEnumString->AddRef();
359 hr = pAutoComplete->Init(GetEditHwnd(), pEnumString, NULL, NULL);
360 pEnumString->Release();
361 if ( FAILED(hr) )
362 {
363 wxLogApiError(_T("IAutoComplete::Init"), hr);
364 return false;
365 }
366
367 // if IAutoComplete2 is available, set more user-friendly options
368 IAutoComplete2 *pAutoComplete2 = NULL;
369 hr = pAutoComplete->QueryInterface
370 (
371 IID_IAutoComplete2,
5c33522f 372 reinterpret_cast<void **>(&pAutoComplete2)
0847dca6
VZ
373 );
374 if ( SUCCEEDED(hr) )
375 {
376 pAutoComplete2->SetOptions(ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST);
377 pAutoComplete2->Release();
378 }
379
380 // the docs are unclear about when can we release it but it seems safe to
381 // do it immediately, presumably the edit control itself keeps a reference
382 // to the auto completer object
383 pAutoComplete->Release();
0847dca6 384 return true;
fd873451 385#else // !HAS_AUTOCOMPLETE
867b485e
VZ
386 wxUnusedVar(choices);
387
fd873451
VZ
388 return false;
389#endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
0847dca6 390}
6502dc68 391#endif // wxUSE_OLE
0847dca6 392
0847dca6
VZ
393// ----------------------------------------------------------------------------
394// editable state
395// ----------------------------------------------------------------------------
396
69a05ef6
VZ
397bool wxTextEntry::IsEditable() const
398{
5ad3f0c8 399 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
69a05ef6
VZ
400}
401
402void wxTextEntry::SetEditable(bool editable)
403{
404 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
405}
406
0847dca6
VZ
407// ----------------------------------------------------------------------------
408// max length
409// ----------------------------------------------------------------------------
410
69a05ef6
VZ
411void wxTextEntry::SetMaxLength(unsigned long len)
412{
413 if ( len >= 0xffff )
414 {
415 // this will set it to a platform-dependent maximum (much more
416 // than 64Kb under NT)
417 len = 0;
418 }
419
420 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
421}
96a4cdeb 422
63f7d502
VZ
423// ----------------------------------------------------------------------------
424// hints
425// ----------------------------------------------------------------------------
426
427#if wxUSE_UXTHEME
428
429#ifndef EM_SETCUEBANNER
430 #define EM_SETCUEBANNER 0x1501
431 #define EM_GETCUEBANNER 0x1502
432#endif
433
434bool wxTextEntry::SetHint(const wxString& hint)
435{
436 if ( wxUxThemeEngine::GetIfActive() )
437 {
438 // notice that this message always works with Unicode strings
439 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER,
440 0, (LPARAM)(const wchar_t *)hint.wc_str()) )
441 return true;
442 }
443
444 return wxTextEntryBase::SetHint(hint);
445}
446
447wxString wxTextEntry::GetHint() const
448{
449 if ( wxUxThemeEngine::GetIfActive() )
450 {
451 wchar_t buf[256];
452 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER,
453 (WPARAM)buf, WXSIZEOF(buf)) )
d2f434e4 454 return wxString(buf);
63f7d502
VZ
455 }
456
457 return wxTextEntryBase::GetHint();
458}
459
460
461#endif // wxUSE_UXTHEME
462
96a4cdeb 463#endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX