forked from jrywu/DIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composition.cpp
609 lines (500 loc) · 17.9 KB
/
Composition.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//
//
// Derived from Microsoft Sample IME by Jeremy '13,7,17
//
//
//#define DEBUG_PRINT
#include "Private.h"
#include "Globals.h"
#include "DIME.h"
#include "CompositionProcessorEngine.h"
#include "UIPresenter.h"
#include "GetTextExtentEditSession.h"
//+---------------------------------------------------------------------------
//
// ITfCompositionSink::OnCompositionTerminated
//
// Callback for ITfCompositionSink. The system calls this method whenever
// someone other than this service ends a composition.
//----------------------------------------------------------------------------
STDAPI CDIME::OnCompositionTerminated(TfEditCookie ecWrite, _In_ ITfComposition *pComposition)
{
debugPrint(L"CDIME::OnCompositionTerminated()");
HRESULT hr = S_OK;
ITfContext* pContext = _pContext;
// Clear dummy composition
_RemoveDummyCompositionForComposing(ecWrite, pComposition);
// Clear display attribute and end composition, _EndComposition will release composition for us
if (pContext)
{
pContext->AddRef();
}
_EndComposition(pContext);
_DeleteCandidateList(TRUE, pContext);
if (pContext)
{
pContext->Release();
pContext = nullptr;
}
return hr;
}
//+---------------------------------------------------------------------------
//
// _IsComposing
//
//----------------------------------------------------------------------------
BOOL CDIME::_IsComposing()
{
return _pComposition != nullptr;
}
//+---------------------------------------------------------------------------
//
// _SetComposition
//
//----------------------------------------------------------------------------
void CDIME::_SetComposition(_In_ ITfComposition *pComposition)
{
_pComposition = pComposition;
lastReadingString.Set(nullptr, 0); //reset lastReadingString
}
//+---------------------------------------------------------------------------
//
// _AddComposingAndChar
//
//----------------------------------------------------------------------------
HRESULT CDIME::_AddComposingAndChar(TfEditCookie ec, _In_ ITfContext *pContext, _In_ CStringRange *pstrAddString)
{
debugPrint(L"CDIME::_AddComposingAndChar() reading string = %s", pstrAddString->Get());
HRESULT hr = S_OK;
ULONG fetched = 0;
TF_SELECTION tfSelection;
if (pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &fetched) != S_OK || fetched == 0)
return S_FALSE;
//
// make range start to selection
//
ITfRange* pAheadSelection = nullptr;
hr = pContext->GetStart(ec, &pAheadSelection);
if (SUCCEEDED(hr) && pAheadSelection)
{
debugPrint(L"CDIME::_AddComposingAndChar() SUCCEEDED( pContext->GetStart( &pAheadSelection))");
hr = pAheadSelection->ShiftEndToRange(ec, tfSelection.range, TF_ANCHOR_START);
if (SUCCEEDED(hr))
{
debugPrint(L"CDIME::_AddComposingAndChar() SUCCEEDED( pAheadSelection->ShiftEndToRange())");
ITfRange* pRange = nullptr;
BOOL exist_composing = _FindComposingRange(ec, pContext, pAheadSelection, &pRange);
if (FAILED(_SetInputString(ec, pContext, pRange, pstrAddString, exist_composing)))
debugPrint(L"CDIME::_AddComposingAndChar() _SetInputString() failed.");
if (pRange)
pRange->Release();
lastReadingString = *pstrAddString;
}
}
tfSelection.range->Release();
if (pAheadSelection)
{
pAheadSelection->Release();
}
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _AddCharAndFinalize
//
//----------------------------------------------------------------------------
HRESULT CDIME::_AddCharAndFinalize(TfEditCookie ec, _In_ ITfContext *pContext, _In_ CStringRange *pstrAddString)
{
debugPrint(L"CDIME::_AddCharAndFinalize()");
HRESULT hr = E_FAIL;
ULONG fetched = 0;
TF_SELECTION tfSelection;
if ((hr = pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &fetched)) != S_OK || fetched != 1 || tfSelection.range==nullptr || pstrAddString ==nullptr)
return hr;
// we use SetText here instead of InsertTextAtSelection because we've already started a composition
// we don't want the app to adjust the insertion point inside our composition
hr = tfSelection.range->SetText(ec, 0, pstrAddString->Get(), (LONG)pstrAddString->GetLength());
if (hr == S_OK)
{
// update the selection, we'll make it an insertion point just past
// the inserted text.
tfSelection.range->Collapse(ec, TF_ANCHOR_END);
if(pContext)
pContext->SetSelection(ec, 1, &tfSelection);
}
tfSelection.range->Release();
return hr;
}
//+---------------------------------------------------------------------------
//
// _FindComposingRange
//
//----------------------------------------------------------------------------
BOOL CDIME::_FindComposingRange(TfEditCookie ec, _In_ ITfContext *pContext, _In_ ITfRange *pSelection, _Outptr_result_maybenull_ ITfRange **ppRange)
{
debugPrint(L"CDIME::_FindComposingRange()");
if (pContext == nullptr || ppRange == nullptr)
{
debugPrint(L"CDIME::_FindComposingRange() failed with null pContext or pRange");
return FALSE;
}
*ppRange = nullptr;
// find GUID_PROP_COMPOSING
ITfProperty* pPropComp = nullptr;
IEnumTfRanges* enumComp = nullptr;
HRESULT hr = pContext->GetProperty(GUID_PROP_COMPOSING, &pPropComp);
if (FAILED(hr) || pPropComp == nullptr)
{
if (FAILED(hr))
debugPrint(L"CDIME::_FindComposingRange() pContext->GetProperty() failed");
else
debugPrint(L"CDIME::_FindComposingRange() failed with null pPropComp");
return FALSE;
}
hr = pPropComp->EnumRanges(ec, &enumComp, pSelection);
if (FAILED(hr) || enumComp == nullptr)
{
if (FAILED(hr))
debugPrint(L"CDIME::_FindComposingRange() pPropComp->EnumRanges failed");
else
debugPrint(L"CDIME::_FindComposingRange() failed with null enumComp");
pPropComp->Release();
return FALSE;
}
BOOL isCompExist = FALSE;
VARIANT var;
ULONG fetched = 0;
while (enumComp->Next(1, ppRange, &fetched) == S_OK && fetched == 1)
{
debugPrint(L"CDIME::_FindComposingRange() enumComp->Next() ");
hr = pPropComp->GetValue(ec, *ppRange, &var);
if (hr == S_OK)
{
if (var.vt == VT_I4 && var.lVal != 0)
{
WCHAR rangeText[4096];
rangeText[0] = NULL;
fetched = 0;
hr = (*ppRange)->GetText(ec, 0, rangeText, 4096, &fetched);
if (SUCCEEDED(hr))
{
if (lastReadingString.GetLength() > 0) // The last reading string is null when composing is just started and the range should be empty.
{
debugPrint(L"CDIME::_FindComposingRange() text in range = %s with %d character, last reading string = %s with %d characters",
rangeText, fetched, lastReadingString.Get(), lastReadingString.GetLength());
UINT cmpCount = (int)lastReadingString.GetLength();
BOOL rangeMatchReadingString = FALSE;
if (fetched > 0 && fetched > cmpCount)
{
LONG shifted = 0;
// range contains other characters than last reading string, and these characters should be skipped with shifting the start anchor
(*ppRange)->ShiftStart(ec, fetched - cmpCount, &shifted, NULL);
debugPrint(L"CDIME::_FindComposingRange() shift start anchor with %d characters", shifted);
rangeText[0] = NULL;
hr = (*ppRange)->GetText(ec, 0, rangeText, 4096, &fetched);
if (SUCCEEDED(hr))
debugPrint(L"CDIME::_FindComposingRange() text in range with shited anchor = %s with %d character", rangeText, fetched);
}
// The range we are composing should always matched with the last reading string.
rangeMatchReadingString = CompareString(GetLocale(), 0, rangeText, cmpCount, lastReadingString.Get(), cmpCount) == CSTR_EQUAL;
if (rangeMatchReadingString)
{
isCompExist = TRUE;
debugPrint(L"CDIME::_FindComposingRange() text in this range mathced with last reading string. isCompExist = TRUE");
break;
}
}
}
else
{
debugPrint(L"CDIME::_FindComposingRange() cannot getText from range isCompExist = TRUE");
}
}
}
debugPrint(L"CDIME::_FindComposingRange() release *ppRange");
if(*ppRange)
(*ppRange)->Release();
*ppRange = nullptr;
}
pPropComp->Release();
enumComp->Release();
return isCompExist;
}
//+---------------------------------------------------------------------------
//
// _SetInputString
//
//----------------------------------------------------------------------------
HRESULT CDIME::_SetInputString(TfEditCookie ec, _In_ ITfContext *pContext, _Out_opt_ ITfRange *pRange, _In_ CStringRange *pstrAddString, BOOL exist_composing)
{
debugPrint(L"CDIME::_SetInputString() exist_composing = %x", exist_composing);
ITfRange* pRangeInsert = nullptr;
HRESULT hr = S_OK;
if (!exist_composing)
{
_InsertAtSelection(ec, pContext, pstrAddString, &pRangeInsert);
if (pRangeInsert == nullptr)
{
return hr;
}
pRange = pRangeInsert;
}
if (pRange != nullptr)
{
hr = pRange->SetText(ec, 0, pstrAddString->Get(), (LONG)pstrAddString->GetLength());
if (FAILED(hr))
{
debugPrint(L"CDIME::_SetInputString() pRange->SetText() failed with hr = %x", hr);
goto exit;
}
}
if (!_SetCompositionLanguage(ec, pContext))
{
debugPrint(L"CDIME::_SetInputString() _SetCompositionLanguage failed");
hr = E_FAIL;
goto exit;
}
if (!_SetCompositionDisplayAttributes(ec, pContext, _gaDisplayAttributeInput))
{
debugPrint(L"CDIME::_SetInputString() _SetCompositionDisplayAttributes() failed");
hr = E_FAIL;
goto exit;
}
// update the selection, we'll make it an insertion point just past
// the inserted text.
ITfRange* pSelection = nullptr;
TF_SELECTION sel;
if ((pRange != nullptr) && (pRange->Clone(&pSelection) == S_OK))
{
hr = pSelection->Collapse(ec, TF_ANCHOR_END);
if (FAILED(hr))
{
debugPrint(L"CDIME::_SetInputString() pSelection->Collapse() failed, hr = %x", hr);
goto exit;
}
sel.range = pSelection;
sel.style.ase = TF_AE_NONE;
sel.style.fInterimChar = FALSE;
pContext->SetSelection(ec, 1, &sel);
pSelection->Release();
}
exit:
if (pRangeInsert)
pRangeInsert->Release();
return hr;
}
//+---------------------------------------------------------------------------
//
// _InsertAtSelection
//
//----------------------------------------------------------------------------
HRESULT CDIME::_InsertAtSelection(TfEditCookie ec, _In_ ITfContext *pContext, _In_ CStringRange *pstrAddString, _Outptr_ ITfRange **ppCompRange)
{
debugPrint(L"CDIME::_InsertAtSelection()");
ITfRange* rangeInsert = nullptr;
ITfInsertAtSelection* pias = nullptr;
HRESULT hr = S_OK;
if (ppCompRange == nullptr || pContext == nullptr || pstrAddString == nullptr)
{
debugPrint(L"CDIME::_InsertAtSelection() failed with null ppCompRange or pContext or pstrAddstring");
hr = E_INVALIDARG;
goto Exit;
}
*ppCompRange = nullptr;
hr = pContext->QueryInterface(IID_ITfInsertAtSelection, (void **)&pias);
if (FAILED(hr) || pias == nullptr)
{
debugPrint(L"CDIME::_InsertAtSelection() failed with null pias or QueryInterface failed");
goto Exit;
}
hr = pias->InsertTextAtSelection(ec, TF_IAS_QUERYONLY, pstrAddString->Get(), (LONG)pstrAddString->GetLength(), &rangeInsert);
if ( FAILED(hr) || rangeInsert == nullptr)
{
debugPrint(L"CDIME::_InsertAtSelection() InsertTextAtSelection failed");
rangeInsert = nullptr;
pias->Release();
goto Exit;
}
WCHAR rangeText[256];
rangeText[0] = NULL;
ULONG fetched = 0;
hr = rangeInsert->GetText(ec, 0, rangeText, 256, &fetched);
if (SUCCEEDED(hr) && rangeText)
debugPrint(L"CDIME::_InsertAtSelection() text in range = %s", rangeText);
*ppCompRange = rangeInsert;
pias->Release();
hr = S_OK;
Exit:
return hr;
}
//+---------------------------------------------------------------------------
//
// _RemoveDummyCompositionForComposing
//
//----------------------------------------------------------------------------
HRESULT CDIME::_RemoveDummyCompositionForComposing
(TfEditCookie ec, _In_ ITfComposition *pComposition)
{
debugPrint(L"CDIME::_RemoveDummyCompositionForComposing()\n");
HRESULT hr = S_OK;
ITfRange* pRange = nullptr;
if (pComposition)
{
hr = pComposition->GetRange(&pRange);
if (SUCCEEDED(hr) && pRange)
{
pRange->SetText(ec, 0, nullptr, 0);
pRange->Release();
}
}
return hr;
}
//+---------------------------------------------------------------------------
//
// _SetCompositionLanguage
//
//----------------------------------------------------------------------------
BOOL CDIME::_SetCompositionLanguage(TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_SetCompositionLanguage()");
HRESULT hr = S_OK;
BOOL ret = TRUE;
if (pContext == nullptr || _pComposition == nullptr)
{
debugPrint(L"CDIME::_SetCompositionLanguage() failed with null pContext or _pComposition");
ret = FALSE;
goto Exit;
}
ITfRange* pRangeComposition = nullptr;
ITfProperty* pLanguageProperty = nullptr;
// we need a range and the context it lives in
hr = _pComposition->GetRange(&pRangeComposition);
if (FAILED(hr) || pRangeComposition == nullptr)
{
if (FAILED(hr))
debugPrint(L"CDIME::_SetCompositionLanguage() _pComposition->GetRange() failed");
else
debugPrint(L"CDIME::_SetCompositionLanguage() failed with null pRangeComposition");
ret = FALSE;
goto Exit;
}
// get our the language property
hr = pContext->GetProperty(GUID_PROP_LANGID, &pLanguageProperty);
if (FAILED(hr) || pLanguageProperty == nullptr)
{
if (FAILED(hr))
debugPrint(L"CDIME::_SetCompositionLanguage() pContext->GetProperty() failed hr = %x", hr);
else
debugPrint(L"CDIME::_SetCompositionLanguage() failed with null pLanguageProperty");
ret = FALSE;
goto Exit;
}
VARIANT var;
var.vt = VT_I4; // we're going to set DWORD
var.lVal = _langid;
hr = pLanguageProperty->SetValue(ec, pRangeComposition, &var);
if (FAILED(hr) || pRangeComposition == nullptr)
{
if (FAILED(hr))
debugPrint(L"CDIME::_SetCompositionLanguage() pLanguageProperty->SetValue() failed hr = %x", hr);
else
debugPrint(L"CDIME::_SetCompositionLanguage() failed with null pRangeComposition ");
ret = FALSE;
goto Exit;
}
pLanguageProperty->Release();
pRangeComposition->Release();
Exit:
return ret;
}
//+---------------------------------------------------------------------------
//
// CProbeComposistionEditSession
//
//----------------------------------------------------------------------------
class CProbeComposistionEditSession : public CEditSessionBase
{
public:
CProbeComposistionEditSession(_In_ CDIME *pTextService, _In_ ITfContext *pContext) : CEditSessionBase(pTextService, pContext)
{
}
// ITfEditSession
STDMETHODIMP DoEditSession(TfEditCookie ec);
};
//+---------------------------------------------------------------------------
//
// ITfEditSession::DoEditSession
//
//----------------------------------------------------------------------------
STDAPI CProbeComposistionEditSession::DoEditSession(TfEditCookie ec)
{
debugPrint(L"CProbeComposistionEditSession::DoEditSession()\n");
if(_pTextService)
_pTextService->_ProbeCompositionRangeNotification(ec, _pContext);
return S_OK;
}
//////////////////////////////////////////////////////////////////////
//
// CDIME class
//
//////////////////////////////////////////////////////////////////////+---------------------------------------------------------------------------
//
// _ProbeComposition
//
// starts a new (std::nothrow) pProbeComposistionEditSession at the selection of the current
// focus context to get correct caret position.
//----------------------------------------------------------------------------
void CDIME::_ProbeComposition(_In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_ProbeComposition() pContext = %x\n", pContext);
CProbeComposistionEditSession* pProbeComposistionEditSession = new (std::nothrow) CProbeComposistionEditSession(this, pContext);
if (pProbeComposistionEditSession && pContext)
{
HRESULT hrES = S_OK, hr = S_OK;
hr = pContext->RequestEditSession(_tfClientId, pProbeComposistionEditSession, TF_ES_ASYNCDONTCARE | TF_ES_READWRITE, &hrES);
debugPrint(L"CDIME::_ProbeComposition() RequestEdisession HRESULT = %x, return HRESULT = %x\n", hrES, hr );
pProbeComposistionEditSession->Release();
}
}
HRESULT CDIME::_ProbeCompositionRangeNotification(_In_ TfEditCookie ec, _In_ ITfContext *pContext)
{
debugPrint(L"CDIME::_ProbeCompositionRangeNotification(), \n");
HRESULT hr = S_OK;
if(!_IsComposing())
_StartComposition(pContext);
hr = E_FAIL;
ULONG fetched = 0;
TF_SELECTION tfSelection;
if ( pContext == nullptr || (hr = pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &fetched)) != S_OK || fetched != 1 ||tfSelection.range==nullptr)
{
_TerminateComposition(ec,pContext);
return hr;
}
tfSelection.range->Release();
ITfRange *pRange;
ITfContextView* pContextView;
ITfDocumentMgr* pDocumgr;
if (_pComposition&&SUCCEEDED(_pComposition->GetRange(&pRange)) && pRange)
{
if(pContext && SUCCEEDED(pContext->GetActiveView(&pContextView)) && pContextView)
{
if(pContext && SUCCEEDED( pContext->GetDocumentMgr(&pDocumgr)) && pDocumgr && _pThreadMgr &&_pUIPresenter)
{
ITfDocumentMgr* pFocusDocuMgr;
_pThreadMgr->GetFocus(&pFocusDocuMgr);
if(pFocusDocuMgr == pDocumgr)
{
_pUIPresenter->_StartLayout(pContext, ec, pRange);
_pUIPresenter->_MoveUIWindowsToTextExt();
}
else
_pUIPresenter->ClearNotify();
pDocumgr->Release();
}
pContextView->Release();
}
pRange->Release();
}
_TerminateComposition(ec,pContext);
return hr;
}