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
44 #include "wine/debug.h"
48 #include "undocshell.h"
59 #include "wine/unicode.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
65 const IAutoCompleteVtbl
*lpVtbl
;
66 const IAutoComplete2Vtbl
*lpvtblAutoComplete2
;
71 WNDPROC wpOrigEditProc
;
72 WNDPROC wpOrigLBoxProc
;
76 AUTOCOMPLETEOPTIONS options
;
79 static const IAutoCompleteVtbl acvt
;
80 static const IAutoComplete2Vtbl ac2vt
;
82 static inline IAutoCompleteImpl
*impl_from_IAutoComplete2( IAutoComplete2
*iface
)
84 return (IAutoCompleteImpl
*)((char*)iface
- FIELD_OFFSET(IAutoCompleteImpl
, lpvtblAutoComplete2
));
89 converts This to an interface pointer
91 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
92 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
94 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
95 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
97 /**************************************************************************
98 * IAutoComplete_Constructor
100 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
102 IAutoCompleteImpl
*lpac
;
104 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
105 return CLASS_E_NOAGGREGATION
;
107 lpac
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
109 return E_OUTOFMEMORY
;
112 lpac
->lpVtbl
= &acvt
;
113 lpac
->lpvtblAutoComplete2
= &ac2vt
;
114 lpac
->enabled
= TRUE
;
115 lpac
->enumstr
= NULL
;
116 lpac
->options
= ACO_AUTOAPPEND
;
117 lpac
->wpOrigEditProc
= NULL
;
118 lpac
->hwndListBox
= NULL
;
119 lpac
->txtbackup
= NULL
;
120 lpac
->quickComplete
= NULL
;
122 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac
), riid
, ppv
))) {
123 IUnknown_Release (_IUnknown_ (lpac
));
124 return E_NOINTERFACE
;
127 TRACE("-- (%p)->\n",lpac
);
132 /**************************************************************************
133 * AutoComplete_QueryInterface
135 static HRESULT WINAPI
IAutoComplete_fnQueryInterface(
136 IAutoComplete
* iface
,
140 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
142 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
145 if(IsEqualIID(riid
, &IID_IUnknown
))
149 else if(IsEqualIID(riid
, &IID_IAutoComplete
))
151 *ppvObj
= (IAutoComplete
*)This
;
153 else if(IsEqualIID(riid
, &IID_IAutoComplete2
))
155 *ppvObj
= _IAutoComplete2_ (This
);
160 IAutoComplete_AddRef((IAutoComplete
*)*ppvObj
);
161 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
164 TRACE("-- Interface: E_NOINTERFACE\n");
165 return E_NOINTERFACE
;
168 /******************************************************************************
169 * IAutoComplete_fnAddRef
171 static ULONG WINAPI
IAutoComplete_fnAddRef(
172 IAutoComplete
* iface
)
174 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
175 ULONG refCount
= InterlockedIncrement(&This
->ref
);
177 TRACE("(%p)->(%u)\n", This
, refCount
- 1);
182 /******************************************************************************
183 * IAutoComplete_fnRelease
185 static ULONG WINAPI
IAutoComplete_fnRelease(
186 IAutoComplete
* iface
)
188 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
189 ULONG refCount
= InterlockedDecrement(&This
->ref
);
191 TRACE("(%p)->(%u)\n", This
, refCount
+ 1);
194 TRACE(" destroying IAutoComplete(%p)\n",This
);
195 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
196 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
197 if (This
->hwndListBox
)
198 DestroyWindow(This
->hwndListBox
);
200 IEnumString_Release(This
->enumstr
);
201 HeapFree(GetProcessHeap(), 0, This
);
206 /******************************************************************************
207 * IAutoComplete_fnEnable
209 static HRESULT WINAPI
IAutoComplete_fnEnable(
210 IAutoComplete
* iface
,
213 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
217 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
219 This
->enabled
= fEnable
;
224 /******************************************************************************
225 * IAutoComplete_fnInit
227 static HRESULT WINAPI
IAutoComplete_fnInit(
228 IAutoComplete
* iface
,
231 LPCOLESTR pwzsRegKeyPath
,
232 LPCOLESTR pwszQuickComplete
)
234 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
235 static const WCHAR lbName
[] = {'L','i','s','t','B','o','x',0};
237 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
238 This
, (long)hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
240 if (This
->options
& ACO_AUTOSUGGEST
) TRACE(" ACO_AUTOSUGGEST\n");
241 if (This
->options
& ACO_AUTOAPPEND
) TRACE(" ACO_AUTOAPPEND\n");
242 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
243 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
244 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
245 if (This
->options
& ACO_UPDOWNKEYDROPSLIST
) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
246 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
248 This
->hwndEdit
= hwndEdit
;
250 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
251 TRACE("No IEnumString interface\n");
252 return E_NOINTERFACE
;
255 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
256 SetWindowLongPtrW( hwndEdit
, GWLP_USERDATA
, (LONG_PTR
)This
);
258 if (This
->options
& ACO_AUTOSUGGEST
) {
261 hwndParent
= GetParent(This
->hwndEdit
);
263 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
264 This
->hwndListBox
= CreateWindowExW(0, lbName
, NULL
,
265 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
266 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
268 (HINSTANCE
)GetWindowLongPtrW( hwndParent
, GWLP_HINSTANCE
), NULL
);
270 if (This
->hwndListBox
) {
271 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
272 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
276 if (pwzsRegKeyPath
) {
278 WCHAR result
[MAX_PATH
];
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
, '\\');
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
);
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
);
304 HeapFree(GetProcessHeap(), 0, key
);
307 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
308 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwszQuickComplete
)+1)*sizeof(WCHAR
));
309 lstrcpyW(This
->quickComplete
, pwszQuickComplete
);
315 /**************************************************************************
316 * IAutoComplete_fnVTable
318 static const IAutoCompleteVtbl acvt
=
320 IAutoComplete_fnQueryInterface
,
321 IAutoComplete_fnAddRef
,
322 IAutoComplete_fnRelease
,
323 IAutoComplete_fnInit
,
324 IAutoComplete_fnEnable
,
327 /**************************************************************************
328 * AutoComplete2_QueryInterface
330 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
331 IAutoComplete2
* iface
,
335 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
337 TRACE ("(%p)->(%s,%p)\n", This
, shdebugstr_guid (riid
), ppvObj
);
339 return IAutoComplete_QueryInterface((IAutoComplete
*)This
, riid
, ppvObj
);
342 /******************************************************************************
343 * IAutoComplete2_fnAddRef
345 static ULONG WINAPI
IAutoComplete2_fnAddRef(
346 IAutoComplete2
* iface
)
348 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
350 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
352 return IAutoComplete2_AddRef((IAutoComplete
*)This
);
355 /******************************************************************************
356 * IAutoComplete2_fnRelease
358 static ULONG WINAPI
IAutoComplete2_fnRelease(
359 IAutoComplete2
* iface
)
361 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
363 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
365 return IAutoComplete_Release((IAutoComplete
*)This
);
368 /******************************************************************************
369 * IAutoComplete2_fnEnable
371 static HRESULT WINAPI
IAutoComplete2_fnEnable(
372 IAutoComplete2
* iface
,
375 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
377 TRACE ("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
379 return IAutoComplete_Enable((IAutoComplete
*)This
, fEnable
);
382 /******************************************************************************
383 * IAutoComplete2_fnInit
385 static HRESULT WINAPI
IAutoComplete2_fnInit(
386 IAutoComplete2
* iface
,
389 LPCOLESTR pwzsRegKeyPath
,
390 LPCOLESTR pwszQuickComplete
)
392 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
394 TRACE("(%p)\n", This
);
396 return IAutoComplete_Init((IAutoComplete
*)This
, hwndEdit
, punkACL
, pwzsRegKeyPath
, pwszQuickComplete
);
399 /**************************************************************************
400 * IAutoComplete_fnGetOptions
402 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
403 IAutoComplete2
* iface
,
408 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
410 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
412 *pdwFlag
= This
->options
;
417 /**************************************************************************
418 * IAutoComplete_fnSetOptions
420 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
421 IAutoComplete2
* iface
,
426 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
428 TRACE("(%p) -> (0x%x)\n", This
, dwFlag
);
430 This
->options
= dwFlag
;
435 /**************************************************************************
436 * IAutoComplete2_fnVTable
438 static const IAutoComplete2Vtbl ac2vt
=
440 IAutoComplete2_fnQueryInterface
,
441 IAutoComplete2_fnAddRef
,
442 IAutoComplete2_fnRelease
,
443 IAutoComplete2_fnInit
,
444 IAutoComplete2_fnEnable
,
446 IAutoComplete2_fnSetOptions
,
447 IAutoComplete2_fnGetOptions
,
451 Window procedure for autocompletion
453 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
455 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
461 BOOL control
, filled
, displayall
= FALSE
;
462 int cpt
, height
, sel
;
464 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
468 case CB_SHOWDROPDOWN
:
469 ShowWindow(This
->hwndListBox
, SW_HIDE
);
472 if ((This
->options
&& ACO_AUTOSUGGEST
) &&
473 ((HWND
)wParam
!= This
->hwndListBox
))
475 ShowWindow(This
->hwndListBox
, SW_HIDE
);
480 GetWindowTextW( hwnd
, (LPWSTR
)hwndText
, 255);
484 /* If quickComplete is set and control is pressed, replace the string */
485 control
= GetKeyState(VK_CONTROL
) & 0x8000;
486 if (control
&& This
->quickComplete
) {
487 hwndQCText
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
488 (lstrlenW(This
->quickComplete
)+lstrlenW(hwndText
))*sizeof(WCHAR
));
489 sel
= sprintfW(hwndQCText
, This
->quickComplete
, hwndText
);
490 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
491 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
492 HeapFree(GetProcessHeap(), 0, hwndQCText
);
495 ShowWindow(This
->hwndListBox
, SW_HIDE
);
503 - if the listbox is not visible, displays it
504 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
505 is present but does not select anything.
506 - if the listbox is visible, change the selection
508 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
509 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
511 /* We must dispays all the entries */
514 if (IsWindowVisible(This
->hwndListBox
)) {
517 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
518 /* Change the selection */
519 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
521 sel
= ((sel
-1)<0)?count
-1:sel
-1;
523 sel
= ((sel
+1)>= count
)?-1:sel
+1;
524 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
529 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
530 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
531 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
532 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
533 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(msg
), lstrlenW(msg
));
534 HeapFree(GetProcessHeap(), 0, msg
);
536 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
537 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(This
->txtbackup
), lstrlenW(This
->txtbackup
));
545 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
546 ShowWindow(This
->hwndListBox
, SW_HIDE
);
547 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
549 if (This
->options
& ACO_AUTOAPPEND
) {
551 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
553 hwndText
[b
-1] = '\0';
556 SetWindowTextW(hwnd
, hwndText
);
564 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
566 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
567 This
->txtbackup
= HeapAlloc(GetProcessHeap(),
568 HEAP_ZERO_MEMORY
, (lstrlenW(hwndText
)+1)*sizeof(WCHAR
));
569 lstrcpyW(This
->txtbackup
, hwndText
);
571 /* Returns if there is no text to search and we doesn't want to display all the entries */
572 if ((!displayall
) && (! *hwndText
) )
575 IEnumString_Reset(This
->enumstr
);
578 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, NULL
);
582 if ((LPWSTR
)strstrW(strs
, hwndText
) == strs
) {
584 if (This
->options
& ACO_AUTOAPPEND
) {
585 SetWindowTextW(hwnd
, strs
);
586 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(hwndText
), lstrlenW(strs
));
590 if (This
->options
& ACO_AUTOSUGGEST
) {
591 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
598 if (This
->options
& ACO_AUTOSUGGEST
) {
600 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
601 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
602 GetWindowRect(hwnd
, &r
);
603 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
604 /* It seems that Windows XP displays 7 lines at most
605 and otherwise displays a vertical scroll bar */
606 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
607 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
610 ShowWindow(This
->hwndListBox
, SW_HIDE
);
616 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
623 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
625 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
631 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
632 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
635 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
636 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
637 sel
= (INT
)SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
638 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
639 SendMessageW(This
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
640 SendMessageW(This
->hwndEdit
, EM_SETSEL
, 0, lstrlenW(msg
));
641 ShowWindow(hwnd
, SW_HIDE
);
642 HeapFree(GetProcessHeap(), 0, msg
);
645 return CallWindowProcW(This
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);