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
;
189 * Set if the user explicitly wants to redraw everything starting with a
190 * ECMATerminal.clearAll().
192 protected boolean reallyCleared
;
195 * If true, the cursor is visible and should be placed onscreen at
196 * (cursorX, cursorY) during a call to flushPhysical().
198 protected boolean cursorVisible
;
201 * Cursor X position if visible.
203 protected int cursorX
;
206 * Cursor Y position if visible.
208 protected int cursorY
;
211 * Get the attributes at one location.
213 * @param x column coordinate. 0 is the left-most column.
214 * @param y row coordinate. 0 is the top-most row.
215 * @return attributes at (x, y)
217 public final CellAttributes
getAttrXY(final int x
, final int y
) {
219 CellAttributes attr
= new CellAttributes();
220 if ((x
>= 0) && (x
< width
) && (y
>= 0) && (y
< height
)) {
221 attr
.setTo(logical
[x
][y
]);
227 * Set the attributes at one location.
229 * @param x column coordinate. 0 is the left-most column.
230 * @param y row coordinate. 0 is the top-most row.
231 * @param attr attributes to use (bold, foreColor, backColor)
233 public final void putAttrXY(final int x
, final int y
,
234 final CellAttributes attr
) {
236 putAttrXY(x
, y
, attr
, true);
240 * Set the attributes at one location.
242 * @param x column coordinate. 0 is the left-most column.
243 * @param y row coordinate. 0 is the top-most row.
244 * @param attr attributes to use (bold, foreColor, backColor)
245 * @param clip if true, honor clipping/offset
247 public final void putAttrXY(final int x
, final int y
248 , final CellAttributes attr
, final boolean clip
) {
265 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
267 logical
[X
][Y
].setForeColor(attr
.getForeColor());
268 logical
[X
][Y
].setBackColor(attr
.getBackColor());
269 logical
[X
][Y
].setBold(attr
.getBold());
270 logical
[X
][Y
].setBlink(attr
.getBlink());
271 logical
[X
][Y
].setReverse(attr
.getReverse());
272 logical
[X
][Y
].setUnderline(attr
.getUnderline());
273 logical
[X
][Y
].setProtect(attr
.getProtect());
278 * Fill the entire screen with one character with attributes.
280 * @param ch character to draw
281 * @param attr attributes to use (bold, foreColor, backColor)
283 public final void putAll(final char ch
, final CellAttributes attr
) {
285 for (int x
= 0; x
< width
; x
++) {
286 for (int y
= 0; y
< height
; y
++) {
287 putCharXY(x
, y
, ch
, attr
);
293 * Render one character with attributes.
295 * @param x column coordinate. 0 is the left-most column.
296 * @param y row coordinate. 0 is the top-most row.
297 * @param ch character + attributes to draw
299 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
300 putCharXY(x
, y
, ch
.getChar(), ch
);
304 * Render one character with attributes.
306 * @param x column coordinate. 0 is the left-most column.
307 * @param y row coordinate. 0 is the top-most row.
308 * @param ch character to draw
309 * @param attr attributes to use (bold, foreColor, backColor)
311 public final void putCharXY(final int x
, final int y
, final char ch
,
312 final CellAttributes attr
) {
325 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
327 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
330 // Do not put control characters on the display
334 logical
[X
][Y
].setChar(ch
);
335 logical
[X
][Y
].setForeColor(attr
.getForeColor());
336 logical
[X
][Y
].setBackColor(attr
.getBackColor());
337 logical
[X
][Y
].setBold(attr
.getBold());
338 logical
[X
][Y
].setBlink(attr
.getBlink());
339 logical
[X
][Y
].setReverse(attr
.getReverse());
340 logical
[X
][Y
].setUnderline(attr
.getUnderline());
341 logical
[X
][Y
].setProtect(attr
.getProtect());
346 * Render one character without changing the underlying attributes.
348 * @param x column coordinate. 0 is the left-most column.
349 * @param y row coordinate. 0 is the top-most row.
350 * @param ch character to draw
352 public final void putCharXY(final int x
, final int y
, final char ch
) {
365 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
367 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
369 logical
[X
][Y
].setChar(ch
);
374 * Render a string. Does not wrap if the string exceeds the line.
376 * @param x column coordinate. 0 is the left-most column.
377 * @param y row coordinate. 0 is the top-most row.
378 * @param str string to draw
379 * @param attr attributes to use (bold, foreColor, backColor)
381 public final void putStrXY(final int x
, final int y
, final String str
,
382 final CellAttributes attr
) {
385 for (int j
= 0; j
< str
.length(); j
++) {
386 char ch
= str
.charAt(j
);
387 putCharXY(i
, y
, ch
, attr
);
396 * Render a string without changing the underlying attribute. Does not
397 * wrap if the string exceeds the line.
399 * @param x column coordinate. 0 is the left-most column.
400 * @param y row coordinate. 0 is the top-most row.
401 * @param str string to draw
403 public final void putStrXY(final int x
, final int y
, final String str
) {
406 for (int j
= 0; j
< str
.length(); j
++) {
407 char ch
= str
.charAt(j
);
417 * Draw a vertical line from (x, y) to (x, y + n).
419 * @param x column coordinate. 0 is the left-most column.
420 * @param y row coordinate. 0 is the top-most row.
421 * @param n number of characters to draw
422 * @param ch character to draw
423 * @param attr attributes to use (bold, foreColor, backColor)
425 public final void vLineXY(final int x
, final int y
, final int n
,
426 final char ch
, final CellAttributes attr
) {
428 for (int i
= y
; i
< y
+ n
; i
++) {
429 putCharXY(x
, i
, ch
, attr
);
434 * Draw a horizontal line from (x, y) to (x + n, y).
436 * @param x column coordinate. 0 is the left-most column.
437 * @param y row coordinate. 0 is the top-most row.
438 * @param n number of characters to draw
439 * @param ch character to draw
440 * @param attr attributes to use (bold, foreColor, backColor)
442 public final void hLineXY(final int x
, final int y
, final int n
,
443 final char ch
, final CellAttributes attr
) {
445 for (int i
= x
; i
< x
+ n
; i
++) {
446 putCharXY(i
, y
, ch
, attr
);
451 * Reallocate screen buffers.
453 * @param width new width
454 * @param height new height
456 private synchronized void reallocate(final int width
, final int height
) {
457 if (logical
!= null) {
458 for (int row
= 0; row
< this.height
; row
++) {
459 for (int col
= 0; col
< this.width
; col
++) {
460 logical
[col
][row
] = null;
465 logical
= new Cell
[width
][height
];
466 if (physical
!= null) {
467 for (int row
= 0; row
< this.height
; row
++) {
468 for (int col
= 0; col
< this.width
; col
++) {
469 physical
[col
][row
] = null;
474 physical
= new Cell
[width
][height
];
476 for (int row
= 0; row
< height
; row
++) {
477 for (int col
= 0; col
< width
; col
++) {
478 physical
[col
][row
] = new Cell();
479 logical
[col
][row
] = new Cell();
484 this.height
= height
;
491 reallyCleared
= true;
496 * Change the width. Everything on-screen will be destroyed and must be
499 * @param width new screen width
501 public final synchronized void setWidth(final int width
) {
502 reallocate(width
, this.height
);
506 * Change the height. Everything on-screen will be destroyed and must be
509 * @param height new screen height
511 public final synchronized void setHeight(final int height
) {
512 reallocate(this.width
, height
);
516 * Change the width and height. Everything on-screen will be destroyed
517 * and must be redrawn.
519 * @param width new screen width
520 * @param height new screen height
522 public final void setDimensions(final int width
, final int height
) {
523 reallocate(width
, height
);
529 * @return current screen height
531 public final synchronized int getHeight() {
538 * @return current screen width
540 public final synchronized int getWidth() {
545 * Public constructor. Sets everything to not-bold, white-on-black.
554 reallocate(width
, height
);
558 * Reset screen to not-bold, white-on-black. Also flushes the offset and
561 public final synchronized void reset() {
563 for (int row
= 0; row
< height
; row
++) {
564 for (int col
= 0; col
< width
; col
++) {
565 logical
[col
][row
].reset();
572 * Flush the offset and clip variables.
574 public final void resetClipping() {
584 * Force the screen to be fully cleared and redrawn on the next flush().
586 public final void clear() {
591 * Draw a box with a border and empty background.
593 * @param left left column of box. 0 is the left-most row.
594 * @param top top row of the box. 0 is the top-most row.
595 * @param right right column of box
596 * @param bottom bottom row of the box
597 * @param border attributes to use for the border
598 * @param background attributes to use for the background
600 public final void drawBox(final int left
, final int top
,
601 final int right
, final int bottom
,
602 final CellAttributes border
, final CellAttributes background
) {
604 drawBox(left
, top
, right
, bottom
, border
, background
, 1, false);
608 * Draw a box with a border and empty background.
610 * @param left left column of box. 0 is the left-most row.
611 * @param top top row of the box. 0 is the top-most row.
612 * @param right right column of box
613 * @param bottom bottom row of the box
614 * @param border attributes to use for the border
615 * @param background attributes to use for the background
616 * @param borderType if 1, draw a single-line border; if 2, draw a
617 * double-line border; if 3, draw double-line top/bottom edges and
618 * single-line left/right edges (like Qmodem)
619 * @param shadow if true, draw a "shadow" on the box
621 public final void drawBox(final int left
, final int top
,
622 final int right
, final int bottom
,
623 final CellAttributes border
, final CellAttributes background
,
624 final int borderType
, final boolean shadow
) {
628 int boxWidth
= right
- left
;
629 int boxHeight
= bottom
- top
;
638 switch (borderType
) {
640 cTopLeft
= GraphicsChars
.ULCORNER
;
641 cTopRight
= GraphicsChars
.URCORNER
;
642 cBottomLeft
= GraphicsChars
.LLCORNER
;
643 cBottomRight
= GraphicsChars
.LRCORNER
;
644 cHSide
= GraphicsChars
.SINGLE_BAR
;
645 cVSide
= GraphicsChars
.WINDOW_SIDE
;
649 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
650 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
651 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
652 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
653 cHSide
= GraphicsChars
.DOUBLE_BAR
;
654 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
658 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
659 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
660 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
661 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
662 cHSide
= GraphicsChars
.WINDOW_TOP
;
663 cVSide
= GraphicsChars
.WINDOW_SIDE
;
666 throw new IllegalArgumentException("Invalid border type: "
670 // Place the corner characters
671 putCharXY(left
, top
, cTopLeft
, border
);
672 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
673 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
674 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
677 // Draw the box lines
678 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
679 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
680 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
681 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
683 // Fill in the interior background
684 for (int i
= 1; i
< boxHeight
- 1; i
++) {
685 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
690 drawBoxShadow(left
, top
, right
, bottom
);
697 * @param left left column of box. 0 is the left-most row.
698 * @param top top row of the box. 0 is the top-most row.
699 * @param right right column of box
700 * @param bottom bottom row of the box
702 public final void drawBoxShadow(final int left
, final int top
,
703 final int right
, final int bottom
) {
707 int boxWidth
= right
- left
;
708 int boxHeight
= bottom
- top
;
709 CellAttributes shadowAttr
= new CellAttributes();
711 // Shadows do not honor clipping but they DO honor offset.
712 int oldClipRight
= clipRight
;
713 int oldClipBottom
= clipBottom
;
715 clipRight = boxWidth + 2;
716 clipBottom = boxHeight + 1;
721 for (int i
= 0; i
< boxHeight
; i
++) {
722 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
723 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
725 for (int i
= 0; i
< boxWidth
; i
++) {
726 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
728 clipRight
= oldClipRight
;
729 clipBottom
= oldClipBottom
;
733 * Subclasses must provide an implementation to push the logical screen
734 * to the physical device.
736 public abstract void flushPhysical();
739 * Put the cursor at (x,y).
741 * @param visible if true, the cursor should be visible
742 * @param x column coordinate to put the cursor on
743 * @param y row coordinate to put the cursor on
745 public void putCursor(final boolean visible
, final int x
, final int y
) {
747 cursorVisible
= visible
;
755 public final void hideCursor() {
756 cursorVisible
= false;