6fed1fc818f05356f02e34b88dc7f9bd429e20e9
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 jexer
.bits
.CellAttributes
;
32 import jexer
.event
.TKeypressEvent
;
33 import jexer
.event
.TMouseEvent
;
34 import jexer
.teditor
.Document
;
35 import jexer
.teditor
.Line
;
36 import jexer
.teditor
.Word
;
37 import static jexer
.TKeypress
.*;
40 * TEditorWidget displays an editable text document. It is unaware of
41 * scrolling behavior, but can respond to mouse and keyboard events.
43 public final class TEditorWidget
extends TWidget
{
46 * The document being edited.
48 private Document document
;
53 * @param parent parent widget
54 * @param text text on the screen
55 * @param x column relative to parent
56 * @param y row relative to parent
57 * @param width width of text area
58 * @param height height of text area
60 public TEditorWidget(final TWidget parent
, final String text
, final int x
,
61 final int y
, final int width
, final int height
) {
63 // Set parent and window
64 super(parent
, x
, y
, width
, height
);
66 setCursorVisible(true);
67 document
= new Document(text
);
76 CellAttributes color
= getTheme().getColor("teditor");
78 int lineNumber
= document
.getLineNumber();
79 for (int i
= 0; i
< getHeight(); i
++) {
81 getScreen().hLineXY(0, i
, getWidth(), ' ', color
);
83 // Now draw document's line
84 if (lineNumber
+ i
< document
.getLineCount()) {
85 Line line
= document
.getLine(lineNumber
+ i
);
87 for (Word word
: line
.getWords()) {
88 getScreen().putStringXY(x
, i
, word
.getText(),
90 x
+= word
.getDisplayLength();
101 * Handle mouse press events.
103 * @param mouse mouse button press event
106 public void onMouseDown(final TMouseEvent mouse
) {
107 if (mouse
.isMouseWheelUp()) {
111 if (mouse
.isMouseWheelDown()) {
116 // TODO: click sets row and column
119 super.onMouseDown(mouse
);
125 * @param keypress keystroke event
128 public void onKeypress(final TKeypressEvent keypress
) {
129 if (keypress
.equals(kbLeft
)) {
131 } else if (keypress
.equals(kbRight
)) {
133 } else if (keypress
.equals(kbUp
)) {
135 } else if (keypress
.equals(kbDown
)) {
137 } else if (keypress
.equals(kbPgUp
)) {
138 document
.up(getHeight() - 1);
139 } else if (keypress
.equals(kbPgDn
)) {
140 document
.down(getHeight() - 1);
141 } else if (keypress
.equals(kbHome
)) {
143 } else if (keypress
.equals(kbEnd
)) {
145 } else if (keypress
.equals(kbCtrlHome
)) {
146 document
.setLineNumber(0);
148 } else if (keypress
.equals(kbCtrlEnd
)) {
149 document
.setLineNumber(document
.getLineCount() - 1);
151 } else if (keypress
.equals(kbIns
)) {
152 document
.setOverwrite(!document
.getOverwrite());
153 } else if (keypress
.equals(kbDel
)) {
155 } else if (keypress
.equals(kbBackspace
)) {
156 document
.backspace();
157 } else if (!keypress
.getKey().isFnKey()
158 && !keypress
.getKey().isAlt()
159 && !keypress
.getKey().isCtrl()
161 // Plain old keystroke, process it
162 document
.addChar(keypress
.getKey().getChar());
164 // Pass other keys (tab etc.) on to TWidget
165 super.onKeypress(keypress
);