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