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]
29 package jexer
.backend
;
31 import jexer
.bits
.Cell
;
32 import jexer
.bits
.CellAttributes
;
33 import jexer
.bits
.GraphicsChars
;
36 * A logical screen composed of a 2D array of Cells.
38 public class LogicalScreen
implements Screen
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Width of the visible window.
50 * Height of the visible window.
55 * Drawing offset for x.
60 * Drawing offset for y.
65 * Ignore anything drawn right of clipRight.
67 private int clipRight
;
70 * Ignore anything drawn below clipBottom.
72 private int clipBottom
;
75 * Ignore anything drawn left of clipLeft.
80 * Ignore anything drawn above clipTop.
85 * The physical screen last sent out on flush().
87 protected Cell
[][] physical
;
90 * The logical screen being rendered to.
92 protected Cell
[][] logical
;
95 * Set if the user explicitly wants to redraw everything starting with a
96 * ECMATerminal.clearAll().
98 protected boolean reallyCleared
;
101 * If true, the cursor is visible and should be placed onscreen at
102 * (cursorX, cursorY) during a call to flushPhysical().
104 protected boolean cursorVisible
;
107 * Cursor X position if visible.
109 protected int cursorX
;
112 * Cursor Y position if visible.
114 protected int cursorY
;
116 // ------------------------------------------------------------------------
117 // Constructors -----------------------------------------------------------
118 // ------------------------------------------------------------------------
121 * Public constructor. Sets everything to not-bold, white-on-black.
123 protected LogicalScreen() {
130 reallocate(width
, height
);
133 // ------------------------------------------------------------------------
134 // Screen -----------------------------------------------------------------
135 // ------------------------------------------------------------------------
138 * Set drawing offset for x.
140 * @param offsetX new drawing offset
142 public final void setOffsetX(final int offsetX
) {
143 this.offsetX
= offsetX
;
147 * Set drawing offset for y.
149 * @param offsetY new drawing offset
151 public final void setOffsetY(final int offsetY
) {
152 this.offsetY
= offsetY
;
156 * Get right drawing clipping boundary.
158 * @return drawing boundary
160 public final int getClipRight() {
165 * Set right drawing clipping boundary.
167 * @param clipRight new boundary
169 public final void setClipRight(final int clipRight
) {
170 this.clipRight
= clipRight
;
174 * Get bottom drawing clipping boundary.
176 * @return drawing boundary
178 public final int getClipBottom() {
183 * Set bottom drawing clipping boundary.
185 * @param clipBottom new boundary
187 public final void setClipBottom(final int clipBottom
) {
188 this.clipBottom
= clipBottom
;
192 * Get left drawing clipping boundary.
194 * @return drawing boundary
196 public final int getClipLeft() {
201 * Set left drawing clipping boundary.
203 * @param clipLeft new boundary
205 public final void setClipLeft(final int clipLeft
) {
206 this.clipLeft
= clipLeft
;
210 * Get top drawing clipping boundary.
212 * @return drawing boundary
214 public final int getClipTop() {
219 * Set top drawing clipping boundary.
221 * @param clipTop new boundary
223 public final void setClipTop(final int clipTop
) {
224 this.clipTop
= clipTop
;
230 * @return if true, the logical screen is not in sync with the physical
233 public final boolean isDirty() {
234 for (int x
= 0; x
< width
; x
++) {
235 for (int y
= 0; y
< height
; y
++) {
236 if (!logical
[x
][y
].equals(physical
[x
][y
])) {
239 if (logical
[x
][y
].isBlink()) {
240 // Blinking screens are always dirty. There is
241 // opportunity for a Netscape blink tag joke here...
251 * Get the attributes at one location.
253 * @param x column coordinate. 0 is the left-most column.
254 * @param y row coordinate. 0 is the top-most row.
255 * @return attributes at (x, y)
257 public final CellAttributes
getAttrXY(final int x
, final int y
) {
258 CellAttributes attr
= new CellAttributes();
259 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
260 attr
.setTo(logical
[x
][y
]);
266 * Get the cell at one location.
268 * @param x column coordinate. 0 is the left-most column.
269 * @param y row coordinate. 0 is the top-most row.
270 * @return the character + attributes
272 public Cell
getCharXY(final int x
, final int y
) {
273 Cell cell
= new Cell();
274 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
275 cell
.setTo(logical
[x
][y
]);
281 * Set the attributes at one location.
283 * @param x column coordinate. 0 is the left-most column.
284 * @param y row coordinate. 0 is the top-most row.
285 * @param attr attributes to use (bold, foreColor, backColor)
287 public final void putAttrXY(final int x
, final int y
,
288 final CellAttributes attr
) {
290 putAttrXY(x
, y
, attr
, true);
294 * Set the attributes at one location.
296 * @param x column coordinate. 0 is the left-most column.
297 * @param y row coordinate. 0 is the top-most row.
298 * @param attr attributes to use (bold, foreColor, backColor)
299 * @param clip if true, honor clipping/offset
301 public final void putAttrXY(final int x
, final int y
,
302 final CellAttributes attr
, final boolean clip
) {
319 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
320 logical
[X
][Y
].setTo(attr
);
322 // If this happens to be the cursor position, make the position
324 if ((cursorX
== X
) && (cursorY
== Y
)) {
325 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
326 physical
[cursorX
][cursorY
].setChar('X');
328 physical
[cursorX
][cursorY
].setChar('Q');
335 * Fill the entire screen with one character with attributes.
337 * @param ch character to draw
338 * @param attr attributes to use (bold, foreColor, backColor)
340 public final void putAll(final char ch
, final CellAttributes attr
) {
342 for (int x
= 0; x
< width
; x
++) {
343 for (int y
= 0; y
< height
; y
++) {
344 putCharXY(x
, y
, ch
, attr
);
350 * Render one character with attributes.
352 * @param x column coordinate. 0 is the left-most column.
353 * @param y row coordinate. 0 is the top-most row.
354 * @param ch character + attributes to draw
356 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
357 putCharXY(x
, y
, ch
.getChar(), ch
);
361 * Render one character with attributes.
363 * @param x column coordinate. 0 is the left-most column.
364 * @param y row coordinate. 0 is the top-most row.
365 * @param ch character to draw
366 * @param attr attributes to use (bold, foreColor, backColor)
368 public final void putCharXY(final int x
, final int y
, final char ch
,
369 final CellAttributes attr
) {
382 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
384 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
386 // Do not put control characters on the display
390 logical
[X
][Y
].setTo(attr
);
391 logical
[X
][Y
].setChar(ch
);
393 // If this happens to be the cursor position, make the position
395 if ((cursorX
== X
) && (cursorY
== Y
)) {
396 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
397 physical
[cursorX
][cursorY
].setChar('X');
399 physical
[cursorX
][cursorY
].setChar('Q');
406 * Render one character without changing the underlying attributes.
408 * @param x column coordinate. 0 is the left-most column.
409 * @param y row coordinate. 0 is the top-most row.
410 * @param ch character to draw
412 public final void putCharXY(final int x
, final int y
, final char ch
) {
425 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
427 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
428 logical
[X
][Y
].setChar(ch
);
430 // If this happens to be the cursor position, make the position
432 if ((cursorX
== X
) && (cursorY
== Y
)) {
433 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
434 physical
[cursorX
][cursorY
].setChar('X');
436 physical
[cursorX
][cursorY
].setChar('Q');
443 * Render a string. Does not wrap if the string exceeds the line.
445 * @param x column coordinate. 0 is the left-most column.
446 * @param y row coordinate. 0 is the top-most row.
447 * @param str string to draw
448 * @param attr attributes to use (bold, foreColor, backColor)
450 public final void putStringXY(final int x
, final int y
, final String str
,
451 final CellAttributes attr
) {
454 for (int j
= 0; j
< str
.length(); j
++) {
455 char ch
= str
.charAt(j
);
456 putCharXY(i
, y
, ch
, attr
);
465 * Render a string without changing the underlying attribute. Does not
466 * wrap if the string exceeds the line.
468 * @param x column coordinate. 0 is the left-most column.
469 * @param y row coordinate. 0 is the top-most row.
470 * @param str string to draw
472 public final void putStringXY(final int x
, final int y
, final String str
) {
475 for (int j
= 0; j
< str
.length(); j
++) {
476 char ch
= str
.charAt(j
);
486 * Draw a vertical line from (x, y) to (x, y + n).
488 * @param x column coordinate. 0 is the left-most column.
489 * @param y row coordinate. 0 is the top-most row.
490 * @param n number of characters to draw
491 * @param ch character to draw
492 * @param attr attributes to use (bold, foreColor, backColor)
494 public final void vLineXY(final int x
, final int y
, final int n
,
495 final char ch
, final CellAttributes attr
) {
497 for (int i
= y
; i
< y
+ n
; i
++) {
498 putCharXY(x
, i
, ch
, attr
);
503 * Draw a horizontal line from (x, y) to (x + n, y).
505 * @param x column coordinate. 0 is the left-most column.
506 * @param y row coordinate. 0 is the top-most row.
507 * @param n number of characters to draw
508 * @param ch character to draw
509 * @param attr attributes to use (bold, foreColor, backColor)
511 public final void hLineXY(final int x
, final int y
, final int n
,
512 final char ch
, final CellAttributes attr
) {
514 for (int i
= x
; i
< x
+ n
; i
++) {
515 putCharXY(i
, y
, ch
, attr
);
520 * Change the width. Everything on-screen will be destroyed and must be
523 * @param width new screen width
525 public final synchronized void setWidth(final int width
) {
526 reallocate(width
, this.height
);
530 * Change the height. Everything on-screen will be destroyed and must be
533 * @param height new screen height
535 public final synchronized void setHeight(final int height
) {
536 reallocate(this.width
, height
);
540 * Change the width and height. Everything on-screen will be destroyed
541 * and must be redrawn.
543 * @param width new screen width
544 * @param height new screen height
546 public final void setDimensions(final int width
, final int height
) {
547 reallocate(width
, height
);
553 * @return current screen height
555 public final synchronized int getHeight() {
562 * @return current screen width
564 public final synchronized int getWidth() {
569 * Reset screen to not-bold, white-on-black. Also flushes the offset and
572 public final synchronized void reset() {
573 for (int row
= 0; row
< height
; row
++) {
574 for (int col
= 0; col
< width
; col
++) {
575 logical
[col
][row
].reset();
582 * Flush the offset and clip variables.
584 public final void resetClipping() {
594 * Clear the logical screen.
596 public final void clear() {
601 * Draw a box with a border and empty background.
603 * @param left left column of box. 0 is the left-most row.
604 * @param top top row of the box. 0 is the top-most row.
605 * @param right right column of box
606 * @param bottom bottom row of the box
607 * @param border attributes to use for the border
608 * @param background attributes to use for the background
610 public final void drawBox(final int left
, final int top
,
611 final int right
, final int bottom
,
612 final CellAttributes border
, final CellAttributes background
) {
614 drawBox(left
, top
, right
, bottom
, border
, background
, 1, false);
618 * Draw a box with a border and empty background.
620 * @param left left column of box. 0 is the left-most row.
621 * @param top top row of the box. 0 is the top-most row.
622 * @param right right column of box
623 * @param bottom bottom row of the box
624 * @param border attributes to use for the border
625 * @param background attributes to use for the background
626 * @param borderType if 1, draw a single-line border; if 2, draw a
627 * double-line border; if 3, draw double-line top/bottom edges and
628 * single-line left/right edges (like Qmodem)
629 * @param shadow if true, draw a "shadow" on the box
631 public final void drawBox(final int left
, final int top
,
632 final int right
, final int bottom
,
633 final CellAttributes border
, final CellAttributes background
,
634 final int borderType
, final boolean shadow
) {
636 int boxWidth
= right
- left
;
637 int boxHeight
= bottom
- top
;
646 switch (borderType
) {
648 cTopLeft
= GraphicsChars
.ULCORNER
;
649 cTopRight
= GraphicsChars
.URCORNER
;
650 cBottomLeft
= GraphicsChars
.LLCORNER
;
651 cBottomRight
= GraphicsChars
.LRCORNER
;
652 cHSide
= GraphicsChars
.SINGLE_BAR
;
653 cVSide
= GraphicsChars
.WINDOW_SIDE
;
657 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
658 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
659 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
660 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
661 cHSide
= GraphicsChars
.DOUBLE_BAR
;
662 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
666 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
667 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
668 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
669 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
670 cHSide
= GraphicsChars
.WINDOW_TOP
;
671 cVSide
= GraphicsChars
.WINDOW_SIDE
;
674 throw new IllegalArgumentException("Invalid border type: "
678 // Place the corner characters
679 putCharXY(left
, top
, cTopLeft
, border
);
680 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
681 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
682 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
685 // Draw the box lines
686 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
687 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
688 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
689 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
691 // Fill in the interior background
692 for (int i
= 1; i
< boxHeight
- 1; i
++) {
693 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
698 drawBoxShadow(left
, top
, right
, bottom
);
705 * @param left left column of box. 0 is the left-most row.
706 * @param top top row of the box. 0 is the top-most row.
707 * @param right right column of box
708 * @param bottom bottom row of the box
710 public final void drawBoxShadow(final int left
, final int top
,
711 final int right
, final int bottom
) {
715 int boxWidth
= right
- left
;
716 int boxHeight
= bottom
- top
;
717 CellAttributes shadowAttr
= new CellAttributes();
719 // Shadows do not honor clipping but they DO honor offset.
720 int oldClipRight
= clipRight
;
721 int oldClipBottom
= clipBottom
;
723 clipRight = boxWidth + 2;
724 clipBottom = boxHeight + 1;
729 for (int i
= 0; i
< boxHeight
; i
++) {
730 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
731 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
733 for (int i
= 0; i
< boxWidth
; i
++) {
734 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
736 clipRight
= oldClipRight
;
737 clipBottom
= oldClipBottom
;
741 * Default implementation does nothing.
743 public void flushPhysical() {}
746 * Put the cursor at (x,y).
748 * @param visible if true, the cursor should be visible
749 * @param x column coordinate to put the cursor on
750 * @param y row coordinate to put the cursor on
752 public void putCursor(final boolean visible
, final int x
, final int y
) {
755 && (cursorY
<= height
- 1)
756 && (cursorX
<= width
- 1)
758 // Make the current cursor position dirty
759 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
760 physical
[cursorX
][cursorY
].setChar('X');
762 physical
[cursorX
][cursorY
].setChar('Q');
766 cursorVisible
= visible
;
774 public final void hideCursor() {
775 cursorVisible
= false;
779 * Get the cursor visibility.
781 * @return true if the cursor is visible
783 public boolean isCursorVisible() {
784 return cursorVisible
;
788 * Get the cursor X position.
790 * @return the cursor x column position
792 public int getCursorX() {
797 * Get the cursor Y position.
799 * @return the cursor y row position
801 public int getCursorY() {
806 * Set the window title. Default implementation does nothing.
808 * @param title the new title
810 public void setTitle(final String title
) {}
812 // ------------------------------------------------------------------------
813 // LogicalScreen ----------------------------------------------------------
814 // ------------------------------------------------------------------------
817 * Reallocate screen buffers.
819 * @param width new width
820 * @param height new height
822 private synchronized void reallocate(final int width
, final int height
) {
823 if (logical
!= null) {
824 for (int row
= 0; row
< this.height
; row
++) {
825 for (int col
= 0; col
< this.width
; col
++) {
826 logical
[col
][row
] = null;
831 logical
= new Cell
[width
][height
];
832 if (physical
!= null) {
833 for (int row
= 0; row
< this.height
; row
++) {
834 for (int col
= 0; col
< this.width
; col
++) {
835 physical
[col
][row
] = null;
840 physical
= new Cell
[width
][height
];
842 for (int row
= 0; row
< height
; row
++) {
843 for (int col
= 0; col
< width
; col
++) {
844 physical
[col
][row
] = new Cell();
845 logical
[col
][row
] = new Cell();
850 this.height
= height
;
857 reallyCleared
= true;
861 * Clear the physical screen.
863 public final void clearPhysical() {
864 for (int row
= 0; row
< height
; row
++) {
865 for (int col
= 0; col
< width
; col
++) {
866 physical
[col
][row
].reset();