X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTEditorWidget.java;h=dcf5e9e6d267380d3b541bf275d4cdef67f8dcea;hb=7d922e0dfd9a6da42b84e01d52adeec6fff10025;hp=8d2a0104b4c598aec5de23f06a4e879e4871171a;hpb=df602ccf5e32585c26dc618dd3b4a759b6820943;p=fanfix.git diff --git a/src/jexer/TEditorWidget.java b/src/jexer/TEditorWidget.java index 8d2a010..dcf5e9e 100644 --- a/src/jexer/TEditorWidget.java +++ b/src/jexer/TEditorWidget.java @@ -43,7 +43,12 @@ import static jexer.TKeypress.*; * TEditorWidget displays an editable text document. It is unaware of * scrolling behavior, but can respond to mouse and keyboard events. */ -public final class TEditorWidget extends TWidget { +public class TEditorWidget extends TWidget { + + /** + * The number of lines to scroll on mouse wheel up/down. + */ + private static final int wheelScrollSize = 3; /** * The document being edited. @@ -122,16 +127,20 @@ public final class TEditorWidget extends TWidget { @Override public void onMouseDown(final TMouseEvent mouse) { if (mouse.isMouseWheelUp()) { - if (topLine > 0) { - topLine--; - alignDocument(false); + for (int i = 0; i < wheelScrollSize; i++) { + if (topLine > 0) { + topLine--; + alignDocument(false); + } } return; } if (mouse.isMouseWheelDown()) { - if (topLine < document.getLineCount() - 1) { - topLine++; - alignDocument(true); + for (int i = 0; i < wheelScrollSize; i++) { + if (topLine < document.getLineCount() - 1) { + topLine++; + alignDocument(true); + } } return; } @@ -155,7 +164,7 @@ public final class TEditorWidget extends TWidget { document.setLineNumber(newLine); setCursorY(mouse.getY()); - if (newX > document.getCurrentLine().getDisplayLength()) { + if (newX >= document.getCurrentLine().getDisplayLength()) { document.end(); alignCursor(); } else { @@ -183,8 +192,13 @@ public final class TEditorWidget extends TWidget { // Need to move topLine to bring document back into view. if (topLineIsTop) { topLine = line - (getHeight() - 1); + if (topLine < 0) { + topLine = 0; + } + assert (topLine >= 0); } else { topLine = line; + assert (topLine >= 0); } } @@ -193,6 +207,7 @@ public final class TEditorWidget extends TWidget { */ // Document is in view, let's set cursorY + assert (line >= topLine); setCursorY(line - topLine); alignCursor(); } @@ -370,6 +385,28 @@ public final class TEditorWidget extends TWidget { return document.getLineCount(); } + /** + * Get the current visible top row number. 1-based. + * + * @return the visible top row number. Row 1 is the first row. + */ + public int getVisibleRowNumber() { + return topLine + 1; + } + + /** + * Set the current visible row number. 1-based. + * + * @param row the new visible row number. Row 1 is the first row. + */ + public void setVisibleRowNumber(final int row) { + assert (row > 0); + if ((row > 0) && (row < document.getLineCount())) { + topLine = row - 1; + alignDocument(true); + } + } + /** * Get the current editing row number. 1-based. * @@ -385,7 +422,11 @@ public final class TEditorWidget extends TWidget { * @param row the new editing row number. Row 1 is the first row. */ public void setEditingRowNumber(final int row) { - document.setLineNumber(row - 1); + assert (row > 0); + if ((row > 0) && (row < document.getLineCount())) { + document.setLineNumber(row - 1); + alignTopLine(true); + } } /** @@ -404,7 +445,10 @@ public final class TEditorWidget extends TWidget { * column. */ public void setEditingColumnNumber(final int column) { - document.setCursor(column - 1); + if ((column > 0) && (column < document.getLineLength())) { + document.setCursor(column - 1); + alignCursor(); + } } /**