#53 progress bar style
[fanfix.git] / src / jexer / TEditorWidget.java
CommitLineData
12b55d76
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
12b55d76
KL
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 */
051e2913 46public class TEditorWidget extends TWidget {
12b55d76 47
615a0d99
KL
48 // ------------------------------------------------------------------------
49 // Constants --------------------------------------------------------------
50 // ------------------------------------------------------------------------
51
b1a8acec
KL
52 /**
53 * The number of lines to scroll on mouse wheel up/down.
54 */
55 private static final int wheelScrollSize = 3;
56
615a0d99
KL
57 // ------------------------------------------------------------------------
58 // Variables --------------------------------------------------------------
59 // ------------------------------------------------------------------------
60
12b55d76
KL
61 /**
62 * The document being edited.
63 */
64 private Document document;
65
e8a11f98
KL
66 /**
67 * The default color for the TEditor class.
68 */
69 private CellAttributes defaultColor = null;
70
71 /**
72 * The topmost line number in the visible area. 0-based.
73 */
74 private int topLine = 0;
75
76 /**
77 * The leftmost column number in the visible area. 0-based.
78 */
79 private int leftColumn = 0;
80
615a0d99
KL
81 // ------------------------------------------------------------------------
82 // Constructors -----------------------------------------------------------
83 // ------------------------------------------------------------------------
84
12b55d76
KL
85 /**
86 * Public constructor.
87 *
88 * @param parent parent widget
89 * @param text text on the screen
90 * @param x column relative to parent
91 * @param y row relative to parent
92 * @param width width of text area
93 * @param height height of text area
94 */
95 public TEditorWidget(final TWidget parent, final String text, final int x,
96 final int y, final int width, final int height) {
97
98 // Set parent and window
99 super(parent, x, y, width, height);
100
101 setCursorVisible(true);
e8a11f98
KL
102
103 defaultColor = getTheme().getColor("teditor");
104 document = new Document(text, defaultColor);
12b55d76
KL
105 }
106
615a0d99
KL
107 // ------------------------------------------------------------------------
108 // TWidget ----------------------------------------------------------------
109 // ------------------------------------------------------------------------
110
12b55d76
KL
111 /**
112 * Draw the text box.
113 */
114 @Override
115 public void draw() {
12b55d76
KL
116 for (int i = 0; i < getHeight(); i++) {
117 // Background line
e8a11f98 118 getScreen().hLineXY(0, i, getWidth(), ' ', defaultColor);
12b55d76
KL
119
120 // Now draw document's line
e8a11f98
KL
121 if (topLine + i < document.getLineCount()) {
122 Line line = document.getLine(topLine + i);
12b55d76
KL
123 int x = 0;
124 for (Word word: line.getWords()) {
e8a11f98
KL
125 // For now, we are cheating: draw outside the left region
126 // if needed and let screen do the clipping.
127 getScreen().putStringXY(x - leftColumn, i, word.getText(),
12b55d76
KL
128 word.getColor());
129 x += word.getDisplayLength();
e8a11f98 130 if (x - leftColumn > getWidth()) {
12b55d76
KL
131 break;
132 }
133 }
134 }
135 }
12b55d76
KL
136 }
137
138 /**
139 * Handle mouse press events.
140 *
141 * @param mouse mouse button press event
142 */
143 @Override
144 public void onMouseDown(final TMouseEvent mouse) {
145 if (mouse.isMouseWheelUp()) {
b1a8acec
KL
146 for (int i = 0; i < wheelScrollSize; i++) {
147 if (topLine > 0) {
148 topLine--;
149 alignDocument(false);
150 }
e8a11f98 151 }
12b55d76
KL
152 return;
153 }
154 if (mouse.isMouseWheelDown()) {
b1a8acec
KL
155 for (int i = 0; i < wheelScrollSize; i++) {
156 if (topLine < document.getLineCount() - 1) {
157 topLine++;
158 alignDocument(true);
159 }
e8a11f98 160 }
12b55d76
KL
161 return;
162 }
163
e8a11f98
KL
164 if (mouse.isMouse1()) {
165 // Set the row and column
166 int newLine = topLine + mouse.getY();
167 int newX = leftColumn + mouse.getX();
df602ccf 168 if (newLine > document.getLineCount() - 1) {
e8a11f98
KL
169 // Go to the end
170 document.setLineNumber(document.getLineCount() - 1);
171 document.end();
df602ccf
KL
172 if (newLine > document.getLineCount() - 1) {
173 setCursorY(document.getLineCount() - 1 - topLine);
e8a11f98 174 } else {
df602ccf 175 setCursorY(mouse.getY());
e8a11f98
KL
176 }
177 alignCursor();
178 return;
179 }
180
181 document.setLineNumber(newLine);
182 setCursorY(mouse.getY());
4297b49b 183 if (newX >= document.getCurrentLine().getDisplayLength()) {
e8a11f98
KL
184 document.end();
185 alignCursor();
186 } else {
df602ccf 187 document.setCursor(newX);
e8a11f98
KL
188 setCursorX(mouse.getX());
189 }
190 return;
191 }
12b55d76
KL
192
193 // Pass to children
194 super.onMouseDown(mouse);
195 }
196
197 /**
198 * Handle keystrokes.
199 *
200 * @param keypress keystroke event
201 */
202 @Override
203 public void onKeypress(final TKeypressEvent keypress) {
204 if (keypress.equals(kbLeft)) {
df602ccf
KL
205 document.left();
206 alignTopLine(false);
12b55d76 207 } else if (keypress.equals(kbRight)) {
df602ccf
KL
208 document.right();
209 alignTopLine(true);
e23989a4
KL
210 } else if (keypress.equals(kbAltLeft)
211 || keypress.equals(kbCtrlLeft)
212 ) {
213 document.backwardsWord();
214 alignTopLine(false);
215 } else if (keypress.equals(kbAltRight)
216 || keypress.equals(kbCtrlRight)
217 ) {
218 document.forwardsWord();
219 alignTopLine(true);
12b55d76 220 } else if (keypress.equals(kbUp)) {
71a389c9
KL
221 document.up();
222 alignTopLine(false);
12b55d76 223 } else if (keypress.equals(kbDown)) {
71a389c9
KL
224 document.down();
225 alignTopLine(true);
12b55d76 226 } else if (keypress.equals(kbPgUp)) {
71a389c9
KL
227 document.up(getHeight() - 1);
228 alignTopLine(false);
12b55d76 229 } else if (keypress.equals(kbPgDn)) {
71a389c9
KL
230 document.down(getHeight() - 1);
231 alignTopLine(true);
12b55d76 232 } else if (keypress.equals(kbHome)) {
e8a11f98
KL
233 if (document.home()) {
234 leftColumn = 0;
235 if (leftColumn < 0) {
236 leftColumn = 0;
237 }
238 setCursorX(0);
239 }
12b55d76 240 } else if (keypress.equals(kbEnd)) {
e8a11f98
KL
241 if (document.end()) {
242 alignCursor();
243 }
12b55d76
KL
244 } else if (keypress.equals(kbCtrlHome)) {
245 document.setLineNumber(0);
246 document.home();
e8a11f98
KL
247 topLine = 0;
248 leftColumn = 0;
249 setCursorX(0);
250 setCursorY(0);
12b55d76
KL
251 } else if (keypress.equals(kbCtrlEnd)) {
252 document.setLineNumber(document.getLineCount() - 1);
253 document.end();
71a389c9 254 alignTopLine(false);
12b55d76
KL
255 } else if (keypress.equals(kbIns)) {
256 document.setOverwrite(!document.getOverwrite());
257 } else if (keypress.equals(kbDel)) {
258 document.del();
71a389c9 259 alignCursor();
39e86397
KL
260 } else if (keypress.equals(kbBackspace)
261 || keypress.equals(kbBackspaceDel)
262 ) {
12b55d76 263 document.backspace();
df602ccf
KL
264 alignTopLine(false);
265 } else if (keypress.equals(kbTab)) {
266 // TODO: tab character. For now just add spaces until we hit
267 // modulo 8.
268 for (int i = document.getCursor(); (i + 1) % 8 != 0; i++) {
269 document.addChar(' ');
270 }
e8a11f98 271 alignCursor();
71a389c9 272 } else if (keypress.equals(kbEnter)) {
df602ccf
KL
273 document.enter();
274 alignTopLine(true);
12b55d76
KL
275 } else if (!keypress.getKey().isFnKey()
276 && !keypress.getKey().isAlt()
277 && !keypress.getKey().isCtrl()
278 ) {
279 // Plain old keystroke, process it
afdec5e9 280 // TODO: fix document to use ints, not chars
2d3f60d8 281 document.addChar(keypress.getKey().getChar());
71a389c9 282 alignCursor();
12b55d76
KL
283 } else {
284 // Pass other keys (tab etc.) on to TWidget
285 super.onKeypress(keypress);
286 }
287 }
288
e8a11f98
KL
289 /**
290 * Method that subclasses can override to handle window/screen resize
291 * events.
292 *
293 * @param resize resize event
294 */
295 @Override
296 public void onResize(final TResizeEvent resize) {
297 // Change my width/height, and pull the cursor in as needed.
298 if (resize.getType() == TResizeEvent.Type.WIDGET) {
299 setWidth(resize.getWidth());
300 setHeight(resize.getHeight());
301 // See if the cursor is now outside the window, and if so move
302 // things.
303 if (getCursorX() >= getWidth()) {
304 leftColumn += getCursorX() - (getWidth() - 1);
305 setCursorX(getWidth() - 1);
306 }
307 if (getCursorY() >= getHeight()) {
308 topLine += getCursorY() - (getHeight() - 1);
309 setCursorY(getHeight() - 1);
310 }
311 } else {
312 // Let superclass handle it
313 super.onResize(resize);
314 }
315 }
316
615a0d99
KL
317 // ------------------------------------------------------------------------
318 // TEditorWidget ----------------------------------------------------------
319 // ------------------------------------------------------------------------
320
321 /**
322 * Align visible area with document current line.
323 *
324 * @param topLineIsTop if true, make the top visible line the document
325 * current line if it was off-screen. If false, make the bottom visible
326 * line the document current line.
327 */
328 private void alignTopLine(final boolean topLineIsTop) {
329 int line = document.getLineNumber();
330
331 if ((line < topLine) || (line > topLine + getHeight() - 1)) {
332 // Need to move topLine to bring document back into view.
333 if (topLineIsTop) {
334 topLine = line - (getHeight() - 1);
335 if (topLine < 0) {
336 topLine = 0;
337 }
338 assert (topLine >= 0);
339 } else {
340 topLine = line;
341 assert (topLine >= 0);
342 }
343 }
344
345 /*
346 System.err.println("line " + line + " topLine " + topLine);
347 */
348
349 // Document is in view, let's set cursorY
350 assert (line >= topLine);
351 setCursorY(line - topLine);
352 alignCursor();
353 }
354
355 /**
356 * Align document current line with visible area.
357 *
358 * @param topLineIsTop if true, make the top visible line the document
359 * current line if it was off-screen. If false, make the bottom visible
360 * line the document current line.
361 */
362 private void alignDocument(final boolean topLineIsTop) {
363 int line = document.getLineNumber();
364 int cursor = document.getCursor();
365
366 if ((line < topLine) || (line > topLine + getHeight() - 1)) {
367 // Need to move document to ensure it fits view.
368 if (topLineIsTop) {
369 document.setLineNumber(topLine);
370 } else {
371 document.setLineNumber(topLine + (getHeight() - 1));
372 }
373 if (cursor < document.getCurrentLine().getDisplayLength()) {
374 document.setCursor(cursor);
375 }
376 }
377
378 /*
379 System.err.println("getLineNumber() " + document.getLineNumber() +
380 " topLine " + topLine);
381 */
382
383 // Document is in view, let's set cursorY
384 setCursorY(document.getLineNumber() - topLine);
385 alignCursor();
386 }
387
388 /**
389 * Align visible cursor with document cursor.
390 */
391 private void alignCursor() {
392 int width = getWidth();
393
394 int desiredX = document.getCursor() - leftColumn;
395 if (desiredX < 0) {
396 // We need to push the screen to the left.
397 leftColumn = document.getCursor();
398 } else if (desiredX > width - 1) {
399 // We need to push the screen to the right.
400 leftColumn = document.getCursor() - (width - 1);
401 }
402
403 /*
404 System.err.println("document cursor " + document.getCursor() +
405 " leftColumn " + leftColumn);
406 */
407
408
409 setCursorX(document.getCursor() - leftColumn);
410 }
411
71a389c9
KL
412 /**
413 * Get the number of lines in the underlying Document.
414 *
415 * @return the number of lines
416 */
417 public int getLineCount() {
418 return document.getLineCount();
419 }
420
b6faeac0
KL
421 /**
422 * Get the current visible top row number. 1-based.
423 *
424 * @return the visible top row number. Row 1 is the first row.
425 */
426 public int getVisibleRowNumber() {
427 return topLine + 1;
428 }
429
430 /**
431 * Set the current visible row number. 1-based.
432 *
433 * @param row the new visible row number. Row 1 is the first row.
434 */
435 public void setVisibleRowNumber(final int row) {
436 assert (row > 0);
437 if ((row > 0) && (row < document.getLineCount())) {
438 topLine = row - 1;
439 alignDocument(true);
440 }
441 }
442
71a389c9
KL
443 /**
444 * Get the current editing row number. 1-based.
445 *
446 * @return the editing row number. Row 1 is the first row.
447 */
448 public int getEditingRowNumber() {
449 return document.getLineNumber() + 1;
450 }
451
452 /**
453 * Set the current editing row number. 1-based.
454 *
455 * @param row the new editing row number. Row 1 is the first row.
456 */
457 public void setEditingRowNumber(final int row) {
fe0770f9
KL
458 assert (row > 0);
459 if ((row > 0) && (row < document.getLineCount())) {
460 document.setLineNumber(row - 1);
461 alignTopLine(true);
462 }
71a389c9
KL
463 }
464
465 /**
466 * Get the current editing column number. 1-based.
467 *
468 * @return the editing column number. Column 1 is the first column.
469 */
470 public int getEditingColumnNumber() {
471 return document.getCursor() + 1;
472 }
473
474 /**
475 * Set the current editing column number. 1-based.
476 *
477 * @param column the new editing column number. Column 1 is the first
478 * column.
479 */
480 public void setEditingColumnNumber(final int column) {
fe0770f9
KL
481 if ((column > 0) && (column < document.getLineLength())) {
482 document.setCursor(column - 1);
483 alignCursor();
484 }
71a389c9
KL
485 }
486
487 /**
488 * Get the maximum possible row number. 1-based.
489 *
490 * @return the maximum row number. Row 1 is the first row.
491 */
492 public int getMaximumRowNumber() {
493 return document.getLineCount() + 1;
494 }
495
496 /**
497 * Get the maximum possible column number. 1-based.
498 *
499 * @return the maximum column number. Column 1 is the first column.
500 */
501 public int getMaximumColumnNumber() {
502 return document.getLineLengthMax() + 1;
503 }
504
505 /**
506 * Get the dirty value.
507 *
508 * @return true if the buffer is dirty
509 */
510 public boolean isDirty() {
511 return document.isDirty();
512 }
513
514 /**
515 * Save contents to file.
516 *
517 * @param filename file to save to
518 * @throws IOException if a java.io operation throws
519 */
520 public void saveToFilename(final String filename) throws IOException {
521 document.saveToFilename(filename);
522 }
523
12b55d76 524}