From 24489803a611e99348e26cadedae1141f48c1a6c Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Thu, 17 Aug 2017 11:10:47 -0400 Subject: [PATCH] #19 TField fixes --- src/jexer/TField.java | 23 +++++++++++++++++- src/jexer/TWidget.java | 19 +++++++++++++++ src/jexer/backend/LogicalScreen.java | 30 ++++++++++++++++++++++++ src/jexer/demos/DemoTextFieldWindow.java | 5 ++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/jexer/TField.java b/src/jexer/TField.java index fbe726b..785a276 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -218,7 +218,8 @@ public class TField extends TWidget { } /** - * Update the cursor position. + * Update the visible cursor position to match the location of position + * and windowStart. */ protected void updateCursor() { if ((position > getWidth()) && fixed) { @@ -230,6 +231,23 @@ public class TField extends TWidget { } } + /** + * Normalize windowStart such that most of the field data if visible. + */ + protected void normalizeWindowStart() { + if (fixed) { + // windowStart had better be zero, there is nothing to do here. + assert (windowStart == 0); + return; + } + windowStart = position - (getWidth() - 1); + if (windowStart < 0) { + windowStart = 0; + } + + updateCursor(); + } + /** * Handle mouse button presses. * @@ -268,6 +286,7 @@ public class TField extends TWidget { windowStart--; } } + normalizeWindowStart(); return; } @@ -322,6 +341,7 @@ public class TField extends TWidget { text = text.substring(0, position) + text.substring(position + 1); } + dispatch(false); return; } @@ -339,6 +359,7 @@ public class TField extends TWidget { } } dispatch(false); + normalizeWindowStart(); return; } diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index d292ec1..22766dd 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -302,6 +302,25 @@ public abstract class TWidget implements Comparable { * @return if true, this widget has a visible cursor */ public final boolean isCursorVisible() { + // If cursor is out of my bounds, it is not visible. + if ((cursorX >= width) + || (cursorX < 0) + || (cursorY >= height) + || (cursorY < 0) + ) { + return false; + } + + // If cursor is out of my window's bounds, it is not visible. + if ((getCursorAbsoluteX() >= window.getAbsoluteX() + + window.getWidth() - 1) + || (getCursorAbsoluteX() < 0) + || (getCursorAbsoluteY() >= window.getAbsoluteY() + + window.getHeight() - 1) + || (getCursorAbsoluteY() < 0) + ) { + return false; + } return cursorVisible; } diff --git a/src/jexer/backend/LogicalScreen.java b/src/jexer/backend/LogicalScreen.java index 4e7971e..c9b025a 100644 --- a/src/jexer/backend/LogicalScreen.java +++ b/src/jexer/backend/LogicalScreen.java @@ -293,6 +293,16 @@ public class LogicalScreen implements Screen { if ((X >= 0) && (X < width) && (Y >= 0) && (Y < height)) { logical[X][Y].setTo(attr); + + // If this happens to be the cursor position, make the position + // dirty. + if ((cursorX == X) && (cursorY == Y)) { + if (physical[cursorX][cursorY].getChar() == 'Q') { + physical[cursorX][cursorY].setChar('X'); + } else { + physical[cursorX][cursorY].setChar('Q'); + } + } } } @@ -354,6 +364,16 @@ public class LogicalScreen implements Screen { logical[X][Y].setTo(attr); logical[X][Y].setChar(ch); + + // If this happens to be the cursor position, make the position + // dirty. + if ((cursorX == X) && (cursorY == Y)) { + if (physical[cursorX][cursorY].getChar() == 'Q') { + physical[cursorX][cursorY].setChar('X'); + } else { + physical[cursorX][cursorY].setChar('Q'); + } + } } } @@ -381,6 +401,16 @@ public class LogicalScreen implements Screen { if ((X >= 0) && (X < width) && (Y >= 0) && (Y < height)) { logical[X][Y].setChar(ch); + + // If this happens to be the cursor position, make the position + // dirty. + if ((cursorX == X) && (cursorY == Y)) { + if (physical[cursorX][cursorY].getChar() == 'Q') { + physical[cursorX][cursorY].setChar('X'); + } else { + physical[cursorX][cursorY].setChar('Q'); + } + } } } diff --git a/src/jexer/demos/DemoTextFieldWindow.java b/src/jexer/demos/DemoTextFieldWindow.java index fcdf461..51656ba 100644 --- a/src/jexer/demos/DemoTextFieldWindow.java +++ b/src/jexer/demos/DemoTextFieldWindow.java @@ -67,6 +67,9 @@ public class DemoTextFieldWindow extends TWindow { addPasswordField(35, row++, 15, false); addLabel("Fixed-width password:", 1, row); addPasswordField(35, row++, 15, true, "hunter2"); + addLabel("Very long text field:", 1, row); + TField selected = addField(35, row++, 40, false, + "Very very long field text that should be outside the window"); row += 1; addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4, @@ -77,6 +80,8 @@ public class DemoTextFieldWindow extends TWindow { } ); + activate(selected); + statusBar = newStatusBar("Text fields"); statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); statusBar.addShortcutKeypress(kbF2, cmShell, "Shell"); -- 2.27.0