2 * AutoComplete interfaces implementation.
4 * Copyright 2004 Maxime Bellengé <maxime.bellenge@laposte.net>
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.
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.
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
23 - ACO_AUTOAPPEND style
24 - ACO_AUTOSUGGEST style
25 - ACO_UPDOWNKEYDROPSLIST style
27 - Handle pwzsRegKeyPath and pwszQuickComplete in Init
30 - implement ACO_SEARCH style
31 - implement ACO_FILTERPREFIXES style
32 - implement ACO_USETAB style
33 - implement ACO_RTLREADING style
40 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
44 const IAutoCompleteVtbl
*lpVtbl
;
45 const IAutoComplete2Vtbl
*lpvtblAutoComplete2
;
50 WNDPROC wpOrigEditProc
;
51 WNDPROC wpOrigLBoxProc
;
55 AUTOCOMPLETEOPTIONS options
;
58 static const IAutoCompleteVtbl acvt
;
59 static const IAutoComplete2Vtbl ac2vt
;
61 static IAutoCompleteImpl
* impl_from_IAutoComplete2( IAutoComplete2
*iface
)
63 return (IAutoCompleteImpl
*)((char*)iface
- FIELD_OFFSET(IAutoCompleteImpl
, lpvtblAutoComplete2
));
68 converts This to an interface pointer
70 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
71 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
73 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
74 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
76 /**************************************************************************
77 * IAutoComplete_Constructor
79 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
81 IAutoCompleteImpl
*lpac
;
83 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
84 return CLASS_E_NOAGGREGATION
;
86 lpac
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
92 lpac
->lpvtblAutoComplete2
= &ac2vt
;
95 lpac
->options
= ACO_AUTOAPPEND
;
96 lpac
->wpOrigEditProc
= NULL
;
97 lpac
->hwndListBox
= NULL
;
98 lpac
->txtbackup
= NULL
;
99 lpac
->quickComplete
= NULL
;
101 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac
), riid
, ppv
))) {
102 IUnknown_Release (_IUnknown_ (lpac
));
103 return E_NOINTERFACE
;
106 TRACE("-- (%p)->\n",lpac
);
111 /**************************************************************************
112 * AutoComplete_QueryInterface
114 static HRESULT WINAPI
IAutoComplete_fnQueryInterface(
115 IAutoComplete
* iface
,
119 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
121 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
124 if(IsEqualIID(riid
, &IID_IUnknown
))
128 else if(IsEqualIID(riid
, &IID_IAutoComplete
))
130 *ppvObj
= (IAutoComplete
*)This
;
132 else if(IsEqualIID(riid
, &IID_IAutoComplete2
))
134 *ppvObj
= _IAutoComplete2_ (This
);
139 IAutoComplete_AddRef((IAutoComplete
*)*ppvObj
);
140 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
143 TRACE("-- Interface: E_NOINTERFACE\n");
144 return E_NOINTERFACE
;
147 /******************************************************************************
148 * IAutoComplete_fnAddRef
150 static ULONG WINAPI
IAutoComplete_fnAddRef(
151 IAutoComplete
* iface
)
153 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
154 ULONG refCount
= InterlockedIncrement(&This
->ref
);
156 TRACE("(%p)->(%u)\n", This
, refCount
- 1);
161 /******************************************************************************
162 * IAutoComplete_fnRelease
164 static ULONG WINAPI
IAutoComplete_fnRelease(
165 IAutoComplete
* iface
)
167 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
168 ULONG refCount
= InterlockedDecrement(&This
->ref
);
170 TRACE("(%p)->(%u)\n", This
, refCount
+ 1);
173 TRACE(" destroying IAutoComplete(%p)\n",This
);
174 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
175 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
176 if (This
->hwndListBox
)
177 DestroyWindow(This
->hwndListBox
);
179 IEnumString_Release(This
->enumstr
);
180 HeapFree(GetProcessHeap(), 0, This
);
185 /******************************************************************************
186 * IAutoComplete_fnEnable
188 static HRESULT WINAPI
IAutoComplete_fnEnable(
189 IAutoComplete
* iface
,
192 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
196 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
198 This
->enabled
= fEnable
;
203 /******************************************************************************
204 * IAutoComplete_fnInit
206 static HRESULT WINAPI
IAutoComplete_fnInit(
207 IAutoComplete
* iface
,
210 LPCOLESTR pwzsRegKeyPath
,
211 LPCOLESTR pwszQuickComplete
)
213 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
214 static const WCHAR lbName
[] = {'L','i','s','t','B','o','x',0};
216 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
217 This
, (long)hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
219 if (This
->options
& ACO_AUTOSUGGEST
) TRACE(" ACO_AUTOSUGGEST\n");
220 if (This
->options
& ACO_AUTOAPPEND
) TRACE(" ACO_AUTOAPPEND\n");
221 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
222 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
223 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
224 if (This
->options
& ACO_UPDOWNKEYDROPSLIST
) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
225 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
227 This
->hwndEdit
= hwndEdit
;
229 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
230 TRACE("No IEnumString interface\n");
231 return E_NOINTERFACE
;
234 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
235 SetWindowLongPtrW( hwndEdit
, GWLP_USERDATA
, (LONG_PTR
)This
);
237 if (This
->options
& ACO_AUTOSUGGEST
) {
240 hwndParent
= GetParent(This
->hwndEdit
);
242 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
243 This
->hwndListBox
= CreateWindowExW(0, lbName
, NULL
,
244 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
245 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
247 (HINSTANCE
)GetWindowLongPtrW( hwndParent
, GWLP_HINSTANCE
), NULL
);
249 if (This
->hwndListBox
) {
250 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
251 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
255 if (pwzsRegKeyPath
) {
257 WCHAR result
[MAX_PATH
];
263 /* pwszRegKeyPath contains the key as well as the value, so we split */
264 key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (wcslen(pwzsRegKeyPath
)+1)*sizeof(WCHAR
));
265 wcscpy(key
, pwzsRegKeyPath
);
266 value
= strrchrW(key
, '\\');
269 /* Now value contains the value and buffer the key */
270 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
271 if (res
!= ERROR_SUCCESS
) {
272 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
273 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
275 if (res
== ERROR_SUCCESS
) {
276 res
= RegQueryValueW(hKey
, value
, result
, &len
);
277 if (res
== ERROR_SUCCESS
) {
278 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
279 wcscpy(This
->quickComplete
, result
);
283 HeapFree(GetProcessHeap(), 0, key
);
286 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
287 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (wcslen(pwszQuickComplete
)+1)*sizeof(WCHAR
));
288 wcscpy(This
->quickComplete
, pwszQuickComplete
);
294 /**************************************************************************
295 * IAutoComplete_fnVTable
297 static const IAutoCompleteVtbl acvt
=
299 IAutoComplete_fnQueryInterface
,
300 IAutoComplete_fnAddRef
,
301 IAutoComplete_fnRelease
,
302 IAutoComplete_fnInit
,
303 IAutoComplete_fnEnable
,
306 /**************************************************************************
307 * AutoComplete2_QueryInterface
309 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
310 IAutoComplete2
* iface
,
314 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
316 TRACE ("(%p)->(%s,%p)\n", This
, shdebugstr_guid (riid
), ppvObj
);
318 return IAutoComplete_QueryInterface((IAutoComplete
*)This
, riid
, ppvObj
);
321 /******************************************************************************
322 * IAutoComplete2_fnAddRef
324 static ULONG WINAPI
IAutoComplete2_fnAddRef(
325 IAutoComplete2
* iface
)
327 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
329 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
331 return IAutoComplete2_AddRef((IAutoComplete
*)This
);
334 /******************************************************************************
335 * IAutoComplete2_fnRelease
337 static ULONG WINAPI
IAutoComplete2_fnRelease(
338 IAutoComplete2
* iface
)
340 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
342 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
344 return IAutoComplete_Release((IAutoComplete
*)This
);
347 /******************************************************************************
348 * IAutoComplete2_fnEnable
350 static HRESULT WINAPI
IAutoComplete2_fnEnable(
351 IAutoComplete2
* iface
,
354 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
356 TRACE ("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
358 return IAutoComplete_Enable((IAutoComplete
*)This
, fEnable
);
361 /******************************************************************************
362 * IAutoComplete2_fnInit
364 static HRESULT WINAPI
IAutoComplete2_fnInit(
365 IAutoComplete2
* iface
,
368 LPCOLESTR pwzsRegKeyPath
,
369 LPCOLESTR pwszQuickComplete
)
371 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
373 TRACE("(%p)\n", This
);
375 return IAutoComplete_Init((IAutoComplete
*)This
, hwndEdit
, punkACL
, pwzsRegKeyPath
, pwszQuickComplete
);
378 /**************************************************************************
379 * IAutoComplete_fnGetOptions
381 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
382 IAutoComplete2
* iface
,
387 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
389 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
391 *pdwFlag
= This
->options
;
396 /**************************************************************************
397 * IAutoComplete_fnSetOptions
399 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
400 IAutoComplete2
* iface
,
405 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
407 TRACE("(%p) -> (0x%x)\n", This
, dwFlag
);
409 This
->options
= dwFlag
;
414 /**************************************************************************
415 * IAutoComplete2_fnVTable
417 static const IAutoComplete2Vtbl ac2vt
=
419 IAutoComplete2_fnQueryInterface
,
420 IAutoComplete2_fnAddRef
,
421 IAutoComplete2_fnRelease
,
422 IAutoComplete2_fnInit
,
423 IAutoComplete2_fnEnable
,
425 IAutoComplete2_fnSetOptions
,
426 IAutoComplete2_fnGetOptions
,
430 Window procedure for autocompletion
432 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
434 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
440 BOOL control
, filled
, displayall
= FALSE
;
441 int cpt
, height
, sel
;
443 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
447 case CB_SHOWDROPDOWN
:
448 ShowWindow(This
->hwndListBox
, SW_HIDE
);
451 if ((This
->options
&& ACO_AUTOSUGGEST
) &&
452 ((HWND
)wParam
!= This
->hwndListBox
))
454 ShowWindow(This
->hwndListBox
, SW_HIDE
);
459 GetWindowTextW( hwnd
, (LPWSTR
)hwndText
, 255);
463 /* If quickComplete is set and control is pressed, replace the string */
464 control
= GetKeyState(VK_CONTROL
) & 0x8000;
465 if (control
&& This
->quickComplete
) {
466 hwndQCText
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
467 (wcslen(This
->quickComplete
)+wcslen(hwndText
))*sizeof(WCHAR
));
468 sel
= swprintf(hwndQCText
, This
->quickComplete
, hwndText
);
469 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
470 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
471 HeapFree(GetProcessHeap(), 0, hwndQCText
);
474 ShowWindow(This
->hwndListBox
, SW_HIDE
);
482 - if the listbox is not visible, displays it
483 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
484 is present but does not select anything.
485 - if the listbox is visible, change the selection
487 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
488 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
490 /* We must display all the entries */
493 if (IsWindowVisible(This
->hwndListBox
)) {
496 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
497 /* Change the selection */
498 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
500 sel
= ((sel
-1)<0)?count
-1:sel
-1;
502 sel
= ((sel
+1)>= count
)?-1:sel
+1;
503 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
508 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
509 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
510 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
511 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
512 SendMessageW(hwnd
, EM_SETSEL
, wcslen(msg
), wcslen(msg
));
513 HeapFree(GetProcessHeap(), 0, msg
);
515 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
516 SendMessageW(hwnd
, EM_SETSEL
, wcslen(This
->txtbackup
), wcslen(This
->txtbackup
));
524 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
525 ShowWindow(This
->hwndListBox
, SW_HIDE
);
526 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
528 if (This
->options
& ACO_AUTOAPPEND
) {
530 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
532 hwndText
[b
-1] = '\0';
535 SetWindowTextW(hwnd
, hwndText
);
543 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
545 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
546 This
->txtbackup
= HeapAlloc(GetProcessHeap(),
547 HEAP_ZERO_MEMORY
, (wcslen(hwndText
)+1)*sizeof(WCHAR
));
548 wcscpy(This
->txtbackup
, hwndText
);
550 /* Returns if there is no text to search and we doesn't want to display all the entries */
551 if ((!displayall
) && (! *hwndText
) )
554 IEnumString_Reset(This
->enumstr
);
557 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, NULL
);
561 if ((LPWSTR
)strstrW(strs
, hwndText
) == strs
) {
563 if (This
->options
& ACO_AUTOAPPEND
) {
564 SetWindowTextW(hwnd
, strs
);
565 SendMessageW(hwnd
, EM_SETSEL
, wcslen(hwndText
), wcslen(strs
));
569 if (This
->options
& ACO_AUTOSUGGEST
) {
570 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
577 if (This
->options
& ACO_AUTOSUGGEST
) {
579 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
580 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
581 GetWindowRect(hwnd
, &r
);
582 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
583 /* It seems that Windows XP displays 7 lines at most
584 and otherwise displays a vertical scroll bar */
585 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
586 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
589 ShowWindow(This
->hwndListBox
, SW_HIDE
);
595 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
602 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
604 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
610 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
611 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
614 sel
= SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
617 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
618 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
619 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
620 SendMessageW(This
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
621 SendMessageW(This
->hwndEdit
, EM_SETSEL
, 0, wcslen(msg
));
622 ShowWindow(hwnd
, SW_HIDE
);
623 HeapFree(GetProcessHeap(), 0, msg
);
626 return CallWindowProcW(This
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);