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