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

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents e831403a80a9
children 681769fb5a7a
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 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 *
19 import dwt.events.SelectionListener; 19 import dwt.events.SelectionListener;
20 import dwt.events.VerifyListener; 20 import dwt.events.VerifyListener;
21 import dwt.graphics.Color; 21 import dwt.graphics.Color;
22 import dwt.graphics.Point; 22 import dwt.graphics.Point;
23 import dwt.graphics.Rectangle; 23 import dwt.graphics.Rectangle;
24 import dwt.internal.cocoa.NSAttributedString;
24 import dwt.internal.cocoa.NSCell; 25 import dwt.internal.cocoa.NSCell;
25 import dwt.internal.cocoa.NSColor; 26 import dwt.internal.cocoa.NSColor;
26 import dwt.internal.cocoa.NSControl; 27 import dwt.internal.cocoa.NSControl;
27 import dwt.internal.cocoa.NSEvent; 28 import dwt.internal.cocoa.NSEvent;
29 import dwt.internal.cocoa.NSFont;
28 import dwt.internal.cocoa.NSMutableString; 30 import dwt.internal.cocoa.NSMutableString;
31 import dwt.internal.cocoa.NSNotification;
29 import dwt.internal.cocoa.NSRange; 32 import dwt.internal.cocoa.NSRange;
30 import dwt.internal.cocoa.NSRect; 33 import dwt.internal.cocoa.NSRect;
31 import dwt.internal.cocoa.NSSearchField; 34 import dwt.internal.cocoa.NSScrollView;
32 import dwt.internal.cocoa.NSSecureTextField;
33 import dwt.internal.cocoa.NSSize; 35 import dwt.internal.cocoa.NSSize;
34 import dwt.internal.cocoa.NSString; 36 import dwt.internal.cocoa.NSString;
37 import dwt.internal.cocoa.NSText;
38 import dwt.internal.cocoa.NSTextContainer;
35 import dwt.internal.cocoa.NSTextField; 39 import dwt.internal.cocoa.NSTextField;
36 import dwt.internal.cocoa.NSTextFieldCell; 40 import dwt.internal.cocoa.NSTextFieldCell;
41 import dwt.internal.cocoa.NSTextStorage;
37 import dwt.internal.cocoa.NSTextView; 42 import dwt.internal.cocoa.NSTextView;
38 import dwt.internal.cocoa.OS; 43 import dwt.internal.cocoa.OS;
39 import dwt.internal.cocoa.SWTScrollView; 44 import dwt.internal.cocoa.SWTScrollView;
45 import dwt.internal.cocoa.SWTSearchField;
46 import dwt.internal.cocoa.SWTSecureTextField;
40 import dwt.internal.cocoa.SWTTextField; 47 import dwt.internal.cocoa.SWTTextField;
41 import dwt.internal.cocoa.SWTTextView; 48 import dwt.internal.cocoa.SWTTextView;
42 49
43 /** 50 /**
44 * Instances of this class are selectable user interface 51 * Instances of this class are selectable user interface
45 * objects that allow the user to enter and modify text. 52 * objects that allow the user to enter and modify text.
53 * Text controls can be either single or multi-line.
54 * When a text control is created with a border, the
55 * operating system includes a platform specific inset
56 * around the contents of the control. When created
57 * without a border, an effort is made to remove the
58 * inset such that the preferred size of the control
59 * is the same size as the contents.
46 * <p> 60 * <p>
47 * <dl> 61 * <dl>
48 * <dt><b>Styles:</b></dt> 62 * <dt><b>Styles:</b></dt>
49 * <dd>CANCEL, CENTER, LEFT, MULTI, PASSWORD, SEARCH, SINGLE, RIGHT, READ_ONLY, WRAP</dd> 63 * <dd>CANCEL, CENTER, LEFT, MULTI, PASSWORD, SEARCH, SINGLE, RIGHT, READ_ONLY, WRAP</dd>
50 * <dt><b>Events:</b></dt> 64 * <dt><b>Events:</b></dt>
54 * Note: Only one of the styles MULTI and SINGLE may be specified, 68 * Note: Only one of the styles MULTI and SINGLE may be specified,
55 * and only one of the styles LEFT, CENTER, and RIGHT may be specified. 69 * and only one of the styles LEFT, CENTER, and RIGHT may be specified.
56 * </p><p> 70 * </p><p>
57 * IMPORTANT: This class is <em>not</em> intended to be subclassed. 71 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
58 * </p> 72 * </p>
73 *
74 * @see <a href="http://www.eclipse.org/swt/snippets/#text">Text snippets</a>
75 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
76 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
59 */ 77 */
60 public class Text : Scrollable { 78 public class Text : Scrollable {
61 int textLimit = LIMIT, tabs = 8; 79 int textLimit = LIMIT, tabs = 8;
62 char echoCharacter; 80 char echoCharacter;
63 bool doubleClick; 81 bool doubleClick, receivingFocus;
64 String hiddenText, message; 82 String hiddenText, message;
83 NSRange selectionRange;
65 84
66 /** 85 /**
67 * The maximum number of characters that can be entered 86 * The maximum number of characters that can be entered
68 * into a text widget. 87 * into a text widget.
69 * <p> 88 * <p>
156 * 175 *
157 * @see ModifyListener 176 * @see ModifyListener
158 * @see #removeModifyListener 177 * @see #removeModifyListener
159 */ 178 */
160 public void addModifyListener (ModifyListener listener) { 179 public void addModifyListener (ModifyListener listener) {
161 checkWidget(); 180 checkWidget ();
162 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 181 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
163 TypedListener typedListener = new TypedListener (listener); 182 TypedListener typedListener = new TypedListener (listener);
164 addListener (DWT.Modify, typedListener); 183 addListener (DWT.Modify, typedListener);
165 } 184 }
166 185
189 * @see SelectionListener 208 * @see SelectionListener
190 * @see #removeSelectionListener 209 * @see #removeSelectionListener
191 * @see SelectionEvent 210 * @see SelectionEvent
192 */ 211 */
193 public void addSelectionListener(SelectionListener listener) { 212 public void addSelectionListener(SelectionListener listener) {
194 checkWidget(); 213 checkWidget ();
195 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 214 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
196 TypedListener typedListener = new TypedListener(listener); 215 TypedListener typedListener = new TypedListener (listener);
197 addListener(DWT.Selection,typedListener); 216 addListener (DWT.Selection,typedListener);
198 addListener(DWT.DefaultSelection,typedListener); 217 addListener (DWT.DefaultSelection,typedListener);
199 } 218 }
200 219
201 /** 220 /**
202 * Adds the listener to the collection of listeners who will 221 * Adds the listener to the collection of listeners who will
203 * be notified when the receiver's text is verified, by sending 222 * be notified when the receiver's text is verified, by sending
216 * 235 *
217 * @see VerifyListener 236 * @see VerifyListener
218 * @see #removeVerifyListener 237 * @see #removeVerifyListener
219 */ 238 */
220 public void addVerifyListener (VerifyListener listener) { 239 public void addVerifyListener (VerifyListener listener) {
221 checkWidget(); 240 checkWidget ();
222 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 241 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
223 TypedListener typedListener = new TypedListener (listener); 242 TypedListener typedListener = new TypedListener (listener);
224 addListener (DWT.Verify, typedListener); 243 addListener (DWT.Verify, typedListener);
225 } 244 }
226 245
240 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 259 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
241 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 260 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
242 * </ul> 261 * </ul>
243 */ 262 */
244 public void append (String string) { 263 public void append (String string) {
245 checkWidget(); 264 checkWidget ();
246 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 265 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
247 if (hooks (DWT.Verify) || filters (DWT.Verify)) { 266 if (hooks (DWT.Verify) || filters (DWT.Verify)) {
248 int charCount = getCharCount (); 267 int charCount = getCharCount ();
249 string = verifyText (string, charCount, charCount, null); 268 string = verifyText (string, charCount, charCount, null);
250 if (string is null) return; 269 if (string is null) return;
251 } 270 }
252 NSString str = NSString.stringWith(string); 271 NSString str = NSString.stringWith (string);
253 if ((style & DWT.SINGLE) !is 0) { 272 if ((style & DWT.SINGLE) !is 0) {
254 // new NSTextFieldCell((cast(NSTextField)view).cell()).title(). 273 setSelection (getCharCount ());
255 } else { 274 insertEditText (string);
256 NSTextView widget = cast(NSTextView)view; 275 } else {
257 NSMutableString mutableString = widget.textStorage().mutableString(); 276 NSTextView widget = cast(NSTextView) view;
258 mutableString.appendString(str); 277 NSMutableString mutableString = widget.textStorage ().mutableString ();
259 NSRange range = new NSRange(); 278 mutableString.appendString (str);
260 range.location = mutableString.length(); 279 NSRange range = new NSRange ();
261 widget.scrollRangeToVisible(range); 280 range.location = mutableString.length ();
281 widget.scrollRangeToVisible (range);
262 } 282 }
263 if (string.length () !is 0) sendEvent (DWT.Modify); 283 if (string.length () !is 0) sendEvent (DWT.Modify);
284 }
285
286 bool becomeFirstResponder (int /*long*/ id, int /*long*/ sel) {
287 receivingFocus = true;
288 bool result = super.becomeFirstResponder (id, sel);
289 receivingFocus = false;
290 return result;
264 } 291 }
265 292
266 static int checkStyle (int style) { 293 static int checkStyle (int style) {
267 if ((style & DWT.SEARCH) !is 0) { 294 if ((style & DWT.SEARCH) !is 0) {
268 style |= DWT.SINGLE | DWT.BORDER; 295 style |= DWT.SINGLE | DWT.BORDER;
290 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 317 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
291 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 318 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
292 * </ul> 319 * </ul>
293 */ 320 */
294 public void clearSelection () { 321 public void clearSelection () {
295 checkWidget(); 322 checkWidget ();
296 Point selection = getSelection (); 323 Point selection = getSelection ();
297 setSelection (selection.x); 324 setSelection (selection.x);
298 } 325 }
299 326
300 public Point computeSize (int wHint, int hHint, bool changed) { 327 public Point computeSize (int wHint, int hHint, bool changed) {
301 checkWidget(); 328 checkWidget ();
302 int width = 0, height = 0; 329 int width = 0, height = 0;
303 if ((style & DWT.SINGLE) !is 0) { 330 if ((style & DWT.SINGLE) !is 0) {
304 NSTextField widget = cast(NSTextField)view; 331 NSTextField widget = cast(NSTextField) view;
305 NSRect oldRect = widget.frame(); 332 NSRect rect = new NSRect ();
306 widget.sizeToFit(); 333 rect.width = rect.height = Float.MAX_VALUE;
307 NSRect newRect = widget.frame(); 334 NSSize size = widget.cell ().cellSizeForBounds (rect);
335 width = (int)Math.ceil (size.width);
336 height = cast(int)Math.ceil (size.height);
337 } else {
338 NSTextView widget = cast(NSTextView) view;
339 NSSize oldSize = null;
340 NSTextContainer textContainer = widget.textContainer ();
341 if ((style & DWT.WRAP) !is 0) {
342 widget.setHorizontallyResizable (true);
343 textContainer.setWidthTracksTextView (false);
344 oldSize = textContainer.containerSize ();
345 NSSize csize = new NSSize ();
346 csize.width = wHint !is DWT.DEFAULT ? wHint : Float.MAX_VALUE;
347 csize.height = hHint !is DWT.DEFAULT ? hHint : Float.MAX_VALUE;
348 textContainer.setContainerSize (csize);
349 }
350 NSRect oldRect = widget.frame ();
351 widget.sizeToFit ();
352 NSRect newRect = widget.frame ();
308 widget.setFrame (oldRect); 353 widget.setFrame (oldRect);
309 width = cast(int)newRect.width; 354 if ((style & DWT.WRAP) !is 0) {
310 height = cast(int)newRect.height; 355 widget.setHorizontallyResizable (false);
311 } else { 356 textContainer.setWidthTracksTextView (true);
312 NSTextView widget = cast(NSTextView)view; 357 textContainer.setContainerSize (oldSize);
313 NSRect oldRect = widget.frame(); 358 }
314 widget.sizeToFit(); 359 width = (int)(newRect.width + 1);
315 NSRect newRect = widget.frame(); 360 height = (int)(newRect.height + 1);
316 widget.setFrame (oldRect);
317 width = cast(int)newRect.width;
318 height = cast(int)newRect.height;
319 } 361 }
320 if (width <= 0) width = DEFAULT_WIDTH; 362 if (width <= 0) width = DEFAULT_WIDTH;
321 if (height <= 0) height = DEFAULT_HEIGHT; 363 if (height <= 0) height = DEFAULT_HEIGHT;
322 if (wHint !is DWT.DEFAULT) width = wHint; 364 if (wHint !is DWT.DEFAULT) width = wHint;
323 if (hHint !is DWT.DEFAULT) height = hHint; 365 if (hHint !is DWT.DEFAULT) height = hHint;
339 * </ul> 381 * </ul>
340 */ 382 */
341 public void copy () { 383 public void copy () {
342 checkWidget (); 384 checkWidget ();
343 if ((style & DWT.SINGLE) !is 0) { 385 if ((style & DWT.SINGLE) !is 0) {
344 386 Point selection = getSelection ();
345 } else { 387 if (selection.x is selection.y) return;
346 (cast(NSTextView)view).copy(); 388 copyToClipboard (getEditText (selection.x, selection.y - 1));
389 } else {
390 NSText text = cast(NSText) view;
391 if (text.selectedRange ().length is 0) return;
392 text.copy (null);
347 } 393 }
348 } 394 }
349 395
350 void createHandle () { 396 void createHandle () {
351 if ((style & DWT.SINGLE) !is 0) { 397 if ((style & DWT.SINGLE) !is 0) {
352 NSTextField widget; 398 NSTextField widget;
353 if ((style & DWT.PASSWORD) !is 0) { 399 if ((style & DWT.PASSWORD) !is 0) {
354 widget = cast(NSTextField)new NSSecureTextField().alloc(); 400 widget = cast(NSTextField) new SWTSecureTextField ().alloc ();
355 } else if ((style & DWT.SEARCH) !is 0) { 401 } else if ((style & DWT.SEARCH) !is 0) {
356 widget = cast(NSTextField)new NSSearchField().alloc(); 402 widget = cast(NSTextField) (new SWTSearchField ()).alloc ();
357 } else { 403 } else {
358 widget = cast(NSTextField)new SWTTextField().alloc(); 404 widget = cast(NSTextField) (new SWTTextField ()).alloc ();
359 } 405 }
360 widget.initWithFrame(new NSRect()); 406 widget.initWithFrame (new NSRect ());
361 widget.setSelectable(true); 407 widget.setSelectable (true);
362 widget.setEditable((style & DWT.READ_ONLY) is 0); 408 widget.setEditable((style & DWT.READ_ONLY) is 0);
363 if ((style & DWT.BORDER) is 0) widget.setBordered(false); 409 if ((style & DWT.BORDER) is 0) {
410 widget.setFocusRingType (OS.NSFocusRingTypeNone);
411 widget.setBordered (false);
412 }
364 int align = OS.NSLeftTextAlignment; 413 int align = OS.NSLeftTextAlignment;
365 if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment; 414 if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment;
366 if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment; 415 if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment;
367 widget.setAlignment(align); 416 widget.setAlignment (align);
368 // widget.setTarget(widget); 417 // widget.setTarget(widget);
369 // widget.setAction(OS.sel_sendSelection); 418 // widget.setAction(OS.sel_sendSelection);
370 widget.setTag(jniRef);
371 view = widget; 419 view = widget;
372 parent.contentView().addSubview_(widget); 420 } else {
373 } else { 421 NSScrollView scrollWidget = cast(NSScrollView) new SWTScrollView ().alloc ();
374 SWTScrollView scrollWidget = cast(SWTScrollView)new SWTScrollView().alloc(); 422 scrollWidget.initWithFrame (new NSRect ());
375 scrollWidget.initWithFrame(new NSRect()); 423 scrollWidget.setHasVerticalScroller ((style & DWT.VERTICAL) !is 0);
376 scrollWidget.setHasVerticalScroller((style & DWT.VERTICAL) !is 0); 424 scrollWidget.setHasHorizontalScroller ((style & DWT.HORIZONTAL) !is 0);
377 scrollWidget.setHasHorizontalScroller((style & DWT.HORIZONTAL) !is 0); 425 scrollWidget.setAutoresizesSubviews (true);
378 scrollWidget.setAutoresizesSubviews(true); 426 if ((style & DWT.BORDER) !is 0) scrollWidget.setBorderType (OS.NSBezelBorder);
379 scrollWidget.setTag(jniRef);
380 427
381 SWTTextView widget = cast(SWTTextView)new SWTTextView().alloc(); 428 NSTextView widget = cast(NSTextView) new SWTTextView ().alloc ();
382 widget.initWithFrame(new NSRect()); 429 widget.initWithFrame (new NSRect ());
383 widget.setEditable((style & DWT.READ_ONLY) is 0); 430 widget.setEditable ((style & DWT.READ_ONLY) is 0);
384 if ((style & DWT.BORDER) is 0) widget.setFocusRingType(OS.NSFocusRingTypeNone);
385 431
386 NSSize size = new NSSize(); 432 NSSize size = new NSSize ();
387 size.width = size.height = Float.MAX_VALUE; 433 size.width = size.height = Float.MAX_VALUE;
388 widget.setMaxSize(size); 434 widget.setMaxSize (size);
389 widget.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable); 435 widget.setAutoresizingMask (OS.NSViewWidthSizable | OS.NSViewHeightSizable);
390 436
391 if ((style & DWT.WRAP) is 0) { 437 if ((style & DWT.WRAP) is 0) {
392 widget.setHorizontallyResizable(true); 438 NSTextContainer textContainer = widget.textContainer ();
393 NSSize csize = new NSSize(); 439 widget.setHorizontallyResizable (true);
440 textContainer.setWidthTracksTextView (false);
441 NSSize csize = new NSSize ();
394 csize.width = csize.height = Float.MAX_VALUE; 442 csize.width = csize.height = Float.MAX_VALUE;
395 widget.textContainer().setWidthTracksTextView(false); 443 textContainer.setContainerSize (csize);
396 widget.textContainer().setContainerSize(csize);
397 } 444 }
398 445
399 int align = OS.NSLeftTextAlignment; 446 int align = OS.NSLeftTextAlignment;
400 if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment; 447 if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment;
401 if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment; 448 if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment;
402 widget.setAlignment(align); 449 widget.setAlignment (align);
403 450
404 // widget.setTarget(widget); 451 // widget.setTarget(widget);
405 // widget.setAction(OS.sel_sendSelection); 452 // widget.setAction(OS.sel_sendSelection);
406 widget.setTag(jniRef); 453 widget.setRichText (false);
407 widget.setRichText(false); 454 widget.setDelegate(widget);
408 455
409 view = widget; 456 view = widget;
410 scrollView = scrollWidget; 457 scrollView = scrollWidget;
411 scrollView.setDocumentView(widget);
412 parent.contentView().addSubview_(scrollView);
413 } 458 }
414 } 459 }
415 460
416 void createWidget () { 461 void createWidget () {
417 super.createWidget (); 462 super.createWidget ();
430 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 475 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
431 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 476 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
432 * </ul> 477 * </ul>
433 */ 478 */
434 public void cut () { 479 public void cut () {
435 checkWidget(); 480 checkWidget ();
436 if ((style & DWT.READ_ONLY) !is 0) return; 481 if ((style & DWT.READ_ONLY) !is 0) return;
437 if ((style & DWT.SINGLE) !is 0) { 482 bool cut = true;
438 483 char [] oldText = null;
439 } else { 484 Point oldSelection = getSelection ();
440 (cast(NSTextView)view).cut(null); 485 if (hooks (DWT.Verify) || filters (DWT.Verify)) {
441 } 486 if (oldSelection.x !is oldSelection.y) {
442 // bool cut = true; 487 oldText = getEditText (oldSelection.x, oldSelection.y - 1);
443 // char [] oldText = null; 488 String newText = verifyText ("", oldSelection.x, oldSelection.y, null);
444 // Point oldSelection = getSelection (); 489 if (newText is null) return;
445 // if (hooks (DWT.Verify) || filters (DWT.Verify)) { 490 if (newText.length () !is 0) {
446 // if (oldSelection.x !is oldSelection.y) { 491 copyToClipboard (oldText);
447 // oldText = getEditText (oldSelection.x, oldSelection.y - 1); 492 if ((style & DWT.SINGLE) !is 0) {
448 // String newText = verifyText ("", oldSelection.x, oldSelection.y, null); 493 insertEditText (newText);
449 // if (newText is null) return; 494 } else {
450 // if (newText.length () !is 0) { 495 NSTextView widget = (NSTextView) view;
451 // copyToClipboard (oldText); 496 widget.replaceCharactersInRange (widget.selectedRange (), NSString.stringWith (newText));
452 // if (txnObject is 0) { 497 }
453 // insertEditText (newText); 498 cut = false;
454 // } else { 499 }
455 // setTXNText (OS.kTXNUseCurrentSelection, OS.kTXNUseCurrentSelection, newText); 500 }
456 // OS.TXNShowSelection (txnObject, false); 501 }
457 // } 502 if (cut) {
458 // cut = false; 503 if ((style & DWT.SINGLE) !is 0) {
459 // } 504 if (oldText is null) oldText = getEditText (oldSelection.x, oldSelection.y - 1);
460 // } 505 copyToClipboard (oldText);
461 // } 506 insertEditText ("");
462 // if (cut) { 507 } else {
463 // if (txnObject is 0) { 508 (cast(NSTextView) view).cut (null);
464 // if (oldText is null) oldText = getEditText (oldSelection.x, oldSelection.y - 1); 509 }
465 // copyToClipboard (oldText); 510 }
466 // insertEditText (""); 511 Point newSelection = getSelection ();
467 // } else { 512 if (!cut || !oldSelection.equals (newSelection)) sendEvent (DWT.Modify);
468 // OS.TXNCut (txnObject);
469 //
470 // /*
471 // * Feature in the Macintosh. When an empty string is set in the TXNObject,
472 // * the font attributes are cleared. The fix is to reset them.
473 // */
474 // if (OS.TXNDataSize (txnObject) / 2 is 0) setFontStyle (font);
475 // }
476 // }
477 // Point newSelection = getSelection ();
478 // if (!cut || !oldSelection.equals (newSelection)) sendEvent (DWT.Modify);
479 } 513 }
480 514
481 Color defaultBackground () { 515 Color defaultBackground () {
482 return display.getSystemColor (DWT.COLOR_LIST_BACKGROUND); 516 return display.getSystemColor (DWT.COLOR_LIST_BACKGROUND);
483 } 517 }
484 518
485 Color defaultForeground () { 519 Color defaultForeground () {
486 return display.getSystemColor (DWT.COLOR_LIST_FOREGROUND); 520 return display.getSystemColor (DWT.COLOR_LIST_FOREGROUND);
521 }
522
523 void deregister() {
524 super.deregister();
525
526 if ((style & DWT.SINGLE) !is 0) {
527 display.removeWidget(((NSControl)view).cell());
528 }
487 } 529 }
488 530
489 bool dragDetect (int x, int y, bool filter, bool [] consume) { 531 bool dragDetect (int x, int y, bool filter, bool [] consume) {
490 if (filter) { 532 if (filter) {
491 Point selection = getSelection (); 533 Point selection = getSelection ();
515 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 557 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
516 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 558 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
517 * </ul> 559 * </ul>
518 */ 560 */
519 public int getCaretLineNumber () { 561 public int getCaretLineNumber () {
520 checkWidget(); 562 checkWidget ();
521 if ((style & DWT.SINGLE) !is 0) return 0; 563 if ((style & DWT.SINGLE) !is 0) return 0;
522 return (getTopPixel () + getCaretLocation ().y) / getLineHeight (); 564 return (getTopPixel () + getCaretLocation ().y) / getLineHeight ();
523 } 565 }
524 566
525 /** 567 /**
535 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 577 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
536 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 578 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
537 * </ul> 579 * </ul>
538 */ 580 */
539 public Point getCaretLocation () { 581 public Point getCaretLocation () {
540 checkWidget(); 582 checkWidget ();
541 if ((style & DWT.SINGLE) !is 0) { 583 if ((style & DWT.SINGLE) !is 0) {
542 //TODO - caret location for unicode text 584 //TODO - caret location for unicode text
543 return new Point (0, 0); 585 return new Point (0, 0);
544 } 586 }
545 // NSText 587 // NSText
546 NSRange range = (cast(NSTextView)view).selectedRange(); 588 // NSRange range = (cast(NSTextView)view).selectedRange();
547 System.out.println(range.location + " " + range.length);
548 return null; 589 return null;
549 } 590 }
550 591
551 /** 592 /**
552 * Returns the character position of the caret. 593 * Returns the character position of the caret.
560 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 601 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
561 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 602 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
562 * </ul> 603 * </ul>
563 */ 604 */
564 public int getCaretPosition () { 605 public int getCaretPosition () {
565 checkWidget(); 606 checkWidget ();
566 if ((style & DWT.SINGLE) !is 0) { 607 if ((style & DWT.SINGLE) !is 0) {
567 //TODO 608 //TODO
568 return 0; 609 return 0;
569 } else { 610 } else {
570 NSRange range = (cast(NSTextView)view).selectedRange(); 611 NSRange range = (cast(NSTextView)view).selectedRange();
571 return range.location; 612 return (int)/*64*/range.location;
572 } 613 }
573 } 614 }
574 615
575 /** 616 /**
576 * Returns the number of characters. 617 * Returns the number of characters.
583 * </ul> 624 * </ul>
584 */ 625 */
585 public int getCharCount () { 626 public int getCharCount () {
586 checkWidget (); 627 checkWidget ();
587 if ((style & DWT.SINGLE) !is 0) { 628 if ((style & DWT.SINGLE) !is 0) {
588 return new NSCell((cast(NSControl)view).cell()).title().length(); 629 return cast(int)/*64*/new NSCell ((cast(NSControl) view).cell ()).title ().length ();
589 } else { 630 } else {
590 //TODO 631 return (int)/*64*/((NSTextView) view).textStorage ().length ();
591 return 0;
592 } 632 }
593 } 633 }
594 634
595 /** 635 /**
596 * Returns the double click enabled flag. 636 * Returns the double click enabled flag.
606 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 646 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
607 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 647 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
608 * </ul> 648 * </ul>
609 */ 649 */
610 public bool getDoubleClickEnabled () { 650 public bool getDoubleClickEnabled () {
611 checkWidget(); 651 checkWidget ();
612 return doubleClick; 652 return doubleClick;
613 } 653 }
614 654
615 /** 655 /**
616 * Returns the echo character. 656 * Returns the echo character.
628 * </ul> 668 * </ul>
629 * 669 *
630 * @see #setEchoChar 670 * @see #setEchoChar
631 */ 671 */
632 public char getEchoChar () { 672 public char getEchoChar () {
633 checkWidget(); 673 checkWidget ();
634 return echoCharacter; 674 return echoCharacter;
635 } 675 }
636 676
637 /** 677 /**
638 * Returns the editable state. 678 * Returns the editable state.
643 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 683 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
644 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 684 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
645 * </ul> 685 * </ul>
646 */ 686 */
647 public bool getEditable () { 687 public bool getEditable () {
648 checkWidget(); 688 checkWidget ();
649 return (style & DWT.READ_ONLY) is 0; 689 return (style & DWT.READ_ONLY) is 0;
650 } 690 }
651 691
692 char [] getEditText () {
693 NSString str = null;
694 if ((style & DWT.SINGLE) !is 0) {
695 str = new NSTextFieldCell (((NSTextField) view).cell ()).title ();
696 } else {
697 str = ((NSTextView)view).textStorage().string();
698 }
699
700 int length = (int)/*64*/str.length ();
701 char [] buffer = new char [length];
702 if (hiddenText !is null) {
703 hiddenText.getChars (0, length, buffer, 0);
704 } else {
705 NSRange range = new NSRange ();
706 range.length = length;
707 str.getCharacters (buffer, range);
708 }
709 return buffer;
710 }
711
712 char [] getEditText (int start, int end) {
713 NSString str = null;
714 if ((style & DWT.SINGLE) !is 0) {
715 str = new NSTextFieldCell (((NSTextField) view).cell ()).title ();
716 } else {
717 str = ((NSTextView)view).textStorage().string();
718 }
719
720 int length = (int)/*64*/str.length ();
721 end = Math.min (end, length - 1);
722 if (start > end) return new char [0];
723 start = Math.max (0, start);
724 NSRange range = new NSRange ();
725 range.location = start;
726 range.length = Math.max (0, end - start + 1);
727 char [] buffer = new char [(int)/*64*/range.length];
728 if (hiddenText !is null) {
729 hiddenText.getChars ((int)/*64*/range.location, (int)/*64*/(range.location + range.length), buffer, 0);
730 } else {
731 str.getCharacters (buffer, range);
732 }
733 return buffer;
734 }
735
652 /** 736 /**
653 * Returns the number of lines. 737 * Returns the number of lines.
654 * 738 *
655 * @return the number of lines in the widget 739 * @return the number of lines in the widget
656 * 740 *
658 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 742 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
659 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 743 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
660 * </ul> 744 * </ul>
661 */ 745 */
662 public int getLineCount () { 746 public int getLineCount () {
663 checkWidget(); 747 checkWidget ();
664 if ((style & DWT.SINGLE) !is 0) return 1; 748 if ((style & DWT.SINGLE) !is 0) return 1;
665 return (cast(NSTextView)view).textStorage().paragraphs().count(); 749 return cast(int)/*64*/(cast(NSTextView) view).textStorage ().paragraphs ().count ();
666 } 750 }
667 751
668 /** 752 /**
669 * Returns the line delimiter. 753 * Returns the line delimiter.
670 * 754 *
676 * </ul> 760 * </ul>
677 * 761 *
678 * @see #DELIMITER 762 * @see #DELIMITER
679 */ 763 */
680 public String getLineDelimiter () { 764 public String getLineDelimiter () {
681 checkWidget(); 765 checkWidget ();
682 return DELIMITER; 766 return DELIMITER;
683 } 767 }
684 768
685 /** 769 /**
686 * Returns the height of a line. 770 * Returns the height of a line.
691 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 775 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
692 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 776 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
693 * </ul> 777 * </ul>
694 */ 778 */
695 public int getLineHeight () { 779 public int getLineHeight () {
696 checkWidget(); 780 checkWidget ();
697 //TODO 781 //TODO
698 return 16; 782 return 16;
699 } 783 }
700 784
701 /** 785 /**
710 * </ul> 794 * </ul>
711 * 795 *
712 * @since 2.1.2 796 * @since 2.1.2
713 */ 797 */
714 public int getOrientation () { 798 public int getOrientation () {
715 checkWidget(); 799 checkWidget ();
716 return style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT); 800 return style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT);
717 } 801 }
718 802
719 /** 803 /**
720 * Returns the widget message. When the widget is created 804 * Returns the widget message. When the widget is created
744 // checkWidget (); 828 // checkWidget ();
745 //TODO 829 //TODO
746 return 0; 830 return 0;
747 } 831 }
748 832
749 public int getPosition (Point point) { 833 /*public*/ int getPosition (Point point) {
750 checkWidget (); 834 checkWidget ();
751 if (point is null) error (DWT.ERROR_NULL_ARGUMENT); 835 if (point is null) error (DWT.ERROR_NULL_ARGUMENT);
752 return getPosition (point.x, point.y); 836 return getPosition (point.x, point.y);
753 } 837 }
754 838
769 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 853 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
770 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 854 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
771 * </ul> 855 * </ul>
772 */ 856 */
773 public Point getSelection () { 857 public Point getSelection () {
774 checkWidget(); 858 checkWidget ();
775 if ((style & DWT.SINGLE) !is 0) { 859 if ((style & DWT.SINGLE) !is 0) {
776 // new NSTextFieldCell((cast(NSTextField)view).cell()).title(). 860 if (selectionRange is null) {
777 return new Point(0, 0); 861 NSString str = new NSTextFieldCell (((NSTextField) view).cell ()).title ();
778 } else { 862 return new Point((int)/*64*/str.length (), (int)/*64*/str.length ());
779 NSTextView widget = cast(NSTextView)view; 863 }
780 NSRange range = widget.selectedRange(); 864 return new Point ((int)/*64*/selectionRange.location, (int)/*64*/(selectionRange.location + selectionRange.length));
781 return new Point(range.location, range.location + range.length); 865 } else {
866 NSTextView widget = cast(NSTextView) view;
867 NSRange range = widget.selectedRange ();
868 return new Point ((int)/*64*/range.location, (int)/*64*/(range.location + range.length));
782 } 869 }
783 } 870 }
784 871
785 /** 872 /**
786 * Returns the number of selected characters. 873 * Returns the number of selected characters.
791 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 878 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
792 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 879 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
793 * </ul> 880 * </ul>
794 */ 881 */
795 public int getSelectionCount () { 882 public int getSelectionCount () {
796 checkWidget(); 883 checkWidget ();
797 if ((style & DWT.SINGLE) !is 0) { 884 if ((style & DWT.SINGLE) !is 0) {
798 // new NSTextFieldCell((cast(NSTextField)view).cell()).title(). 885 return selectionRange !is null ? cast(int)/*64*/selectionRange.length : 0;
799 return -1; 886 } else {
800 } else { 887 NSTextView widget = cast(NSTextView) view;
801 NSTextView widget = cast(NSTextView)view; 888 NSRange range = widget.selectedRange ();
802 NSRange range = widget.selectedRange(); 889 return (int)/*64*/range.length;
803 return range.length;
804 } 890 }
805 } 891 }
806 892
807 /** 893 /**
808 * Gets the selected text, or an empty string if there is no current selection. 894 * Gets the selected text, or an empty string if there is no current selection.
813 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 899 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
814 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 900 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
815 * </ul> 901 * </ul>
816 */ 902 */
817 public String getSelectionText () { 903 public String getSelectionText () {
818 checkWidget(); 904 checkWidget ();
819 if ((style & DWT.SINGLE) !is 0) { 905 if ((style & DWT.SINGLE) !is 0) {
820 //TODO 906 Point selection = getSelection ();
821 return ""; 907 if (selection.x is selection.y) return "";
822 } else { 908 return new String (getEditText (selection.x, selection.y - 1));
823 NSTextView widget = cast(NSTextView)view; 909 } else {
824 NSRange range = widget.selectedRange(); 910 NSTextView widget = cast(NSTextView) view;
825 NSString str = widget.textStorage().string(); 911 NSRange range = widget.selectedRange ();
826 char[] buffer = new char[range.length]; 912 NSString str = widget.textStorage ().string ();
827 str.getCharacters_range_(buffer, range); 913 char[] buffer = new char [(int)/*64*/range.length];
828 return new String(buffer); 914 str.getCharacters (buffer, range);
915 return new String (buffer);
829 } 916 }
830 } 917 }
831 918
832 /** 919 /**
833 * Returns the number of tabs. 920 * Returns the number of tabs.
843 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 930 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
844 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 931 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
845 * </ul> 932 * </ul>
846 */ 933 */
847 public int getTabs () { 934 public int getTabs () {
848 checkWidget(); 935 checkWidget ();
849 return tabs; 936 return tabs;
850 } 937 }
851 938
852 /** 939 /**
853 * Returns the widget text. 940 * Returns the widget text.
862 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 949 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
863 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 950 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
864 * </ul> 951 * </ul>
865 */ 952 */
866 public String getText () { 953 public String getText () {
867 checkWidget(); 954 checkWidget ();
868 NSString str; 955 NSString str;
869 if ((style & DWT.SINGLE) !is 0) { 956 if ((style & DWT.SINGLE) !is 0) {
870 str = new NSTextFieldCell((cast(NSTextField)view).cell()).title(); 957 return new_String (getEditText ());
871 } else { 958 } else {
872 str = (cast(NSTextView)view).textStorage().string(); 959 str = (cast(NSTextView)view).textStorage ().string ();
873 } 960 }
874 if (str is null) return ""; 961 return str.getString();
875 char[] buffer = new char[str.length()];
876 str.getCharacters_(buffer);
877 return new String(buffer);
878 } 962 }
879 963
880 /** 964 /**
881 * Returns a range of text. Returns an empty string if the 965 * Returns a range of text. Returns an empty string if the
882 * start of the range is greater than the end. 966 * start of the range is greater than the end.
895 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 979 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
896 * </ul> 980 * </ul>
897 */ 981 */
898 public String getText (int start, int end) { 982 public String getText (int start, int end) {
899 checkWidget (); 983 checkWidget ();
900 NSString str; 984 if (!(start <= end && 0 <= end)) return ""; //$NON-NLS-1$
901 if ((style & DWT.SINGLE) !is 0) { 985 if ((style & DWT.SINGLE) !is 0) {
902 str = new NSTextFieldCell((cast(NSTextField)view).cell()).title(); 986 return new_String (getEditText (start, end));
903 987 }
904 } else { 988 NSTextStorage storage = ((NSTextView) view).textStorage ();
905 str = null; 989 end = Math.min (end, (int)/*64*/storage.length () - 1);
906 // return getTXNText (OS.kTXNStartOffset, OS.kTXNEndOffset); 990 if (start > end) return ""; //$NON-NLS-1$
907 } 991 start = Math.max (0, start);
908 if (str is null) return ""; 992 NSRange range = new NSRange ();
909 char[] buffer = new char[str.length()]; 993 range.location = start;
910 str.getCharacters_(buffer); 994 range.length = end - start + 1;
911 return new String(buffer, start, end - start); 995 NSAttributedString substring = storage.attributedSubstringFromRange (range);
996 NSString string = substring.string ();
997 return string.getString();
912 } 998 }
913 999
914 /** 1000 /**
915 * Returns the maximum number of characters that the receiver is capable of holding. 1001 * Returns the maximum number of characters that the receiver is capable of holding.
916 * <p> 1002 * <p>
926 * </ul> 1012 * </ul>
927 * 1013 *
928 * @see #LIMIT 1014 * @see #LIMIT
929 */ 1015 */
930 public int getTextLimit () { 1016 public int getTextLimit () {
931 checkWidget(); 1017 checkWidget ();
932 return textLimit; 1018 return textLimit;
933 } 1019 }
934 1020
935 /** 1021 /**
936 * Returns the zero-relative index of the line which is currently 1022 * Returns the zero-relative index of the line which is currently
945 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1031 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
946 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1032 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
947 * </ul> 1033 * </ul>
948 */ 1034 */
949 public int getTopIndex () { 1035 public int getTopIndex () {
950 checkWidget(); 1036 checkWidget ();
951 if ((style & DWT.SINGLE) !is 0) return 0; 1037 if ((style & DWT.SINGLE) !is 0) return 0;
952 return getTopPixel () / getLineHeight (); 1038 return getTopPixel () / getLineHeight ();
953 } 1039 }
954 1040
955 /** 1041 /**
971 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1057 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
972 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1058 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
973 * </ul> 1059 * </ul>
974 */ 1060 */
975 public int getTopPixel () { 1061 public int getTopPixel () {
976 checkWidget(); 1062 checkWidget ();
977 if ((style & DWT.SINGLE) !is 0) return 0; 1063 if ((style & DWT.SINGLE) !is 0) return 0;
978 //TODO 1064 //TODO
979 return 0; 1065 return 0;
980 } 1066 }
981 1067
994 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1080 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
995 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1081 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
996 * </ul> 1082 * </ul>
997 */ 1083 */
998 public void insert (String string) { 1084 public void insert (String string) {
999 checkWidget(); 1085 checkWidget ();
1000 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 1086 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
1001 if (hooks (DWT.Verify) || filters (DWT.Verify)) { 1087 if (hooks (DWT.Verify) || filters (DWT.Verify)) {
1002 Point selection = getSelection (); 1088 Point selection = getSelection ();
1003 string = verifyText (string, selection.x, selection.y, null); 1089 string = verifyText (string, selection.x, selection.y, null);
1004 if (string is null) return; 1090 if (string is null) return;
1005 } 1091 }
1006 if ((style & DWT.SINGLE) !is 0) { 1092 if ((style & DWT.SINGLE) !is 0) {
1007 // new NSTextFieldCell((cast(NSTextField)view).cell()).title(). 1093 insertEditText (string);
1008 } else { 1094 } else {
1009 // 1095 NSString str = NSString.stringWith (string);
1010 NSString str = NSString.stringWith(string); 1096 NSTextView widget = cast(NSTextView) view;
1011 NSTextView widget = cast(NSTextView)view; 1097 NSRange range = widget.selectedRange ();
1012 NSRange range = widget.selectedRange(); 1098 widget.textStorage ().replaceCharactersInRange (range, str);
1013 widget.textStorage().replaceCharactersInRange_withString_(range, str);
1014 } 1099 }
1015 if (string.length () !is 0) sendEvent (DWT.Modify); 1100 if (string.length () !is 0) sendEvent (DWT.Modify);
1101 }
1102
1103 void insertEditText (String string) {
1104 int length = string.length ();
1105 Point selection = getSelection ();
1106 if (hasFocus () && hiddenText is null) {
1107 if (textLimit !is LIMIT) {
1108 int charCount = getCharCount();
1109 if (charCount - (selection.y - selection.x) + length > textLimit) {
1110 length = textLimit - charCount + (selection.y - selection.x);
1111 }
1112 }
1113 char [] buffer = new char [length];
1114 string.getChars (0, buffer.length, buffer, 0);
1115 NSString nsstring = NSString.stringWithCharacters (buffer, buffer.length);
1116 NSText editor = ((NSTextField) view).currentEditor ();
1117 editor.replaceCharactersInRange (editor.selectedRange (), nsstring);
1118 selectionRange = null;
1119 } else {
1120 String oldText = getText ();
1121 if (textLimit !is LIMIT) {
1122 int charCount = oldText.length ();
1123 if (charCount - (selection.y - selection.x) + length > textLimit) {
1124 string = string.substring(0, textLimit - charCount + (selection.y - selection.x));
1125 }
1126 }
1127 String newText = oldText.substring (0, selection.x) + string + oldText.substring (selection.y);
1128 setEditText (newText);
1129 setSelection (selection.x + string.length ());
1130 }
1016 } 1131 }
1017 1132
1018 /** 1133 /**
1019 * Pastes text from clipboard. 1134 * Pastes text from clipboard.
1020 * <p> 1135 * <p>
1026 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1141 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1027 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1142 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1028 * </ul> 1143 * </ul>
1029 */ 1144 */
1030 public void paste () { 1145 public void paste () {
1031 checkWidget(); 1146 checkWidget ();
1032 if ((style & DWT.READ_ONLY) !is 0) return; 1147 if ((style & DWT.READ_ONLY) !is 0) return;
1033 // bool paste = true; 1148 bool paste = true;
1034 // String oldText = null; 1149 String oldText = null;
1035 // if (hooks (DWT.Verify) || filters (DWT.Verify)) { 1150 if (hooks (DWT.Verify) || filters (DWT.Verify)) {
1036 // oldText = getClipboardText (); 1151 oldText = getClipboardText ();
1037 // if (oldText !is null) { 1152 if (oldText !is null) {
1038 // Point selection = getSelection (); 1153 Point selection = getSelection ();
1039 // String newText = verifyText (oldText, selection.x, selection.y, null); 1154 String newText = verifyText (oldText, selection.x, selection.y, null);
1040 // if (newText is null) return; 1155 if (newText is null) return;
1041 // if (!newText.equals (oldText)) { 1156 if (!newText.equals (oldText)) {
1042 // if (txnObject is 0) { 1157 if ((style & DWT.SINGLE) !is 0) {
1043 // insertEditText (newText); 1158 insertEditText (newText);
1044 // } else { 1159 } else {
1045 // setTXNText (OS.kTXNUseCurrentSelection, OS.kTXNUseCurrentSelection, newText); 1160 NSTextView textView = (NSTextView) view;
1046 // OS.TXNShowSelection (txnObject, false); 1161 textView.replaceCharactersInRange (textView.selectedRange (), NSString.stringWith (newText));
1047 // } 1162 }
1048 // paste = false; 1163 paste = false;
1049 // } 1164 }
1050 // } 1165 }
1051 // } 1166 }
1052 // if (paste) { 1167 if (paste) {
1053 // if (txnObject is 0) { 1168 if ((style & DWT.SINGLE) !is 0) {
1054 // if (oldText is null) oldText = getClipboardText (); 1169 if (oldText is null) oldText = getClipboardText ();
1055 // insertEditText (oldText); 1170 insertEditText (oldText);
1056 // } else { 1171 } else {
1057 // if (textLimit !is LIMIT) { 1172 //TODO check text limit
1058 // if (oldText is null) oldText = getClipboardText (); 1173 ((NSTextView) view).paste (null);
1059 // setTXNText (OS.kTXNUseCurrentSelection, OS.kTXNUseCurrentSelection, oldText); 1174 }
1060 // OS.TXNShowSelection (txnObject, false); 1175 }
1061 // } else { 1176 sendEvent (DWT.Modify);
1062 // OS.TXNPaste (txnObject); 1177 }
1063 // } 1178
1064 // } 1179 void register() {
1065 // } 1180 super.register();
1066 // sendEvent (DWT.Modify); 1181
1182 if ((style & DWT.SINGLE) !is 0) {
1183 display.addWidget(((NSControl)view).cell(), this);
1184 }
1067 } 1185 }
1068 1186
1069 void releaseWidget () { 1187 void releaseWidget () {
1070 super.releaseWidget (); 1188 super.releaseWidget ();
1071 hiddenText = message = null; 1189 hiddenText = message = null;
1190 selectionRange = null;
1072 } 1191 }
1073 1192
1074 /** 1193 /**
1075 * Removes the listener from the collection of listeners who will 1194 * Removes the listener from the collection of listeners who will
1076 * be notified when the receiver's text is modified. 1195 * be notified when the receiver's text is modified.
1087 * 1206 *
1088 * @see ModifyListener 1207 * @see ModifyListener
1089 * @see #addModifyListener 1208 * @see #addModifyListener
1090 */ 1209 */
1091 public void removeModifyListener (ModifyListener listener) { 1210 public void removeModifyListener (ModifyListener listener) {
1092 checkWidget(); 1211 checkWidget ();
1093 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 1212 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
1094 if (eventTable is null) return; 1213 if (eventTable is null) return;
1095 eventTable.unhook (DWT.Modify, listener); 1214 eventTable.unhook (DWT.Modify, listener);
1096 } 1215 }
1097 1216
1111 * 1230 *
1112 * @see SelectionListener 1231 * @see SelectionListener
1113 * @see #addSelectionListener 1232 * @see #addSelectionListener
1114 */ 1233 */
1115 public void removeSelectionListener(SelectionListener listener) { 1234 public void removeSelectionListener(SelectionListener listener) {
1116 checkWidget(); 1235 checkWidget ();
1117 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 1236 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
1118 if (eventTable is null) return; 1237 if (eventTable is null) return;
1119 eventTable.unhook(DWT.Selection, listener); 1238 eventTable.unhook (DWT.Selection, listener);
1120 eventTable.unhook(DWT.DefaultSelection,listener); 1239 eventTable.unhook (DWT.DefaultSelection,listener);
1121 } 1240 }
1122 1241
1123 /** 1242 /**
1124 * Removes the listener from the collection of listeners who will 1243 * Removes the listener from the collection of listeners who will
1125 * be notified when the control is verified. 1244 * be notified when the control is verified.
1136 * 1255 *
1137 * @see VerifyListener 1256 * @see VerifyListener
1138 * @see #addVerifyListener 1257 * @see #addVerifyListener
1139 */ 1258 */
1140 public void removeVerifyListener (VerifyListener listener) { 1259 public void removeVerifyListener (VerifyListener listener) {
1141 checkWidget(); 1260 checkWidget ();
1142 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); 1261 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
1143 if (eventTable is null) return; 1262 if (eventTable is null) return;
1144 eventTable.unhook (DWT.Verify, listener); 1263 eventTable.unhook (DWT.Verify, listener);
1145 } 1264 }
1146 1265
1151 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1270 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1152 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1271 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1153 * </ul> 1272 * </ul>
1154 */ 1273 */
1155 public void selectAll () { 1274 public void selectAll () {
1156 checkWidget(); 1275 checkWidget ();
1157 if ((style & DWT.SINGLE) !is 0) { 1276 if ((style & DWT.SINGLE) !is 0) {
1158 setSelection (0, getCharCount ()); 1277 setSelection (0, getCharCount ());
1159 } else { 1278 } else {
1160 (cast(NSTextView)view).selectAll(null); 1279 (cast(NSTextView) view).selectAll (null);
1161 } 1280 }
1162 } 1281 }
1163 1282
1164 bool sendKeyEvent (int type, Event event) { 1283 bool sendKeyEvent (NSEvent nsEvent, int type) {
1165 if (!super.sendKeyEvent (type, event)) { 1284 bool result = super.sendKeyEvent (nsEvent, type);
1166 return false; 1285 if (!result) return result;
1167 } 1286 if (type !is DWT.KeyDown) return result;
1168 if (type !is DWT.KeyDown) return true; 1287 int stateMask = 0;
1169 if ((style & DWT.READ_ONLY) !is 0) return true; 1288 int /*long*/ modifierFlags = nsEvent.modifierFlags();
1170 if (event.character is 0) return true; 1289 if ((modifierFlags & OS.NSAlternateKeyMask) !is 0) stateMask |= DWT.ALT;
1171 if ((event.stateMask & DWT.COMMAND) !is 0) return true; 1290 if ((modifierFlags & OS.NSShiftKeyMask) !is 0) stateMask |= DWT.SHIFT;
1291 if ((modifierFlags & OS.NSControlKeyMask) !is 0) stateMask |= DWT.CONTROL;
1292 if ((modifierFlags & OS.NSCommandKeyMask) !is 0) stateMask |= DWT.COMMAND;
1293 if (stateMask is DWT.COMMAND) {
1294 short keyCode = nsEvent.keyCode ();
1295 switch (keyCode) {
1296 case 7: /* X */
1297 cut ();
1298 return false;
1299 case 8: /* C */
1300 copy ();
1301 return false;
1302 case 9: /* V */
1303 paste ();
1304 return false;
1305 }
1306 }
1307 if ((style & DWT.SINGLE) !is 0) {
1308 short keyCode = nsEvent.keyCode ();
1309 switch (keyCode) {
1310 case 76: /* KP Enter */
1311 case 36: /* Return */
1312 postEvent (DWT.DefaultSelection);
1313 }
1314 }
1315 if ((stateMask & DWT.COMMAND) !is 0) return result;
1172 String oldText = ""; 1316 String oldText = "";
1173 int charCount = getCharCount (); 1317 int charCount = getCharCount ();
1174 Point selection = getSelection (); 1318 Point selection = getSelection ();
1175 int start = selection.x, end = selection.y; 1319 int start = selection.x, end = selection.y;
1176 switch (event.character) { 1320 short keyCode = nsEvent.keyCode ();
1177 case DWT.BS: 1321 NSString characters = nsEvent.charactersIgnoringModifiers();
1322 char character = (char) characters.characterAtIndex(0);
1323 switch (keyCode) {
1324 case 51: /* Backspace */
1178 if (start is end) { 1325 if (start is end) {
1179 if (start is 0) return true; 1326 if (start is 0) return true;
1180 start = Math.max (0, start - 1); 1327 start = Math.max (0, start - 1);
1181 } 1328 }
1182 break; 1329 break;
1183 case DWT.DEL: 1330 case 117: /* Delete */
1184 if (start is end) { 1331 if (start is end) {
1185 if (start is charCount) return true; 1332 if (start is charCount) return true;
1186 end = Math.min (end + 1, charCount); 1333 end = Math.min (end + 1, charCount);
1187 } 1334 }
1188 break; 1335 break;
1189 case DWT.CR: 1336 case 36: /* Return */
1190 if ((style & DWT.SINGLE) !is 0) return true; 1337 if ((style & DWT.SINGLE) !is 0) return true;
1191 oldText = DELIMITER; 1338 oldText = DELIMITER;
1192 break; 1339 break;
1193 default: 1340 default:
1194 if (event.character !is '\t' && event.character < 0x20) return true; 1341 if (character !is '\t' && character < 0x20) return true;
1195 oldText = new String (new char [] {event.character}); 1342 oldText = new String (new char [] {character});
1196 } 1343 }
1197 String newText = verifyText (oldText, start, end, event); 1344 String newText = verifyText (oldText, start, end, nsEvent);
1198 if (newText is null) return false; 1345 if (newText is null) return false;
1199 if (charCount - (end - start) + newText.length () > textLimit) { 1346 if (charCount - (end - start) + newText.length () > textLimit) {
1200 return false; 1347 return false;
1201 } 1348 }
1202 bool result = newText is oldText; 1349 result = newText is oldText;
1203 if (newText !is oldText || hiddenText !is null) { 1350 if (newText !is oldText || hiddenText !is null) {
1204 // if (txnObject is 0) { 1351 if ((style & DWT.SINGLE) !is 0) {
1205 // String text = new String (getEditText (0, -1)); 1352 insertEditText (newText);
1206 // String leftText = text.substring (0, start); 1353 } else {
1207 // String rightText = text.substring (end, text.length ()); 1354 NSString str = NSString.stringWith (newText);
1208 // setEditText (leftText + newText + rightText); 1355 NSTextView widget = (NSTextView) view;
1209 // start += newText.length (); 1356 NSRange range = widget.selectedRange ();
1210 // setSelection (new Point (start, start)); 1357 widget.textStorage ().replaceCharactersInRange (range, str);
1211 // result = false; 1358 }
1212 // } else { 1359 if (newText.length () !is 0) sendEvent (DWT.Modify);
1213 // setTXNText (start, end, newText); 1360 }
1214 // }
1215 }
1216 /*
1217 * Post the modify event so that the character will be inserted
1218 * into the widget when the modify event is delivered. Normally,
1219 * modify events are sent but it is safe to post the event here
1220 * because this method is called from the event loop.
1221 */
1222 postEvent (DWT.Modify);
1223 return result; 1361 return result;
1224 } 1362 }
1225 1363
1226 void setBackground (float [] color) { 1364 void setBackground (float [] color) {
1227 NSColor nsColor; 1365 NSColor nsColor;
1228 if (color is null) { 1366 if (color is null) {
1229 return; // TODO reset to OS default 1367 return; // TODO reset to OS default
1230 } else { 1368 } else {
1231 nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1); 1369 nsColor = NSColor.colorWithDeviceRed (color [0], color [1], color [2], 1);
1232 } 1370 }
1233 if ((style & DWT.SINGLE) !is 0) { 1371 if ((style & DWT.SINGLE) !is 0) {
1234 (cast(NSTextField)view).setBackgroundColor(nsColor); 1372 (cast(NSTextField) view).setBackgroundColor (nsColor);
1235 } else { 1373 } else {
1236 (cast(NSTextView)view).setBackgroundColor(nsColor); 1374 (cast(NSTextView) view).setBackgroundColor (nsColor);
1237 } 1375 }
1238 } 1376 }
1239 1377
1240 /** 1378 /**
1241 * Sets the double click enabled flag. 1379 * Sets the double click enabled flag.
1254 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1392 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1255 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1393 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1256 * </ul> 1394 * </ul>
1257 */ 1395 */
1258 public void setDoubleClickEnabled (bool doubleClick) { 1396 public void setDoubleClickEnabled (bool doubleClick) {
1259 checkWidget(); 1397 checkWidget ();
1260 this.doubleClick = doubleClick; 1398 this.doubleClick = doubleClick;
1261 } 1399 }
1262 1400
1263 /** 1401 /**
1264 * Sets the echo character. 1402 * Sets the echo character.
1280 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1418 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1281 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1419 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1282 * </ul> 1420 * </ul>
1283 */ 1421 */
1284 public void setEchoChar (char echo) { 1422 public void setEchoChar (char echo) {
1285 checkWidget(); 1423 checkWidget ();
1286 if ((style & DWT.MULTI) !is 0) return; 1424 if ((style & DWT.MULTI) !is 0) return;
1287 // if (txnObject is 0) { 1425 // if (txnObject is 0) {
1288 // if ((style & DWT.PASSWORD) is 0) { 1426 // if ((style & DWT.PASSWORD) is 0) {
1289 // Point selection = getSelection (); 1427 // Point selection = getSelection ();
1290 // String text = getText (); 1428 // String text = getText ();
1307 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1445 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1308 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1446 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1309 * </ul> 1447 * </ul>
1310 */ 1448 */
1311 public void setEditable (bool editable) { 1449 public void setEditable (bool editable) {
1312 checkWidget(); 1450 checkWidget ();
1313 if (editable) { 1451 if (editable) {
1314 style &= ~DWT.READ_ONLY; 1452 style &= ~DWT.READ_ONLY;
1315 } else { 1453 } else {
1316 style |= DWT.READ_ONLY; 1454 style |= DWT.READ_ONLY;
1317 } 1455 }
1318 if ((style & DWT.SINGLE) !is 0) { 1456 if ((style & DWT.SINGLE) !is 0) {
1319 (cast(NSTextField)view).setEditable(editable); 1457 (cast(NSTextField) view).setEditable (editable);
1320 } else { 1458 } else {
1321 (cast(NSTextView)view).setEditable(editable); 1459 (cast(NSTextView) view).setEditable (editable);
1322 } 1460 }
1461 }
1462
1463 void setEditText (String string) {
1464 char [] buffer;
1465 if ((style & DWT.PASSWORD) is 0 && echoCharacter !is '\0') {
1466 hiddenText = string;
1467 buffer = new char [Math.min(hiddenText.length (), textLimit)];
1468 for (int i = 0; i < buffer.length; i++) buffer [i] = echoCharacter;
1469 } else {
1470 hiddenText = null;
1471 buffer = new char [Math.min(string.length (), textLimit)];
1472 string.getChars (0, buffer.length, buffer, 0);
1473 }
1474 NSString nsstring = NSString.stringWithCharacters (buffer, buffer.length);
1475 new NSCell (((NSTextField) view).cell ()).setTitle (nsstring);
1476 selectionRange = null;
1477 }
1478
1479 void setFont(NSFont font) {
1480 if ((style & DWT.MULTI) !is 0) {
1481 ((NSTextView) view).setFont (font);
1482 return;
1483 }
1484 super.setFont (font);
1323 } 1485 }
1324 1486
1325 void setForeground (float [] color) { 1487 void setForeground (float [] color) {
1326 NSColor nsColor; 1488 NSColor nsColor;
1327 if (color is null) { 1489 if (color is null) {
1328 return; // TODO reset to OS default 1490 return; // TODO reset to OS default
1329 } else { 1491 } else {
1330 nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1); 1492 nsColor = NSColor.colorWithDeviceRed (color [0], color [1], color [2], 1);
1331 } 1493 }
1332 if ((style & DWT.SINGLE) !is 0) { 1494 if ((style & DWT.SINGLE) !is 0) {
1333 (cast(NSTextField)view).setTextColor(nsColor); 1495 (cast(NSTextField) view).setTextColor (nsColor);
1334 } else { 1496 } else {
1335 (cast(NSTextView)view).setTextColor_(nsColor); 1497 (cast(NSTextView) view).setTextColor (nsColor);
1336 } 1498 }
1337 } 1499 }
1338 1500
1339 /** 1501 /**
1340 * Sets the orientation of the receiver, which must be one 1502 * Sets the orientation of the receiver, which must be one
1352 * </ul> 1514 * </ul>
1353 * 1515 *
1354 * @since 2.1.2 1516 * @since 2.1.2
1355 */ 1517 */
1356 public void setOrientation (int orientation) { 1518 public void setOrientation (int orientation) {
1357 checkWidget(); 1519 checkWidget ();
1358 } 1520 }
1359 1521
1360 /** 1522 /**
1361 * Sets the widget message. When the widget is created 1523 * Sets the widget message. When the widget is created
1362 * with the style <code>DWT.SEARCH</code>, the message text 1524 * with the style <code>DWT.SEARCH</code>, the message text
1415 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1577 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1416 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1578 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1417 * </ul> 1579 * </ul>
1418 */ 1580 */
1419 public void setSelection (int start) { 1581 public void setSelection (int start) {
1420 checkWidget(); 1582 checkWidget ();
1421 setSelection (start, start); 1583 setSelection (start, start);
1422 } 1584 }
1423 1585
1424 /** 1586 /**
1425 * Sets the selection to the range specified 1587 * Sets the selection to the range specified
1445 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1607 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1446 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1608 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1447 * </ul> 1609 * </ul>
1448 */ 1610 */
1449 public void setSelection (int start, int end) { 1611 public void setSelection (int start, int end) {
1450 checkWidget(); 1612 checkWidget ();
1451 if ((style & DWT.SINGLE) !is 0) { 1613 if ((style & DWT.SINGLE) !is 0) {
1452 // int length = getCharCount (); 1614 NSString str = new NSCell (((NSTextField) view).cell ()).title ();
1453 // ControlEditTextSelectionRec selection = new ControlEditTextSelectionRec (); 1615 int length = (int)/*64*/str.length ();
1454 // selection.selStart = cast(short) Math.min (Math.max (Math.min (start, end), 0), length); 1616 int selStart = Math.min (Math.max (Math.min (start, end), 0), length);
1455 // selection.selEnd = cast(short) Math.min (Math.max (Math.max (start, end), 0), length); 1617 int selEnd = Math.min (Math.max (Math.max (start, end), 0), length);
1456 // if (hasFocus ()) { 1618 selectionRange = new NSRange ();
1457 // OS.SetControlData (handle, OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection); 1619 selectionRange.location = selStart;
1458 // } else { 1620 selectionRange.length = selEnd - selStart;
1459 // this.selection = selection; 1621 if (this is display.getFocusControl ()) {
1460 // } 1622 NSText editor = view.window ().fieldEditor (false, view);
1461 } else { 1623 editor.setSelectedRange (selectionRange);
1462 //TODO - range test 1624 }
1625 } else {
1626 int length = (int)/*64*/((NSTextView) view).textStorage ().length ();
1627 int selStart = Math.min (Math.max (Math.min (start, end), 0), length);
1628 int selEnd = Math.min (Math.max (Math.max (start, end), 0), length);
1463 NSRange range = new NSRange (); 1629 NSRange range = new NSRange ();
1464 range.location = start; 1630 range.location = selStart;
1465 range.length = end - start + 1; 1631 range.length = selEnd - selStart;
1466 (cast(NSTextView)view).setSelectedRange (range); 1632 (cast(NSTextView) view).setSelectedRange (range);
1467 } 1633 }
1468 } 1634 }
1469 1635
1470 /** 1636 /**
1471 * Sets the selection to the range specified 1637 * Sets the selection to the range specified
1495 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1661 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1496 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1662 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1497 * </ul> 1663 * </ul>
1498 */ 1664 */
1499 public void setSelection (Point selection) { 1665 public void setSelection (Point selection) {
1500 checkWidget(); 1666 checkWidget ();
1501 if (selection is null) error (DWT.ERROR_NULL_ARGUMENT); 1667 if (selection is null) error (DWT.ERROR_NULL_ARGUMENT);
1502 setSelection (selection.x, selection.y); 1668 setSelection (selection.x, selection.y);
1503 } 1669 }
1504 1670
1505 /** 1671 /**
1517 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1683 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1518 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1684 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1519 * </ul> 1685 * </ul>
1520 */ 1686 */
1521 public void setTabs (int tabs) { 1687 public void setTabs (int tabs) {
1522 checkWidget(); 1688 checkWidget ();
1523 if (this.tabs is tabs) return; 1689 if (this.tabs is tabs) return;
1524 // if (txnObject is 0) return; 1690 // if (txnObject is 0) return;
1525 // this.tabs = tabs; 1691 // this.tabs = tabs;
1526 // TXNTab tab = new TXNTab (); 1692 // TXNTab tab = new TXNTab ();
1527 // tab.value = cast(short) (textExtent (new char[]{' '}, 0).x * tabs); 1693 // tab.value = cast(short) (textExtent (new char[]{' '}, 0).x * tabs);
1545 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1711 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1546 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1712 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1547 * </ul> 1713 * </ul>
1548 */ 1714 */
1549 public void setText (String string) { 1715 public void setText (String string) {
1550 checkWidget(); 1716 checkWidget ();
1551 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 1717 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
1552 if (hooks (DWT.Verify) || filters (DWT.Verify)) { 1718 if (hooks (DWT.Verify) || filters (DWT.Verify)) {
1553 string = verifyText (string, 0, getCharCount (), null); 1719 string = verifyText (string, 0, getCharCount (), null);
1554 if (string is null) return; 1720 if (string is null) return;
1555 } 1721 }
1556 NSString str = NSString.stringWith(string); 1722 if ((style & DWT.SINGLE) !is 0) {
1557 if ((style & DWT.SINGLE) !is 0) { 1723 setEditText (string);
1558 new NSCell((cast(NSTextField)view).cell()).setTitle(str); 1724 } else {
1559 } else { 1725 NSString str = NSString.stringWith (string);
1560 (cast(NSTextView)view).setString(str); 1726 (cast(NSTextView) view).setString (str);
1561 } 1727 }
1562 sendEvent (DWT.Modify); 1728 sendEvent (DWT.Modify);
1563 } 1729 }
1564 1730
1565 /** 1731 /**
1585 * </ul> 1751 * </ul>
1586 * 1752 *
1587 * @see #LIMIT 1753 * @see #LIMIT
1588 */ 1754 */
1589 public void setTextLimit (int limit) { 1755 public void setTextLimit (int limit) {
1590 checkWidget(); 1756 checkWidget ();
1591 if (limit is 0) error (DWT.ERROR_CANNOT_BE_ZERO); 1757 if (limit is 0) error (DWT.ERROR_CANNOT_BE_ZERO);
1592 textLimit = limit; 1758 textLimit = limit;
1593 } 1759 }
1594 1760
1595 /** 1761 /**
1603 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1769 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1604 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1770 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1605 * </ul> 1771 * </ul>
1606 */ 1772 */
1607 public void setTopIndex (int index) { 1773 public void setTopIndex (int index) {
1608 checkWidget(); 1774 checkWidget ();
1609 if ((style & DWT.SINGLE) !is 0) return; 1775 if ((style & DWT.SINGLE) !is 0) return;
1610 //TODO no working 1776 //TODO no working
1611 NSTextView widget = cast(NSTextView)view; 1777 // NSTextView widget = cast(NSTextView) view;
1612 NSRange range = new NSRange(); 1778 // NSRange range = new NSRange ();
1613 NSRect rect = widget.firstRectForCharacterRange(range); 1779 // NSRect rect = widget.firstRectForCharacterRange (range);
1614 view.scrollRectToVisible(rect); 1780 // view.scrollRectToVisible (rect);
1615 } 1781 }
1616 1782
1617 /** 1783 /**
1618 * Shows the selection. 1784 * Shows the selection.
1619 * <p> 1785 * <p>
1626 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1792 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1627 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1793 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1628 * </ul> 1794 * </ul>
1629 */ 1795 */
1630 public void showSelection () { 1796 public void showSelection () {
1631 checkWidget(); 1797 checkWidget ();
1632 if ((style & DWT.SINGLE) !is 0) { 1798 if ((style & DWT.SINGLE) !is 0) {
1633 setSelection (getSelection()); 1799 setSelection (getSelection ());
1634 } else { 1800 } else {
1635 NSTextView widget = cast(NSTextView)view; 1801 NSTextView widget = cast(NSTextView) view;
1636 widget.scrollRangeToVisible(widget.selectedRange()); 1802 widget.scrollRangeToVisible (widget.selectedRange ());
1637 } 1803 }
1804 }
1805
1806 void textViewDidChangeSelection(int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) {
1807 NSNotification notification = new NSNotification (aNotification);
1808 NSText editor = new NSText (notification.object ().id);
1809 selectionRange = editor.selectedRange ();
1810 }
1811
1812 void textDidChange (int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) {
1813 postEvent (DWT.Modify);
1814 }
1815
1816 NSRange textView_willChangeSelectionFromCharacterRange_toCharacterRange (int /*long*/ id, int /*long*/ sel, int /*long*/ aTextView, int /*long*/ oldSelectedCharRange, int /*long*/ newSelectedCharRange) {
1817 /*
1818 * If the selection is changing as a result of the receiver getting focus
1819 * then return the receiver's last selection range, otherwise the full
1820 * text will be automatically selected.
1821 */
1822 if (receivingFocus && selectionRange !is null) return selectionRange;
1823
1824 /* allow the selection change to proceed */
1825 NSRange result = new NSRange ();
1826 OS.memmove(result, newSelectedCharRange, NSRange.sizeof);
1827 return result;
1638 } 1828 }
1639 1829
1640 int traversalCode (int key, NSEvent theEvent) { 1830 int traversalCode (int key, NSEvent theEvent) {
1641 int bits = super.traversalCode (key, theEvent); 1831 int bits = super.traversalCode (key, theEvent);
1642 if ((style & DWT.READ_ONLY) !is 0) return bits; 1832 if ((style & DWT.READ_ONLY) !is 0) return bits;
1643 if ((style & DWT.MULTI) !is 0) { 1833 if ((style & DWT.MULTI) !is 0) {
1644 bits &= ~DWT.TRAVERSE_RETURN; 1834 bits &= ~DWT.TRAVERSE_RETURN;
1645 // if (key is 48 /* Tab */ && theEvent !is 0) { 1835 if (key is 48 /* Tab */ && theEvent !is null) {
1646 // int [] modifiers = new int [1]; 1836 int /*long*/ modifiers = theEvent.modifierFlags ();
1647 // OS.GetEventParameter (theEvent, OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers); 1837 bool next = (modifiers & OS.NSShiftKeyMask) is 0;
1648 // bool next = (modifiers [0] & OS.shiftKey) is 0; 1838 if (next && (modifiers & OS.NSControlKeyMask) is 0) {
1649 // if (next && (modifiers [0] & OS.controlKey) is 0) { 1839 bits &= ~(DWT.TRAVERSE_TAB_NEXT | DWT.TRAVERSE_TAB_PREVIOUS);
1650 // bits &= ~(DWT.TRAVERSE_TAB_NEXT | DWT.TRAVERSE_TAB_PREVIOUS); 1840 }
1651 // } 1841 }
1652 // }
1653 } 1842 }
1654 return bits; 1843 return bits;
1655 } 1844 }
1656 1845
1657 String verifyText (String string, int start, int end, Event keyEvent) { 1846 String verifyText (String string, int start, int end, NSEvent keyEvent) {
1658 Event event = new Event (); 1847 Event event = new Event ();
1848 if (keyEvent !is null) setKeyState(event, DWT.MouseDown, keyEvent);
1659 event.text = string; 1849 event.text = string;
1660 event.start = start; 1850 event.start = start;
1661 event.end = end; 1851 event.end = end;
1662 if (keyEvent !is null) {
1663 event.character = keyEvent.character;
1664 event.keyCode = keyEvent.keyCode;
1665 event.stateMask = keyEvent.stateMask;
1666 }
1667 /* 1852 /*
1668 * It is possible (but unlikely), that application 1853 * It is possible (but unlikely), that application
1669 * code could have disposed the widget in the verify 1854 * code could have disposed the widget in the verify
1670 * event. If this happens, answer null to cancel 1855 * event. If this happens, answer null to cancel
1671 * the operation. 1856 * the operation.