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
;
181 * When true, logical != physical.
183 protected volatile boolean dirty
;
188 * @return if true, the logical screen is not in sync with the physical
191 public final boolean isDirty() {
196 * Set if the user explicitly wants to redraw everything starting with a
197 * ECMATerminal.clearAll().
199 protected boolean reallyCleared
;
202 * If true, the cursor is visible and should be placed onscreen at
203 * (cursorX, cursorY) during a call to flushPhysical().
205 protected boolean cursorVisible
;
208 * Cursor X position if visible.
210 protected int cursorX
;
213 * Cursor Y position if visible.
215 protected int cursorY
;
218 * Get the attributes at one location.
220 * @param x column coordinate. 0 is the left-most column.
221 * @param y row coordinate. 0 is the top-most row.
222 * @return attributes at (x, y)
224 public final CellAttributes
getAttrXY(final int x
, final int y
) {
225 CellAttributes attr
= new CellAttributes();
226 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
227 attr
.setTo(logical
[x
][y
]);
233 * Set the attributes at one location.
235 * @param x column coordinate. 0 is the left-most column.
236 * @param y row coordinate. 0 is the top-most row.
237 * @param attr attributes to use (bold, foreColor, backColor)
239 public final void putAttrXY(final int x
, final int y
,
240 final CellAttributes attr
) {
242 putAttrXY(x
, y
, attr
, true);
246 * Set the attributes at one location.
248 * @param x column coordinate. 0 is the left-most column.
249 * @param y row coordinate. 0 is the top-most row.
250 * @param attr attributes to use (bold, foreColor, backColor)
251 * @param clip if true, honor clipping/offset
253 public final void putAttrXY(final int x
, final int y
,
254 final CellAttributes attr
, final boolean clip
) {
271 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
273 logical
[X
][Y
].setForeColor(attr
.getForeColor());
274 logical
[X
][Y
].setBackColor(attr
.getBackColor());
275 logical
[X
][Y
].setBold(attr
.isBold());
276 logical
[X
][Y
].setBlink(attr
.isBlink());
277 logical
[X
][Y
].setReverse(attr
.isReverse());
278 logical
[X
][Y
].setUnderline(attr
.isUnderline());
279 logical
[X
][Y
].setProtect(attr
.isProtect());
284 * Fill the entire screen with one character with attributes.
286 * @param ch character to draw
287 * @param attr attributes to use (bold, foreColor, backColor)
289 public final void putAll(final char ch
, final CellAttributes attr
) {
291 for (int x
= 0; x
< width
; x
++) {
292 for (int y
= 0; y
< height
; y
++) {
293 putCharXY(x
, y
, ch
, attr
);
299 * Render one character with attributes.
301 * @param x column coordinate. 0 is the left-most column.
302 * @param y row coordinate. 0 is the top-most row.
303 * @param ch character + attributes to draw
305 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
306 putCharXY(x
, y
, ch
.getChar(), ch
);
310 * Render one character with attributes.
312 * @param x column coordinate. 0 is the left-most column.
313 * @param y row coordinate. 0 is the top-most row.
314 * @param ch character to draw
315 * @param attr attributes to use (bold, foreColor, backColor)
317 public final void putCharXY(final int x
, final int y
, final char ch
,
318 final CellAttributes attr
) {
331 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
333 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
336 // Do not put control characters on the display
340 logical
[X
][Y
].setChar(ch
);
341 logical
[X
][Y
].setForeColor(attr
.getForeColor());
342 logical
[X
][Y
].setBackColor(attr
.getBackColor());
343 logical
[X
][Y
].setBold(attr
.isBold());
344 logical
[X
][Y
].setBlink(attr
.isBlink());
345 logical
[X
][Y
].setReverse(attr
.isReverse());
346 logical
[X
][Y
].setUnderline(attr
.isUnderline());
347 logical
[X
][Y
].setProtect(attr
.isProtect());
352 * Render one character without changing the underlying attributes.
354 * @param x column coordinate. 0 is the left-most column.
355 * @param y row coordinate. 0 is the top-most row.
356 * @param ch character to draw
358 public final void putCharXY(final int x
, final int y
, final char ch
) {
371 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
373 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
375 logical
[X
][Y
].setChar(ch
);
380 * Render a string. Does not wrap if the string exceeds the line.
382 * @param x column coordinate. 0 is the left-most column.
383 * @param y row coordinate. 0 is the top-most row.
384 * @param str string to draw
385 * @param attr attributes to use (bold, foreColor, backColor)
387 public final void putStringXY(final int x
, final int y
, final String str
,
388 final CellAttributes attr
) {
391 for (int j
= 0; j
< str
.length(); j
++) {
392 char ch
= str
.charAt(j
);
393 putCharXY(i
, y
, ch
, attr
);
402 * Render a string without changing the underlying attribute. Does not
403 * wrap if the string exceeds the line.
405 * @param x column coordinate. 0 is the left-most column.
406 * @param y row coordinate. 0 is the top-most row.
407 * @param str string to draw
409 public final void putStringXY(final int x
, final int y
, final String str
) {
412 for (int j
= 0; j
< str
.length(); j
++) {
413 char ch
= str
.charAt(j
);
423 * Draw a vertical line from (x, y) to (x, y + n).
425 * @param x column coordinate. 0 is the left-most column.
426 * @param y row coordinate. 0 is the top-most row.
427 * @param n number of characters to draw
428 * @param ch character to draw
429 * @param attr attributes to use (bold, foreColor, backColor)
431 public final void vLineXY(final int x
, final int y
, final int n
,
432 final char ch
, final CellAttributes attr
) {
434 for (int i
= y
; i
< y
+ n
; i
++) {
435 putCharXY(x
, i
, ch
, attr
);
440 * Draw a horizontal line from (x, y) to (x + n, y).
442 * @param x column coordinate. 0 is the left-most column.
443 * @param y row coordinate. 0 is the top-most row.
444 * @param n number of characters to draw
445 * @param ch character to draw
446 * @param attr attributes to use (bold, foreColor, backColor)
448 public final void hLineXY(final int x
, final int y
, final int n
,
449 final char ch
, final CellAttributes attr
) {
451 for (int i
= x
; i
< x
+ n
; i
++) {
452 putCharXY(i
, y
, ch
, attr
);
457 * Reallocate screen buffers.
459 * @param width new width
460 * @param height new height
462 private synchronized void reallocate(final int width
, final int height
) {
463 if (logical
!= null) {
464 for (int row
= 0; row
< this.height
; row
++) {
465 for (int col
= 0; col
< this.width
; col
++) {
466 logical
[col
][row
] = null;
471 logical
= new Cell
[width
][height
];
472 if (physical
!= null) {
473 for (int row
= 0; row
< this.height
; row
++) {
474 for (int col
= 0; col
< this.width
; col
++) {
475 physical
[col
][row
] = null;
480 physical
= new Cell
[width
][height
];
482 for (int row
= 0; row
< height
; row
++) {
483 for (int col
= 0; col
< width
; col
++) {
484 physical
[col
][row
] = new Cell();
485 logical
[col
][row
] = new Cell();
490 this.height
= height
;
497 reallyCleared
= true;
502 * Change the width. Everything on-screen will be destroyed and must be
505 * @param width new screen width
507 public final synchronized void setWidth(final int width
) {
508 reallocate(width
, this.height
);
512 * Change the height. Everything on-screen will be destroyed and must be
515 * @param height new screen height
517 public final synchronized void setHeight(final int height
) {
518 reallocate(this.width
, height
);
522 * Change the width and height. Everything on-screen will be destroyed
523 * and must be redrawn.
525 * @param width new screen width
526 * @param height new screen height
528 public final void setDimensions(final int width
, final int height
) {
529 reallocate(width
, height
);
535 * @return current screen height
537 public final synchronized int getHeight() {
544 * @return current screen width
546 public final synchronized int getWidth() {
551 * Public constructor. Sets everything to not-bold, white-on-black.
553 protected LogicalScreen() {
560 reallocate(width
, height
);
564 * Reset screen to not-bold, white-on-black. Also flushes the offset and
567 public final synchronized void reset() {
569 for (int row
= 0; row
< height
; row
++) {
570 for (int col
= 0; col
< width
; col
++) {
571 logical
[col
][row
].reset();
578 * Flush the offset and clip variables.
580 public final void resetClipping() {
590 * Clear the logical screen.
592 public final void clear() {
597 * Clear the physical screen.
599 public final void clearPhysical() {
601 for (int row
= 0; row
< height
; row
++) {
602 for (int col
= 0; col
< width
; col
++) {
603 physical
[col
][row
].reset();
609 * Draw a box with a border and empty background.
611 * @param left left column of box. 0 is the left-most row.
612 * @param top top row of the box. 0 is the top-most row.
613 * @param right right column of box
614 * @param bottom bottom row of the box
615 * @param border attributes to use for the border
616 * @param background attributes to use for the background
618 public final void drawBox(final int left
, final int top
,
619 final int right
, final int bottom
,
620 final CellAttributes border
, final CellAttributes background
) {
622 drawBox(left
, top
, right
, bottom
, border
, background
, 1, false);
626 * Draw a box with a border and empty background.
628 * @param left left column of box. 0 is the left-most row.
629 * @param top top row of the box. 0 is the top-most row.
630 * @param right right column of box
631 * @param bottom bottom row of the box
632 * @param border attributes to use for the border
633 * @param background attributes to use for the background
634 * @param borderType if 1, draw a single-line border; if 2, draw a
635 * double-line border; if 3, draw double-line top/bottom edges and
636 * single-line left/right edges (like Qmodem)
637 * @param shadow if true, draw a "shadow" on the box
639 public final void drawBox(final int left
, final int top
,
640 final int right
, final int bottom
,
641 final CellAttributes border
, final CellAttributes background
,
642 final int borderType
, final boolean shadow
) {
644 int boxWidth
= right
- left
;
645 int boxHeight
= bottom
- top
;
654 switch (borderType
) {
656 cTopLeft
= GraphicsChars
.ULCORNER
;
657 cTopRight
= GraphicsChars
.URCORNER
;
658 cBottomLeft
= GraphicsChars
.LLCORNER
;
659 cBottomRight
= GraphicsChars
.LRCORNER
;
660 cHSide
= GraphicsChars
.SINGLE_BAR
;
661 cVSide
= GraphicsChars
.WINDOW_SIDE
;
665 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
666 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
667 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
668 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
669 cHSide
= GraphicsChars
.DOUBLE_BAR
;
670 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
674 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
675 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
676 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
677 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
678 cHSide
= GraphicsChars
.WINDOW_TOP
;
679 cVSide
= GraphicsChars
.WINDOW_SIDE
;
682 throw new IllegalArgumentException("Invalid border type: "
686 // Place the corner characters
687 putCharXY(left
, top
, cTopLeft
, border
);
688 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
689 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
690 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
693 // Draw the box lines
694 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
695 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
696 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
697 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
699 // Fill in the interior background
700 for (int i
= 1; i
< boxHeight
- 1; i
++) {
701 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
706 drawBoxShadow(left
, top
, right
, bottom
);
713 * @param left left column of box. 0 is the left-most row.
714 * @param top top row of the box. 0 is the top-most row.
715 * @param right right column of box
716 * @param bottom bottom row of the box
718 public final void drawBoxShadow(final int left
, final int top
,
719 final int right
, final int bottom
) {
723 int boxWidth
= right
- left
;
724 int boxHeight
= bottom
- top
;
725 CellAttributes shadowAttr
= new CellAttributes();
727 // Shadows do not honor clipping but they DO honor offset.
728 int oldClipRight
= clipRight
;
729 int oldClipBottom
= clipBottom
;
731 clipRight = boxWidth + 2;
732 clipBottom = boxHeight + 1;
737 for (int i
= 0; i
< boxHeight
; i
++) {
738 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
739 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
741 for (int i
= 0; i
< boxWidth
; i
++) {
742 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
744 clipRight
= oldClipRight
;
745 clipBottom
= oldClipBottom
;
749 * Default implementation does nothing.
751 public void flushPhysical() {}
754 * Put the cursor at (x,y).
756 * @param visible if true, the cursor should be visible
757 * @param x column coordinate to put the cursor on
758 * @param y row coordinate to put the cursor on
760 public void putCursor(final boolean visible
, final int x
, final int y
) {
762 cursorVisible
= visible
;
770 public final void hideCursor() {
771 cursorVisible
= false;
775 * Set the window title. Default implementation does nothing.
777 * @param title the new title
779 public void setTitle(final String title
) {}