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
) {
218 CellAttributes attr
= new CellAttributes();
219 attr
.setTo(logical
[x
][y
]);
224 * Set the attributes at one location.
226 * @param x column coordinate. 0 is the left-most column.
227 * @param y row coordinate. 0 is the top-most row.
228 * @param attr attributes to use (bold, foreColor, backColor)
230 public final void putAttrXY(final int x
, final int y
,
231 final CellAttributes attr
) {
233 putAttrXY(x
, y
, attr
, true);
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)
242 * @param clip if true, honor clipping/offset
244 public final void putAttrXY(final int x
, final int y
,
245 final CellAttributes attr
, final boolean clip
) {
262 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
264 logical
[X
][Y
].setForeColor(attr
.getForeColor());
265 logical
[X
][Y
].setBackColor(attr
.getBackColor());
266 logical
[X
][Y
].setBold(attr
.getBold());
267 logical
[X
][Y
].setBlink(attr
.getBlink());
268 logical
[X
][Y
].setReverse(attr
.getReverse());
269 logical
[X
][Y
].setUnderline(attr
.getUnderline());
270 logical
[X
][Y
].setProtect(attr
.getProtect());
275 * Fill the entire screen with one character with attributes.
277 * @param ch character to draw
278 * @param attr attributes to use (bold, foreColor, backColor)
280 public final void putAll(final char ch
, final CellAttributes attr
) {
281 for (int x
= 0; x
< width
; x
++) {
282 for (int y
= 0; y
< height
; y
++) {
283 putCharXY(x
, y
, ch
, attr
);
289 * Render one character with attributes.
291 * @param x column coordinate. 0 is the left-most column.
292 * @param y row coordinate. 0 is the top-most row.
293 * @param ch character + attributes to draw
295 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
296 putCharXY(x
, y
, ch
.getChar(), ch
);
300 * Render one character with attributes.
302 * @param x column coordinate. 0 is the left-most column.
303 * @param y row coordinate. 0 is the top-most row.
304 * @param ch character to draw
305 * @param attr attributes to use (bold, foreColor, backColor)
307 public final void putCharXY(final int x
, final int y
, final char ch
,
308 final CellAttributes attr
) {
321 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
323 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
326 // Do not put control characters on the display
330 logical
[X
][Y
].setChar(ch
);
331 logical
[X
][Y
].setForeColor(attr
.getForeColor());
332 logical
[X
][Y
].setBackColor(attr
.getBackColor());
333 logical
[X
][Y
].setBold(attr
.getBold());
334 logical
[X
][Y
].setBlink(attr
.getBlink());
335 logical
[X
][Y
].setReverse(attr
.getReverse());
336 logical
[X
][Y
].setUnderline(attr
.getUnderline());
337 logical
[X
][Y
].setProtect(attr
.getProtect());
342 * Render one character without changing the underlying attributes.
344 * @param x column coordinate. 0 is the left-most column.
345 * @param y row coordinate. 0 is the top-most row.
346 * @param ch character to draw
348 public final void putCharXY(final int x
, final int y
, final char ch
) {
360 // System.err.printf("putCharXY: %d, %d, %c\n", X, Y, ch);
362 if ((X
>= 0) && (X
< width
) && (Y
>= 0) && (Y
< height
)) {
364 logical
[X
][Y
].setChar(ch
);
369 * Render a string. Does not wrap if the string exceeds the line.
371 * @param x column coordinate. 0 is the left-most column.
372 * @param y row coordinate. 0 is the top-most row.
373 * @param str string to draw
374 * @param attr attributes to use (bold, foreColor, backColor)
376 public final void putStrXY(final int x
, final int y
, final String str
,
377 final CellAttributes attr
) {
380 for (int j
= 0; j
< str
.length(); j
++) {
381 char ch
= str
.charAt(j
);
382 putCharXY(i
, y
, ch
, attr
);
391 * Render a string without changing the underlying attribute. Does not
392 * wrap if the string exceeds the line.
394 * @param x column coordinate. 0 is the left-most column.
395 * @param y row coordinate. 0 is the top-most row.
396 * @param str string to draw
398 public final void putStrXY(final int x
, final int y
, final String str
) {
400 for (int j
= 0; j
< str
.length(); j
++) {
401 char ch
= str
.charAt(j
);
411 * Draw a vertical line from (x, y) to (x, y + n).
413 * @param x column coordinate. 0 is the left-most column.
414 * @param y row coordinate. 0 is the top-most row.
415 * @param n number of characters to draw
416 * @param ch character to draw
417 * @param attr attributes to use (bold, foreColor, backColor)
419 public final void vLineXY(final int x
, final int y
, final int n
,
420 final char ch
, final CellAttributes attr
) {
422 for (int i
= y
; i
< y
+ n
; i
++) {
423 putCharXY(x
, i
, ch
, attr
);
428 * Draw a horizontal line from (x, y) to (x + n, y).
430 * @param x column coordinate. 0 is the left-most column.
431 * @param y row coordinate. 0 is the top-most row.
432 * @param n number of characters to draw
433 * @param ch character to draw
434 * @param attr attributes to use (bold, foreColor, backColor)
436 public final void hLineXY(final int x
, final int y
, final int n
,
437 final char ch
, final CellAttributes attr
) {
439 for (int i
= x
; i
< x
+ n
; i
++) {
440 putCharXY(i
, y
, ch
, attr
);
445 * Reallocate screen buffers.
447 * @param width new width
448 * @param height new height
450 private void reallocate(final int width
, final int height
) {
451 if (logical
!= null) {
452 for (int row
= 0; row
< this.height
; row
++) {
453 for (int col
= 0; col
< this.width
; col
++) {
454 logical
[col
][row
] = null;
459 logical
= new Cell
[width
][height
];
460 if (physical
!= null) {
461 for (int row
= 0; row
< this.height
; row
++) {
462 for (int col
= 0; col
< this.width
; col
++) {
463 physical
[col
][row
] = null;
468 physical
= new Cell
[width
][height
];
470 for (int row
= 0; row
< height
; row
++) {
471 for (int col
= 0; col
< width
; col
++) {
472 physical
[col
][row
] = new Cell();
473 logical
[col
][row
] = new Cell();
478 this.height
= height
;
485 reallyCleared
= true;
490 * Change the width. Everything on-screen will be destroyed and must be
493 * @param width new screen width
495 public final void setWidth(final int width
) {
496 reallocate(width
, this.height
);
500 * Change the height. Everything on-screen will be destroyed and must be
503 * @param height new screen height
505 public final void setHeight(final int height
) {
506 reallocate(this.width
, height
);
510 * Change the width and height. Everything on-screen will be destroyed
511 * and must be redrawn.
513 * @param width new screen width
514 * @param height new screen height
516 public final void setDimensions(final int width
, final int height
) {
517 reallocate(width
, height
);
523 * @return current screen height
525 public final int getHeight() {
532 * @return current screen width
534 public final int getWidth() {
539 * Public constructor. Sets everything to not-bold, white-on-black.
548 reallocate(width
, height
);
552 * Reset screen to not-bold, white-on-black. Also flushes the offset and
555 public final void reset() {
557 for (int row
= 0; row
< height
; row
++) {
558 for (int col
= 0; col
< width
; col
++) {
559 logical
[col
][row
].reset();
566 * Flush the offset and clip variables.
568 public final void resetClipping() {
578 * Force the screen to be fully cleared and redrawn on the next flush().
580 public final void clear() {
585 * Draw a box with a border and empty background.
587 * @param left left column of box. 0 is the left-most row.
588 * @param top top row of the box. 0 is the top-most row.
589 * @param right right column of box
590 * @param bottom bottom row of the box
591 * @param border attributes to use for the border
592 * @param background attributes to use for the background
594 public final void drawBox(final int left
, final int top
,
595 final int right
, final int bottom
,
596 final CellAttributes border
, final CellAttributes background
) {
598 drawBox(left
, top
, right
, bottom
, border
, background
, 1, false);
602 * Draw a box with a border and empty background.
604 * @param left left column of box. 0 is the left-most row.
605 * @param top top row of the box. 0 is the top-most row.
606 * @param right right column of box
607 * @param bottom bottom row of the box
608 * @param border attributes to use for the border
609 * @param background attributes to use for the background
610 * @param borderType if 1, draw a single-line border; if 2, draw a
611 * double-line border; if 3, draw double-line top/bottom edges and
612 * single-line left/right edges (like Qmodem)
613 * @param shadow if true, draw a "shadow" on the box
615 public final void drawBox(final int left
, final int top
,
616 final int right
, final int bottom
,
617 final CellAttributes border
, final CellAttributes background
,
618 final int borderType
, final boolean shadow
) {
622 int boxWidth
= right
- left
;
623 int boxHeight
= bottom
- top
;
632 switch (borderType
) {
634 cTopLeft
= GraphicsChars
.ULCORNER
;
635 cTopRight
= GraphicsChars
.URCORNER
;
636 cBottomLeft
= GraphicsChars
.LLCORNER
;
637 cBottomRight
= GraphicsChars
.LRCORNER
;
638 cHSide
= GraphicsChars
.SINGLE_BAR
;
639 cVSide
= GraphicsChars
.WINDOW_SIDE
;
643 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP_DOUBLE
;
644 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP_DOUBLE
;
645 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM_DOUBLE
;
646 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM_DOUBLE
;
647 cHSide
= GraphicsChars
.DOUBLE_BAR
;
648 cVSide
= GraphicsChars
.WINDOW_SIDE_DOUBLE
;
652 cTopLeft
= GraphicsChars
.WINDOW_LEFT_TOP
;
653 cTopRight
= GraphicsChars
.WINDOW_RIGHT_TOP
;
654 cBottomLeft
= GraphicsChars
.WINDOW_LEFT_BOTTOM
;
655 cBottomRight
= GraphicsChars
.WINDOW_RIGHT_BOTTOM
;
656 cHSide
= GraphicsChars
.WINDOW_TOP
;
657 cVSide
= GraphicsChars
.WINDOW_SIDE
;
660 throw new IllegalArgumentException("Invalid border type: "
664 // Place the corner characters
665 putCharXY(left
, top
, cTopLeft
, border
);
666 putCharXY(left
+ boxWidth
- 1, top
, cTopRight
, border
);
667 putCharXY(left
, top
+ boxHeight
- 1, cBottomLeft
, border
);
668 putCharXY(left
+ boxWidth
- 1, top
+ boxHeight
- 1, cBottomRight
,
671 // Draw the box lines
672 hLineXY(left
+ 1, top
, boxWidth
- 2, cHSide
, border
);
673 vLineXY(left
, top
+ 1, boxHeight
- 2, cVSide
, border
);
674 hLineXY(left
+ 1, top
+ boxHeight
- 1, boxWidth
- 2, cHSide
, border
);
675 vLineXY(left
+ boxWidth
- 1, top
+ 1, boxHeight
- 2, cVSide
, border
);
677 // Fill in the interior background
678 for (int i
= 1; i
< boxHeight
- 1; i
++) {
679 hLineXY(1 + left
, i
+ top
, boxWidth
- 2, ' ', background
);
684 drawBoxShadow(left
, top
, right
, bottom
);
691 * @param left left column of box. 0 is the left-most row.
692 * @param top top row of the box. 0 is the top-most row.
693 * @param right right column of box
694 * @param bottom bottom row of the box
696 public final void drawBoxShadow(final int left
, final int top
,
697 final int right
, final int bottom
) {
701 int boxWidth
= right
- left
;
702 int boxHeight
= bottom
- top
;
703 CellAttributes shadowAttr
= new CellAttributes();
705 // Shadows do not honor clipping but they DO honor offset.
706 int oldClipRight
= clipRight
;
707 int oldClipBottom
= clipBottom
;
709 clipRight = boxWidth + 2;
710 clipBottom = boxHeight + 1;
715 for (int i
= 0; i
< boxHeight
; i
++) {
716 putAttrXY(boxLeft
+ boxWidth
, boxTop
+ 1 + i
, shadowAttr
);
717 putAttrXY(boxLeft
+ boxWidth
+ 1, boxTop
+ 1 + i
, shadowAttr
);
719 for (int i
= 0; i
< boxWidth
; i
++) {
720 putAttrXY(boxLeft
+ 2 + i
, boxTop
+ boxHeight
, shadowAttr
);
722 clipRight
= oldClipRight
;
723 clipBottom
= oldClipBottom
;
727 * Subclasses must provide an implementation to push the logical screen
728 * to the physical device.
730 abstract public void flushPhysical();
733 * Put the cursor at (x,y).
735 * @param visible if true, the cursor should be visible
736 * @param x column coordinate to put the cursor on
737 * @param y row coordinate to put the cursor on
739 public final void putCursor(final boolean visible
,
740 final int x
, final int y
) {
742 cursorVisible
= visible
;
750 public final void hideCursor() {
751 cursorVisible
= false;