Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / TField.java
index 0a99840db7f06d9aadff53e6fa11aa35520472ee..7c8b5bc415e62882a24941734da6ac213c706b75 100644 (file)
@@ -219,12 +219,8 @@ public class TField extends TWidget {
 
         if (keypress.equals(kbLeft)) {
             if (position > 0) {
-                if (position < text.length()) {
-                    screenPosition -= StringUtils.width(text.codePointAt(position));
-                } else {
-                    screenPosition--;
-                }
-                position--;
+                screenPosition -= StringUtils.width(text.codePointBefore(position));
+                position -= Character.charCount(text.codePointBefore(position));
             }
             if (fixed == false) {
                 if ((screenPosition == windowStart) && (windowStart > 0)) {
@@ -239,19 +235,23 @@ public class TField extends TWidget {
         if (keypress.equals(kbRight)) {
             if (position < text.length()) {
                 screenPosition += StringUtils.width(text.codePointAt(position));
-                position++;
+                position += Character.charCount(text.codePointAt(position));
                 if (fixed == true) {
                     if (screenPosition == getWidth()) {
                         screenPosition--;
-                        position--;
+                        position -= Character.charCount(text.codePointAt(position));
                     }
                 } else {
-                    if ((screenPosition - windowStart) >= getWidth()) {
+                    while ((screenPosition - windowStart +
+                            StringUtils.width(text.codePointAt(text.length() - 1)))
+                        > getWidth()
+                    ) {
                         windowStart += StringUtils.width(text.codePointAt(
                             screenToTextPosition(windowStart)));
                     }
                 }
             }
+            assert (position <= text.length());
             return;
         }
 
@@ -286,7 +286,7 @@ public class TField extends TWidget {
 
         if (keypress.equals(kbBackspace) || keypress.equals(kbBackspaceDel)) {
             if (position > 0) {
-                position--;
+                position -= Character.charCount(text.codePointBefore(position));
                 text = text.substring(0, position)
                         + text.substring(position + 1);
                 screenPosition = StringUtils.width(text.substring(0, position));
@@ -378,6 +378,17 @@ public class TField extends TWidget {
     // TWidget ----------------------------------------------------------------
     // ------------------------------------------------------------------------
 
+    /**
+     * Override TWidget's height: we can only set height at construction
+     * time.
+     *
+     * @param height new widget height (ignored)
+     */
+    @Override
+    public void setHeight(final int height) {
+        // Do nothing
+    }
+
     /**
      * Draw the text field.
      */
@@ -416,6 +427,7 @@ public class TField extends TWidget {
     private String codePointString(final int ch) {
         StringBuilder sb = new StringBuilder(1);
         sb.append(Character.toChars(ch));
+        assert (Character.charCount(ch) == sb.length());
         return sb.toString();
     }
 
@@ -467,11 +479,11 @@ public class TField extends TWidget {
     protected void dispatch(final boolean enter) {
         if (enter) {
             if (enterAction != null) {
-                enterAction.DO();
+                enterAction.DO(this);
             }
         } else {
             if (updateAction != null) {
-                updateAction.DO();
+                updateAction.DO(this);
             }
         }
     }
@@ -590,7 +602,7 @@ public class TField extends TWidget {
         screenPosition = StringUtils.width(text);
         if (fixed == true) {
             if (screenPosition >= getWidth()) {
-                position = text.length() - 1;
+                position -= Character.charCount(text.codePointBefore(position));
                 screenPosition = StringUtils.width(text) - 1;
              }
         } else {
@@ -612,7 +624,7 @@ public class TField extends TWidget {
     public void setPosition(final int position) {
         if ((position < 0) || (position >= text.length())) {
             throw new IndexOutOfBoundsException("Max length is " +
-                StringUtils.width(text) + ", requested position " + position);
+                text.length() + ", requested position " + position);
         }
         this.position = position;
         normalizeWindowStart();