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 final class TEditorWidget
extends TWidget
{
49 * The document being edited.
51 private Document document
;
54 * The default color for the TEditor class.
56 private CellAttributes defaultColor
= null;
59 * The topmost line number in the visible area. 0-based.
61 private int topLine
= 0;
64 * The leftmost column number in the visible area. 0-based.
66 private int leftColumn
= 0;
71 * @param parent parent widget
72 * @param text text on the screen
73 * @param x column relative to parent
74 * @param y row relative to parent
75 * @param width width of text area
76 * @param height height of text area
78 public TEditorWidget(final TWidget parent
, final String text
, final int x
,
79 final int y
, final int width
, final int height
) {
81 // Set parent and window
82 super(parent
, x
, y
, width
, height
);
84 setCursorVisible(true);
86 defaultColor
= getTheme().getColor("teditor");
87 document
= new Document(text
, defaultColor
);
95 for (int i
= 0; i
< getHeight(); i
++) {
97 getScreen().hLineXY(0, i
, getWidth(), ' ', defaultColor
);
99 // Now draw document's line
100 if (topLine
+ i
< document
.getLineCount()) {
101 Line line
= document
.getLine(topLine
+ i
);
103 for (Word word
: line
.getWords()) {
104 // For now, we are cheating: draw outside the left region
105 // if needed and let screen do the clipping.
106 getScreen().putStringXY(x
- leftColumn
, i
, word
.getText(),
108 x
+= word
.getDisplayLength();
109 if (x
- leftColumn
> getWidth()) {
118 * Handle mouse press events.
120 * @param mouse mouse button press event
123 public void onMouseDown(final TMouseEvent mouse
) {
124 if (mouse
.isMouseWheelUp()) {
127 alignDocument(false);
131 if (mouse
.isMouseWheelDown()) {
132 if (topLine
< document
.getLineCount() - 1) {
139 if (mouse
.isMouse1()) {
140 // Set the row and column
141 int newLine
= topLine
+ mouse
.getY();
142 int newX
= leftColumn
+ mouse
.getX();
143 if (newLine
> document
.getLineCount() - 1) {
145 document
.setLineNumber(document
.getLineCount() - 1);
147 if (newLine
> document
.getLineCount() - 1) {
148 setCursorY(document
.getLineCount() - 1 - topLine
);
150 setCursorY(mouse
.getY());
156 document
.setLineNumber(newLine
);
157 setCursorY(mouse
.getY());
158 if (newX
> document
.getCurrentLine().getDisplayLength()) {
162 document
.setCursor(newX
);
163 setCursorX(mouse
.getX());
169 super.onMouseDown(mouse
);
173 * Align visible area with document current line.
175 * @param topLineIsTop if true, make the top visible line the document
176 * current line if it was off-screen. If false, make the bottom visible
177 * line the document current line.
179 private void alignTopLine(final boolean topLineIsTop
) {
180 int line
= document
.getLineNumber();
182 if ((line
< topLine
) || (line
> topLine
+ getHeight() - 1)) {
183 // Need to move topLine to bring document back into view.
185 topLine
= line
- (getHeight() - 1);
192 System.err.println("line " + line + " topLine " + topLine);
195 // Document is in view, let's set cursorY
196 setCursorY(line
- topLine
);
201 * Align document current line with visible area.
203 * @param topLineIsTop if true, make the top visible line the document
204 * current line if it was off-screen. If false, make the bottom visible
205 * line the document current line.
207 private void alignDocument(final boolean topLineIsTop
) {
208 int line
= document
.getLineNumber();
209 int cursor
= document
.getCursor();
211 if ((line
< topLine
) || (line
> topLine
+ getHeight() - 1)) {
212 // Need to move document to ensure it fits view.
214 document
.setLineNumber(topLine
);
216 document
.setLineNumber(topLine
+ (getHeight() - 1));
218 if (cursor
< document
.getCurrentLine().getDisplayLength()) {
219 document
.setCursor(cursor
);
224 System.err.println("getLineNumber() " + document.getLineNumber() +
225 " topLine " + topLine);
228 // Document is in view, let's set cursorY
229 setCursorY(document
.getLineNumber() - topLine
);
234 * Align visible cursor with document cursor.
236 private void alignCursor() {
237 int width
= getWidth();
239 int desiredX
= document
.getCursor() - leftColumn
;
241 // We need to push the screen to the left.
242 leftColumn
= document
.getCursor();
243 } else if (desiredX
> width
- 1) {
244 // We need to push the screen to the right.
245 leftColumn
= document
.getCursor() - (width
- 1);
249 System.err.println("document cursor " + document.getCursor() +
250 " leftColumn " + leftColumn);
254 setCursorX(document
.getCursor() - leftColumn
);
260 * @param keypress keystroke event
263 public void onKeypress(final TKeypressEvent keypress
) {
264 if (keypress
.equals(kbLeft
)) {
267 } else if (keypress
.equals(kbRight
)) {
270 } else if (keypress
.equals(kbUp
)) {
273 } else if (keypress
.equals(kbDown
)) {
276 } else if (keypress
.equals(kbPgUp
)) {
277 document
.up(getHeight() - 1);
279 } else if (keypress
.equals(kbPgDn
)) {
280 document
.down(getHeight() - 1);
282 } else if (keypress
.equals(kbHome
)) {
283 if (document
.home()) {
285 if (leftColumn
< 0) {
290 } else if (keypress
.equals(kbEnd
)) {
291 if (document
.end()) {
294 } else if (keypress
.equals(kbCtrlHome
)) {
295 document
.setLineNumber(0);
301 } else if (keypress
.equals(kbCtrlEnd
)) {
302 document
.setLineNumber(document
.getLineCount() - 1);
305 } else if (keypress
.equals(kbIns
)) {
306 document
.setOverwrite(!document
.getOverwrite());
307 } else if (keypress
.equals(kbDel
)) {
310 } else if (keypress
.equals(kbBackspace
)) {
311 document
.backspace();
313 } else if (keypress
.equals(kbTab
)) {
314 // TODO: tab character. For now just add spaces until we hit
316 for (int i
= document
.getCursor(); (i
+ 1) % 8 != 0; i
++) {
317 document
.addChar(' ');
320 } else if (keypress
.equals(kbEnter
)) {
323 } else if (!keypress
.getKey().isFnKey()
324 && !keypress
.getKey().isAlt()
325 && !keypress
.getKey().isCtrl()
327 // Plain old keystroke, process it
328 document
.addChar(keypress
.getKey().getChar());
331 // Pass other keys (tab etc.) on to TWidget
332 super.onKeypress(keypress
);
337 * Method that subclasses can override to handle window/screen resize
340 * @param resize resize event
343 public void onResize(final TResizeEvent resize
) {
344 // Change my width/height, and pull the cursor in as needed.
345 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
346 setWidth(resize
.getWidth());
347 setHeight(resize
.getHeight());
348 // See if the cursor is now outside the window, and if so move
350 if (getCursorX() >= getWidth()) {
351 leftColumn
+= getCursorX() - (getWidth() - 1);
352 setCursorX(getWidth() - 1);
354 if (getCursorY() >= getHeight()) {
355 topLine
+= getCursorY() - (getHeight() - 1);
356 setCursorY(getHeight() - 1);
359 // Let superclass handle it
360 super.onResize(resize
);
365 * Get the number of lines in the underlying Document.
367 * @return the number of lines
369 public int getLineCount() {
370 return document
.getLineCount();
374 * Get the current editing row number. 1-based.
376 * @return the editing row number. Row 1 is the first row.
378 public int getEditingRowNumber() {
379 return document
.getLineNumber() + 1;
383 * Set the current editing row number. 1-based.
385 * @param row the new editing row number. Row 1 is the first row.
387 public void setEditingRowNumber(final int row
) {
388 document
.setLineNumber(row
- 1);
392 * Get the current editing column number. 1-based.
394 * @return the editing column number. Column 1 is the first column.
396 public int getEditingColumnNumber() {
397 return document
.getCursor() + 1;
401 * Set the current editing column number. 1-based.
403 * @param column the new editing column number. Column 1 is the first
406 public void setEditingColumnNumber(final int column
) {
407 document
.setCursor(column
- 1);
411 * Get the maximum possible row number. 1-based.
413 * @return the maximum row number. Row 1 is the first row.
415 public int getMaximumRowNumber() {
416 return document
.getLineCount() + 1;
420 * Get the maximum possible column number. 1-based.
422 * @return the maximum column number. Column 1 is the first column.
424 public int getMaximumColumnNumber() {
425 return document
.getLineLengthMax() + 1;
429 * Get the dirty value.
431 * @return true if the buffer is dirty
433 public boolean isDirty() {
434 return document
.isDirty();
438 * Save contents to file.
440 * @param filename file to save to
441 * @throws IOException if a java.io operation throws
443 public void saveToFilename(final String filename
) throws IOException
{
444 document
.saveToFilename(filename
);