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);
124 GetWindowTextW( hwnd, hwndText, sizeof(hwndText)/sizeof(WCHAR));
128 /* If quickComplete is set and control is pressed, replace the string */
129 control = GetKeyState(VK_CONTROL) & 0x8000;
130 if (control && This->quickComplete) {
131 hwndQCText = HeapAlloc(GetProcessHeap(), 0,
132 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
133 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
134 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
135 SendMessageW(hwnd, EM_SETSEL, 0, sel);
136 HeapFree(GetProcessHeap(), 0, hwndQCText);
139 ShowWindow(This->hwndListBox, SW_HIDE);
147 - if the listbox is not visible, displays it
148 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
149 is present but does not select anything.
150 - if the listbox is visible, change the selection
152 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
153 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
155 /* We must display all the entries */
158 if (IsWindowVisible(This->hwndListBox)) {
161 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
162 /* Change the selection */
163 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
165 sel = ((sel-1) < 0) ? count-1 : sel-1;
167 sel = ((sel+1) >= count) ? -1 : sel+1;
168 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
173 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
174 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
175 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
176 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
177 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
178 HeapFree(GetProcessHeap(), 0, msg);
180 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
181 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
189 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
190 ShowWindow(This->hwndListBox, SW_HIDE);
191 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
193 if (This->options & ACO_AUTOAPPEND) {
195 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, 0);
197 hwndText[b-1] = '\0';
200 SetWindowTextW(hwnd, hwndText);
208 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
210 HeapFree(GetProcessHeap(), 0, This->txtbackup);
211 This->txtbackup = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(hwndText)+1)*sizeof(WCHAR));
212 lstrcpyW(This->txtbackup, hwndText);
214 /* Returns if there is no text to search and we doesn't want to display all the entries */
215 if ((!displayall) && (! *hwndText) )
218 IEnumString_Reset(This->enumstr);
222 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
226 if (StrStrIW(strs, hwndText) == strs) {
227 if (!filled && (This->options & ACO_AUTOAPPEND)) {
228 INT typed_length = strlenW(hwndText);
231 strcpyW(buffW, hwndText);
232 strcatW(buffW, &strs[typed_length]);
233 SetWindowTextW(hwnd, buffW);
234 SendMessageW(hwnd, EM_SETSEL, typed_length, strlenW(strs));
235 if (!(This->options & ACO_AUTOSUGGEST))
239 if (This->options & ACO_AUTOSUGGEST) {
240 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
248 if (This->options & ACO_AUTOSUGGEST) {
250 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
251 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
252 GetWindowRect(hwnd, &r);
253 SetParent(This->hwndListBox, HWND_DESKTOP);
254 /* It seems that Windows XP displays 7 lines at most
255 and otherwise displays a vertical scroll bar */
256 SetWindowPos(This->hwndListBox, HWND_TOP,
257 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
260 ShowWindow(This->hwndListBox, SW_HIDE);
267 WNDPROC proc = This->wpOrigEditProc;
269 RemovePropW(hwnd, autocomplete_propertyW);
270 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)proc);
271 This->hwndEdit = NULL;
272 if (This->hwndListBox)
273 DestroyWindow(This->hwndListBox);
274 IAutoComplete2_Release(&This->IAutoComplete2_iface);
275 return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
278 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
285 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
287 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
293 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
294 SendMessageW(hwnd, LB_SETCURSEL, sel, 0);
297 sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
300 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
301 msg = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
302 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
303 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
304 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
305 ShowWindow(hwnd, SW_HIDE);
306 HeapFree(GetProcessHeap(), 0, msg);
309 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);
314 static void create_listbox(IAutoCompleteImpl *This)
318 hwndParent = GetParent(This->hwndEdit);
320 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
321 This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
322 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
323 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
324 hwndParent, NULL, shell32_hInstance, NULL );
326 if (This->hwndListBox) {
327 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
328 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
332 /**************************************************************************
333 * AutoComplete_QueryInterface
335 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
336 IAutoComplete2 * iface,
340 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
342 TRACE("(%p)->(IID:%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
345 if (IsEqualIID(riid, &IID_IUnknown) ||
346 IsEqualIID(riid, &IID_IAutoComplete) ||
347 IsEqualIID(riid, &IID_IAutoComplete2))
351 else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
353 *ppvObj = &This->IAutoCompleteDropDown_iface;
358 IUnknown_AddRef((IUnknown*)*ppvObj);
359 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
362 WARN("unsupported interface: %s\n", debugstr_guid(riid));
363 return E_NOINTERFACE;
366 /******************************************************************************
367 * IAutoComplete2_fnAddRef
369 static ULONG WINAPI IAutoComplete2_fnAddRef(
370 IAutoComplete2 * iface)
372 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
373 ULONG refCount = InterlockedIncrement(&This->ref);
375 TRACE("(%p)->(%u)\n", This, refCount - 1);
380 /******************************************************************************
381 * IAutoComplete2_fnRelease
383 static ULONG WINAPI IAutoComplete2_fnRelease(
384 IAutoComplete2 * iface)
386 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
387 ULONG refCount = InterlockedDecrement(&This->ref);
389 TRACE("(%p)->(%u)\n", This, refCount + 1);
392 TRACE("destroying IAutoComplete(%p)\n", This);
393 HeapFree(GetProcessHeap(), 0, This->quickComplete);
394 HeapFree(GetProcessHeap(), 0, This->txtbackup);
396 IEnumString_Release(This->enumstr);
397 HeapFree(GetProcessHeap(), 0, This);
402 /******************************************************************************
403 * IAutoComplete2_fnEnable
405 static HRESULT WINAPI IAutoComplete2_fnEnable(
406 IAutoComplete2 * iface,
409 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
412 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
414 This->enabled = fEnable;
419 /******************************************************************************
420 * IAutoComplete2_fnInit
422 static HRESULT WINAPI IAutoComplete2_fnInit(
423 IAutoComplete2 * iface,
426 LPCOLESTR pwzsRegKeyPath,
427 LPCOLESTR pwszQuickComplete)
429 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
431 TRACE("(%p)->(%p, %p, %s, %s)\n",
432 This, hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
434 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
435 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
436 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
437 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
439 if (!hwndEdit || !punkACL)
442 if (This->initialized)
444 WARN("Autocompletion object is already initialized\n");
445 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
446 return This->hwndEdit ? E_FAIL : E_UNEXPECTED;
449 if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
450 WARN("No IEnumString interface\n");
451 return E_NOINTERFACE;
454 This->initialized = TRUE;
455 This->hwndEdit = hwndEdit;
456 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
457 /* Keep at least one reference to the object until the edit window is destroyed. */
458 IAutoComplete2_AddRef(&This->IAutoComplete2_iface);
459 SetPropW( hwndEdit, autocomplete_propertyW, This );
461 if (This->options & ACO_AUTOSUGGEST)
462 create_listbox(This);
464 if (pwzsRegKeyPath) {
466 WCHAR result[MAX_PATH];
472 /* pwszRegKeyPath contains the key as well as the value, so we split */
473 key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
474 strcpyW(key, pwzsRegKeyPath);
475 value = strrchrW(key, '\\');
478 /* Now value contains the value and buffer the key */
479 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
480 if (res != ERROR_SUCCESS) {
481 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
482 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
484 if (res == ERROR_SUCCESS) {
485 res = RegQueryValueW(hKey, value, result, &len);
486 if (res == ERROR_SUCCESS) {
487 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
488 strcpyW(This->quickComplete, result);
492 HeapFree(GetProcessHeap(), 0, key);
495 if ((pwszQuickComplete) && (!This->quickComplete)) {
496 This->quickComplete = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
497 lstrcpyW(This->quickComplete, pwszQuickComplete);
503 /**************************************************************************
504 * IAutoComplete2_fnGetOptions
506 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
507 IAutoComplete2 * iface,
510 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
513 TRACE("(%p) -> (%p)\n", This, pdwFlag);
515 *pdwFlag = This->options;
520 /**************************************************************************
521 * IAutoComplete2_fnSetOptions
523 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
524 IAutoComplete2 * iface,
527 IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
530 TRACE("(%p) -> (0x%x)\n", This, dwFlag);
532 This->options = dwFlag;
534 if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
535 create_listbox(This);
540 /**************************************************************************
541 * IAutoComplete2 VTable
543 static const IAutoComplete2Vtbl acvt =
545 IAutoComplete2_fnQueryInterface,
546 IAutoComplete2_fnAddRef,
547 IAutoComplete2_fnRelease,
548 IAutoComplete2_fnInit,
549 IAutoComplete2_fnEnable,
551 IAutoComplete2_fnSetOptions,
552 IAutoComplete2_fnGetOptions,
556 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
557 REFIID riid, LPVOID *ppvObj)
559 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
560 return IAutoComplete2_fnQueryInterface(&This->IAutoComplete2_iface, riid, ppvObj);
563 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
565 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
566 return IAutoComplete2_fnAddRef(&This->IAutoComplete2_iface);
569 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
571 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
572 return IAutoComplete2_fnRelease(&This->IAutoComplete2_iface);
575 /**************************************************************************
576 * IAutoCompleteDropDown_fnGetDropDownStatus
578 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
579 IAutoCompleteDropDown *iface,
583 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
586 TRACE("(%p) -> (%p, %p)\n", This, pdwFlags, ppwszString);
588 dropped = IsWindowVisible(This->hwndListBox);
591 *pdwFlags = (dropped ? ACDD_VISIBLE : 0);
597 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
602 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
603 *ppwszString = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
604 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)*ppwszString);
616 /**************************************************************************
617 * IAutoCompleteDropDown_fnResetEnumarator
619 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
620 IAutoCompleteDropDown *iface)
622 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
624 FIXME("(%p): stub\n", This);
629 /**************************************************************************
630 * IAutoCompleteDropDown VTable
632 static const IAutoCompleteDropDownVtbl acdropdownvt =
634 IAutoCompleteDropDown_fnQueryInterface,
635 IAutoCompleteDropDown_fnAddRef,
636 IAutoCompleteDropDown_fnRelease,
637 IAutoCompleteDropDown_fnGetDropDownStatus,
638 IAutoCompleteDropDown_fnResetEnumerator,
641 /**************************************************************************
642 * IAutoComplete_Constructor
644 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
646 IAutoCompleteImpl *lpac;
649 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
650 return CLASS_E_NOAGGREGATION;
652 lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
654 return E_OUTOFMEMORY;
657 lpac->IAutoComplete2_iface.lpVtbl = &acvt;
658 lpac->IAutoCompleteDropDown_iface.lpVtbl = &acdropdownvt;
659 lpac->enabled = TRUE;
660 lpac->options = ACO_AUTOAPPEND;
662 hr = IAutoComplete2_QueryInterface(&lpac->IAutoComplete2_iface, riid, ppv);
663 IAutoComplete2_Release(&lpac->IAutoComplete2_iface);
665 TRACE("-- (%p)->\n",lpac);