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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
59 #include "wine/unicode.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(shell);
65 IAutoCompleteVtbl *lpVtbl;
66 IAutoComplete2Vtbl *lpvtblAutoComplete2;
71 WNDPROC wpOrigEditProc;
72 WNDPROC wpOrigLBoxProc;
76 AUTOCOMPLETEOPTIONS options;
79 static struct IAutoCompleteVtbl acvt;
80 static struct IAutoComplete2Vtbl ac2vt;
82 #define _IAutoComplete2_Offset ((int)(&(((IAutoCompleteImpl*)0)->lpvtblAutoComplete2)))
83 #define _ICOM_THIS_From_IAutoComplete2(class, name) class* This = (class*)(((char*)name)-_IAutoComplete2_Offset);
86 converts This to a interface pointer
88 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
89 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
91 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
92 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
94 /**************************************************************************
95 * IAutoComplete_Constructor
97 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
99 IAutoCompleteImpl *lpac;
101 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
102 return CLASS_E_NOAGGREGATION;
104 lpac = (IAutoCompleteImpl*)HeapAlloc(GetProcessHeap(),
105 HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
107 return E_OUTOFMEMORY;
110 lpac->lpVtbl = &acvt;
111 lpac->lpvtblAutoComplete2 = &ac2vt;
112 lpac->enabled = TRUE;
113 lpac->enumstr = NULL;
114 lpac->options = ACO_AUTOAPPEND;
115 lpac->wpOrigEditProc = NULL;
116 lpac->hwndListBox = NULL;
117 lpac->txtbackup = NULL;
118 lpac->quickComplete = NULL;
120 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac), riid, ppv))) {
121 IUnknown_Release (_IUnknown_ (lpac));
122 return E_NOINTERFACE;
125 TRACE("-- (%p)->\n",lpac);
130 /**************************************************************************
131 * AutoComplete_QueryInterface
133 static HRESULT WINAPI IAutoComplete_fnQueryInterface(
134 IAutoComplete * iface,
138 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
140 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
143 if(IsEqualIID(riid, &IID_IUnknown))
147 else if(IsEqualIID(riid, &IID_IAutoComplete))
149 *ppvObj = (IAutoComplete*)This;
151 else if(IsEqualIID(riid, &IID_IAutoComplete2))
153 *ppvObj = _IAutoComplete2_ (This);
158 IAutoComplete_AddRef((IAutoComplete*)*ppvObj);
159 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
162 TRACE("-- Interface: E_NOINTERFACE\n");
163 return E_NOINTERFACE;
166 /******************************************************************************
167 * IAutoComplete_fnAddRef
169 static ULONG WINAPI IAutoComplete_fnAddRef(
170 IAutoComplete * iface)
172 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
173 ULONG refCount = InterlockedIncrement(&This->ref);
175 TRACE("(%p)->(%lu)\n", This, refCount - 1);
180 /******************************************************************************
181 * IAutoComplete_fnRelease
183 static ULONG WINAPI IAutoComplete_fnRelease(
184 IAutoComplete * iface)
186 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
187 ULONG refCount = InterlockedDecrement(&This->ref);
189 TRACE("(%p)->(%lu)\n", This, refCount + 1);
192 TRACE(" destroying IAutoComplete(%p)\n",This);
193 HeapFree(GetProcessHeap(), 0, This->quickComplete);
194 HeapFree(GetProcessHeap(), 0, This->txtbackup);
195 if (This->hwndListBox)
196 DestroyWindow(This->hwndListBox);
198 IEnumString_Release(This->enumstr);
199 HeapFree(GetProcessHeap(), 0, This);
204 /******************************************************************************
205 * IAutoComplete_fnEnable
207 static HRESULT WINAPI IAutoComplete_fnEnable(
208 IAutoComplete * iface,
211 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
215 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
217 This->enabled = fEnable;
222 /******************************************************************************
223 * IAutoComplete_fnInit
225 static HRESULT WINAPI IAutoComplete_fnInit(
226 IAutoComplete * iface,
229 LPCOLESTR pwzsRegKeyPath,
230 LPCOLESTR pwszQuickComplete)
232 IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
233 static const WCHAR lbName[] = {'L','i','s','t','B','o','x',0};
235 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
236 This, (long)hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
238 if (This->options & ACO_AUTOSUGGEST) TRACE(" ACO_AUTOSUGGEST\n");
239 if (This->options & ACO_AUTOAPPEND) TRACE(" ACO_AUTOAPPEND\n");
240 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
241 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
242 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
243 if (This->options & ACO_UPDOWNKEYDROPSLIST) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
244 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
246 This->hwndEdit = hwndEdit;
248 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
249 TRACE("No IEnumString interface\n");
250 return E_NOINTERFACE;
253 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
254 SetWindowLongPtrW( hwndEdit, GWLP_USERDATA, (LONG_PTR)This);
256 if (This->options & ACO_AUTOSUGGEST) {
259 hwndParent = GetParent(This->hwndEdit);
261 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
262 This->hwndListBox = CreateWindowExW(0, lbName, NULL,
263 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
264 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
266 (HINSTANCE)GetWindowLongPtrW( hwndParent, GWLP_HINSTANCE ), NULL);
268 if (This->hwndListBox) {
269 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
270 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
274 if (pwzsRegKeyPath) {
276 WCHAR result[MAX_PATH];
282 /* pwszRegKeyPath contains the key as well as the value, so we split */
283 key = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
284 strcpyW(key, pwzsRegKeyPath);
285 value = strrchrW(key, '\\');
288 /* Now value contains the value and buffer the key */
289 res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
290 if (res != ERROR_SUCCESS) {
291 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
292 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);
294 if (res == ERROR_SUCCESS) {
295 res = RegQueryValueW(hKey, value, result, &len);
296 if (res == ERROR_SUCCESS) {
297 This->quickComplete = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
298 strcpyW(This->quickComplete, result);
302 HeapFree(GetProcessHeap(), 0, key);
305 if ((pwszQuickComplete) && (!This->quickComplete)) {
306 This->quickComplete = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
307 lstrcpyW(This->quickComplete, pwszQuickComplete);
313 /**************************************************************************
314 * IAutoComplete_fnVTable
316 static IAutoCompleteVtbl acvt =
318 IAutoComplete_fnQueryInterface,
319 IAutoComplete_fnAddRef,
320 IAutoComplete_fnRelease,
321 IAutoComplete_fnInit,
322 IAutoComplete_fnEnable,
325 /**************************************************************************
326 * AutoComplete2_QueryInterface
328 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
329 IAutoComplete2 * iface,
333 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
335 TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
337 return IAutoComplete_QueryInterface((IAutoComplete*)This, riid, ppvObj);
340 /******************************************************************************
341 * IAutoComplete2_fnAddRef
343 static ULONG WINAPI IAutoComplete2_fnAddRef(
344 IAutoComplete2 * iface)
346 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
348 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
350 return IAutoComplete2_AddRef((IAutoComplete*)This);
353 /******************************************************************************
354 * IAutoComplete2_fnRelease
356 static ULONG WINAPI IAutoComplete2_fnRelease(
357 IAutoComplete2 * iface)
359 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
361 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
363 return IAutoComplete_Release((IAutoComplete*)This);
366 /******************************************************************************
367 * IAutoComplete2_fnEnable
369 static HRESULT WINAPI IAutoComplete2_fnEnable(
370 IAutoComplete2 * iface,
373 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
375 TRACE ("(%p)->(%s)\n", This, (fEnable)?"true":"false");
377 return IAutoComplete_Enable((IAutoComplete*)This, fEnable);
380 /******************************************************************************
381 * IAutoComplete2_fnInit
383 static HRESULT WINAPI IAutoComplete2_fnInit(
384 IAutoComplete2 * iface,
387 LPCOLESTR pwzsRegKeyPath,
388 LPCOLESTR pwszQuickComplete)
390 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
392 TRACE("(%p)\n", This);
394 return IAutoComplete_Init((IAutoComplete*)This, hwndEdit, punkACL, pwzsRegKeyPath, pwszQuickComplete);
397 /**************************************************************************
398 * IAutoComplete_fnGetOptions
400 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
401 IAutoComplete2 * iface,
406 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
408 TRACE("(%p) -> (%p)\n", This, pdwFlag);
410 *pdwFlag = This->options;
415 /**************************************************************************
416 * IAutoComplete_fnSetOptions
418 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
419 IAutoComplete2 * iface,
424 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
426 TRACE("(%p) -> (0x%lx)\n", This, dwFlag);
428 This->options = dwFlag;
433 /**************************************************************************
434 * IAutoComplete2_fnVTable
436 static IAutoComplete2Vtbl ac2vt =
438 IAutoComplete2_fnQueryInterface,
439 IAutoComplete2_fnAddRef,
440 IAutoComplete2_fnRelease,
441 IAutoComplete2_fnInit,
442 IAutoComplete2_fnEnable,
444 IAutoComplete2_fnSetOptions,
445 IAutoComplete2_fnGetOptions,
449 Window procedure for autocompletion
451 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
453 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
459 BOOL control, filled, displayall = FALSE;
460 int cpt, height, sel;
462 if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
466 case CB_SHOWDROPDOWN:
467 ShowWindow(This->hwndListBox, SW_HIDE);
470 if ((This->options && ACO_AUTOSUGGEST) &&
471 ((HWND)wParam != This->hwndListBox))
473 ShowWindow(This->hwndListBox, SW_HIDE);
478 GetWindowTextW( hwnd, (LPWSTR)hwndText, 255);
482 /* If quickComplete is set and control is pressed, replace the string */
483 control = GetKeyState(VK_CONTROL) & 0x8000;
484 if (control && This->quickComplete) {
485 hwndQCText = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
486 (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
487 sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
488 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
489 SendMessageW(hwnd, EM_SETSEL, 0, sel);
490 HeapFree(GetProcessHeap(), 0, hwndQCText);
493 ShowWindow(This->hwndListBox, SW_HIDE);
501 - if the listbox is not visible, displays it
502 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
503 is present but does not select anything.
504 - if the listbox is visible, change the selection
506 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
507 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
509 /* We must dispays all the entries */
512 if (IsWindowVisible(This->hwndListBox)) {
515 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
516 /* Change the selection */
517 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
519 sel = ((sel-1)<0)?count-1:sel-1;
521 sel = ((sel+1)>= count)?-1:sel+1;
522 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
527 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
528 msg = (WCHAR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
529 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
530 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
531 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
532 HeapFree(GetProcessHeap(), 0, msg);
534 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
535 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
543 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
544 ShowWindow(This->hwndListBox, SW_HIDE);
545 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
547 if (This->options & ACO_AUTOAPPEND) {
549 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, (LPARAM)NULL);
551 hwndText[b-1] = '\0';
554 SetWindowTextW(hwnd, hwndText);
562 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
564 HeapFree(GetProcessHeap(), 0, This->txtbackup);
565 This->txtbackup = (WCHAR*) HeapAlloc(GetProcessHeap(),
566 HEAP_ZERO_MEMORY, (lstrlenW(hwndText)+1)*sizeof(WCHAR));
567 lstrcpyW(This->txtbackup, hwndText);
569 /* Returns if there is no text to search and we doesn't want to display all the entries */
570 if ((!displayall) && (! *hwndText) )
573 IEnumString_Reset(This->enumstr);
576 hr = IEnumString_Next(This->enumstr, 1, &strs, NULL);
580 if ((LPWSTR)strstrW(strs, hwndText) == strs) {
582 if (This->options & ACO_AUTOAPPEND) {
583 SetWindowTextW(hwnd, strs);
584 SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(strs));
588 if (This->options & ACO_AUTOSUGGEST) {
589 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
596 if (This->options & ACO_AUTOSUGGEST) {
598 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
599 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
600 GetWindowRect(hwnd, &r);
601 SetParent(This->hwndListBox, HWND_DESKTOP);
602 /* It seems that Windows XP displays 7 lines at most
603 and otherwise displays a vertical scroll bar */
604 SetWindowPos(This->hwndListBox, HWND_TOP,
605 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
608 ShowWindow(This->hwndListBox, SW_HIDE);
614 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
621 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
623 IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
629 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
630 SendMessageW(hwnd, LB_SETCURSEL, (WPARAM)sel, (LPARAM)0);
633 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
634 msg = (WCHAR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
635 sel = (INT)SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
636 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
637 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
638 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
639 ShowWindow(hwnd, SW_HIDE);
640 HeapFree(GetProcessHeap(), 0, msg);
643 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);