Permalink
Browse files

PIVOT-891 (more): Fix the up/down selection logic in TextArea to do t…

…he "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.

This is a merge of revision 1460982 from branches/2.0.x to trunk.



git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1461214 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 13b3157 commit e2a34cbb4d95c29063a07de49f8cb5277e008858 Roger Lee Whitcomb committed Mar 26, 2013
Showing with 89 additions and 30 deletions.
  1. +89 −30 wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
@@ -364,7 +364,6 @@ public int getInsertionPoint(int x, int y) {
@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 boolean mouseClick(Component component, Mouse.Button button,
textArea.setSelection(textArea.getRowOffset(index), textArea.getRowLength(index));
}
}
- }
+ }
return consumed;
}
@@ -1330,17 +1329,48 @@ public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation
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 boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation
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;
-
- Bounds trailingSelectionBounds = getCharacterBounds(from);
- x = trailingSelectionBounds.x + trailingSelectionBounds.width;
- }
-
- int index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN);
+ int 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++;
+ 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;
+ }
+
+ 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++;
+ }
+
+ textArea.setSelection(selectionStart, index - selectionStart);
+ scrollCharacterToVisible(index);
+ }
}
-
- textArea.setSelection(selectionStart, index - selectionStart);
- scrollCharacterToVisible(index);
}
} else {
int from;
@@ -1392,7 +1449,9 @@ public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation
if (index != -1) {
textArea.setSelection(index, 0);
scrollCharacterToVisible(index);
+ caretX = caret.x;
}
+ anchor = -1;
}
consumed = true;

0 comments on commit e2a34cb

Please sign in to comment.