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"
58 #include "wine/unicode.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
64 const IAutoComplete2Vtbl
*lpVtbl
;
69 WNDPROC wpOrigEditProc
;
70 WNDPROC wpOrigLBoxProc
;
74 AUTOCOMPLETEOPTIONS options
;
77 static const IAutoComplete2Vtbl acvt
;
81 converts This to an interface pointer
83 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
84 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtbl)
86 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
87 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
89 /**************************************************************************
90 * IAutoComplete_Constructor
92 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
94 IAutoCompleteImpl
*lpac
;
96 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
97 return CLASS_E_NOAGGREGATION
;
99 lpac
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
101 return E_OUTOFMEMORY
;
104 lpac
->lpVtbl
= &acvt
;
105 lpac
->enabled
= TRUE
;
106 lpac
->enumstr
= NULL
;
107 lpac
->options
= ACO_AUTOAPPEND
;
108 lpac
->wpOrigEditProc
= NULL
;
109 lpac
->hwndListBox
= NULL
;
110 lpac
->txtbackup
= NULL
;
111 lpac
->quickComplete
= NULL
;
113 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac
), riid
, ppv
))) {
114 IUnknown_Release (_IUnknown_ (lpac
));
115 return E_NOINTERFACE
;
118 TRACE("-- (%p)->\n",lpac
);
123 /**************************************************************************
124 * AutoComplete_QueryInterface
126 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
127 IAutoComplete2
* iface
,
131 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
133 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
136 if (IsEqualIID(riid
, &IID_IUnknown
) ||
137 IsEqualIID(riid
, &IID_IAutoComplete
) ||
138 IsEqualIID(riid
, &IID_IAutoComplete2
))
140 *ppvObj
= (IAutoComplete2
*)This
;
145 IUnknown_AddRef((IUnknown
*)*ppvObj
);
146 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
149 TRACE("-- Interface: E_NOINTERFACE\n");
150 return E_NOINTERFACE
;
153 /******************************************************************************
154 * IAutoComplete2_fnAddRef
156 static ULONG WINAPI
IAutoComplete2_fnAddRef(
157 IAutoComplete2
* iface
)
159 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
160 ULONG refCount
= InterlockedIncrement(&This
->ref
);
162 TRACE("(%p)->(%u)\n", This
, refCount
- 1);
167 /******************************************************************************
168 * IAutoComplete2_fnRelease
170 static ULONG WINAPI
IAutoComplete2_fnRelease(
171 IAutoComplete2
* iface
)
173 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
174 ULONG refCount
= InterlockedDecrement(&This
->ref
);
176 TRACE("(%p)->(%u)\n", This
, refCount
+ 1);
179 TRACE(" destroying IAutoComplete(%p)\n",This
);
180 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
181 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
182 if (This
->hwndListBox
)
183 DestroyWindow(This
->hwndListBox
);
185 IEnumString_Release(This
->enumstr
);
186 HeapFree(GetProcessHeap(), 0, This
);
191 /******************************************************************************
192 * IAutoComplete2_fnEnable
194 static HRESULT WINAPI
IAutoComplete2_fnEnable(
195 IAutoComplete2
* iface
,
198 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
202 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
204 This
->enabled
= fEnable
;
209 /******************************************************************************
210 * IAutoComplete2_fnInit
212 static HRESULT WINAPI
IAutoComplete2_fnInit(
213 IAutoComplete2
* iface
,
216 LPCOLESTR pwzsRegKeyPath
,
217 LPCOLESTR pwszQuickComplete
)
219 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
220 static const WCHAR lbName
[] = {'L','i','s','t','B','o','x',0};
222 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
223 This
, (long)hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
225 if (This
->options
& ACO_AUTOSUGGEST
) TRACE(" ACO_AUTOSUGGEST\n");
226 if (This
->options
& ACO_AUTOAPPEND
) TRACE(" ACO_AUTOAPPEND\n");
227 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
228 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
229 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
230 if (This
->options
& ACO_UPDOWNKEYDROPSLIST
) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
231 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
233 This
->hwndEdit
= hwndEdit
;
235 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
236 TRACE("No IEnumString interface\n");
237 return E_NOINTERFACE
;
240 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
241 SetWindowLongPtrW( hwndEdit
, GWLP_USERDATA
, (LONG_PTR
)This
);
243 if (This
->options
& ACO_AUTOSUGGEST
) {
246 hwndParent
= GetParent(This
->hwndEdit
);
248 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
249 This
->hwndListBox
= CreateWindowExW(0, lbName
, NULL
,
250 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
251 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
253 (HINSTANCE
)GetWindowLongPtrW( hwndParent
, GWLP_HINSTANCE
), NULL
);
255 if (This
->hwndListBox
) {
256 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
257 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
261 if (pwzsRegKeyPath
) {
263 WCHAR result
[MAX_PATH
];
269 /* pwszRegKeyPath contains the key as well as the value, so we split */
270 key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwzsRegKeyPath
)+1)*sizeof(WCHAR
));
271 strcpyW(key
, pwzsRegKeyPath
);
272 value
= strrchrW(key
, '\\');
275 /* Now value contains the value and buffer the key */
276 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
277 if (res
!= ERROR_SUCCESS
) {
278 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
279 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
281 if (res
== ERROR_SUCCESS
) {
282 res
= RegQueryValueW(hKey
, value
, result
, &len
);
283 if (res
== ERROR_SUCCESS
) {
284 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
285 strcpyW(This
->quickComplete
, result
);
289 HeapFree(GetProcessHeap(), 0, key
);
292 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
293 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwszQuickComplete
)+1)*sizeof(WCHAR
));
294 lstrcpyW(This
->quickComplete
, pwszQuickComplete
);
300 /**************************************************************************
301 * IAutoComplete2_fnGetOptions
303 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
304 IAutoComplete2
* iface
,
309 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
311 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
313 *pdwFlag
= This
->options
;
318 /**************************************************************************
319 * IAutoComplete2_fnSetOptions
321 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
322 IAutoComplete2
* iface
,
327 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
329 TRACE("(%p) -> (0x%x)\n", This
, dwFlag
);
331 This
->options
= dwFlag
;
336 /**************************************************************************
337 * IAutoComplete2_fnVTable
339 static const IAutoComplete2Vtbl acvt
=
341 IAutoComplete2_fnQueryInterface
,
342 IAutoComplete2_fnAddRef
,
343 IAutoComplete2_fnRelease
,
344 IAutoComplete2_fnInit
,
345 IAutoComplete2_fnEnable
,
347 IAutoComplete2_fnSetOptions
,
348 IAutoComplete2_fnGetOptions
,
352 Window procedure for autocompletion
354 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
356 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
362 BOOL control
, filled
, displayall
= FALSE
;
363 int cpt
, height
, sel
;
365 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
369 case CB_SHOWDROPDOWN
:
370 ShowWindow(This
->hwndListBox
, SW_HIDE
);
373 if ((This
->options
&& ACO_AUTOSUGGEST
) &&
374 ((HWND
)wParam
!= This
->hwndListBox
))
376 ShowWindow(This
->hwndListBox
, SW_HIDE
);
381 GetWindowTextW( hwnd
, (LPWSTR
)hwndText
, 255);
385 /* If quickComplete is set and control is pressed, replace the string */
386 control
= GetKeyState(VK_CONTROL
) & 0x8000;
387 if (control
&& This
->quickComplete
) {
388 hwndQCText
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
389 (lstrlenW(This
->quickComplete
)+lstrlenW(hwndText
))*sizeof(WCHAR
));
390 sel
= sprintfW(hwndQCText
, This
->quickComplete
, hwndText
);
391 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
392 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
393 HeapFree(GetProcessHeap(), 0, hwndQCText
);
396 ShowWindow(This
->hwndListBox
, SW_HIDE
);
404 - if the listbox is not visible, displays it
405 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
406 is present but does not select anything.
407 - if the listbox is visible, change the selection
409 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
410 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
412 /* We must display all the entries */
415 if (IsWindowVisible(This
->hwndListBox
)) {
418 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
419 /* Change the selection */
420 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
422 sel
= ((sel
-1)<0)?count
-1:sel
-1;
424 sel
= ((sel
+1)>= count
)?-1:sel
+1;
425 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
430 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
431 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
432 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
433 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
434 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(msg
), lstrlenW(msg
));
435 HeapFree(GetProcessHeap(), 0, msg
);
437 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
438 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(This
->txtbackup
), lstrlenW(This
->txtbackup
));
446 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
447 ShowWindow(This
->hwndListBox
, SW_HIDE
);
448 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
450 if (This
->options
& ACO_AUTOAPPEND
) {
452 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
454 hwndText
[b
-1] = '\0';
457 SetWindowTextW(hwnd
, hwndText
);
465 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
467 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
468 This
->txtbackup
= HeapAlloc(GetProcessHeap(),
469 HEAP_ZERO_MEMORY
, (lstrlenW(hwndText
)+1)*sizeof(WCHAR
));
470 lstrcpyW(This
->txtbackup
, hwndText
);
472 /* Returns if there is no text to search and we doesn't want to display all the entries */
473 if ((!displayall
) && (! *hwndText
) )
476 IEnumString_Reset(This
->enumstr
);
480 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, &fetched
);
484 if (strstrW(strs
, hwndText
) == strs
) {
485 if (This
->options
& ACO_AUTOAPPEND
) {
486 SetWindowTextW(hwnd
, strs
);
487 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(hwndText
), lstrlenW(strs
));
491 if (This
->options
& ACO_AUTOSUGGEST
) {
492 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
499 if (This
->options
& ACO_AUTOSUGGEST
) {
501 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
502 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
503 GetWindowRect(hwnd
, &r
);
504 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
505 /* It seems that Windows XP displays 7 lines at most
506 and otherwise displays a vertical scroll bar */
507 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
508 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
511 ShowWindow(This
->hwndListBox
, SW_HIDE
);
517 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
524 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
526 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
532 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
533 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
536 sel
= SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
539 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
540 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
541 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
542 SendMessageW(This
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
543 SendMessageW(This
->hwndEdit
, EM_SETSEL
, 0, lstrlenW(msg
));
544 ShowWindow(hwnd
, SW_HIDE
);
545 HeapFree(GetProcessHeap(), 0, msg
);
548 return CallWindowProcW(This
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);