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 const IAutoComplete2Vtbl *lpVtbl;
68 const IAutoCompleteDropDownVtbl *lpDropDownVtbl;
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 converts This to an interface pointer
91 #define _IUnknown_(This) ((IUnknown*)&(This)->lpVtbl)
92 #define _IAutoComplete2_(This) ((IAutoComplete2*)&(This)->lpVtbl)
93 #define _IAutoCompleteDropDown_(This) (&(This)->lpDropDownVtbl)
95 static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
97 return (IAutoCompleteImpl *)((char *)iface - FIELD_OFFSET(IAutoCompleteImpl, lpDropDownVtbl));
100 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
101 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
103 static void create_listbox(IAutoCompleteImpl *This)
107 hwndParent = GetParent(This->hwndEdit);
109 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
110 This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
111 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
112 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
113 hwndParent, NULL, shell32_hInstance, NULL );
115 if (This->hwndListBox) {
116 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
117 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
121 /**************************************************************************
122 * IAutoComplete_Constructor
124 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
126 IAutoCompleteImpl *lpac;
129 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
130 return CLASS_E_NOAGGREGATION;
132 lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
134 return E_OUTOFMEMORY;
137 lpac->lpVtbl = &acvt;
138 lpac->lpDropDownVtbl = &acdropdownvt;
139 lpac->enabled = TRUE;
140 lpac->options = ACO_AUTOAPPEND;
142 hr = IUnknown_QueryInterface(_IUnknown_ (lpac), riid, ppv);
143 IUnknown_Release(_IUnknown_ (lpac));
145 TRACE("-- (%p)->\n",lpac);
150 /**************************************************************************
151 * AutoComplete_QueryInterface
153 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
154 IAutoComplete2 * iface,
158 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
160 TRACE("(%p)->(IID:%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
163 if (IsEqualIID(riid, &IID_IUnknown) ||
164 IsEqualIID(riid, &IID_IAutoComplete) ||
165 IsEqualIID(riid, &IID_IAutoComplete2))
169 else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
171 *ppvObj = _IAutoCompleteDropDown_(This);
176 IUnknown_AddRef((IUnknown*)*ppvObj);
177 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
180 WARN("unsupported interface: %s\n", debugstr_guid(riid));
181 return E_NOINTERFACE;
184 /******************************************************************************
185 * IAutoComplete2_fnAddRef
187 static ULONG WINAPI IAutoComplete2_fnAddRef(
188 IAutoComplete2 * iface)
190 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
191 ULONG refCount = InterlockedIncrement(&This->ref);
193 TRACE("(%p)->(%u)\n", This, refCount - 1);
198 /******************************************************************************
199 * IAutoComplete2_fnRelease
201 static ULONG WINAPI IAutoComplete2_fnRelease(
202 IAutoComplete2 * iface)
204 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
205 ULONG refCount = InterlockedDecrement(&This->ref);
207 TRACE("(%p)->(%u)\n", This, refCount + 1);
210 TRACE("destroying IAutoComplete(%p)\n", This);
211 HeapFree(GetProcessHeap(), 0, This->quickComplete);
212 HeapFree(GetProcessHeap(), 0, This->txtbackup);
214 IEnumString_Release(This->enumstr);
215 HeapFree(GetProcessHeap(), 0, This);
220 /******************************************************************************
221 * IAutoComplete2_fnEnable
223 static HRESULT WINAPI IAutoComplete2_fnEnable(
224 IAutoComplete2 * iface,
227 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
231 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
233 This->enabled = fEnable;
238 /******************************************************************************
239 * IAutoComplete2_fnInit
241 static HRESULT WINAPI IAutoComplete2_fnInit(
242 IAutoComplete2 * iface,
245 LPCOLESTR pwzsRegKeyPath,
246 LPCOLESTR pwszQuickComplete)
248 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
250 TRACE("(%p)->(%p, %p, %s, %s)\n",
251 This, hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
253 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
254 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
255 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
256 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
258 if (!hwndEdit || !punkACL)
261 if (This->initialized)
263 WARN("Autocompletion object is already initialized\n");
264 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
265 return This->hwndEdit ? E_FAIL : E_UNEXPECTED;
268 if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
269 WARN("No IEnumString interface\n");
270 return E_NOINTERFACE;
273 This->initialized = TRUE;
274 This->hwndEdit = hwndEdit;
275 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
276 /* Keep at least one reference to the object until the edit window is destroyed. */
277 IAutoComplete2_AddRef((IAutoComplete2 *)This);
278 SetPropW( hwndEdit, autocomplete_propertyW, This );
280 if (This->options & ACO_AUTOSUGGEST)
281 create_listbox(This);
283 if (pwzsRegKeyPath) {
285 WCHAR result[MAX_PATH];
291 /* pwszRegKeyPath contains the key as well as the value, so we split */
292 key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
293 strcpyW(key, pwzsRegKeyPath);
294 value = strrchrW(key, '\\');
297 /* Now value contains the value and buffer the key */
298 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
299 if (res != ERROR_SUCCESS) {
300 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
301 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
303 if (res == ERROR_SUCCESS) {
304 res = RegQueryValueW(hKey, value, result, &len);
305 if (res == ERROR_SUCCESS) {
306 This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
307 strcpyW(This->quickComplete, result);
311 HeapFree(GetProcessHeap(), 0, key);
314 if ((pwszQuickComplete) && (!This->quickComplete)) {
315 This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
316 lstrcpyW(This->quickComplete, pwszQuickComplete);
322 /**************************************************************************
323 * IAutoComplete2_fnGetOptions
325 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
326 IAutoComplete2 * iface,
331 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
333 TRACE("(%p) -> (%p)\n", This, pdwFlag);
335 *pdwFlag = This->options;
340 /**************************************************************************
341 * IAutoComplete2_fnSetOptions
343 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
344 IAutoComplete2 * iface,
349 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
351 TRACE("(%p) -> (0x%x)\n", This, dwFlag);
353 This->options = dwFlag;
355 if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
356 create_listbox(This);
361 /**************************************************************************
362 * IAutoCompleteDropDown_fnGetDropDownStatus
364 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
365 IAutoCompleteDropDown *iface,
369 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
372 TRACE("(%p) -> (%p, %p)\n", This, pdwFlags, ppwszString);
374 dropped = IsWindowVisible(This->hwndListBox);
377 *pdwFlags = (dropped ? ACDD_VISIBLE : 0);
383 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
388 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
389 *ppwszString = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
390 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)*ppwszString);
402 /**************************************************************************
403 * IAutoCompleteDropDown_fnResetEnumarator
405 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
406 IAutoCompleteDropDown *iface)
408 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
410 FIXME("(%p): stub\n", This);
417 /**************************************************************************
418 * IAutoComplete2 VTable
420 static const IAutoComplete2Vtbl acvt =
422 IAutoComplete2_fnQueryInterface,
423 IAutoComplete2_fnAddRef,
424 IAutoComplete2_fnRelease,
425 IAutoComplete2_fnInit,
426 IAutoComplete2_fnEnable,
428 IAutoComplete2_fnSetOptions,
429 IAutoComplete2_fnGetOptions,
433 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
434 REFIID riid, LPVOID *ppvObj)
436 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
437 return IAutoComplete2_fnQueryInterface(_IAutoComplete2_(This), riid, ppvObj);
440 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
442 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
443 return IAutoComplete2_fnAddRef(_IAutoComplete2_(This));
446 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
448 IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
449 return IAutoComplete2_fnRelease(_IAutoComplete2_(This));
452 /**************************************************************************
453 * IAutoCompleteDropDown VTable
455 static const IAutoCompleteDropDownVtbl acdropdownvt =
457 IAutoCompleteDropDown_fnQueryInterface,
458 IAutoCompleteDropDown_fnAddRef,
459 IAutoCompleteDropDown_fnRelease,
460 IAutoCompleteDropDown_fnGetDropDownStatus,
461 IAutoCompleteDropDown_fnResetEnumerator,
465 Window procedure for autocompletion
467 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
469 IAutoCompleteImpl *This = GetPropW(hwnd, autocomplete_propertyW);
475 BOOL control, filled, displayall = FALSE;
476 int cpt, height, sel;
478 if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
482 case CB_SHOWDROPDOWN:
483 ShowWindow(This->hwndListBox, SW_HIDE);
486 if ((This->options & ACO_AUTOSUGGEST) &&
487 ((HWND)wParam != This->hwndListBox))
489 ShowWindow(This->hwndListBox, SW_HIDE);
491 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
493 GetWindowTextW( hwnd, hwndText, 255);
497 /* If quickComplete is set and control is pressed, replace the string */
498 control = GetKeyState(VK_CONTROL) & 0x8000;
499 if (control && This->quickComplete) {
500 hwndQCText = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
501 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
502 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
503 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
504 SendMessageW(hwnd, EM_SETSEL, 0, sel);
505 HeapFree(GetProcessHeap(), 0, hwndQCText);
508 ShowWindow(This->hwndListBox, SW_HIDE);
516 - if the listbox is not visible, displays it
517 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
518 is present but does not select anything.
519 - if the listbox is visible, change the selection
521 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
522 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
524 /* We must display all the entries */
527 if (IsWindowVisible(This->hwndListBox)) {
530 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
531 /* Change the selection */
532 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
534 sel = ((sel-1)<0)?count-1:sel-1;
536 sel = ((sel+1)>= count)?-1:sel+1;
537 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
542 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
543 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
544 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
545 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
546 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
547 HeapFree(GetProcessHeap(), 0, msg);
549 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
550 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
558 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
559 ShowWindow(This->hwndListBox, SW_HIDE);
560 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
562 if (This->options & ACO_AUTOAPPEND) {
564 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, 0);
566 hwndText[b-1] = '\0';
569 SetWindowTextW(hwnd, hwndText);
577 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
579 HeapFree(GetProcessHeap(), 0, This->txtbackup);
580 This->txtbackup = HeapAlloc(GetProcessHeap(),
581 HEAP_ZERO_MEMORY, (lstrlenW(hwndText)+1)*sizeof(WCHAR));
582 lstrcpyW(This->txtbackup, hwndText);
584 /* Returns if there is no text to search and we doesn't want to display all the entries */
585 if ((!displayall) && (! *hwndText) )
588 IEnumString_Reset(This->enumstr);
592 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
596 if (strstrW(strs, hwndText) == strs) {
597 if (!filled && (This->options & ACO_AUTOAPPEND)) {
598 SetWindowTextW(hwnd, strs);
599 SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(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((IAutoComplete2 *)This);
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(), HEAP_ZERO_MEMORY, (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);