1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 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 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.cocoa.*;
20
21 /**
22 * Instances of this class represent input method editors.
23 * These are typically in-line pre-edit text areas that allow
24 * the user to compose characters from Far Eastern languages
25 * such as Japanese, Chinese or Korean.
26 *
27 * <dl>
28 * <dt><b>Styles:</b></dt>
29 * <dd>(none)</dd>
30 * <dt><b>Events:</b></dt>
31 * <dd>ImeComposition</dd>
32 * </dl>
33 * <p>
34 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
35 * </p>
36 *
37 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
38 *
39 * @since 3.4
40 * @noextend This class is not intended to be subclassed by clients.
41 */
42 public class IME extends Widget {
43 Canvas parent;
44 int caretOffset;
45 int startOffset;
46 int commitCount;
47 String text;
48 int [] ranges;
49 TextStyle [] styles;
50
51 static final int UNDERLINE_THICK = 1 << 16;
52
53 /**
54 * Prevents uninitialized instances from being created outside the package.
55 */
IME()56 IME () {
57 }
58
59 /**
60 * Constructs a new instance of this class given its parent
61 * and a style value describing its behavior and appearance.
62 * <p>
63 * The style value is either one of the style constants defined in
64 * class <code>SWT</code> which is applicable to instances of this
65 * class, or must be built by <em>bitwise OR</em>'ing together
66 * (that is, using the <code>int</code> "|" operator) two or more
67 * of those <code>SWT</code> style constants. The class description
68 * lists the style constants that are applicable to the class.
69 * Style bits are also inherited from superclasses.
70 * </p>
71 *
72 * @param parent a canvas control which will be the parent of the new instance (cannot be null)
73 * @param style the style of control to construct
74 *
75 * @exception IllegalArgumentException <ul>
76 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
77 * </ul>
78 * @exception SWTException <ul>
79 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
80 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
81 * </ul>
82 *
83 * @see Widget#checkSubclass
84 * @see Widget#getStyle
85 */
IME(Canvas parent, int style)86 public IME (Canvas parent, int style) {
87 super (parent, style);
88 this.parent = parent;
89 createWidget ();
90 }
91
92 @Override
attributedSubstringFromRange(long id, long sel, long rangePtr)93 long attributedSubstringFromRange (long id, long sel, long rangePtr) {
94 Event event = new Event ();
95 event.detail = SWT.COMPOSITION_SELECTION;
96 sendEvent (SWT.ImeComposition, event);
97 NSRange range = new NSRange ();
98 OS.memmove (range, rangePtr, NSRange.sizeof);
99 int start = (int)range.location;
100 int end = (int)(range.location + range.length);
101 if (event.start <= start && start <= event.end && event.start <= end && end <= event.end) {
102 NSString str = (NSString) new NSString().alloc();
103 str = str.initWithString(event.text.substring(start - event.start, end - event.start));
104 NSAttributedString attriStr = ((NSAttributedString)new NSAttributedString().alloc()).initWithString(str, null);
105 str.release();
106 attriStr.autorelease ();
107 return attriStr.id;
108 }
109 return 0;
110 }
111
112 @Override
characterIndexForPoint(long id, long sel, long point)113 long characterIndexForPoint (long id, long sel, long point) {
114 if (!isInlineEnabled ()) return OS.NSNotFound();
115 NSPoint pt = new NSPoint ();
116 OS.memmove (pt, point, NSPoint.sizeof);
117 NSView view = parent.view;
118 pt = view.window ().convertScreenToBase (pt);
119 pt = view.convertPoint_fromView_ (pt, null);
120 Event event = new Event ();
121 event.detail = SWT.COMPOSITION_OFFSET;
122 event.x = (int) pt.x;
123 event.y = (int) pt.y;
124 sendEvent (SWT.ImeComposition, event);
125 int offset = event.index + event.count;
126 return offset != -1 ? offset : OS.NSNotFound();
127 }
128
129 @Override
createWidget()130 void createWidget () {
131 text = "";
132 startOffset = -1;
133 if (parent.getIME () == null) {
134 parent.setIME (this);
135 }
136 }
137
138 @Override
firstRectForCharacterRange(long id, long sel, long range)139 NSRect firstRectForCharacterRange(long id, long sel, long range) {
140 NSRect rect = new NSRect ();
141 Caret caret = parent.caret;
142 if (caret != null) {
143 NSView view = parent.view;
144 NSPoint pt = new NSPoint ();
145 pt.x = caret.x;
146 pt.y = caret.y + caret.height;
147 pt = view.convertPoint_toView_ (pt, null);
148 pt = view.window ().convertBaseToScreen (pt);
149 rect.x = pt.x;
150 rect.y = pt.y;
151 rect.width = caret.width;
152 rect.height = caret.height;
153 }
154 return rect;
155 }
156
157 /**
158 * Returns the offset of the caret from the start of the document.
159 * -1 means that there is currently no active composition.
160 * The caret is within the current composition.
161 *
162 * @return the caret offset
163 *
164 * @exception SWTException <ul>
165 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
166 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
167 * </ul>
168 */
getCaretOffset()169 public int getCaretOffset () {
170 checkWidget ();
171 return startOffset + caretOffset;
172 }
173
174 /**
175 * Returns the commit count of the composition. This is the
176 * number of characters that have been composed. When the
177 * commit count is equal to the length of the composition
178 * text, then the in-line edit operation is complete.
179 *
180 * @return the commit count
181 *
182 * @exception SWTException <ul>
183 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
184 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
185 * </ul>
186 *
187 * @see IME#getText
188 */
getCommitCount()189 public int getCommitCount () {
190 checkWidget ();
191 return commitCount;
192 }
193
194 /**
195 * Returns the offset of the composition from the start of the document.
196 * This is the start offset of the composition within the document and
197 * in not changed by the input method editor itself during the in-line edit
198 * session.
199 *
200 * @return the offset of the composition
201 *
202 * @exception SWTException <ul>
203 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
204 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
205 * </ul>
206 */
getCompositionOffset()207 public int getCompositionOffset () {
208 checkWidget ();
209 return startOffset;
210 }
211
212 /**
213 * Returns the ranges for the style that should be applied during the
214 * in-line edit session.
215 * <p>
216 * The ranges array contains start and end pairs. Each pair refers to
217 * the corresponding style in the styles array. For example, the pair
218 * that starts at ranges[n] and ends at ranges[n+1] uses the style
219 * at styles[n/2] returned by <code>getStyles()</code>.
220 * </p>
221 * @return the ranges for the styles
222 *
223 * @exception SWTException <ul>
224 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
225 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
226 * </ul>
227 *
228 * @see IME#getStyles
229 */
getRanges()230 public int [] getRanges () {
231 checkWidget ();
232 if (ranges == null) return new int [0];
233 int [] result = new int [ranges.length];
234 for (int i = 0; i < result.length; i++) {
235 result [i] = ranges [i] + startOffset;
236 }
237 return result;
238 }
239
240 /**
241 * Returns the styles for the ranges.
242 * <p>
243 * The ranges array contains start and end pairs. Each pair refers to
244 * the corresponding style in the styles array. For example, the pair
245 * that starts at ranges[n] and ends at ranges[n+1] uses the style
246 * at styles[n/2].
247 * </p>
248 *
249 * @return the ranges for the styles
250 *
251 * @exception SWTException <ul>
252 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
253 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
254 * </ul>
255 *
256 * @see IME#getRanges
257 */
getStyles()258 public TextStyle [] getStyles () {
259 checkWidget ();
260 if (styles == null) return new TextStyle [0];
261 TextStyle [] result = new TextStyle [styles.length];
262 System.arraycopy (styles, 0, result, 0, styles.length);
263 return result;
264 }
265
getStyle(NSDictionary attribs)266 TextStyle getStyle (NSDictionary attribs) {
267 NSArray keys = attribs.allKeys ();
268 long count = keys.count ();
269 TextStyle style = new TextStyle ();
270 for (int j = 0; j < count; j++) {
271 NSString key = new NSString (keys.objectAtIndex (j));
272 if (key.isEqual (OS.NSBackgroundColorAttributeName)) {
273 NSColor color = new NSColor (attribs.objectForKey (key));
274 style.background = Color.cocoa_new (display, display.getNSColorRGB(color));
275 } else if (key.isEqual (OS.NSForegroundColorAttributeName)) {
276 NSColor color = new NSColor (attribs.objectForKey (key));
277 style.foreground = Color.cocoa_new (display, display.getNSColorRGB(color));
278 } else if (key.isEqual (OS.NSUnderlineColorAttributeName)) {
279 NSColor color = new NSColor (attribs.objectForKey (key));
280 style.underlineColor = Color.cocoa_new (display, display.getNSColorRGB(color));
281 } else if (key.isEqual (OS.NSUnderlineStyleAttributeName)) {
282 NSNumber value = new NSNumber (attribs.objectForKey (key));
283 switch (value.intValue ()) {
284 case OS.NSUnderlineStyleSingle: style.underlineStyle = SWT.UNDERLINE_SINGLE; break;
285 case OS.NSUnderlineStyleDouble: style.underlineStyle = SWT.UNDERLINE_DOUBLE; break;
286 case OS.NSUnderlineStyleThick: style.underlineStyle = UNDERLINE_THICK; break;
287 }
288 style.underline = value.intValue () != OS.NSUnderlineStyleNone;
289 } else if (key.isEqual (OS.NSStrikethroughColorAttributeName)) {
290 NSColor color = new NSColor (attribs.objectForKey (key));
291 style.strikeoutColor = Color.cocoa_new (display, display.getNSColorRGB(color));
292 } else if (key.isEqual (OS.NSStrikethroughStyleAttributeName)) {
293 NSNumber value = new NSNumber (attribs.objectForKey (key));
294 style.strikeout = value.intValue () != OS.NSUnderlineStyleNone;
295 } else if (key.isEqual (OS.NSFontAttributeName)) {
296 NSFont font = new NSFont (attribs.objectForKey (key));
297 font.retain();
298 style.font = Font.cocoa_new (display, font);
299 }
300 }
301 return style;
302 }
303
304 /**
305 * Returns the composition text.
306 * <p>
307 * The text for an IME is the characters in the widget that
308 * are in the current composition. When the commit count is
309 * equal to the length of the composition text, then the
310 * in-line edit operation is complete.
311 * </p>
312 *
313 * @return the widget text
314 *
315 * @exception SWTException <ul>
316 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
317 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
318 * </ul>
319 */
getText()320 public String getText () {
321 checkWidget ();
322 return text;
323 }
324
325 /**
326 * Returns <code>true</code> if the caret should be wide, and
327 * <code>false</code> otherwise. In some languages, for example
328 * Korean, the caret is typically widened to the width of the
329 * current character in the in-line edit session.
330 *
331 * @return the wide caret state
332 *
333 * @exception SWTException <ul>
334 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
335 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
336 * </ul>
337 */
getWideCaret()338 public boolean getWideCaret() {
339 return false;
340 }
341
342 @Override
hasMarkedText(long id, long sel)343 boolean hasMarkedText (long id, long sel) {
344 return text.length () != 0;
345 }
346
347 @Override
insertText(long id, long sel, long string)348 boolean insertText (long id, long sel, long string) {
349 if (startOffset == -1) return true;
350 NSString str = new NSString (string);
351 if (str.isKindOfClass (OS.class_NSAttributedString)) {
352 str = new NSAttributedString (string).string ();
353 }
354 int length = (int)str.length ();
355 int end = startOffset + text.length ();
356 resetStyles ();
357 caretOffset = commitCount = length;
358 Event event = new Event ();
359 event.detail = SWT.COMPOSITION_CHANGED;
360 event.start = startOffset;
361 event.end = end;
362 event.text = text = str.getString();
363 sendEvent (SWT.ImeComposition, event);
364 text = "";
365 caretOffset = commitCount = 0;
366 startOffset = -1;
367 return event.doit;
368 }
369
isInlineEnabled()370 boolean isInlineEnabled () {
371 return hooks (SWT.ImeComposition);
372 }
373
374 @Override
markedRange(long id, long sel)375 NSRange markedRange (long id, long sel) {
376 NSRange range = new NSRange ();
377 if (startOffset != -1) {
378 range.location = startOffset;
379 range.length = text.length ();
380 } else {
381 range.location = OS.NSNotFound();
382 }
383 return range;
384 }
385
resetStyles()386 void resetStyles () {
387 if (styles != null) {
388 for (int i = 0; i < styles.length; i++) {
389 TextStyle style = styles [i];
390 Font font = style.font;
391 if (font != null) font.handle.release ();
392 }
393 }
394 styles = null;
395 ranges = null;
396 }
397
398 @Override
releaseParent()399 void releaseParent () {
400 super.releaseParent ();
401 if (this == parent.getIME ()) parent.setIME (null);
402 }
403
404 @Override
releaseWidget()405 void releaseWidget () {
406 super.releaseWidget ();
407 parent = null;
408 text = null;
409 resetStyles ();
410 }
411
412 @Override
selectedRange(long id, long sel)413 NSRange selectedRange (long id, long sel) {
414 Event event = new Event ();
415 event.detail = SWT.COMPOSITION_SELECTION;
416 sendEvent (SWT.ImeComposition, event);
417 NSRange range = new NSRange ();
418 range.location = event.start;
419 range.length = event.text.length ();
420 return range;
421 }
422
423 /**
424 * Sets the offset of the composition from the start of the document.
425 * This is the start offset of the composition within the document and
426 * in not changed by the input method editor itself during the in-line edit
427 * session but may need to be changed by clients of the IME. For example,
428 * if during an in-line edit operation, a text editor inserts characters
429 * above the IME, then the IME must be informed that the composition
430 * offset has changed.
431 *
432 * @param offset the offset of the composition
433 *
434 * @exception SWTException <ul>
435 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
436 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
437 * </ul>
438 */
setCompositionOffset(int offset)439 public void setCompositionOffset (int offset) {
440 checkWidget ();
441 if (offset < 0) return;
442 if (startOffset != -1) {
443 startOffset = offset;
444 }
445 }
446
447 @Override
setMarkedText_selectedRange(long id, long sel, long string, long selRange)448 boolean setMarkedText_selectedRange (long id, long sel, long string, long selRange) {
449 if (!isInlineEnabled ()) return true;
450 resetStyles ();
451 caretOffset = commitCount = 0;
452 int end = startOffset + text.length ();
453 if (startOffset == -1) {
454 Event event = new Event ();
455 event.detail = SWT.COMPOSITION_SELECTION;
456 sendEvent (SWT.ImeComposition, event);
457 startOffset = event.start;
458 end = event.end;
459 }
460 NSString str = new NSString (string);
461 if (str.isKindOfClass (OS.class_NSAttributedString)) {
462 NSAttributedString attribStr = new NSAttributedString (string);
463 str = attribStr.string ();
464 int length = (int)str.length ();
465 styles = new TextStyle [length];
466 ranges = new int [length * 2];
467 NSRange rangeLimit = new NSRange (), effectiveRange = new NSRange ();
468 rangeLimit.length = length;
469 int rangeCount = 0;
470 long ptr = C.malloc (NSRange.sizeof);
471 for (int i = 0; i < length;) {
472 NSDictionary attribs = attribStr.attributesAtIndex(i, ptr, rangeLimit);
473 OS.memmove (effectiveRange, ptr, NSRange.sizeof);
474 i = (int)(effectiveRange.location + effectiveRange.length);
475 ranges [rangeCount * 2] = (int)effectiveRange.location;
476 ranges [rangeCount * 2 + 1] = (int)(effectiveRange.location + effectiveRange.length - 1);
477 styles [rangeCount++] = getStyle (attribs);
478 }
479 C.free (ptr);
480 if (rangeCount != styles.length) {
481 TextStyle [] newStyles = new TextStyle [rangeCount];
482 System.arraycopy (styles, 0, newStyles, 0, newStyles.length);
483 styles = newStyles;
484 int [] newRanges = new int [rangeCount * 2];
485 System.arraycopy (ranges, 0, newRanges, 0, newRanges.length);
486 ranges = newRanges;
487 }
488 }
489 int length = (int)str.length ();
490 if (ranges == null && length > 0) {
491 styles = new TextStyle []{getStyle (display.markedAttributes)};
492 ranges = new int[]{0, length - 1};
493 }
494 NSRange range = new NSRange ();
495 OS.memmove (range, selRange, NSRange.sizeof);
496 caretOffset = (int)range.location;
497 Event event = new Event ();
498 event.detail = SWT.COMPOSITION_CHANGED;
499 event.start = startOffset;
500 event.end = end;
501 event.text = text = str.getString();
502 sendEvent (SWT.ImeComposition, event);
503 if (isDisposed ()) return false;
504 if (text.length () == 0) {
505 Shell s = parent.getShell ();
506 s.keyInputHappened = true;
507 startOffset = -1;
508 resetStyles ();
509 }
510 return true;
511 }
512
513 @Override
validAttributesForMarkedText(long id, long sel)514 long validAttributesForMarkedText (long id, long sel) {
515 NSMutableArray attribs = NSMutableArray.arrayWithCapacity (6);
516 attribs.addObject (OS.NSForegroundColorAttributeName);
517 attribs.addObject (OS.NSBackgroundColorAttributeName);
518 attribs.addObject (OS.NSUnderlineStyleAttributeName);
519 attribs.addObject (OS.NSUnderlineColorAttributeName);
520 attribs.addObject (OS.NSStrikethroughStyleAttributeName);
521 attribs.addObject (OS.NSStrikethroughColorAttributeName);
522 return attribs.id;
523 }
524
525 }
526