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
{
41 * Width of the visible window.
46 * Height of the visible window.
51 * Drawing offset for x.
56 * Set drawing offset for x.
58 * @param offsetX new drawing offset
60 public final void setOffsetX(final int offsetX
) {
61 this.offsetX
= offsetX
;
65 * Drawing offset for y.
70 * Set drawing offset for y.
72 * @param offsetY new drawing offset
74 public final void setOffsetY(final int offsetY
) {
75 this.offsetY
= offsetY
;
79 * Ignore anything drawn right of clipRight.
81 private int clipRight
;
84 * Get right drawing clipping boundary.
86 * @return drawing boundary
88 public final int getClipRight() {
93 * Set right drawing clipping boundary.
95 * @param clipRight new boundary
97 public final void setClipRight(final int clipRight
) {
98 this.clipRight
= clipRight
;
102 * Ignore anything drawn below clipBottom.
104 private int clipBottom
;
107 * Get bottom drawing clipping boundary.
109 * @return drawing boundary
111 public final int getClipBottom() {
116 * Set bottom drawing clipping boundary.
118 * @param clipBottom new boundary
120 public final void setClipBottom(final int clipBottom
) {
121 this.clipBottom
= clipBottom
;
125 * Ignore anything drawn left of clipLeft.
127 private int clipLeft
;
130 * Get left drawing clipping boundary.
132 * @return drawing boundary
134 public final int getClipLeft() {
139 * Set left drawing clipping boundary.
141 * @param clipLeft new boundary
143 public final void setClipLeft(final int clipLeft
) {
144 this.clipLeft
= clipLeft
;
148 * Ignore anything drawn above clipTop.
153 * Get top drawing clipping boundary.
155 * @return drawing boundary
157 public final int getClipTop() {
162 * Set top drawing clipping boundary.
164 * @param clipTop new boundary
166 public final void setClipTop(final int clipTop
) {
167 this.clipTop
= clipTop
;
171 * The physical screen last sent out on flush().
173 protected Cell
[][] physical
;
176 * The logical screen being rendered to.
178 protected Cell
[][] logical
;
183 * @return if true, the logical screen is not in sync with the physical
186 public final boolean isDirty() {
187 for (int x
= 0; x
< width
; x
++) {
188 for (int y
= 0; y
< height
; y
++) {
189 if (!logical
[x
][y
].equals(physical
[x
][y
])) {
192 if (logical
[x
][y
].isBlink()) {
193 // Blinking screens are always dirty. There is
194 // opportunity for a Netscape blink tag joke here...
204 * Set if the user explicitly wants to redraw everything starting with a
205 * ECMATerminal.clearAll().
207 protected boolean reallyCleared
;
210 * If true, the cursor is visible and should be placed onscreen at
211 * (cursorX, cursorY) during a call to flushPhysical().
213 protected boolean cursorVisible
;
216 * Cursor X position if visible.
218 protected int cursorX
;
221 * Cursor Y position if visible.
223 protected int cursorY
;
226 * Get the attributes at one location.
228 * @param x column coordinate. 0 is the left-most column.
229 * @param y row coordinate. 0 is the top-most row.
230 * @return attributes at (x, y)
232 public final CellAttributes
getAttrXY(final int x
, final int y
) {
233 CellAttributes attr
= new CellAttributes();
234 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
235 attr
.setTo(logical
[x
][y
]);
241 * Get the cell at one location.
243 * @param x column coordinate. 0 is the left-most column.
244 * @param y row coordinate. 0 is the top-most row.
245 * @return the character + attributes
247 public Cell
getCharXY(final int x
, final int y
) {
248 Cell cell
= new Cell();
249 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
250 cell
.setTo(logical
[x
][y
]);
256 * Set the attributes at one location.
258 * @param x column coordinate. 0 is the left-most column.
259 * @param y row coordinate. 0 is the top-most row.
260 * @param attr attributes to use (bold, foreColor, backColor)
262 public final void putAttrXY(final int x
, final int y
,
263 final CellAttributes attr
) {
265 putAttrXY(x
, y
, attr
, true);
269 * Set the attributes at one location.
271 * @param x column coordinate. 0 is the left-most column.
272 * @param y row coordinate. 0 is the top-most row.
273 * @param attr attributes to use (bold, foreColor, backColor)
274 * @param clip if true, honor clipping/offset
276 public final void putAttrXY(final int x
, final int y
,
277 final CellAttributes attr
, final boolean clip
) {
294 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
295 logical
[X
][Y
].setTo(attr
);
300 * Fill the entire screen with one character with attributes.
302 * @param ch character to draw
303 * @param attr attributes to use (bold, foreColor, backColor)
305 public final void putAll(final char ch
, final CellAttributes attr
) {
307 for (int x
= 0; x
< width
; x
++) {
308 for (int y
= 0; y
< height
; y
++) {
309 putCharXY(x
, y
, ch
, attr
);
315 * Render one character with attributes.
317 * @param x column coordinate. 0 is the left-most column.
318 * @param y row coordinate. 0 is the top-most row.
319 * @param ch character + attributes to draw
321 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
322 putCharXY(x
, y
, ch
.getChar(), ch
);
326 * Render one character with attributes.
328 * @param x column coordinate. 0 is the left-most column.
329 * @param y row coordinate. 0 is the top-most row.
330 * @param ch character to draw
331 * @param attr attributes to use (bold, foreColor, backColor)
333 public final void putCharXY(final int x
, final int y
, final char ch
,
334 final CellAttributes attr
) {
347 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
349 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
351 // Do not put control characters on the display
355 logical
[X
][Y
].setTo(attr
);
356 logical
[X
][Y
].setChar(ch
);
361 * Render one character without changing the underlying 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
367 public final void putCharXY(final int x
, final int y
, final char ch
) {
380 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
382 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
383 logical
[X
][Y
].setChar(ch
);
388 * Render a string. Does not wrap if the string exceeds the line.
390 * @param x column coordinate. 0 is the left-most column.
391 * @param y row coordinate. 0 is the top-most row.
392 * @param str string to draw
393 * @param attr attributes to use (bold, foreColor, backColor)
395 public final void putStringXY(final int x
, final int y
, final String str
,
396 final CellAttributes attr
) {
399 for (int j
= 0; j
< str
.length(); j
++) {
400 char ch
= str
.charAt(j
);
401 putCharXY(i
, y
, ch
, attr
);
410 * Render a string without changing the underlying attribute. Does not
411 * wrap if the string exceeds the line.
413 * @param x column coordinate. 0 is the left-most column.
414 * @param y row coordinate. 0 is the top-most row.
415 * @param str string to draw
417 public final void putStringXY(final int x
, final int y
, final String str
) {
420 for (int j
= 0; j
< str
.length(); j
++) {
421 char ch
= str
.charAt(j
);
431 * Draw a vertical line from (x, y) to (x, y + n).
433 * @param x column coordinate. 0 is the left-most column.
434 * @param y row coordinate. 0 is the top-most row.
435 * @param n number of characters to draw
436 * @param ch character to draw
437 * @param attr attributes to use (bold, foreColor, backColor)
439 public final void vLineXY(final int x
, final int y
, final int n
,
440 final char ch
, final CellAttributes attr
) {
442 for (int i
= y
; i
< y
+ n
; i
++) {
443 putCharXY(x
, i
, ch
, attr
);
448 * Draw a horizontal line from (x, y) to (x + n, y).
450 * @param x column coordinate. 0 is the left-most column.
451 * @param y row coordinate. 0 is the top-most row.
452 * @param n number of characters to draw
453 * @param ch character to draw
454 * @param attr attributes to use (bold, foreColor, backColor)
456 public final void hLineXY(final int x
, final int y
, final int n
,
457 final char ch
, final CellAttributes attr
) {
459 for (int i
= x
; i
< x
+ n
; i
++) {
460 putCharXY(i
, y
, ch
, attr
);
465 * Reallocate screen buffers.
467 * @param width new width
468 * @param height new height
470 private synchronized void reallocate(final int width
, final int height
) {
471 if (logical
!= null) {
472 for (int row
= 0; row
< this.height
; row
++) {
473 for (int col
= 0; col
< this.width
; col
++) {
474 logical
[col
][row
] = null;
479 logical
= new Cell
[width
][height
];
480 if (physical
!= null) {
481 for (int row
= 0; row
< this.height
; row
++) {
482 for (int col
= 0; col
< this.width
; col
++) {
483 physical
[col
][row
] = null;
488 physical
= new Cell
[width
][height
];
490 for (int row
= 0; row
< height
; row
++) {
491 for (int col
= 0; col
< width
; col
++) {
492 physical
[col
][row
] = new Cell();
493 logical
[col
][row
] = new Cell();
498 this.height
= height
;
505 reallyCleared
= true;
509 * Change the width. Everything on-screen will be destroyed and must be
512 * @param width new screen width
514 public final synchronized void setWidth(final int width
) {
515 reallocate(width
, this.height
);
519 * Change the height. Everything on-screen will be destroyed and must be
522 * @param height new screen height
524 public final synchronized void setHeight(final int height
) {
525 reallocate(this.width
, height
);
529 * Change the width and height. Everything on-screen will be destroyed
530 * and must be redrawn.
532 * @param width new screen width
533 * @param height new screen height
535 public final void setDimensions(final int width
, final int height
) {
536 reallocate(width
, height
);
542 * @return current screen height
544 public final synchronized int getHeight() {
551 * @return current screen width
553 public final synchronized int getWidth() {
558 * Public constructor. Sets everything to not-bold, white-on-black.
560 protected LogicalScreen() {
567 reallocate(width
, height
);
571 * Reset screen to not-bold, white-on-black. Also flushes the offset and
574 public final synchronized void reset() {
575 for (int row
= 0; row
< height
; row
++) {
576 for (int col
= 0; col
< width
; col
++) {
577 logical
[col
][row
].reset();
584 * Flush the offset and clip variables.
586 public final void resetClipping() {
596 * Clear the logical screen.
598 public final void clear() {
603 * Clear the physical screen.
605 public final void clearPhysical() {
606 for (int row
= 0; row
< height
; row
++) {
607 for (int col
= 0; col
< width
; col
++) {
608 physical
[col
][row
].reset();
614 * Draw a box with a border and empty background.
616 * @param left left column of box. 0 is the left-most row.
617 * @param top top row of the box. 0 is the top-most row.
618 * @param right right column of box
619 * @param bottom bottom row of the box
620 * @param border attributes to use for the border
621 * @param background attributes to use for the background
623 public final void drawBox(final int left
, final int top
,
624 final int right
, final int bottom
,
625 final CellAttributes border
, final CellAttributes background
) {
627 drawBox(left
, top
, right
, bottom
, border
, background
, 1, false);
631 * Draw a box with a border and empty background.
633 * @param left left column of box. 0 is the left-most row.
634 * @param top top row of the box. 0 is the top-most row.
635 * @param right right column of box
636 * @param bottom bottom row of the box
637 * @param border attributes to use for the border
638 * @param background attributes to use for the background
639 * @param borderType if 1, draw a single-line border; if 2, draw a
640 * double-line border; if 3, draw double-line top/bottom edges and
641 * single-line left/right edges (like Qmodem)
642 * @param shadow if true, draw a "shadow" on the box
644 public final void drawBox(final int left
, final int top
,
645 final int right
, final int bottom
,
646 final CellAttributes border
, final CellAttributes background
,
647 final int borderType
, final boolean shadow
) {
649 int boxWidth
= right
- left
;
650 int boxHeight
= bottom
- top
;
659 switch (borderType
) {
661 cTopLeft
= GraphicsChars
.ULCORNER
;
662 cTopRight
= GraphicsChars
.URCORNER
;
663 cBottomLeft
= GraphicsChars
.LLCORNER
;
664 cBottomRight
= GraphicsChars
.LRCORNER
;
665 cHSide
= GraphicsChars
.SINGLE_BAR
;
666 cVSide
= GraphicsChars
.WINDOW_SIDE
;
670 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
671 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
672 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
673 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
674 cHSide
= GraphicsChars
.DOUBLE_BAR
;
675 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
679 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
680 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
681 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
682 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
683 cHSide
= GraphicsChars
.WINDOW_TOP
;
684 cVSide
= GraphicsChars
.WINDOW_SIDE
;
687 throw new IllegalArgumentException("Invalid border type: "
691 // Place the corner characters
692 putCharXY(left
, top
, cTopLeft
, border
);
693 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
694 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
695 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
698 // Draw the box lines
699 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
700 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
701 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
702 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
704 // Fill in the interior background
705 for (int i
= 1; i
< boxHeight
- 1; i
++) {
706 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
711 drawBoxShadow(left
, top
, right
, bottom
);
718 * @param left left column of box. 0 is the left-most row.
719 * @param top top row of the box. 0 is the top-most row.
720 * @param right right column of box
721 * @param bottom bottom row of the box
723 public final void drawBoxShadow(final int left
, final int top
,
724 final int right
, final int bottom
) {
728 int boxWidth
= right
- left
;
729 int boxHeight
= bottom
- top
;
730 CellAttributes shadowAttr
= new CellAttributes();
732 // Shadows do not honor clipping but they DO honor offset.
733 int oldClipRight
= clipRight
;
734 int oldClipBottom
= clipBottom
;
736 clipRight = boxWidth + 2;
737 clipBottom = boxHeight + 1;
742 for (int i
= 0; i
< boxHeight
; i
++) {
743 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
744 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
746 for (int i
= 0; i
< boxWidth
; i
++) {
747 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
749 clipRight
= oldClipRight
;
750 clipBottom
= oldClipBottom
;
754 * Default implementation does nothing.
756 public void flushPhysical() {}
759 * Put the cursor at (x,y).
761 * @param visible if true, the cursor should be visible
762 * @param x column coordinate to put the cursor on
763 * @param y row coordinate to put the cursor on
765 public void putCursor(final boolean visible
, final int x
, final int y
) {
768 && (cursorY
<= height
- 1)
769 && (cursorX
<= width
- 1)
771 // Make the current cursor position dirty
772 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
773 physical
[cursorX
][cursorY
].setChar('X');
775 physical
[cursorX
][cursorY
].setChar('Q');
779 cursorVisible
= visible
;
787 public final void hideCursor() {
788 cursorVisible
= false;
792 * Get the cursor visibility.
794 * @return true if the cursor is visible
796 public boolean isCursorVisible() {
797 return cursorVisible
;
801 * Get the cursor X position.
803 * @return the cursor x column position
805 public int getCursorX() {
810 * Get the cursor Y position.
812 * @return the cursor y row position
814 public int getCursorY() {
819 * Set the window title. Default implementation does nothing.
821 * @param title the new title
823 public void setTitle(final String title
) {}