-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathIMEHandler.cs
More file actions
224 lines (182 loc) · 7.82 KB
/
IMEHandler.cs
File metadata and controls
224 lines (182 loc) · 7.82 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
// Copyright © 2018 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Text;
using CefSharp.Structs;
using Range = CefSharp.Structs.Range;
namespace CefSharp.Wpf.Internals
{
/// <summary>
/// ImeHandler provides implementation when message WM_IME_COMPOSITION is received.
/// </summary>
public static class ImeHandler
{
// Black SkColor value for underline.
public static uint ColorUNDERLINE = 0xFF000000;
// Transparent SkColor value for background.
public static uint ColorBKCOLOR = 0x00000000;
public static bool GetResult(IntPtr hwnd, uint lParam, out string text)
{
var hIMC = ImeNative.ImmGetContext(hwnd);
var ret = GetString(hIMC, lParam, ImeNative.GCS_RESULTSTR, out text);
ImeNative.ImmReleaseContext(hwnd, hIMC);
return ret;
}
public static bool GetComposition(IntPtr hwnd, uint lParam, List<CompositionUnderline> underlines, ref int compositionStart, out string text)
{
var hIMC = ImeNative.ImmGetContext(hwnd);
bool ret = GetString(hIMC, lParam, ImeNative.GCS_COMPSTR, out text);
if (ret)
{
GetCompositionInfo(hwnd, lParam, text, underlines, ref compositionStart);
}
ImeNative.ImmReleaseContext(hwnd, hIMC);
return ret;
}
private static bool GetString(IntPtr hIMC, uint lParam, uint type, out string text)
{
text = string.Empty;
if (!IsParam(lParam, type))
{
return false;
}
var strLen = ImeNative.ImmGetCompositionString(hIMC, type, null, 0);
if (strLen <= 0)
{
return false;
}
// buffer contains char (2 bytes)
byte[] buffer = new byte[strLen];
ImeNative.ImmGetCompositionString(hIMC, type, buffer, strLen);
text = Encoding.Unicode.GetString(buffer);
return true;
}
private static void GetCompositionInfo(IntPtr hwnd, uint lParam, string text, List<CompositionUnderline> underlines, ref int compositionStart)
{
var hIMC = ImeNative.ImmGetContext(hwnd);
underlines.Clear();
byte[] attributes = null;
int targetStart = text.Length;
int targetEnd = text.Length;
if (IsParam(lParam, ImeNative.GCS_COMPATTR))
{
attributes = GetCompositionSelectionRange(hIMC, ref targetStart, ref targetEnd);
}
// Retrieve the selection range information. If CS_NOMOVECARET is specified
// it means the cursor should not be moved and we therefore place the caret at
// the beginning of the composition string. Otherwise we should honour the
// GCS_CURSORPOS value if it's available.
if (!IsParam(lParam, ImeNative.CS_NOMOVECARET) && IsParam(lParam, ImeNative.GCS_CURSORPOS))
{
// IMM32 does not support non-zero-width selection in a composition. So
// always use the caret position as selection range.
int cursor = (int)ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_CURSORPOS, null, 0);
compositionStart = cursor;
}
else
{
compositionStart = 0;
}
if (attributes != null &&
// character before
((compositionStart > 0 && (compositionStart - 1) < attributes.Length && attributes[compositionStart - 1] == ImeNative.ATTR_INPUT)
||
// character after
(compositionStart >= 0 && compositionStart < attributes.Length && attributes[compositionStart] == ImeNative.ATTR_INPUT)))
{
// as MS does with their ime implementation we should only use the GCS_CURSORPOS if the character
// before or after is new input.
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Documents/ImmComposition.cs,1079
}
else
{
compositionStart = text.Length;
}
if (IsParam(lParam, ImeNative.GCS_COMPCLAUSE))
{
GetCompositionUnderlines(hIMC, targetStart, targetEnd, underlines);
}
if (underlines.Count == 0)
{
var range = new Range();
bool thick = false;
if (targetStart > 0)
{
range = new Range(0, targetStart);
}
if (targetEnd > targetStart)
{
range = new Range(targetStart, targetEnd);
thick = true;
}
if (targetEnd < text.Length)
{
range = new Range(targetEnd, text.Length);
}
underlines.Add(new CompositionUnderline(range, ColorUNDERLINE, ColorBKCOLOR, thick));
}
ImeNative.ImmReleaseContext(hwnd, hIMC);
}
private static void GetCompositionUnderlines(IntPtr hIMC, int targetStart, int targetEnd, List<CompositionUnderline> underlines)
{
var clauseSize = ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPCLAUSE, null, 0);
if (clauseSize <= 0)
{
return;
}
int clauseLength = (int)clauseSize / sizeof(Int32);
// buffer contains 32 bytes (4 bytes) array
var clauseData = new byte[(int)clauseSize];
ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPCLAUSE, clauseData, clauseSize);
var clauseLength_1 = clauseLength - 1;
for (int i = 0; i < clauseLength_1; i++)
{
int from = BitConverter.ToInt32(clauseData, i * sizeof(Int32));
int to = BitConverter.ToInt32(clauseData, (i + 1) * sizeof(Int32));
var range = new Range(from, to);
bool thick = (range.From >= targetStart && range.To <= targetEnd);
underlines.Add(new CompositionUnderline(range, ColorUNDERLINE, ColorBKCOLOR, thick));
}
}
private static byte[] GetCompositionSelectionRange(IntPtr hIMC, ref int targetStart, ref int targetEnd)
{
var attributeSize = ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPATTR, null, 0);
if (attributeSize <= 0)
{
return null;
}
int start = 0;
int end = 0;
// Buffer contains 8bit array
var attributeData = new byte[attributeSize];
ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPATTR, attributeData, attributeSize);
for (start = 0; start < attributeSize; ++start)
{
if (IsSelectionAttribute(attributeData[start]))
{
break;
}
}
for (end = start; end < attributeSize; ++end)
{
if (!IsSelectionAttribute(attributeData[end]))
{
break;
}
}
targetStart = start;
targetEnd = end;
return attributeData;
}
private static bool IsSelectionAttribute(byte attribute)
{
return (attribute == ImeNative.ATTR_TARGET_CONVERTED || attribute == ImeNative.ATTR_TARGET_NOTCONVERTED);
}
private static bool IsParam(uint lParam, uint type)
{
return (lParam & type) == type;
}
}
}