-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
InputModeWindow.cpp
596 lines (495 loc) · 12.3 KB
/
InputModeWindow.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
#include "imcrvtip.h"
#include "TextService.h"
#include "InputModeWindow.h"
#define INPUTMODE_TIMER_ID 0x54ab516b
class CInputModeWindowGetTextExtEditSession : public CEditSessionBase
{
public:
CInputModeWindowGetTextExtEditSession(CTextService *pTextService, ITfContext *pContext,
ITfContextView *pContextView, CInputModeWindow *pInputModeWindow) : CEditSessionBase(pTextService, pContext)
{
_pInputModeWindow = pInputModeWindow;
_pInputModeWindow->AddRef();
_pContextView = pContextView;
_pContextView->AddRef();
}
~CInputModeWindowGetTextExtEditSession()
{
SafeRelease(&_pInputModeWindow);
SafeRelease(&_pContextView);
}
// ITfEditSession
STDMETHODIMP DoEditSession(TfEditCookie ec)
{
TF_SELECTION tfSelection;
ULONG cFetched = 0;
if(_pContext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfSelection, &cFetched) != S_OK)
{
return E_FAIL;
}
if(cFetched != 1)
{
SafeRelease(&tfSelection.range);
return E_FAIL;
}
RECT rc;
BOOL fClipped;
if(_pContextView->GetTextExt(ec, tfSelection.range, &rc, &fClipped) != S_OK)
{
SafeRelease(&tfSelection.range);
return E_FAIL;
}
//ignore abnormal position (from CUAS ?)
if((rc.top == rc.bottom) && ((rc.right - rc.left) == 1))
{
SafeRelease(&tfSelection.range);
return E_FAIL;
}
POINT pt;
pt.x = rc.left;
pt.y = rc.bottom;
HMONITOR hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi;
mi.cbSize = sizeof(mi);
GetMonitorInfoW(hMonitor, &mi);
RECT rw;
_pInputModeWindow->_GetRect(&rw);
LONG height = rw.bottom - rw.top;
LONG width = rw.right - rw.left;
if(rc.left < mi.rcWork.left)
{
rc.left = mi.rcWork.left;
}
if(mi.rcWork.right < rc.right)
{
rc.left = mi.rcWork.right - width;
}
if(mi.rcWork.bottom < rc.top)
{
rc.bottom = mi.rcWork.bottom - height - IM_MERGIN_Y;
}
else if(mi.rcWork.bottom < (rc.bottom + height + IM_MERGIN_Y))
{
rc.bottom = rc.top - height - IM_MERGIN_Y * 2;
}
if(rc.bottom < mi.rcWork.top)
{
rc.bottom = mi.rcWork.top - IM_MERGIN_Y;
}
_pInputModeWindow->_Move(rc.left, rc.bottom + IM_MERGIN_Y);
_pInputModeWindow->_Show(TRUE);
SafeRelease(&tfSelection.range);
return S_OK;
}
private:
ITfContextView *_pContextView;
CInputModeWindow *_pInputModeWindow;
};
CInputModeWindow::CInputModeWindow()
{
DllAddRef();
_cRef = 1;
_hwnd = nullptr;
_hwndParent = nullptr;
_pTextService = nullptr;
_pContext = nullptr;
_size = 0;
}
CInputModeWindow::~CInputModeWindow()
{
_Destroy();
DllRelease();
}
STDAPI CInputModeWindow::QueryInterface(REFIID riid, void **ppvObj)
{
if(ppvObj == nullptr)
{
return E_INVALIDARG;
}
*ppvObj = nullptr;
if(IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_ITfTextLayoutSink))
{
*ppvObj = (ITfTextLayoutSink *)this;
}
if(*ppvObj)
{
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDAPI_(ULONG) CInputModeWindow::AddRef()
{
return ++_cRef;
}
STDAPI_(ULONG) CInputModeWindow::Release()
{
if(--_cRef == 0)
{
delete this;
return 0;
}
return _cRef;
}
STDAPI CInputModeWindow::OnLayoutChange(ITfContext *pContext, TfLayoutCode lcode, ITfContextView *pContextView)
{
HRESULT hr;
if(pContext != _pContext)
{
return S_OK;
}
switch(lcode)
{
case TF_LC_CREATE:
break;
case TF_LC_CHANGE:
try
{
CInputModeWindowGetTextExtEditSession *pEditSession =
new CInputModeWindowGetTextExtEditSession(_pTextService, pContext, pContextView, this);
pContext->RequestEditSession(_pTextService->_GetClientId(), pEditSession, TF_ES_SYNC | TF_ES_READ, &hr);
SafeRelease(&pEditSession);
}
catch(...)
{
}
break;
case TF_LC_DESTROY:
_Destroy();
break;
default:
break;
}
return S_OK;
}
HRESULT CInputModeWindow::_AdviseTextLayoutSink()
{
HRESULT hr = E_FAIL;
ITfSource *pSource;
if(_pContext->QueryInterface(IID_PPV_ARGS(&pSource)) == S_OK)
{
hr = pSource->AdviseSink(IID_IUNK_ARGS((ITfTextLayoutSink *)this), &_dwCookieTextLayoutSink);
SafeRelease(&pSource);
}
return hr;
}
HRESULT CInputModeWindow::_UnadviseTextLayoutSink()
{
HRESULT hr = E_FAIL;
if(_pContext != nullptr)
{
ITfSource *pSource;
if(_pContext->QueryInterface(IID_PPV_ARGS(&pSource)) == S_OK)
{
hr = pSource->UnadviseSink(_dwCookieTextLayoutSink);
SafeRelease(&pSource);
}
}
return hr;
}
BOOL CInputModeWindow::_Create(CTextService *pTextService, ITfContext *pContext, BOOL bCandidateWindow, HWND hWnd)
{
POINT pt = {0, 0};
if(pContext != nullptr)
{
_pContext = pContext;
_pContext->AddRef();
if(_AdviseTextLayoutSink() != S_OK)
{
return FALSE;
}
}
if(!bCandidateWindow && _pContext == nullptr)
{
return FALSE;
}
_pTextService = pTextService;
_pTextService->AddRef();
_bCandidateWindow = bCandidateWindow;
if(_bCandidateWindow)
{
_hwndParent = hWnd;
}
else
{
ITfContextView *pContextView;
if(_pContext->GetActiveView(&pContextView) == S_OK)
{
if(FAILED(pContextView->GetWnd(&_hwndParent)) || _hwndParent == nullptr)
{
_hwndParent = GetFocus();
}
SafeRelease(&pContextView);
}
}
_hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE,
InputModeWindowClass, L"", WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
_hwndParent, nullptr, g_hInst, this);
if(_hwnd == nullptr)
{
return FALSE;
}
HDC hdc = GetDC(nullptr);
_size = MulDiv(16, GetDeviceCaps(hdc, LOGPIXELSY), 96);
ReleaseDC(nullptr, hdc);
if(_bCandidateWindow)
{
RECT r;
GetClientRect(_hwndParent, &r);
pt.x = r.left;
pt.y = r.bottom;
ClientToScreen(_hwndParent, &pt);
}
SetWindowPos(_hwnd, HWND_TOPMOST, pt.x, pt.y + IM_MERGIN_Y,
_size + IM_MERGIN_X * 2, _size + IM_MERGIN_Y * 2, SWP_NOACTIVATE);
return TRUE;
}
BOOL CInputModeWindow::_InitClass()
{
WNDCLASSEXW wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.style = CS_VREDRAW | CS_HREDRAW;
wcex.lpfnWndProc = CInputModeWindow::_WindowPreProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInst;
wcex.hIcon = nullptr;
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = InputModeWindowClass;
wcex.hIconSm = nullptr;
ATOM atom = RegisterClassExW(&wcex);
return (atom != 0);
}
void CInputModeWindow::_UninitClass()
{
UnregisterClassW(InputModeWindowClass, g_hInst);
}
LRESULT CALLBACK CInputModeWindow::_WindowPreProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CInputModeWindow *pInputModeWindow = nullptr;
switch(uMsg)
{
case WM_NCCREATE:
pInputModeWindow = (CInputModeWindow *)((LPCREATESTRUCTW)lParam)->lpCreateParams;
SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pInputModeWindow);
break;
default:
pInputModeWindow = (CInputModeWindow *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
break;
}
if(pInputModeWindow != nullptr)
{
return pInputModeWindow->_WindowProc(hWnd, uMsg, wParam, lParam);
}
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK CInputModeWindow::_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
HDC hmemdc;
HBITMAP hmembmp;
HPEN npen;
HBRUSH nbrush;
HGDIOBJ bmp, pen, brush;
HICON hIcon;
RECT r;
switch(uMsg)
{
case WM_CREATE:
if(!_bCandidateWindow)
{
SetTimer(hWnd, INPUTMODE_TIMER_ID, _pTextService->cx_showmodesec * 1000, nullptr);
}
break;
case WM_TIMER:
if(wParam == INPUTMODE_TIMER_ID)
{
// CAUTION!! killing self
_pTextService->_EndInputModeWindow();
}
break;
case WM_DESTROY:
if(!_bCandidateWindow)
{
KillTimer(hWnd, INPUTMODE_TIMER_ID);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &r);
hmemdc = CreateCompatibleDC(hdc);
hmembmp = CreateCompatibleBitmap(hdc, r.right, r.bottom);
bmp = SelectObject(hmemdc, hmembmp);
npen = CreatePen(PS_SOLID, 1, RGB(0x00, 0x00, 0x00));
pen = SelectObject(hmemdc, npen);
nbrush = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
brush = SelectObject(hmemdc, nbrush);
Rectangle(hmemdc, 0, 0, r.right, r.bottom);
_pTextService->_GetIcon(&hIcon);
DrawIconEx(hmemdc, IM_MERGIN_X, IM_MERGIN_Y, hIcon, _size, _size, 0, nbrush, DI_NORMAL);
SelectObject(hmemdc, pen);
SelectObject(hmemdc, brush);
DeleteObject(npen);
DeleteObject(nbrush);
BitBlt(hdc, 0, 0, r.right, r.bottom, hmemdc, 0, 0, SRCCOPY);
SelectObject(hmemdc, bmp);
DeleteObject(hmembmp);
DeleteObject(hmemdc);
EndPaint(hWnd, &ps);
break;
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
default:
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
return 0;
}
void CInputModeWindow::_Destroy()
{
if(_hwnd != nullptr)
{
DestroyWindow(_hwnd);
_hwnd = nullptr;
}
_UnadviseTextLayoutSink();
SafeRelease(&_pContext);
SafeRelease(&_pTextService);
}
void CInputModeWindow::_Move(int x, int y)
{
if(_hwnd != nullptr)
{
SetWindowPos(_hwnd, HWND_TOPMOST, x, y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
}
}
void CInputModeWindow::_Show(BOOL bShow)
{
if(_hwnd != nullptr)
{
SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE | (bShow ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
}
}
void CInputModeWindow::_Redraw()
{
if(_hwnd != nullptr)
{
InvalidateRect(_hwnd, nullptr, FALSE);
UpdateWindow(_hwnd);
}
}
void CInputModeWindow::_GetRect(LPRECT lpRect)
{
if(lpRect != nullptr)
{
ZeroMemory(lpRect, sizeof(*lpRect));
if(_hwnd != nullptr)
{
GetClientRect(_hwnd, lpRect);
}
}
}
class CInputModeWindowEditSession : public CEditSessionBase
{
public:
CInputModeWindowEditSession(CTextService *pTextService, ITfContext *pContext,
CInputModeWindow *pInputModeWindow) : CEditSessionBase(pTextService, pContext)
{
_pInputModeWindow = pInputModeWindow;
_pInputModeWindow->AddRef();
}
~CInputModeWindowEditSession()
{
SafeRelease(&_pInputModeWindow);
}
// ITfEditSession
STDMETHODIMP DoEditSession(TfEditCookie ec)
{
HRESULT hr;
ITfContextView *pContextView;
if(_pContext->GetActiveView(&pContextView) == S_OK)
{
try
{
CInputModeWindowGetTextExtEditSession *pEditSession =
new CInputModeWindowGetTextExtEditSession(_pTextService, _pContext, pContextView, _pInputModeWindow);
_pContext->RequestEditSession(_pTextService->_GetClientId(), pEditSession, TF_ES_SYNC | TF_ES_READ, &hr);
SafeRelease(&pEditSession);
}
catch(...)
{
}
SafeRelease(&pContextView);
}
return S_OK;
}
private:
CInputModeWindow *_pInputModeWindow;
};
void CTextService::_StartInputModeWindow()
{
switch(inputmode)
{
case im_default:
case im_hiragana:
case im_katakana:
case im_katakana_ank:
case im_jlatin:
case im_ascii:
_EndInputModeWindow();
ITfDocumentMgr *pDocumentMgr;
if((_pThreadMgr->GetFocus(&pDocumentMgr) == S_OK) && (pDocumentMgr != nullptr))
{
ITfContext *pContext;
if((pDocumentMgr->GetTop(&pContext) == S_OK) && (pContext != nullptr))
{
try
{
_pInputModeWindow = new CInputModeWindow();
if(_pInputModeWindow->_Create(this, pContext, FALSE, nullptr))
{
HRESULT hr = E_FAIL;
HRESULT hrSession = E_FAIL;
try
{
CInputModeWindowEditSession *pEditSession = new CInputModeWindowEditSession(this, pContext, _pInputModeWindow);
// Asynchronous, read-only
hr = pContext->RequestEditSession(_ClientId, pEditSession, TF_ES_ASYNC | TF_ES_READ, &hrSession);
SafeRelease(&pEditSession);
// It is possible that asynchronous requests are treated as synchronous requests.
if(hr != S_OK || (hrSession != TF_S_ASYNC && hrSession != S_OK))
{
_EndInputModeWindow();
}
}
catch(...)
{
}
}
}
catch(...)
{
}
SafeRelease(&pContext);
}
SafeRelease(&pDocumentMgr);
}
break;
default:
break;
}
}
void CTextService::_EndInputModeWindow()
{
if(_pInputModeWindow != nullptr)
{
_pInputModeWindow->_Destroy();
}
SafeRelease(&_pInputModeWindow);
}