# HG changeset patch # User Jacob Carlborg # Date 1230488902 -3600 # Node ID 681769fb5a7a9890459f09f360c685f91ad32316 # Parent 2d895a35783344cf8c200154bdd426426f1a2441 Ported dwt.widgets.Text diff -r 2d895a357833 -r 681769fb5a7a dwt/internal/cocoa/OS.d --- a/dwt/internal/cocoa/OS.d Sun Dec 28 18:14:40 2008 +0100 +++ b/dwt/internal/cocoa/OS.d Sun Dec 28 19:28:22 2008 +0100 @@ -2977,7 +2977,7 @@ public static const int NSFocusRingOnly = 0; public static const int NSFocusRingTypeDefault = 0; public static const int NSFocusRingTypeExterior = 2; -public static const int NSFocusRingTypeNone = 1; +alias Cocoa.NSFocusRingType.NSFocusRingTypeNone NSFocusRingTypeNone; public static const int NSFontAntialiasedIntegerAdvancementsRenderingMode = 3; public static const int NSFontAntialiasedRenderingMode = 1; public static const int NSFontBoldTrait = 2; diff -r 2d895a357833 -r 681769fb5a7a dwt/widgets/Text.d --- a/dwt/widgets/Text.d Sun Dec 28 18:14:40 2008 +0100 +++ b/dwt/widgets/Text.d Sun Dec 28 19:28:22 2008 +0100 @@ -7,6 +7,9 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg *******************************************************************************/ module dwt.widgets.Text; @@ -47,6 +50,13 @@ import dwt.internal.cocoa.SWTTextField; import dwt.internal.cocoa.SWTTextView; +import dwt.internal.c.Carbon; +import dwt.internal.objc.cocoa.Cocoa; +import objc = dwt.internal.objc.runtime; +import dwt.widgets.Composite; +import dwt.widgets.Scrollable; +import dwt.widgets.TypedListener; + /** * Instances of this class are selectable user interface * objects that allow the user to enter and modify text. @@ -90,22 +100,22 @@ * the native widget implementation. *

*/ - public static final int LIMIT; + public static const int LIMIT; /** * The delimiter used by multi-line text widgets. When text * is queried and from the widget, it will be delimited using * this delimiter. */ - public static final String DELIMITER; - static final char PASSWORD = '\u2022'; + public static const String DELIMITER; + static const wchar PASSWORD = '\u2022'; /* * These values can be different on different platforms. * Therefore they are not initialized in the declaration * to stop the compiler from inlining. */ - static { + static this () { LIMIT = 0x7FFFFFFF; DELIMITER = "\r"; } @@ -262,7 +272,7 @@ */ public void append (String string) { checkWidget (); - if (string is null) error (DWT.ERROR_NULL_ARGUMENT); + //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); if (hooks (DWT.Verify) || filters (DWT.Verify)) { int charCount = getCharCount (); string = verifyText (string, charCount, charCount, null); @@ -276,14 +286,14 @@ NSTextView widget = cast(NSTextView) view; NSMutableString mutableString = widget.textStorage ().mutableString (); mutableString.appendString (str); - NSRange range = new NSRange (); + NSRange range = NSRange (); range.location = mutableString.length (); widget.scrollRangeToVisible (range); } if (string.length () !is 0) sendEvent (DWT.Modify); } -bool becomeFirstResponder (int /*long*/ id, int /*long*/ sel) { +bool becomeFirstResponder (objc.id id, objc.SEL sel) { receivingFocus = true; bool result = super.becomeFirstResponder (id, sel); receivingFocus = false; @@ -329,20 +339,20 @@ int width = 0, height = 0; if ((style & DWT.SINGLE) !is 0) { NSTextField widget = cast(NSTextField) view; - NSRect rect = new NSRect (); + NSRect rect = NSRect (); rect.width = rect.height = Float.MAX_VALUE; NSSize size = widget.cell ().cellSizeForBounds (rect); - width = (int)Math.ceil (size.width); + width = cast(int)Math.ceil (size.width); height = cast(int)Math.ceil (size.height); } else { NSTextView widget = cast(NSTextView) view; - NSSize oldSize = null; + NSSize oldSize; NSTextContainer textContainer = widget.textContainer (); if ((style & DWT.WRAP) !is 0) { widget.setHorizontallyResizable (true); textContainer.setWidthTracksTextView (false); oldSize = textContainer.containerSize (); - NSSize csize = new NSSize (); + NSSize csize = NSSize (); csize.width = wHint !is DWT.DEFAULT ? wHint : Float.MAX_VALUE; csize.height = hHint !is DWT.DEFAULT ? hHint : Float.MAX_VALUE; textContainer.setContainerSize (csize); @@ -356,8 +366,8 @@ textContainer.setWidthTracksTextView (true); textContainer.setContainerSize (oldSize); } - width = (int)(newRect.width + 1); - height = (int)(newRect.height + 1); + width = cast(int)(newRect.width + 1); + height = cast(int)(newRect.height + 1); } if (width <= 0) width = DEFAULT_WIDTH; if (height <= 0) height = DEFAULT_HEIGHT; @@ -397,39 +407,39 @@ if ((style & DWT.SINGLE) !is 0) { NSTextField widget; if ((style & DWT.PASSWORD) !is 0) { - widget = cast(NSTextField) new SWTSecureTextField ().alloc (); + widget = cast(NSTextField) (new SWTSecureTextField ()).alloc (); } else if ((style & DWT.SEARCH) !is 0) { widget = cast(NSTextField) (new SWTSearchField ()).alloc (); } else { widget = cast(NSTextField) (new SWTTextField ()).alloc (); } - widget.initWithFrame (new NSRect ()); + widget.initWithFrame (NSRect ()); widget.setSelectable (true); widget.setEditable((style & DWT.READ_ONLY) is 0); if ((style & DWT.BORDER) is 0) { widget.setFocusRingType (OS.NSFocusRingTypeNone); widget.setBordered (false); } - int align = OS.NSLeftTextAlignment; - if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment; - if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment; - widget.setAlignment (align); + NSTextAlignment align_ = OS.NSLeftTextAlignment; + if ((style & DWT.CENTER) !is 0) align_ = OS.NSCenterTextAlignment; + if ((style & DWT.RIGHT) !is 0) align_ = OS.NSRightTextAlignment; + widget.setAlignment (align_); // widget.setTarget(widget); // widget.setAction(OS.sel_sendSelection); view = widget; } else { - NSScrollView scrollWidget = cast(NSScrollView) new SWTScrollView ().alloc (); - scrollWidget.initWithFrame (new NSRect ()); + NSScrollView scrollWidget = cast(NSScrollView) (new SWTScrollView ()).alloc (); + scrollWidget.initWithFrame (NSRect ()); scrollWidget.setHasVerticalScroller ((style & DWT.VERTICAL) !is 0); scrollWidget.setHasHorizontalScroller ((style & DWT.HORIZONTAL) !is 0); scrollWidget.setAutoresizesSubviews (true); if ((style & DWT.BORDER) !is 0) scrollWidget.setBorderType (OS.NSBezelBorder); - NSTextView widget = cast(NSTextView) new SWTTextView ().alloc (); - widget.initWithFrame (new NSRect ()); + NSTextView widget = cast(NSTextView) (new SWTTextView ()).alloc (); + widget.initWithFrame (NSRect ()); widget.setEditable ((style & DWT.READ_ONLY) is 0); - NSSize size = new NSSize (); + NSSize size = NSSize (); size.width = size.height = Float.MAX_VALUE; widget.setMaxSize (size); widget.setAutoresizingMask (OS.NSViewWidthSizable | OS.NSViewHeightSizable); @@ -438,15 +448,15 @@ NSTextContainer textContainer = widget.textContainer (); widget.setHorizontallyResizable (true); textContainer.setWidthTracksTextView (false); - NSSize csize = new NSSize (); + NSSize csize = NSSize (); csize.width = csize.height = Float.MAX_VALUE; textContainer.setContainerSize (csize); } - int align = OS.NSLeftTextAlignment; - if ((style & DWT.CENTER) !is 0) align = OS.NSCenterTextAlignment; - if ((style & DWT.RIGHT) !is 0) align = OS.NSRightTextAlignment; - widget.setAlignment (align); + NSTextAlignment align_ = OS.NSLeftTextAlignment; + if ((style & DWT.CENTER) !is 0) align_ = OS.NSCenterTextAlignment; + if ((style & DWT.RIGHT) !is 0) align_ = OS.NSRightTextAlignment; + widget.setAlignment (align_); // widget.setTarget(widget); // widget.setAction(OS.sel_sendSelection); @@ -492,7 +502,7 @@ if ((style & DWT.SINGLE) !is 0) { insertEditText (newText); } else { - NSTextView widget = (NSTextView) view; + NSTextView widget = cast(NSTextView) view; widget.replaceCharactersInRange (widget.selectedRange (), NSString.stringWith (newText)); } cut = false; @@ -524,7 +534,7 @@ super.deregister(); if ((style & DWT.SINGLE) !is 0) { - display.removeWidget(((NSControl)view).cell()); + display.removeWidget((cast(NSControl)view).cell()); } } @@ -609,7 +619,7 @@ return 0; } else { NSRange range = (cast(NSTextView)view).selectedRange(); - return (int)/*64*/range.location; + return cast(int)/*64*/range.location; } } @@ -626,9 +636,9 @@ public int getCharCount () { checkWidget (); if ((style & DWT.SINGLE) !is 0) { - return cast(int)/*64*/new NSCell ((cast(NSControl) view).cell ()).title ().length (); + return cast(int)/*64*/(new NSCell ((cast(NSControl) view).cell ())).title ().length (); } else { - return (int)/*64*/((NSTextView) view).textStorage ().length (); + return cast(int)/*64*/(cast(NSTextView) view).textStorage ().length (); } } @@ -692,19 +702,19 @@ char [] getEditText () { NSString str = null; if ((style & DWT.SINGLE) !is 0) { - str = new NSTextFieldCell (((NSTextField) view).cell ()).title (); + str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title (); } else { - str = ((NSTextView)view).textStorage().string(); + str = (cast(NSTextView)view).textStorage().string(); } - int length = (int)/*64*/str.length (); - char [] buffer = new char [length]; + NSUInteger length_ = str.length (); + char [] buffer = new char [length_]; if (hiddenText !is null) { - hiddenText.getChars (0, length, buffer, 0); + hiddenText.getChars (0, length_, buffer, 0); } else { - NSRange range = new NSRange (); - range.length = length; - str.getCharacters (buffer, range); + NSRange range = NSRange (); + range.length = length_; + str.getCharacters (buffer.toString16().ptr, range); } return buffer; } @@ -712,23 +722,23 @@ char [] getEditText (int start, int end) { NSString str = null; if ((style & DWT.SINGLE) !is 0) { - str = new NSTextFieldCell (((NSTextField) view).cell ()).title (); + str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title (); } else { - str = ((NSTextView)view).textStorage().string(); + str = (cast(NSTextView)view).textStorage().string(); } - int length = (int)/*64*/str.length (); + int length = cast(int)/*64*/str.length (); end = Math.min (end, length - 1); if (start > end) return new char [0]; start = Math.max (0, start); - NSRange range = new NSRange (); + NSRange range = NSRange (); range.location = start; range.length = Math.max (0, end - start + 1); - char [] buffer = new char [(int)/*64*/range.length]; + char [] buffer = new char [range.length]; if (hiddenText !is null) { - hiddenText.getChars ((int)/*64*/range.location, (int)/*64*/(range.location + range.length), buffer, 0); + hiddenText.getChars (cast(int)/*64*/range.location, cast(int)/*64*/(range.location + range.length), buffer, 0); } else { - str.getCharacters (buffer, range); + str.getCharacters (buffer.toString16().ptr, range); } return buffer; } @@ -858,14 +868,14 @@ checkWidget (); if ((style & DWT.SINGLE) !is 0) { if (selectionRange is null) { - NSString str = new NSTextFieldCell (((NSTextField) view).cell ()).title (); - return new Point((int)/*64*/str.length (), (int)/*64*/str.length ()); + NSString str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title (); + return new Point(cast(int)/*64*/str.length (), cast(int)/*64*/str.length ()); } - return new Point ((int)/*64*/selectionRange.location, (int)/*64*/(selectionRange.location + selectionRange.length)); + return new Point (cast(int)/*64*/selectionRange.location, cast(int)/*64*/(selectionRange.location + selectionRange.length)); } else { NSTextView widget = cast(NSTextView) view; NSRange range = widget.selectedRange (); - return new Point ((int)/*64*/range.location, (int)/*64*/(range.location + range.length)); + return new Point (cast(int)/*64*/range.location, cast(int)/*64*/(range.location + range.length)); } } @@ -886,7 +896,7 @@ } else { NSTextView widget = cast(NSTextView) view; NSRange range = widget.selectedRange (); - return (int)/*64*/range.length; + return cast(int)/*64*/range.length; } } @@ -905,14 +915,14 @@ if ((style & DWT.SINGLE) !is 0) { Point selection = getSelection (); if (selection.x is selection.y) return ""; - return new String (getEditText (selection.x, selection.y - 1)); + return new_String (getEditText (selection.x, selection.y - 1)); } else { NSTextView widget = cast(NSTextView) view; NSRange range = widget.selectedRange (); NSString str = widget.textStorage ().string (); - char[] buffer = new char [(int)/*64*/range.length]; - str.getCharacters (buffer, range); - return new String (buffer); + char[] buffer = new char [range.length]; + str.getCharacters (buffer.toString16().ptr, range); + return new_String (buffer); } } @@ -985,11 +995,11 @@ if ((style & DWT.SINGLE) !is 0) { return new_String (getEditText (start, end)); } - NSTextStorage storage = ((NSTextView) view).textStorage (); - end = Math.min (end, (int)/*64*/storage.length () - 1); + NSTextStorage storage = (cast(NSTextView) view).textStorage (); + end = Math.min (end, cast(int)/*64*/storage.length () - 1); if (start > end) return ""; //$NON-NLS-1$ start = Math.max (0, start); - NSRange range = new NSRange (); + NSRange range = NSRange (); range.location = start; range.length = end - start + 1; NSAttributedString substring = storage.attributedSubstringFromRange (range); @@ -1083,7 +1093,7 @@ */ public void insert (String string) { checkWidget (); - if (string is null) error (DWT.ERROR_NULL_ARGUMENT); + //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); if (hooks (DWT.Verify) || filters (DWT.Verify)) { Point selection = getSelection (); string = verifyText (string, selection.x, selection.y, null); @@ -1101,19 +1111,19 @@ } void insertEditText (String string) { - int length = string.length (); + int length_ = string.length (); Point selection = getSelection (); if (hasFocus () && hiddenText is null) { if (textLimit !is LIMIT) { int charCount = getCharCount(); - if (charCount - (selection.y - selection.x) + length > textLimit) { - length = textLimit - charCount + (selection.y - selection.x); + if (charCount - (selection.y - selection.x) + length_ > textLimit) { + length_ = textLimit - charCount + (selection.y - selection.x); } } - char [] buffer = new char [length]; + char [] buffer = new char [length_]; string.getChars (0, buffer.length, buffer, 0); - NSString nsstring = NSString.stringWithCharacters (buffer, buffer.length); - NSText editor = ((NSTextField) view).currentEditor (); + NSString nsstring = NSString.stringWithCharacters (buffer.toString16().ptr, buffer.length); + NSText editor = (cast(NSTextField) view).currentEditor (); editor.replaceCharactersInRange (editor.selectedRange (), nsstring); selectionRange = null; } else { @@ -1157,7 +1167,7 @@ if ((style & DWT.SINGLE) !is 0) { insertEditText (newText); } else { - NSTextView textView = (NSTextView) view; + NSTextView textView = cast(NSTextView) view; textView.replaceCharactersInRange (textView.selectedRange (), NSString.stringWith (newText)); } paste = false; @@ -1170,7 +1180,7 @@ insertEditText (oldText); } else { //TODO check text limit - ((NSTextView) view).paste (null); + (cast(NSTextView) view).paste (null); } } sendEvent (DWT.Modify); @@ -1180,7 +1190,7 @@ super.register(); if ((style & DWT.SINGLE) !is 0) { - display.addWidget(((NSControl)view).cell(), this); + display.addWidget((cast(NSControl)view).cell(), this); } } @@ -1285,13 +1295,13 @@ if (!result) return result; if (type !is DWT.KeyDown) return result; int stateMask = 0; - int /*long*/ modifierFlags = nsEvent.modifierFlags(); + NSUInteger modifierFlags = nsEvent.modifierFlags(); if ((modifierFlags & OS.NSAlternateKeyMask) !is 0) stateMask |= DWT.ALT; if ((modifierFlags & OS.NSShiftKeyMask) !is 0) stateMask |= DWT.SHIFT; if ((modifierFlags & OS.NSControlKeyMask) !is 0) stateMask |= DWT.CONTROL; if ((modifierFlags & OS.NSCommandKeyMask) !is 0) stateMask |= DWT.COMMAND; if (stateMask is DWT.COMMAND) { - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); switch (keyCode) { case 7: /* X */ cut (); @@ -1305,7 +1315,7 @@ } } if ((style & DWT.SINGLE) !is 0) { - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); switch (keyCode) { case 76: /* KP Enter */ case 36: /* Return */ @@ -1317,9 +1327,9 @@ int charCount = getCharCount (); Point selection = getSelection (); int start = selection.x, end = selection.y; - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); NSString characters = nsEvent.charactersIgnoringModifiers(); - char character = (char) characters.characterAtIndex(0); + char character = cast(char) characters.characterAtIndex(0); switch (keyCode) { case 51: /* Backspace */ if (start is end) { @@ -1339,7 +1349,7 @@ break; default: if (character !is '\t' && character < 0x20) return true; - oldText = new String (new char [] {character}); + oldText = new String ([character]); } String newText = verifyText (oldText, start, end, nsEvent); if (newText is null) return false; @@ -1352,7 +1362,7 @@ insertEditText (newText); } else { NSString str = NSString.stringWith (newText); - NSTextView widget = (NSTextView) view; + NSTextView widget = cast(NSTextView) view; NSRange range = widget.selectedRange (); widget.textStorage ().replaceCharactersInRange (range, str); } @@ -1361,7 +1371,7 @@ return result; } -void setBackground (float [] color) { +void setBackground (CGFloat [] color) { NSColor nsColor; if (color is null) { return; // TODO reset to OS default @@ -1471,20 +1481,20 @@ buffer = new char [Math.min(string.length (), textLimit)]; string.getChars (0, buffer.length, buffer, 0); } - NSString nsstring = NSString.stringWithCharacters (buffer, buffer.length); - new NSCell (((NSTextField) view).cell ()).setTitle (nsstring); + NSString nsstring = NSString.stringWithCharacters (buffer.toString16().ptr, buffer.length); + (new NSCell ((cast(NSTextField) view).cell ())).setTitle (nsstring); selectionRange = null; } void setFont(NSFont font) { if ((style & DWT.MULTI) !is 0) { - ((NSTextView) view).setFont (font); + (cast(NSTextView) view).setFont (font); return; } super.setFont (font); } -void setForeground (float [] color) { +void setForeground (CGFloat [] color) { NSColor nsColor; if (color is null) { return; // TODO reset to OS default @@ -1543,7 +1553,7 @@ */ public void setMessage (String message) { checkWidget (); - if (message is null) error (DWT.ERROR_NULL_ARGUMENT); + //if (message is null) error (DWT.ERROR_NULL_ARGUMENT); this.message = message; // if ((style & DWT.SEARCH) !is 0) { // char [] buffer = new char [message.length ()]; @@ -1611,11 +1621,11 @@ public void setSelection (int start, int end) { checkWidget (); if ((style & DWT.SINGLE) !is 0) { - NSString str = new NSCell (((NSTextField) view).cell ()).title (); - int length = (int)/*64*/str.length (); + NSString str = (new NSCell ((cast(NSTextField) view).cell ())).title (); + int length = cast(int)/*64*/str.length (); int selStart = Math.min (Math.max (Math.min (start, end), 0), length); int selEnd = Math.min (Math.max (Math.max (start, end), 0), length); - selectionRange = new NSRange (); + selectionRange = NSRange (); selectionRange.location = selStart; selectionRange.length = selEnd - selStart; if (this is display.getFocusControl ()) { @@ -1623,10 +1633,10 @@ editor.setSelectedRange (selectionRange); } } else { - int length = (int)/*64*/((NSTextView) view).textStorage ().length (); + int length = cast(int)/*64*/(cast(NSTextView) view).textStorage ().length (); int selStart = Math.min (Math.max (Math.min (start, end), 0), length); int selEnd = Math.min (Math.max (Math.max (start, end), 0), length); - NSRange range = new NSRange (); + NSRange range = NSRange (); range.location = selStart; range.length = selEnd - selStart; (cast(NSTextView) view).setSelectedRange (range); @@ -1714,7 +1724,7 @@ */ public void setText (String string) { checkWidget (); - if (string is null) error (DWT.ERROR_NULL_ARGUMENT); + //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); if (hooks (DWT.Verify) || filters (DWT.Verify)) { string = verifyText (string, 0, getCharCount (), null); if (string is null) return; @@ -1775,7 +1785,7 @@ if ((style & DWT.SINGLE) !is 0) return; //TODO no working // NSTextView widget = cast(NSTextView) view; -// NSRange range = new NSRange (); +// NSRange range = NSRange (); // NSRect rect = widget.firstRectForCharacterRange (range); // view.scrollRectToVisible (rect); } @@ -1803,17 +1813,17 @@ } } -void textViewDidChangeSelection(int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) { +void textViewDidChangeSelection(objc.id id, objc.SEL sel, objc.id aNotification) { NSNotification notification = new NSNotification (aNotification); NSText editor = new NSText (notification.object ().id); selectionRange = editor.selectedRange (); } -void textDidChange (int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) { +void textDidChange (objc.id id, objc.SEL sel, objc.id aNotification) { postEvent (DWT.Modify); } -NSRange textView_willChangeSelectionFromCharacterRange_toCharacterRange (int /*long*/ id, int /*long*/ sel, int /*long*/ aTextView, int /*long*/ oldSelectedCharRange, int /*long*/ newSelectedCharRange) { +NSRange textView_willChangeSelectionFromCharacterRange_toCharacterRange (objc.id id, objc.SEL sel, objc.id aTextView, objc.id oldSelectedCharRange, objc.id newSelectedCharRange) { /* * If the selection is changing as a result of the receiver getting focus * then return the receiver's last selection range, otherwise the full @@ -1822,8 +1832,8 @@ if (receivingFocus && selectionRange !is null) return selectionRange; /* allow the selection change to proceed */ - NSRange result = new NSRange (); - OS.memmove(result, newSelectedCharRange, NSRange.sizeof); + NSRange result = NSRange (); + OS.memmove(&result, newSelectedCharRange, NSRange.sizeof); return result; } @@ -1833,7 +1843,7 @@ if ((style & DWT.MULTI) !is 0) { bits &= ~DWT.TRAVERSE_RETURN; if (key is 48 /* Tab */ && theEvent !is null) { - int /*long*/ modifiers = theEvent.modifierFlags (); + NSUInteger modifiers = theEvent.modifierFlags (); bool next = (modifiers & OS.NSShiftKeyMask) is 0; if (next && (modifiers & OS.NSControlKeyMask) is 0) { bits &= ~(DWT.TRAVERSE_TAB_NEXT | DWT.TRAVERSE_TAB_PREVIOUS);