-
Notifications
You must be signed in to change notification settings - Fork 19
/
AutoComplete.cpp
283 lines (244 loc) · 6.73 KB
/
AutoComplete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// sktoolslib - common files for SK tools
// Copyright (C) 2012-2013, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "AutoComplete.h"
#include <shlguid.h>
#include <shobjidl.h>
CAutoComplete::CAutoComplete(CSimpleIni* pIni)
: CRegHistory(pIni)
, m_pcacs(nullptr)
, m_pac(nullptr)
, m_pdrop(nullptr)
{
}
CAutoComplete::~CAutoComplete()
{
if (m_pac)
m_pac->Release();
if (m_pcacs)
m_pcacs->Release();
if (m_pdrop)
m_pdrop->Release();
}
bool CAutoComplete::Init(HWND hEdit)
{
if (m_pac)
m_pac->Release();
if (m_pdrop)
m_pdrop->Release();
if (CoCreateInstance(CLSID_AutoComplete,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IAutoComplete2,
reinterpret_cast<LPVOID*>(&m_pac)) == S_OK)
{
IUnknown* punkSource;
if (m_pcacs)
delete m_pcacs;
m_pcacs = new CAutoCompleteEnum(m_arEntries);
if (m_pcacs->QueryInterface(IID_IUnknown,
reinterpret_cast<void**>(&punkSource)) == S_OK)
{
if (m_pac->Init(hEdit, punkSource, nullptr, nullptr) == S_OK)
{
m_pac->Enable(TRUE);
m_pac->SetOptions(ACO_UPDOWNKEYDROPSLIST | ACO_AUTOSUGGEST);
if (m_pac->QueryInterface(IID_IAutoCompleteDropDown, reinterpret_cast<void**>(&m_pdrop)) != S_OK)
{
m_pdrop = nullptr;
}
return true;
}
}
else
{
delete m_pcacs;
m_pcacs = nullptr;
}
}
return false;
}
bool CAutoComplete::Enable(bool bEnable) const
{
if (m_pac)
{
if (m_pac->Enable(bEnable) == S_OK)
return true;
}
return false;
}
bool CAutoComplete::AddEntry(LPCWSTR szText)
{
bool bRet = CRegHistory::AddEntry(szText);
if (m_pcacs)
m_pcacs->Init(m_arEntries);
if (m_pdrop)
m_pdrop->ResetEnumerator();
return bRet;
}
bool CAutoComplete::RemoveSelected()
{
if (m_pdrop == nullptr)
return false;
if (m_pcacs == nullptr)
return false;
DWORD flags;
LPWSTR string = nullptr;
if (m_pdrop->GetDropDownStatus(&flags, &string) == S_OK)
{
if (flags & ACDD_VISIBLE)
{
RemoveEntry(string);
// need to save the history now, otherwise adding a new
// entry with AddEntry() will reload the history from the
// registry, and our removed item would get restored
Save();
if (string)
CoTaskMemFree(string);
m_pcacs->Init(m_arEntries);
m_pdrop->ResetEnumerator();
}
else
{
if (string)
CoTaskMemFree(string);
return false;
}
}
return true;
}
void CAutoComplete::SetOptions(DWORD dwFlags) const
{
if (m_pac)
{
m_pac->SetOptions(dwFlags);
if (m_pcacs)
m_pcacs->Init(m_arEntries);
if (m_pdrop)
m_pdrop->ResetEnumerator();
}
}
DWORD CAutoComplete::GetOptions() const
{
DWORD dwFlags = 0;
if (m_pac)
{
m_pac->GetOptions(&dwFlags);
}
return dwFlags;
}
CAutoCompleteEnum::CAutoCompleteEnum(const std::vector<std::wstring>& vec)
: m_cRefCount(0)
, m_iCur(0)
{
Init(vec);
}
CAutoCompleteEnum::CAutoCompleteEnum(const std::vector<std::wstring*>& vec)
: m_cRefCount(0)
, m_iCur(0)
{
Init(vec);
}
void CAutoCompleteEnum::Init(const std::vector<std::wstring>& vec)
{
m_vecStrings.clear();
for (size_t i = 0; i < vec.size(); ++i)
m_vecStrings.push_back(vec[i]);
}
void CAutoCompleteEnum::Init(const std::vector<std::wstring*>& vec)
{
m_vecStrings.clear();
for (size_t i = 0; i < vec.size(); ++i)
m_vecStrings.push_back(*vec[i]);
}
STDMETHODIMP CAutoCompleteEnum::QueryInterface(REFIID refiid, void** ppv)
{
*ppv = nullptr;
if (IID_IUnknown == refiid || IID_IEnumString == refiid)
*ppv = this;
if (*ppv != nullptr)
{
static_cast<LPUNKNOWN>(*ppv)->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG)
CAutoCompleteEnum::AddRef()
{
return ++m_cRefCount;
}
STDMETHODIMP_(ULONG)
CAutoCompleteEnum::Release()
{
--m_cRefCount;
if (m_cRefCount == 0)
{
delete this;
return 0;
}
return m_cRefCount;
}
STDMETHODIMP CAutoCompleteEnum::Next(ULONG celt, LPOLESTR* rgelt, ULONG* pceltFetched)
{
HRESULT hr = S_FALSE;
if (!celt)
celt = 1;
if (pceltFetched)
*pceltFetched = 0;
ULONG i = 0;
for (; i < celt; i++)
{
if (m_iCur == static_cast<ULONG>(m_vecStrings.size()))
break;
rgelt[i] = static_cast<LPWSTR>(::CoTaskMemAlloc(static_cast<ULONG>(sizeof(WCHAR)) * (m_vecStrings[m_iCur].size() + 1)));
wcscpy_s(rgelt[i], m_vecStrings[m_iCur].size() + 1, m_vecStrings[m_iCur].c_str());
if (pceltFetched)
(*pceltFetched)++;
m_iCur++;
}
if (i == celt)
hr = S_OK;
return hr;
}
STDMETHODIMP CAutoCompleteEnum::Skip(ULONG celt)
{
if ((m_iCur + static_cast<int>(celt)) >= m_vecStrings.size())
return S_FALSE;
m_iCur += celt;
return S_OK;
}
STDMETHODIMP CAutoCompleteEnum::Reset()
{
m_iCur = 0;
return S_OK;
}
STDMETHODIMP CAutoCompleteEnum::Clone(IEnumString** ppenum)
{
if (ppenum == nullptr)
return E_POINTER;
try
{
CAutoCompleteEnum* newEnum = new CAutoCompleteEnum(m_vecStrings);
newEnum->AddRef();
newEnum->m_iCur = m_iCur;
*ppenum = newEnum;
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
return S_OK;
}