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 IAutoCompleteVtbl
*lpVtbl
;
65 const IAutoComplete2Vtbl
*lpvtblAutoComplete2
;
70 WNDPROC wpOrigEditProc
;
71 WNDPROC wpOrigLBoxProc
;
75 AUTOCOMPLETEOPTIONS options
;
78 static const IAutoCompleteVtbl acvt
;
79 static const IAutoComplete2Vtbl ac2vt
;
81 static inline IAutoCompleteImpl
*impl_from_IAutoComplete2( IAutoComplete2
*iface
)
83 return (IAutoCompleteImpl
*)((char*)iface
- FIELD_OFFSET(IAutoCompleteImpl
, lpvtblAutoComplete2
));
88 converts This to an interface pointer
90 #define _IUnknown_(This) (IUnknown*)&(This->lpVtbl)
91 #define _IAutoComplete2_(This) (IAutoComplete2*)&(This->lpvtblAutoComplete2)
93 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
94 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
96 /**************************************************************************
97 * IAutoComplete_Constructor
99 HRESULT WINAPI
IAutoComplete_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
)
101 IAutoCompleteImpl
*lpac
;
103 if (pUnkOuter
&& !IsEqualIID (riid
, &IID_IUnknown
))
104 return CLASS_E_NOAGGREGATION
;
106 lpac
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAutoCompleteImpl
));
108 return E_OUTOFMEMORY
;
111 lpac
->lpVtbl
= &acvt
;
112 lpac
->lpvtblAutoComplete2
= &ac2vt
;
113 lpac
->enabled
= TRUE
;
114 lpac
->enumstr
= NULL
;
115 lpac
->options
= ACO_AUTOAPPEND
;
116 lpac
->wpOrigEditProc
= NULL
;
117 lpac
->hwndListBox
= NULL
;
118 lpac
->txtbackup
= NULL
;
119 lpac
->quickComplete
= NULL
;
121 if (!SUCCEEDED (IUnknown_QueryInterface (_IUnknown_ (lpac
), riid
, ppv
))) {
122 IUnknown_Release (_IUnknown_ (lpac
));
123 return E_NOINTERFACE
;
126 TRACE("-- (%p)->\n",lpac
);
131 /**************************************************************************
132 * AutoComplete_QueryInterface
134 static HRESULT WINAPI
IAutoComplete_fnQueryInterface(
135 IAutoComplete
* iface
,
139 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
141 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This
, shdebugstr_guid(riid
), ppvObj
);
144 if(IsEqualIID(riid
, &IID_IUnknown
))
148 else if(IsEqualIID(riid
, &IID_IAutoComplete
))
150 *ppvObj
= (IAutoComplete
*)This
;
152 else if(IsEqualIID(riid
, &IID_IAutoComplete2
))
154 *ppvObj
= _IAutoComplete2_ (This
);
159 IAutoComplete_AddRef((IAutoComplete
*)*ppvObj
);
160 TRACE("-- Interface: (%p)->(%p)\n", ppvObj
, *ppvObj
);
163 TRACE("-- Interface: E_NOINTERFACE\n");
164 return E_NOINTERFACE
;
167 /******************************************************************************
168 * IAutoComplete_fnAddRef
170 static ULONG WINAPI
IAutoComplete_fnAddRef(
171 IAutoComplete
* iface
)
173 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
174 ULONG refCount
= InterlockedIncrement(&This
->ref
);
176 TRACE("(%p)->(%u)\n", This
, refCount
- 1);
181 /******************************************************************************
182 * IAutoComplete_fnRelease
184 static ULONG WINAPI
IAutoComplete_fnRelease(
185 IAutoComplete
* iface
)
187 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
188 ULONG refCount
= InterlockedDecrement(&This
->ref
);
190 TRACE("(%p)->(%u)\n", This
, refCount
+ 1);
193 TRACE(" destroying IAutoComplete(%p)\n",This
);
194 HeapFree(GetProcessHeap(), 0, This
->quickComplete
);
195 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
196 if (This
->hwndListBox
)
197 DestroyWindow(This
->hwndListBox
);
199 IEnumString_Release(This
->enumstr
);
200 HeapFree(GetProcessHeap(), 0, This
);
205 /******************************************************************************
206 * IAutoComplete_fnEnable
208 static HRESULT WINAPI
IAutoComplete_fnEnable(
209 IAutoComplete
* iface
,
212 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
216 TRACE("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
218 This
->enabled
= fEnable
;
223 /******************************************************************************
224 * IAutoComplete_fnInit
226 static HRESULT WINAPI
IAutoComplete_fnInit(
227 IAutoComplete
* iface
,
230 LPCOLESTR pwzsRegKeyPath
,
231 LPCOLESTR pwszQuickComplete
)
233 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)iface
;
234 static const WCHAR lbName
[] = {'L','i','s','t','B','o','x',0};
236 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
237 This
, (long)hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
239 if (This
->options
& ACO_AUTOSUGGEST
) TRACE(" ACO_AUTOSUGGEST\n");
240 if (This
->options
& ACO_AUTOAPPEND
) TRACE(" ACO_AUTOAPPEND\n");
241 if (This
->options
& ACO_SEARCH
) FIXME(" ACO_SEARCH not supported\n");
242 if (This
->options
& ACO_FILTERPREFIXES
) FIXME(" ACO_FILTERPREFIXES not supported\n");
243 if (This
->options
& ACO_USETAB
) FIXME(" ACO_USETAB not supported\n");
244 if (This
->options
& ACO_UPDOWNKEYDROPSLIST
) TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
245 if (This
->options
& ACO_RTLREADING
) FIXME(" ACO_RTLREADING not supported\n");
247 This
->hwndEdit
= hwndEdit
;
249 if (!SUCCEEDED (IUnknown_QueryInterface (punkACL
, &IID_IEnumString
, (LPVOID
*)&This
->enumstr
))) {
250 TRACE("No IEnumString interface\n");
251 return E_NOINTERFACE
;
254 This
->wpOrigEditProc
= (WNDPROC
) SetWindowLongPtrW( hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
) ACEditSubclassProc
);
255 SetWindowLongPtrW( hwndEdit
, GWLP_USERDATA
, (LONG_PTR
)This
);
257 if (This
->options
& ACO_AUTOSUGGEST
) {
260 hwndParent
= GetParent(This
->hwndEdit
);
262 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
263 This
->hwndListBox
= CreateWindowExW(0, lbName
, NULL
,
264 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
265 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
267 (HINSTANCE
)GetWindowLongPtrW( hwndParent
, GWLP_HINSTANCE
), NULL
);
269 if (This
->hwndListBox
) {
270 This
->wpOrigLBoxProc
= (WNDPROC
) SetWindowLongPtrW( This
->hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
) ACLBoxSubclassProc
);
271 SetWindowLongPtrW( This
->hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)This
);
275 if (pwzsRegKeyPath
) {
277 WCHAR result
[MAX_PATH
];
283 /* pwszRegKeyPath contains the key as well as the value, so we split */
284 key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwzsRegKeyPath
)+1)*sizeof(WCHAR
));
285 strcpyW(key
, pwzsRegKeyPath
);
286 value
= strrchrW(key
, '\\');
289 /* Now value contains the value and buffer the key */
290 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
291 if (res
!= ERROR_SUCCESS
) {
292 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
293 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
295 if (res
== ERROR_SUCCESS
) {
296 res
= RegQueryValueW(hKey
, value
, result
, &len
);
297 if (res
== ERROR_SUCCESS
) {
298 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
*sizeof(WCHAR
));
299 strcpyW(This
->quickComplete
, result
);
303 HeapFree(GetProcessHeap(), 0, key
);
306 if ((pwszQuickComplete
) && (!This
->quickComplete
)) {
307 This
->quickComplete
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (lstrlenW(pwszQuickComplete
)+1)*sizeof(WCHAR
));
308 lstrcpyW(This
->quickComplete
, pwszQuickComplete
);
314 /**************************************************************************
315 * IAutoComplete_fnVTable
317 static const IAutoCompleteVtbl acvt
=
319 IAutoComplete_fnQueryInterface
,
320 IAutoComplete_fnAddRef
,
321 IAutoComplete_fnRelease
,
322 IAutoComplete_fnInit
,
323 IAutoComplete_fnEnable
,
326 /**************************************************************************
327 * AutoComplete2_QueryInterface
329 static HRESULT WINAPI
IAutoComplete2_fnQueryInterface(
330 IAutoComplete2
* iface
,
334 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
336 TRACE ("(%p)->(%s,%p)\n", This
, shdebugstr_guid (riid
), ppvObj
);
338 return IAutoComplete_QueryInterface((IAutoComplete
*)This
, riid
, ppvObj
);
341 /******************************************************************************
342 * IAutoComplete2_fnAddRef
344 static ULONG WINAPI
IAutoComplete2_fnAddRef(
345 IAutoComplete2
* iface
)
347 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
349 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
351 return IAutoComplete2_AddRef((IAutoComplete
*)This
);
354 /******************************************************************************
355 * IAutoComplete2_fnRelease
357 static ULONG WINAPI
IAutoComplete2_fnRelease(
358 IAutoComplete2
* iface
)
360 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
362 TRACE ("(%p)->(count=%u)\n", This
, This
->ref
);
364 return IAutoComplete_Release((IAutoComplete
*)This
);
367 /******************************************************************************
368 * IAutoComplete2_fnEnable
370 static HRESULT WINAPI
IAutoComplete2_fnEnable(
371 IAutoComplete2
* iface
,
374 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
376 TRACE ("(%p)->(%s)\n", This
, (fEnable
)?"true":"false");
378 return IAutoComplete_Enable((IAutoComplete
*)This
, fEnable
);
381 /******************************************************************************
382 * IAutoComplete2_fnInit
384 static HRESULT WINAPI
IAutoComplete2_fnInit(
385 IAutoComplete2
* iface
,
388 LPCOLESTR pwzsRegKeyPath
,
389 LPCOLESTR pwszQuickComplete
)
391 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
393 TRACE("(%p)\n", This
);
395 return IAutoComplete_Init((IAutoComplete
*)This
, hwndEdit
, punkACL
, pwzsRegKeyPath
, pwszQuickComplete
);
398 /**************************************************************************
399 * IAutoComplete_fnGetOptions
401 static HRESULT WINAPI
IAutoComplete2_fnGetOptions(
402 IAutoComplete2
* iface
,
407 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
409 TRACE("(%p) -> (%p)\n", This
, pdwFlag
);
411 *pdwFlag
= This
->options
;
416 /**************************************************************************
417 * IAutoComplete_fnSetOptions
419 static HRESULT WINAPI
IAutoComplete2_fnSetOptions(
420 IAutoComplete2
* iface
,
425 IAutoCompleteImpl
*This
= impl_from_IAutoComplete2(iface
);
427 TRACE("(%p) -> (0x%x)\n", This
, dwFlag
);
429 This
->options
= dwFlag
;
434 /**************************************************************************
435 * IAutoComplete2_fnVTable
437 static const IAutoComplete2Vtbl ac2vt
=
439 IAutoComplete2_fnQueryInterface
,
440 IAutoComplete2_fnAddRef
,
441 IAutoComplete2_fnRelease
,
442 IAutoComplete2_fnInit
,
443 IAutoComplete2_fnEnable
,
445 IAutoComplete2_fnSetOptions
,
446 IAutoComplete2_fnGetOptions
,
450 Window procedure for autocompletion
452 static LRESULT APIENTRY
ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
454 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
460 BOOL control
, filled
, displayall
= FALSE
;
461 int cpt
, height
, sel
;
463 if (!This
->enabled
) return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
467 case CB_SHOWDROPDOWN
:
468 ShowWindow(This
->hwndListBox
, SW_HIDE
);
471 if ((This
->options
&& ACO_AUTOSUGGEST
) &&
472 ((HWND
)wParam
!= This
->hwndListBox
))
474 ShowWindow(This
->hwndListBox
, SW_HIDE
);
479 GetWindowTextW( hwnd
, (LPWSTR
)hwndText
, 255);
483 /* If quickComplete is set and control is pressed, replace the string */
484 control
= GetKeyState(VK_CONTROL
) & 0x8000;
485 if (control
&& This
->quickComplete
) {
486 hwndQCText
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
487 (lstrlenW(This
->quickComplete
)+lstrlenW(hwndText
))*sizeof(WCHAR
));
488 sel
= sprintfW(hwndQCText
, This
->quickComplete
, hwndText
);
489 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
490 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
491 HeapFree(GetProcessHeap(), 0, hwndQCText
);
494 ShowWindow(This
->hwndListBox
, SW_HIDE
);
502 - if the listbox is not visible, displays it
503 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
504 is present but does not select anything.
505 - if the listbox is visible, change the selection
507 if ( (This
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
508 && (!IsWindowVisible(This
->hwndListBox
) && (! *hwndText
)) )
510 /* We must display all the entries */
513 if (IsWindowVisible(This
->hwndListBox
)) {
516 count
= SendMessageW(This
->hwndListBox
, LB_GETCOUNT
, 0, 0);
517 /* Change the selection */
518 sel
= SendMessageW(This
->hwndListBox
, LB_GETCURSEL
, 0, 0);
520 sel
= ((sel
-1)<0)?count
-1:sel
-1;
522 sel
= ((sel
+1)>= count
)?-1:sel
+1;
523 SendMessageW(This
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
528 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
529 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
530 SendMessageW(This
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
531 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
532 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(msg
), lstrlenW(msg
));
533 HeapFree(GetProcessHeap(), 0, msg
);
535 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)This
->txtbackup
);
536 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(This
->txtbackup
), lstrlenW(This
->txtbackup
));
544 if ((! *hwndText
) && (This
->options
& ACO_AUTOSUGGEST
)) {
545 ShowWindow(This
->hwndListBox
, SW_HIDE
);
546 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
548 if (This
->options
& ACO_AUTOAPPEND
) {
550 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
552 hwndText
[b
-1] = '\0';
555 SetWindowTextW(hwnd
, hwndText
);
563 SendMessageW(This
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
565 HeapFree(GetProcessHeap(), 0, This
->txtbackup
);
566 This
->txtbackup
= HeapAlloc(GetProcessHeap(),
567 HEAP_ZERO_MEMORY
, (lstrlenW(hwndText
)+1)*sizeof(WCHAR
));
568 lstrcpyW(This
->txtbackup
, hwndText
);
570 /* Returns if there is no text to search and we doesn't want to display all the entries */
571 if ((!displayall
) && (! *hwndText
) )
574 IEnumString_Reset(This
->enumstr
);
577 hr
= IEnumString_Next(This
->enumstr
, 1, &strs
, NULL
);
581 if (strstrW(strs
, hwndText
) == strs
) {
582 if (This
->options
& ACO_AUTOAPPEND
) {
583 SetWindowTextW(hwnd
, strs
);
584 SendMessageW(hwnd
, EM_SETSEL
, lstrlenW(hwndText
), lstrlenW(strs
));
588 if (This
->options
& ACO_AUTOSUGGEST
) {
589 SendMessageW(This
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)strs
);
596 if (This
->options
& ACO_AUTOSUGGEST
) {
598 height
= SendMessageW(This
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
599 SendMessageW(This
->hwndListBox
, LB_CARETOFF
, 0, 0);
600 GetWindowRect(hwnd
, &r
);
601 SetParent(This
->hwndListBox
, HWND_DESKTOP
);
602 /* It seems that Windows XP displays 7 lines at most
603 and otherwise displays a vertical scroll bar */
604 SetWindowPos(This
->hwndListBox
, HWND_TOP
,
605 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
*(cpt
+1)),
608 ShowWindow(This
->hwndListBox
, SW_HIDE
);
614 return CallWindowProcW(This
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
621 static LRESULT APIENTRY
ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
623 IAutoCompleteImpl
*This
= (IAutoCompleteImpl
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
629 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
630 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
633 sel
= SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
636 len
= SendMessageW(This
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
637 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+1)*sizeof(WCHAR
));
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
);