Merge branch 'subtree'
[fanfix.git] / src / jexer / TField.java
index 681ce7e93e0cbf278dd0d6aaadd7a9e92c79c813..90dd4e427abcf5660bfcc04b32b76c7c5da8edf7 100644 (file)
@@ -31,14 +31,16 @@ package jexer;
 import jexer.bits.CellAttributes;
 import jexer.bits.GraphicsChars;
 import jexer.bits.StringUtils;
+import jexer.event.TCommandEvent;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMouseEvent;
+import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
 /**
  * TField implements an editable text field.
  */
-public class TField extends TWidget {
+public class TField extends TWidget implements EditMenuUser {
 
     // ------------------------------------------------------------------------
     // Variables --------------------------------------------------------------
@@ -219,13 +221,8 @@ public class TField extends TWidget {
 
         if (keypress.equals(kbLeft)) {
             if (position > 0) {
-                if (position < codePointLength(text)) {
-                    screenPosition -= StringUtils.width(text.codePointBefore(position));
-                    position -= Character.charCount(text.codePointBefore(position));
-                } else {
-                    screenPosition--;
-                    position--;
-                }
+                screenPosition -= StringUtils.width(text.codePointBefore(position));
+                position -= Character.charCount(text.codePointBefore(position));
             }
             if (fixed == false) {
                 if ((screenPosition == windowStart) && (windowStart > 0)) {
@@ -238,21 +235,26 @@ public class TField extends TWidget {
         }
 
         if (keypress.equals(kbRight)) {
-            if (position < codePointLength(text)) {
+            if (position < text.length()) {
+                int lastPosition = position;
                 screenPosition += StringUtils.width(text.codePointAt(position));
                 position += Character.charCount(text.codePointAt(position));
                 if (fixed == true) {
                     if (screenPosition == getWidth()) {
                         screenPosition--;
-                        position -= Character.charCount(text.codePointAt(position));
+                        position -= Character.charCount(text.codePointAt(lastPosition));
                     }
                 } 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;
         }
 
@@ -276,7 +278,7 @@ public class TField extends TWidget {
         }
 
         if (keypress.equals(kbDel)) {
-            if ((codePointLength(text) > 0) && (position < codePointLength(text))) {
+            if ((text.length() > 0) && (position < text.length())) {
                 text = text.substring(0, position)
                         + text.substring(position + 1);
                 screenPosition = StringUtils.width(text.substring(0, position));
@@ -310,12 +312,12 @@ public class TField extends TWidget {
             && !keypress.getKey().isCtrl()
         ) {
             // Plain old keystroke, process it
-            if ((position == codePointLength(text))
+            if ((position == text.length())
                 && (StringUtils.width(text) < getWidth())) {
 
                 // Append case
                 appendChar(keypress.getKey().getChar());
-            } else if ((position < codePointLength(text))
+            } else if ((position < text.length())
                 && (StringUtils.width(text) < getWidth())) {
 
                 // Overwrite or insert a character
@@ -330,7 +332,7 @@ public class TField extends TWidget {
                     // Insert character
                     insertChar(keypress.getKey().getChar());
                 }
-            } else if ((position < codePointLength(text))
+            } else if ((position < text.length())
                 && (StringUtils.width(text) >= getWidth())) {
 
                 // Multiple cases here
@@ -353,7 +355,7 @@ public class TField extends TWidget {
                     screenPosition += StringUtils.width(text.codePointAt(position));
                     position += Character.charCount(keypress.getKey().getChar());
                 } else {
-                    if (position == codePointLength(text)) {
+                    if (position == text.length()) {
                         // Append this character
                         appendChar(keypress.getKey().getChar());
                     } else {
@@ -375,10 +377,58 @@ public class TField extends TWidget {
         super.onKeypress(keypress);
     }
 
+    /**
+     * Handle posted command events.
+     *
+     * @param command command event
+     */
+    @Override
+    public void onCommand(final TCommandEvent command) {
+        if (command.equals(cmCut)) {
+            // Copy text to clipboard, and then remove it.
+            getClipboard().copyText(text);
+            setText("");
+            return;
+        }
+
+        if (command.equals(cmCopy)) {
+            // Copy text to clipboard.
+            getClipboard().copyText(text);
+            return;
+        }
+
+        if (command.equals(cmPaste)) {
+            // Paste text from clipboard.
+            String newText = getClipboard().pasteText();
+            if (newText != null) {
+                setText(newText);
+            }
+            return;
+        }
+
+        if (command.equals(cmClear)) {
+            // Remove text.
+            setText("");
+            return;
+        }
+
+    }
+
     // ------------------------------------------------------------------------
     // 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.
      */
@@ -417,19 +467,10 @@ 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();
     }
 
-    /**
-     * Get the number of codepoints in a string.
-     *
-     * @param str the string
-     * @return the number of codepoints
-     */
-    private int codePointLength(final String str) {
-        return str.codePointCount(0, str.length());
-    }
-
     /**
      * Get field background character.
      *
@@ -466,7 +507,11 @@ public class TField extends TWidget {
         assert (text != null);
         this.text = text;
         position = 0;
+        screenPosition = 0;
         windowStart = 0;
+        if ((fixed == true) && (this.text.length() > getWidth())) {
+            this.text = this.text.substring(0, getWidth());
+        }
     }
 
     /**
@@ -478,11 +523,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);
             }
         }
     }
@@ -499,7 +544,7 @@ public class TField extends TWidget {
         }
 
         int n = 0;
-        for (int i = 0; i < codePointLength(text); i++) {
+        for (int i = 0; i < text.length(); i++) {
             n += StringUtils.width(text.codePointAt(i));
             if (n >= screenPosition) {
                 return i + 1;
@@ -507,7 +552,7 @@ public class TField extends TWidget {
         }
         // screenPosition exceeds the available text length.
         throw new IndexOutOfBoundsException("screenPosition " + screenPosition +
-            " exceeds available text length " + codePointLength(text));
+            " exceeds available text length " + text.length());
     }
 
     /**
@@ -552,7 +597,7 @@ public class TField extends TWidget {
         position += Character.charCount(ch);
         screenPosition += StringUtils.width(ch);
 
-        assert (position == codePointLength(text));
+        assert (position == text.length());
 
         if (fixed) {
             if (screenPosition >= getWidth()) {
@@ -597,11 +642,11 @@ public class TField extends TWidget {
      * adjust the window start to show as much of the field as possible.
      */
     public void end() {
-        position = codePointLength(text);
+        position = text.length();
         screenPosition = StringUtils.width(text);
         if (fixed == true) {
             if (screenPosition >= getWidth()) {
-                position = codePointLength(text) - 1;
+                position -= Character.charCount(text.codePointBefore(position));
                 screenPosition = StringUtils.width(text) - 1;
              }
         } else {
@@ -621,9 +666,9 @@ public class TField extends TWidget {
      * the available text
      */
     public void setPosition(final int position) {
-        if ((position < 0) || (position >= codePointLength(text))) {
+        if ((position < 0) || (position >= text.length())) {
             throw new IndexOutOfBoundsException("Max length is " +
-                codePointLength(text) + ", requested position " + position);
+                text.length() + ", requested position " + position);
         }
         this.position = position;
         normalizeWindowStart();
@@ -667,4 +712,44 @@ public class TField extends TWidget {
         updateAction = action;
     }
 
+    // ------------------------------------------------------------------------
+    // EditMenuUser -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Check if the cut menu item should be enabled.
+     *
+     * @return true if the cut menu item should be enabled
+     */
+    public boolean isEditMenuCut() {
+        return true;
+    }
+
+    /**
+     * Check if the copy menu item should be enabled.
+     *
+     * @return true if the copy menu item should be enabled
+     */
+    public boolean isEditMenuCopy() {
+        return true;
+    }
+
+    /**
+     * Check if the paste menu item should be enabled.
+     *
+     * @return true if the paste menu item should be enabled
+     */
+    public boolean isEditMenuPaste() {
+        return true;
+    }
+
+    /**
+     * Check if the clear menu item should be enabled.
+     *
+     * @return true if the clear menu item should be enabled
+     */
+    public boolean isEditMenuClear() {
+        return true;
+    }
+
 }