Author: rwhitcomb Date: Tue Mar 26 06:03:47 2013 New Revision: 1460982 URL: http://svn.apache.org/r1460982 Log: PIVOT-891 (more): Fix the up/down selection logic in TextArea to do the "right" thing by remembering the "anchor" point where the selection started and reducing / enlarging the area depending on whether you are on one side of the anchor or the other. A previous change fixed the left/right logic. Further changes will make these same changes to TextPane.
Probably need to check selection logic in lists and text input fields as well. Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java?rev=1460982&r1=1460981&r2=1460982&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Tue Mar 26 06:03:47 2013 @@ -364,7 +364,6 @@ public class TextAreaSkin extends Compon @Override public int getNextInsertionPoint(int x, int from, TextArea.ScrollDirection direction) { int index = -1; - if (paragraphViews.getLength() > 0) { if (from == -1) { int i = (direction == TextArea.ScrollDirection.DOWN) ? 0 : paragraphViews.getLength() - 1; @@ -1021,7 +1020,7 @@ public class TextAreaSkin extends Compon textArea.setSelection(textArea.getRowOffset(index), textArea.getRowLength(index)); } } - } + } return consumed; } @@ -1330,17 +1329,48 @@ public class TextAreaSkin extends Compon int selectionStart = textArea.getSelectionStart(); int selectionLength = textArea.getSelectionLength(); - int index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP); - - if (index != -1) { - if (shiftPressed) { - selectionLength = selectionStart + selectionLength - index; + int index = -1; + if (shiftPressed) { + if (anchor == -1) { + anchor = selectionStart; + index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP); + if (index != -1) { + selectionLength = selectionStart - index; + } } else { + if (selectionStart < anchor) { + // continue upwards + index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP); + if (index != -1) { + selectionLength = selectionStart + selectionLength - index; + } + } else { + // reduce downward size + Bounds trailingSelectionBounds = getCharacterBounds(selectionStart + selectionLength - 1); + int x = trailingSelectionBounds.x + trailingSelectionBounds.width; + index = getNextInsertionPoint(x, selectionStart + selectionLength - 1, TextArea.ScrollDirection.UP); + if (index != -1) { + if (index < anchor) { + selectionLength = anchor - index; + } else { + selectionLength = index - selectionStart; + index = selectionStart; + } + } + } + } + } else { + index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP); + if (index != -1) { selectionLength = 0; } + anchor = -1; + } + if (index != -1) { textArea.setSelection(index, selectionLength); scrollCharacterToVisible(index); + caretX = caret.x; } consumed = true; @@ -1351,31 +1381,58 @@ public class TextAreaSkin extends Compon if (shiftPressed) { int from; int x; - if (selectionLength == 0) { - // Get next insertion point from leading selection character - from = selectionStart; - x = caretX; - } else { - // Get next insertion point from right edge of trailing selection - // character - from = selectionStart + selectionLength; + int index; - Bounds trailingSelectionBounds = getCharacterBounds(from); - x = trailingSelectionBounds.x + trailingSelectionBounds.width; - } + if (anchor == -1) { + anchor = selectionStart; + index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.DOWN); + if (index != -1) { + selectionLength = index - selectionStart; + } + } else { + if (selectionStart < anchor) { + // Reducing upward size + // Get next insertion point from leading selection character + from = selectionStart; + x = caretX; + + index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN); + + if (index != -1) { + if (index < anchor) { + selectionStart = index; + selectionLength = anchor - index; + } else { + selectionStart = anchor; + selectionLength = index - anchor; + } - int index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN); + textArea.setSelection(selectionStart, selectionLength); + scrollCharacterToVisible(selectionStart); + } + } else { + // Increasing downward size + // Get next insertion point from right edge of trailing selection + // character + from = selectionStart + selectionLength - 1; + + Bounds trailingSelectionBounds = getCharacterBounds(from); + x = trailingSelectionBounds.x + trailingSelectionBounds.width; + + index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN); + + if (index != -1) { + // If the next character is a paragraph terminator and is + // not the final terminator character, increment the selection + if (index < textArea.getCharacterCount() - 1 + && textArea.getCharacterAt(index) == '\n') { + index++; + } - if (index != -1) { - // If the next character is a paragraph terminator and is - // not the final terminator character, increment the selection - if (index < textArea.getCharacterCount() - 1 - && textArea.getCharacterAt(index) == '\n') { - index++; + textArea.setSelection(selectionStart, index - selectionStart); + scrollCharacterToVisible(index); + } } - - textArea.setSelection(selectionStart, index - selectionStart); - scrollCharacterToVisible(index); } } else { int from; @@ -1392,7 +1449,9 @@ public class TextAreaSkin extends Compon if (index != -1) { textArea.setSelection(index, 0); scrollCharacterToVisible(index); + caretX = caret.x; } + anchor = -1; } consumed = true;