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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
41 #include "wine/debug.h"
45 #include "undocshell.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
62 IAutoCompleteVtbl
*lpVtbl
;
63 IAutoComplete2Vtbl
*lpvtblAutoComplete2
;
68 WNDPROC wpOrigEditProc
;
69 WNDPROC wpOrigLBoxProc
;
73 AUTOCOMPLETEOPTIONS options
;
76 static struct IAutoCompleteVtbl acvt
;
77 static struct IAutoComplete2Vtbl ac2vt
;
79 #define _IAutoComplete2_Offset ((int)(&(((IAutoCompleteImpl*)0)->lpvtblAutoComplete2)))
80 #define _ICOM_THIS_From_IAutoComplete2(class, name) class* This = (class*)(((char*)name)-_IAutoComplete2_Offset);
83 converts This to a interface pointer
85 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
86 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
88 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
89 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
91 /**************************************************************************
92 * IAutoComplete_Constructor
94 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
96 IAutoCompleteImpl
*lpac
;
98 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
99 return CLASS_E_NOAGGREGATION
;
101 lpac
= (IAutoCompleteImpl
*)HeapAlloc(GetProcessHeap(),
102 HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
104 return E_OUTOFMEMORY
;
107 lpac
->lpVtbl
= &acvt
;
108 lpac
->lpvtblAutoComplete2
= &ac2vt
;
109 lpac
->enabled
= TRUE
;
110 lpac
->enumstr
= NULL
;
111 lpac
->options
= ACO_AUTOAPPEND
;
112 lpac
->wpOrigEditProc
= NULL
;
113 lpac
->hwndListBox
= NULL
;
114 lpac
->txtbackup
= NULL
;
115 lpac
->quickComplete
= NULL
;
117 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac
), riid
, ppv
))) {
118 IUnknown_Release (_IUnknown_ (lpac
));
119 return E_NOINTERFACE
;
122 TRACE("-- (%p)->\n",lpac
);
127 /**************************************************************************
128 * AutoComplete_QueryInterface
130 static HRESULT WINAPI
IAutoComplete_fnQueryInterface(
131 IAutoComplete
* iface
,
135 ICOM_THIS(IAutoCompleteImpl
, iface
);
137 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
140 if(IsEqualIID(riid
, &IID_IUnknown
))
144 else if(IsEqualIID(riid
, &IID_IAutoComplete
))
146 *ppvObj
= (IAutoComplete
*)This
;
148 else if(IsEqualIID(riid
, &IID_IAutoComplete2
))
150 *ppvObj
= _IAutoComplete2_ (This
);
155 IAutoComplete_AddRef((IAutoComplete
*)*ppvObj
);
156 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
159 TRACE("-- Interface: E_NOINTERFACE\n");
160 return E_NOINTERFACE
;
163 /******************************************************************************
164 * IAutoComplete_fnAddRef
166 static ULONG WINAPI
IAutoComplete_fnAddRef(
167 IAutoComplete
* iface
)
169 ICOM_THIS(IAutoCompleteImpl
,iface
);
171 TRACE("(%p)->(%lu)\n",This
,This
->ref
);
172 return ++(This
->ref
);
175 /******************************************************************************
176 * IAutoComplete_fnRelease
178 static ULONG WINAPI
IAutoComplete_fnRelease(
179 IAutoComplete
* iface
)
181 ICOM_THIS(IAutoCompleteImpl
,iface
);
183 TRACE("(%p)->(%lu)\n",This
,This
->ref
);
185 if (!--(This
->ref
)) {
186 TRACE(" destroying IAutoComplete(%p)\n",This
);
187 if (This
->quickComplete
)
188 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
190 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
191 if (This
->hwndListBox
)
192 DestroyWindow(This
->hwndListBox
);
194 IEnumString_Release(This
->enumstr
);
195 HeapFree(GetProcessHeap(), 0, This
);
201 /******************************************************************************
202 * IAutoComplete_fnEnable
204 static HRESULT WINAPI
IAutoComplete_fnEnable(
205 IAutoComplete
* iface
,
208 ICOM_THIS(IAutoCompleteImpl
, iface
);
212 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
214 This
->enabled
= fEnable
;
219 /******************************************************************************
220 * IAutoComplete_fnInit
222 static HRESULT WINAPI
IAutoComplete_fnInit(
223 IAutoComplete
* iface
,
226 LPCOLESTR pwzsRegKeyPath
,
227 LPCOLESTR pwszQuickComplete
)
229 ICOM_THIS(IAutoCompleteImpl
, iface
);
230 static const WCHAR lbName
[] = {'L','i','s','t','B','o','x',0};
232 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
233 This
, (long)hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
235 if (This
->options
& ACO_AUTOSUGGEST
) TRACE(" ACO_AUTOSUGGEST\n");
236 if (This
->options
& ACO_AUTOAPPEND
) TRACE(" ACO_AUTOAPPEND\n");
237 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
238 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
239 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
240 if (This
->options
& ACO_UPDOWNKEYDROPSLIST
) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
241 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
243 This
->hwndEdit
= hwndEdit
;
245 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
246 TRACE("No IEnumString interface\n");
247 return E_NOINTERFACE
;
250 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
251 SetWindowLongPtrW( hwndEdit
, GWLP_USERDATA
, (LONG_PTR
)This
);
253 if (This
->options
& ACO_AUTOSUGGEST
) {
256 hwndParent
= GetParent(This
->hwndEdit
);
258 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
259 This
->hwndListBox
= CreateWindowExW(0, lbName
, NULL
,
260 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
261 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
263 (HINSTANCE
)GetWindowLongA( hwndParent
, GWL_HINSTANCE
), NULL
);
265 if (This
->hwndListBox
) {
266 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
267 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
271 if (pwzsRegKeyPath
) {
273 WCHAR result
[MAX_PATH
];
279 /* pwszRegKeyPath contains the key as well as the value, so we split */
280 key
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwzsRegKeyPath
)+1)*sizeof(WCHAR
));
281 strcpyW(key
, pwzsRegKeyPath
);
282 value
= strrchrW(key
, '\\');
285 /* Now value contains the value and buffer the key */
286 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
287 if (res
!= ERROR_SUCCESS
) {
288 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
289 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
291 if (res
== ERROR_SUCCESS
) {
292 res
= RegQueryValueW(hKey
, value
, result
, &len
);
293 if (res
== ERROR_SUCCESS
) {
294 This
->quickComplete
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
295 strcpyW(This
->quickComplete
, result
);
299 HeapFree(GetProcessHeap(), 0, key
);
302 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
303 This
->quickComplete
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwszQuickComplete
)+1)*sizeof(WCHAR
));
304 lstrcpyW(This
->quickComplete
, pwszQuickComplete
);
310 /**************************************************************************
311 * IAutoComplete_fnVTable
313 static IAutoCompleteVtbl acvt
=
315 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
316 IAutoComplete_fnQueryInterface
,
317 IAutoComplete_fnAddRef
,
318 IAutoComplete_fnRelease
,
319 IAutoComplete_fnInit
,
320 IAutoComplete_fnEnable
,
323 /**************************************************************************
324 * AutoComplete2_QueryInterface
326 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
327 IAutoComplete2
* iface
,
331 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
, iface
);
333 TRACE ("(%p)->(%s,%p)\n", This
, shdebugstr_guid (riid
), ppvObj
);
335 return IAutoComplete_QueryInterface((IAutoComplete
*)This
, riid
, ppvObj
);
338 /******************************************************************************
339 * IAutoComplete2_fnAddRef
341 static ULONG WINAPI
IAutoComplete2_fnAddRef(
342 IAutoComplete2
* iface
)
344 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
,iface
);
346 TRACE ("(%p)->(count=%lu)\n", This
, This
->ref
);
348 return IAutoComplete2_AddRef((IAutoComplete
*)This
);
351 /******************************************************************************
352 * IAutoComplete2_fnRelease
354 static ULONG WINAPI
IAutoComplete2_fnRelease(
355 IAutoComplete2
* iface
)
357 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
,iface
);
359 TRACE ("(%p)->(count=%lu)\n", This
, This
->ref
);
361 return IAutoComplete_Release((IAutoComplete
*)This
);
364 /******************************************************************************
365 * IAutoComplete2_fnEnable
367 static HRESULT WINAPI
IAutoComplete2_fnEnable(
368 IAutoComplete2
* iface
,
371 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
, iface
);
373 TRACE ("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
375 return IAutoComplete_Enable((IAutoComplete
*)This
, fEnable
);
378 /******************************************************************************
379 * IAutoComplete2_fnInit
381 static HRESULT WINAPI
IAutoComplete2_fnInit(
382 IAutoComplete2
* iface
,
385 LPCOLESTR pwzsRegKeyPath
,
386 LPCOLESTR pwszQuickComplete
)
388 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
, iface
);
390 TRACE("(%p)\n", This
);
392 return IAutoComplete_Init((IAutoComplete
*)This
, hwndEdit
, punkACL
, pwzsRegKeyPath
, pwszQuickComplete
);
395 /**************************************************************************
396 * IAutoComplete_fnGetOptions
398 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
399 IAutoComplete2
* iface
,
404 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
, iface
);
406 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
408 *pdwFlag
= This
->options
;
413 /**************************************************************************
414 * IAutoComplete_fnSetOptions
416 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
417 IAutoComplete2
* iface
,
422 _ICOM_THIS_From_IAutoComplete2(IAutoCompleteImpl
, iface
);
424 TRACE("(%p) -> (0x%lx)\n", This
, dwFlag
);
426 This
->options
= dwFlag
;
431 /**************************************************************************
432 * IAutoComplete2_fnVTable
434 static IAutoComplete2Vtbl ac2vt
=
436 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
437 IAutoComplete2_fnQueryInterface
,
438 IAutoComplete2_fnAddRef
,
439 IAutoComplete2_fnRelease
,
440 IAutoComplete2_fnInit
,
441 IAutoComplete2_fnEnable
,
443 IAutoComplete2_fnSetOptions
,
444 IAutoComplete2_fnGetOptions
,
448 Window procedure for autocompletion
450 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
452 ICOM_THIS(IAutoCompleteImpl
, GetWindowLongPtrW(hwnd
, GWLP_USERDATA
));
458 BOOL control
, filled
, displayall
= FALSE
;
459 int cpt
, height
, sel
;
461 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
465 case CB_SHOWDROPDOWN
:
466 ShowWindow(This
->hwndListBox
, SW_HIDE
);
469 if ((This
->options
&& ACO_AUTOSUGGEST
) &&
470 ((HWND
)wParam
!= This
->hwndListBox
))
472 ShowWindow(This
->hwndListBox
, SW_HIDE
);
477 GetWindowTextW( hwnd
, (LPWSTR
)hwndText
, 255);
481 /* If quickComplete is set and control is pressed, replace the string */
482 control
= GetKeyState(VK_CONTROL
) & 0x8000;
483 if (control
&& This
->quickComplete
) {
484 hwndQCText
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
485 (lstrlenW(This
->quickComplete
)+lstrlenW(hwndText
))*sizeof(WCHAR
));
486 sel
= sprintfW(hwndQCText
, This
->quickComplete
, hwndText
);
487 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
488 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
489 HeapFree(GetProcessHeap(), 0, hwndQCText
);
492 ShowWindow(This
->hwndListBox
, SW_HIDE
);
500 - if the listbox is not visible, displays it
501 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
502 is present but does not select anything.
503 - if the listbox is visible, change the selection
505 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
506 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
508 /* We must dispays all the entries */
511 if (IsWindowVisible(This
->hwndListBox
)) {
514 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
515 /* Change the selection */
516 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
518 sel
= ((sel
-1)<0)?count
-1:sel
-1;
520 sel
= ((sel
+1)>= count
)?-1:sel
+1;
521 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
526 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
527 msg
= (WCHAR
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
528 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
529 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
530 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(msg
), lstrlenW(msg
));
531 HeapFree(GetProcessHeap(), 0, msg
);
533 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
534 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(This
->txtbackup
), lstrlenW(This
->txtbackup
));
542 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
543 ShowWindow(This
->hwndListBox
, SW_HIDE
);
544 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
546 if (This
->options
& ACO_AUTOAPPEND
) {
548 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
550 hwndText
[b
-1] = '\0';
553 SetWindowTextW(hwnd
, hwndText
);
561 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
563 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
564 This
->txtbackup
= (WCHAR
*) HeapAlloc(GetProcessHeap(),
565 HEAP_ZERO_MEMORY
, (lstrlenW(hwndText
)+1)*sizeof(WCHAR
));
566 lstrcpyW(This
->txtbackup
, hwndText
);
568 /* Returns if there is no text to search and we doesn't want to display all the entries */
569 if ((!displayall
) && (! *hwndText
) )
572 IEnumString_Reset(This
->enumstr
);
575 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, NULL
);
579 if ((LPWSTR
)strstrW(strs
, hwndText
) == strs
) {
581 if (This
->options
& ACO_AUTOAPPEND
) {
582 SetWindowTextW(hwnd
, strs
);
583 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(hwndText
), lstrlenW(strs
));
587 if (This
->options
& ACO_AUTOSUGGEST
) {
588 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
595 if (This
->options
& ACO_AUTOSUGGEST
) {
597 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
598 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
599 GetWindowRect(hwnd
, &r
);
600 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
601 /* It seems that Windows XP displays 7 lines at most
602 and otherwise displays a vertical scroll bar */
603 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
604 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
607 ShowWindow(This
->hwndListBox
, SW_HIDE
);
613 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
620 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
622 ICOM_THIS(IAutoCompleteImpl
, GetWindowLongPtrW(hwnd
, GWLP_USERDATA
));
628 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
629 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
632 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
633 msg
= (WCHAR
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
634 sel
= (INT
)SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
635 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
636 SendMessageW(This
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
637 SendMessageW(This
->hwndEdit
, EM_SETSEL
, 0, lstrlenW(msg
));
638 ShowWindow(hwnd
, SW_HIDE
);
639 HeapFree(GetProcessHeap(), 0, msg
);
642 return CallWindowProcW(This
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);