]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textentry.cpp
Fix wxGrid compilation with MSVC7.
[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/dynlib.h"
35
36 #include "wx/msw/private.h"
37
38 #if wxUSE_UXTHEME
39 #include "wx/msw/uxtheme.h"
40 #endif
41
42 #define GetEditHwnd() ((HWND)(GetEditHWND()))
43
44 // ----------------------------------------------------------------------------
45 // wxIEnumString implements IEnumString interface
46 // ----------------------------------------------------------------------------
47
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
54
55 #include "wx/msw/ole/oleutils.h"
56 #include <shldisp.h>
57
58 #if defined(__MINGW32__) || defined (__WATCOMC__)
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
71 DEFINE_GUID(CLSID_AutoComplete,
72 0x00bb2763, 0x6a77, 0x11d0, 0xa5, 0x35, 0x00, 0xc0, 0x4f, 0xd7, 0xd0, 0x62);
73
74 class wxIEnumString : public IEnumString
75 {
76 public:
77 wxIEnumString(const wxArrayString& strings) : m_strings(strings)
78 {
79 m_index = 0;
80 }
81
82 void ChangeStrings(const wxArrayString& strings)
83 {
84 m_strings = strings;
85 Reset();
86 }
87
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
108 const wxWX2WCbuf wcbuf = m_strings[m_index].wc_str();
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
116 *rgelt++ = static_cast<LPOLESTR>(olestr);
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
157 private:
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
165 wxArrayString m_strings;
166 unsigned m_index;
167
168 wxDECLARE_NO_COPY_CLASS(wxIEnumString);
169 };
170
171 BEGIN_IID_TABLE(wxIEnumString)
172 ADD_IID(Unknown)
173 ADD_IID(EnumString)
174 END_IID_TABLE;
175
176 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumString)
177
178 #endif // HAS_AUTOCOMPLETE
179
180 // ============================================================================
181 // wxTextEntry implementation
182 // ============================================================================
183
184 // ----------------------------------------------------------------------------
185 // operations on text
186 // ----------------------------------------------------------------------------
187
188 void wxTextEntry::WriteText(const wxString& text)
189 {
190 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
191 }
192
193 wxString wxTextEntry::DoGetValue() const
194 {
195 return wxGetWindowText(GetEditHWND());
196 }
197
198 void wxTextEntry::Remove(long from, long to)
199 {
200 DoSetSelection(from, to, SetSel_NoScroll);
201 WriteText(wxString());
202 }
203
204 // ----------------------------------------------------------------------------
205 // clipboard operations
206 // ----------------------------------------------------------------------------
207
208 void wxTextEntry::Copy()
209 {
210 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
211 }
212
213 void wxTextEntry::Cut()
214 {
215 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
216 }
217
218 void wxTextEntry::Paste()
219 {
220 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
221 }
222
223 // ----------------------------------------------------------------------------
224 // undo/redo
225 // ----------------------------------------------------------------------------
226
227 void wxTextEntry::Undo()
228 {
229 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
230 }
231
232 void wxTextEntry::Redo()
233 {
234 // same as Undo, since Undo undoes the undo
235 Undo();
236 return;
237 }
238
239 bool wxTextEntry::CanUndo() const
240 {
241 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
242 }
243
244 bool wxTextEntry::CanRedo() const
245 {
246 // see comment in Redo()
247 return CanUndo();
248 }
249
250 // ----------------------------------------------------------------------------
251 // insertion point and selection
252 // ----------------------------------------------------------------------------
253
254 void wxTextEntry::SetInsertionPoint(long pos)
255 {
256 // calling DoSetSelection(-1, -1) would select everything which is not what
257 // we want here
258 if ( pos == -1 )
259 pos = GetLastPosition();
260
261 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
262 // and not just SetSelection() here
263 DoSetSelection(pos, pos);
264 }
265
266 long wxTextEntry::GetInsertionPoint() const
267 {
268 long from;
269 GetSelection(&from, NULL);
270 return from;
271 }
272
273 long wxTextEntry::GetLastPosition() const
274 {
275 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
276 }
277
278 void 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
290 void 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
301 // ----------------------------------------------------------------------------
302 // auto-completion
303 // ----------------------------------------------------------------------------
304
305 #if wxUSE_OLE
306 bool wxTextEntry::AutoCompleteFileNames()
307 {
308 #ifdef HAS_AUTOCOMPLETE
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 {
314 if ( !s_dllShlwapi.Load(wxT("shlwapi.dll"), wxDL_VERBATIM | wxDL_QUIET) )
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 {
330 wxLogApiError(wxT("SHAutoComplete()"), hr);
331
332 return false;
333 }
334 return true;
335 #else // !HAS_AUTOCOMPLETE
336 return false;
337 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
338 }
339
340 bool wxTextEntry::AutoComplete(const wxArrayString& choices)
341 {
342 #ifdef HAS_AUTOCOMPLETE
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
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,
362 reinterpret_cast<void **>(&pAutoComplete)
363 );
364 if ( FAILED(hr) )
365 {
366 wxLogApiError(wxT("CoCreateInstance(CLSID_AutoComplete)"), hr);
367 return false;
368 }
369
370 // associate it with our strings
371 m_enumStrings = new wxIEnumString(choices);
372 m_enumStrings->AddRef();
373 hr = pAutoComplete->Init(GetEditHwnd(), m_enumStrings, NULL, NULL);
374 m_enumStrings->Release();
375 if ( FAILED(hr) )
376 {
377 wxLogApiError(wxT("IAutoComplete::Init"), hr);
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,
386 reinterpret_cast<void **>(&pAutoComplete2)
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();
398 return true;
399 #else // !HAS_AUTOCOMPLETE
400 wxUnusedVar(choices);
401
402 return false;
403 #endif // HAS_AUTOCOMPLETE/!HAS_AUTOCOMPLETE
404 }
405 #endif // wxUSE_OLE
406
407 // ----------------------------------------------------------------------------
408 // editable state
409 // ----------------------------------------------------------------------------
410
411 bool wxTextEntry::IsEditable() const
412 {
413 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
414 }
415
416 void wxTextEntry::SetEditable(bool editable)
417 {
418 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
419 }
420
421 // ----------------------------------------------------------------------------
422 // max length
423 // ----------------------------------------------------------------------------
424
425 void 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 }
436
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
448 bool wxTextEntry::SetHint(const wxString& hint)
449 {
450 if ( wxUxThemeEngine::GetIfActive() )
451 {
452 // notice that this message always works with Unicode strings
453 if ( ::SendMessage(GetEditHwnd(), EM_SETCUEBANNER,
454 0, (LPARAM)(const wchar_t *)hint.wc_str()) )
455 return true;
456 }
457
458 return wxTextEntryBase::SetHint(hint);
459 }
460
461 wxString wxTextEntry::GetHint() const
462 {
463 if ( wxUxThemeEngine::GetIfActive() )
464 {
465 wchar_t buf[256];
466 if ( ::SendMessage(GetEditHwnd(), EM_GETCUEBANNER,
467 (WPARAM)buf, WXSIZEOF(buf)) )
468 return wxString(buf);
469 }
470
471 return wxTextEntryBase::GetHint();
472 }
473
474
475 #endif // wxUSE_UXTHEME
476
477 // ----------------------------------------------------------------------------
478 // margins support
479 // ----------------------------------------------------------------------------
480
481 bool wxTextEntry::DoSetMargins(const wxPoint& margins)
482 {
483 #if !defined(__WXWINCE__)
484 bool res = true;
485
486 if ( margins.x != -1 )
487 {
488 // left margin
489 ::SendMessage(GetEditHwnd(), EM_SETMARGINS,
490 EC_LEFTMARGIN, MAKELONG(margins.x, 0));
491 }
492
493 if ( margins.y != -1 )
494 {
495 res = false;
496 }
497
498 return res;
499 #else
500 return false;
501 #endif
502 }
503
504 wxPoint wxTextEntry::DoGetMargins() const
505 {
506 #if !defined(__WXWINCE__)
507 LRESULT lResult = ::SendMessage(GetEditHwnd(), EM_GETMARGINS,
508 0, 0);
509 int left = LOWORD(lResult);
510 int top = -1;
511 return wxPoint(left, top);
512 #else
513 return wxPoint(-1, -1);
514 #endif
515 }
516
517 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX