-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchCandidateProvider.cpp
217 lines (172 loc) · 6.16 KB
/
SearchCandidateProvider.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
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "private.h"
#include "SearchCandidateProvider.h"
#include <new>
#include "SampleIME.h"
#include "CompositionProcessorEngine.h"
#include "TipCandidateList.h"
#include "TipCandidateString.h"
/*------------------------------------------------------------------------------
create instance of CSearchCandidateProvider
------------------------------------------------------------------------------*/
HRESULT CSearchCandidateProvider::CreateInstance(_Outptr_ ITfFnSearchCandidateProvider **ppobj,
_In_ ITfTextInputProcessorEx *ptip)
{
if (ppobj == nullptr)
{
return E_INVALIDARG;
}
*ppobj = nullptr;
*ppobj = new (std::nothrow) CSearchCandidateProvider(ptip);
if (nullptr == *ppobj)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
/*------------------------------------------------------------------------------
create instance of CSearchCandidateProvider
------------------------------------------------------------------------------*/
HRESULT CSearchCandidateProvider::CreateInstance(REFIID riid, _Outptr_ void **ppvObj,
_In_ ITfTextInputProcessorEx *ptip)
{
if (ppvObj == nullptr)
{
return E_INVALIDARG;
}
*ppvObj = nullptr;
*ppvObj = new (std::nothrow) CSearchCandidateProvider(ptip);
if (nullptr == *ppvObj)
{
return E_OUTOFMEMORY;
}
return ((CSearchCandidateProvider *)(*ppvObj))->QueryInterface(riid, ppvObj);
}
/*------------------------------------------------------------------------------
constructor of CSearchCandidateProvider
------------------------------------------------------------------------------*/
CSearchCandidateProvider::CSearchCandidateProvider(_In_ ITfTextInputProcessorEx *ptip)
{
assert(ptip != nullptr);
_pTip = ptip;
_refCount = 0;
}
/*------------------------------------------------------------------------------
destructor of CSearchCandidateProvider
------------------------------------------------------------------------------*/
CSearchCandidateProvider::~CSearchCandidateProvider(void)
{
}
/*------------------------------------------------------------------------------
query interface
(IUnknown method)
------------------------------------------------------------------------------*/
STDMETHODIMP CSearchCandidateProvider::QueryInterface(REFIID riid, _Outptr_ void **ppvObj)
{
if (ppvObj == nullptr)
{
return E_POINTER;
}
*ppvObj = nullptr;
if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, __uuidof(ITfFnSearchCandidateProvider)))
{
*ppvObj = (ITfFnSearchCandidateProvider *)this;
}
else if (IsEqualIID(riid, IID_ITfFunction))
{
*ppvObj = (ITfFunction *)this;
}
if (*ppvObj == nullptr)
{
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
/*------------------------------------------------------------------------------
increment reference count
(IUnknown method)
------------------------------------------------------------------------------*/
STDMETHODIMP_(ULONG) CSearchCandidateProvider::AddRef()
{
return (ULONG)InterlockedIncrement(&_refCount);
}
/*------------------------------------------------------------------------------
decrement reference count and release object
(IUnknown method)
------------------------------------------------------------------------------*/
STDMETHODIMP_(ULONG) CSearchCandidateProvider::Release()
{
ULONG ref = (ULONG)InterlockedDecrement(&_refCount);
if (0 < ref)
{
return ref;
}
delete this;
return 0;
}
STDMETHODIMP CSearchCandidateProvider::GetDisplayName(_Out_ BSTR *pbstrName)
{
if (pbstrName == nullptr)
{
return E_INVALIDARG;
}
*pbstrName = SysAllocString(L"SearchCandidateProvider");
return S_OK;
}
STDMETHODIMP CSearchCandidateProvider::GetSearchCandidates(BSTR bstrQuery, BSTR bstrApplicationID,
_Outptr_result_maybenull_ ITfCandidateList **pplist)
{
bstrApplicationID;
bstrQuery;
HRESULT hr = E_FAIL;
*pplist = nullptr;
if (nullptr == _pTip)
{
return hr;
}
CCompositionProcessorEngine *pCompositionProcessorEngine = ((CSampleIME *)_pTip)->GetCompositionProcessorEngine();
if (nullptr == pCompositionProcessorEngine)
{
return hr;
}
CSampleImeArray<CCandidateListItem> candidateList;
pCompositionProcessorEngine->GetCandidateList(&candidateList, TRUE, FALSE);
int cCand = min(candidateList.Count(), FAKECANDIDATENUMBER);
if (0 < cCand)
{
hr = CTipCandidateList::CreateInstance(pplist, cCand);
if (FAILED(hr))
{
return hr;
}
for (int iCand = 0; iCand < cCand; iCand++)
{
ITfCandidateString *pCandStr = nullptr;
CTipCandidateString::CreateInstance(IID_ITfCandidateString, (void **)&pCandStr);
((CTipCandidateString *)pCandStr)->SetIndex(iCand);
((CTipCandidateString *)pCandStr)
->SetString(candidateList.GetAt(iCand)->_ItemString.Get(),
candidateList.GetAt(iCand)->_ItemString.GetLength());
((CTipCandidateList *)(*pplist))->SetCandidate(&pCandStr);
}
}
hr = S_OK;
return hr;
}
/*------------------------------------------------------------------------------
set result
(ITfFnSearchCandidateProvider method)
------------------------------------------------------------------------------*/
STDMETHODIMP CSearchCandidateProvider::SetResult(BSTR bstrQuery, BSTR bstrApplicationID, BSTR bstrResult)
{
bstrQuery;
bstrApplicationID;
bstrResult;
return E_NOTIMPL;
}