You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/10/29 19:31:08 UTC

svn commit: r831058 - /incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java

Author: gbrown
Date: Thu Oct 29 18:31:07 2009
New Revision: 831058

URL: http://svn.apache.org/viewvc?rev=831058&view=rev
Log:
Fix some issues in TextInput.

Modified:
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java?rev=831058&r1=831057&r2=831058&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java Thu Oct 29 18:31:07 2009
@@ -80,7 +80,7 @@
             int selectionStart = textInput.getSelectionStart();
             int selectionLength = textInput.getSelectionLength();
 
-            if (scrollX < 0) {
+            if (anchorX < 0) {
                 // Add the previous character to the selection
                 if (selectionStart > 0) {
                     selectionStart--;
@@ -107,7 +107,7 @@
     private int scrollLeft = 0;
 
     // TODO Use an anchor and a scroll direction like TextArea
-    private int scrollX = 0;
+    private int anchorX = 0;
 
     private BlinkCaretCallback blinkCaretCallback = new BlinkCaretCallback();
     private ApplicationContext.ScheduledCallback scheduledBlinkCaretCallback = null;
@@ -251,6 +251,8 @@
         }
 
         updateSelection();
+        showCaret(textInput.isFocused()
+            && textInput.getSelectionLength() == 0);
     }
 
     @Override
@@ -375,39 +377,45 @@
     }
 
     public int getInsertionPoint(int x, int y) {
-        LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
-        float ascent = lm.getAscent();
-
-        // Translate to glyph coordinates
-        x -= (padding.left - scrollLeft + 1);
-        y -= (padding.top + 1);
+        int offset = -1;
 
         int n = glyphVector.getNumGlyphs();
-        int i = 0;
+        if (n > 0) {
+            LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
+            float ascent = lm.getAscent();
 
-        while (i < n) {
-            Shape glyphLogicalBounds = glyphVector.getGlyphLogicalBounds(i);
+            // Translate to glyph coordinates
+            x -= (padding.left - scrollLeft + 1);
+            y -= (padding.top + 1);
+
+            if (x < 0) {
+                offset = 0;
+            } else if (x > glyphVector.getLogicalBounds().getWidth()) {
+                offset = n;
+            } else {
+                int i = 0;
+                while (i < n) {
+                    Shape glyphLogicalBounds = glyphVector.getGlyphLogicalBounds(i);
+
+                    if (glyphLogicalBounds.contains(x, y - ascent)) {
+                        Rectangle2D glyphBounds2D = glyphLogicalBounds.getBounds2D();
+
+                        if (x - glyphBounds2D.getX() > glyphBounds2D.getWidth() / 2) {
+                            // The user clicked on the right half of the character; select
+                            // the next character
+                            i++;
+                        }
 
-            if (glyphLogicalBounds.contains(x, y - ascent)) {
-                Rectangle2D glyphBounds2D = glyphLogicalBounds.getBounds2D();
+                        offset = i;
+                        break;
+                    }
 
-                if (x - glyphBounds2D.getX() > glyphBounds2D.getWidth() / 2) {
-                    // The user clicked on the right half of the character; select
-                    // the next character
                     i++;
                 }
-
-                break;
             }
-
-            i++;
-        }
-
-        if (i == n) {
-            i = -1;
         }
 
-        return i;
+        return offset;
     }
 
     public Bounds getCharacterBounds(int offset) {
@@ -887,7 +895,7 @@
 
                     textInput.setSelection(selectionStart, selectionLength);
                 } else {
-                    scrollX = x;
+                    anchorX = x;
 
                     if (scheduledScrollSelectionCallback == null) {
                         scheduledScrollSelectionCallback =
@@ -1301,12 +1309,22 @@
     private void updateSelection() {
         TextInput textInput = (TextInput)getComponent();
         TextNode textNode = textInput.getTextNode();
+        int n = textNode.getCharacterCount();
 
-        if (textNode.getCharacterCount() > 0) {
+        if (n > 0) {
             int selectionStart = textInput.getSelectionStart();
             int selectionLength = textInput.getSelectionLength();
 
-            Bounds leadingSelectionBounds = getCharacterBounds(selectionStart);
+            Bounds leadingSelectionBounds;
+            if (selectionStart < n) {
+                leadingSelectionBounds = getCharacterBounds(selectionStart);
+            } else {
+                // The insertion point is after the last character
+                Rectangle2D glyphVectorBounds = glyphVector.getLogicalBounds();
+                int x = (int)Math.ceil(glyphVectorBounds.getWidth()) + (padding.left - scrollLeft + 1);
+                int y = padding.top + 1;
+                leadingSelectionBounds = new Bounds(x, y, 0, averageCharacterSize.height);
+            }
 
             if (selectionLength == 0) {
                 caret = leadingSelectionBounds.toRectangle();