2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.io
.IOException
;
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.event
.TKeypressEvent
;
35 import jexer
.event
.TMouseEvent
;
36 import jexer
.event
.TResizeEvent
;
37 import jexer
.teditor
.Document
;
38 import jexer
.teditor
.Line
;
39 import jexer
.teditor
.Word
;
40 import static jexer
.TKeypress
.*;
43 * TEditorWidget displays an editable text document. It is unaware of
44 * scrolling behavior, but can respond to mouse and keyboard events.
46 public class TEditorWidget
extends TWidget
{
49 * The number of lines to scroll on mouse wheel up/down.
51 private static final int wheelScrollSize
= 3;
54 * The document being edited.
56 private Document document
;
59 * The default color for the TEditor class.
61 private CellAttributes defaultColor
= null;
64 * The topmost line number in the visible area. 0-based.
66 private int topLine
= 0;
69 * The leftmost column number in the visible area. 0-based.
71 private int leftColumn
= 0;
76 * @param parent parent widget
77 * @param text text on the screen
78 * @param x column relative to parent
79 * @param y row relative to parent
80 * @param width width of text area
81 * @param height height of text area
83 public TEditorWidget(final TWidget parent
, final String text
, final int x
,
84 final int y
, final int width
, final int height
) {
86 // Set parent and window
87 super(parent
, x
, y
, width
, height
);
89 setCursorVisible(true);
91 defaultColor
= getTheme().getColor("teditor");
92 document
= new Document(text
, defaultColor
);
100 for (int i
= 0; i
< getHeight(); i
++) {
102 getScreen().hLineXY(0, i
, getWidth(), ' ', defaultColor
);
104 // Now draw document's line
105 if (topLine
+ i
< document
.getLineCount()) {
106 Line line
= document
.getLine(topLine
+ i
);
108 for (Word word
: line
.getWords()) {
109 // For now, we are cheating: draw outside the left region
110 // if needed and let screen do the clipping.
111 getScreen().putStringXY(x
- leftColumn
, i
, word
.getText(),
113 x
+= word
.getDisplayLength();
114 if (x
- leftColumn
> getWidth()) {
123 * Handle mouse press events.
125 * @param mouse mouse button press event
128 public void onMouseDown(final TMouseEvent mouse
) {
129 if (mouse
.isMouseWheelUp()) {
130 for (int i
= 0; i
< wheelScrollSize
; i
++) {
133 alignDocument(false);
138 if (mouse
.isMouseWheelDown()) {
139 for (int i
= 0; i
< wheelScrollSize
; i
++) {
140 if (topLine
< document
.getLineCount() - 1) {
148 if (mouse
.isMouse1()) {
149 // Set the row and column
150 int newLine
= topLine
+ mouse
.getY();
151 int newX
= leftColumn
+ mouse
.getX();
152 if (newLine
> document
.getLineCount() - 1) {
154 document
.setLineNumber(document
.getLineCount() - 1);
156 if (newLine
> document
.getLineCount() - 1) {
157 setCursorY(document
.getLineCount() - 1 - topLine
);
159 setCursorY(mouse
.getY());
165 document
.setLineNumber(newLine
);
166 setCursorY(mouse
.getY());
167 if (newX
>= document
.getCurrentLine().getDisplayLength()) {
171 document
.setCursor(newX
);
172 setCursorX(mouse
.getX());
178 super.onMouseDown(mouse
);
182 * Align visible area with document current line.
184 * @param topLineIsTop if true, make the top visible line the document
185 * current line if it was off-screen. If false, make the bottom visible
186 * line the document current line.
188 private void alignTopLine(final boolean topLineIsTop
) {
189 int line
= document
.getLineNumber();
191 if ((line
< topLine
) || (line
> topLine
+ getHeight() - 1)) {
192 // Need to move topLine to bring document back into view.
194 topLine
= line
- (getHeight() - 1);
198 assert (topLine
>= 0);
201 assert (topLine
>= 0);
206 System.err.println("line " + line + " topLine " + topLine);
209 // Document is in view, let's set cursorY
210 assert (line
>= topLine
);
211 setCursorY(line
- topLine
);
216 * Align document current line with visible area.
218 * @param topLineIsTop if true, make the top visible line the document
219 * current line if it was off-screen. If false, make the bottom visible
220 * line the document current line.
222 private void alignDocument(final boolean topLineIsTop
) {
223 int line
= document
.getLineNumber();
224 int cursor
= document
.getCursor();
226 if ((line
< topLine
) || (line
> topLine
+ getHeight() - 1)) {
227 // Need to move document to ensure it fits view.
229 document
.setLineNumber(topLine
);
231 document
.setLineNumber(topLine
+ (getHeight() - 1));
233 if (cursor
< document
.getCurrentLine().getDisplayLength()) {
234 document
.setCursor(cursor
);
239 System.err.println("getLineNumber() " + document.getLineNumber() +
240 " topLine " + topLine);
243 // Document is in view, let's set cursorY
244 setCursorY(document
.getLineNumber() - topLine
);
249 * Align visible cursor with document cursor.
251 private void alignCursor() {
252 int width
= getWidth();
254 int desiredX
= document
.getCursor() - leftColumn
;
256 // We need to push the screen to the left.
257 leftColumn
= document
.getCursor();
258 } else if (desiredX
> width
- 1) {
259 // We need to push the screen to the right.
260 leftColumn
= document
.getCursor() - (width
- 1);
264 System.err.println("document cursor " + document.getCursor() +
265 " leftColumn " + leftColumn);
269 setCursorX(document
.getCursor() - leftColumn
);
275 * @param keypress keystroke event
278 public void onKeypress(final TKeypressEvent keypress
) {
279 if (keypress
.equals(kbLeft
)) {
282 } else if (keypress
.equals(kbRight
)) {
285 } else if (keypress
.equals(kbUp
)) {
288 } else if (keypress
.equals(kbDown
)) {
291 } else if (keypress
.equals(kbPgUp
)) {
292 document
.up(getHeight() - 1);
294 } else if (keypress
.equals(kbPgDn
)) {
295 document
.down(getHeight() - 1);
297 } else if (keypress
.equals(kbHome
)) {
298 if (document
.home()) {
300 if (leftColumn
< 0) {
305 } else if (keypress
.equals(kbEnd
)) {
306 if (document
.end()) {
309 } else if (keypress
.equals(kbCtrlHome
)) {
310 document
.setLineNumber(0);
316 } else if (keypress
.equals(kbCtrlEnd
)) {
317 document
.setLineNumber(document
.getLineCount() - 1);
320 } else if (keypress
.equals(kbIns
)) {
321 document
.setOverwrite(!document
.getOverwrite());
322 } else if (keypress
.equals(kbDel
)) {
325 } else if (keypress
.equals(kbBackspace
)) {
326 document
.backspace();
328 } else if (keypress
.equals(kbTab
)) {
329 // TODO: tab character. For now just add spaces until we hit
331 for (int i
= document
.getCursor(); (i
+ 1) % 8 != 0; i
++) {
332 document
.addChar(' ');
335 } else if (keypress
.equals(kbEnter
)) {
338 } else if (!keypress
.getKey().isFnKey()
339 && !keypress
.getKey().isAlt()
340 && !keypress
.getKey().isCtrl()
342 // Plain old keystroke, process it
343 document
.addChar(keypress
.getKey().getChar());
346 // Pass other keys (tab etc.) on to TWidget
347 super.onKeypress(keypress
);
352 * Method that subclasses can override to handle window/screen resize
355 * @param resize resize event
358 public void onResize(final TResizeEvent resize
) {
359 // Change my width/height, and pull the cursor in as needed.
360 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
361 setWidth(resize
.getWidth());
362 setHeight(resize
.getHeight());
363 // See if the cursor is now outside the window, and if so move
365 if (getCursorX() >= getWidth()) {
366 leftColumn
+= getCursorX() - (getWidth() - 1);
367 setCursorX(getWidth() - 1);
369 if (getCursorY() >= getHeight()) {
370 topLine
+= getCursorY() - (getHeight() - 1);
371 setCursorY(getHeight() - 1);
374 // Let superclass handle it
375 super.onResize(resize
);
380 * Get the number of lines in the underlying Document.
382 * @return the number of lines
384 public int getLineCount() {
385 return document
.getLineCount();
389 * Get the current visible top row number. 1-based.
391 * @return the visible top row number. Row 1 is the first row.
393 public int getVisibleRowNumber() {
398 * Set the current visible row number. 1-based.
400 * @param row the new visible row number. Row 1 is the first row.
402 public void setVisibleRowNumber(final int row
) {
404 if ((row
> 0) && (row
< document
.getLineCount())) {
411 * Get the current editing row number. 1-based.
413 * @return the editing row number. Row 1 is the first row.
415 public int getEditingRowNumber() {
416 return document
.getLineNumber() + 1;
420 * Set the current editing row number. 1-based.
422 * @param row the new editing row number. Row 1 is the first row.
424 public void setEditingRowNumber(final int row
) {
426 if ((row
> 0) && (row
< document
.getLineCount())) {
427 document
.setLineNumber(row
- 1);
433 * Get the current editing column number. 1-based.
435 * @return the editing column number. Column 1 is the first column.
437 public int getEditingColumnNumber() {
438 return document
.getCursor() + 1;
442 * Set the current editing column number. 1-based.
444 * @param column the new editing column number. Column 1 is the first
447 public void setEditingColumnNumber(final int column
) {
448 if ((column
> 0) && (column
< document
.getLineLength())) {
449 document
.setCursor(column
- 1);
455 * Get the maximum possible row number. 1-based.
457 * @return the maximum row number. Row 1 is the first row.
459 public int getMaximumRowNumber() {
460 return document
.getLineCount() + 1;
464 * Get the maximum possible column number. 1-based.
466 * @return the maximum column number. Column 1 is the first column.
468 public int getMaximumColumnNumber() {
469 return document
.getLineLengthMax() + 1;
473 * Get the dirty value.
475 * @return true if the buffer is dirty
477 public boolean isDirty() {
478 return document
.isDirty();
482 * Save contents to file.
484 * @param filename file to save to
485 * @throws IOException if a java.io operation throws
487 public void saveToFilename(final String filename
) throws IOException
{
488 document
.saveToFilename(filename
);