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