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