-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIME Test.cpp
More file actions
235 lines (173 loc) · 5.54 KB
/
IME Test.cpp
File metadata and controls
235 lines (173 loc) · 5.54 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
// IME Test.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "CInput.h"
/*----------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int GetCodePageFromLang( LANGID langid );
CInput g_input;
/*----------------------------------------------------------------------------*/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char szWindowName[] = "IME Test";
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowName;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
ATOM atom = RegisterClassEx(&wcex);
g_input.InitInput();
HWND hWnd = CreateWindow(szWindowName, szWindowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 400, 300, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*----------------------------------------------------------------------------*/
void ShowInputText(HDC hdc, HWND hWnd)
{
HFONT hFont = CreateFont(16, 0, 0, 0, 400, FALSE, FALSE, FALSE, g_input.GetCharSet(), OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "±¼¸²");
if(hFont) {
HFONT hFontOld = (HFONT)SelectObject(hdc, (HGDIOBJ)hFont);
char text[256];
int textLen;
SIZE editTextSize;
/* Draw Indicator */
SetTextColor(hdc, 0x00000000);
if(g_input.GetImeState() == 1) {
SetBkColor(hdc, 0x007F7FFF);
} else {
SetBkColor(hdc, 0x00CCCCCC);
}
const wchar_t* indicator = g_input.GetIndicator();
TextOutW(hdc, 0, 0, indicator, wcslen(indicator));
/* Draw Edit Text */
SetBkColor(hdc, 0x00FFFFFF);
textLen = g_input.GetInput(text, sizeof(text));
TextOutA(hdc, 20, 0, text, textLen);
GetTextExtentPoint32A(hdc, text, textLen, &editTextSize);
/* Draw Compostion Text */
SetBkColor(hdc, 0x00CCCCCC);
textLen = g_input.GetComp(text, sizeof(text));
TextOutA(hdc, 20 + editTextSize.cx, 0, text, textLen);
/* Draw Underline */
int start, end;
SIZE ulStart, ulEnd;
g_input.GetUnderLine(&start, &end);
GetTextExtentPoint32A(hdc, text, start, &ulStart);
GetTextExtentPoint32A(hdc, text, end, &ulEnd);
HPEN hPen = CreatePen(PS_DOT, 0, 0x000000FF);
HPEN hOldPen = (HPEN)SelectObject(hdc, (HGDIOBJ)hPen);
MoveToEx(hdc, 20 + editTextSize.cx+ulStart.cx, ulEnd.cy-1, NULL);
LineTo(hdc, 20 + editTextSize.cx+ulEnd.cx, ulEnd.cy-1);
SelectObject(hdc, (HGDIOBJ)hOldPen);
int cx = 0;
int cy = 20;
/* Draw Reading */
SetBkColor(hdc, 0x007FFF7F);
textLen = g_input.GetReading(text, sizeof(text));
if(!g_input.IsVerticalReading()) {
TextOut(hdc, 0, cy, text, textLen);
} else {
char* begin = text;
char* end = text + textLen;
for(int i=0; i<4 && begin<end; ++i) {
char* next = CharNextExA(g_input.GetCodePage(), begin, 0);
TextOut(hdc, 0, cy, begin, next-begin);
SIZE readSize;
GetTextExtentPoint32(hdc, begin, next-begin, &readSize);
cy += readSize.cy;
begin = next;
}
}
/* Draw Candidate */
SetTextColor(hdc, 0x00000000);
int count = min(g_input.GetCandidateCount(), g_input.GetCandidatePageStart()+g_input.GetCandidatePageSize());
for(int i=g_input.GetCandidatePageStart(); i<count; ++i) {
if(i == g_input.GetCandidateSelection()) {
SetBkColor(hdc, 0x00FFCFCF);
} else {
SetBkColor(hdc, 0x00FF7F7F);
}
int textLen = g_input.GetCandidate(i, text, sizeof(text));
TextOut(hdc, cx, cy, text, textLen);
SIZE candSize;
GetTextExtentPoint32(hdc, text, textLen, &candSize);
if(!g_input.IsVerticalCandidate()) {
cx += candSize.cx;
} else {
cy += candSize.cy;
}
}
SelectObject(hdc, (HGDIOBJ)hFontOld);
}
}
/*----------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
g_input.OnInputLanguageChange(hWnd, 0, (LPARAM)GetKeyboardLayout(0));
return 0L;
/* IME Messages */
case WM_INPUTLANGCHANGE:
g_input.OnInputLanguageChange(hWnd, wParam, lParam);
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_IME_SETCONTEXT:
lParam = 0;
break;
case WM_IME_STARTCOMPOSITION:
return 0L;
case WM_IME_COMPOSITION:
if(g_input.OnComposition(hWnd, wParam, lParam)) {
InvalidateRect(hWnd, NULL, TRUE);
return 0L;
}
break;
case WM_IME_ENDCOMPOSITION:
if(g_input.OnEndComposition(hWnd, wParam, lParam)) {
InvalidateRect(hWnd, NULL, TRUE);
return 0L;
}
break;
case WM_IME_NOTIFY:
if(g_input.OnNotify(hWnd, wParam, lParam)) {
InvalidateRect(hWnd, NULL, TRUE);
return 0L;
}
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_CHAR:
g_input.OnChar(hWnd, wParam, lParam);
InvalidateRect(hWnd, NULL, TRUE);
break;
/* End of IME Messages */
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
ShowInputText(hdc, hWnd);
EndPaint(hWnd, &ps);
return 0L;
case WM_DESTROY:
PostQuitMessage(0);
return 0L;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
/*----------------------------------------------------------------------------*/