author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 5506 | 202f599c92aa |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
1954 | 25 |
|
26 |
#include "awt.h" |
|
2 | 27 |
#include <imm.h> |
28 |
#include "awt_Component.h" |
|
29 |
#include "awt_InputTextInfor.h" |
|
30 |
||
31 |
#define WCHAR_SZ sizeof(WCHAR) |
|
32 |
#define DWORD_SZ sizeof(DWORD) |
|
33 |
||
34 |
// The start and end index of the result and composition in GCS_INDEX array. |
|
35 |
#define START_RESULTSTR 0 |
|
36 |
#define END_RESULTSTR 3 |
|
37 |
#define START_COMPSTR 4 |
|
38 |
#define END_COMPSTR 8 |
|
39 |
||
40 |
// The GCS_INDEX array is partitioned into 2 parts, one is result string related and the |
|
41 |
// other is composing string related. |
|
42 |
const DWORD AwtInputTextInfor::GCS_INDEX[9]= {GCS_RESULTSTR, GCS_RESULTREADSTR, GCS_RESULTCLAUSE, |
|
43 |
GCS_RESULTREADCLAUSE, GCS_COMPSTR, GCS_COMPREADSTR, |
|
44 |
GCS_COMPCLAUSE, GCS_COMPREADCLAUSE,GCS_COMPATTR}; |
|
45 |
/* Default constructor */ |
|
46 |
AwtInputTextInfor::AwtInputTextInfor() : |
|
47 |
m_flags(0), m_cursorPosW(0), m_jtext(NULL), m_pResultTextInfor(NULL), \ |
|
48 |
m_cStrW(0), m_cReadStrW(0), m_cClauseW(0), m_cReadClauseW(0), m_cAttrW(0), \ |
|
49 |
m_lpStrW(NULL), m_lpReadStrW(NULL), m_lpClauseW(NULL), m_lpReadClauseW(NULL), m_lpAttrW(NULL) |
|
50 |
{} |
|
51 |
||
52 |
||
53 |
/* Retrieve the context data from the current IMC. |
|
54 |
Params: |
|
55 |
HIMC hIMC - the input method context, must NOT be NULL |
|
56 |
LPARAMS flags - message param to WM_IME_COMPOSITION. |
|
57 |
Returns 0 if success. |
|
58 |
*/ |
|
59 |
int |
|
60 |
AwtInputTextInfor::GetContextData(HIMC hIMC, const LPARAM flags) { |
|
61 |
||
62 |
DASSERT(hIMC != 0); |
|
63 |
||
64 |
m_flags = flags; |
|
65 |
// Based on different flags received, we use different GCS_XXX from the |
|
66 |
// GCS_INDEX array. |
|
67 |
int startIndex = 0, endIndex = 0; |
|
68 |
||
69 |
if (flags & GCS_COMPSTR) { |
|
70 |
startIndex = START_COMPSTR; |
|
71 |
endIndex = END_COMPSTR; |
|
72 |
/* For some window input method such as Chinese QuanPing, when the user |
|
73 |
* commits some text, the IMM sends WM_IME_COMPOSITION with GCS_COMPSTR/GCS_RESULTSTR. |
|
74 |
* So we have to extract the result string from IMC. For most of other cases, |
|
75 |
* m_pResultTextInfor is NULL and this is why we choose to have a pointer as its member |
|
76 |
* rather than having a list of the result string information. |
|
77 |
*/ |
|
78 |
if (flags & GCS_RESULTSTR) { |
|
79 |
m_pResultTextInfor = new AwtInputTextInfor; |
|
80 |
m_pResultTextInfor->GetContextData(hIMC, GCS_RESULTSTR); |
|
81 |
} |
|
82 |
} else if (flags & GCS_RESULTSTR) { |
|
83 |
startIndex = START_RESULTSTR; |
|
84 |
endIndex = END_RESULTSTR; |
|
85 |
} else { // unknown flags. |
|
86 |
return -1; |
|
87 |
} |
|
88 |
||
89 |
/* Get the data from the input context */ |
|
90 |
LONG cbData[5] = {0}; |
|
91 |
LPVOID lpData[5] = {NULL}; |
|
92 |
for (int i = startIndex, j = 0; i <= endIndex; i++, j++) { |
|
1954 | 93 |
cbData[j] = ::ImmGetCompositionString(hIMC, GCS_INDEX[i], NULL, 0); |
2 | 94 |
if (cbData[j] == 0) { |
95 |
lpData[j] = NULL; |
|
96 |
} else { |
|
97 |
LPBYTE lpTemp = new BYTE[cbData[j]]; |
|
1954 | 98 |
cbData[j] = ::ImmGetCompositionString(hIMC, GCS_INDEX[i], lpTemp, cbData[j]); |
2 | 99 |
if (IMM_ERROR_GENERAL != cbData[j]) { |
100 |
lpData[j] = (LPVOID)lpTemp; |
|
101 |
} else { |
|
102 |
lpData[j] = NULL; |
|
103 |
return -1; |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
// Assign the context data |
|
109 |
m_cStrW = cbData[0]/WCHAR_SZ; |
|
110 |
m_lpStrW = (LPWSTR)lpData[0]; |
|
111 |
||
112 |
m_cReadStrW = cbData[1]/WCHAR_SZ; |
|
113 |
m_lpReadStrW = (LPWSTR)lpData[1]; |
|
114 |
||
115 |
m_cClauseW = cbData[2]/DWORD_SZ - 1; |
|
116 |
m_lpClauseW = (LPDWORD)lpData[2]; |
|
117 |
||
118 |
m_cReadClauseW = cbData[3]/DWORD_SZ - 1; |
|
119 |
m_lpReadClauseW = (LPDWORD)lpData[3]; |
|
120 |
||
121 |
if (cbData[4] > 0) { |
|
122 |
m_cAttrW = cbData[4]; |
|
123 |
m_lpAttrW = (LPBYTE)lpData[4]; |
|
124 |
} |
|
125 |
||
126 |
// Get the cursor position |
|
127 |
if (flags & GCS_COMPSTR) { |
|
1954 | 128 |
m_cursorPosW = ::ImmGetCompositionString(hIMC, GCS_CURSORPOS, |
2 | 129 |
NULL, 0); |
130 |
} |
|
131 |
||
132 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
133 |
if (m_cStrW > 0) { |
|
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
134 |
m_jtext = MakeJavaString(env, m_lpStrW, m_cStrW); |
2 | 135 |
} |
136 |
||
137 |
// Merge the string if necessary |
|
138 |
if (m_pResultTextInfor != NULL) { |
|
139 |
jstring jresultText = m_pResultTextInfor->GetText(); |
|
140 |
if (m_jtext != NULL && jresultText != NULL) { |
|
141 |
jstring jMergedtext = (jstring)JNU_CallMethodByName(env, NULL, jresultText, |
|
142 |
"concat", |
|
143 |
"(Ljava/lang/String;)Ljava/lang/String;", |
|
144 |
m_jtext).l; |
|
145 |
DASSERT(!safe_ExceptionOccurred(env)); |
|
146 |
DASSERT(jMergedtext != NULL); |
|
147 |
||
148 |
env->DeleteLocalRef(m_jtext); |
|
149 |
m_jtext = jMergedtext; |
|
150 |
} |
|
151 |
else if (m_jtext == NULL && jresultText != NULL) { |
|
152 |
/* No composing text, assign the committed text to m_jtext */ |
|
153 |
m_jtext = (jstring)env->NewLocalRef(jresultText); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
return 0; |
|
158 |
} |
|
159 |
||
160 |
/* |
|
161 |
* Destructor |
|
162 |
* free the pointer in the m_lpInfoStrW array |
|
163 |
*/ |
|
164 |
AwtInputTextInfor::~AwtInputTextInfor() { |
|
165 |
||
166 |
if (m_jtext) { |
|
167 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
168 |
env->DeleteLocalRef(m_jtext); |
|
169 |
m_jtext = NULL; |
|
170 |
} |
|
171 |
||
172 |
delete [] m_lpStrW; |
|
173 |
delete [] m_lpReadStrW; |
|
174 |
delete [] m_lpClauseW; |
|
175 |
delete [] m_lpReadClauseW; |
|
176 |
delete [] m_lpAttrW; |
|
177 |
||
178 |
if (m_pResultTextInfor) { |
|
179 |
delete m_pResultTextInfor; |
|
180 |
m_pResultTextInfor = NULL; |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
||
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
185 |
jstring AwtInputTextInfor::MakeJavaString(JNIEnv* env, LPWSTR lpStrW, int cStrW) { |
2 | 186 |
|
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
187 |
if (env == NULL || lpStrW == NULL || cStrW == 0) { |
1954 | 188 |
return NULL; |
189 |
} else { |
|
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
190 |
return env->NewString(reinterpret_cast<jchar*>(lpStrW), cStrW); |
1954 | 191 |
} |
2 | 192 |
} |
193 |
||
194 |
// |
|
195 |
// Convert Clause and Reading Information for DBCS string to that for Unicode string |
|
196 |
// *lpBndClauseW and *lpReadingClauseW must be deleted by caller. |
|
197 |
// |
|
198 |
int AwtInputTextInfor::GetClauseInfor(int*& lpBndClauseW, jstring*& lpReadingClauseW) { |
|
199 |
||
200 |
if ( m_cStrW ==0 || m_cClauseW ==0 || m_cClauseW != m_cReadClauseW || |
|
201 |
m_lpClauseW == NULL || m_lpReadClauseW == NULL || |
|
202 |
m_lpClauseW[0] != 0 || m_lpClauseW[m_cClauseW] != (DWORD)m_cStrW || |
|
203 |
m_lpReadClauseW[0] != 0 || m_lpReadClauseW[m_cReadClauseW] != (DWORD)m_cReadStrW) { |
|
204 |
lpBndClauseW = NULL; |
|
205 |
lpReadingClauseW = NULL; |
|
206 |
return 0; |
|
207 |
} |
|
208 |
||
209 |
int* bndClauseW = NULL; |
|
210 |
jstring* readingClauseW = NULL; |
|
211 |
||
212 |
//Convert ANSI string caluse information to UNICODE string clause information. |
|
213 |
try { |
|
214 |
bndClauseW = new int[m_cClauseW + 1]; |
|
215 |
readingClauseW = new jstring[m_cClauseW]; |
|
216 |
} catch (std::bad_alloc&) { |
|
217 |
lpBndClauseW = NULL; |
|
218 |
lpReadingClauseW = NULL; |
|
219 |
delete [] bndClauseW; |
|
220 |
throw; |
|
221 |
} |
|
222 |
||
223 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
224 |
||
225 |
for ( int cls = 0; cls < m_cClauseW; cls++ ) { |
|
226 |
bndClauseW[cls] = m_lpClauseW[cls]; |
|
227 |
||
228 |
if ( m_lpReadClauseW[cls + 1] <= (DWORD)m_cReadStrW ) { |
|
229 |
LPWSTR lpHWStrW = m_lpReadStrW + m_lpReadClauseW[cls]; |
|
230 |
int cHWStrW = m_lpReadClauseW[cls+1] - m_lpReadClauseW[cls]; |
|
231 |
||
232 |
if (PRIMARYLANGID(AwtComponent::GetInputLanguage()) == LANG_JAPANESE) { |
|
233 |
LCID lcJPN = MAKELCID(MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT),SORT_DEFAULT); |
|
234 |
// Reading string is given in half width katakana in Japanese Windows |
|
235 |
// Convert it to full width katakana. |
|
1954 | 236 |
int cFWStrW = ::LCMapString(lcJPN, LCMAP_FULLWIDTH, lpHWStrW, cHWStrW, NULL, 0); |
2 | 237 |
LPWSTR lpFWStrW; |
238 |
try { |
|
239 |
lpFWStrW = new WCHAR[cFWStrW]; |
|
240 |
} catch (std::bad_alloc&) { |
|
241 |
lpBndClauseW = NULL; |
|
242 |
lpReadingClauseW = NULL; |
|
243 |
delete [] bndClauseW; |
|
244 |
delete [] readingClauseW; |
|
245 |
throw; |
|
246 |
} |
|
247 |
||
1954 | 248 |
::LCMapString(lcJPN, LCMAP_FULLWIDTH, lpHWStrW, cHWStrW, lpFWStrW, cFWStrW); |
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
249 |
readingClauseW[cls] = MakeJavaString(env, lpFWStrW, cFWStrW); |
2 | 250 |
delete [] lpFWStrW; |
251 |
} else { |
|
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
252 |
readingClauseW[cls] = MakeJavaString(env, lpHWStrW, cHWStrW); |
2 | 253 |
} |
254 |
} |
|
255 |
else { |
|
1955
e4f3afbad6e9
6741364: Some input method problems after the fix for 6585765
art
parents:
1954
diff
changeset
|
256 |
readingClauseW[cls] = NULL; |
2 | 257 |
} |
258 |
} |
|
259 |
||
260 |
bndClauseW[m_cClauseW] = m_cStrW; |
|
261 |
||
262 |
int retVal = 0; |
|
263 |
int cCommittedStrW = GetCommittedTextLength(); |
|
264 |
||
265 |
/* The conditions to merge the clause information are described below: |
|
266 |
Senario 1: |
|
267 |
m_flags & GCS_RESULTSTR is true only, this case m_pResultTextInfor must be NULL. |
|
268 |
No need to merge. |
|
269 |
||
270 |
Senario 2: |
|
271 |
m_flags & GCS_COMPSTR is true only, this case m_pResultTextInfor is also NULL. |
|
272 |
No need to merge either. |
|
273 |
||
274 |
Senario 3: |
|
275 |
m_flags & GCS_COMPSTR and m_flags & GCS_RESULTSTR both yield to true, in this case |
|
276 |
m_pResultTextInfor won't be NULL and if there is nothing to commit though, we don't |
|
277 |
have to merge. Or if the current composing string size is 0, we don't have to merge either. |
|
278 |
||
279 |
So in clusion, the three conditions not not merge are: |
|
21278 | 280 |
1. no committed string |
2 | 281 |
2. m_pResultTextInfor points to NULL |
282 |
3. the current string size is 0; |
|
283 |
||
284 |
Same rule applies to merge the attribute information. |
|
285 |
*/ |
|
286 |
if (m_cStrW == 0 || cCommittedStrW == 0 || |
|
287 |
m_pResultTextInfor == NULL) { |
|
288 |
lpBndClauseW = bndClauseW; |
|
289 |
lpReadingClauseW = readingClauseW; |
|
290 |
retVal = m_cClauseW; |
|
291 |
} else { /* partial commit case */ |
|
292 |
int* bndResultClauseW = NULL; |
|
293 |
jstring* readingResultClauseW = NULL; |
|
294 |
int cResultClauseW = m_pResultTextInfor->GetClauseInfor(bndResultClauseW, readingResultClauseW); |
|
295 |
||
296 |
// Concatenate Clause information. |
|
297 |
int cMergedClauseW = m_cClauseW + cResultClauseW; |
|
298 |
int* bndMergedClauseW = NULL; |
|
299 |
jstring* readingMergedClauseW = NULL; |
|
300 |
try { |
|
301 |
bndMergedClauseW = new int[cMergedClauseW+1]; |
|
302 |
readingMergedClauseW = new jstring[cMergedClauseW]; |
|
303 |
} catch (std::bad_alloc&) { |
|
304 |
delete [] bndMergedClauseW; |
|
305 |
throw; |
|
306 |
} |
|
307 |
||
308 |
int i = 0; |
|
309 |
if (cResultClauseW > 0 && bndResultClauseW && readingResultClauseW) { |
|
310 |
for (; i < cResultClauseW; i++) { |
|
311 |
bndMergedClauseW[i] = bndResultClauseW[i]; |
|
312 |
readingMergedClauseW[i] = readingResultClauseW[i]; |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
if (m_cClauseW > 0 && bndClauseW && readingClauseW) { |
|
317 |
for(int j = 0; j < m_cClauseW; j++, i++) { |
|
318 |
bndMergedClauseW[i] = bndClauseW[j] + cCommittedStrW; |
|
319 |
readingMergedClauseW[i] = readingClauseW[j]; |
|
320 |
} |
|
321 |
} |
|
322 |
delete [] bndClauseW; |
|
323 |
delete [] readingClauseW; |
|
324 |
bndMergedClauseW[cMergedClauseW] = m_cStrW + cCommittedStrW; |
|
325 |
lpBndClauseW = bndMergedClauseW; |
|
326 |
lpReadingClauseW = readingMergedClauseW; |
|
327 |
retVal = cMergedClauseW; |
|
328 |
} |
|
329 |
||
330 |
return retVal; |
|
331 |
} |
|
332 |
||
333 |
// |
|
334 |
// Convert Attribute Information for DBCS string to that for Unicode string |
|
335 |
// *lpBndAttrW and *lpValAttrW must be deleted by caller. |
|
336 |
// |
|
337 |
int AwtInputTextInfor::GetAttributeInfor(int*& lpBndAttrW, BYTE*& lpValAttrW) { |
|
338 |
if (m_cStrW == 0 || m_cAttrW != m_cStrW) { |
|
339 |
lpBndAttrW = NULL; |
|
340 |
lpValAttrW = NULL; |
|
341 |
||
342 |
return 0; |
|
343 |
} |
|
344 |
||
345 |
int* bndAttrW = NULL; |
|
346 |
BYTE* valAttrW = NULL; |
|
347 |
||
348 |
//Scan attribute byte array and make attribute run information. |
|
349 |
try { |
|
350 |
bndAttrW = new int[m_cAttrW + 1]; |
|
351 |
valAttrW = new BYTE[m_cAttrW]; |
|
352 |
} catch (std::bad_alloc&) { |
|
353 |
lpBndAttrW = NULL; |
|
354 |
lpValAttrW = NULL; |
|
355 |
delete [] bndAttrW; |
|
356 |
throw; |
|
357 |
} |
|
358 |
||
359 |
int cAttrWT = 0; |
|
360 |
bndAttrW[0] = 0; |
|
361 |
valAttrW[0] = m_lpAttrW[0]; |
|
362 |
/* remove duplicate attribute in the m_lpAttrW array. */ |
|
363 |
for ( int offW = 1; offW < m_cAttrW; offW++ ) { |
|
364 |
if ( m_lpAttrW[offW] != valAttrW[cAttrWT]) { |
|
365 |
cAttrWT++; |
|
366 |
bndAttrW[cAttrWT] = offW; |
|
367 |
valAttrW[cAttrWT] = m_lpAttrW[offW]; |
|
368 |
} |
|
369 |
} |
|
370 |
bndAttrW[++cAttrWT] = m_cStrW; |
|
371 |
||
372 |
int retVal = 0; |
|
373 |
||
374 |
int cCommittedStrW = GetCommittedTextLength(); |
|
375 |
if (m_cStrW == 0 || |
|
376 |
cCommittedStrW == 0 || m_pResultTextInfor == NULL) { |
|
377 |
lpBndAttrW = bndAttrW; |
|
378 |
lpValAttrW = valAttrW; |
|
379 |
retVal = cAttrWT; |
|
380 |
} else { |
|
381 |
int cMergedAttrW = 1 + cAttrWT; |
|
382 |
int* bndMergedAttrW = NULL; |
|
383 |
BYTE* valMergedAttrW = NULL; |
|
384 |
try { |
|
385 |
bndMergedAttrW = new int[cMergedAttrW+1]; |
|
386 |
valMergedAttrW = new BYTE[cMergedAttrW]; |
|
387 |
} catch (std::bad_alloc&) { |
|
388 |
delete [] bndMergedAttrW; |
|
389 |
throw; |
|
390 |
} |
|
391 |
bndMergedAttrW[0] = 0; |
|
392 |
valMergedAttrW[0] = ATTR_CONVERTED; |
|
393 |
for (int j = 0; j < cAttrWT; j++) { |
|
394 |
bndMergedAttrW[j+1] = bndAttrW[j]+cCommittedStrW; |
|
395 |
valMergedAttrW[j+1] = valAttrW[j]; |
|
396 |
} |
|
397 |
bndMergedAttrW[cMergedAttrW] = m_cStrW + cCommittedStrW; |
|
398 |
||
399 |
delete [] bndAttrW; |
|
400 |
delete [] valAttrW; |
|
401 |
lpBndAttrW = bndMergedAttrW; |
|
402 |
lpValAttrW = valMergedAttrW; |
|
403 |
retVal = cMergedAttrW; |
|
404 |
} |
|
405 |
||
406 |
return retVal; |
|
407 |
} |
|
408 |
||
409 |
// |
|
410 |
// Returns the cursor position of the current composition. |
|
411 |
// returns 0 if the current mode is not GCS_COMPSTR |
|
412 |
// |
|
413 |
int AwtInputTextInfor::GetCursorPosition() const { |
|
414 |
if (m_flags & GCS_COMPSTR) { |
|
415 |
return m_cursorPosW; |
|
416 |
} else { |
|
417 |
return 0; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
||
422 |
// |
|
423 |
// Returns the committed text length |
|
424 |
// |
|
425 |
int AwtInputTextInfor::GetCommittedTextLength() const { |
|
426 |
||
427 |
if ((m_flags & GCS_COMPSTR) && m_pResultTextInfor) { |
|
428 |
return m_pResultTextInfor->GetCommittedTextLength(); |
|
429 |
} |
|
430 |
||
431 |
if (m_flags & GCS_RESULTSTR) |
|
432 |
return m_cStrW; |
|
433 |
else |
|
434 |
return 0; |
|
435 |
} |