comparison dwt/widgets/IME.d @ 45:d8635bb48c7c

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