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
.session
.AWTSessionInfo
;
37 import java
.awt
.Color
;
38 import java
.awt
.Cursor
;
40 import java
.awt
.FontMetrics
;
41 import java
.awt
.Frame
;
42 import java
.awt
.Graphics
;
43 import java
.awt
.Insets
;
44 import java
.awt
.Point
;
45 import java
.awt
.Rectangle
;
46 import java
.awt
.Toolkit
;
47 import java
.awt
.geom
.Rectangle2D
;
48 import java
.awt
.image
.BufferedImage
;
49 import java
.io
.InputStream
;
52 * This Screen implementation draws to a Java AWT Frame.
54 public final class AWTScreen
extends Screen
{
56 private static Color MYBLACK
;
57 private static Color MYRED
;
58 private static Color MYGREEN
;
59 private static Color MYYELLOW
;
60 private static Color MYBLUE
;
61 private static Color MYMAGENTA
;
62 private static Color MYCYAN
;
63 private static Color MYWHITE
;
65 private static Color MYBOLD_BLACK
;
66 private static Color MYBOLD_RED
;
67 private static Color MYBOLD_GREEN
;
68 private static Color MYBOLD_YELLOW
;
69 private static Color MYBOLD_BLUE
;
70 private static Color MYBOLD_MAGENTA
;
71 private static Color MYBOLD_CYAN
;
72 private static Color MYBOLD_WHITE
;
74 private static boolean dosColors
= false;
77 * Setup AWT colors to match DOS color palette.
79 private static void setDOSColors() {
83 MYBLACK
= new Color(0x00, 0x00, 0x00);
84 MYRED
= new Color(0xa8, 0x00, 0x00);
85 MYGREEN
= new Color(0x00, 0xa8, 0x00);
86 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
87 MYBLUE
= new Color(0x00, 0x00, 0xa8);
88 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
89 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
90 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
91 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
92 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
93 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
94 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
95 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
96 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
97 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
98 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
104 * AWTFrame is our top-level hook into the AWT system.
106 class AWTFrame
extends Frame
{
109 * The terminus font resource filename.
111 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
114 * The TUI Screen data.
119 * Width of a character cell.
121 private int textWidth
= 1;
124 * Height of a character cell.
126 private int textHeight
= 1;
129 * Descent of a character cell.
131 private int maxDescent
= 0;
136 private int top
= 30;
141 private int left
= 30;
144 * Convert a CellAttributes foreground color to an AWT Color.
146 * @param attr the text attributes
147 * @return the AWT Color
149 private Color
attrToForegroundColor(final CellAttributes attr
) {
156 if (attr
.getBold()) {
157 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
159 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
161 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
163 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
165 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
166 return MYBOLD_YELLOW
;
167 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
169 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
170 return MYBOLD_MAGENTA
;
171 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
175 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
177 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
179 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
181 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
183 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
185 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
187 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
189 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
193 throw new IllegalArgumentException("Invalid color: " + attr
.getForeColor().getValue());
197 * Convert a CellAttributes background color to an AWT Color.
199 * @param attr the text attributes
200 * @return the AWT Color
202 private Color
attrToBackgroundColor(final CellAttributes attr
) {
209 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
211 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
213 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
215 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
217 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
219 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
221 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
223 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
226 throw new IllegalArgumentException("Invalid color: " + attr
.getBackColor().getValue());
230 * Public constructor.
232 * @param screen the Screen that Backend talks to
234 public AWTFrame(final AWTScreen screen
) {
235 this.screen
= screen
;
238 setTitle("Jexer Application");
239 setBackground(java
.awt
.Color
.black
);
240 // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
241 // setFont(new Font("Liberation Mono", Font.BOLD, 16));
242 // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
245 // Always try to use Terminus, the one decent font.
246 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
247 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
248 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
249 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 22);
251 } catch (Exception e
) {
253 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
254 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
259 // Kill the X11 cursor
260 // Transparent 16 x 16 pixel cursor image.
261 BufferedImage cursorImg
= new BufferedImage(16, 16,
262 BufferedImage
.TYPE_INT_ARGB
);
264 // Create a new blank cursor.
265 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
266 cursorImg
, new Point(0, 0), "blank cursor");
267 setCursor(blankCursor
);
271 * Resize to font dimensions.
273 public void resizeToScreen() {
274 Graphics gr
= getGraphics();
275 FontMetrics fm
= gr
.getFontMetrics();
276 maxDescent
= fm
.getMaxDescent();
277 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
278 int leading
= fm
.getLeading();
279 textWidth
= (int)Math
.round(bounds
.getWidth());
280 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
281 // This also produces the same number, but works better for ugly
283 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
285 // Figure out the thickness of borders and use that to set the
287 Insets insets
= getInsets();
291 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
292 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
295 System.err.printf("W: %d H: %d MD: %d L: %d\n", textWidth,
296 textHeight, maxDescent, leading);
301 * Update redraws the whole screen.
303 * @param gr the AWT Graphics context
306 public void update(final Graphics gr
) {
307 // The default update clears the area. Don't do that, instead
308 // just paint it directly.
313 * Paint redraws the whole screen.
315 * @param gr the AWT Graphics context
318 public void paint(final Graphics gr
) {
319 // Do nothing until the screen reference has been set.
320 if (screen
== null) {
323 if (screen
.frame
== null) {
328 int xCellMax
= screen
.width
;
330 int yCellMax
= screen
.height
;
332 Rectangle bounds
= gr
.getClipBounds();
333 if (bounds
!= null) {
334 // Only update what is in the bounds
335 xCellMin
= screen
.textColumn(bounds
.x
);
336 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
) + 1;
337 if (xCellMax
> screen
.width
) {
338 xCellMax
= screen
.width
;
340 if (xCellMin
>= xCellMax
) {
341 xCellMin
= xCellMax
- 2;
346 yCellMin
= screen
.textRow(bounds
.y
);
347 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
) + 1;
348 if (yCellMax
> screen
.height
) {
349 yCellMax
= screen
.height
;
351 if (yCellMin
>= yCellMax
) {
352 yCellMin
= yCellMax
- 2;
359 // Prevent updates to the screen's data from the TApplication
361 synchronized (screen
) {
363 System.err.printf("bounds %s X %d %d Y %d %d\n",
364 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
367 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
368 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
370 int xPixel
= x
* textWidth
+ left
;
371 int yPixel
= y
* textHeight
+ top
;
373 Cell lCell
= screen
.logical
[x
][y
];
374 Cell pCell
= screen
.physical
[x
][y
];
376 if (!lCell
.equals(pCell
) || reallyCleared
) {
377 // Draw the background rectangle, then the
378 // foreground character.
379 gr
.setColor(attrToBackgroundColor(lCell
));
380 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
381 gr
.setColor(attrToForegroundColor(lCell
));
382 char [] chars
= new char[1];
383 chars
[0] = lCell
.getChar();
384 gr
.drawChars(chars
, 0, 1, xPixel
,
385 yPixel
+ textHeight
- maxDescent
);
387 // Physical is always updated
388 physical
[x
][y
].setTo(lCell
);
393 // Draw the cursor if it is visible
395 && (cursorY
<= screen
.height
- 1)
396 && (cursorX
<= screen
.width
- 1)
398 int xPixel
= cursorX
* textWidth
+ left
;
399 int yPixel
= cursorY
* textHeight
+ top
;
400 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
401 gr
.setColor(attrToForegroundColor(lCell
));
402 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
406 reallyCleared
= false;
407 } // synchronized (screen)
412 * The raw AWT Frame. Note package private access.
417 * Public constructor.
420 frame
= new AWTFrame(this);
424 * Create the AWTSessionInfo. Note package private access.
426 * @return the sessionInfo
428 AWTSessionInfo
getSessionInfo() {
429 AWTSessionInfo sessionInfo
= new AWTSessionInfo(frame
, frame
.textWidth
,
435 * Push the logical screen to the physical device.
438 public void flushPhysical() {
441 // Really refreshed, do it all
446 // Do nothing if nothing happened.
451 // Request a repaint, let the frame's repaint/update methods do the
454 // Find the minimum-size damaged region.
455 int xMin
= frame
.getWidth();
457 int yMin
= frame
.getHeight();
460 synchronized (this) {
461 for (int y
= 0; y
< height
; y
++) {
462 for (int x
= 0; x
< width
; x
++) {
463 Cell lCell
= logical
[x
][y
];
464 Cell pCell
= physical
[x
][y
];
466 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
467 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
469 if (!lCell
.equals(pCell
)
477 if (xPixel
+ frame
.textWidth
> xMax
) {
478 xMax
= xPixel
+ frame
.textWidth
;
483 if (yPixel
+ frame
.textHeight
> yMax
) {
484 yMax
= yPixel
+ frame
.textHeight
;
490 if (xMin
+ frame
.textWidth
>= xMax
) {
491 xMax
+= frame
.textWidth
;
493 if (yMin
+ frame
.textHeight
>= yMax
) {
494 yMax
+= frame
.textHeight
;
497 // Repaint the desired area
498 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
499 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
503 * Put the cursor at (x,y).
505 * @param visible if true, the cursor should be visible
506 * @param x column coordinate to put the cursor on
507 * @param y row coordinate to put the cursor on
510 public void putCursor(final boolean visible
, final int x
, final int y
) {
512 && (cursorY
<= height
- 1)
513 && (cursorX
<= width
- 1)
515 // Make the current cursor position dirty
516 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
517 physical
[cursorX
][cursorY
].setChar('X');
519 physical
[cursorX
][cursorY
].setChar('Q');
523 super.putCursor(visible
, x
, y
);
527 * Convert pixel column position to text cell column position.
529 * @param x pixel column position
530 * @return text cell column position
532 public int textColumn(final int x
) {
533 return ((x
- frame
.left
) / frame
.textWidth
);
537 * Convert pixel row position to text cell row position.
539 * @param y pixel row position
540 * @return text cell row position
542 public int textRow(final int y
) {
543 return ((y
- frame
.top
) / frame
.textHeight
);