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