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/11/01 21:54:59 UTC

svn commit: r831759 - in /incubator/pivot/trunk: tools/src/org/apache/pivot/tools/wtk/ wtk/src/org/apache/pivot/wtk/ wtk/src/org/apache/pivot/wtk/content/ wtk/src/org/apache/pivot/wtk/skin/ wtk/src/org/apache/pivot/wtk/skin/terra/

Author: gbrown
Date: Sun Nov  1 20:54:59 2009
New Revision: 831759

URL: http://svn.apache.org/viewvc?rev=831759&view=rev
Log:
Reinstate auto-scrolling in TextInput; various other minor updates.

Modified:
    incubator/pivot/trunk/tools/src/org/apache/pivot/tools/wtk/EventLogger.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java

Modified: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/wtk/EventLogger.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/wtk/EventLogger.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/wtk/EventLogger.java (original)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/wtk/EventLogger.java Sun Nov  1 20:54:59 2009
@@ -31,8 +31,6 @@
 import org.apache.pivot.collections.HashMap;
 import org.apache.pivot.collections.HashSet;
 import org.apache.pivot.collections.Sequence;
-import org.apache.pivot.collections.immutable.ImmutableList;
-import org.apache.pivot.collections.immutable.ImmutableSet;
 import org.apache.pivot.util.ImmutableIterator;
 import org.apache.pivot.util.ListenerList;
 import org.apache.pivot.util.ThreadUtilities;

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java Sun Nov  1 20:54:59 2009
@@ -239,13 +239,14 @@
      * selection.
      */
     public void setSelectedIndex(int selectedIndex) {
+        if (selectedIndex < -1
+            || selectedIndex >= listData.getLength()) {
+            throw new IndexOutOfBoundsException();
+        }
+
         int previousSelectedIndex = this.selectedIndex;
 
         if (previousSelectedIndex != selectedIndex) {
-            if (selectedIndex < -1 || selectedIndex >= listData.getLength()) {
-                throw new IndexOutOfBoundsException();
-            }
-
             this.selectedIndex = selectedIndex;
             listButtonSelectionListeners.selectedIndexChanged(this, previousSelectedIndex);
         }

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java Sun Nov  1 20:54:59 2009
@@ -605,6 +605,21 @@
     }
 
     /**
+     * Sets the selection.
+     *
+     * @param selection
+     *
+     * @see #setSelection(int, int)
+     */
+    public final void setSelection(Span selection) {
+        if (selection == null) {
+            throw new IllegalArgumentException("selection is null.");
+        }
+
+        setSelection(Math.min(selection.start, selection.end), (int)selection.getLength());
+    }
+
+    /**
      * Selects all text.
      */
     public void selectAll() {

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java Sun Nov  1 20:54:59 2009
@@ -41,12 +41,11 @@
          * Returns the insertion point for a given location.
          *
          * @param x
-         * @param y
          *
          * @return
          * The insertion point for the given location.
          */
-        public int getInsertionPoint(int x, int y);
+        public int getInsertionPoint(int x);
 
         /**
          * Returns the bounds of the character at a given offset within the
@@ -507,6 +506,13 @@
         }
     }
 
+    /**
+     * Sets the selection.
+     *
+     * @param selection
+     *
+     * @see #setSelection(int, int)
+     */
     public final void setSelection(Span selection) {
         if (selection == null) {
             throw new IllegalArgumentException("selection is null.");
@@ -715,9 +721,9 @@
         }
     }
 
-    public int getInsertionPoint(int x, int y) {
+    public int getInsertionPoint(int x) {
         TextInput.Skin textInputSkin = (TextInput.Skin)getSkin();
-        return textInputSkin.getInsertionPoint(x, y);
+        return textInputSkin.getInsertionPoint(x);
     }
 
     public Bounds getCharacterBounds(int offset) {

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java Sun Nov  1 20:54:59 2009
@@ -45,17 +45,17 @@
 
     @Override
     public void render(Object data, Button button, boolean highlighted) {
-        Color color = Color.WHITE;
+        Color color;
 
         if (data instanceof ColorItem) {
             ColorItem colorItem = (ColorItem)data;
             color = colorItem.getColor();
+        } else  if (data instanceof Color) {
+            color = (Color)data;
+        } else if (data != null) {
+            color = GraphicsUtilities.decodeColor(data.toString());
         } else {
-            if (data instanceof Color) {
-                color = (Color)data;
-            } else if (data != null) {
-                color = GraphicsUtilities.decodeColor(data.toString());
-            }
+            color = Color.WHITE;
         }
 
         colorBadge.setColor(color);

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java?rev=831759&r1=831758&r2=831759&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Sun Nov  1 20:54:59 2009
@@ -1341,12 +1341,12 @@
     private DocumentView documentView = null;
 
     private int caretX = 0;
-    private int anchor = -1;
     private Rectangle caret = new Rectangle();
     private Area selection = null;
 
     private boolean caretOn = false;
 
+    private int anchor = -1;
     private Direction scrollDirection = null;
 
     private BlinkCaretCallback blinkCaretCallback = new BlinkCaretCallback();
@@ -1815,9 +1815,9 @@
     public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
         boolean consumed = super.mouseDown(component, button, x, y);
 
-        TextArea textArea = (TextArea)component;
-
         if (button == Mouse.Button.LEFT) {
+            TextArea textArea = (TextArea)component;
+
             anchor = getInsertionPoint(x, y);
 
             if (anchor != -1) {
@@ -1863,6 +1863,7 @@
         }
 
         anchor = -1;
+        scrollDirection = null;
 
         return consumed;
     }

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=831759&r1=831758&r2=831759&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 Sun Nov  1 20:54:59 2009
@@ -77,17 +77,40 @@
             TextInput textInput = (TextInput)getComponent();
             int selectionStart = textInput.getSelectionStart();
             int selectionLength = textInput.getSelectionLength();
+            int selectionEnd = selectionStart + selectionLength - 1;
 
-            // TODO
+            switch (scrollDirection) {
+                case FORWARD: {
+                    TextNode textNode = textInput.getTextNode();
+                    if (selectionEnd < textNode.getCharacterCount() - 1) {
+                        selectionEnd++;
+                        textInput.setSelection(selectionStart, selectionEnd - selectionStart + 1);
+                        scrollCharacterToVisible(selectionEnd);
+                    }
+
+                    break;
+                }
+
+                case BACKWARD: {
+                    if (selectionStart > 0) {
+                        selectionStart--;
+                        textInput.setSelection(selectionStart, selectionEnd - selectionStart + 1);
+                        scrollCharacterToVisible(selectionStart);
+                    }
+
+                    break;
+                }
 
-            textInput.setSelection(selectionStart, selectionLength);
+                default: {
+                    throw new RuntimeException();
+                }
+            }
         }
     }
 
     private GlyphVector glyphVector = null;
 
-    // TODO
-    // private int anchor = -1;
+    private int anchor = -1;
     private Rectangle caret = new Rectangle();
     private Rectangle selection = null;
 
@@ -95,6 +118,8 @@
 
     private boolean caretOn = true;
 
+    private Direction scrollDirection = null;
+
     private BlinkCaretCallback blinkCaretCallback = new BlinkCaretCallback();
     private ApplicationContext.ScheduledCallback scheduledBlinkCaretCallback = null;
 
@@ -288,11 +313,7 @@
 
         // Paint the bevel
         graphics.setColor(bevelColor);
-        GraphicsUtilities.drawLine(graphics, 1, 1, width - 2, Orientation.HORIZONTAL);
-
-        // Paint the border
-        graphics.setColor(borderColor);
-        GraphicsUtilities.drawRect(graphics, 0, 0, width, height);
+        GraphicsUtilities.drawLine(graphics, 0, 0, width, Orientation.HORIZONTAL);
 
         // Paint the content
         if (FONT_RENDER_CONTEXT.isAntiAliased()) {
@@ -372,21 +393,20 @@
                 graphics.fill(caret);
             }
         }
+
+        // Paint the border
+        graphics.setColor(borderColor);
+        GraphicsUtilities.drawRect(graphics, 0, 0, width, height);
     }
 
-    public int getInsertionPoint(int x, int y) {
+    public int getInsertionPoint(int x) {
         int offset = -1;
 
         if (glyphVector == null) {
             offset = 0;
         } else {
-            LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
-            float ascent = lm.getAscent();
-
             // Translate to glyph coordinates
             x -= (padding.left - scrollLeft + 1);
-            y -= (padding.top + 1);
-
 
             Rectangle2D textBounds = glyphVector.getLogicalBounds();
 
@@ -399,11 +419,14 @@
                 int i = 0;
                 while (i < n) {
                     Shape glyphBounds = glyphVector.getGlyphLogicalBounds(i);
+                    Rectangle2D glyphBounds2D = glyphBounds.getBounds2D();
 
-                    if (glyphBounds.contains(x, y - ascent)) {
-                        Rectangle2D glyphBounds2D = glyphBounds.getBounds2D();
+                    float glyphX = (float)glyphBounds2D.getX();
+                    float glyphWidth = (float)glyphBounds2D.getWidth();
+                    if (x >= glyphX
+                        && x < glyphX + glyphWidth) {
 
-                        if (x - glyphBounds2D.getX() > glyphBounds2D.getWidth() / 2) {
+                        if (x - glyphX > glyphWidth / 2) {
                             // The user clicked on the right half of the character; select
                             // the next character
                             i++;
@@ -886,46 +909,38 @@
 
         if (Mouse.getCapturer() == component) {
             TextInput textInput = (TextInput)getComponent();
-            TextNode textNode = textInput.getTextNode();
+            int width = getWidth();
 
-            if (textNode != null
-                && textNode.getCharacterCount() > 0) {
-                if (x >= 0
-                    && x < textInput.getWidth()) {
-                    // Stop the scroll selection timer
-                    if (scheduledScrollSelectionCallback != null) {
-                        scheduledScrollSelectionCallback.cancel();
-                        scheduledScrollSelectionCallback = null;
-                    }
+            if (x >= 0
+                && x < width) {
+                // Stop the scroll selection timer
+                if (scheduledScrollSelectionCallback != null) {
+                    scheduledScrollSelectionCallback.cancel();
+                    scheduledScrollSelectionCallback = null;
+                }
 
-                    // Get the current selection
-                    int selectionStart = textInput.getSelectionStart();
-                    int selectionLength = textInput.getSelectionLength();
+                scrollDirection = null;
 
-                    // Get the insertion index
-                    int index = getInsertionPoint(x, y);
+                int offset = getInsertionPoint(x);
 
-                    if (index < selectionStart) {
-                        selectionLength += (selectionStart - index);
-                        selectionStart = index;
+                if (offset != -1) {
+                    // Select the range
+                    if (offset > anchor) {
+                        textInput.setSelection(anchor, offset - anchor);
                     } else {
-                        if (index > selectionStart + selectionLength) {
-                            selectionLength = index - selectionStart;
-                        }
+                        textInput.setSelection(offset, anchor - offset);
                     }
+                }
+            } else {
+                if (scheduledScrollSelectionCallback == null) {
+                    scrollDirection = (x < 0) ? Direction.BACKWARD : Direction.FORWARD;
 
-                    textInput.setSelection(selectionStart, selectionLength);
-                } else {
-                    // TODO Set anchor offset
-
-                    if (scheduledScrollSelectionCallback == null) {
-                        scheduledScrollSelectionCallback =
-                            ApplicationContext.scheduleRecurringCallback(scrollSelectionCallback,
-                                SCROLL_RATE);
+                    scheduledScrollSelectionCallback =
+                        ApplicationContext.scheduleRecurringCallback(scrollSelectionCallback,
+                            SCROLL_RATE);
 
-                        // Run the callback once now to scroll the selection immediately
-                        scrollSelectionCallback.run();
-                    }
+                    // Run the callback once now to scroll the selection immediately
+                    scrollSelectionCallback.run();
                 }
             }
         } else {
@@ -941,21 +956,36 @@
 
     @Override
     public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
+        boolean consumed = super.mouseDown(component, button, x, y);
+
         if (button == Mouse.Button.LEFT) {
-            // Move the caret to the insertion point
             TextInput textInput = (TextInput)getComponent();
 
-            // TODO Use anchor here
-            int offset = getInsertionPoint(x, y);
-            if (offset != -1) {
-                textInput.setSelection(offset, 0);
+            anchor = getInsertionPoint(x);
+
+            if (anchor != -1) {
+                if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
+                    // Select the range
+                    int selectionStart = textInput.getSelectionStart();
+
+                    if (anchor > selectionStart) {
+                        textInput.setSelection(selectionStart, anchor - selectionStart);
+                    } else {
+                        textInput.setSelection(anchor, selectionStart - anchor);
+                    }
+                } else {
+                    // Move the caret to the insertion point
+                    textInput.setSelection(anchor, 0);
+                    consumed = true;
+                }
             }
 
+
             // Set focus to the text input
             textInput.requestFocus();
         }
 
-        return super.mouseDown(component, button, x, y);
+        return consumed;
     }
 
     @Override
@@ -972,6 +1002,8 @@
             Mouse.release();
         }
 
+        anchor = -1;
+
         return consumed;
     }