2 * AutoComplete interfaces implementation.
4 * Copyright 2004 Maxime Bellengé <maxime.bellenge@laposte.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 - ACO_AUTOAPPEND style
24 - ACO_AUTOSUGGEST style
25 - ACO_UPDOWNKEYDROPSLIST style
27 - Handle pwzsRegKeyPath and pwszQuickComplete in Init
30 - implement ACO_SEARCH style
31 - implement ACO_FILTERPREFIXES style
32 - implement ACO_USETAB style
33 - implement ACO_RTLREADING style
44 #include "wine/debug.h"
48 #include "undocshell.h"
58 #include "wine/unicode.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(shell);
64 const IAutoComplete2Vtbl *lpVtbl;
65 const IAutoCompleteDropDownVtbl *lpDropDownVtbl;
70 WNDPROC wpOrigEditProc;
71 WNDPROC wpOrigLBoxProc;
75 AUTOCOMPLETEOPTIONS options;
78 static const IAutoComplete2Vtbl acvt;
79 static const IAutoCompleteDropDownVtbl acdropdownvt;
83 converts This to an interface pointer
85 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
86 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpVtbl)
87 #define _IAutoCompleteDropDown_(This) (IAutoCompleteDropDown*)&(This->lpDropDownVtbl)
89 static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
91 return (IAutoCompleteImpl *)((char *)iface - FIELD_OFFSET(IAutoCompleteImpl, lpDropDownVtbl));
94 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
95 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
97 static void create_listbox(IAutoCompleteImpl *This)
101 hwndParent = GetParent(This->hwndEdit);
103 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
104 This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
105 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
106 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
108 (HINSTANCE)GetWindowLongPtrW( hwndParent, GWLP_HINSTANCE ), NULL);
110 if (This->hwndListBox) {
111 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
112 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
116 /**************************************************************************
117 * IAutoComplete_Constructor
119 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
121 IAutoCompleteImpl *lpac;
123 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
124 return CLASS_E_NOAGGREGATION;
126 lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
128 return E_OUTOFMEMORY;
131 lpac->lpVtbl = &acvt;
132 lpac->lpDropDownVtbl = &acdropdownvt;
133 lpac->enabled = TRUE;
134 lpac->enumstr = NULL;
135 lpac->options = ACO_AUTOAPPEND;
136 lpac->wpOrigEditProc = NULL;
137 lpac->hwndListBox = NULL;
138 lpac->txtbackup = NULL;
139 lpac->quickComplete = NULL;
141 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac), riid, ppv))) {
142 IUnknown_Release (_IUnknown_ (lpac));
143 return E_NOINTERFACE;
146 TRACE("-- (%p)->\n",lpac);
151 /**************************************************************************
152 * AutoComplete_QueryInterface
154 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
155 IAutoComplete2 * iface,
159 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
161 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
164 if (IsEqualIID(riid, &IID_IUnknown) ||
165 IsEqualIID(riid, &IID_IAutoComplete) ||
166 IsEqualIID(riid, &IID_IAutoComplete2))
168 *ppvObj = (IAutoComplete2*)This;
170 else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
172 *ppvObj = _IAutoCompleteDropDown_(This);
177 IUnknown_AddRef((IUnknown*)*ppvObj);
178 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
181 WARN("unsupported interface: %s\n", debugstr_guid(riid));
182 return E_NOINTERFACE;
185 /******************************************************************************
186 * IAutoComplete2_fnAddRef
188 static ULONG WINAPI IAutoComplete2_fnAddRef(
189 IAutoComplete2 * iface)
191 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
192 ULONG refCount = InterlockedIncrement(&This->ref);
194 TRACE("(%p)->(%u)\n", This, refCount - 1);
199 /******************************************************************************
200 * IAutoComplete2_fnRelease
202 static ULONG WINAPI IAutoComplete2_fnRelease(
203 IAutoComplete2 * iface)
205 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
206 ULONG refCount = InterlockedDecrement(&This->ref);
208 TRACE("(%p)->(%u)\n", This, refCount + 1);
211 TRACE(" destroying IAutoComplete(%p)\n",This);
212 HeapFree(GetProcessHeap(), 0, This->quickComplete);
213 HeapFree(GetProcessHeap(), 0, This->txtbackup);
214 if (This->hwndListBox)
215 DestroyWindow(This->hwndListBox);
217 IEnumString_Release(This->enumstr);
218 HeapFree(GetProcessHeap(), 0, This);
223 /******************************************************************************
224 * IAutoComplete2_fnEnable
226 static HRESULT WINAPI IAutoComplete2_fnEnable(
227 IAutoComplete2 * iface,
230 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
234 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
236 This->enabled = fEnable;
241 /******************************************************************************
242 * IAutoComplete2_fnInit
244 static HRESULT WINAPI IAutoComplete2_fnInit(
245 IAutoComplete2 * iface,
248 LPCOLESTR pwzsRegKeyPath,
249 LPCOLESTR pwszQuickComplete)
251 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
253 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
254 This, (long)hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
256 if (This->options & ACO_AUTOSUGGEST) TRACE(" ACO_AUTOSUGGEST\n");
257 if (This->options & ACO_AUTOAPPEND) TRACE(" ACO_AUTOAPPEND\n");
258 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
259 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
260 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
261 if (This->options & ACO_UPDOWNKEYDROPSLIST) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
262 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
264 This->hwndEdit = hwndEdit;
266 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
267 TRACE("No IEnumString interface\n");
268 return E_NOINTERFACE;
271 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
272 SetWindowLongPtrW( hwndEdit, GWLP_USERDATA, (LONG_PTR)This);
274 if (This->options & ACO_AUTOSUGGEST)
275 create_listbox(This);
277 if (pwzsRegKeyPath) {
279 WCHAR result[MAX_PATH];
285 /* pwszRegKeyPath contains the key as well as the value, so we split */
286 key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
287 strcpyW(key, pwzsRegKeyPath);
288 value = strrchrW(key, '\\');
291 /* Now value contains the value and buffer the key */
292 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
293 if (res != ERROR_SUCCESS) {
294 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
295 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
297 if (res == ERROR_SUCCESS) {
298 res = RegQueryValueW(hKey, value, result, &len);
299 if (res == ERROR_SUCCESS) {
300 This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
301 strcpyW(This->quickComplete, result);
305 HeapFree(GetProcessHeap(), 0, key);
308 if ((pwszQuickComplete) && (!This->quickComplete)) {
309 This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
310 lstrcpyW(This->quickComplete, pwszQuickComplete);
316 /**************************************************************************
317 * IAutoComplete2_fnGetOptions
319 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
320 IAutoComplete2 * iface,
325 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
327 TRACE("(%p) -> (%p)\n", This, pdwFlag);
329 *pdwFlag = This->options;
334 /**************************************************************************
335 * IAutoComplete2_fnSetOptions
337 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
338 IAutoComplete2 * iface,
343 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
345 TRACE("(%p) -> (0x%x)\n", This, dwFlag);
347 This->options = dwFlag;
349 if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
350 create_listbox(This);
355 /**************************************************************************
356 * IAutoCompleteDropDown_fnGetDropDownStatus
358 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
359 IAutoCompleteDropDown *iface,
363 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
365 FIXME("(%p) -> (%p, %p): stub\n", This, pdwFlags, ppwszString);
375 /**************************************************************************
376 * IAutoCompleteDropDown_fnResetEnumarator
378 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
379 IAutoCompleteDropDown *iface)
381 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
383 FIXME("(%p): stub\n", This);
390 /**************************************************************************
391 * IAutoComplete2 VTable
393 static const IAutoComplete2Vtbl acvt =
395 IAutoComplete2_fnQueryInterface,
396 IAutoComplete2_fnAddRef,
397 IAutoComplete2_fnRelease,
398 IAutoComplete2_fnInit,
399 IAutoComplete2_fnEnable,
401 IAutoComplete2_fnSetOptions,
402 IAutoComplete2_fnGetOptions,
406 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
407 REFIID riid, LPVOID *ppvObj)
409 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
410 return IAutoComplete2_fnQueryInterface(_IAutoComplete2_(This), riid, ppvObj);
413 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
415 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
416 return IAutoComplete2_fnAddRef(_IAutoComplete2_(This));
419 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
421 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
422 return IAutoComplete2_fnRelease(_IAutoComplete2_(This));
425 /**************************************************************************
426 * IAutoCompleteDropDown VTable
428 static const IAutoCompleteDropDownVtbl acdropdownvt =
430 IAutoCompleteDropDown_fnQueryInterface,
431 IAutoCompleteDropDown_fnAddRef,
432 IAutoCompleteDropDown_fnRelease,
433 IAutoCompleteDropDown_fnGetDropDownStatus,
434 IAutoCompleteDropDown_fnResetEnumerator,
438 Window procedure for autocompletion
440 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
442 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
448 BOOL control, filled, displayall = FALSE;
449 int cpt, height, sel;
451 if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
455 case CB_SHOWDROPDOWN:
456 ShowWindow(This->hwndListBox, SW_HIDE);
459 if ((This->options & ACO_AUTOSUGGEST) &&
460 ((HWND)wParam != This->hwndListBox))
462 ShowWindow(This->hwndListBox, SW_HIDE);
467 GetWindowTextW( hwnd, (LPWSTR)hwndText, 255);
471 /* If quickComplete is set and control is pressed, replace the string */
472 control = GetKeyState(VK_CONTROL) & 0x8000;
473 if (control && This->quickComplete) {
474 hwndQCText = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
475 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
476 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
477 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
478 SendMessageW(hwnd, EM_SETSEL, 0, sel);
479 HeapFree(GetProcessHeap(), 0, hwndQCText);
482 ShowWindow(This->hwndListBox, SW_HIDE);
490 - if the listbox is not visible, displays it
491 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
492 is present but does not select anything.
493 - if the listbox is visible, change the selection
495 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
496 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
498 /* We must display all the entries */
501 if (IsWindowVisible(This->hwndListBox)) {
504 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
505 /* Change the selection */
506 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
508 sel = ((sel-1)<0)?count-1:sel-1;
510 sel = ((sel+1)>= count)?-1:sel+1;
511 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
516 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
517 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
518 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
519 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
520 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
521 HeapFree(GetProcessHeap(), 0, msg);
523 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
524 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
532 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
533 ShowWindow(This->hwndListBox, SW_HIDE);
534 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
536 if (This->options & ACO_AUTOAPPEND) {
538 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, (LPARAM)NULL);
540 hwndText[b-1] = '\0';
543 SetWindowTextW(hwnd, hwndText);
551 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
553 HeapFree(GetProcessHeap(), 0, This->txtbackup);
554 This->txtbackup = HeapAlloc(GetProcessHeap(),
555 HEAP_ZERO_MEMORY, (lstrlenW(hwndText)+1)*sizeof(WCHAR));
556 lstrcpyW(This->txtbackup, hwndText);
558 /* Returns if there is no text to search and we doesn't want to display all the entries */
559 if ((!displayall) && (! *hwndText) )
562 IEnumString_Reset(This->enumstr);
566 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
570 if (strstrW(strs, hwndText) == strs) {
571 if (!filled && (This->options & ACO_AUTOAPPEND)) {
572 SetWindowTextW(hwnd, strs);
573 SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(strs));
574 if (!(This->options & ACO_AUTOSUGGEST))
578 if (This->options & ACO_AUTOSUGGEST) {
579 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
587 if (This->options & ACO_AUTOSUGGEST) {
589 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
590 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
591 GetWindowRect(hwnd, &r);
592 SetParent(This->hwndListBox, HWND_DESKTOP);
593 /* It seems that Windows XP displays 7 lines at most
594 and otherwise displays a vertical scroll bar */
595 SetWindowPos(This->hwndListBox, HWND_TOP,
596 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
599 ShowWindow(This->hwndListBox, SW_HIDE);
605 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
612 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
614 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
620 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
621 SendMessageW(hwnd, LB_SETCURSEL, (WPARAM)sel, (LPARAM)0);
624 sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
627 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
628 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
629 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
630 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
631 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
632 ShowWindow(hwnd, SW_HIDE);
633 HeapFree(GetProcessHeap(), 0, msg);
636 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);