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