only keep the (re)sources here
[nikiroo-utils.git] / jexer / TField.java
CommitLineData
daa4106c 1/*
128e5be1
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
128e5be1 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
128e5be1 7 *
e16dda65
KL
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:
128e5be1 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
128e5be1 17 *
e16dda65
KL
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.
128e5be1
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
31import jexer.bits.CellAttributes;
32import jexer.bits.GraphicsChars;
58c6a100 33import jexer.bits.StringUtils;
128e5be1
KL
34import jexer.event.TKeypressEvent;
35import jexer.event.TMouseEvent;
36import static jexer.TKeypress.*;
37
38/**
87a17f3c 39 * TField implements an editable text field.
128e5be1 40 */
87a17f3c 41public class TField extends TWidget {
128e5be1 42
d36057df
KL
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
46
1dac6b8d
KL
47 /**
48 * Background character for unfilled-in text.
49 */
afdec5e9 50 protected int backgroundChar = GraphicsChars.HATCH;
1dac6b8d 51
128e5be1
KL
52 /**
53 * Field text.
54 */
87a17f3c 55 protected String text = "";
128e5be1 56
128e5be1
KL
57 /**
58 * If true, only allow enough characters that will fit in the width. If
59 * false, allow the field to scroll to the right.
60 */
87a17f3c 61 protected boolean fixed = false;
128e5be1
KL
62
63 /**
64 * Current editing position within text.
65 */
87a17f3c 66 protected int position = 0;
128e5be1 67
58c6a100
KL
68 /**
69 * Current editing position screen column number.
70 */
71 protected int screenPosition = 0;
72
128e5be1
KL
73 /**
74 * Beginning of visible portion.
75 */
87a17f3c 76 protected int windowStart = 0;
128e5be1
KL
77
78 /**
79 * If true, new characters are inserted at position.
80 */
87a17f3c 81 protected boolean insertMode = true;
128e5be1
KL
82
83 /**
84 * Remember mouse state.
85 */
87a17f3c 86 protected TMouseEvent mouse;
128e5be1
KL
87
88 /**
89 * The action to perform when the user presses enter.
90 */
87a17f3c 91 protected TAction enterAction;
128e5be1
KL
92
93 /**
94 * The action to perform when the text is updated.
95 */
87a17f3c 96 protected TAction updateAction;
128e5be1 97
1dac6b8d
KL
98 /**
99 * The color to use when this field is active.
100 */
101 private String activeColorKey = "tfield.active";
102
103 /**
104 * The color to use when this field is not active.
105 */
106 private String inactiveColorKey = "tfield.inactive";
107
d36057df
KL
108 // ------------------------------------------------------------------------
109 // Constructors -----------------------------------------------------------
110 // ------------------------------------------------------------------------
111
128e5be1
KL
112 /**
113 * Public constructor.
114 *
115 * @param parent parent widget
116 * @param x column relative to parent
117 * @param y row relative to parent
118 * @param width visible text width
119 * @param fixed if true, the text cannot exceed the display width
120 */
121 public TField(final TWidget parent, final int x, final int y,
122 final int width, final boolean fixed) {
123
124 this(parent, x, y, width, fixed, "", null, null);
125 }
126
127 /**
128 * Public constructor.
129 *
130 * @param parent parent widget
131 * @param x column relative to parent
132 * @param y row relative to parent
133 * @param width visible text width
134 * @param fixed if true, the text cannot exceed the display width
135 * @param text initial text, default is empty string
136 */
137 public TField(final TWidget parent, final int x, final int y,
138 final int width, final boolean fixed, final String text) {
139
140 this(parent, x, y, width, fixed, text, null, null);
141 }
142
143 /**
144 * Public constructor.
145 *
146 * @param parent parent widget
147 * @param x column relative to parent
148 * @param y row relative to parent
149 * @param width visible text width
150 * @param fixed if true, the text cannot exceed the display width
151 * @param text initial text, default is empty string
152 * @param enterAction function to call when enter key is pressed
153 * @param updateAction function to call when the text is updated
154 */
155 public TField(final TWidget parent, final int x, final int y,
156 final int width, final boolean fixed, final String text,
157 final TAction enterAction, final TAction updateAction) {
158
159 // Set parent and window
a83fea2b 160 super(parent, x, y, width, 1);
128e5be1 161
7c870d89 162 setCursorVisible(true);
128e5be1
KL
163 this.fixed = fixed;
164 this.text = text;
165 this.enterAction = enterAction;
166 this.updateAction = updateAction;
167 }
168
d36057df
KL
169 // ------------------------------------------------------------------------
170 // Event handlers ---------------------------------------------------------
171 // ------------------------------------------------------------------------
172
128e5be1
KL
173 /**
174 * Returns true if the mouse is currently on the field.
175 *
176 * @return if true the mouse is currently on the field
177 */
87a17f3c 178 protected boolean mouseOnField() {
128e5be1
KL
179 int rightEdge = getWidth() - 1;
180 if ((mouse != null)
181 && (mouse.getY() == 0)
182 && (mouse.getX() >= 0)
183 && (mouse.getX() <= rightEdge)
184 ) {
185 return true;
186 }
187 return false;
188 }
189
128e5be1
KL
190 /**
191 * Handle mouse button presses.
192 *
193 * @param mouse mouse button event
194 */
195 @Override
196 public void onMouseDown(final TMouseEvent mouse) {
197 this.mouse = mouse;
198
7c870d89 199 if ((mouseOnField()) && (mouse.isMouse1())) {
128e5be1
KL
200 // Move cursor
201 int deltaX = mouse.getX() - getCursorX();
58c6a100
KL
202 screenPosition += deltaX;
203 if (screenPosition > StringUtils.width(text)) {
204 screenPosition = StringUtils.width(text);
128e5be1 205 }
58c6a100 206 position = screenToTextPosition(screenPosition);
128e5be1
KL
207 updateCursor();
208 return;
209 }
210 }
211
212 /**
213 * Handle keystrokes.
214 *
215 * @param keypress keystroke event
216 */
217 @Override
218 public void onKeypress(final TKeypressEvent keypress) {
219
220 if (keypress.equals(kbLeft)) {
221 if (position > 0) {
ebb9a1e4
KL
222 screenPosition -= StringUtils.width(text.codePointBefore(position));
223 position -= Character.charCount(text.codePointBefore(position));
128e5be1
KL
224 }
225 if (fixed == false) {
58c6a100 226 if ((screenPosition == windowStart) && (windowStart > 0)) {
84a34fdf 227 windowStart -= StringUtils.width(text.codePointAt(
58c6a100 228 screenToTextPosition(windowStart)));
128e5be1
KL
229 }
230 }
24489803 231 normalizeWindowStart();
128e5be1
KL
232 return;
233 }
234
235 if (keypress.equals(kbRight)) {
ebb9a1e4 236 if (position < text.length()) {
84a34fdf 237 screenPosition += StringUtils.width(text.codePointAt(position));
6b2633ac 238 position += Character.charCount(text.codePointAt(position));
128e5be1 239 if (fixed == true) {
58c6a100
KL
240 if (screenPosition == getWidth()) {
241 screenPosition--;
6b2633ac 242 position -= Character.charCount(text.codePointAt(position));
128e5be1
KL
243 }
244 } else {
ebb9a1e4
KL
245 while ((screenPosition - windowStart +
246 StringUtils.width(text.codePointAt(text.length() - 1)))
247 > getWidth()
248 ) {
84a34fdf 249 windowStart += StringUtils.width(text.codePointAt(
58c6a100 250 screenToTextPosition(windowStart)));
128e5be1
KL
251 }
252 }
253 }
ebb9a1e4 254 assert (position <= text.length());
128e5be1
KL
255 return;
256 }
257
258 if (keypress.equals(kbEnter)) {
259 dispatch(true);
260 return;
261 }
262
263 if (keypress.equals(kbIns)) {
264 insertMode = !insertMode;
265 return;
266 }
267 if (keypress.equals(kbHome)) {
1b120607 268 home();
128e5be1
KL
269 return;
270 }
271
272 if (keypress.equals(kbEnd)) {
1b120607 273 end();
128e5be1
KL
274 return;
275 }
276
277 if (keypress.equals(kbDel)) {
ebb9a1e4 278 if ((text.length() > 0) && (position < text.length())) {
128e5be1
KL
279 text = text.substring(0, position)
280 + text.substring(position + 1);
58c6a100 281 screenPosition = StringUtils.width(text.substring(0, position));
128e5be1 282 }
24489803 283 dispatch(false);
128e5be1
KL
284 return;
285 }
286
287 if (keypress.equals(kbBackspace) || keypress.equals(kbBackspaceDel)) {
288 if (position > 0) {
6b2633ac 289 position -= Character.charCount(text.codePointBefore(position));
128e5be1
KL
290 text = text.substring(0, position)
291 + text.substring(position + 1);
58c6a100 292 screenPosition = StringUtils.width(text.substring(0, position));
128e5be1
KL
293 }
294 if (fixed == false) {
58c6a100 295 if ((screenPosition >= windowStart)
128e5be1
KL
296 && (windowStart > 0)
297 ) {
84a34fdf 298 windowStart -= StringUtils.width(text.codePointAt(
58c6a100 299 screenToTextPosition(windowStart)));
128e5be1
KL
300 }
301 }
302 dispatch(false);
24489803 303 normalizeWindowStart();
128e5be1
KL
304 return;
305 }
306
7c870d89
KL
307 if (!keypress.getKey().isFnKey()
308 && !keypress.getKey().isAlt()
309 && !keypress.getKey().isCtrl()
128e5be1
KL
310 ) {
311 // Plain old keystroke, process it
ebb9a1e4 312 if ((position == text.length())
58c6a100 313 && (StringUtils.width(text) < getWidth())) {
128e5be1
KL
314
315 // Append case
7c870d89 316 appendChar(keypress.getKey().getChar());
ebb9a1e4 317 } else if ((position < text.length())
58c6a100 318 && (StringUtils.width(text) < getWidth())) {
128e5be1
KL
319
320 // Overwrite or insert a character
321 if (insertMode == false) {
322 // Replace character
323 text = text.substring(0, position)
84a34fdf 324 + codePointString(keypress.getKey().getChar())
128e5be1 325 + text.substring(position + 1);
84a34fdf
KL
326 screenPosition += StringUtils.width(text.codePointAt(position));
327 position += Character.charCount(keypress.getKey().getChar());
128e5be1
KL
328 } else {
329 // Insert character
7c870d89 330 insertChar(keypress.getKey().getChar());
128e5be1 331 }
ebb9a1e4 332 } else if ((position < text.length())
58c6a100 333 && (StringUtils.width(text) >= getWidth())) {
128e5be1
KL
334
335 // Multiple cases here
336 if ((fixed == true) && (insertMode == true)) {
337 // Buffer is full, do nothing
338 } else if ((fixed == true) && (insertMode == false)) {
339 // Overwrite the last character, maybe move position
340 text = text.substring(0, position)
84a34fdf 341 + codePointString(keypress.getKey().getChar())
128e5be1 342 + text.substring(position + 1);
58c6a100 343 if (screenPosition < getWidth() - 1) {
84a34fdf
KL
344 screenPosition += StringUtils.width(text.codePointAt(position));
345 position += Character.charCount(keypress.getKey().getChar());
128e5be1
KL
346 }
347 } else if ((fixed == false) && (insertMode == false)) {
348 // Overwrite the last character, definitely move position
349 text = text.substring(0, position)
84a34fdf 350 + codePointString(keypress.getKey().getChar())
128e5be1 351 + text.substring(position + 1);
84a34fdf
KL
352 screenPosition += StringUtils.width(text.codePointAt(position));
353 position += Character.charCount(keypress.getKey().getChar());
128e5be1 354 } else {
ebb9a1e4 355 if (position == text.length()) {
128e5be1 356 // Append this character
7c870d89 357 appendChar(keypress.getKey().getChar());
128e5be1
KL
358 } else {
359 // Insert this character
7c870d89 360 insertChar(keypress.getKey().getChar());
128e5be1
KL
361 }
362 }
363 } else {
364 assert (!fixed);
365
366 // Append this character
7c870d89 367 appendChar(keypress.getKey().getChar());
128e5be1
KL
368 }
369 dispatch(false);
370 return;
371 }
372
373 // Pass to parent for the things we don't care about.
374 super.onKeypress(keypress);
375 }
376
d36057df
KL
377 // ------------------------------------------------------------------------
378 // TWidget ----------------------------------------------------------------
379 // ------------------------------------------------------------------------
380
d8dc8aea
KL
381 /**
382 * Override TWidget's height: we can only set height at construction
383 * time.
384 *
385 * @param height new widget height (ignored)
386 */
387 @Override
388 public void setHeight(final int height) {
389 // Do nothing
390 }
391
d36057df
KL
392 /**
393 * Draw the text field.
394 */
395 @Override
396 public void draw() {
397 CellAttributes fieldColor;
398
399 if (isAbsoluteActive()) {
1dac6b8d 400 fieldColor = getTheme().getColor(activeColorKey);
d36057df 401 } else {
1dac6b8d 402 fieldColor = getTheme().getColor(inactiveColorKey);
d36057df
KL
403 }
404
405 int end = windowStart + getWidth();
58c6a100
KL
406 if (end > StringUtils.width(text)) {
407 end = StringUtils.width(text);
d36057df 408 }
1dac6b8d 409 hLineXY(0, 0, getWidth(), backgroundChar, fieldColor);
58c6a100
KL
410 putStringXY(0, 0, text.substring(screenToTextPosition(windowStart),
411 screenToTextPosition(end)), fieldColor);
d36057df
KL
412
413 // Fix the cursor, it will be rendered by TApplication.drawAll().
414 updateCursor();
415 }
416
417 // ------------------------------------------------------------------------
418 // TField -----------------------------------------------------------------
419 // ------------------------------------------------------------------------
420
84a34fdf
KL
421 /**
422 * Convert a char (codepoint) to a string.
423 *
424 * @param ch the char
425 * @return the string
426 */
427 private String codePointString(final int ch) {
428 StringBuilder sb = new StringBuilder(1);
429 sb.append(Character.toChars(ch));
ebb9a1e4 430 assert (Character.charCount(ch) == sb.length());
84a34fdf
KL
431 return sb.toString();
432 }
433
1dac6b8d
KL
434 /**
435 * Get field background character.
436 *
437 * @return background character
438 */
afdec5e9 439 public final int getBackgroundChar() {
1dac6b8d
KL
440 return backgroundChar;
441 }
442
443 /**
444 * Set field background character.
445 *
446 * @param backgroundChar the background character
447 */
afdec5e9 448 public void setBackgroundChar(final int backgroundChar) {
1dac6b8d
KL
449 this.backgroundChar = backgroundChar;
450 }
451
d36057df
KL
452 /**
453 * Get field text.
454 *
455 * @return field text
456 */
457 public final String getText() {
458 return text;
459 }
460
461 /**
462 * Set field text.
463 *
464 * @param text the new field text
465 */
051e2913 466 public void setText(final String text) {
a69ed767 467 assert (text != null);
d36057df
KL
468 this.text = text;
469 position = 0;
470 windowStart = 0;
471 }
472
473 /**
474 * Dispatch to the action function.
475 *
476 * @param enter if true, the user pressed Enter, else this was an update
477 * to the text.
478 */
479 protected void dispatch(final boolean enter) {
480 if (enter) {
481 if (enterAction != null) {
a524aa2e 482 enterAction.DO(this);
d36057df
KL
483 }
484 } else {
485 if (updateAction != null) {
a524aa2e 486 updateAction.DO(this);
d36057df
KL
487 }
488 }
489 }
490
58c6a100
KL
491 /**
492 * Determine string position from screen position.
493 *
494 * @param screenPosition the position on screen
495 * @return the equivalent position in text
496 */
497 protected int screenToTextPosition(final int screenPosition) {
498 if (screenPosition == 0) {
499 return 0;
500 }
501
502 int n = 0;
ebb9a1e4 503 for (int i = 0; i < text.length(); i++) {
84a34fdf 504 n += StringUtils.width(text.codePointAt(i));
58c6a100
KL
505 if (n >= screenPosition) {
506 return i + 1;
507 }
508 }
509 // screenPosition exceeds the available text length.
510 throw new IndexOutOfBoundsException("screenPosition " + screenPosition +
ebb9a1e4 511 " exceeds available text length " + text.length());
58c6a100
KL
512 }
513
d36057df
KL
514 /**
515 * Update the visible cursor position to match the location of position
516 * and windowStart.
517 */
518 protected void updateCursor() {
58c6a100 519 if ((screenPosition > getWidth()) && fixed) {
d36057df 520 setCursorX(getWidth());
58c6a100 521 } else if ((screenPosition - windowStart >= getWidth()) && !fixed) {
d36057df
KL
522 setCursorX(getWidth() - 1);
523 } else {
58c6a100 524 setCursorX(screenPosition - windowStart);
d36057df
KL
525 }
526 }
527
528 /**
529 * Normalize windowStart such that most of the field data if visible.
530 */
531 protected void normalizeWindowStart() {
532 if (fixed) {
533 // windowStart had better be zero, there is nothing to do here.
534 assert (windowStart == 0);
535 return;
536 }
58c6a100 537 windowStart = screenPosition - (getWidth() - 1);
d36057df
KL
538 if (windowStart < 0) {
539 windowStart = 0;
540 }
541
542 updateCursor();
543 }
544
128e5be1
KL
545 /**
546 * Append char to the end of the field.
547 *
afdec5e9 548 * @param ch char to append
128e5be1 549 */
afdec5e9 550 protected void appendChar(final int ch) {
128e5be1 551 // Append the LAST character
84a34fdf
KL
552 text += codePointString(ch);
553 position += Character.charCount(ch);
58c6a100 554 screenPosition += StringUtils.width(ch);
128e5be1 555
ebb9a1e4 556 assert (position == text.length());
128e5be1
KL
557
558 if (fixed) {
58c6a100 559 if (screenPosition >= getWidth()) {
84a34fdf 560 position -= Character.charCount(ch);
58c6a100 561 screenPosition -= StringUtils.width(ch);
128e5be1
KL
562 }
563 } else {
58c6a100 564 if ((screenPosition - windowStart) >= getWidth()) {
128e5be1
KL
565 windowStart++;
566 }
567 }
568 }
569
570 /**
571 * Insert char somewhere in the middle of the field.
572 *
573 * @param ch char to append
574 */
afdec5e9 575 protected void insertChar(final int ch) {
84a34fdf
KL
576 text = text.substring(0, position) + codePointString(ch)
577 + text.substring(position);
578 position += Character.charCount(ch);
58c6a100
KL
579 screenPosition += StringUtils.width(ch);
580 if ((screenPosition - windowStart) == getWidth()) {
128e5be1
KL
581 assert (!fixed);
582 windowStart++;
583 }
584 }
585
1b120607
KL
586 /**
587 * Position the cursor at the first column. The field may adjust the
588 * window start to show as much of the field as possible.
589 */
590 public void home() {
591 position = 0;
58c6a100 592 screenPosition = 0;
1b120607
KL
593 windowStart = 0;
594 }
595
596 /**
597 * Set the editing position to the last filled character. The field may
598 * adjust the window start to show as much of the field as possible.
599 */
600 public void end() {
ebb9a1e4 601 position = text.length();
58c6a100 602 screenPosition = StringUtils.width(text);
1b120607 603 if (fixed == true) {
58c6a100 604 if (screenPosition >= getWidth()) {
ebb9a1e4 605 position -= Character.charCount(text.codePointBefore(position));
58c6a100
KL
606 screenPosition = StringUtils.width(text) - 1;
607 }
1b120607 608 } else {
58c6a100 609 windowStart = StringUtils.width(text) - getWidth() + 1;
1b120607
KL
610 if (windowStart < 0) {
611 windowStart = 0;
612 }
613 }
614 }
615
616 /**
617 * Set the editing position. The field may adjust the window start to
618 * show as much of the field as possible.
619 *
620 * @param position the new position
621 * @throws IndexOutOfBoundsException if position is outside the range of
622 * the available text
623 */
624 public void setPosition(final int position) {
ebb9a1e4 625 if ((position < 0) || (position >= text.length())) {
1b120607 626 throw new IndexOutOfBoundsException("Max length is " +
ebb9a1e4 627 text.length() + ", requested position " + position);
1b120607
KL
628 }
629 this.position = position;
630 normalizeWindowStart();
631 }
632
1dac6b8d
KL
633 /**
634 * Set the active color key.
635 *
636 * @param activeColorKey ColorTheme key color to use when this field is
637 * active
638 */
639 public void setActiveColorKey(final String activeColorKey) {
640 this.activeColorKey = activeColorKey;
641 }
642
643 /**
644 * Set the inactive color key.
645 *
646 * @param inactiveColorKey ColorTheme key color to use when this field is
647 * inactive
648 */
649 public void setInactiveColorKey(final String inactiveColorKey) {
650 this.inactiveColorKey = inactiveColorKey;
651 }
652
2b427404
KL
653 /**
654 * Set the action to perform when the user presses enter.
655 *
656 * @param action the action to perform when the user presses enter
657 */
658 public void setEnterAction(final TAction action) {
659 enterAction = action;
660 }
661
662 /**
663 * Set the action to perform when the field is updated.
664 *
665 * @param action the action to perform when the field is updated
666 */
667 public void setUpdateAction(final TAction action) {
668 updateAction = action;
669 }
1dac6b8d 670
128e5be1 671}