shell32: autocomplete: Make SetOptions handle setting the ACO_AUTOSUGGEST after Init...
[wine] / dlls / shell32 / autocomplete.c
1 /*
2  *      AutoComplete interfaces implementation.
3  *
4  *      Copyright 2004  Maxime Bellengé <maxime.bellenge@laposte.net>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
22   Implemented:
23   - ACO_AUTOAPPEND style
24   - ACO_AUTOSUGGEST style
25   - ACO_UPDOWNKEYDROPSLIST style
26
27   - Handle pwzsRegKeyPath and pwszQuickComplete in Init
28
29   TODO:
30   - implement ACO_SEARCH style
31   - implement ACO_FILTERPREFIXES style
32   - implement ACO_USETAB style
33   - implement ACO_RTLREADING style
34   
35  */
36 #include "config.h"
37
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define COBJMACROS
43
44 #include "wine/debug.h"
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #include "undocshell.h"
49 #include "shlwapi.h"
50 #include "winerror.h"
51 #include "objbase.h"
52
53 #include "pidl.h"
54 #include "shlobj.h"
55 #include "shldisp.h"
56 #include "debughlp.h"
57
58 #include "wine/unicode.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(shell);
61
62 typedef struct
63 {
64     const IAutoComplete2Vtbl  *lpVtbl;
65     const IAutoCompleteDropDownVtbl *lpDropDownVtbl;
66     LONG ref;
67     BOOL  enabled;
68     HWND hwndEdit;
69     HWND hwndListBox;
70     WNDPROC wpOrigEditProc;
71     WNDPROC wpOrigLBoxProc;
72     WCHAR *txtbackup;
73     WCHAR *quickComplete;
74     IEnumString *enumstr;
75     AUTOCOMPLETEOPTIONS options;
76 } IAutoCompleteImpl;
77
78 static const IAutoComplete2Vtbl acvt;
79 static const IAutoCompleteDropDownVtbl acdropdownvt;
80
81
82 /*
83   converts This to an interface pointer
84 */
85 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
86 #define _IAutoComplete2_(This)  (IAutoComplete2*)&(This->lpVtbl)
87 #define _IAutoCompleteDropDown_(This)  (IAutoCompleteDropDown*)&(This->lpDropDownVtbl)
88
89 static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
90 {
91     return (IAutoCompleteImpl *)((char *)iface - FIELD_OFFSET(IAutoCompleteImpl, lpDropDownVtbl));
92 }
93
94 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
95 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
96
97 static void create_listbox(IAutoCompleteImpl *This)
98 {
99     HWND hwndParent;
100
101     hwndParent = GetParent(This->hwndEdit);
102
103     /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
104     This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
105                                     WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
106                                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
107                                     hwndParent, NULL,
108                                     (HINSTANCE)GetWindowLongPtrW( hwndParent, GWLP_HINSTANCE ), NULL);
109
110     if (This->hwndListBox) {
111         This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
112         SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
113     }
114 }
115
116 /**************************************************************************
117  *  IAutoComplete_Constructor
118  */
119 HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
120 {
121     IAutoCompleteImpl *lpac;
122
123     if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
124         return CLASS_E_NOAGGREGATION;
125
126     lpac = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
127     if (!lpac) 
128         return E_OUTOFMEMORY;
129
130     lpac->ref = 1;
131     lpac->lpVtbl = &acvt;
132     lpac->lpDropDownVtbl = &acdropdownvt;
133     lpac->enabled = TRUE;
134     lpac->enumstr = NULL;
135     lpac->options = ACO_AUTOAPPEND;
136     lpac->wpOrigEditProc = NULL;
137     lpac->hwndListBox = NULL;
138     lpac->txtbackup = NULL;
139     lpac->quickComplete = NULL;
140     
141     if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac), riid, ppv))) {
142         IUnknown_Release (_IUnknown_ (lpac));
143         return E_NOINTERFACE;
144     }
145     
146     TRACE("-- (%p)->\n",lpac);
147
148     return S_OK;
149 }
150
151 /**************************************************************************
152  *  AutoComplete_QueryInterface
153  */
154 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
155     IAutoComplete2 * iface,
156     REFIID riid,
157     LPVOID *ppvObj)
158 {
159     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
160     
161     TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
162     *ppvObj = NULL;
163
164     if (IsEqualIID(riid, &IID_IUnknown) ||
165         IsEqualIID(riid, &IID_IAutoComplete) ||
166         IsEqualIID(riid, &IID_IAutoComplete2))
167     {
168         *ppvObj = (IAutoComplete2*)This;
169     }
170     else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
171     {
172         *ppvObj = _IAutoCompleteDropDown_(This);
173     }
174
175     if (*ppvObj)
176     {
177         IUnknown_AddRef((IUnknown*)*ppvObj);
178         TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
179         return S_OK;
180     }
181     WARN("unsupported interface: %s\n", debugstr_guid(riid));
182     return E_NOINTERFACE;       
183 }
184
185 /******************************************************************************
186  * IAutoComplete2_fnAddRef
187  */
188 static ULONG WINAPI IAutoComplete2_fnAddRef(
189         IAutoComplete2 * iface)
190 {
191     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
192     ULONG refCount = InterlockedIncrement(&This->ref);
193     
194     TRACE("(%p)->(%u)\n", This, refCount - 1);
195
196     return refCount;
197 }
198
199 /******************************************************************************
200  * IAutoComplete2_fnRelease
201  */
202 static ULONG WINAPI IAutoComplete2_fnRelease(
203         IAutoComplete2 * iface)
204 {
205     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
206     ULONG refCount = InterlockedDecrement(&This->ref);
207     
208     TRACE("(%p)->(%u)\n", This, refCount + 1);
209
210     if (!refCount) {
211         TRACE(" destroying IAutoComplete(%p)\n",This);
212         HeapFree(GetProcessHeap(), 0, This->quickComplete);
213         HeapFree(GetProcessHeap(), 0, This->txtbackup);
214         if (This->hwndListBox)
215             DestroyWindow(This->hwndListBox);
216         if (This->enumstr)
217             IEnumString_Release(This->enumstr);
218         HeapFree(GetProcessHeap(), 0, This);
219     }
220     return refCount;
221 }
222
223 /******************************************************************************
224  * IAutoComplete2_fnEnable
225  */
226 static HRESULT WINAPI IAutoComplete2_fnEnable(
227     IAutoComplete2 * iface,
228     BOOL fEnable)
229 {
230     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
231
232     HRESULT hr = S_OK;
233
234     TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
235
236     This->enabled = fEnable;
237
238     return hr;
239 }
240
241 /******************************************************************************
242  * IAutoComplete2_fnInit
243  */
244 static HRESULT WINAPI IAutoComplete2_fnInit(
245     IAutoComplete2 * iface,
246     HWND hwndEdit,
247     IUnknown *punkACL,
248     LPCOLESTR pwzsRegKeyPath,
249     LPCOLESTR pwszQuickComplete)
250 {
251     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
252
253     TRACE("(%p)->(0x%08lx, %p, %s, %s)\n", 
254           This, (long)hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
255
256     if (This->options & ACO_AUTOSUGGEST) TRACE(" ACO_AUTOSUGGEST\n");
257     if (This->options & ACO_AUTOAPPEND) TRACE(" ACO_AUTOAPPEND\n");
258     if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
259     if (This->options & ACO_FILTERPREFIXES) FIXME(" ACO_FILTERPREFIXES not supported\n");
260     if (This->options & ACO_USETAB) FIXME(" ACO_USETAB not supported\n");
261     if (This->options & ACO_UPDOWNKEYDROPSLIST) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
262     if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
263
264     This->hwndEdit = hwndEdit;
265
266     if (!SUCCEEDED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
267         TRACE("No IEnumString interface\n");
268         return  E_NOINTERFACE;
269     }
270
271     This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
272     SetWindowLongPtrW( hwndEdit, GWLP_USERDATA, (LONG_PTR)This);
273
274     if (This->options & ACO_AUTOSUGGEST)
275         create_listbox(This);
276
277     if (pwzsRegKeyPath) {
278         WCHAR *key;
279         WCHAR result[MAX_PATH];
280         WCHAR *value;
281         HKEY hKey = 0;
282         LONG res;
283         LONG len;
284
285         /* pwszRegKeyPath contains the key as well as the value, so we split */
286         key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwzsRegKeyPath)+1)*sizeof(WCHAR));
287         strcpyW(key, pwzsRegKeyPath);
288         value = strrchrW(key, '\\');
289         *value = 0;
290         value++;
291         /* Now value contains the value and buffer the key */
292         res = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
293         if (res != ERROR_SUCCESS) {
294             /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
295             res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey);  
296         }
297         if (res == ERROR_SUCCESS) {
298             res = RegQueryValueW(hKey, value, result, &len);
299             if (res == ERROR_SUCCESS) {
300                 This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(WCHAR));
301                 strcpyW(This->quickComplete, result);
302             }
303             RegCloseKey(hKey);
304         }
305         HeapFree(GetProcessHeap(), 0, key);
306     }
307
308     if ((pwszQuickComplete) && (!This->quickComplete)) {
309         This->quickComplete = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
310         lstrcpyW(This->quickComplete, pwszQuickComplete);
311     }
312
313     return S_OK;
314 }
315
316 /**************************************************************************
317  *  IAutoComplete2_fnGetOptions
318  */
319 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
320     IAutoComplete2 * iface,
321     DWORD *pdwFlag)
322 {
323     HRESULT hr = S_OK;
324
325     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
326
327     TRACE("(%p) -> (%p)\n", This, pdwFlag);
328
329     *pdwFlag = This->options;
330
331     return hr;
332 }
333
334 /**************************************************************************
335  *  IAutoComplete2_fnSetOptions
336  */
337 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
338     IAutoComplete2 * iface,
339     DWORD dwFlag)
340 {
341     HRESULT hr = S_OK;
342
343     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
344
345     TRACE("(%p) -> (0x%x)\n", This, dwFlag);
346
347     This->options = dwFlag;
348
349     if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
350         create_listbox(This);
351
352     return hr;
353 }
354
355 /**************************************************************************
356  *  IAutoCompleteDropDown_fnGetDropDownStatus
357  */
358 static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
359     IAutoCompleteDropDown *iface,
360     DWORD *pdwFlags,
361     LPWSTR *ppwszString)
362 {
363     IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
364
365     FIXME("(%p) -> (%p, %p): stub\n", This, pdwFlags, ppwszString);
366
367     if (pdwFlags)
368         *pdwFlags = 0;
369     if (ppwszString)
370         *ppwszString = NULL;
371
372     return E_NOTIMPL;
373 }
374
375 /**************************************************************************
376  *  IAutoCompleteDropDown_fnResetEnumarator
377  */
378 static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
379     IAutoCompleteDropDown *iface)
380 {
381     IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
382
383     FIXME("(%p): stub\n", This);
384
385     return E_NOTIMPL;
386 }
387
388
389
390 /**************************************************************************
391  *  IAutoComplete2 VTable
392  */
393 static const IAutoComplete2Vtbl acvt =
394 {
395     IAutoComplete2_fnQueryInterface,
396     IAutoComplete2_fnAddRef,
397     IAutoComplete2_fnRelease,
398     IAutoComplete2_fnInit,
399     IAutoComplete2_fnEnable,
400     /* IAutoComplete2 */
401     IAutoComplete2_fnSetOptions,
402     IAutoComplete2_fnGetOptions,
403 };
404
405
406 static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
407             REFIID riid, LPVOID *ppvObj)
408 {
409     IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
410     return IAutoComplete2_fnQueryInterface(_IAutoComplete2_(This), riid, ppvObj);
411 }
412
413 static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
414 {
415     IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
416     return IAutoComplete2_fnAddRef(_IAutoComplete2_(This));
417 }
418
419 static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
420 {
421     IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
422     return IAutoComplete2_fnRelease(_IAutoComplete2_(This));
423 }
424
425 /**************************************************************************
426  *  IAutoCompleteDropDown VTable
427  */
428 static const IAutoCompleteDropDownVtbl acdropdownvt =
429 {
430     IAutoCompleteDropDown_fnQueryInterface,
431     IAutoCompleteDropDown_fnAddRef,
432     IAutoCompleteDropDown_fnRelease,
433     IAutoCompleteDropDown_fnGetDropDownStatus,
434     IAutoCompleteDropDown_fnResetEnumerator,
435 };
436
437 /*
438   Window procedure for autocompletion
439  */
440 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
441 {
442     IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
443     LPOLESTR strs;
444     HRESULT hr;
445     WCHAR hwndText[255];
446     WCHAR *hwndQCText;
447     RECT r;
448     BOOL control, filled, displayall = FALSE;
449     int cpt, height, sel;
450
451     if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
452
453     switch (uMsg)
454     {
455         case CB_SHOWDROPDOWN:
456             ShowWindow(This->hwndListBox, SW_HIDE);
457             break;
458         case WM_KILLFOCUS:
459             if ((This->options & ACO_AUTOSUGGEST) &&
460                 ((HWND)wParam != This->hwndListBox))
461             {
462                 ShowWindow(This->hwndListBox, SW_HIDE);
463             }
464             break;
465         case WM_KEYUP:
466             
467             GetWindowTextW( hwnd, (LPWSTR)hwndText, 255);
468       
469             switch(wParam) {
470                 case VK_RETURN:
471                     /* If quickComplete is set and control is pressed, replace the string */
472                     control = GetKeyState(VK_CONTROL) & 0x8000;             
473                     if (control && This->quickComplete) {
474                         hwndQCText = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
475                                                        (lstrlenW(This->quickComplete)+lstrlenW(hwndText))*sizeof(WCHAR));
476                         sel = sprintfW(hwndQCText, This->quickComplete, hwndText);
477                         SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)hwndQCText);
478                         SendMessageW(hwnd, EM_SETSEL, 0, sel);
479                         HeapFree(GetProcessHeap(), 0, hwndQCText);
480                     }
481
482                     ShowWindow(This->hwndListBox, SW_HIDE);
483                     return 0;
484                 case VK_LEFT:
485                 case VK_RIGHT:
486                     return 0;
487                 case VK_UP:           
488                 case VK_DOWN:
489                     /* Two cases here : 
490                        - if the listbox is not visible, displays it 
491                        with all the entries if the style ACO_UPDOWNKEYDROPSLIST
492                        is present but does not select anything.
493                        - if the listbox is visible, change the selection
494                     */
495                     if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST)) 
496                          && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
497                     {
498                          /* We must display all the entries */
499                          displayall = TRUE;
500                     } else {
501                         if (IsWindowVisible(This->hwndListBox)) {
502                             int count;
503
504                             count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
505                             /* Change the selection */
506                             sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
507                             if (wParam == VK_UP)
508                                 sel = ((sel-1)<0)?count-1:sel-1;
509                             else
510                                 sel = ((sel+1)>= count)?-1:sel+1;
511                             SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
512                             if (sel != -1) {
513                                 WCHAR *msg;
514                                 int len;
515                                 
516                                 len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, (LPARAM)NULL);
517                                 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
518                                 SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
519                                 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)msg);
520                                 SendMessageW(hwnd, EM_SETSEL, lstrlenW(msg), lstrlenW(msg));
521                                 HeapFree(GetProcessHeap(), 0, msg);
522                             } else {
523                                 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
524                                 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
525                             }                   
526                         }               
527                         return 0;
528                     }
529                     break;
530                 case VK_BACK:
531                 case VK_DELETE:
532                     if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
533                         ShowWindow(This->hwndListBox, SW_HIDE);
534                         return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
535                     }
536                     if (This->options & ACO_AUTOAPPEND) {
537                         DWORD b;
538                         SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, (LPARAM)NULL);
539                         if (b>1) {
540                             hwndText[b-1] = '\0';
541                         } else {
542                             hwndText[0] = '\0';
543                             SetWindowTextW(hwnd, hwndText); 
544                         }                       
545                     }
546                     break;
547                 default:                    
548                     ;
549             }
550       
551             SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
552
553             HeapFree(GetProcessHeap(), 0, This->txtbackup);
554             This->txtbackup = HeapAlloc(GetProcessHeap(),
555                                                  HEAP_ZERO_MEMORY, (lstrlenW(hwndText)+1)*sizeof(WCHAR));                                                             
556             lstrcpyW(This->txtbackup, hwndText);
557
558             /* Returns if there is no text to search and we doesn't want to display all the entries */
559             if ((!displayall) && (! *hwndText) )
560                 break;
561             
562             IEnumString_Reset(This->enumstr);
563             filled = FALSE;
564             for(cpt = 0;;) {
565                 ULONG fetched;
566                 hr = IEnumString_Next(This->enumstr, 1, &strs, &fetched);
567                 if (hr != S_OK)
568                     break;
569
570                 if (strstrW(strs, hwndText) == strs) {
571                     if (!filled && (This->options & ACO_AUTOAPPEND)) {
572                         SetWindowTextW(hwnd, strs);
573                         SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(strs));
574                         if (!(This->options & ACO_AUTOSUGGEST))
575                             break;
576                     }           
577
578                     if (This->options & ACO_AUTOSUGGEST) {
579                         SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
580                         cpt++;
581                     }
582
583                     filled = TRUE;
584                 }               
585             }
586             
587             if (This->options & ACO_AUTOSUGGEST) {
588                 if (filled) {
589                     height = SendMessageW(This->hwndListBox, LB_GETITEMHEIGHT, 0, 0);
590                     SendMessageW(This->hwndListBox, LB_CARETOFF, 0, 0);
591                     GetWindowRect(hwnd, &r);
592                     SetParent(This->hwndListBox, HWND_DESKTOP);
593                     /* It seems that Windows XP displays 7 lines at most 
594                        and otherwise displays a vertical scroll bar */
595                     SetWindowPos(This->hwndListBox, HWND_TOP, 
596                                  r.left, r.bottom + 1, r.right - r.left, min(height * 7, height*(cpt+1)), 
597                                  SWP_SHOWWINDOW );
598                 } else {
599                     ShowWindow(This->hwndListBox, SW_HIDE);
600                 }
601             }
602             
603             break; 
604         default:
605             return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
606             
607     }
608
609     return 0;
610 }
611
612 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
613 {
614     IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
615     WCHAR *msg;
616     int sel, len;
617
618     switch (uMsg) {
619         case WM_MOUSEMOVE:
620             sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
621             SendMessageW(hwnd, LB_SETCURSEL, (WPARAM)sel, (LPARAM)0);
622             break;
623         case WM_LBUTTONDOWN:
624             sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
625             if (sel < 0)
626                 break;
627             len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
628             msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (len+1)*sizeof(WCHAR));
629             SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
630             SendMessageW(This->hwndEdit, WM_SETTEXT, 0, (LPARAM)msg);
631             SendMessageW(This->hwndEdit, EM_SETSEL, 0, lstrlenW(msg));
632             ShowWindow(hwnd, SW_HIDE);
633             HeapFree(GetProcessHeap(), 0, msg);
634             break;
635         default:
636             return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);
637     }
638     return 0;
639 }