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
28 - implement ACO_SEARCH style
29 - implement ACO_FILTERPREFIXES style
30 - implement ACO_USETAB style
31 - implement ACO_RTLREADING style
33 - Handle pwzsRegKeyPath and pwszQuickComplete in Init.
40 #include "wine/debug.h"
44 #include "undocshell.h"
55 #include "wine/unicode.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(shell);
61 ICOM_VFIELD(IAutoComplete);
62 ICOM_VTABLE (IAutoComplete2) * lpvtblAutoComplete2;
67 WNDPROC wpOrigEditProc;
68 WNDPROC wpOrigLBoxProc;
71 AUTOCOMPLETEOPTIONS options;
74 static struct ICOM_VTABLE(IAutoComplete) acvt;
75 static struct ICOM_VTABLE(IAutoComplete2) ac2vt;
77 #define _IAutoComplete2_Offset ((int)(&(((IAutoCompleteImpl*)0)->lpvtblAutoComplete2)))
78 #define _ICOM_THIS_From_IAutoComplete2(class, name) class* This = (class*)(((char*)name)-_IAutoComplete2_Offset);
81 converts This to a interface pointer
83 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
84 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
86 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
87 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
89 /**************************************************************************
90 * IAutoComplete_Constructor
92 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
94 IAutoCompleteImpl *lpac;
96 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
97 return CLASS_E_NOAGGREGATION;
99 lpac = (IAutoCompleteImpl*)HeapAlloc(GetProcessHeap(),
100 HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
102 return E_OUTOFMEMORY;
105 lpac->lpVtbl = &acvt;
106 lpac->lpvtblAutoComplete2 = &ac2vt;
108 lpac->enumstr = NULL;
109 lpac->options = ACO_AUTOAPPEND;
110 lpac->wpOrigEditProc = NULL;
111 lpac->hwndListBox = NULL;
112 lpac->txtbackup = NULL;
114 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac), riid, ppv))) {
115 IUnknown_Release (_IUnknown_ (lpac));
116 return E_NOINTERFACE;
119 TRACE("-- (%p)->\n",lpac);
124 /**************************************************************************
125 * AutoComplete_QueryInterface
127 static HRESULT WINAPI IAutoComplete_fnQueryInterface(
128 IAutoComplete * iface,
132 ICOM_THIS(IAutoCompleteImpl, iface);
134 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
137 if(IsEqualIID(riid, &IID_IUnknown))
141 else if(IsEqualIID(riid, &IID_IAutoComplete))
143 *ppvObj = (IAutoComplete*)This;
145 else if(IsEqualIID(riid, &IID_IAutoComplete2))
147 *ppvObj = _IAutoComplete2_ (This);
152 IAutoComplete_AddRef((IAutoComplete*)*ppvObj);
153 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
156 TRACE("-- Interface: E_NOINTERFACE\n");
157 return E_NOINTERFACE;
160 /******************************************************************************
161 * IAutoComplete_fnAddRef
163 static ULONG WINAPI IAutoComplete_fnAddRef(
164 IAutoComplete * iface)
166 ICOM_THIS(IAutoCompleteImpl,iface);
168 TRACE("(%p)->(%lu)\n",This,This->ref);
169 return ++(This->ref);
172 /******************************************************************************
173 * IAutoComplete_fnRelease
175 static ULONG WINAPI IAutoComplete_fnRelease(
176 IAutoComplete * iface)
178 ICOM_THIS(IAutoCompleteImpl,iface);
180 TRACE("(%p)->(%lu)\n",This,This->ref);
182 if (!--(This->ref)) {
183 TRACE(" destroying IAutoComplete(%p)\n",This);
185 HeapFree(GetProcessHeap(), 0, This->txtbackup);
186 if (This->hwndListBox)
187 DestroyWindow(This->hwndListBox);
189 IEnumString_Release(This->enumstr);
190 HeapFree(GetProcessHeap(), 0, This);
196 /******************************************************************************
197 * IAutoComplete_fnEnable
199 static HRESULT WINAPI IAutoComplete_fnEnable(
200 IAutoComplete * iface,
203 ICOM_THIS(IAutoCompleteImpl, iface);
207 TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
209 This->enable = fEnable;
214 /******************************************************************************
215 * IAutoComplete_fnInit
217 static HRESULT WINAPI IAutoComplete_fnInit(
218 IAutoComplete * iface,
221 LPCOLESTR pwzsRegKeyPath,
222 LPCOLESTR pwszQuickComplete)
224 ICOM_THIS(IAutoCompleteImpl, iface);
225 static const WCHAR lbName[] = {'L','i','s','t','B','o','x',0};
227 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
228 This, (long)hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
230 if (This->options & ACO_AUTOSUGGEST) TRACE(" ACO_AUTOSUGGEST\n");
231 if (This->options & ACO_AUTOAPPEND) TRACE(" ACO_AUTOAPPEND\n");
232 if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
233 if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
234 if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
235 if (This->options & ACO_UPDOWNKEYDROPSLIST) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
236 if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
238 This->hwndEdit = hwndEdit;
240 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
241 TRACE("No IEnumString interface\n");
242 return E_NOINTERFACE;
245 This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
246 SetWindowLongPtrW( hwndEdit, GWLP_USERDATA, (LONG_PTR)This);
248 if (This->options & ACO_AUTOSUGGEST) {
250 TRACE("Creating ListBox\n");
252 hwndParent = GetParent(This->hwndEdit);
254 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
255 This->hwndListBox = CreateWindowExW(0, lbName, NULL,
256 WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
257 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
259 (HINSTANCE)GetWindowLongA( hwndParent, GWL_HINSTANCE ), NULL);
261 if (!This->hwndListBox) {
262 TRACE("Problem creating ListBox\n");
266 This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
267 SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
274 /**************************************************************************
275 * IAutoComplete_fnVTable
277 static ICOM_VTABLE (IAutoComplete) acvt =
279 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
280 IAutoComplete_fnQueryInterface,
281 IAutoComplete_fnAddRef,
282 IAutoComplete_fnRelease,
283 IAutoComplete_fnInit,
284 IAutoComplete_fnEnable,
287 /**************************************************************************
288 * AutoComplete2_QueryInterface
290 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
291 IAutoComplete2 * iface,
295 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
297 TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
299 return IAutoComplete_QueryInterface((IAutoComplete*)This, riid, ppvObj);
302 /******************************************************************************
303 * IAutoComplete2_fnAddRef
305 static ULONG WINAPI IAutoComplete2_fnAddRef(
306 IAutoComplete2 * iface)
308 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
310 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
312 return IAutoComplete2_AddRef((IAutoComplete*)This);
315 /******************************************************************************
316 * IAutoComplete2_fnRelease
318 static ULONG WINAPI IAutoComplete2_fnRelease(
319 IAutoComplete2 * iface)
321 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
323 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
325 return IAutoComplete_Release((IAutoComplete*)This);
328 /******************************************************************************
329 * IAutoComplete2_fnEnable
331 static HRESULT WINAPI IAutoComplete2_fnEnable(
332 IAutoComplete2 * iface,
335 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
337 TRACE ("(%p)->(%s)\n", This, (fEnable)?"true":"false");
339 return IAutoComplete_Enable((IAutoComplete*)This, fEnable);
342 /******************************************************************************
343 * IAutoComplete2_fnInit
345 static HRESULT WINAPI IAutoComplete2_fnInit(
346 IAutoComplete2 * iface,
349 LPCOLESTR pwzsRegKeyPath,
350 LPCOLESTR pwszQuickComplete)
352 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
354 TRACE("(%p)\n", This);
356 return IAutoComplete_Init((IAutoComplete*)This, hwndEdit, punkACL, pwzsRegKeyPath, pwszQuickComplete);
359 /**************************************************************************
360 * IAutoComplete_fnGetOptions
362 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
363 IAutoComplete2 * iface,
367 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
369 TRACE("(%p) -> (%p)\n", This, pdwFlag);
371 *pdwFlag = This->options;
376 /**************************************************************************
377 * IAutoComplete_fnSetOptions
379 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
380 IAutoComplete2 * iface,
385 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
387 TRACE("(%p) -> (0x%lx)\n", This, dwFlag);
389 This->options = dwFlag;
394 /**************************************************************************
395 * IAutoComplete2_fnVTable
397 static ICOM_VTABLE (IAutoComplete2) ac2vt =
399 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
400 IAutoComplete2_fnQueryInterface,
401 IAutoComplete2_fnAddRef,
402 IAutoComplete2_fnRelease,
403 IAutoComplete2_fnInit,
404 IAutoComplete2_fnEnable,
406 IAutoComplete2_fnSetOptions,
407 IAutoComplete2_fnGetOptions,
411 Window procedure for autocompletion
413 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
415 ICOM_THIS(IAutoCompleteImpl, GetWindowLongPtrW(hwnd, GWLP_USERDATA));
420 BOOL filled, displayall = FALSE;
421 int cpt, height, sel;
425 case CB_SHOWDROPDOWN:
426 ShowWindow(This->hwndListBox, SW_HIDE);
427 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
430 if ((This->options && ACO_AUTOSUGGEST) &&
431 ((HWND)wParam != This->hwndListBox))
433 ShowWindow(This->hwndListBox, SW_HIDE);
434 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
439 GetWindowTextW( hwnd, (LPWSTR)hwndText, 255);
445 - if the listbox is not visible, displays it
446 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
447 is present but does not select anything.
448 - if the listbox is visible, change the selection
450 if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST))
451 && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
453 /* We must dispays all the entries */
458 count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
459 /* Change the selection */
460 sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
462 sel = ((sel-1)<0)?count-1:sel-1;
464 sel = ((sel+1)>= count)?-1:sel+1;
465 SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
470 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
471 msg = (WCHAR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
472 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
473 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
474 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
475 HeapFree(GetProcessHeap(), 0, msg);
477 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
478 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
487 if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
488 ShowWindow(This->hwndListBox, SW_HIDE);
489 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
490 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
492 if (This->options & ACO_AUTOAPPEND) {
494 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, (LPARAM)NULL);
496 hwndText[b-1] = '\0';
499 SetWindowTextW(hwnd, hwndText);
507 SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
509 HeapFree(GetProcessHeap(), 0, This->txtbackup);
510 This->txtbackup = (WCHAR*) HeapAlloc(GetProcessHeap(),
511 HEAP_ZERO_MEMORY, lstrlenW(hwndText)*sizeof(WCHAR));
512 lstrcpyW(This->txtbackup, hwndText);
514 /* Returns if there is no text to search and we doesn't want to display all the entries */
515 if ((!displayall) && (! *hwndText) )
518 IEnumString_Reset(This->enumstr);
521 hr = IEnumString_Next(This->enumstr, 1, &strs, NULL);
525 if ((LPWSTR)strstrW(strs, hwndText) == strs) {
527 if (This->options & ACO_AUTOAPPEND) {
528 SetWindowTextW(hwnd, strs);
529 SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(strs));
533 if (This->options & ACO_AUTOSUGGEST) {
534 SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
541 if (This->options & ACO_AUTOSUGGEST) {
543 height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
544 SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
545 GetWindowRect(hwnd, &r);
546 SetParent(This->hwndListBox, HWND_DESKTOP);
547 /* It seems that Windows XP displays 7 lines at most
548 and otherwise displays a vertical scroll bar */
549 SetWindowPos(This->hwndListBox, HWND_TOP,
550 r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)),
553 ShowWindow(This->hwndListBox, SW_HIDE);
559 return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
566 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
568 ICOM_THIS(IAutoCompleteImpl, GetWindowLongPtrW(hwnd, GWLP_USERDATA));
574 sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
575 SendMessageW(hwnd, LB_SETCURSEL, (WPARAM)sel, (LPARAM)0);
578 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
579 msg = (WCHAR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
580 sel = (INT)SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
581 SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
582 SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
583 SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
584 ShowWindow(hwnd, SW_HIDE);
585 HeapFree(GetProcessHeap(), 0, msg);
588 return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);