TEditor 80% complete
[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
71a389c9
KL
31import java.io.IOException;
32
12b55d76
KL
33import jexer.bits.CellAttributes;
34import jexer.event.TKeypressEvent;
35import jexer.event.TMouseEvent;
e8a11f98 36import jexer.event.TResizeEvent;
12b55d76
KL
37import jexer.teditor.Document;
38import jexer.teditor.Line;
39import jexer.teditor.Word;
40import static jexer.TKeypress.*;
41
42/**
43 * TEditorWidget displays an editable text document. It is unaware of
44 * scrolling behavior, but can respond to mouse and keyboard events.
45 */
46public final class TEditorWidget extends TWidget {
47
48 /**
49 * The document being edited.
50 */
51 private Document document;
52
e8a11f98
KL
53 /**
54 * The default color for the TEditor class.
55 */
56 private CellAttributes defaultColor = null;
57
58 /**
59 * The topmost line number in the visible area. 0-based.
60 */
61 private int topLine = 0;
62
63 /**
64 * The leftmost column number in the visible area. 0-based.
65 */
66 private int leftColumn = 0;
67
12b55d76
KL
68 /**
69 * Public constructor.
70 *
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
77 */
78 public TEditorWidget(final TWidget parent, final String text, final int x,
79 final int y, final int width, final int height) {
80
81 // Set parent and window
82 super(parent, x, y, width, height);
83
84 setCursorVisible(true);
e8a11f98
KL
85
86 defaultColor = getTheme().getColor("teditor");
87 document = new Document(text, defaultColor);
12b55d76
KL
88 }
89
90 /**
91 * Draw the text box.
92 */
93 @Override
94 public void draw() {
12b55d76
KL
95 for (int i = 0; i < getHeight(); i++) {
96 // Background line
e8a11f98 97 getScreen().hLineXY(0, i, getWidth(), ' ', defaultColor);
12b55d76
KL
98
99 // Now draw document's line
e8a11f98
KL
100 if (topLine + i < document.getLineCount()) {
101 Line line = document.getLine(topLine + i);
12b55d76
KL
102 int x = 0;
103 for (Word word: line.getWords()) {
e8a11f98
KL
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(),
12b55d76
KL
107 word.getColor());
108 x += word.getDisplayLength();
e8a11f98 109 if (x - leftColumn > getWidth()) {
12b55d76
KL
110 break;
111 }
112 }
113 }
114 }
115
116 }
117
118 /**
119 * Handle mouse press events.
120 *
121 * @param mouse mouse button press event
122 */
123 @Override
124 public void onMouseDown(final TMouseEvent mouse) {
125 if (mouse.isMouseWheelUp()) {
71a389c9
KL
126 if (topLine > 0) {
127 topLine--;
128 alignDocument(false);
e8a11f98 129 }
12b55d76
KL
130 return;
131 }
132 if (mouse.isMouseWheelDown()) {
71a389c9
KL
133 if (topLine < document.getLineCount() - 1) {
134 topLine++;
135 alignDocument(true);
e8a11f98 136 }
12b55d76
KL
137 return;
138 }
139
e8a11f98
KL
140 if (mouse.isMouse1()) {
141 // Set the row and column
142 int newLine = topLine + mouse.getY();
143 int newX = leftColumn + mouse.getX();
144 if (newLine > document.getLineCount()) {
145 // Go to the end
146 document.setLineNumber(document.getLineCount() - 1);
147 document.end();
148 if (document.getLineCount() > getHeight()) {
149 setCursorY(getHeight() - 1);
150 } else {
151 setCursorY(document.getLineCount() - 1);
152 }
153 alignCursor();
154 return;
155 }
156
157 document.setLineNumber(newLine);
158 setCursorY(mouse.getY());
159 if (newX > document.getCurrentLine().getDisplayLength()) {
160 document.end();
161 alignCursor();
162 } else {
163 setCursorX(mouse.getX());
164 }
165 return;
166 }
12b55d76
KL
167
168 // Pass to children
169 super.onMouseDown(mouse);
170 }
171
71a389c9
KL
172 /**
173 * Align visible area with document current line.
174 *
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.
178 */
179 private void alignTopLine(final boolean topLineIsTop) {
180 int line = document.getLineNumber();
181
182 if ((line < topLine) || (line > topLine + getHeight() - 1)) {
183 // Need to move topLine to bring document back into view.
184 if (topLineIsTop) {
185 topLine = line - (getHeight() - 1);
186 } else {
187 topLine = line;
188 }
189 }
190
191 /*
192 System.err.println("line " + line + " topLine " + topLine);
193 */
194
195 // Document is in view, let's set cursorY
196 setCursorY(line - topLine);
197 alignCursor();
198 }
199
200 /**
201 * Align document current line with visible area.
202 *
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.
206 */
207 private void alignDocument(final boolean topLineIsTop) {
208 int line = document.getLineNumber();
209
210 if ((line < topLine) || (line > topLine + getHeight() - 1)) {
211 // Need to move document to ensure it fits view.
212 if (topLineIsTop) {
213 document.setLineNumber(topLine);
214 } else {
215 document.setLineNumber(topLine + (getHeight() - 1));
216 }
217 }
218
219 /*
220 System.err.println("getLineNumber() " + document.getLineNumber() +
221 " topLine " + topLine);
222 */
223
224 // Document is in view, let's set cursorY
225 setCursorY(document.getLineNumber() - topLine);
226 alignCursor();
227 }
228
e8a11f98
KL
229 /**
230 * Align visible cursor with document cursor.
231 */
232 private void alignCursor() {
233 int width = getWidth();
234
235 int desiredX = document.getCursor() - leftColumn;
236 if (desiredX < 0) {
237 // We need to push the screen to the left.
238 leftColumn = document.getCursor();
239 } else if (desiredX > width - 1) {
240 // We need to push the screen to the right.
241 leftColumn = document.getCursor() - (width - 1);
242 }
243
244 /*
245 System.err.println("document cursor " + document.getCursor() +
246 " leftColumn " + leftColumn);
247 */
248
249 setCursorX(document.getCursor() - leftColumn);
250 }
251
12b55d76
KL
252 /**
253 * Handle keystrokes.
254 *
255 * @param keypress keystroke event
256 */
257 @Override
258 public void onKeypress(final TKeypressEvent keypress) {
259 if (keypress.equals(kbLeft)) {
e8a11f98
KL
260 if (document.left()) {
261 alignCursor();
262 }
12b55d76 263 } else if (keypress.equals(kbRight)) {
e8a11f98
KL
264 if (document.right()) {
265 alignCursor();
266 }
12b55d76 267 } else if (keypress.equals(kbUp)) {
71a389c9
KL
268 document.up();
269 alignTopLine(false);
12b55d76 270 } else if (keypress.equals(kbDown)) {
71a389c9
KL
271 document.down();
272 alignTopLine(true);
12b55d76 273 } else if (keypress.equals(kbPgUp)) {
71a389c9
KL
274 document.up(getHeight() - 1);
275 alignTopLine(false);
12b55d76 276 } else if (keypress.equals(kbPgDn)) {
71a389c9
KL
277 document.down(getHeight() - 1);
278 alignTopLine(true);
12b55d76 279 } else if (keypress.equals(kbHome)) {
e8a11f98
KL
280 if (document.home()) {
281 leftColumn = 0;
282 if (leftColumn < 0) {
283 leftColumn = 0;
284 }
285 setCursorX(0);
286 }
12b55d76 287 } else if (keypress.equals(kbEnd)) {
e8a11f98
KL
288 if (document.end()) {
289 alignCursor();
290 }
12b55d76
KL
291 } else if (keypress.equals(kbCtrlHome)) {
292 document.setLineNumber(0);
293 document.home();
e8a11f98
KL
294 topLine = 0;
295 leftColumn = 0;
296 setCursorX(0);
297 setCursorY(0);
12b55d76
KL
298 } else if (keypress.equals(kbCtrlEnd)) {
299 document.setLineNumber(document.getLineCount() - 1);
300 document.end();
71a389c9 301 alignTopLine(false);
12b55d76
KL
302 } else if (keypress.equals(kbIns)) {
303 document.setOverwrite(!document.getOverwrite());
304 } else if (keypress.equals(kbDel)) {
71a389c9 305 // TODO: join lines
12b55d76 306 document.del();
71a389c9 307 alignCursor();
12b55d76
KL
308 } else if (keypress.equals(kbBackspace)) {
309 document.backspace();
e8a11f98 310 alignCursor();
71a389c9
KL
311 } else if (keypress.equals(kbEnter)) {
312 // TODO: split lines
12b55d76
KL
313 } else if (!keypress.getKey().isFnKey()
314 && !keypress.getKey().isAlt()
315 && !keypress.getKey().isCtrl()
316 ) {
317 // Plain old keystroke, process it
318 document.addChar(keypress.getKey().getChar());
71a389c9 319 alignCursor();
12b55d76
KL
320 } else {
321 // Pass other keys (tab etc.) on to TWidget
322 super.onKeypress(keypress);
323 }
324 }
325
e8a11f98
KL
326 /**
327 * Method that subclasses can override to handle window/screen resize
328 * events.
329 *
330 * @param resize resize event
331 */
332 @Override
333 public void onResize(final TResizeEvent resize) {
334 // Change my width/height, and pull the cursor in as needed.
335 if (resize.getType() == TResizeEvent.Type.WIDGET) {
336 setWidth(resize.getWidth());
337 setHeight(resize.getHeight());
338 // See if the cursor is now outside the window, and if so move
339 // things.
340 if (getCursorX() >= getWidth()) {
341 leftColumn += getCursorX() - (getWidth() - 1);
342 setCursorX(getWidth() - 1);
343 }
344 if (getCursorY() >= getHeight()) {
345 topLine += getCursorY() - (getHeight() - 1);
346 setCursorY(getHeight() - 1);
347 }
348 } else {
349 // Let superclass handle it
350 super.onResize(resize);
351 }
352 }
353
71a389c9
KL
354 /**
355 * Get the number of lines in the underlying Document.
356 *
357 * @return the number of lines
358 */
359 public int getLineCount() {
360 return document.getLineCount();
361 }
362
363 /**
364 * Get the current editing row number. 1-based.
365 *
366 * @return the editing row number. Row 1 is the first row.
367 */
368 public int getEditingRowNumber() {
369 return document.getLineNumber() + 1;
370 }
371
372 /**
373 * Set the current editing row number. 1-based.
374 *
375 * @param row the new editing row number. Row 1 is the first row.
376 */
377 public void setEditingRowNumber(final int row) {
378 document.setLineNumber(row - 1);
379 }
380
381 /**
382 * Get the current editing column number. 1-based.
383 *
384 * @return the editing column number. Column 1 is the first column.
385 */
386 public int getEditingColumnNumber() {
387 return document.getCursor() + 1;
388 }
389
390 /**
391 * Set the current editing column number. 1-based.
392 *
393 * @param column the new editing column number. Column 1 is the first
394 * column.
395 */
396 public void setEditingColumnNumber(final int column) {
397 document.setCursor(column - 1);
398 }
399
400 /**
401 * Get the maximum possible row number. 1-based.
402 *
403 * @return the maximum row number. Row 1 is the first row.
404 */
405 public int getMaximumRowNumber() {
406 return document.getLineCount() + 1;
407 }
408
409 /**
410 * Get the maximum possible column number. 1-based.
411 *
412 * @return the maximum column number. Column 1 is the first column.
413 */
414 public int getMaximumColumnNumber() {
415 return document.getLineLengthMax() + 1;
416 }
417
418 /**
419 * Get the dirty value.
420 *
421 * @return true if the buffer is dirty
422 */
423 public boolean isDirty() {
424 return document.isDirty();
425 }
426
427 /**
428 * Save contents to file.
429 *
430 * @param filename file to save to
431 * @throws IOException if a java.io operation throws
432 */
433 public void saveToFilename(final String filename) throws IOException {
434 document.saveToFilename(filename);
435 }
436
12b55d76 437}