forked from jrywu/DIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CandidateHandler.cpp
371 lines (311 loc) · 11.7 KB
/
CandidateHandler.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//#define DEBUG_PRINT
#include "DIME.h"
#include "BaseStructure.h"
#include "UIPresenter.h"
#include "CompositionProcessorEngine.h"
//+---------------------------------------------------------------------------
//
// _HandleCandidateFinalize
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateFinalize(TfEditCookie ec, _In_ ITfContext *pContext)
{
return _HandleCandidateWorker(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateConvert
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateConvert(TfEditCookie ec, _In_ ITfContext *pContext)
{
return _HandleCandidateWorker(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateWorker
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateWorker(TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_HandleCandidateWorker() \n");
HRESULT hr = S_OK;
CStringRange commitString, convertedString;
CDIMEArray<CCandidateListItem> candidatePhraseList;
CStringRange lastChar;
CStringRange notify;
if (nullptr == _pUIPresenter)
{
goto Exit; //should not happen
}
const WCHAR* pCandidateString = nullptr;
DWORD_PTR candidateLen = 0;
BOOL arrayUsingSPCode =FALSE;
if (!_IsComposing())
_StartComposition(pContext);
if (Global::imeMode == IME_MODE_ARRAY)// check if the _strokebuffer is array special code
{
candidateLen = _pCompositionProcessorEngine->CollectWordFromArraySpeicalCode(&pCandidateString);
if(candidateLen) arrayUsingSPCode = TRUE;
}
candidateLen = 0;
candidateLen = _pUIPresenter->_GetSelectedCandidateString(&pCandidateString);
if (candidateLen == 0)
{
if(_candidateMode == CANDIDATE_WITH_NEXT_COMPOSITION || _candidateMode == CANDIDATE_PHRASE)
{
_HandleCancel(ec, pContext);
goto Exit;
}
else
{
hr = S_FALSE;
_HandleCancel(ec, pContext);
DoBeep(BEEP_COMPOSITION_ERROR); //beep for no valid mapping found
goto Exit;
}
}
if (_pCompositionProcessorEngine->IsArrayShortCode() && candidateLen == 1 && *pCandidateString == 0x2394) // empty position in arry short code table.
{
hr = S_FALSE;
if (Global::imeMode == IME_MODE_PHONETIC)
DoBeep(BEEP_WARNING);
else
{
if (CConfig::GetClearOnBeep()) _HandleCancel(ec, pContext);
DoBeep(BEEP_COMPOSITION_ERROR); //beep for no valid mapping found
}
goto Exit;
}
StringCchCopy(_commitString, 1, L"\0");
StringCchCatN(_commitString, MAX_COMMIT_LENGTH, pCandidateString, candidateLen);
commitString.Set(_commitString, candidateLen);
//_commitString = commitString;
PWCHAR pwch = new (std::nothrow) WCHAR[2]; // pCandidateString will be destroyed after _detelteCanddiateList was called.
if(candidateLen > 1)
{
StringCchCopyN(pwch, 2, pCandidateString + candidateLen -1, 1);
}else // cnadidateLen ==1
{
StringCchCopyN(pwch, 2, pCandidateString, 1);
}
lastChar.Set(pwch, 1 );
//-----------------do reverse conversion notify. We should not show notify in UI-less mode, thus cancel reverse conversion notify in UILess Mode
if (!_IsUILessMode())
{
if (_pITfReverseConversion[Global::imeMode])
{
_AsyncReverseConversion(pContext); //asynchronized the reverse conversion with editsession for better perfomance
}
//-----------------or complete real key code when input with wildcard
else if (_isCandidateWithWildcard)
{
const WCHAR* pCandidateKeyCode = nullptr;
DWORD_PTR keyCodeLen = _pUIPresenter->_GetSelectedCandidateKeyCode(&pCandidateKeyCode);
StringCchCopy(_commitKeyCode, 1, L"\0");
StringCchCatN(_commitKeyCode, MAX_KEY_LENGTH, pCandidateKeyCode, keyCodeLen);
CStringRange commitKeyCode, convertedKeyCode;
_pCompositionProcessorEngine->GetReadingString(&convertedKeyCode, NULL, &commitKeyCode.Set(_commitKeyCode, wcslen(_commitKeyCode)));
_pUIPresenter->ShowNotifyText(&convertedKeyCode);
}
}
//-----------------do array spcial code notify. We should not show notify in UI-less mode, thus cancel forceSP mode in UILess Mode---
BOOL ArraySPFound = FALSE;
if(Global::imeMode == IME_MODE_ARRAY && !_IsUILessMode() && !arrayUsingSPCode && (CConfig::GetArrayForceSP() || CConfig::GetArrayNotifySP()))
{
CStringRange specialCode;
CStringRange notifyText;
ArraySPFound = _pCompositionProcessorEngine->GetArraySpeicalCodeFromConvertedText(&commitString, &specialCode);
if(specialCode.Get())
_pUIPresenter->ShowNotifyText(&specialCode);
}
convertedString = commitString;
//----------------- do TC to SC covert if required----------------------------------------------------------------------------------
if(CConfig::GetDoHanConvert())
{
_pCompositionProcessorEngine->GetSCFromTC(&commitString, &convertedString);
}
//----------------- commit the selected string ------------------------------------------------------------------------------------
if(Global::imeMode == IME_MODE_ARRAY && !_IsUILessMode() && !arrayUsingSPCode && CConfig::GetArrayForceSP() && ArraySPFound )
{
_HandleCancel(ec, pContext);
DoBeep(BEEP_WARNING);
return hr;
}
else
{
hr = _AddComposingAndChar(ec, pContext, &convertedString);
if (FAILED(hr)) return hr;
// Do not send _endcandidatelist (or handleComplete) here to avoid cand dissapear in win8 metro
_HandleComplete(ec,pContext);
}
//-----------------do accociated phrase (make phrase)--------------------------------------------------------------------------------
if (CConfig::GetMakePhrase())
{
_pCompositionProcessorEngine->GetCandidateStringInConverted(lastChar, &candidatePhraseList);
// We have a candidate list if candidatePhraseList.Cnt is not 0
// If we are showing reverse conversion, use UIPresenter
if (candidatePhraseList.Count())
{
CStringRange emptyComposition;
if (!_IsComposing())
_StartComposition(pContext); //StartCandidateList require a valid selection from a valid pComposition to determine the location to show the candidate window
_AddComposingAndChar(ec, pContext, &emptyComposition.Set(L" ",1));
_pUIPresenter->_ClearCandidateList();
_pUIPresenter->_SetCandidateTextColor(CConfig::GetPhraseColor(), CConfig::GetItemBGColor()); // Text color is green
_pUIPresenter->_SetCandidateSelectedTextColor(CConfig::GetSelectedColor(), CConfig::GetSelectedBGColor());
_pUIPresenter->_SetCandidateNumberColor(CConfig::GetNumberColor(), CConfig::GetItemBGColor());
_pUIPresenter->_SetCandidateFillColor(CConfig::GetItemBGColor());//(HBRUSH)(COLOR_WINDOW+1)); // Background color is window
_pUIPresenter->_SetCandidateText(&candidatePhraseList, _pCompositionProcessorEngine->GetCandidateListIndexRange(),
TRUE, _pCompositionProcessorEngine->GetCandidateWindowWidth());
_pUIPresenter->_SetCandidateSelection(-1, FALSE); // set selected index to -1 if showing phrase candidates
_candidateMode = CANDIDATE_PHRASE;
_isCandidateWithWildcard = FALSE;
}
else
{ //cancel the composition if the phrase lookup return 0 results
_HandleCancel(ec,pContext);
}
}
else
_DeleteCandidateList(TRUE, pContext); // endCanddiateUI if not doing associated phrase.
Exit:
return hr;
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateArrowKey
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateArrowKey(TfEditCookie ec, _In_ ITfContext *pContext, _In_ KEYSTROKE_FUNCTION keyFunction)
{
ec;
pContext;
_pUIPresenter->AdviseUIChangedByArrowKey(keyFunction);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateSelectByNumber
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateSelectByNumber(TfEditCookie ec, _In_ ITfContext *pContext, _In_ UINT uCode)
{
int iSelectAsNumber = _pCompositionProcessorEngine->GetCandidateListIndexRange()->GetIndex(uCode, _candidateMode);
if (iSelectAsNumber == -1)
{
return S_FALSE;
}
if (_pUIPresenter)
{
if (_pUIPresenter->_SetCandidateSelectionInPage(iSelectAsNumber))
{
return _HandleCandidateConvert(ec, pContext);
}
}
return S_FALSE;
}
//+---------------------------------------------------------------------------
//
// _HandlePhraseFinalize
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandlePhraseFinalize(TfEditCookie ec, _In_ ITfContext *pContext)
{
HRESULT hr = S_OK;
DWORD phraseLen = 0;
const WCHAR* pPhraseString = nullptr;
phraseLen = (DWORD)_pUIPresenter->_GetSelectedCandidateString(&pPhraseString);
CStringRange phraseString, clearString;
phraseString.Set(pPhraseString, phraseLen);
if (phraseLen)
{
if ((hr = _AddComposingAndChar(ec, pContext, &phraseString)) != S_OK)
{
return hr;
}
}
_HandleComplete(ec, pContext);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandlePhraseArrowKey
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandlePhraseArrowKey(TfEditCookie ec, _In_ ITfContext *pContext, _In_ KEYSTROKE_FUNCTION keyFunction)
{
ec;
pContext;
_pUIPresenter->AdviseUIChangedByArrowKey(keyFunction);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandlePhraseSelectByNumber
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandlePhraseSelectByNumber(TfEditCookie ec, _In_ ITfContext *pContext, _In_ UINT uCode)
{
int iSelectAsNumber = _pCompositionProcessorEngine->GetCandidateListIndexRange()->GetIndex(uCode, _candidateMode);
if (iSelectAsNumber == -1)
{
return S_FALSE;
}
if (_pUIPresenter)
{
if (_pUIPresenter->_SetCandidateSelectionInPage(iSelectAsNumber))
{
return _HandlePhraseFinalize(ec, pContext);
}
}
return S_FALSE;
}
//+---------------------------------------------------------------------------
//
// _CreateAndStartCandidate
//
//----------------------------------------------------------------------------
HRESULT CDIME::_CreateAndStartCandidate(_In_ CCompositionProcessorEngine *pCompositionProcessorEngine, TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_CreateAndStartCandidate(), _candidateMode = %d", _candidateMode);
HRESULT hr = S_OK;
if ((_candidateMode == CANDIDATE_NONE) && (_pUIPresenter))
{
// we don't cache the document manager object. So get it from pContext.
ITfDocumentMgr* pDocumentMgr = nullptr;
if (SUCCEEDED(pContext->GetDocumentMgr(&pDocumentMgr)))
{
// get the composition range.
ITfRange* pRange = nullptr;
if (SUCCEEDED(_pComposition->GetRange(&pRange)))
{
hr = _pUIPresenter->_StartCandidateList(_tfClientId, pDocumentMgr, pContext, ec, pRange, pCompositionProcessorEngine->GetCandidateWindowWidth());
pRange->Release();
}
pDocumentMgr->Release();
}
}
return hr;
}
//+---------------------------------------------------------------------------
//
// _DeleteCandidateList
//
//----------------------------------------------------------------------------
VOID CDIME::_DeleteCandidateList(BOOL isForce, _In_opt_ ITfContext *pContext)
{
//isForce;
pContext;
debugPrint(L"CDIME::_DeleteCandidateList()\n");
if(_pCompositionProcessorEngine)
{
_pCompositionProcessorEngine->PurgeVirtualKey();
}
if (_pUIPresenter && isForce)
{
_pUIPresenter->_EndCandidateList();
}
_candidateMode = CANDIDATE_NONE;
_isCandidateWithWildcard = FALSE;
}