- Use Interlocked* instead of ++/-- in AddRef/Release.
[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 a 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 = (IAutoCompleteImpl*)HeapAlloc(GetProcessHeap(),
105       HEAP_ZERO_MEMORY, sizeof(IAutoCompleteImpl));
106     if (!lpac) 
107         return E_OUTOFMEMORY;
108
109     lpac->ref = 1;
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;
119     
120     if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac), riid, ppv))) {
121         IUnknown_Release (_IUnknown_ (lpac));
122         return E_NOINTERFACE;
123     }
124     
125     TRACE("-- (%p)->\n",lpac);
126
127     return S_OK;
128 }
129
130 /**************************************************************************
131  *  AutoComplete_QueryInterface
132  */
133 static HRESULT WINAPI IAutoComplete_fnQueryInterface(
134     IAutoComplete * iface,
135     REFIID riid,
136     LPVOID *ppvObj)
137 {
138     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
139     
140     TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
141     *ppvObj = NULL;
142
143     if(IsEqualIID(riid, &IID_IUnknown)) 
144     {
145         *ppvObj = This;
146     } 
147     else if(IsEqualIID(riid, &IID_IAutoComplete))
148     {
149         *ppvObj = (IAutoComplete*)This;
150     }
151     else if(IsEqualIID(riid, &IID_IAutoComplete2))
152     {
153         *ppvObj = _IAutoComplete2_ (This);
154     }
155
156     if (*ppvObj)
157     {
158         IAutoComplete_AddRef((IAutoComplete*)*ppvObj);
159         TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
160         return S_OK;
161     }
162     TRACE("-- Interface: E_NOINTERFACE\n");
163     return E_NOINTERFACE;       
164 }
165
166 /******************************************************************************
167  * IAutoComplete_fnAddRef
168  */
169 static ULONG WINAPI IAutoComplete_fnAddRef(
170         IAutoComplete * iface)
171 {
172     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
173     ULONG refCount = InterlockedIncrement(&This->ref);
174     
175     TRACE("(%p)->(%lu)\n", This, refCount - 1);
176
177     return refCount;
178 }
179
180 /******************************************************************************
181  * IAutoComplete_fnRelease
182  */
183 static ULONG WINAPI IAutoComplete_fnRelease(
184         IAutoComplete * iface)
185 {
186     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
187     ULONG refCount = InterlockedDecrement(&This->ref);
188     
189     TRACE("(%p)->(%lu)\n", This, refCount + 1);
190
191     if (!refCount) {
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);
197         if (This->enumstr)
198             IEnumString_Release(This->enumstr);
199         HeapFree(GetProcessHeap(), 0, This);
200     }
201     return refCount;
202 }
203
204 /******************************************************************************
205  * IAutoComplete_fnEnable
206  */
207 static HRESULT WINAPI IAutoComplete_fnEnable(
208     IAutoComplete * iface,
209     BOOL fEnable)
210 {
211     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
212
213     HRESULT hr = S_OK;
214
215     TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
216
217     This->enabled = fEnable;
218
219     return hr;
220 }
221
222 /******************************************************************************
223  * IAutoComplete_fnInit
224  */
225 static HRESULT WINAPI IAutoComplete_fnInit(
226     IAutoComplete * iface,
227     HWND hwndEdit,
228     IUnknown *punkACL,
229     LPCOLESTR pwzsRegKeyPath,
230     LPCOLESTR pwszQuickComplete)
231 {
232     IAutoCompleteImpl *This = (IAutoCompleteImpl *)iface;
233     static const WCHAR lbName[] = {'L','i','s','t','B','o','x',0};
234
235     TRACE("(%p)->(0x%08lx, %p, %s, %s)\n", 
236           This, (long)hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
237
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");
245
246     This->hwndEdit = hwndEdit;
247
248     if (!SUCCEEDED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
249         TRACE("No IEnumString interface\n");
250         return  E_NOINTERFACE;
251     }
252
253     This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW( hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
254     SetWindowLongPtrW( hwndEdit, GWLP_USERDATA, (LONG_PTR)This);
255
256     if (This->options & ACO_AUTOSUGGEST) {
257         HWND hwndParent;
258
259         hwndParent = GetParent(This->hwndEdit);
260         
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,
265                                             hwndParent, NULL, 
266                                             (HINSTANCE)GetWindowLongPtrW( hwndParent, GWLP_HINSTANCE ), NULL);
267                                             
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);
271         }
272     }
273
274     if (pwzsRegKeyPath) {
275         WCHAR *key;
276         WCHAR result[MAX_PATH];
277         WCHAR *value;
278         HKEY hKey = 0;
279         LONG res;
280         LONG len;
281
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, '\\');
286         *value = 0;
287         value++;
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);  
293         }
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);
299             }
300             RegCloseKey(hKey);
301         }
302         HeapFree(GetProcessHeap(), 0, key);
303     }
304
305     if ((pwszQuickComplete) && (!This->quickComplete)) {
306         This->quickComplete = (WCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszQuickComplete)+1)*sizeof(WCHAR));
307         lstrcpyW(This->quickComplete, pwszQuickComplete);
308     }
309
310     return S_OK;
311 }
312
313 /**************************************************************************
314  *  IAutoComplete_fnVTable
315  */
316 static IAutoCompleteVtbl acvt =
317 {
318     IAutoComplete_fnQueryInterface,
319     IAutoComplete_fnAddRef,
320     IAutoComplete_fnRelease,
321     IAutoComplete_fnInit,
322     IAutoComplete_fnEnable,
323 };
324
325 /**************************************************************************
326  *  AutoComplete2_QueryInterface
327  */
328 static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
329     IAutoComplete2 * iface,
330     REFIID riid,
331     LPVOID *ppvObj)
332 {
333     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
334
335     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
336
337     return IAutoComplete_QueryInterface((IAutoComplete*)This, riid, ppvObj);
338 }
339
340 /******************************************************************************
341  * IAutoComplete2_fnAddRef
342  */
343 static ULONG WINAPI IAutoComplete2_fnAddRef(
344         IAutoComplete2 * iface)
345 {
346     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
347
348     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
349
350     return IAutoComplete2_AddRef((IAutoComplete*)This);
351 }
352
353 /******************************************************************************
354  * IAutoComplete2_fnRelease
355  */
356 static ULONG WINAPI IAutoComplete2_fnRelease(
357         IAutoComplete2 * iface)
358 {
359     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl,iface);
360
361     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
362
363     return IAutoComplete_Release((IAutoComplete*)This);
364 }
365
366 /******************************************************************************
367  * IAutoComplete2_fnEnable
368  */
369 static HRESULT WINAPI IAutoComplete2_fnEnable(
370     IAutoComplete2 * iface,
371     BOOL fEnable)
372 {
373     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
374
375     TRACE ("(%p)->(%s)\n", This, (fEnable)?"true":"false");
376
377     return IAutoComplete_Enable((IAutoComplete*)This, fEnable);
378 }
379
380 /******************************************************************************
381  * IAutoComplete2_fnInit
382  */
383 static HRESULT WINAPI IAutoComplete2_fnInit(
384     IAutoComplete2 * iface,
385     HWND hwndEdit,
386     IUnknown *punkACL,
387     LPCOLESTR pwzsRegKeyPath,
388     LPCOLESTR pwszQuickComplete)
389 {
390     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
391
392     TRACE("(%p)\n", This);
393
394     return IAutoComplete_Init((IAutoComplete*)This, hwndEdit, punkACL, pwzsRegKeyPath, pwszQuickComplete);
395 }
396
397 /**************************************************************************
398  *  IAutoComplete_fnGetOptions
399  */
400 static HRESULT WINAPI IAutoComplete2_fnGetOptions(
401     IAutoComplete2 * iface,
402     DWORD *pdwFlag)
403 {
404     HRESULT hr = S_OK;
405
406     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
407
408     TRACE("(%p) -> (%p)\n", This, pdwFlag);
409
410     *pdwFlag = This->options;
411
412     return hr;
413 }
414
415 /**************************************************************************
416  *  IAutoComplete_fnSetOptions
417  */
418 static HRESULT WINAPI IAutoComplete2_fnSetOptions(
419     IAutoComplete2 * iface,
420     DWORD dwFlag)
421 {
422     HRESULT hr = S_OK;
423
424     _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl, iface);
425
426     TRACE("(%p) -> (0x%lx)\n", This, dwFlag);
427
428     This->options = dwFlag;
429
430     return hr;
431 }
432
433 /**************************************************************************
434  *  IAutoComplete2_fnVTable
435  */
436 static IAutoComplete2Vtbl ac2vt =
437 {
438     IAutoComplete2_fnQueryInterface,
439     IAutoComplete2_fnAddRef,
440     IAutoComplete2_fnRelease,
441     IAutoComplete2_fnInit,
442     IAutoComplete2_fnEnable,
443     /* IAutoComplete2 */
444     IAutoComplete2_fnSetOptions,
445     IAutoComplete2_fnGetOptions,
446 };
447
448 /*
449   Window procedure for autocompletion
450  */
451 static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
452 {
453     IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
454     LPOLESTR strs;
455     HRESULT hr;
456     WCHAR hwndText[255];
457     WCHAR *hwndQCText;
458     RECT r;
459     BOOL control, filled, displayall = FALSE;
460     int cpt, height, sel;
461
462     if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
463
464     switch (uMsg)
465     {
466         case CB_SHOWDROPDOWN:
467             ShowWindow(This->hwndListBox, SW_HIDE);
468             break;
469         case WM_KILLFOCUS:
470             if ((This->options && ACO_AUTOSUGGEST) && 
471                 ((HWND)wParam != This->hwndListBox))
472             {
473                 ShowWindow(This->hwndListBox, SW_HIDE);
474             }
475             break;
476         case WM_KEYUP:
477             
478             GetWindowTextW( hwnd, (LPWSTR)hwndText, 255);
479       
480             switch(wParam) {
481                 case VK_RETURN:
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);
491                     }
492
493                     ShowWindow(This->hwndListBox, SW_HIDE);
494                     return 0;
495                 case VK_LEFT:
496                 case VK_RIGHT:
497                     return 0;
498                 case VK_UP:           
499                 case VK_DOWN:
500                     /* Two cases here : 
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
505                     */
506                     if ( (This->options & (ACO_AUTOSUGGEST | ACO_UPDOWNKEYDROPSLIST)) 
507                          && (!IsWindowVisible(This->hwndListBox) && (! *hwndText)) )
508                     {
509                          /* We must dispays all the entries */
510                          displayall = TRUE;
511                     } else {
512                         if (IsWindowVisible(This->hwndListBox)) {
513                             int count;
514
515                             count = SendMessageW(This->hwndListBox, LB_GETCOUNT, 0, 0);
516                             /* Change the selection */
517                             sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
518                             if (wParam == VK_UP)
519                                 sel = ((sel-1)<0)?count-1:sel-1;
520                             else
521                                 sel = ((sel+1)>= count)?-1:sel+1;
522                             SendMessageW(This->hwndListBox, LB_SETCURSEL, sel, 0);
523                             if (sel != -1) {
524                                 WCHAR *msg;
525                                 int len;
526                                 
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);
533                             } else {
534                                 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)This->txtbackup);
535                                 SendMessageW(hwnd, EM_SETSEL, lstrlenW(This->txtbackup), lstrlenW(This->txtbackup));
536                             }                   
537                         }               
538                         return 0;
539                     }
540                     break;
541                 case VK_BACK:
542                 case VK_DELETE:
543                     if ((! *hwndText) && (This->options & ACO_AUTOSUGGEST)) {
544                         ShowWindow(This->hwndListBox, SW_HIDE);
545                         return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
546                     }
547                     if (This->options & ACO_AUTOAPPEND) {
548                         DWORD b;
549                         SendMessageW(hwnd, EM_GETSEL, (WPARAM)&b, (LPARAM)NULL);
550                         if (b>1) {
551                             hwndText[b-1] = '\0';
552                         } else {
553                             hwndText[0] = '\0';
554                             SetWindowTextW(hwnd, hwndText); 
555                         }                       
556                     }
557                     break;
558                 default:                    
559                     ;
560             }
561       
562             SendMessageW(This->hwndListBox, LB_RESETCONTENT, 0, 0);
563
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);
568
569             /* Returns if there is no text to search and we doesn't want to display all the entries */
570             if ((!displayall) && (! *hwndText) )
571                 break;
572             
573             IEnumString_Reset(This->enumstr);
574             filled = FALSE;
575             for(cpt = 0;;) {
576                 hr = IEnumString_Next(This->enumstr, 1, &strs, NULL);
577                 if (hr != S_OK)
578                     break;
579
580                 if ((LPWSTR)strstrW(strs, hwndText) == strs) {
581                     
582                     if (This->options & ACO_AUTOAPPEND) {
583                         SetWindowTextW(hwnd, strs);
584                         SendMessageW(hwnd, EM_SETSEL, lstrlenW(hwndText), lstrlenW(strs));
585                         break;
586                     }           
587
588                     if (This->options & ACO_AUTOSUGGEST) {
589                         SendMessageW(This->hwndListBox, LB_ADDSTRING, 0, (LPARAM)strs);
590                         filled = TRUE;
591                         cpt++;
592                     }
593                 }               
594             }
595             
596             if (This->options & ACO_AUTOSUGGEST) {
597                 if (filled) {
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)), 
606                                  SWP_SHOWWINDOW );
607                 } else {
608                     ShowWindow(This->hwndListBox, SW_HIDE);
609                 }
610             }
611             
612             break; 
613         default:
614             return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
615             
616     }
617
618     return 0;
619 }
620
621 static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
622 {
623     IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
624     WCHAR *msg;
625     int sel = -1, len;
626
627     switch (uMsg) {
628         case WM_MOUSEMOVE:
629             sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
630             SendMessageW(hwnd, LB_SETCURSEL, (WPARAM)sel, (LPARAM)0);
631             break;
632         case WM_LBUTTONDOWN:
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);
641             break;
642         default:
643             return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);
644     }
645     return 0;
646 }