2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.Cell
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.bits
.GraphicsChars
;
38 * This class represents a text-based screen. Drawing operations write to a
41 public abstract class Screen
{
44 * Width of the visible window.
49 * Height of the visible window.
54 * Drawing offset for x.
59 * Set drawing offset for x.
61 * @param offsetX new drawing offset
63 public final void setOffsetX(final int offsetX
) {
64 this.offsetX
= offsetX
;
68 * Drawing offset for y.
73 * Set drawing offset for y.
75 * @param offsetY new drawing offset
77 public final void setOffsetY(final int offsetY
) {
78 this.offsetY
= offsetY
;
82 * Ignore anything drawn right of clipRight.
84 private int clipRight
;
87 * Get right drawing clipping boundary.
89 * @return drawing boundary
91 public final int getClipRight() {
96 * Set right drawing clipping boundary.
98 * @param clipRight new boundary
100 public final void setClipRight(final int clipRight
) {
101 this.clipRight
= clipRight
;
105 * Ignore anything drawn below clipBottom.
107 private int clipBottom
;
110 * Get bottom drawing clipping boundary.
112 * @return drawing boundary
114 public final int getClipBottom() {
119 * Set bottom drawing clipping boundary.
121 * @param clipBottom new boundary
123 public final void setClipBottom(final int clipBottom
) {
124 this.clipBottom
= clipBottom
;
128 * Ignore anything drawn left of clipLeft.
130 private int clipLeft
;
133 * Get left drawing clipping boundary.
135 * @return drawing boundary
137 public final int getClipLeft() {
142 * Set left drawing clipping boundary.
144 * @param clipLeft new boundary
146 public final void setClipLeft(final int clipLeft
) {
147 this.clipLeft
= clipLeft
;
151 * Ignore anything drawn above clipTop.
156 * Get top drawing clipping boundary.
158 * @return drawing boundary
160 public final int getClipTop() {
165 * Set top drawing clipping boundary.
167 * @param clipTop new boundary
169 public final void setClipTop(final int clipTop
) {
170 this.clipTop
= clipTop
;
174 * The physical screen last sent out on flush().
176 protected Cell
[][] physical
;
179 * The logical screen being rendered to.
181 protected Cell
[][] logical
;
184 * When true, logical != physical.
186 protected boolean dirty
;
191 * @return if true, the logical screen is not in sync with the physical
194 public final boolean isDirty() {
199 * Set if the user explicitly wants to redraw everything starting with a
200 * ECMATerminal.clearAll().
202 protected boolean reallyCleared
;
205 * If true, the cursor is visible and should be placed onscreen at
206 * (cursorX, cursorY) during a call to flushPhysical().
208 protected boolean cursorVisible
;
211 * Cursor X position if visible.
213 protected int cursorX
;
216 * Cursor Y position if visible.
218 protected int cursorY
;
221 * Get the attributes at one location.
223 * @param x column coordinate. 0 is the left-most column.
224 * @param y row coordinate. 0 is the top-most row.
225 * @return attributes at (x, y)
227 public final CellAttributes
getAttrXY(final int x
, final int y
) {
229 CellAttributes attr
= new CellAttributes();
230 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
231 attr
.setTo(logical
[x
][y
]);
237 * Set the attributes at one location.
239 * @param x column coordinate. 0 is the left-most column.
240 * @param y row coordinate. 0 is the top-most row.
241 * @param attr attributes to use (bold, foreColor, backColor)
243 public final void putAttrXY(final int x
, final int y
,
244 final CellAttributes attr
) {
246 putAttrXY(x
, y
, attr
, true);
250 * Set the attributes at one location.
252 * @param x column coordinate. 0 is the left-most column.
253 * @param y row coordinate. 0 is the top-most row.
254 * @param attr attributes to use (bold, foreColor, backColor)
255 * @param clip if true, honor clipping/offset
257 public final void putAttrXY(final int x
, final int y
258 , final CellAttributes attr
, final boolean clip
) {
275 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
277 logical
[X
][Y
].setForeColor(attr
.getForeColor());
278 logical
[X
][Y
].setBackColor(attr
.getBackColor());
279 logical
[X
][Y
].setBold(attr
.getBold());
280 logical
[X
][Y
].setBlink(attr
.getBlink());
281 logical
[X
][Y
].setReverse(attr
.getReverse());
282 logical
[X
][Y
].setUnderline(attr
.getUnderline());
283 logical
[X
][Y
].setProtect(attr
.getProtect());
288 * Fill the entire screen with one character with attributes.
290 * @param ch character to draw
291 * @param attr attributes to use (bold, foreColor, backColor)
293 public final void putAll(final char ch
, final CellAttributes attr
) {
295 for (int x
= 0; x
< width
; x
++) {
296 for (int y
= 0; y
< height
; y
++) {
297 putCharXY(x
, y
, ch
, attr
);
303 * Render one character with attributes.
305 * @param x column coordinate. 0 is the left-most column.
306 * @param y row coordinate. 0 is the top-most row.
307 * @param ch character + attributes to draw
309 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
310 putCharXY(x
, y
, ch
.getChar(), ch
);
314 * Render one character with attributes.
316 * @param x column coordinate. 0 is the left-most column.
317 * @param y row coordinate. 0 is the top-most row.
318 * @param ch character to draw
319 * @param attr attributes to use (bold, foreColor, backColor)
321 public final void putCharXY(final int x
, final int y
, final char ch
,
322 final CellAttributes attr
) {
335 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
337 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
340 // Do not put control characters on the display
344 logical
[X
][Y
].setChar(ch
);
345 logical
[X
][Y
].setForeColor(attr
.getForeColor());
346 logical
[X
][Y
].setBackColor(attr
.getBackColor());
347 logical
[X
][Y
].setBold(attr
.getBold());
348 logical
[X
][Y
].setBlink(attr
.getBlink());
349 logical
[X
][Y
].setReverse(attr
.getReverse());
350 logical
[X
][Y
].setUnderline(attr
.getUnderline());
351 logical
[X
][Y
].setProtect(attr
.getProtect());
356 * Render one character without changing the underlying attributes.
358 * @param x column coordinate. 0 is the left-most column.
359 * @param y row coordinate. 0 is the top-most row.
360 * @param ch character to draw
362 public final void putCharXY(final int x
, final int y
, final char ch
) {
375 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
377 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
379 logical
[X
][Y
].setChar(ch
);
384 * Render a string. Does not wrap if the string exceeds the line.
386 * @param x column coordinate. 0 is the left-most column.
387 * @param y row coordinate. 0 is the top-most row.
388 * @param str string to draw
389 * @param attr attributes to use (bold, foreColor, backColor)
391 public final void putStrXY(final int x
, final int y
, final String str
,
392 final CellAttributes attr
) {
395 for (int j
= 0; j
< str
.length(); j
++) {
396 char ch
= str
.charAt(j
);
397 putCharXY(i
, y
, ch
, attr
);
406 * Render a string without changing the underlying attribute. Does not
407 * wrap if the string exceeds the line.
409 * @param x column coordinate. 0 is the left-most column.
410 * @param y row coordinate. 0 is the top-most row.
411 * @param str string to draw
413 public final void putStrXY(final int x
, final int y
, final String str
) {
416 for (int j
= 0; j
< str
.length(); j
++) {
417 char ch
= str
.charAt(j
);
427 * Draw a vertical line from (x, y) to (x, y + n).
429 * @param x column coordinate. 0 is the left-most column.
430 * @param y row coordinate. 0 is the top-most row.
431 * @param n number of characters to draw
432 * @param ch character to draw
433 * @param attr attributes to use (bold, foreColor, backColor)
435 public final void vLineXY(final int x
, final int y
, final int n
,
436 final char ch
, final CellAttributes attr
) {
438 for (int i
= y
; i
< y
+ n
; i
++) {
439 putCharXY(x
, i
, ch
, attr
);
444 * Draw a horizontal line from (x, y) to (x + n, y).
446 * @param x column coordinate. 0 is the left-most column.
447 * @param y row coordinate. 0 is the top-most row.
448 * @param n number of characters to draw
449 * @param ch character to draw
450 * @param attr attributes to use (bold, foreColor, backColor)
452 public final void hLineXY(final int x
, final int y
, final int n
,
453 final char ch
, final CellAttributes attr
) {
455 for (int i
= x
; i
< x
+ n
; i
++) {
456 putCharXY(i
, y
, ch
, attr
);
461 * Reallocate screen buffers.
463 * @param width new width
464 * @param height new height
466 private synchronized void reallocate(final int width
, final int height
) {
467 if (logical
!= null) {
468 for (int row
= 0; row
< this.height
; row
++) {
469 for (int col
= 0; col
< this.width
; col
++) {
470 logical
[col
][row
] = null;
475 logical
= new Cell
[width
][height
];
476 if (physical
!= null) {
477 for (int row
= 0; row
< this.height
; row
++) {
478 for (int col
= 0; col
< this.width
; col
++) {
479 physical
[col
][row
] = null;
484 physical
= new Cell
[width
][height
];
486 for (int row
= 0; row
< height
; row
++) {
487 for (int col
= 0; col
< width
; col
++) {
488 physical
[col
][row
] = new Cell();
489 logical
[col
][row
] = new Cell();
494 this.height
= height
;
501 reallyCleared
= true;
506 * Change the width. Everything on-screen will be destroyed and must be
509 * @param width new screen width
511 public final synchronized void setWidth(final int width
) {
512 reallocate(width
, this.height
);
516 * Change the height. Everything on-screen will be destroyed and must be
519 * @param height new screen height
521 public final synchronized void setHeight(final int height
) {
522 reallocate(this.width
, height
);
526 * Change the width and height. Everything on-screen will be destroyed
527 * and must be redrawn.
529 * @param width new screen width
530 * @param height new screen height
532 public final void setDimensions(final int width
, final int height
) {
533 reallocate(width
, height
);
539 * @return current screen height
541 public final synchronized int getHeight() {
548 * @return current screen width
550 public final synchronized int getWidth() {
555 * Public constructor. Sets everything to not-bold, white-on-black.
564 reallocate(width
, height
);
568 * Reset screen to not-bold, white-on-black. Also flushes the offset and
571 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 * Force the screen to be fully cleared and redrawn on the next flush().
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
) {
638 int boxWidth
= right
- left
;
639 int boxHeight
= bottom
- top
;
648 switch (borderType
) {
650 cTopLeft
= GraphicsChars
.ULCORNER
;
651 cTopRight
= GraphicsChars
.URCORNER
;
652 cBottomLeft
= GraphicsChars
.LLCORNER
;
653 cBottomRight
= GraphicsChars
.LRCORNER
;
654 cHSide
= GraphicsChars
.SINGLE_BAR
;
655 cVSide
= GraphicsChars
.WINDOW_SIDE
;
659 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
660 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
661 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
662 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
663 cHSide
= GraphicsChars
.DOUBLE_BAR
;
664 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
668 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
669 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
670 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
671 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
672 cHSide
= GraphicsChars
.WINDOW_TOP
;
673 cVSide
= GraphicsChars
.WINDOW_SIDE
;
676 throw new IllegalArgumentException("Invalid border type: "
680 // Place the corner characters
681 putCharXY(left
, top
, cTopLeft
, border
);
682 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
683 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
684 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
687 // Draw the box lines
688 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
689 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
690 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
691 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
693 // Fill in the interior background
694 for (int i
= 1; i
< boxHeight
- 1; i
++) {
695 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
700 drawBoxShadow(left
, top
, right
, bottom
);
707 * @param left left column of box. 0 is the left-most row.
708 * @param top top row of the box. 0 is the top-most row.
709 * @param right right column of box
710 * @param bottom bottom row of the box
712 public final void drawBoxShadow(final int left
, final int top
,
713 final int right
, final int bottom
) {
717 int boxWidth
= right
- left
;
718 int boxHeight
= bottom
- top
;
719 CellAttributes shadowAttr
= new CellAttributes();
721 // Shadows do not honor clipping but they DO honor offset.
722 int oldClipRight
= clipRight
;
723 int oldClipBottom
= clipBottom
;
725 clipRight = boxWidth + 2;
726 clipBottom = boxHeight + 1;
731 for (int i
= 0; i
< boxHeight
; i
++) {
732 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
733 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
735 for (int i
= 0; i
< boxWidth
; i
++) {
736 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
738 clipRight
= oldClipRight
;
739 clipBottom
= oldClipBottom
;
743 * Subclasses must provide an implementation to push the logical screen
744 * to the physical device.
746 public abstract void flushPhysical();
749 * Put the cursor at (x,y).
751 * @param visible if true, the cursor should be visible
752 * @param x column coordinate to put the cursor on
753 * @param y row coordinate to put the cursor on
755 public void putCursor(final boolean visible
, final int x
, final int y
) {
757 cursorVisible
= visible
;
765 public final void hideCursor() {
766 cursorVisible
= false;