This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
forked from ryancheung/MonoGame.IMEHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMENativeWindow.cs
405 lines (342 loc) · 13.2 KB
/
IMENativeWindow.cs
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
#if WINDOWSDX || XNA
using Microsoft.Xna.Framework;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MonoGame.IMEHelper
{
/// <summary>
/// Native window class that handles IME.
/// </summary>
internal sealed class IMENativeWindow : NativeWindow, IDisposable
{
private WinFormsIMEHandler _imeHandler;
private IMMCompositionString
_compstr, _compclause, _compattr,
_compread, _compreadclause, _compreadattr,
_resstr, _resclause,
_resread, _resreadclause;
private IMMCompositionInt _compcurpos;
private bool _disposed, _showIMEWin;
private IntPtr _context;
/// <summary>
/// Gets the state if the IME should be enabled
/// </summary>
public bool IsEnabled { get; private set; }
public bool IsIMEOpen { get; private set; }
/// <summary>
/// Composition String
/// </summary>
public string CompositionString { get { return _compstr.ToString(); } }
/// <summary>
/// Composition Clause
/// </summary>
public string CompositionClause { get { return _compclause.ToString(); } }
/// <summary>
/// Composition String Reads
/// </summary>
public string CompositionReadString { get { return _compread.ToString(); } }
/// <summary>
/// Composition Clause Reads
/// </summary>
public string CompositionReadClause { get { return _compreadclause.ToString(); } }
/// <summary>
/// Result String
/// </summary>
public string ResultString { get { return _resstr.ToString(); } }
/// <summary>
/// Result Clause
/// </summary>
public string ResultClause { get { return _resclause.ToString(); } }
/// <summary>
/// Result String Reads
/// </summary>
public string ResultReadString { get { return _resread.ToString(); } }
/// <summary>
/// Result Clause Reads
/// </summary>
public string ResultReadClause { get { return _resreadclause.ToString(); } }
/// <summary>
/// Caret position of the composition
/// </summary>
public int CompositionCursorPos { get { return _compcurpos.Value; } }
/// <summary>
/// Array of the candidates
/// </summary>
public string[] Candidates { get; private set; }
/// <summary>
/// First candidate index of current page
/// </summary>
public uint CandidatesPageStart { get; private set; }
/// <summary>
/// How many candidates should display per page
/// </summary>
public uint CandidatesPageSize { get; private set; }
/// <summary>
/// The selected canddiate index
/// </summary>
public uint CandidatesSelection { get; private set; }
/// <summary>
/// Get the composition attribute at character index.
/// </summary>
/// <param name="index">Character Index</param>
/// <returns>Composition Attribute</returns>
public CompositionAttributes GetCompositionAttr(int index)
{
return (CompositionAttributes)_compattr[index];
}
/// <summary>
/// Get the composition read attribute at character index.
/// </summary>
/// <param name="index">Character Index</param>
/// <returns>Composition Attribute</returns>
public CompositionAttributes GetCompositionReadAttr(int index)
{
return (CompositionAttributes)_compreadattr[index];
}
/// <summary>
/// Constructor, must be called when the window create.
/// </summary>
/// <param name="handle">Handle of the window</param>
/// <param name="showDefaultIMEWindow">True if you want to display the default IME window</param>
internal IMENativeWindow(WinFormsIMEHandler imeHandler, IntPtr handle, bool showDefaultIMEWindow = false)
{
this._imeHandler = imeHandler;
this._context = IntPtr.Zero;
this.Candidates = new string[0];
this._compcurpos = new IMMCompositionInt(IMM.GCSCursorPos);
this._compstr = new IMMCompositionString(IMM.GCSCompStr);
this._compclause = new IMMCompositionString(IMM.GCSCompClause);
this._compattr = new IMMCompositionString(IMM.GCSCompAttr);
this._compread = new IMMCompositionString(IMM.GCSCompReadStr);
this._compreadclause = new IMMCompositionString(IMM.GCSCompReadClause);
this._compreadattr = new IMMCompositionString(IMM.GCSCompReadAttr);
this._resstr = new IMMCompositionString(IMM.GCSResultStr);
this._resclause = new IMMCompositionString(IMM.GCSResultClause);
this._resread = new IMMCompositionString(IMM.GCSResultReadStr);
this._resreadclause = new IMMCompositionString(IMM.GCSResultReadClause);
this._showIMEWin = showDefaultIMEWindow;
AssignHandle(handle);
}
/// <summary>
/// Enable the IME
/// </summary>
public void EnableIME()
{
IsEnabled = true;
IMM.DestroyCaret();
IMM.CreateCaret(Handle, IntPtr.Zero, 1, 1);
_context = IMM.ImmGetContext(Handle);
if (_context != IntPtr.Zero)
{
IMM.ImmAssociateContext(Handle, _context);
IMM.ImmReleaseContext(Handle, _context);
return;
}
// This fix the bug that _context is 0 on fullscreen mode.
ImeContext.Enable(Handle);
}
/// <summary>
/// Disable the IME
/// </summary>
public void DisableIME()
{
IsEnabled = false;
IMM.DestroyCaret();
IMM.ImmAssociateContext(Handle, IntPtr.Zero);
}
public void SetTextInputRect(ref Rectangle rect)
{
_context = IMM.ImmGetContext(Handle);
var candidateForm = new IMM.CandidateForm(new IMM.Point(rect.X, rect.Y));
IMM.ImmSetCandidateWindow(_context, ref candidateForm);
IMM.SetCaretPos(rect.X, rect.Y);
IMM.ImmReleaseContext(Handle, _context);
}
/// <summary>
/// Dispose everything
/// </summary>
public void Dispose()
{
if (!_disposed)
{
ReleaseHandle();
_disposed = true;
}
}
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
case IMM.ImeSetContext:
if (msg.WParam.ToInt32() == 1)
{
if (IsEnabled)
EnableIME();
if (!_showIMEWin)
msg.LParam = (IntPtr)0;
}
break;
case IMM.InputLanguageChange:
return;
case IMM.ImeNotify:
IMENotify(msg.WParam.ToInt32());
if (!_showIMEWin) return;
break;
case IMM.ImeStartCompostition:
IMEStartComposion(msg.LParam.ToInt32());
return;
case IMM.ImeComposition:
IMESetContextForAll();
IMEComposition(msg.LParam.ToInt32());
IMM.ImmReleaseContext(Handle, _context);
break;
case IMM.ImeEndComposition:
IMESetContextForAll();
IMEEndComposition(msg.LParam.ToInt32());
IMM.ImmReleaseContext(Handle, _context);
if (!_showIMEWin) return;
break;
case IMM.Char:
CharEvent(msg.WParam.ToInt32());
break;
}
base.WndProc(ref msg);
}
private void ClearComposition()
{
_compstr.Clear();
_compclause.Clear();
_compattr.Clear();
_compread.Clear();
_compreadclause.Clear();
_compreadattr.Clear();
}
private void ClearResult()
{
_resstr.Clear();
_resclause.Clear();
_resread.Clear();
_resreadclause.Clear();
}
#region IME Message Handlers
private void IMESetContextForAll()
{
_context = IMM.ImmGetContext(Handle);
_compcurpos.IMEHandle = _context;
_compstr.IMEHandle = _context;
_compclause.IMEHandle = _context;
_compattr.IMEHandle = _context;
_compread.IMEHandle = _context;
_compreadclause.IMEHandle = _context;
_compreadattr.IMEHandle = _context;
_resstr.IMEHandle = _context;
_resclause.IMEHandle = _context;
_resread.IMEHandle = _context;
_resreadclause.IMEHandle = _context;
}
private void IMENotify(int WParam)
{
switch (WParam)
{
case IMM.ImnSetOpenStatus:
_context = IMM.ImmGetContext(Handle);
IsIMEOpen = IMM.ImmGetOpenStatus(_context);
System.Diagnostics.Trace.WriteLine(string.Format("IsIMEOpen: {0}", IsIMEOpen ? "True" : "False"));
break;
case IMM.ImnOpenCandidate:
case IMM.ImnChangeCandidate:
IMEChangeCandidate();
break;
case IMM.ImnCloseCandidate:
IMECloseCandidate();
break;
case IMM.ImnPrivate:
break;
default:
break;
}
}
private void IMEChangeCandidate()
{
_context = IMM.ImmGetContext(Handle);
uint length = IMM.ImmGetCandidateList(_context, 0, IntPtr.Zero, 0);
if (length > 0)
{
IntPtr pointer = Marshal.AllocHGlobal((int)length);
length = IMM.ImmGetCandidateList(_context, 0, pointer, length);
IMM.CandidateList cList = (IMM.CandidateList)Marshal.PtrToStructure(pointer, typeof(IMM.CandidateList));
CandidatesSelection = cList.dwSelection;
CandidatesPageStart = cList.dwPageStart;
CandidatesPageSize = cList.dwPageSize;
if (cList.dwCount > 1)
{
Candidates = new string[cList.dwCount];
for (int i = 0; i < cList.dwCount; i++)
{
int sOffset = Marshal.ReadInt32(pointer, 24 + 4 * i);
Candidates[i] = Marshal.PtrToStringUni(pointer + sOffset);
}
_imeHandler.OnTextComposition(CompositionString, CompositionCursorPos, new CandidateList {
Candidates = Candidates,
CandidatesPageStart = CandidatesPageStart,
CandidatesPageSize = CandidatesPageSize,
CandidatesSelection = CandidatesSelection
});
}
else
IMECloseCandidate();
Marshal.FreeHGlobal(pointer);
}
IMM.ImmReleaseContext(Handle, _context);
}
private void IMECloseCandidate()
{
CandidatesSelection = CandidatesPageStart = CandidatesPageSize = 0;
Candidates = new string[0];
_imeHandler.OnTextComposition(CompositionString, CompositionCursorPos, null);
}
private void IMEStartComposion(int lParam)
{
ClearComposition();
ClearResult();
_imeHandler.OnTextComposition(string.Empty, 0);
}
private void IMEComposition(int lParam)
{
if (_compstr.Update(lParam))
{
_compclause.Update();
_compattr.Update();
_compread.Update();
_compreadclause.Update();
_compreadattr.Update();
_compcurpos.Update();
_imeHandler.OnTextComposition(CompositionString, CompositionCursorPos);
}
}
private void IMEEndComposition(int lParam)
{
ClearComposition();
if (_resstr.Update(lParam))
{
_resclause.Update();
_resread.Update();
_resreadclause.Update();
_imeHandler.OnTextComposition(string.Empty, 0);
}
}
private void CharEvent(int wParam)
{
var charInput = (char)wParam;
var key = Microsoft.Xna.Framework.Input.Keys.None;
if (!char.IsSurrogate(charInput))
key = (Microsoft.Xna.Framework.Input.Keys) (IMM.VkKeyScanEx(charInput, InputLanguage.CurrentInputLanguage.Handle) & 0xff);
_imeHandler.OnTextInput(charInput, key);
if (IsEnabled)
IMECloseCandidate();
}
#endregion
}
}
#endif