-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCandidateHandler.cpp
425 lines (352 loc) · 14.3 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* DIME IME for Windows 7/8/10/11
BSD 3-Clause License
Copyright (c) 2022, Jeremy Wu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//#define DEBUG_PRINT
#include "DIME.h"
#include "BaseStructure.h"
#include "UIPresenter.h"
#include "CompositionProcessorEngine.h"
//+---------------------------------------------------------------------------
//
// _HandleCandidateFinalize
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateFinalize(TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_HandleCandidateFinalize()");
return _HandleCandidateWorker(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateConvert
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateConvert(TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_HandleCandidateFinalize()");
return _HandleCandidateWorker(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateWorker
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateWorker(TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_HandleCandidateWorker() _IsComposing()= %d", _IsComposing());
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);
candidateLen = _pUIPresenter->_GetSelectedCandidateString(&pCandidateString);
if (Global::imeMode == IME_MODE::IME_MODE_ARRAY && CConfig::GetArrayScope() != ARRAY_SCOPE::ARRAY40_BIG5)
// check if the _strokebuffer is array special code
{
if(_pCompositionProcessorEngine->CollectWordFromArraySpeicalCode(&pCandidateString))
arrayUsingSPCode = TRUE;
}
if (candidateLen == 0)
{
//if(_candidateMode == CANDIDATE_WITH_NEXT_COMPOSITION || _candidateMode == CANDIDATE_PHRASE)
if(_candidateMode == CANDIDATE_MODE::CANDIDATE_PHRASE)
{
_HandleCancel(ec, pContext);
goto Exit;
}
else
{
hr = S_FALSE;
_HandleCancel(ec, pContext);
DoBeep(BEEP_TYPE::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::IME_MODE_PHONETIC)
DoBeep(BEEP_TYPE::BEEP_WARNING);
else
{
if (CConfig::GetClearOnBeep()) _HandleCancel(ec, pContext);
DoBeep(BEEP_TYPE::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 (pwch == NULL) //should not happen.
goto Exit;
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[(UINT)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");
if(pCandidateKeyCode)
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::IME_MODE_ARRAY && CConfig::GetArrayScope() != ARRAY_SCOPE::ARRAY40_BIG5 &&
!_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::IME_MODE_ARRAY && !_IsUILessMode() && !arrayUsingSPCode && CConfig::GetArrayForceSP() && ArraySPFound )
{
_HandleCancel(ec, pContext);
DoBeep(BEEP_TYPE::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)--------------------------------------------------------------------------------
// Issue #27. Turn off associated phrase to avoid cursor gone in command shell.
if (CConfig::GetMakePhrase() && !isCMDShell())
{
_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())
{
_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(),
FALSE, _pCompositionProcessorEngine->GetCandidateWindowWidth());
_pUIPresenter->_SetCandidateSelection(-1, FALSE); // set selected index to -1 if showing phrase candidates
_candidateMode = CANDIDATE_MODE::CANDIDATE_PHRASE;
_isCandidateWithWildcard = FALSE;
//StartCandidateList require a valid selection from a valid pComposition to determine the location to show the candidate window
//Moved after setCandidateText or candidatePhraseList can be corrupt after probe comosition.
CStringRange emptyComposition;
if (!_IsComposing())
_StartComposition(pContext);
_AddComposingAndChar(ec, pContext, &emptyComposition.Set(L" ",1));
}
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)
{
debugPrint(L"CDIME::_HandleCandidateArrowKey()");
ec;
pContext;
_pUIPresenter->AdviseUIChangedByArrowKey(keyFunction);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandleCandidateSelectByNumber
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandleCandidateSelectByNumber(TfEditCookie ec, _In_ ITfContext *pContext, _In_ UINT uCode,_In_ WCHAR wch)
{
debugPrint(L"CDIME::_HandleCandidateSelectByNumber() \n");
int iSelectAsNumber = _pCompositionProcessorEngine->GetCandidateListIndexRange()->GetIndex(uCode, wch, _candidateMode);
debugPrint(L"CDIME::_HandleCandidateSelectByNumber() iSelectAsNumber = %d", iSelectAsNumber);
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)
{
debugPrint(L"CDIME::_HandlePhraseFinalize() ");
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)
{
debugPrint(L"CDIME::_HandlePhraseArrowKey() ");
ec;
pContext;
_pUIPresenter->AdviseUIChangedByArrowKey(keyFunction);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandlePhraseSelectByNumber
//
//----------------------------------------------------------------------------
HRESULT CDIME::_HandlePhraseSelectByNumber(TfEditCookie ec, _In_ ITfContext *pContext, _In_ UINT uCode, _In_ WCHAR wch)
{
debugPrint(L"CDIME::_HandlePhraseSelectByNumber() ");
int iSelectAsNumber = _pCompositionProcessorEngine->GetCandidateListIndexRange()->GetIndex(uCode, wch, _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_MODE::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_MODE::CANDIDATE_NONE;
_isCandidateWithWildcard = FALSE;
}