Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions src/main/java/com/googlecode/lanterna/gui2/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.googlecode.lanterna.TerminalTextUtils;
import com.googlecode.lanterna.graphics.ThemeDefinition;
import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType;
import com.googlecode.lanterna.input.MouseAction;
import com.googlecode.lanterna.input.MouseActionType;

Expand Down Expand Up @@ -659,26 +660,23 @@ else if(verticalFocusSwitching) {
MouseAction mouseAction = (MouseAction) keyStroke;
MouseActionType actionType = mouseAction.getActionType();
if (actionType == MouseActionType.SCROLL_UP) {
if (canMoveCaretUp()) {
performMoveCaretUp();
}
return handleKeyStrokeReadOnly(new KeyStroke(KeyType.ARROW_UP));
} else if (actionType == MouseActionType.SCROLL_DOWN) {
if (canMoveCaretDown()) {
performMoveCaretDown();
}
} else {
TerminalPosition offset = getRenderer().getViewTopLeft();
int newCaretPositionColumn = mouseAction.getPosition().getColumn() - getGlobalPosition().getColumn() + offset.getColumn();
int newCaretPositionRow = mouseAction.getPosition().getRow() - getGlobalPosition().getRow() + offset.getRow();
if (newCaretPositionRow >= 0 && newCaretPositionRow < lines.size()) {
String newActiveLine = lines.get(newCaretPositionRow);
int minPositionAttempt = 0;
int maxPositionAttempt = newActiveLine.length();
newCaretPositionColumn = Math.max(minPositionAttempt, Math.min(newCaretPositionColumn, maxPositionAttempt));

caretPosition = caretPosition.with(new TerminalPosition(newCaretPositionColumn, newCaretPositionRow));
}
return handleKeyStrokeReadOnly(new KeyStroke(KeyType.ARROW_DOWN));
}

TerminalPosition offset = getRenderer().getViewTopLeft();
int newCaretPositionColumn = mouseAction.getPosition().getColumn() - getGlobalPosition().getColumn() + offset.getColumn();
int newCaretPositionRow = mouseAction.getPosition().getRow() - getGlobalPosition().getRow() + offset.getRow();
if (newCaretPositionRow >= 0 && newCaretPositionRow < lines.size()) {
String newActiveLine = lines.get(newCaretPositionRow);
int minPositionAttempt = 0;
int maxPositionAttempt = newActiveLine.length();
newCaretPositionColumn = Math.max(minPositionAttempt, Math.min(newCaretPositionColumn, maxPositionAttempt));

caretPosition = caretPosition.with(new TerminalPosition(newCaretPositionColumn, newCaretPositionRow));
}

result = Result.HANDLED;
break;
}
Expand Down Expand Up @@ -760,6 +758,19 @@ private Result handleKeyStrokeReadOnly(KeyStroke keyStroke) {
case PAGE_UP:
getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeRow(-getSize().getRows()));
return Result.HANDLED;
case MOUSE_EVENT:
// Not focused
if (!isFocused()) { break; }

// Only handle Scrollup or down actions
MouseAction mouseAction = (MouseAction) keyStroke;
MouseActionType actionType = mouseAction.getActionType();
if (actionType == MouseActionType.SCROLL_UP) {
return handleKeyStrokeReadOnly(new KeyStroke(KeyType.ARROW_UP));
} else if (actionType == MouseActionType.SCROLL_DOWN) {
return handleKeyStrokeReadOnly(new KeyStroke(KeyType.ARROW_DOWN));
}
return Result.HANDLED;
default:
}
return super.handleKeyStroke(keyStroke);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/googlecode/lanterna/gui2/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.googlecode.lanterna.TestTerminalFactory;
import com.googlecode.lanterna.bundle.LanternaThemes;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.terminal.MouseCaptureMode;

import java.io.IOException;

Expand All @@ -30,7 +31,7 @@
*/
public abstract class TestBase {
void run(String[] args) throws IOException, InterruptedException {
Screen screen = new TestTerminalFactory(args).createScreen();
Screen screen = new TestTerminalFactory(args).setMouseCaptureMode(MouseCaptureMode.CLICK_AUTODETECT).createScreen();
screen.startScreen();
MultiWindowTextGUI textGUI = createTextGUI(screen);
String theme = extractTheme(args);
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/com/googlecode/lanterna/gui2/TextBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void init(WindowBasedTextGUI textGUI) {
leftPanel.addComponent(new TextBox("Some text").withBorder(Borders.singleLine("With init")));
leftPanel.addComponent(new TextBox(new TerminalSize(10, 1), "Here is some text that is too long to fit in the text box").withBorder(Borders.singleLine("Long text")));
leftPanel.addComponent(new TextBox("password").setMask('*').withBorder(Borders.singleLine("Password")));

rightPanel.addComponent(new TextBox(new TerminalSize(15, 5),
"This is a normal TextBox. The mouse handling should work when focused.\n" +
"Well here we are again\n" +
"It's always such a pleasure\n" +
"Remember when you tried\n" +
Expand All @@ -50,7 +50,18 @@ public void init(WindowBasedTextGUI textGUI) {
"私は笑っていませんが\n" +
"状況を振り返ると\n" +
"自分のやさしさに驚くほどです").withBorder(Borders.singleLine()));

rightPanel.addComponent(new TextBox(new TerminalSize(15, 5),
"This is a read-only TextBox. The mouse handling should continue to work when focused.\n" +
"Well here we are again\n" +
"It's always such a pleasure\n" +
"Remember when you tried\n" +
"to kill me twice?\n" +
"\n" +
"あのときは笑いが止まりませんでしたね\n" +
"私は笑っていませんが\n" +
"状況を振り返ると\n" +
"自分のやさしさに驚くほどです").setReadOnly(true).withBorder(Borders.singleLine()));

mainPanel.addComponent(leftPanel.withBorder(Borders.singleLine("Single line")));
mainPanel.addComponent(rightPanel.withBorder(Borders.singleLine("Multiline")));

Expand Down