more TEditor stubs
[fanfix.git] / src / jexer / TEditorWidget.java
CommitLineData
12b55d76
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
31import jexer.bits.CellAttributes;
32import jexer.event.TKeypressEvent;
33import jexer.event.TMouseEvent;
34import jexer.teditor.Document;
35import jexer.teditor.Line;
36import jexer.teditor.Word;
37import static jexer.TKeypress.*;
38
39/**
40 * TEditorWidget displays an editable text document. It is unaware of
41 * scrolling behavior, but can respond to mouse and keyboard events.
42 */
43public final class TEditorWidget extends TWidget {
44
45 /**
46 * The document being edited.
47 */
48 private Document document;
49
50 /**
51 * Public constructor.
52 *
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
59 */
60 public TEditorWidget(final TWidget parent, final String text, final int x,
61 final int y, final int width, final int height) {
62
63 // Set parent and window
64 super(parent, x, y, width, height);
65
66 setCursorVisible(true);
67 document = new Document(text);
68 }
69
70 /**
71 * Draw the text box.
72 */
73 @Override
74 public void draw() {
75 // Setup my color
76 CellAttributes color = getTheme().getColor("teditor");
77
78 int lineNumber = document.getLineNumber();
79 for (int i = 0; i < getHeight(); i++) {
80 // Background line
81 getScreen().hLineXY(0, i, getWidth(), ' ', color);
82
83 // Now draw document's line
84 if (lineNumber + i < document.getLineCount()) {
85 Line line = document.getLine(lineNumber + i);
86 int x = 0;
87 for (Word word: line.getWords()) {
88 getScreen().putStringXY(x, i, word.getText(),
89 word.getColor());
90 x += word.getDisplayLength();
91 if (x > getWidth()) {
92 break;
93 }
94 }
95 }
96 }
97
98 }
99
100 /**
101 * Handle mouse press events.
102 *
103 * @param mouse mouse button press event
104 */
105 @Override
106 public void onMouseDown(final TMouseEvent mouse) {
107 if (mouse.isMouseWheelUp()) {
108 document.up();
109 return;
110 }
111 if (mouse.isMouseWheelDown()) {
112 document.down();
113 return;
114 }
115
116 // TODO: click sets row and column
117
118 // Pass to children
119 super.onMouseDown(mouse);
120 }
121
122 /**
123 * Handle keystrokes.
124 *
125 * @param keypress keystroke event
126 */
127 @Override
128 public void onKeypress(final TKeypressEvent keypress) {
129 if (keypress.equals(kbLeft)) {
130 document.left();
131 } else if (keypress.equals(kbRight)) {
132 document.right();
133 } else if (keypress.equals(kbUp)) {
134 document.up();
135 } else if (keypress.equals(kbDown)) {
136 document.down();
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)) {
142 document.home();
143 } else if (keypress.equals(kbEnd)) {
144 document.end();
145 } else if (keypress.equals(kbCtrlHome)) {
146 document.setLineNumber(0);
147 document.home();
148 } else if (keypress.equals(kbCtrlEnd)) {
149 document.setLineNumber(document.getLineCount() - 1);
150 document.end();
151 } else if (keypress.equals(kbIns)) {
152 document.setOverwrite(!document.getOverwrite());
153 } else if (keypress.equals(kbDel)) {
154 document.del();
155 } else if (keypress.equals(kbBackspace)) {
156 document.backspace();
157 } else if (!keypress.getKey().isFnKey()
158 && !keypress.getKey().isAlt()
159 && !keypress.getKey().isCtrl()
160 ) {
161 // Plain old keystroke, process it
162 document.addChar(keypress.getKey().getChar());
163 } else {
164 // Pass other keys (tab etc.) on to TWidget
165 super.onKeypress(keypress);
166 }
167 }
168
169}