changeset 285:75cb516e281e

StyledText fixes from dwt-win: Fix: delete the whole codepoint for backspace. Fix: getPointAtPosition handle whole codepoints correctly.
author Frank Benoit <benoit@tionex.de>
date Tue, 05 Aug 2008 01:15:34 +0200
parents 6956821fe8ed
children 4c1340edee0d 695802b523c0
files dwt/custom/StyledText.d dwt/dwthelper/utils.d
diffstat 2 files changed, 44 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/dwt/custom/StyledText.d	Mon Aug 04 02:06:13 2008 +0200
+++ b/dwt/custom/StyledText.d	Tue Aug 05 01:15:34 2008 +0200
@@ -2240,12 +2240,13 @@
         int lineIndex = content.getLineAtOffset(caretOffset);
         int lineOffset = content.getOffsetAtLine(lineIndex);
         if (caretOffset is lineOffset) {
+            // DWT: on line start, delete line break
             lineOffset = content.getOffsetAtLine(lineIndex - 1);
             event.start = lineOffset + content.getLine(lineIndex - 1).length;
             event.end = caretOffset;
         } else {
             TextLayout layout = renderer.getTextLayout(lineIndex);
-            int start = layout.getPreviousOffset(caretOffset - lineOffset, DWT.MOVEMENT_CHAR);
+            int start = layout.getPreviousOffset(caretOffset - lineOffset, DWT.MOVEMENT_CLUSTER);
             renderer.disposeTextLayout(layout);
             event.start = start + lineOffset;
             event.end = caretOffset;
@@ -3987,7 +3988,7 @@
 int getOffsetAtPoint(int x, int y, int lineIndex) {
     TextLayout layout = renderer.getTextLayout(lineIndex);
     x += horizontalScrollOffset - leftMargin;
-    int[] trailing = new int[1];
+    int[1] trailing;
     int offsetInLine = layout.getOffset(x, y, trailing);
     caretAlignment = OFFSET_LEADING;
     if (trailing[0] !is 0) {
@@ -4909,7 +4910,9 @@
     TextLayout layout = renderer.getTextLayout(lineIndex);
     if (lineLength !is 0  && offsetInLine <= lineLength) {
         if (offsetInLine is lineLength) {
-            point = layout.getLocation(offsetInLine - 1, true);
+            // DWT: Instead of go back one byte, go back one codepoint
+            int offsetInLine_m1 = layout.getPreviousOffset(offsetInLine, DWT.MOVEMENT_CLUSTER);
+            point = layout.getLocation(offsetInLine_m1, true);
         } else {
             switch (caretAlignment) {
                 case OFFSET_LEADING:
@@ -4920,7 +4923,9 @@
                     if (offsetInLine is 0) {
                         point = layout.getLocation(offsetInLine, false);
                     } else {
-                        point = layout.getLocation(offsetInLine - 1, true);
+                        // DWT: Instead of go back one byte, go back one codepoint
+                        int offsetInLine_m1 = layout.getPreviousOffset(offsetInLine, DWT.MOVEMENT_CLUSTER);
+                        point = layout.getLocation(offsetInLine_m1, true);
                     }
                     break;
             }
@@ -8323,6 +8328,7 @@
     setCaretLocation();
     super.redraw();
 }
+// DWT: If necessary, scroll to show the location
 bool showLocation(Rectangle rect, bool scrollPage) {
     int clientAreaWidth = this.clientAreaWidth - leftMargin - rightMargin;
     int clientAreaHeight = this.clientAreaHeight - topMargin - bottomMargin;
--- a/dwt/dwthelper/utils.d	Mon Aug 04 02:06:13 2008 +0200
+++ b/dwt/dwthelper/utils.d	Tue Aug 05 01:15:34 2008 +0200
@@ -340,9 +340,13 @@
     return res;
 }
 int indexToCodepointIndex( String str, int index ){
+    if( index < 0 ) return index;
     int i = 0;
     int res = 0;
     while( i < index ){
+        if( i >= str.length ){
+            break;
+        }
         if( str[i] < 0x80 ){
             i+=1;
         }
@@ -435,7 +439,7 @@
                 i+=4;
             }
             else{
-                Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str );
+                Trace.formatln( "invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
                 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
             }
             searchRelCp--;
@@ -446,8 +450,9 @@
             do{
                 i--;
                 if( i < 0 ){
-                    Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
-                    tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
+                    return -1;
+                    //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
+                    //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
                 }
             } while(( str[i] & 0xC0 ) is 0x80 );
             searchRelCp++;
@@ -470,6 +475,32 @@
     }
     return offset;
 }
+int utf8OffsetIncr( String str, int offset ){
+    int res = offset +1;
+    if( str.length <= res || res <= 0 ){
+        return res;
+    }
+    int tries = 4;
+    while(( str[res] & 0xC0 ) is 0x80 ){
+        res++;
+        assert( tries-- > 0 );
+    }
+    return res;
+}
+int utf8OffsetDecr( String str, int offset ){
+    int res = offset-1;
+    if( str.length <= res || res <= 0 ){
+        return res;
+    }
+    int tries = 4;
+    while(( str[res] & 0xC0 ) is 0x80 ){
+        res--;
+        assert( tries-- > 0 );
+    }
+    Trace.formatln( "utf8OffsetDecr {}->{}", offset, res );
+    Trace.memory( str );
+    return res;
+}
 
 bool CharacterIsDefined( dchar ch ){
     return (ch in tango.text.UnicodeData.unicodeData) !is null;