changeset 139:86fc761a24ae

Fixes #11
author Jacob Carlborg <doob@me.com>
date Thu, 04 Jun 2009 23:23:10 +0200
parents 3d9eb62cd2e3
children d8c1f0540bcb
files dwt/dwthelper/utils.d dwt/internal/cocoa/NSString.d dwt/widgets/Combo.d dwt/widgets/Text.d dwt/widgets/Widget.d
diffstat 5 files changed, 113 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/dwt/dwthelper/utils.d	Wed Jun 03 00:36:31 2009 +0200
+++ b/dwt/dwthelper/utils.d	Thu Jun 04 23:23:10 2009 +0200
@@ -555,6 +555,14 @@
     return cont.dup;
 }
 
+wchar[] new_String( wchar[] cont, int offset, int len ){
+    return cont[ offset .. offset+len ].dup;
+}
+
+wchar[] new_String (wchar[] cont){
+    return cont.dup;
+}
+
 public String toUpperCase( String str ){
     return tango.text.Unicode.toUpper( str );
 }
@@ -638,6 +646,18 @@
     return toString16( str );
 }
 
+public String fromString16( wchar[] str ){
+    return toString( str );
+}
+
+public char toChar(wchar c){
+    return [c].toString()[0];
+}
+
+public wchar toWChar(char c){
+    return [c].toString16()[0];
+}
+
 public bool endsWith( String src, String pattern ){
     if( src.length < pattern.length ){
         return false;
@@ -649,6 +669,10 @@
     return src == other;
 }
 
+public bool equals( wchar[] src, wchar[] other ){
+    return src == other;
+}
+
 public bool equalsIgnoreCase( String src, String other ){
     return tango.text.Unicode.toFold(src) == tango.text.Unicode.toFold(other);
 }
--- a/dwt/internal/cocoa/NSString.d	Wed Jun 03 00:36:31 2009 +0200
+++ b/dwt/internal/cocoa/NSString.d	Thu Jun 04 23:23:10 2009 +0200
@@ -21,8 +21,6 @@
 import dwt.internal.objc.cocoa.Cocoa;
 import objc = dwt.internal.objc.runtime;
 
-import tango.stdc.stdlib;
-
 public class NSString : NSObject {
 
 public this() {
@@ -43,10 +41,20 @@
     return dwt.dwthelper.utils.toString(buffer);
 }
 
+public wchar[] getString16() {
+    wchar[] buffer = new wchar[lengthOfCharacters];
+    getCharacters(buffer.ptr);
+    return buffer;
+}
+
 public static NSString stringWith(String str) {
     return stringWithUTF8String((str ~ '\0').ptr);
 }
 
+public static NSString stringWith16(wchar[] buffer) {
+	return stringWithCharacters(buffer.ptr, buffer.length);
+}
+
 public /*const*/char* UTF8String() {
     return cast(/*const*/char*) OS.objc_msgSend(this.id, OS.sel_UTF8String);
 }
--- a/dwt/widgets/Combo.d	Wed Jun 03 00:36:31 2009 +0200
+++ b/dwt/widgets/Combo.d	Thu Jun 04 23:23:10 2009 +0200
@@ -1235,7 +1235,7 @@
     int start = selection.x, end = selection.y;
     ushort keyCode = nsEvent.keyCode ();
     NSString characters = nsEvent.charactersIgnoringModifiers();
-    wchar character = characters.characterAtIndex(0);
+    char character = toChar(characters.characterAtIndex(0));
     switch (keyCode) {
         case 51: /* Backspace */
             if (start is end) {
@@ -1253,7 +1253,7 @@
             if (character !is '\t' && character < 0x20) return true;
             oldText = new_String ([character]);
     }
-    String newText = verifyText (oldText, start, end, nsEvent);
+    char[] newText = verifyText (oldText, start, end, nsEvent);
     if (newText is null) return false;
     if (charCount - (end - start) + newText.length () > textLimit) {
         return false;
--- a/dwt/widgets/Text.d	Wed Jun 03 00:36:31 2009 +0200
+++ b/dwt/widgets/Text.d	Thu Jun 04 23:23:10 2009 +0200
@@ -58,8 +58,6 @@
 import dwt.widgets.Scrollable;
 import dwt.widgets.TypedListener;
 
-import mambo.io;
-
 /**
  * Instances of this class are selectable user interface
  * objects that allow the user to enter and modify text.
@@ -98,7 +96,7 @@
     alias Scrollable.translateTraversal translateTraversal;
     
     int textLimit, tabs = 8;
-    char echoCharacter;
+    wchar echoCharacter;
     bool doubleClick, receivingFocus;
     wchar[] hiddenText, message;
     NSRange* selectionRange;
@@ -119,7 +117,7 @@
     * is queried and from the widget, it will be delimited using
     * this delimiter.
     */
-	public static const String DELIMITER;
+	public static const wchar[] DELIMITER;
 	static const wchar PASSWORD = '\u2022';
 
 	/*
@@ -284,7 +282,8 @@
  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
  * </ul>
  */
-public void append (String string) {
+public void append (String stri) {
+    wchar[] string = stri.toString16();
     checkWidget ();
     //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
     if (hooks (DWT.Verify) || filters (DWT.Verify)) {
@@ -292,7 +291,7 @@
         string = verifyText (string, charCount, charCount, null);
         if (string is null) return;
     }
-    NSString str = NSString.stringWith (string);
+    NSString str = NSString.stringWith16 (string);
     if ((style & DWT.SINGLE) !is 0) {
         setSelection (getCharCount ());
         insertEditText (string);
@@ -504,12 +503,12 @@
     checkWidget ();
     if ((style & DWT.READ_ONLY) !is 0) return;
     bool cut = true;
-    char [] oldText = null;
+    wchar [] oldText = null;
     Point oldSelection = getSelection ();
     if (hooks (DWT.Verify) || filters (DWT.Verify)) {
         if (oldSelection.x !is oldSelection.y) {
             oldText = getEditText (oldSelection.x, oldSelection.y - 1);
-            String newText = verifyText ("", oldSelection.x, oldSelection.y, null);
+            wchar[] newText = verifyText ("", oldSelection.x, oldSelection.y, null);
             if (newText is null) return;
             if (newText.length () !is 0) {
                 copyToClipboard (oldText);
@@ -517,7 +516,7 @@
                     insertEditText (newText);
                 } else {
                     NSTextView widget = cast(NSTextView) view;
-                    widget.replaceCharactersInRange (widget.selectedRange (), NSString.stringWith (newText));
+                    widget.replaceCharactersInRange (widget.selectedRange (), NSString.stringWith16 (newText));
                 }
                 cut = false;
             }
@@ -695,7 +694,7 @@
  */
 public char getEchoChar () {
     checkWidget ();
-    return echoCharacter;
+    return toChar(echoCharacter);
 }
 
 /**
@@ -713,7 +712,7 @@
     return (style & DWT.READ_ONLY) is 0;
 }
 
-char [] getEditText () {
+wchar [] getEditText () {
     NSString str = null;
     if ((style & DWT.SINGLE) !is 0) {
         str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title ();
@@ -730,10 +729,10 @@
         range.length = length_;
         str.getCharacters (buffer.ptr, range);
     }
-    return dwt.dwthelper.utils.toString(buffer);
+    return buffer;//.fromString16();
 }
 
-char [] getEditText (int start, int end) {
+wchar [] getEditText (int start, int end) {
     NSString str = null;
     if ((style & DWT.SINGLE) !is 0) {
         str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title ();
@@ -743,7 +742,7 @@
 
     int length = cast(int)/*64*/str.length ();
     end = Math.min (end, length - 1);
-    if (start > end) return new char [0];
+    if (start > end) return new wchar [0];
     start = Math.max (0, start);
     NSRange range = NSRange ();
     range.location = start;
@@ -754,7 +753,7 @@
     } else {
         str.getCharacters (buffer.ptr, range);
     }
-    return dwt.dwthelper.utils.toString(buffer);
+    return buffer;//.fromString16();
 }
 
 /**
@@ -787,7 +786,7 @@
  */
 public String getLineDelimiter () {
     checkWidget ();
-    return DELIMITER;
+    return DELIMITER.fromString16();
 }
 
 /**
@@ -845,7 +844,7 @@
  */
 public String getMessage () {
     checkWidget ();
-    return dwt.dwthelper.utils.toString(message);
+    return message.fromString16();
 }
 
 int getPosition (int x, int y) {
@@ -929,14 +928,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).fromString16());
     } else {
         NSTextView widget = cast(NSTextView) view;
         NSRange range = widget.selectedRange ();
         NSString str = widget.textStorage ().string ();
         wchar[] buffer = new wchar [range.length];        
         str.getCharacters (buffer.ptr, range);
-        return dwt.dwthelper.utils.toString(buffer);
+        return buffer.fromString16();
     }
 }
 
@@ -978,13 +977,24 @@
     checkWidget ();
     NSString str;
     if ((style & DWT.SINGLE) !is 0) {
-        return new_String (getEditText ());
+        return new_String (getEditText ().fromString16());
     } else {
         str = (cast(NSTextView)view).textStorage ().string ();
     }
     return str.getString();
 }
 
+private wchar[] getText16 () {
+    checkWidget ();
+    NSString str;
+    if ((style & DWT.SINGLE) !is 0) {
+        return getEditText ().dup;
+    } else {
+        str = (cast(NSTextView)view).textStorage ().string ();
+    }
+    return str.getString16();
+}
+
 /**
  * Returns a range of text.  Returns an empty string if the
  * start of the range is greater than the end.
@@ -1007,7 +1017,7 @@
     checkWidget ();
     if (!(start <= end && 0 <= end)) return ""; //$NON-NLS-1$
     if ((style & DWT.SINGLE) !is 0) {
-        return new_String (getEditText (start, end));
+        return new_String (getEditText (start, end).fromString16());
     }
     NSTextStorage storage = (cast(NSTextView) view).textStorage ();
     end = Math.min (end, cast(int)/*64*/storage.length () - 1);
@@ -1105,7 +1115,8 @@
  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
  * </ul>
  */
-public void insert (String string) {
+public void insert (String stri) {
+    wchar[] string = stri.toString16();
     checkWidget ();
     //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
     if (hooks (DWT.Verify) || filters (DWT.Verify)) {
@@ -1116,7 +1127,7 @@
     if ((style & DWT.SINGLE) !is 0) {
         insertEditText (string);
     } else {
-        NSString str = NSString.stringWith (string);
+        NSString str = NSString.stringWith16 (string);
         NSTextView widget = cast(NSTextView) view;
         NSRange range = widget.selectedRange ();
         widget.textStorage ().replaceCharactersInRange (range, str);
@@ -1124,7 +1135,7 @@
     if (string.length () !is 0) sendEvent (DWT.Modify);
 }
 
-void insertEditText (String string) {
+void insertEditText (wchar[] string) {
     int length_ = string.length ();
     Point selection = getSelection ();
     if (hasFocus () && hiddenText is null) {
@@ -1134,21 +1145,21 @@
                 length_ = textLimit - charCount + (selection.y - selection.x);
             }
         }
-        char [] buffer = new char [length_];
+        wchar [] buffer = new wchar [length_];
         string.getChars (0, buffer.length, buffer, 0);
-        NSString nsstring = NSString.stringWithCharacters (buffer.toString16().ptr, buffer.length);
+        NSString nsstring = NSString.stringWithCharacters (buffer.ptr, buffer.length);
         NSText editor = (cast(NSTextField) view).currentEditor ();
         editor.replaceCharactersInRange (editor.selectedRange (), nsstring);
         selectionRange = null;
     } else {
-        String oldText = getText ();
+        wchar[] oldText = getText16 ();
         if (textLimit !is LIMIT) {
             int charCount = oldText.length ();
             if (charCount - (selection.y - selection.x) + length_ > textLimit) {
                 string = string.substring(0, textLimit - charCount + (selection.y - selection.x));
             }
         }
-        String newText = oldText.substring (0, selection.x) ~ string ~ oldText.substring (selection.y);
+        wchar[] newText = oldText.substring (0, selection.x) ~ string ~ oldText.substring (selection.y);
         setEditText (newText);
         setSelection (selection.x + string.length ());
     }
@@ -1170,19 +1181,19 @@
     checkWidget ();
     if ((style & DWT.READ_ONLY) !is 0) return;
     bool paste = true;
-    String oldText = null;
+    wchar[] oldText = null;
     if (hooks (DWT.Verify) || filters (DWT.Verify)) {
-        oldText = getClipboardText ();
+        oldText = getClipboardText16 ();
         if (oldText !is null) {
             Point selection = getSelection ();
-            String newText = verifyText (oldText, selection.x, selection.y, null);
+            wchar[] newText = verifyText (oldText, selection.x, selection.y, null);
             if (newText is null) return;
             if (!newText.equals (oldText)) {
                 if ((style & DWT.SINGLE) !is 0) {
                     insertEditText (newText);
                 } else {
                     NSTextView textView = cast(NSTextView) view;
-                    textView.replaceCharactersInRange (textView.selectedRange (), NSString.stringWith (newText));
+                    textView.replaceCharactersInRange (textView.selectedRange (), NSString.stringWith16 (newText));
                 }
                 paste = false;
             }
@@ -1190,7 +1201,7 @@
     }
     if (paste) {
         if ((style & DWT.SINGLE) !is 0) {
-            if (oldText is null) oldText = getClipboardText ();
+            if (oldText is null) oldText = getClipboardText16 ();
             insertEditText (oldText);
         } else {
             //TODO check text limit
@@ -1339,13 +1350,13 @@
         }
     }
     if ((stateMask & DWT.COMMAND) !is 0) return result;
-    String oldText = "";
+    wchar[] oldText = "";
     int charCount = getCharCount ();
     Point selection = getSelection ();
     int start = selection.x, end = selection.y;
     ushort keyCode = nsEvent.keyCode ();
     NSString characters = nsEvent.charactersIgnoringModifiers();
-    char character = cast(char) characters.characterAtIndex(0);
+    wchar character = characters.characterAtIndex(0);
     switch (keyCode) {
         case 51: /* Backspace */
             if (start is end) {
@@ -1367,7 +1378,7 @@
             if (character !is '\t' && character < 0x20) return true;
             oldText = new_String ([character]);
     }
-    String newText = verifyText (oldText, start, end, nsEvent);
+    wchar[] newText = verifyText (oldText, start, end, nsEvent);
     if (newText is null) return false;
     if (charCount - (end - start) + newText.length () > textLimit) {
         return false;
@@ -1377,7 +1388,7 @@
         if ((style & DWT.SINGLE) !is 0) {
             insertEditText (newText);
         } else {
-            NSString str = NSString.stringWith (newText);
+            NSString str = NSString.stringWith16 (newText);
             NSTextView widget = cast(NSTextView) view;
             NSRange range = widget.selectedRange ();
             widget.textStorage ().replaceCharactersInRange (range, str);
@@ -1459,7 +1470,7 @@
 //  } else {
 //      OS.TXNEchoMode (txnObject, echo, OS.kTextEncodingMacUnicode, echo !is '\0');
 //  }
-    echoCharacter = echo;
+    echoCharacter = toWChar(echo);
 }
 
 /**
@@ -1486,18 +1497,18 @@
     }
 }
 
-void setEditText (String string) {
-    char [] buffer;
-    if ((style & DWT.PASSWORD) is 0 && echoCharacter !is '\0') {
-        hiddenText = string.toString16();
-        buffer = new char [Math.min(hiddenText.length (), textLimit)];
+void setEditText (wchar[] string) {
+    wchar [] buffer;
+    if ((style & DWT.PASSWORD) && echoCharacter !is '\0') {
+        hiddenText = string;
+        buffer = new wchar [Math.min(hiddenText.length (), textLimit)];
         for (int i = 0; i < buffer.length; i++) buffer [i] = echoCharacter;
     } else {
         hiddenText = null;
-        buffer = new char [Math.min(string.length (), textLimit)];
+        buffer = new wchar [Math.min(string.length (), textLimit)];
         string.getChars (0, buffer.length, buffer, 0);
     }
-    NSString nsstring = NSString.stringWithCharacters (buffer.toString16().ptr, buffer.length);
+    NSString nsstring = NSString.stringWithCharacters (buffer.ptr, buffer.length);
     (new NSCell ((cast(NSTextField) view).cell ())).setTitle (nsstring);
     selectionRange = null;
 }
@@ -1739,17 +1750,18 @@
  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
  * </ul>
  */
-public void setText (String string) {
+public void setText (String stri) {
+    wchar[] string = stri.toString16();
     checkWidget ();
     //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
-    if (hooks (DWT.Verify) || filters (DWT.Verify)) {
+   if (hooks (DWT.Verify) || filters (DWT.Verify)) {
         string = verifyText (string, 0, getCharCount (), null);
         if (string is null) return;
-    }
+   }
     if ((style & DWT.SINGLE) !is 0) {
         setEditText (string);
     } else {
-        NSString str = NSString.stringWith (string);
+        NSString str = NSString.stringWith16 (string);
         (cast(NSTextView) view).setString (str);
     }
     sendEvent (DWT.Modify);
@@ -1871,10 +1883,10 @@
     return bits;
 }
 
-String verifyText (String string, int start, int end, NSEvent keyEvent) {
+wchar[] verifyText (wchar[] string, int start, int end, NSEvent keyEvent) {
     Event event = new Event ();
     if (keyEvent !is null) setKeyState(event, DWT.MouseDown, keyEvent);
-    event.text = string;
+    event.text = string.fromString16();
     event.start = start;
     event.end = end;
     /*
@@ -1885,7 +1897,7 @@
      */
     sendEvent (DWT.Verify, event);
     if (!event.doit || isDisposed ()) return null;
-    return event.text;
+    return event.text.toString16();
 }
 
 }
--- a/dwt/widgets/Widget.d	Wed Jun 03 00:36:31 2009 +0200
+++ b/dwt/widgets/Widget.d	Thu Jun 04 23:23:10 2009 +0200
@@ -210,6 +210,12 @@
     return string !is null ? string.getString () : null;
 }
 
+wchar[] getClipboardText16 () {
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard ();
+    NSString string = pasteboard.stringForType (OS.NSStringPboardType);
+    return string !is null ? string.getString16 () : null;
+}
+
 NSBezierPath getClipping () {
     return null;
 }
@@ -459,6 +465,13 @@
     pasteboard.setString (NSString.stringWithCharacters (buf.ptr, buf.length), OS.NSStringPboardType);
 }
 
+void copyToClipboard (wchar [] buffer) {
+    if (buffer.length is 0) return;
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard ();
+    pasteboard.declareTypes (NSArray.arrayWithObject (OS.NSStringPboardType), null);
+    pasteboard.setString (NSString.stringWithCharacters (buffer.ptr, buffer.length), OS.NSStringPboardType);
+}
+
 void createHandle () {
 }