2 * AutoComplete interfaces implementation.
4 * Copyright 2004 Maxime Bellengé <maxime.bellenge@laposte.net>
5 * Copyright 2009 Andrew Hill
6 * Copyright 2020 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 - ACO_AUTOAPPEND style
26 - ACO_AUTOSUGGEST style
27 - ACO_UPDOWNKEYDROPSLIST style
29 - Handle pwzsRegKeyPath and pwszQuickComplete in Init
32 - implement ACO_SEARCH style
33 - implement ACO_FILTERPREFIXES style
34 - implement ACO_USETAB style
35 - implement ACO_RTLREADING style
41 static const WCHAR autocomplete_propertyW
[] = {'W','i','n','e',' ','A','u','t','o',
42 'c','o','m','p','l','e','t','e',' ',
43 'c','o','n','t','r','o','l',0};
45 /**************************************************************************
46 * IAutoComplete_Constructor
48 CAutoComplete::CAutoComplete()
52 options
= ACO_AUTOAPPEND
;
53 wpOrigEditProc
= NULL
;
58 wpOrigLBoxProc
= NULL
;
61 /**************************************************************************
62 * IAutoComplete_Destructor
64 CAutoComplete::~CAutoComplete()
66 TRACE(" destroying IAutoComplete(%p)\n", this);
67 HeapFree(GetProcessHeap(), 0, quickComplete
);
68 HeapFree(GetProcessHeap(), 0, txtbackup
);
71 SetWindowLongPtrW(hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
)wpOrigEditProc
);
72 RemovePropW(hwndEdit
, autocomplete_propertyW
);
75 DestroyWindow(hwndListBox
);
78 /******************************************************************************
79 * IAutoComplete_fnEnable
81 HRESULT WINAPI
CAutoComplete::Enable(BOOL fEnable
)
85 TRACE("(%p)->(%s)\n", this, (fEnable
) ? "true" : "false");
92 /******************************************************************************
95 void CAutoComplete::CreateListbox()
97 HWND hwndParent
= GetParent(hwndEdit
);
99 /* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
100 hwndListBox
= CreateWindowExW(0, WC_LISTBOXW
, NULL
,
101 WS_BORDER
| WS_CHILD
| WS_VSCROLL
| LBS_HASSTRINGS
| LBS_NOTIFY
| LBS_NOINTEGRALHEIGHT
,
102 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
104 (HINSTANCE
)GetWindowLongPtrW(hwndParent
, GWLP_HINSTANCE
), NULL
);
108 wpOrigLBoxProc
= (WNDPROC
)SetWindowLongPtrW(hwndListBox
, GWLP_WNDPROC
, (LONG_PTR
)ACLBoxSubclassProc
);
109 SetWindowLongPtrW(hwndListBox
, GWLP_USERDATA
, (LONG_PTR
)this);
114 /******************************************************************************
115 * IAutoComplete_fnInit
117 HRESULT WINAPI
CAutoComplete::Init(HWND hwndEdit
, IUnknown
*punkACL
, LPCOLESTR pwzsRegKeyPath
, LPCOLESTR pwszQuickComplete
)
119 TRACE("(%p)->(0x%08lx, %p, %s, %s)\n",
120 this, hwndEdit
, punkACL
, debugstr_w(pwzsRegKeyPath
), debugstr_w(pwszQuickComplete
));
122 if (options
& ACO_AUTOSUGGEST
)
123 TRACE(" ACO_AUTOSUGGEST\n");
124 if (options
& ACO_AUTOAPPEND
)
125 TRACE(" ACO_AUTOAPPEND\n");
126 if (options
& ACO_SEARCH
)
127 FIXME(" ACO_SEARCH not supported\n");
128 if (options
& ACO_FILTERPREFIXES
)
129 FIXME(" ACO_FILTERPREFIXES not supported\n");
130 if (options
& ACO_USETAB
)
131 FIXME(" ACO_USETAB not supported\n");
132 if (options
& ACO_UPDOWNKEYDROPSLIST
)
133 TRACE(" ACO_UPDOWNKEYDROPSLIST\n");
134 if (options
& ACO_RTLREADING
)
135 FIXME(" ACO_RTLREADING not supported\n");
137 if (!hwndEdit
|| !punkACL
)
140 if (this->initialized
)
142 WARN("Autocompletion object is already initialized\n");
143 /* This->hwndEdit is set to NULL when the edit window is destroyed. */
144 return this->hwndEdit
? E_FAIL
: E_UNEXPECTED
;
147 if (!SUCCEEDED(punkACL
->QueryInterface(IID_PPV_ARG(IEnumString
,&enumstr
))))
149 TRACE("No IEnumString interface\n");
150 return E_NOINTERFACE
;
153 this->hwndEdit
= hwndEdit
;
154 this->initialized
= TRUE
;
156 /* Keep at least one reference to the object until the edit window is destroyed. */
159 /* If another AutoComplete object was previously assigned to this edit control,
160 release it but keep the same callback on the control, to avoid an infinite
161 recursive loop in ACEditSubclassProc while the property is set to this object */
162 CAutoComplete
*prev
= static_cast<CAutoComplete
*>(GetPropW(hwndEdit
, autocomplete_propertyW
));
164 if (prev
&& prev
->initialized
)
166 this->wpOrigEditProc
= prev
->wpOrigEditProc
;
167 SetPropW(hwndEdit
, autocomplete_propertyW
, this);
168 prev
->wpOrigEditProc
= NULL
;
173 SetPropW( this->hwndEdit
, autocomplete_propertyW
, (HANDLE
)this );
174 this->wpOrigEditProc
= (WNDPROC
)SetWindowLongPtrW(hwndEdit
, GWLP_WNDPROC
, (LONG_PTR
)ACEditSubclassProc
);
177 if (options
& ACO_AUTOSUGGEST
)
179 this->CreateListbox();
185 WCHAR result
[MAX_PATH
];
191 /* pwszRegKeyPath contains the key as well as the value, so we split */
192 key
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (wcslen(pwzsRegKeyPath
) + 1) * sizeof(WCHAR
));
196 wcscpy(key
, pwzsRegKeyPath
);
197 value
= const_cast<WCHAR
*>(wcsrchr(key
, '\\'));
203 /* Now value contains the value and buffer the key */
204 res
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
, &hKey
);
206 if (res
!= ERROR_SUCCESS
)
208 /* if the key is not found, MSDN states we must seek in HKEY_LOCAL_MACHINE */
209 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, key
, 0, KEY_READ
, &hKey
);
212 if (res
== ERROR_SUCCESS
)
214 len
= sizeof(result
);
215 res
= RegQueryValueW(hKey
, value
, result
, &len
);
216 if (res
== ERROR_SUCCESS
)
218 quickComplete
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
219 wcscpy(quickComplete
, result
);
225 HeapFree(GetProcessHeap(), 0, key
);
229 TRACE("HeapAlloc Failed when trying to alloca %d bytes\n", (wcslen(pwzsRegKeyPath
) + 1) * sizeof(WCHAR
));
234 if ((pwszQuickComplete
) && (!quickComplete
))
236 quickComplete
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (wcslen(pwszQuickComplete
) + 1) * sizeof(WCHAR
));
240 wcscpy(quickComplete
, pwszQuickComplete
);
244 TRACE("HeapAlloc Failed when trying to alloca %d bytes\n", (wcslen(pwszQuickComplete
) + 1) * sizeof(WCHAR
));
252 /**************************************************************************
253 * IAutoComplete_fnGetOptions
255 HRESULT WINAPI
CAutoComplete::GetOptions(DWORD
*pdwFlag
)
259 TRACE("(%p) -> (%p)\n", this, pdwFlag
);
266 /**************************************************************************
267 * IAutoComplete_fnSetOptions
269 HRESULT WINAPI
CAutoComplete::SetOptions(DWORD dwFlag
)
273 TRACE("(%p) -> (0x%x)\n", this, dwFlag
);
275 options
= (AUTOCOMPLETEOPTIONS
)dwFlag
;
277 if ((options
& ACO_AUTOSUGGEST
) && hwndEdit
&& !hwndListBox
)
283 /* Edit_BackWord --- Delete previous word in text box */
284 static void Edit_BackWord(HWND hwndEdit
)
288 SendMessageW(hwndEdit
, EM_GETSEL
, (WPARAM
)&iStart
, (LPARAM
)&iEnd
);
290 if (iStart
!= iEnd
|| iStart
< 0)
293 size_t cchText
= GetWindowTextLengthW(hwndEdit
);
294 if (cchText
< (size_t)iStart
|| (INT
)cchText
<= 0)
297 CComHeapPtr
<WCHAR
> pszText
;
298 if (!pszText
.Allocate(cchText
+ 1))
301 if (GetWindowTextW(hwndEdit
, pszText
, cchText
+ 1) <= 0)
305 for (--iStart
; 0 < iStart
; --iStart
)
307 GetStringTypeW(CT_CTYPE1
, &pszText
[iStart
- 1], 2, types
);
308 if (((types
[0] & C1_PUNCT
) && !(types
[1] & C1_SPACE
)) ||
309 ((types
[0] & C1_SPACE
) && (types
[1] & (C1_ALPHA
| C1_DIGIT
))))
311 SendMessageW(hwndEdit
, EM_SETSEL
, iStart
, iEnd
);
312 SendMessageW(hwndEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)L
"");
319 SendMessageW(hwndEdit
, EM_SETSEL
, iStart
, iEnd
);
320 SendMessageW(hwndEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)L
"");
325 Window procedure for autocompletion
327 LRESULT APIENTRY
CAutoComplete::ACEditSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
329 CAutoComplete
*pThis
= static_cast<CAutoComplete
*>(GetPropW(hwnd
, autocomplete_propertyW
));
334 BOOL control
, filled
, displayall
= FALSE
;
335 int cpt
, height
, sel
;
340 return CallWindowProcW(pThis
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
345 case CB_SHOWDROPDOWN
:
347 ShowWindow(pThis
->hwndListBox
, SW_HIDE
);
352 if ((pThis
->options
& ACO_AUTOSUGGEST
) && ((HWND
)wParam
!= pThis
->hwndListBox
))
354 ShowWindow(pThis
->hwndListBox
, SW_HIDE
);
356 return CallWindowProcW(pThis
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
361 GetWindowTextW(hwnd
, (LPWSTR
)hwndText
, 255);
367 /* If quickComplete is set and control is pressed, replace the string */
368 control
= GetKeyState(VK_CONTROL
) & 0x8000;
369 if (control
&& pThis
->quickComplete
)
371 hwndQCText
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
372 (wcslen(pThis
->quickComplete
)+wcslen(hwndText
))*sizeof(WCHAR
));
373 sel
= swprintf(hwndQCText
, pThis
->quickComplete
, hwndText
);
374 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)hwndQCText
);
375 SendMessageW(hwnd
, EM_SETSEL
, 0, sel
);
376 HeapFree(GetProcessHeap(), 0, hwndQCText
);
379 ShowWindow(pThis
->hwndListBox
, SW_HIDE
);
393 - if the listbox is not visible, displays it
394 with all the entries if the style ACO_UPDOWNKEYDROPSLIST
395 is present but does not select anything.
396 - if the listbox is visible, change the selection
398 if ( (pThis
->options
& (ACO_AUTOSUGGEST
| ACO_UPDOWNKEYDROPSLIST
))
399 && (!IsWindowVisible(pThis
->hwndListBox
) && (! *hwndText
)) )
401 /* We must display all the entries */
406 if (IsWindowVisible(pThis
->hwndListBox
))
410 count
= SendMessageW(pThis
->hwndListBox
, LB_GETCOUNT
, 0, 0);
411 /* Change the selection */
412 sel
= SendMessageW(pThis
->hwndListBox
, LB_GETCURSEL
, 0, 0);
414 sel
= ((sel
-1) < 0) ? count
-1 : sel
-1;
416 sel
= ((sel
+1) >= count
) ? -1 : sel
+1;
418 SendMessageW(pThis
->hwndListBox
, LB_SETCURSEL
, sel
, 0);
425 len
= SendMessageW(pThis
->hwndListBox
, LB_GETTEXTLEN
, sel
, (LPARAM
)NULL
);
426 msg
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+ 1) * sizeof(WCHAR
));
430 SendMessageW(pThis
->hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
431 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)msg
);
432 SendMessageW(hwnd
, EM_SETSEL
, wcslen(msg
), wcslen(msg
));
434 HeapFree(GetProcessHeap(), 0, msg
);
438 TRACE("HeapAlloc failed to allocate %d bytes\n", (len
+ 1) * sizeof(WCHAR
));
443 SendMessageW(hwnd
, WM_SETTEXT
, 0, (LPARAM
)pThis
->txtbackup
);
444 SendMessageW(hwnd
, EM_SETSEL
, wcslen(pThis
->txtbackup
), wcslen(pThis
->txtbackup
));
453 if (GetKeyState(VK_CONTROL
) < 0) // Ctrl+Backspace
462 if ((! *hwndText
) && (pThis
->options
& ACO_AUTOSUGGEST
))
464 ShowWindow(pThis
->hwndListBox
, SW_HIDE
);
465 return CallWindowProcW(pThis
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
468 if (pThis
->options
& ACO_AUTOAPPEND
)
471 SendMessageW(hwnd
, EM_GETSEL
, (WPARAM
)&b
, (LPARAM
)NULL
);
474 hwndText
[b
-1] = '\0';
479 SetWindowTextW(hwnd
, hwndText
);
488 SendMessageW(pThis
->hwndListBox
, LB_RESETCONTENT
, 0, 0);
490 HeapFree(GetProcessHeap(), 0, pThis
->txtbackup
);
492 pThis
->txtbackup
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (wcslen(hwndText
)+1)*sizeof(WCHAR
));
494 if (pThis
->txtbackup
)
496 wcscpy(pThis
->txtbackup
, hwndText
);
500 TRACE("HeapAlloc failed to allocate %d bytes\n", (wcslen(hwndText
)+1)*sizeof(WCHAR
));
503 /* Returns if there is no text to search and we doesn't want to display all the entries */
504 if ((!displayall
) && (! *hwndText
) )
507 pThis
->enumstr
->Reset();
509 size_t curlen
= wcslen(hwndText
);
513 CComHeapPtr
<OLECHAR
> strs
;
514 hr
= pThis
->enumstr
->Next(1, &strs
, &fetched
);
518 if (!_wcsnicmp(hwndText
, strs
, curlen
))
521 if (pThis
->options
& ACO_AUTOAPPEND
&& *hwndText
)
523 CComBSTR
str((PCWSTR
)strs
);
524 memcpy(str
.m_str
, hwndText
, curlen
* sizeof(WCHAR
));
525 SetWindowTextW(hwnd
, str
);
526 SendMessageW(hwnd
, EM_SETSEL
, curlen
, str
.Length());
527 if (!(pThis
->options
& ACO_AUTOSUGGEST
))
531 if (pThis
->options
& ACO_AUTOSUGGEST
)
533 SendMessageW(pThis
->hwndListBox
, LB_ADDSTRING
, 0, (LPARAM
)(LPOLESTR
)strs
);
540 if (pThis
->options
& ACO_AUTOSUGGEST
)
544 height
= SendMessageW(pThis
->hwndListBox
, LB_GETITEMHEIGHT
, 0, 0);
545 SendMessageW(pThis
->hwndListBox
, LB_CARETOFF
, 0, 0);
546 GetWindowRect(hwnd
, &r
);
547 SetParent(pThis
->hwndListBox
, HWND_DESKTOP
);
548 /* It seems that Windows XP displays 7 lines at most
549 and otherwise displays a vertical scroll bar */
550 SetWindowPos(pThis
->hwndListBox
, HWND_TOP
,
551 r
.left
, r
.bottom
+ 1, r
.right
- r
.left
, min(height
* 7, height
* (cpt
+ 1)),
556 ShowWindow(pThis
->hwndListBox
, SW_HIDE
);
564 /* Release our reference that we had since ->Init() */
572 return CallWindowProcW(pThis
->wpOrigEditProc
, hwnd
, uMsg
, wParam
, lParam
);
580 LRESULT APIENTRY
CAutoComplete::ACLBoxSubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
582 CAutoComplete
*pThis
= reinterpret_cast<CAutoComplete
*>(GetWindowLongPtrW(hwnd
, GWLP_USERDATA
));
590 sel
= SendMessageW(hwnd
, LB_ITEMFROMPOINT
, 0, lParam
);
591 SendMessageW(hwnd
, LB_SETCURSEL
, (WPARAM
)sel
, (LPARAM
)0);
596 sel
= SendMessageW(hwnd
, LB_GETCURSEL
, 0, 0);
601 len
= SendMessageW(pThis
->hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
602 msg
= (WCHAR
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (len
+ 1) * sizeof(WCHAR
));
606 SendMessageW(hwnd
, LB_GETTEXT
, sel
, (LPARAM
)msg
);
607 SendMessageW(pThis
->hwndEdit
, WM_SETTEXT
, 0, (LPARAM
)msg
);
608 SendMessageW(pThis
->hwndEdit
, EM_SETSEL
, 0, wcslen(msg
));
609 ShowWindow(hwnd
, SW_HIDE
);
611 HeapFree(GetProcessHeap(), 0, msg
);
615 TRACE("HeapAlloc failed to allocate %d bytes\n", (len
+ 1) * sizeof(WCHAR
));
621 return CallWindowProcW(pThis
->wpOrigLBoxProc
, hwnd
, uMsg
, wParam
, lParam
);
626 /**************************************************************************
627 * IAutoCompleteDropDown
629 HRESULT STDMETHODCALLTYPE
CAutoComplete::GetDropDownStatus(DWORD
*pdwFlags
, LPWSTR
*ppwszString
)
631 BOOL dropped
= IsWindowVisible(hwndListBox
);
634 *pdwFlags
= (dropped
? ACDD_VISIBLE
: 0);
642 int sel
= SendMessageW(hwndListBox
, LB_GETCURSEL
, 0, 0);
645 DWORD len
= SendMessageW(hwndListBox
, LB_GETTEXTLEN
, sel
, 0);
646 *ppwszString
= (LPWSTR
)CoTaskMemAlloc((len
+1)*sizeof(WCHAR
));
647 SendMessageW(hwndListBox
, LB_GETTEXT
, sel
, (LPARAM
)*ppwszString
);
655 HRESULT STDMETHODCALLTYPE
CAutoComplete::ResetEnumerator()
657 FIXME("(%p): stub\n", this);
661 /**************************************************************************
664 HRESULT STDMETHODCALLTYPE
CAutoComplete::Next(ULONG celt
, LPOLESTR
*rgelt
, ULONG
*pceltFetched
)
666 FIXME("(%p, %d, %p, %p): stub\n", this, celt
, rgelt
, pceltFetched
);
671 HRESULT STDMETHODCALLTYPE
CAutoComplete::Skip(ULONG celt
)
673 FIXME("(%p, %d): stub\n", this, celt
);
677 HRESULT STDMETHODCALLTYPE
CAutoComplete::Reset()
679 FIXME("(%p): stub\n", this);
683 HRESULT STDMETHODCALLTYPE
CAutoComplete::Clone(IEnumString
**ppOut
)
685 FIXME("(%p, %p): stub\n", this, ppOut
);