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