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
34 - implement ResetEnumerator
35 - string compares should be case-insensitive, the content of the list should be sorted
46 #include "wine/debug.h"
50 #include "undocshell.h"
59 #include "shell32_main.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(shell);
67 IAutoComplete2 IAutoComplete2_iface;
68 IAutoCompleteDropDown IAutoCompleteDropDown_iface;
74 WNDPROC wpOrigEditProc;
75 WNDPROC wpOrigLBoxProc;
79 AUTOCOMPLETEOPTIONS options;
82 static const IAutoComplete2Vtbl acvt;
83 static const IAutoCompleteDropDownVtbl acdropdownvt;
85 static const WCHAR autocomplete_propertyW[] = {'W','i','n','e',' ','A','u','t','o',
86 'c','o','m','p','l','e','t','e',' ',
87 'c','o','n','t','r','o','l',0};
89 static inline IAutoCompleteImpl *impl_from_IAutoComplete2(IAutoComplete2 *iface)
91 return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoComplete2_iface);
94 static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
96 return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoCompleteDropDown_iface);
99 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
100 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
102 static void create_listbox(IAutoCompleteImpl *This)
106 hwndParent = GetParent(This->hwndEdit);
108 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
109 This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
110 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
111 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
112 hwndParent, NULL, shell32_hInstance, NULL );
114 if (This->hwndListBox) {
115 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
116 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
120 /**************************************************************************
121 * IAutoComplete_Constructor
123 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
125 IAutoCompleteImpl *lpac;
128 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
129 return CLASS_E_NOAGGREGATION;
131 lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
133 return E_OUTOFMEMORY;
136 lpac->IAutoComplete2_iface.lpVtbl = &acvt;
137 lpac->IAutoCompleteDropDown_iface.lpVtbl = &acdropdownvt;
138 lpac->enabled = TRUE;
139 lpac->options = ACO_AUTOAPPEND;
141 hr = IAutoComplete2_QueryInterface(&lpac->IAutoComplete2_iface, riid, ppv);
142 IAutoComplete2_Release(&lpac->IAutoComplete2_iface);
144 TRACE("-- (%p)->\n",lpac);
149 /**************************************************************************
150 * AutoComplete_QueryInterface
152 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
153 IAutoComplete2 * iface,
157 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
159 TRACE("(%p)->(IID:%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
162 if (IsEqualIID(riid, &IID_IUnknown) ||
163 IsEqualIID(riid, &IID_IAutoComplete) ||
164 IsEqualIID(riid, &IID_IAutoComplete2))
168 else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
170 *ppvObj = &This->IAutoCompleteDropDown_iface;
175 IUnknown_AddRef((IUnknown*)*ppvObj);
176 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
179 WARN("unsupported interface: %s\n", debugstr_guid(riid));
180 return E_NOINTERFACE;
183 /******************************************************************************
184 * IAutoComplete2_fnAddRef
186 static ULONG WINAPI IAutoComplete2_fnAddRef(
187 IAutoComplete2 * iface)
189 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
190 ULONG refCount = InterlockedIncrement(&This->ref);
192 TRACE("(%p)->(%u)\n", This, refCount - 1);
197 /******************************************************************************
198 * IAutoComplete2_fnRelease
200 static ULONG WINAPI IAutoComplete2_fnRelease(
201 IAutoComplete2 * iface)
203 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
204 ULONG refCount = InterlockedDecrement(&This->ref);
206 TRACE("(%p)->(%u)\n", This, refCount + 1);
209 TRACE("destroying IAutoComplete(%p)\n", This);
210 HeapFree(GetProcessHeap(), 0, This->quickComplete);
211 HeapFree(GetProcessHeap(), 0, This->txtbackup);
213 IEnumString_Release(This->enumstr);
214 HeapFree(GetProcessHeap(), 0, This);
219 /******************************************************************************
220 * IAutoComplete2_fnEnable
222 static HRESULT WINAPI IAutoComplete2_fnEnable(
223 IAutoComplete2 * iface,
226 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
229 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
231 This->enabled = fEnable;
236 /******************************************************************************
237 * IAutoComplete2_fnInit
239 static HRESULT WINAPI IAutoComplete2_fnInit(
240 IAutoComplete2 * iface,
243 LPCOLESTR pwzsRegKeyPath,
244 LPCOLESTR pwszQuickComplete)
246 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
248 TRACE("(%p)->(%p, %p, %s, %s)\n",
249 This, hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
251 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
252 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
253 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
254 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
256 if (!hwndEdit || !punkACL)
259 if (This->initialized)
261 WARN("Autocompletion object is already initialized\n");
262 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
263 return This->hwndEdit ? E_FAIL : E_UNEXPECTED;
266 if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
267 WARN("No IEnumString interface\n");
268 return E_NOINTERFACE;
271 This->initialized = TRUE;
272 This->hwndEdit = hwndEdit;
273 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
274 /* Keep at least one reference to the object until the edit window is destroyed. */
275 IAutoComplete2_AddRef(&This->IAutoComplete2_iface);
276 SetPropW( hwndEdit, autocomplete_propertyW, This );
278 if (This->options & ACO_AUTOSUGGEST)
279 create_listbox(This);
281 if (pwzsRegKeyPath) {
283 WCHAR result[MAX_PATH];
289 /* pwszRegKeyPath contains the key as well as the value, so we split */
290 key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
291 strcpyW(key, pwzsRegKeyPath);
292 value = strrchrW(key, '\\');
295 /* Now value contains the value and buffer the key */
296 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
297 if (res != ERROR_SUCCESS) {
298 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
299 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
301 if (res == ERROR_SUCCESS) {
302 res = RegQueryValueW(hKey, value, result, &len);
303 if (res == ERROR_SUCCESS) {
304 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
305 strcpyW(This->quickComplete, result);
309 HeapFree(GetProcessHeap(), 0, key);
312 if ((pwszQuickComplete) && (!This->quickComplete)) {
313 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
314 lstrcpyW(This->quickComplete, pwszQuickComplete);
320 /**************************************************************************
321 * IAutoComplete2_fnGetOptions
323 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
324 IAutoComplete2 * iface,
327 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
330 TRACE("(%p) -> (%p)\n", This, pdwFlag);
332 *pdwFlag = This->options;
337 /**************************************************************************
338 * IAutoComplete2_fnSetOptions
340 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
341 IAutoComplete2 * iface,
344 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
347 TRACE("(%p) -> (0x%x)\n", This, dwFlag);
349 This->options = dwFlag;
351 if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
352 create_listbox(This);
357 /**************************************************************************
358 * IAutoCompleteDropDown_fnGetDropDownStatus
360 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
361 IAutoCompleteDropDown *iface,
365 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
368 TRACE("(%p) -> (%p, %p)\n", This, pdwFlags, ppwszString);
370 dropped = IsWindowVisible(This->hwndListBox);
373 *pdwFlags = (dropped ? ACDD_VISIBLE : 0);
379 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
384 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
385 *ppwszString = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
386 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)*ppwszString);
398 /**************************************************************************
399 * IAutoCompleteDropDown_fnResetEnumarator
401 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
402 IAutoCompleteDropDown *iface)
404 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
406 FIXME("(%p): stub\n", This);
413 /**************************************************************************
414 * IAutoComplete2 VTable
416 static const IAutoComplete2Vtbl acvt =
418 IAutoComplete2_fnQueryInterface,
419 IAutoComplete2_fnAddRef,
420 IAutoComplete2_fnRelease,
421 IAutoComplete2_fnInit,
422 IAutoComplete2_fnEnable,
424 IAutoComplete2_fnSetOptions,
425 IAutoComplete2_fnGetOptions,
429 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
430 REFIID riid, LPVOID *ppvObj)
432 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
433 return IAutoComplete2_fnQueryInterface(&This->IAutoComplete2_iface, riid, ppvObj);
436 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
438 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
439 return IAutoComplete2_fnAddRef(&This->IAutoComplete2_iface);
442 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
444 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
445 return IAutoComplete2_fnRelease(&This->IAutoComplete2_iface);
448 /**************************************************************************
449 * IAutoCompleteDropDown VTable
451 static const IAutoCompleteDropDownVtbl acdropdownvt =
453 IAutoCompleteDropDown_fnQueryInterface,
454 IAutoCompleteDropDown_fnAddRef,
455 IAutoCompleteDropDown_fnRelease,
456 IAutoCompleteDropDown_fnGetDropDownStatus,
457 IAutoCompleteDropDown_fnResetEnumerator,
461 Window procedure for autocompletion
463 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
465 IAutoCompleteImpl *This = GetPropW(hwnd, autocomplete_propertyW);
471 BOOL control, filled, displayall = FALSE;
472 int cpt, height, sel;
474 if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
478 case CB_SHOWDROPDOWN:
479 ShowWindow(This->hwndListBox, SW_HIDE);
482 if ((This->options & ACO_AUTOSUGGEST) &&
483 ((HWND)wParam != This->hwndListBox))
485 ShowWindow(This->hwndListBox, SW_HIDE);
487 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
489 GetWindowTextW( hwnd, hwndText, sizeof(hwndText)/sizeof(WCHAR));
493 /* If quickComplete is set and control is pressed, replace the string */
494 control = GetKeyState(VK_CONTROL) & 0x8000;
495 if (control && This->quickComplete) {
496 hwndQCText = HeapAlloc(GetProcessHeap(), 0,
497 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
498 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
499 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
500 SendMessageW(hwnd, EM_SETSEL, 0, sel);
501 HeapFree(GetProcessHeap(), 0, hwndQCText);
504 ShowWindow(This->hwndListBox, SW_HIDE);
512 - if the listbox is not visible, displays it
513 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
514 is present but does not select anything.
515 - if the listbox is visible, change the selection
517 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
518 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
520 /* We must display all the entries */
523 if (IsWindowVisible(This->hwndListBox)) {
526 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
527 /* Change the selection */
528 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
530 sel = ((sel-1)<0)?count-1:sel-1;
532 sel = ((sel+1)>= count)?-1:sel+1;
533 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
538 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
539 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
540 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
541 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
542 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
543 HeapFree(GetProcessHeap(), 0, msg);
545 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
546 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
554 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
555 ShowWindow(This->hwndListBox, SW_HIDE);
556 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
558 if (This->options & ACO_AUTOAPPEND) {
560 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, 0);
562 hwndText[b-1] = '\0';
565 SetWindowTextW(hwnd, hwndText);
573 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
575 HeapFree(GetProcessHeap(), 0, This->txtbackup);
576 This->txtbackup = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(hwndText)+1)*sizeof(WCHAR));
577 lstrcpyW(This->txtbackup, hwndText);
579 /* Returns if there is no text to search and we doesn't want to display all the entries */
580 if ((!displayall) && (! *hwndText) )
583 IEnumString_Reset(This->enumstr);
587 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
591 if (StrStrIW(strs, hwndText) == strs) {
592 if (!filled && (This->options & ACO_AUTOAPPEND)) {
593 INT typed_length = strlenW(hwndText);
596 strcpyW(buffW, hwndText);
597 strcatW(buffW, &strs[typed_length]);
598 SetWindowTextW(hwnd, buffW);
599 SendMessageW(hwnd, EM_SETSEL, typed_length, strlenW(strs));
600 if (!(This->options & ACO_AUTOSUGGEST))
604 if (This->options & ACO_AUTOSUGGEST) {
605 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
613 if (This->options & ACO_AUTOSUGGEST) {
615 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
616 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
617 GetWindowRect(hwnd, &r);
618 SetParent(This->hwndListBox, HWND_DESKTOP);
619 /* It seems that Windows XP displays 7 lines at most
620 and otherwise displays a vertical scroll bar */
621 SetWindowPos(This->hwndListBox, HWND_TOP,
622 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
625 ShowWindow(This->hwndListBox, SW_HIDE);
632 WNDPROC proc = This->wpOrigEditProc;
634 RemovePropW(hwnd, autocomplete_propertyW);
635 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)proc);
636 This->hwndEdit = NULL;
637 if (This->hwndListBox)
638 DestroyWindow(This->hwndListBox);
639 IAutoComplete2_Release(&This->IAutoComplete2_iface);
640 return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
643 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
650 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
652 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
658 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
659 SendMessageW(hwnd, LB_SETCURSEL, sel, 0);
662 sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
665 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
666 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
667 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
668 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
669 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
670 ShowWindow(hwnd, SW_HIDE);
671 HeapFree(GetProcessHeap(), 0, msg);
674 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);