1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.ole.win32.*;
20 import org.eclipse.swt.internal.win32.*;
21
22 /**
23 * Instances of this class represent input method editors.
24 * These are typically in-line pre-edit text areas that allow
25 * the user to compose characters from Far Eastern languages
26 * such as Japanese, Chinese or Korean.
27 *
28 * <dl>
29 * <dt><b>Styles:</b></dt>
30 * <dd>(none)</dd>
31 * <dt><b>Events:</b></dt>
32 * <dd>ImeComposition</dd>
33 * </dl>
34 * <p>
35 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
36 * </p>
37 *
38 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
39 *
40 * @since 3.4
41 * @noextend This class is not intended to be subclassed by clients.
42 */
43 public class IME extends Widget {
44 Canvas parent;
45 int caretOffset;
46 int startOffset;
47 int commitCount;
48 String text;
49 int [] ranges;
50 TextStyle [] styles;
51
52 static final int WM_MSIME_MOUSE = OS.RegisterWindowMessage (new TCHAR (0, "MSIMEMouseOperation", true)); //$NON-NLS-1$
53
54 /* TextLayout has a copy of these constants */
55 static final int UNDERLINE_IME_DOT = 1 << 16;
56 static final int UNDERLINE_IME_DASH = 2 << 16;
57 static final int UNDERLINE_IME_THICK = 3 << 16;
58
59 /**
60 * Prevents uninitialized instances from being created outside the package.
61 */
IME()62 IME () {
63 }
64
65 /**
66 * Constructs a new instance of this class given its parent
67 * and a style value describing its behavior and appearance.
68 * <p>
69 * The style value is either one of the style constants defined in
70 * class <code>SWT</code> which is applicable to instances of this
71 * class, or must be built by <em>bitwise OR</em>'ing together
72 * (that is, using the <code>int</code> "|" operator) two or more
73 * of those <code>SWT</code> style constants. The class description
74 * lists the style constants that are applicable to the class.
75 * Style bits are also inherited from superclasses.
76 * </p>
77 *
78 * @param parent a canvas control which will be the parent of the new instance (cannot be null)
79 * @param style the style of control to construct
80 *
81 * @exception IllegalArgumentException <ul>
82 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
83 * </ul>
84 * @exception SWTException <ul>
85 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
86 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
87 * </ul>
88 *
89 * @see Widget#checkSubclass
90 * @see Widget#getStyle
91 */
IME(Canvas parent, int style)92 public IME (Canvas parent, int style) {
93 super (parent, style);
94 this.parent = parent;
95 createWidget ();
96 }
97
createWidget()98 void createWidget () {
99 text = ""; //$NON-NLS-1$
100 startOffset = -1;
101 if (parent.getIME () == null) {
102 parent.setIME (this);
103 }
104 }
105
106 /**
107 * Returns the offset of the caret from the start of the document.
108 * -1 means that there is currently no active composition.
109 * The caret is within the current composition.
110 *
111 * @return the caret offset
112 *
113 * @exception SWTException <ul>
114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
116 * </ul>
117 */
getCaretOffset()118 public int getCaretOffset () {
119 checkWidget ();
120 return startOffset + caretOffset;
121 }
122
123 /**
124 * Returns the commit count of the composition. This is the
125 * number of characters that have been composed. When the
126 * commit count is equal to the length of the composition
127 * text, then the in-line edit operation is complete.
128 *
129 * @return the commit count
130 *
131 * @exception SWTException <ul>
132 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
133 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
134 * </ul>
135 *
136 * @see IME#getText
137 */
getCommitCount()138 public int getCommitCount () {
139 checkWidget ();
140 return commitCount;
141 }
142
143 /**
144 * Returns the offset of the composition from the start of the document.
145 * This is the start offset of the composition within the document and
146 * in not changed by the input method editor itself during the in-line edit
147 * session.
148 *
149 * @return the offset of the composition
150 *
151 * @exception SWTException <ul>
152 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
153 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
154 * </ul>
155 */
getCompositionOffset()156 public int getCompositionOffset () {
157 checkWidget ();
158 return startOffset;
159 }
160
getDisplayAttribute(short langid, int attInfo)161 TF_DISPLAYATTRIBUTE getDisplayAttribute (short langid, int attInfo) {
162 long [] ppv = new long [1];
163 int hr = COM.CoCreateInstance (COM.CLSID_TF_InputProcessorProfiles, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_ITfInputProcessorProfiles, ppv);
164 TF_DISPLAYATTRIBUTE pda = null;
165 if (hr == OS.S_OK) {
166 ITfInputProcessorProfiles pProfiles = new ITfInputProcessorProfiles (ppv [0]);
167 GUID pclsid = new GUID ();
168 GUID pguidProfile = new GUID ();
169 hr = pProfiles.GetDefaultLanguageProfile (langid, COM.GUID_TFCAT_TIP_KEYBOARD, pclsid, pguidProfile);
170 if (hr == OS.S_OK) {
171 hr = COM.CoCreateInstance (pclsid, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_ITfDisplayAttributeProvider, ppv);
172 if (hr == OS.S_OK) {
173 ITfDisplayAttributeProvider pProvider = new ITfDisplayAttributeProvider (ppv [0]);
174 hr = pProvider.EnumDisplayAttributeInfo (ppv);
175 if (hr == OS.S_OK) {
176 IEnumTfDisplayAttributeInfo pEnum = new IEnumTfDisplayAttributeInfo (ppv [0]);
177 TF_DISPLAYATTRIBUTE tempPda = new TF_DISPLAYATTRIBUTE ();
178 while ((hr = pEnum.Next (1, ppv, null)) == OS.S_OK) {
179 ITfDisplayAttributeInfo pDispInfo = new ITfDisplayAttributeInfo (ppv [0]);
180 pDispInfo.GetAttributeInfo (tempPda);
181 pDispInfo.Release ();
182 if (tempPda.bAttr == attInfo) {
183 pda = tempPda;
184 break;
185 }
186 }
187 pEnum.Release ();
188 }
189 pProvider.Release ();
190 }
191 }
192 pProfiles.Release ();
193 }
194 if (pda == null) {
195 pda = new TF_DISPLAYATTRIBUTE ();
196 switch (attInfo) {
197 case OS.TF_ATTR_INPUT:
198 pda.lsStyle = OS.TF_LS_SQUIGGLE;
199 break;
200 case OS.TF_ATTR_CONVERTED:
201 case OS.TF_ATTR_TARGET_CONVERTED:
202 pda.lsStyle = OS.TF_LS_SOLID;
203 pda.fBoldLine = attInfo == OS.TF_ATTR_TARGET_CONVERTED;
204 break;
205 }
206 }
207 return pda;
208 }
209
210 /**
211 * Returns the ranges for the style that should be applied during the
212 * in-line edit session.
213 * <p>
214 * The ranges array contains start and end pairs. Each pair refers to
215 * the corresponding style in the styles array. For example, the pair
216 * that starts at ranges[n] and ends at ranges[n+1] uses the style
217 * at styles[n/2] returned by <code>getStyles()</code>.
218 * </p>
219 * @return the ranges for the styles
220 *
221 * @exception SWTException <ul>
222 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
223 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
224 * </ul>
225 *
226 * @see IME#getStyles
227 */
getRanges()228 public int [] getRanges () {
229 checkWidget ();
230 if (ranges == null) return new int [0];
231 int [] result = new int [ranges.length];
232 for (int i = 0; i < result.length; i++) {
233 result [i] = ranges [i] + startOffset;
234 }
235 return result;
236 }
237
238 /**
239 * Returns the styles for the ranges.
240 * <p>
241 * The ranges array contains start and end pairs. Each pair refers to
242 * the corresponding style in the styles array. For example, the pair
243 * that starts at ranges[n] and ends at ranges[n+1] uses the style
244 * at styles[n/2].
245 * </p>
246 *
247 * @return the ranges for the styles
248 *
249 * @exception SWTException <ul>
250 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
251 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
252 * </ul>
253 *
254 * @see IME#getRanges
255 */
getStyles()256 public TextStyle [] getStyles () {
257 checkWidget ();
258 if (styles == null) return new TextStyle [0];
259 TextStyle [] result = new TextStyle [styles.length];
260 System.arraycopy (styles, 0, result, 0, styles.length);
261 return result;
262 }
263
264 /**
265 * Returns the composition text.
266 * <p>
267 * The text for an IME is the characters in the widget that
268 * are in the current composition. When the commit count is
269 * equal to the length of the composition text, then the
270 * in-line edit operation is complete.
271 * </p>
272 *
273 * @return the widget text
274 *
275 * @exception SWTException <ul>
276 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
277 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
278 * </ul>
279 */
getText()280 public String getText () {
281 checkWidget ();
282 return text;
283 }
284
285 /**
286 * Returns <code>true</code> if the caret should be wide, and
287 * <code>false</code> otherwise. In some languages, for example
288 * Korean, the caret is typically widened to the width of the
289 * current character in the in-line edit session.
290 *
291 * @return the wide caret state
292 *
293 * @exception SWTException <ul>
294 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
295 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
296 * </ul>
297 */
getWideCaret()298 public boolean getWideCaret() {
299 checkWidget ();
300 long layout = OS.GetKeyboardLayout (0);
301 short langID = (short)OS.LOWORD (layout);
302 return OS.PRIMARYLANGID (langID) == OS.LANG_KOREAN;
303 }
304
isInlineEnabled()305 boolean isInlineEnabled () {
306 return OS.IsDBLocale && hooks (SWT.ImeComposition);
307 }
308
309 @Override
releaseParent()310 void releaseParent () {
311 super.releaseParent ();
312 if (this == parent.getIME ()) parent.setIME (null);
313 }
314
315 @Override
releaseWidget()316 void releaseWidget () {
317 super.releaseWidget ();
318 parent = null;
319 text = null;
320 styles = null;
321 ranges = null;
322 }
323
324 /**
325 * Sets the offset of the composition from the start of the document.
326 * This is the start offset of the composition within the document and
327 * in not changed by the input method editor itself during the in-line edit
328 * session but may need to be changed by clients of the IME. For example,
329 * if during an in-line edit operation, a text editor inserts characters
330 * above the IME, then the IME must be informed that the composition
331 * offset has changed.
332 *
333 * @param offset the offset of the composition
334 *
335 * @exception SWTException <ul>
336 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
337 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
338 * </ul>
339 */
setCompositionOffset(int offset)340 public void setCompositionOffset (int offset) {
341 checkWidget ();
342 if (offset < 0) return;
343 if (startOffset != -1) {
344 startOffset = offset;
345 }
346 }
347
WM_IME_COMPOSITION(long wParam, long lParam)348 LRESULT WM_IME_COMPOSITION (long wParam, long lParam) {
349 if (!isInlineEnabled ()) return null;
350 ranges = null;
351 styles = null;
352 caretOffset = commitCount = 0;
353 long hwnd = parent.handle;
354 long hIMC = OS.ImmGetContext (hwnd);
355 if (hIMC != 0) {
356 char [] buffer = null;
357 if ((lParam & OS.GCS_RESULTSTR) != 0) {
358 int length = OS.ImmGetCompositionString (hIMC, OS.GCS_RESULTSTR, (char [])null, 0);
359 if (length > 0) {
360 buffer = new char [length / TCHAR.sizeof];
361 OS.ImmGetCompositionString (hIMC, OS.GCS_RESULTSTR, buffer, length);
362 if (startOffset == -1) {
363 Event event = new Event ();
364 event.detail = SWT.COMPOSITION_SELECTION;
365 sendEvent (SWT.ImeComposition, event);
366 startOffset = event.start;
367 }
368 Event event = new Event ();
369 event.detail = SWT.COMPOSITION_CHANGED;
370 event.start = startOffset;
371 event.end = startOffset + text.length();
372 event.text = text = buffer != null ? new String (buffer) : ""; //$NON-NLS-1$
373 commitCount = text.length ();
374 sendEvent (SWT.ImeComposition, event);
375 String chars = text;
376 text = ""; //$NON-NLS-1$
377 startOffset = -1;
378 commitCount = 0;
379 if (event.doit) {
380 Display display = this.display;
381 display.lastKey = 0;
382 display.lastVirtual = display.lastNull = display.lastDead = false;
383 length = chars.length ();
384 for (int i = 0; i < length; i++) {
385 char c = chars.charAt (i);
386 display.lastAscii = c;
387 event = new Event ();
388 event.character = c;
389 parent.sendEvent (SWT.KeyDown, event);
390 }
391 }
392 }
393 if ((lParam & OS.GCS_COMPSTR) == 0) return LRESULT.ONE;
394 }
395 buffer = null;
396 if ((lParam & OS.GCS_COMPSTR) != 0) {
397 int length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPSTR, (char [])null, 0);
398 if (length > 0) {
399 buffer = new char [length / TCHAR.sizeof];
400 OS.ImmGetCompositionString (hIMC, OS.GCS_COMPSTR, buffer, length);
401 if ((lParam & OS.GCS_CURSORPOS) != 0) {
402 caretOffset = OS.ImmGetCompositionString (hIMC, OS.GCS_CURSORPOS, (char [])null, 0);
403 }
404 int [] clauses = null;
405 if ((lParam & OS.GCS_COMPCLAUSE) != 0) {
406 length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPCLAUSE, (int [])null, 0);
407 if (length > 0) {
408 clauses = new int [length / 4];
409 OS.ImmGetCompositionString (hIMC, OS.GCS_COMPCLAUSE, clauses, length);
410 }
411 }
412 if ((lParam & OS.GCS_COMPATTR) != 0 && clauses != null) {
413 length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPATTR, (byte [])null, 0);
414 if (length > 0) {
415 byte [] attrs = new byte [length];
416 OS.ImmGetCompositionString (hIMC, OS.GCS_COMPATTR, attrs, length);
417 length = clauses.length - 1;
418 ranges = new int [length * 2];
419 styles = new TextStyle [length];
420 long layout = OS.GetKeyboardLayout (0);
421 short langID = (short)OS.LOWORD (layout);
422 TF_DISPLAYATTRIBUTE attr = null;
423 TextStyle style = null;
424 for (int i = 0; i < length; i++) {
425 ranges [i * 2] = clauses [i];
426 ranges [i * 2 + 1] = clauses [i + 1] - 1;
427 styles [i] = style = new TextStyle ();
428 /* Added length check to avoid possibility of AIOOB, bug 444926 */
429 if (clauses [i] >= 0 && clauses [i] < attrs.length) {
430 attr = getDisplayAttribute (langID, attrs [clauses [i]]);
431 if (attr != null) {
432 switch (attr.crText.type) {
433 case OS.TF_CT_COLORREF:
434 style.foreground = Color.win32_new (display, attr.crText.cr);
435 break;
436 case OS.TF_CT_SYSCOLOR:
437 int colorRef = OS.GetSysColor (attr.crText.cr);
438 style.foreground = Color.win32_new (display, colorRef);
439 break;
440 }
441 switch (attr.crBk.type) {
442 case OS.TF_CT_COLORREF:
443 style.background = Color.win32_new (display, attr.crBk.cr);
444 break;
445 case OS.TF_CT_SYSCOLOR:
446 int colorRef = OS.GetSysColor (attr.crBk.cr);
447 style.background = Color.win32_new (display, colorRef);
448 break;
449 }
450 switch (attr.crLine.type) {
451 case OS.TF_CT_COLORREF:
452 style.underlineColor = Color.win32_new (display, attr.crLine.cr);
453 break;
454 case OS.TF_CT_SYSCOLOR:
455 int colorRef = OS.GetSysColor (attr.crLine.cr);
456 style.underlineColor = Color.win32_new (display, colorRef);
457 break;
458 }
459 style.underline = attr.lsStyle != OS.TF_LS_NONE;
460 switch (attr.lsStyle) {
461 case OS.TF_LS_SQUIGGLE:
462 style.underlineStyle = SWT.UNDERLINE_SQUIGGLE;
463 break;
464 case OS.TF_LS_DASH:
465 style.underlineStyle = UNDERLINE_IME_DASH;
466 break;
467 case OS.TF_LS_DOT:
468 style.underlineStyle = UNDERLINE_IME_DOT;
469 break;
470 case OS.TF_LS_SOLID:
471 style.underlineStyle = attr.fBoldLine ? UNDERLINE_IME_THICK : SWT.UNDERLINE_SINGLE;
472 break;
473 }
474 }
475 }
476 }
477 }
478 }
479 }
480 OS.ImmReleaseContext (hwnd, hIMC);
481 }
482 int end = startOffset + text.length();
483 if (startOffset == -1) {
484 Event event = new Event ();
485 event.detail = SWT.COMPOSITION_SELECTION;
486 sendEvent (SWT.ImeComposition, event);
487 startOffset = event.start;
488 end = event.end;
489 }
490 Event event = new Event ();
491 event.detail = SWT.COMPOSITION_CHANGED;
492 event.start = startOffset;
493 event.end = end;
494 event.text = text = buffer != null ? new String (buffer) : ""; //$NON-NLS-1$
495 sendEvent (SWT.ImeComposition, event);
496 if (text.length() == 0) {
497 startOffset = -1;
498 ranges = null;
499 styles = null;
500 }
501 }
502 return LRESULT.ONE;
503 }
504
WM_IME_COMPOSITION_START(long wParam, long lParam)505 LRESULT WM_IME_COMPOSITION_START (long wParam, long lParam) {
506 return isInlineEnabled () ? LRESULT.ONE : null;
507 }
508
WM_IME_ENDCOMPOSITION(long wParam, long lParam)509 LRESULT WM_IME_ENDCOMPOSITION (long wParam, long lParam) {
510 // Reset defaults. Otherwise the next composition overwrites the previous one.
511 startOffset = -1;
512 caretOffset = 0;
513 return isInlineEnabled () ? LRESULT.ONE : null;
514 }
515
WM_KEYDOWN(long wParam, long lParam)516 LRESULT WM_KEYDOWN (long wParam, long lParam) {
517 if (wParam == OS.VK_HANJA) {
518 long hKL = OS.GetKeyboardLayout (0);
519 short langID = (short)OS.LOWORD (hKL);
520 if (OS.PRIMARYLANGID (langID) == OS.LANG_KOREAN) {
521 Event event = new Event ();
522 event.detail = SWT.COMPOSITION_SELECTION;
523 sendEvent (SWT.ImeComposition, event);
524 if (event.start == event.end) {
525 event.text = null;
526 event.end = event.start + 1;
527 sendEvent (SWT.ImeComposition, event);
528 }
529 if (event.text != null && event.text.length() > 0) {
530 int length = event.text.length();
531 if (length > 1) {
532 event.end = event.start + 1;
533 }
534 long hwnd = parent.handle;
535 long hIMC = OS.ImmGetContext (hwnd);
536 TCHAR buffer = new TCHAR (0, event.text, true);
537 long rc = OS.ImmEscape(hKL, hIMC, OS.IME_ESC_HANJA_MODE, buffer);
538 if (rc != 0) {
539 sendEvent (SWT.ImeComposition, event);
540 }
541 }
542 }
543 }
544 return null;
545 }
546
WM_KILLFOCUS(long wParam, long lParam)547 LRESULT WM_KILLFOCUS (long wParam, long lParam) {
548 if (!isInlineEnabled ()) return null;
549 long hwnd = parent.handle;
550 long hIMC = OS.ImmGetContext (hwnd);
551 if (hIMC != 0) {
552 if (OS.ImmGetOpenStatus (hIMC)) {
553 OS.ImmNotifyIME (hIMC, OS.NI_COMPOSITIONSTR, OS.CPS_COMPLETE, 0);
554 }
555 OS.ImmReleaseContext (hwnd, hIMC);
556 }
557 return null;
558 }
559
WM_LBUTTONDOWN(long wParam, long lParam)560 LRESULT WM_LBUTTONDOWN (long wParam, long lParam) {
561 if (!isInlineEnabled ()) return null;
562 long hwnd = parent.handle;
563 long hIMC = OS.ImmGetContext (hwnd);
564 if (hIMC != 0) {
565 if (OS.ImmGetOpenStatus (hIMC)) {
566 if (OS.ImmGetCompositionString (hIMC, OS.GCS_COMPSTR, (char [])null, 0) > 0) {
567 Event event = new Event ();
568 event.detail = SWT.COMPOSITION_OFFSET;
569 event.setLocationInPixels(OS.GET_X_LPARAM (lParam), OS.GET_Y_LPARAM (lParam));
570 sendEvent (SWT.ImeComposition, event);
571 int offset = event.index;
572 int length = text.length();
573 if (offset != -1 && startOffset != -1 && startOffset <= offset && offset < startOffset + length) {
574 long imeWnd = OS.ImmGetDefaultIMEWnd (hwnd);
575 offset = event.index + event.count - startOffset;
576 int trailing = event.count > 0 ? 1 : 2;
577 long param = OS.MAKEWPARAM (OS.MAKEWORD (OS.IMEMOUSE_LDOWN, trailing), offset);
578 OS.SendMessage (imeWnd, WM_MSIME_MOUSE, param, hIMC);
579 } else {
580 OS.ImmNotifyIME (hIMC, OS.NI_COMPOSITIONSTR, OS.CPS_COMPLETE, 0);
581 }
582 }
583 }
584 OS.ImmReleaseContext (hwnd, hIMC);
585 }
586 return null;
587 }
588
589 }
590