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
34 - implement ResetEnumerator
35 - string compares should be case-insensitive, the content of the list should be sorted
46 #include "wine/debug.h"
50 #include "undocshell.h"
59 #include "shell32_main.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
67 IAutoComplete2 IAutoComplete2_iface
;
68 IAutoCompleteDropDown IAutoCompleteDropDown_iface
;
74 WNDPROC wpOrigEditProc
;
75 WNDPROC wpOrigLBoxProc
;
79 AUTOCOMPLETEOPTIONS options
;
82 static const WCHAR autocomplete_propertyW
[] = {'W','i','n','e',' ','A','u','t','o',
83 'c','o','m','p','l','e','t','e',' ',
84 'c','o','n','t','r','o','l',0};
86 static inline IAutoCompleteImpl
*impl_from_IAutoComplete2(IAutoComplete2
*iface
)
88 return CONTAINING_RECORD(iface
, IAutoCompleteImpl
, IAutoComplete2_iface
);
91 static inline IAutoCompleteImpl
*impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown
*iface
)
93 return CONTAINING_RECORD(iface
, IAutoCompleteImpl
, IAutoCompleteDropDown_iface
);
97 Window procedure for autocompletion
99 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
101 IAutoCompleteImpl
*This
= GetPropW(hwnd
, autocomplete_propertyW
);
106 BOOL control
, filled
, displayall
= FALSE
;
107 int cpt
, height
, sel
;
109 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
113 case CB_SHOWDROPDOWN
:
114 ShowWindow(This
->hwndListBox
, SW_HIDE
);
117 if ((This
->options
& ACO_AUTOSUGGEST
) && ((HWND
)wParam
!= This
->hwndListBox
))
119 ShowWindow(This
->hwndListBox
, SW_HIDE
);
121 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
126 GetWindowTextW( hwnd
, hwndText
, sizeof(hwndText
)/sizeof(WCHAR
));
130 /* If quickComplete is set and control is pressed, replace the string */
131 control
= GetKeyState(VK_CONTROL
) & 0x8000;
132 if (control
&& This
->quickComplete
) {
133 hwndQCText
= HeapAlloc(GetProcessHeap(), 0,
134 (lstrlenW(This
->quickComplete
)+lstrlenW(hwndText
))*sizeof(WCHAR
));
135 sel
= sprintfW(hwndQCText
, This
->quickComplete
, hwndText
);
136 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
137 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
138 HeapFree(GetProcessHeap(), 0, hwndQCText
);
141 ShowWindow(This
->hwndListBox
, SW_HIDE
);
149 - if the listbox is not visible, displays it
150 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
151 is present but does not select anything.
152 - if the listbox is visible, change the selection
154 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
155 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
157 /* We must display all the entries */
160 if (IsWindowVisible(This
->hwndListBox
)) {
163 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
164 /* Change the selection */
165 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
167 sel
= ((sel
-1) < 0) ? count
-1 : sel
-1;
169 sel
= ((sel
+1) >= count
) ? -1 : sel
+1;
170 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
175 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
176 msg
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
177 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
178 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
179 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(msg
), lstrlenW(msg
));
180 HeapFree(GetProcessHeap(), 0, msg
);
182 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
183 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(This
->txtbackup
), lstrlenW(This
->txtbackup
));
191 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
192 ShowWindow(This
->hwndListBox
, SW_HIDE
);
193 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
195 if (This
->options
& ACO_AUTOAPPEND
) {
197 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, 0);
199 hwndText
[b
-1] = '\0';
202 SetWindowTextW(hwnd
, hwndText
);
210 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
212 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
213 len
= strlenW(hwndText
);
214 This
->txtbackup
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1)*sizeof(WCHAR
));
215 lstrcpyW(This
->txtbackup
, hwndText
);
217 /* Returns if there is no text to search and we doesn't want to display all the entries */
218 if ((!displayall
) && (! *hwndText
) )
221 IEnumString_Reset(This
->enumstr
);
224 LPOLESTR strs
= NULL
;
227 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, &fetched
);
231 if (!strncmpiW(hwndText
, strs
, len
)) {
232 if (!filled
&& (This
->options
& ACO_AUTOAPPEND
)) {
235 strcpyW(buffW
, hwndText
);
236 strcatW(buffW
, &strs
[len
]);
237 SetWindowTextW(hwnd
, buffW
);
238 SendMessageW(hwnd
, EM_SETSEL
, len
, strlenW(strs
));
239 if (!(This
->options
& ACO_AUTOSUGGEST
)) {
245 if (This
->options
& ACO_AUTOSUGGEST
) {
246 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
256 if (This
->options
& ACO_AUTOSUGGEST
) {
258 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
259 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
260 GetWindowRect(hwnd
, &r
);
261 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
262 /* It seems that Windows XP displays 7 lines at most
263 and otherwise displays a vertical scroll bar */
264 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
265 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
268 ShowWindow(This
->hwndListBox
, SW_HIDE
);
276 WNDPROC proc
= This
->wpOrigEditProc
;
278 RemovePropW(hwnd
, autocomplete_propertyW
);
279 SetWindowLongPtrW(hwnd
, GWLP_WNDPROC
, (LONG_PTR
)proc
);
280 This
->hwndEdit
= NULL
;
281 if (This
->hwndListBox
)
282 DestroyWindow(This
->hwndListBox
);
283 IAutoComplete2_Release(&This
->IAutoComplete2_iface
);
284 return CallWindowProcW(proc
, hwnd
, uMsg
, wParam
, lParam
);
287 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
294 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
296 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
302 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
303 SendMessageW(hwnd
, LB_SETCURSEL
, sel
, 0);
306 sel
= SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
309 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
310 msg
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
311 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
312 SendMessageW(This
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
313 SendMessageW(This
->hwndEdit
, EM_SETSEL
, 0, lstrlenW(msg
));
314 ShowWindow(hwnd
, SW_HIDE
);
315 HeapFree(GetProcessHeap(), 0, msg
);
318 return CallWindowProcW(This
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);
323 static void create_listbox(IAutoCompleteImpl
*This
)
327 hwndParent
= GetParent(This
->hwndEdit
);
329 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
330 This
->hwndListBox
= CreateWindowExW(0, WC_LISTBOXW
, NULL
,
331 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
332 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
333 hwndParent
, NULL
, shell32_hInstance
, NULL
);
335 if (This
->hwndListBox
) {
336 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
337 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
341 /**************************************************************************
342 * AutoComplete_QueryInterface
344 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
345 IAutoComplete2
* iface
,
349 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
351 TRACE("(%p)->(IID:%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
354 if (IsEqualIID(riid
, &IID_IUnknown
) ||
355 IsEqualIID(riid
, &IID_IAutoComplete
) ||
356 IsEqualIID(riid
, &IID_IAutoComplete2
))
358 *ppvObj
= &This
->IAutoComplete2_iface
;
360 else if (IsEqualIID(riid
, &IID_IAutoCompleteDropDown
))
362 *ppvObj
= &This
->IAutoCompleteDropDown_iface
;
367 IUnknown_AddRef((IUnknown
*)*ppvObj
);
368 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
371 WARN("unsupported interface: %s\n", debugstr_guid(riid
));
372 return E_NOINTERFACE
;
375 /******************************************************************************
376 * IAutoComplete2_fnAddRef
378 static ULONG WINAPI
IAutoComplete2_fnAddRef(
379 IAutoComplete2
* iface
)
381 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
382 ULONG refCount
= InterlockedIncrement(&This
->ref
);
384 TRACE("(%p)->(%u)\n", This
, refCount
- 1);
389 /******************************************************************************
390 * IAutoComplete2_fnRelease
392 static ULONG WINAPI
IAutoComplete2_fnRelease(
393 IAutoComplete2
* iface
)
395 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
396 ULONG refCount
= InterlockedDecrement(&This
->ref
);
398 TRACE("(%p)->(%u)\n", This
, refCount
+ 1);
401 TRACE("destroying IAutoComplete(%p)\n", This
);
402 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
403 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
405 IEnumString_Release(This
->enumstr
);
406 HeapFree(GetProcessHeap(), 0, This
);
411 /******************************************************************************
412 * IAutoComplete2_fnEnable
414 static HRESULT WINAPI
IAutoComplete2_fnEnable(
415 IAutoComplete2
* iface
,
418 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
421 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
423 This
->enabled
= fEnable
;
428 /******************************************************************************
429 * IAutoComplete2_fnInit
431 static HRESULT WINAPI
IAutoComplete2_fnInit(
432 IAutoComplete2
* iface
,
435 LPCOLESTR pwzsRegKeyPath
,
436 LPCOLESTR pwszQuickComplete
)
438 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
440 TRACE("(%p)->(%p, %p, %s, %s)\n",
441 This
, hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
443 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
444 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
445 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
446 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
448 if (!hwndEdit
|| !punkACL
)
451 if (This
->initialized
)
453 WARN("Autocompletion object is already initialized\n");
454 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
455 return This
->hwndEdit
? E_FAIL
: E_UNEXPECTED
;
458 if (FAILED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
459 WARN("No IEnumString interface\n");
460 return E_NOINTERFACE
;
463 This
->initialized
= TRUE
;
464 This
->hwndEdit
= hwndEdit
;
465 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
466 /* Keep at least one reference to the object until the edit window is destroyed. */
467 IAutoComplete2_AddRef(&This
->IAutoComplete2_iface
);
468 SetPropW( hwndEdit
, autocomplete_propertyW
, This
);
470 if (This
->options
& ACO_AUTOSUGGEST
)
471 create_listbox(This
);
473 if (pwzsRegKeyPath
) {
475 WCHAR result
[MAX_PATH
];
481 /* pwszRegKeyPath contains the key as well as the value, so we split */
482 key
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwzsRegKeyPath
)+1)*sizeof(WCHAR
));
483 strcpyW(key
, pwzsRegKeyPath
);
484 value
= strrchrW(key
, '\\');
487 /* Now value contains the value and buffer the key */
488 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
489 if (res
!= ERROR_SUCCESS
) {
490 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
491 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
493 if (res
== ERROR_SUCCESS
) {
494 res
= RegQueryValueW(hKey
, value
, result
, &len
);
495 if (res
== ERROR_SUCCESS
) {
496 This
->quickComplete
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
497 strcpyW(This
->quickComplete
, result
);
501 HeapFree(GetProcessHeap(), 0, key
);
504 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
505 This
->quickComplete
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwszQuickComplete
)+1)*sizeof(WCHAR
));
506 lstrcpyW(This
->quickComplete
, pwszQuickComplete
);
512 /**************************************************************************
513 * IAutoComplete2_fnGetOptions
515 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
516 IAutoComplete2
* iface
,
519 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
522 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
524 *pdwFlag
= This
->options
;
529 /**************************************************************************
530 * IAutoComplete2_fnSetOptions
532 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
533 IAutoComplete2
* iface
,
536 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
539 TRACE("(%p) -> (0x%x)\n", This
, dwFlag
);
541 This
->options
= dwFlag
;
543 if ((This
->options
& ACO_AUTOSUGGEST
) && This
->hwndEdit
&& !This
->hwndListBox
)
544 create_listbox(This
);
549 /**************************************************************************
550 * IAutoComplete2 VTable
552 static const IAutoComplete2Vtbl acvt
=
554 IAutoComplete2_fnQueryInterface
,
555 IAutoComplete2_fnAddRef
,
556 IAutoComplete2_fnRelease
,
557 IAutoComplete2_fnInit
,
558 IAutoComplete2_fnEnable
,
560 IAutoComplete2_fnSetOptions
,
561 IAutoComplete2_fnGetOptions
,
565 static HRESULT WINAPI
IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown
*iface
,
566 REFIID riid
, LPVOID
*ppvObj
)
568 IAutoCompleteImpl
*This
= impl_from_IAutoCompleteDropDown(iface
);
569 return IAutoComplete2_QueryInterface(&This
->IAutoComplete2_iface
, riid
, ppvObj
);
572 static ULONG WINAPI
IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown
*iface
)
574 IAutoCompleteImpl
*This
= impl_from_IAutoCompleteDropDown(iface
);
575 return IAutoComplete2_AddRef(&This
->IAutoComplete2_iface
);
578 static ULONG WINAPI
IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown
*iface
)
580 IAutoCompleteImpl
*This
= impl_from_IAutoCompleteDropDown(iface
);
581 return IAutoComplete2_Release(&This
->IAutoComplete2_iface
);
584 /**************************************************************************
585 * IAutoCompleteDropDown_fnGetDropDownStatus
587 static HRESULT WINAPI
IAutoCompleteDropDown_fnGetDropDownStatus(
588 IAutoCompleteDropDown
*iface
,
592 IAutoCompleteImpl
*This
= impl_from_IAutoCompleteDropDown(iface
);
595 TRACE("(%p) -> (%p, %p)\n", This
, pdwFlags
, ppwszString
);
597 dropped
= IsWindowVisible(This
->hwndListBox
);
600 *pdwFlags
= (dropped
? ACDD_VISIBLE
: 0);
606 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
611 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
612 *ppwszString
= CoTaskMemAlloc((len
+1)*sizeof(WCHAR
));
613 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)*ppwszString
);
625 /**************************************************************************
626 * IAutoCompleteDropDown_fnResetEnumarator
628 static HRESULT WINAPI
IAutoCompleteDropDown_fnResetEnumerator(
629 IAutoCompleteDropDown
*iface
)
631 IAutoCompleteImpl
*This
= impl_from_IAutoCompleteDropDown(iface
);
633 FIXME("(%p): stub\n", This
);
638 /**************************************************************************
639 * IAutoCompleteDropDown VTable
641 static const IAutoCompleteDropDownVtbl acdropdownvt
=
643 IAutoCompleteDropDown_fnQueryInterface
,
644 IAutoCompleteDropDown_fnAddRef
,
645 IAutoCompleteDropDown_fnRelease
,
646 IAutoCompleteDropDown_fnGetDropDownStatus
,
647 IAutoCompleteDropDown_fnResetEnumerator
,
650 /**************************************************************************
651 * IAutoComplete_Constructor
653 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
655 IAutoCompleteImpl
*lpac
;
658 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
659 return CLASS_E_NOAGGREGATION
;
661 lpac
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
663 return E_OUTOFMEMORY
;
666 lpac
->IAutoComplete2_iface
.lpVtbl
= &acvt
;
667 lpac
->IAutoCompleteDropDown_iface
.lpVtbl
= &acdropdownvt
;
668 lpac
->enabled
= TRUE
;
669 lpac
->options
= ACO_AUTOAPPEND
;
671 hr
= IAutoComplete2_QueryInterface(&lpac
->IAutoComplete2_iface
, riid
, ppv
);
672 IAutoComplete2_Release(&lpac
->IAutoComplete2_iface
);
674 TRACE("-- (%p)->\n",lpac
);