1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
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.*;
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.
29 * <dt><b>Styles:</b></dt>
31 * <dt><b>Events:</b></dt>
32 * <dd>ImeComposition</dd>
35 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
38 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41 * @noextend This class is not intended to be subclassed by clients.
43 public class IME extends Widget {
52 static final int WM_MSIME_MOUSE = OS.RegisterWindowMessage (new TCHAR (0, "MSIMEMouseOperation", true)); //$NON-NLS-1$
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;
60 * Prevents uninitialized instances from being created outside the package.
66 * Constructs a new instance of this class given its parent
67 * and a style value describing its behavior and appearance.
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.
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
81 * @exception IllegalArgumentException <ul>
82 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
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>
89 * @see Widget#checkSubclass
90 * @see Widget#getStyle
92 public IME (Canvas parent, int style) {
93 super (parent, style);
98 void createWidget () {
99 text = ""; //$NON-NLS-1$
101 if (parent.getIME () == null) {
102 parent.setIME (this);
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.
111 * @return the caret offset
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>
118 public int getCaretOffset () {
120 return startOffset + caretOffset;
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.
129 * @return the commit count
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>
138 public int getCommitCount () {
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
149 * @return the offset of the composition
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>
156 public int getCompositionOffset () {
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;
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);
171 hr = COM.CoCreateInstance (pclsid, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_ITfDisplayAttributeProvider, ppv);
173 ITfDisplayAttributeProvider pProvider = new ITfDisplayAttributeProvider (ppv [0]);
174 hr = pProvider.EnumDisplayAttributeInfo (ppv);
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) {
189 pProvider.Release ();
192 pProfiles.Release ();
195 pda = new TF_DISPLAYATTRIBUTE ();
197 case OS.TF_ATTR_INPUT:
198 pda.lsStyle = OS.TF_LS_SQUIGGLE;
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;
211 * Returns the ranges for the style that should be applied during the
212 * in-line edit session.
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>.
219 * @return the ranges for the styles
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>
228 public int [] getRanges () {
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;
239 * Returns the styles for the ranges.
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
247 * @return the ranges for the styles
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>
256 public TextStyle [] getStyles () {
258 if (styles == null) return new TextStyle [0];
259 TextStyle [] result = new TextStyle [styles.length];
260 System.arraycopy (styles, 0, result, 0, styles.length);
265 * Returns the composition text.
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.
273 * @return the widget text
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>
280 public String getText () {
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.
291 * @return the wide caret state
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>
298 public boolean getWideCaret() {
300 long layout = OS.GetKeyboardLayout (0);
301 short langID = (short)OS.LOWORD (layout);
302 return OS.PRIMARYLANGID (langID) == OS.LANG_KOREAN;
305 boolean isInlineEnabled () {
306 return OS.IsDBLocale && hooks (SWT.ImeComposition);
310 void releaseParent () {
311 super.releaseParent ();
312 if (this == parent.getIME ()) parent.setIME (null);
316 void releaseWidget () {
317 super.releaseWidget ();
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.
333 * @param offset the offset of the composition
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>
340 public void setCompositionOffset (int offset) {
342 if (offset < 0) return;
343 if (startOffset != -1) {
344 startOffset = offset;
348 LRESULT WM_IME_COMPOSITION (long wParam, long lParam) {
349 if (!isInlineEnabled ()) return null;
352 caretOffset = commitCount = 0;
353 long hwnd = parent.handle;
354 long hIMC = OS.ImmGetContext (hwnd);
356 char [] buffer = null;
357 if ((lParam & OS.GCS_RESULTSTR) != 0) {
358 int length = OS.ImmGetCompositionString (hIMC, OS.GCS_RESULTSTR, (char [])null, 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;
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);
376 text = ""; //$NON-NLS-1$
380 Display display = this.display;
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 ();
389 parent.sendEvent (SWT.KeyDown, event);
393 if ((lParam & OS.GCS_COMPSTR) == 0) return LRESULT.ONE;
396 if ((lParam & OS.GCS_COMPSTR) != 0) {
397 int length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPSTR, (char [])null, 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);
404 int [] clauses = null;
405 if ((lParam & OS.GCS_COMPCLAUSE) != 0) {
406 length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPCLAUSE, (int [])null, 0);
408 clauses = new int [length / 4];
409 OS.ImmGetCompositionString (hIMC, OS.GCS_COMPCLAUSE, clauses, length);
412 if ((lParam & OS.GCS_COMPATTR) != 0 && clauses != null) {
413 length = OS.ImmGetCompositionString (hIMC, OS.GCS_COMPATTR, (byte [])null, 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]]);
432 switch (attr.crText.type) {
433 case OS.TF_CT_COLORREF:
434 style.foreground = Color.win32_new (display, attr.crText.cr);
436 case OS.TF_CT_SYSCOLOR:
437 int colorRef = OS.GetSysColor (attr.crText.cr);
438 style.foreground = Color.win32_new (display, colorRef);
441 switch (attr.crBk.type) {
442 case OS.TF_CT_COLORREF:
443 style.background = Color.win32_new (display, attr.crBk.cr);
445 case OS.TF_CT_SYSCOLOR:
446 int colorRef = OS.GetSysColor (attr.crBk.cr);
447 style.background = Color.win32_new (display, colorRef);
450 switch (attr.crLine.type) {
451 case OS.TF_CT_COLORREF:
452 style.underlineColor = Color.win32_new (display, attr.crLine.cr);
454 case OS.TF_CT_SYSCOLOR:
455 int colorRef = OS.GetSysColor (attr.crLine.cr);
456 style.underlineColor = Color.win32_new (display, colorRef);
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;
465 style.underlineStyle = UNDERLINE_IME_DASH;
468 style.underlineStyle = UNDERLINE_IME_DOT;
471 style.underlineStyle = attr.fBoldLine ? UNDERLINE_IME_THICK : SWT.UNDERLINE_SINGLE;
480 OS.ImmReleaseContext (hwnd, hIMC);
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;
490 Event event = new Event ();
491 event.detail = SWT.COMPOSITION_CHANGED;
492 event.start = startOffset;
494 event.text = text = buffer != null ? new String (buffer) : ""; //$NON-NLS-1$
495 sendEvent (SWT.ImeComposition, event);
496 if (text.length() == 0) {
505 LRESULT WM_IME_COMPOSITION_START (long wParam, long lParam) {
506 return isInlineEnabled () ? LRESULT.ONE : null;
509 LRESULT WM_IME_ENDCOMPOSITION (long wParam, long lParam) {
510 // Reset defaults. Otherwise the next composition overwrites the previous one.
513 return isInlineEnabled () ? LRESULT.ONE : null;
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) {
526 event.end = event.start + 1;
527 sendEvent (SWT.ImeComposition, event);
529 if (event.text != null && event.text.length() > 0) {
530 int length = event.text.length();
532 event.end = event.start + 1;
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);
539 sendEvent (SWT.ImeComposition, event);
547 LRESULT WM_KILLFOCUS (long wParam, long lParam) {
548 if (!isInlineEnabled ()) return null;
549 long hwnd = parent.handle;
550 long hIMC = OS.ImmGetContext (hwnd);
552 if (OS.ImmGetOpenStatus (hIMC)) {
553 OS.ImmNotifyIME (hIMC, OS.NI_COMPOSITIONSTR, OS.CPS_COMPLETE, 0);
555 OS.ImmReleaseContext (hwnd, hIMC);
560 LRESULT WM_LBUTTONDOWN (long wParam, long lParam) {
561 if (!isInlineEnabled ()) return null;
562 long hwnd = parent.handle;
563 long hIMC = OS.ImmGetContext (hwnd);
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);
580 OS.ImmNotifyIME (hIMC, OS.NI_COMPOSITIONSTR, OS.CPS_COMPLETE, 0);
584 OS.ImmReleaseContext (hwnd, hIMC);