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 WCHAR autocomplete_propertyW[] = {'W','i','n','e',' ','A','u','t','o',
83 'c','o','m','p','l','e','t','e',' ',
84 'c','o','n','t','r','o','l',0};
86 static inline IAutoCompleteImpl *impl_from_IAutoComplete2(IAutoComplete2 *iface)
88 return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoComplete2_iface);
91 static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
93 return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoCompleteDropDown_iface);
97 Window procedure for autocompletion
99 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
101 IAutoCompleteImpl *This = GetPropW(hwnd, autocomplete_propertyW);
107 BOOL control, filled, displayall = FALSE;
108 int cpt, height, sel;
110 if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
114 case CB_SHOWDROPDOWN:
115 ShowWindow(This->hwndListBox, SW_HIDE);
118 if ((This->options & ACO_AUTOSUGGEST) && ((HWND)wParam != This->hwndListBox))
120 ShowWindow(This->hwndListBox, SW_HIDE);
122 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
127 GetWindowTextW( hwnd, hwndText, sizeof(hwndText)/sizeof(WCHAR));
131 /* If quickComplete is set and control is pressed, replace the string */
132 control = GetKeyState(VK_CONTROL) & 0x8000;
133 if (control && This->quickComplete) {
134 hwndQCText = HeapAlloc(GetProcessHeap(), 0,
135 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
136 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
137 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
138 SendMessageW(hwnd, EM_SETSEL, 0, sel);
139 HeapFree(GetProcessHeap(), 0, hwndQCText);
142 ShowWindow(This->hwndListBox, SW_HIDE);
150 - if the listbox is not visible, displays it
151 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
152 is present but does not select anything.
153 - if the listbox is visible, change the selection
155 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
156 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
158 /* We must display all the entries */
161 if (IsWindowVisible(This->hwndListBox)) {
164 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
165 /* Change the selection */
166 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
168 sel = ((sel-1) < 0) ? count-1 : sel-1;
170 sel = ((sel+1) >= count) ? -1 : sel+1;
171 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
176 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
177 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
178 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
179 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
180 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
181 HeapFree(GetProcessHeap(), 0, msg);
183 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
184 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
192 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
193 ShowWindow(This->hwndListBox, SW_HIDE);
194 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
196 if (This->options & ACO_AUTOAPPEND) {
198 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, 0);
200 hwndText[b-1] = '\0';
203 SetWindowTextW(hwnd, hwndText);
211 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
213 HeapFree(GetProcessHeap(), 0, This->txtbackup);
214 len = strlenW(hwndText);
215 This->txtbackup = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
216 lstrcpyW(This->txtbackup, hwndText);
218 /* Returns if there is no text to search and we doesn't want to display all the entries */
219 if ((!displayall) && (! *hwndText) )
222 IEnumString_Reset(This->enumstr);
226 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
230 if (!strncmpiW(hwndText, strs, len)) {
231 if (!filled && (This->options & ACO_AUTOAPPEND)) {
234 strcpyW(buffW, hwndText);
235 strcatW(buffW, &strs[len]);
236 SetWindowTextW(hwnd, buffW);
237 SendMessageW(hwnd, EM_SETSEL, len, strlenW(strs));
238 if (!(This->options & ACO_AUTOSUGGEST))
242 if (This->options & ACO_AUTOSUGGEST) {
243 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
251 if (This->options & ACO_AUTOSUGGEST) {
253 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
254 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
255 GetWindowRect(hwnd, &r);
256 SetParent(This->hwndListBox, HWND_DESKTOP);
257 /* It seems that Windows XP displays 7 lines at most
258 and otherwise displays a vertical scroll bar */
259 SetWindowPos(This->hwndListBox, HWND_TOP,
260 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
263 ShowWindow(This->hwndListBox, SW_HIDE);
271 WNDPROC proc = This->wpOrigEditProc;
273 RemovePropW(hwnd, autocomplete_propertyW);
274 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)proc);
275 This->hwndEdit = NULL;
276 if (This->hwndListBox)
277 DestroyWindow(This->hwndListBox);
278 IAutoComplete2_Release(&This->IAutoComplete2_iface);
279 return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
282 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
289 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
291 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
297 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
298 SendMessageW(hwnd, LB_SETCURSEL, sel, 0);
301 sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
304 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
305 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
306 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
307 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
308 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
309 ShowWindow(hwnd, SW_HIDE);
310 HeapFree(GetProcessHeap(), 0, msg);
313 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);
318 static void create_listbox(IAutoCompleteImpl *This)
322 hwndParent = GetParent(This->hwndEdit);
324 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
325 This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
326 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
327 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
328 hwndParent, NULL, shell32_hInstance, NULL );
330 if (This->hwndListBox) {
331 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
332 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
336 /**************************************************************************
337 * AutoComplete_QueryInterface
339 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
340 IAutoComplete2 * iface,
344 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
346 TRACE("(%p)->(IID:%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
349 if (IsEqualIID(riid, &IID_IUnknown) ||
350 IsEqualIID(riid, &IID_IAutoComplete) ||
351 IsEqualIID(riid, &IID_IAutoComplete2))
355 else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
357 *ppvObj = &This->IAutoCompleteDropDown_iface;
362 IUnknown_AddRef((IUnknown*)*ppvObj);
363 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
366 WARN("unsupported interface: %s\n", debugstr_guid(riid));
367 return E_NOINTERFACE;
370 /******************************************************************************
371 * IAutoComplete2_fnAddRef
373 static ULONG WINAPI IAutoComplete2_fnAddRef(
374 IAutoComplete2 * iface)
376 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
377 ULONG refCount = InterlockedIncrement(&This->ref);
379 TRACE("(%p)->(%u)\n", This, refCount - 1);
384 /******************************************************************************
385 * IAutoComplete2_fnRelease
387 static ULONG WINAPI IAutoComplete2_fnRelease(
388 IAutoComplete2 * iface)
390 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
391 ULONG refCount = InterlockedDecrement(&This->ref);
393 TRACE("(%p)->(%u)\n", This, refCount + 1);
396 TRACE("destroying IAutoComplete(%p)\n", This);
397 HeapFree(GetProcessHeap(), 0, This->quickComplete);
398 HeapFree(GetProcessHeap(), 0, This->txtbackup);
400 IEnumString_Release(This->enumstr);
401 HeapFree(GetProcessHeap(), 0, This);
406 /******************************************************************************
407 * IAutoComplete2_fnEnable
409 static HRESULT WINAPI IAutoComplete2_fnEnable(
410 IAutoComplete2 * iface,
413 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
416 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
418 This->enabled = fEnable;
423 /******************************************************************************
424 * IAutoComplete2_fnInit
426 static HRESULT WINAPI IAutoComplete2_fnInit(
427 IAutoComplete2 * iface,
430 LPCOLESTR pwzsRegKeyPath,
431 LPCOLESTR pwszQuickComplete)
433 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
435 TRACE("(%p)->(%p, %p, %s, %s)\n",
436 This, hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
438 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
439 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
440 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
441 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
443 if (!hwndEdit || !punkACL)
446 if (This->initialized)
448 WARN("Autocompletion object is already initialized\n");
449 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
450 return This->hwndEdit ? E_FAIL : E_UNEXPECTED;
453 if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
454 WARN("No IEnumString interface\n");
455 return E_NOINTERFACE;
458 This->initialized = TRUE;
459 This->hwndEdit = hwndEdit;
460 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
461 /* Keep at least one reference to the object until the edit window is destroyed. */
462 IAutoComplete2_AddRef(&This->IAutoComplete2_iface);
463 SetPropW( hwndEdit, autocomplete_propertyW, This );
465 if (This->options & ACO_AUTOSUGGEST)
466 create_listbox(This);
468 if (pwzsRegKeyPath) {
470 WCHAR result[MAX_PATH];
476 /* pwszRegKeyPath contains the key as well as the value, so we split */
477 key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
478 strcpyW(key, pwzsRegKeyPath);
479 value = strrchrW(key, '\\');
482 /* Now value contains the value and buffer the key */
483 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
484 if (res != ERROR_SUCCESS) {
485 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
486 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
488 if (res == ERROR_SUCCESS) {
489 res = RegQueryValueW(hKey, value, result, &len);
490 if (res == ERROR_SUCCESS) {
491 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
492 strcpyW(This->quickComplete, result);
496 HeapFree(GetProcessHeap(), 0, key);
499 if ((pwszQuickComplete) && (!This->quickComplete)) {
500 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
501 lstrcpyW(This->quickComplete, pwszQuickComplete);
507 /**************************************************************************
508 * IAutoComplete2_fnGetOptions
510 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
511 IAutoComplete2 * iface,
514 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
517 TRACE("(%p) -> (%p)\n", This, pdwFlag);
519 *pdwFlag = This->options;
524 /**************************************************************************
525 * IAutoComplete2_fnSetOptions
527 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
528 IAutoComplete2 * iface,
531 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
534 TRACE("(%p) -> (0x%x)\n", This, dwFlag);
536 This->options = dwFlag;
538 if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
539 create_listbox(This);
544 /**************************************************************************
545 * IAutoComplete2 VTable
547 static const IAutoComplete2Vtbl acvt =
549 IAutoComplete2_fnQueryInterface,
550 IAutoComplete2_fnAddRef,
551 IAutoComplete2_fnRelease,
552 IAutoComplete2_fnInit,
553 IAutoComplete2_fnEnable,
555 IAutoComplete2_fnSetOptions,
556 IAutoComplete2_fnGetOptions,
560 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
561 REFIID riid, LPVOID *ppvObj)
563 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
564 return IAutoComplete2_fnQueryInterface(&This->IAutoComplete2_iface, riid, ppvObj);
567 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
569 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
570 return IAutoComplete2_fnAddRef(&This->IAutoComplete2_iface);
573 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
575 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
576 return IAutoComplete2_fnRelease(&This->IAutoComplete2_iface);
579 /**************************************************************************
580 * IAutoCompleteDropDown_fnGetDropDownStatus
582 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
583 IAutoCompleteDropDown *iface,
587 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
590 TRACE("(%p) -> (%p, %p)\n", This, pdwFlags, ppwszString);
592 dropped = IsWindowVisible(This->hwndListBox);
595 *pdwFlags = (dropped ? ACDD_VISIBLE : 0);
601 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
606 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
607 *ppwszString = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
608 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)*ppwszString);
620 /**************************************************************************
621 * IAutoCompleteDropDown_fnResetEnumarator
623 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
624 IAutoCompleteDropDown *iface)
626 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
628 FIXME("(%p): stub\n", This);
633 /**************************************************************************
634 * IAutoCompleteDropDown VTable
636 static const IAutoCompleteDropDownVtbl acdropdownvt =
638 IAutoCompleteDropDown_fnQueryInterface,
639 IAutoCompleteDropDown_fnAddRef,
640 IAutoCompleteDropDown_fnRelease,
641 IAutoCompleteDropDown_fnGetDropDownStatus,
642 IAutoCompleteDropDown_fnResetEnumerator,
645 /**************************************************************************
646 * IAutoComplete_Constructor
648 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
650 IAutoCompleteImpl *lpac;
653 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
654 return CLASS_E_NOAGGREGATION;
656 lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
658 return E_OUTOFMEMORY;
661 lpac->IAutoComplete2_iface.lpVtbl = &acvt;
662 lpac->IAutoCompleteDropDown_iface.lpVtbl = &acdropdownvt;
663 lpac->enabled = TRUE;
664 lpac->options = ACO_AUTOAPPEND;
666 hr = IAutoComplete2_QueryInterface(&lpac->IAutoComplete2_iface, riid, ppv);
667 IAutoComplete2_Release(&lpac->IAutoComplete2_iface);
669 TRACE("-- (%p)->\n",lpac);