-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyEditHandlers.cpp
More file actions
401 lines (322 loc) · 10.6 KB
/
KeyEditHandlers.cpp
File metadata and controls
401 lines (322 loc) · 10.6 KB
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
//////////////////////////////////////////////////////////////////////
//
// KeyEditHandlers.cpp
//
// Backspace/Delete handling to keep ComposingText and TSF composition range
// in sync.
//
//////////////////////////////////////////////////////////////////////
#include "Globals.h"
#include "TextService.h"
#include <string>
//+---------------------------------------------------------------------------
//
// IsRangeCovered
//
// Returns TRUE if pRangeTest is entirely contained within pRangeCover.
//
//----------------------------------------------------------------------------
static BOOL IsRangeCovered(TfEditCookie ec, ITfRange* pRangeTest, ITfRange* pRangeCover)
{
LONG lResult;
if (pRangeCover->CompareStart(ec, pRangeTest, TF_ANCHOR_START, &lResult) != S_OK ||
lResult > 0)
{
return FALSE;
}
if (pRangeCover->CompareEnd(ec, pRangeTest, TF_ANCHOR_END, &lResult) != S_OK ||
lResult < 0)
{
return FALSE;
}
return TRUE;
}
//+---------------------------------------------------------------------------
//
// GetRangeText
//
// Read all text in range. (Uses MOVESTART, so clone before reading.)
//
//----------------------------------------------------------------------------
static std::wstring GetRangeText(TfEditCookie ec, ITfRange* pRange)
{
if (!pRange)
{
return L"";
}
ITfRange* pClone = nullptr;
if (pRange->Clone(&pClone) != S_OK || !pClone)
{
return L"";
}
std::wstring result;
WCHAR buf[256];
ULONG cchOut = 0;
for (;;)
{
cchOut = 0;
HRESULT hr = pClone->GetText(
ec,
TF_TF_MOVESTART,
buf,
(ULONG)(sizeof(buf) / sizeof(buf[0])),
&cchOut);
if (FAILED(hr) || cchOut == 0)
{
break;
}
result.append(buf, buf + cchOut);
}
pClone->Release();
return result;
}
//+---------------------------------------------------------------------------
//
// GetSurfaceCursorFromSelection
//
// Compute surface cursor = length of (composition start .. caret).
//
// NOTE:
// ITfRange has no SetEndPoint(). Use ShiftEndToRange().
//
//----------------------------------------------------------------------------
static LONG GetSurfaceCursorFromSelection(TfEditCookie ec, ITfContext* pContext, ITfRange* pCompositionRange)
{
if (!pContext || !pCompositionRange)
{
return 0;
}
TF_SELECTION tfSelection;
ULONG cFetched = 0;
if (pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &cFetched) != S_OK || cFetched != 1)
{
return 0;
}
LONG surfaceCursor = 0;
// composition の start..caret の range を作る
ITfRange* pPrefix = nullptr;
if (pCompositionRange->Clone(&pPrefix) == S_OK && pPrefix)
{
// start に潰す(start=end=composition start)
pPrefix->Collapse(ec, TF_ANCHOR_START);
// end を caret(start) に合わせる
// ITfRange::ShiftEndToRange(ec, targetRange, targetAnchor)
// -> pPrefix の end が tfSelection.range の start に移動する
if (pPrefix->ShiftEndToRange(ec, tfSelection.range, TF_ANCHOR_START) == S_OK)
{
std::wstring prefixText = GetRangeText(ec, pPrefix);
surfaceCursor = (LONG)prefixText.size();
}
pPrefix->Release();
}
tfSelection.range->Release();
return surfaceCursor;
}
//+---------------------------------------------------------------------------
//
// SetCaretInComposition
//
// Move caret to composition start + surfaceCursor.
// (Uses ShiftStart on a cloned range.)
//
//----------------------------------------------------------------------------
static void SetCaretInComposition(TfEditCookie ec, ITfContext* pContext, ITfRange* pCompositionRange, LONG surfaceCursor)
{
if (!pContext || !pCompositionRange)
{
return;
}
ITfRange* pCaret = nullptr;
if (pCompositionRange->Clone(&pCaret) != S_OK || !pCaret)
{
return;
}
// start に collapse
pCaret->Collapse(ec, TF_ANCHOR_START);
// start を surfaceCursor だけ進める
LONG moved = 0;
pCaret->ShiftStart(ec, surfaceCursor, &moved, NULL);
// insertion point にする
pCaret->Collapse(ec, TF_ANCHOR_START);
TF_SELECTION sel;
sel.range = pCaret;
sel.style.ase = TF_AE_NONE;
sel.style.fInterimChar = FALSE;
pContext->SetSelection(ec, 1, &sel);
pCaret->Release();
}
//+---------------------------------------------------------------------------
//
// MapSurfaceCursorToRawCursor
//
// Approx map surface cursor -> raw cursor by converting prefixes.
//
//----------------------------------------------------------------------------
static LONG MapSurfaceCursorToRawCursor(const RomajiKanaConverter& converter,
const std::wstring& rawText,
LONG surfaceCursor)
{
if (surfaceCursor <= 0)
{
return 0;
}
if (rawText.empty())
{
return 0;
}
LONG best = (LONG)rawText.size();
// i 文字までの raw を変換して、その長さが surfaceCursor 以上になった最小 i を採用
for (LONG i = 0; i <= (LONG)rawText.size(); i++)
{
std::wstring rawPrefix = rawText.substr(0, i);
std::wstring surfPrefix = converter.ConvertFromRaw(rawPrefix);
if ((LONG)surfPrefix.size() >= surfaceCursor)
{
best = i;
break;
}
}
if (best < 0) best = 0;
if (best > (LONG)rawText.size()) best = (LONG)rawText.size();
return best;
}
//+---------------------------------------------------------------------------
//
// _HandleBackspaceKey
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleBackspaceKey(TfEditCookie ec, ITfContext* pContext)
{
if (!pContext)
{
return E_INVALIDARG;
}
if (!_IsComposing())
{
return S_OK;
}
ITfRange* pRangeComposition = nullptr;
if (_pComposition == nullptr || _pComposition->GetRange(&pRangeComposition) != S_OK || !pRangeComposition)
{
return S_OK;
}
// selection が composition 内か確認
TF_SELECTION tfSelection;
ULONG cFetched = 0;
if (pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &cFetched) != S_OK || cFetched != 1)
{
pRangeComposition->Release();
return S_OK;
}
BOOL covered = IsRangeCovered(ec, tfSelection.range, pRangeComposition);
tfSelection.range->Release();
if (!covered)
{
pRangeComposition->Release();
return S_OK;
}
// caret(surface) -> raw cursor にマップ
LONG surfaceCursor = GetSurfaceCursorFromSelection(ec, pContext, pRangeComposition);
LONG rawCursor = MapSurfaceCursorToRawCursor(_romajiConverter, _composingText.GetRawText(), surfaceCursor);
_composingText.SetRawCursor(rawCursor);
// Backspace: raw のカーソル前を 1 文字削除
if (!_composingText.DeleteRawBeforeCursor())
{
pRangeComposition->Release();
return S_OK;
}
// raw -> surface 再生成
_composingText.UpdateSurfaceFromRaw(_romajiConverter);
_composingText.ClearLiveConversionText();
const std::wstring& newText = _composingText.GetCurrentText();
if (newText.empty())
{
// 空なら composition 終了
pRangeComposition->SetText(ec, 0, L"", 0);
pRangeComposition->Release();
_composingText.Reset();
_TerminateComposition(ec, pContext);
return S_OK;
}
// composition 範囲を更新
pRangeComposition->SetText(ec, 0, newText.c_str(), (ULONG)newText.size());
// 表示属性
_SetCompositionDisplayAttributes(ec, pContext, _gaDisplayAttributeInput);
// caret を再設定
LONG newSurfaceCursor = _composingText.GetSurfaceCursor();
if (newSurfaceCursor < 0) newSurfaceCursor = 0;
if (newSurfaceCursor > (LONG)newText.size()) newSurfaceCursor = (LONG)newText.size();
SetCaretInComposition(ec, pContext, pRangeComposition, newSurfaceCursor);
pRangeComposition->Release();
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandleDeleteKey
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleDeleteKey(TfEditCookie ec, ITfContext* pContext)
{
if (!pContext)
{
return E_INVALIDARG;
}
if (!_IsComposing())
{
return S_OK;
}
ITfRange* pRangeComposition = nullptr;
if (_pComposition == nullptr || _pComposition->GetRange(&pRangeComposition) != S_OK || !pRangeComposition)
{
return S_OK;
}
// selection が composition 内か確認
TF_SELECTION tfSelection;
ULONG cFetched = 0;
if (pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &cFetched) != S_OK || cFetched != 1)
{
pRangeComposition->Release();
return S_OK;
}
BOOL covered = IsRangeCovered(ec, tfSelection.range, pRangeComposition);
tfSelection.range->Release();
if (!covered)
{
pRangeComposition->Release();
return S_OK;
}
// caret(surface) -> raw cursor にマップ
LONG surfaceCursor = GetSurfaceCursorFromSelection(ec, pContext, pRangeComposition);
LONG rawCursor = MapSurfaceCursorToRawCursor(_romajiConverter, _composingText.GetRawText(), surfaceCursor);
_composingText.SetRawCursor(rawCursor);
// Delete: raw のカーソル位置を 1 文字削除
if (!_composingText.DeleteRawAtCursor())
{
pRangeComposition->Release();
return S_OK;
}
// raw -> surface 再生成
_composingText.UpdateSurfaceFromRaw(_romajiConverter);
_composingText.ClearLiveConversionText();
const std::wstring& newText = _composingText.GetCurrentText();
if (newText.empty())
{
// 空なら composition 終了
pRangeComposition->SetText(ec, 0, L"", 0);
pRangeComposition->Release();
_composingText.Reset();
_TerminateComposition(ec, pContext);
return S_OK;
}
// composition 範囲を更新
pRangeComposition->SetText(ec, 0, newText.c_str(), (ULONG)newText.size());
// 表示属性
_SetCompositionDisplayAttributes(ec, pContext, _gaDisplayAttributeInput);
// caret を再設定(Delete はカーソル位置維持)
LONG newSurfaceCursor = _composingText.GetSurfaceCursor();
if (newSurfaceCursor < 0) newSurfaceCursor = 0;
if (newSurfaceCursor > (LONG)newText.size()) newSurfaceCursor = (LONG)newText.size();
SetCaretInComposition(ec, pContext, pRangeComposition, newSurfaceCursor);
pRangeComposition->Release();
return S_OK;
}