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
;
50 import javax
.swing
.JFrame
;
51 import javax
.swing
.SwingUtilities
;
54 * This Screen implementation draws to a Java AWT Frame.
56 public final class AWTScreen
extends Screen
{
58 private static Color MYBLACK
;
59 private static Color MYRED
;
60 private static Color MYGREEN
;
61 private static Color MYYELLOW
;
62 private static Color MYBLUE
;
63 private static Color MYMAGENTA
;
64 private static Color MYCYAN
;
65 private static Color MYWHITE
;
67 private static Color MYBOLD_BLACK
;
68 private static Color MYBOLD_RED
;
69 private static Color MYBOLD_GREEN
;
70 private static Color MYBOLD_YELLOW
;
71 private static Color MYBOLD_BLUE
;
72 private static Color MYBOLD_MAGENTA
;
73 private static Color MYBOLD_CYAN
;
74 private static Color MYBOLD_WHITE
;
76 private static boolean dosColors
= false;
79 * Setup AWT colors to match DOS color palette.
81 private static void setDOSColors() {
85 MYBLACK
= new Color(0x00, 0x00, 0x00);
86 MYRED
= new Color(0xa8, 0x00, 0x00);
87 MYGREEN
= new Color(0x00, 0xa8, 0x00);
88 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
89 MYBLUE
= new Color(0x00, 0x00, 0xa8);
90 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
91 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
92 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
93 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
94 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
95 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
96 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
97 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
98 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
99 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
100 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
106 * AWTFrame is our top-level hook into the AWT system.
108 class AWTFrame
extends JFrame
{
111 * The terminus font resource filename.
113 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
116 * The TUI Screen data.
121 * Width of a character cell.
123 private int textWidth
= 1;
126 * Height of a character cell.
128 private int textHeight
= 1;
131 * Descent of a character cell.
133 private int maxDescent
= 0;
138 private int top
= 30;
143 private int left
= 30;
146 * Convert a CellAttributes foreground color to an AWT Color.
148 * @param attr the text attributes
149 * @return the AWT Color
151 private Color
attrToForegroundColor(final CellAttributes attr
) {
158 if (attr
.getBold()) {
159 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
161 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
163 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
165 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
167 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
168 return MYBOLD_YELLOW
;
169 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
171 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
172 return MYBOLD_MAGENTA
;
173 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
177 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
179 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
181 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
183 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
185 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
187 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
189 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
191 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
195 throw new IllegalArgumentException("Invalid color: " + attr
.getForeColor().getValue());
199 * Convert a CellAttributes background color to an AWT Color.
201 * @param attr the text attributes
202 * @return the AWT Color
204 private Color
attrToBackgroundColor(final CellAttributes attr
) {
211 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
213 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
215 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
217 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
219 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
221 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
223 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
225 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
228 throw new IllegalArgumentException("Invalid color: " + attr
.getBackColor().getValue());
232 * Public constructor.
234 * @param screen the Screen that Backend talks to
236 public AWTFrame(final AWTScreen screen
) {
237 this.screen
= screen
;
240 setTitle("Jexer Application");
241 setBackground(Color
.black
);
242 // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
243 // setFont(new Font("Liberation Mono", Font.BOLD, 16));
244 // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
247 // Always try to use Terminus, the one decent font.
248 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
249 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
250 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
251 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 22);
253 } catch (Exception e
) {
255 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
256 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
262 // Kill the X11 cursor
263 // Transparent 16 x 16 pixel cursor image.
264 BufferedImage cursorImg
= new BufferedImage(16, 16,
265 BufferedImage
.TYPE_INT_ARGB
);
267 // Create a new blank cursor.
268 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
269 cursorImg
, new Point(0, 0), "blank cursor");
270 setCursor(blankCursor
);
274 * Resize to font dimensions.
276 public void resizeToScreen() {
277 Graphics gr
= getGraphics();
278 FontMetrics fm
= gr
.getFontMetrics();
279 maxDescent
= fm
.getMaxDescent();
280 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
281 int leading
= fm
.getLeading();
282 textWidth
= (int)Math
.round(bounds
.getWidth());
283 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
284 // This also produces the same number, but works better for ugly
286 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
288 // Figure out the thickness of borders and use that to set the
290 Insets insets
= getInsets();
294 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
295 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
298 System.err.printf("W: %d H: %d MD: %d L: %d\n", textWidth,
299 textHeight, maxDescent, leading);
304 * Update redraws the whole screen.
306 * @param gr the AWT Graphics context
309 public void update(final Graphics gr
) {
310 // The default update clears the area. Don't do that, instead
311 // just paint it directly.
316 * Paint redraws the whole screen.
318 * @param gr the AWT Graphics context
321 public void paint(final Graphics gr
) {
322 // Do nothing until the screen reference has been set.
323 if (screen
== null) {
326 if (screen
.frame
== null) {
331 int xCellMax
= screen
.width
;
333 int yCellMax
= screen
.height
;
335 Rectangle bounds
= gr
.getClipBounds();
336 if (bounds
!= null) {
337 // Only update what is in the bounds
338 xCellMin
= screen
.textColumn(bounds
.x
);
339 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
) + 1;
340 if (xCellMax
> screen
.width
) {
341 xCellMax
= screen
.width
;
343 if (xCellMin
>= xCellMax
) {
344 xCellMin
= xCellMax
- 2;
349 yCellMin
= screen
.textRow(bounds
.y
);
350 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
) + 1;
351 if (yCellMax
> screen
.height
) {
352 yCellMax
= screen
.height
;
354 if (yCellMin
>= yCellMax
) {
355 yCellMin
= yCellMax
- 2;
362 // Prevent updates to the screen's data from the TApplication
364 synchronized (screen
) {
366 System.err.printf("bounds %s X %d %d Y %d %d\n",
367 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
370 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
371 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
373 int xPixel
= x
* textWidth
+ left
;
374 int yPixel
= y
* textHeight
+ top
;
376 Cell lCell
= screen
.logical
[x
][y
];
377 Cell pCell
= screen
.physical
[x
][y
];
379 if (!lCell
.equals(pCell
) || reallyCleared
) {
380 // Draw the background rectangle, then the
381 // foreground character.
382 gr
.setColor(attrToBackgroundColor(lCell
));
383 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
384 gr
.setColor(attrToForegroundColor(lCell
));
385 char [] chars
= new char[1];
386 chars
[0] = lCell
.getChar();
387 gr
.drawChars(chars
, 0, 1, xPixel
,
388 yPixel
+ textHeight
- maxDescent
);
390 // Physical is always updated
391 physical
[x
][y
].setTo(lCell
);
396 // Draw the cursor if it is visible
398 && (cursorY
<= screen
.height
- 1)
399 && (cursorX
<= screen
.width
- 1)
401 int xPixel
= cursorX
* textWidth
+ left
;
402 int yPixel
= cursorY
* textHeight
+ top
;
403 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
404 gr
.setColor(attrToForegroundColor(lCell
));
405 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
409 reallyCleared
= false;
410 } // synchronized (screen)
415 * The raw AWT Frame. Note package private access.
420 * Public constructor.
424 SwingUtilities
.invokeAndWait(new Runnable() {
426 AWTScreen
.this.frame
= new AWTFrame(AWTScreen
.this);
429 } catch (Exception e
) {
435 * Create the AWTSessionInfo. Note package private access.
437 * @return the sessionInfo
439 AWTSessionInfo
getSessionInfo() {
440 AWTSessionInfo sessionInfo
= new AWTSessionInfo(frame
, frame
.textWidth
,
446 * Push the logical screen to the physical device.
449 public void flushPhysical() {
452 // Really refreshed, do it all
457 // Do nothing if nothing happened.
462 // Request a repaint, let the frame's repaint/update methods do the
465 // Find the minimum-size damaged region.
466 int xMin
= frame
.getWidth();
468 int yMin
= frame
.getHeight();
471 synchronized (this) {
472 for (int y
= 0; y
< height
; y
++) {
473 for (int x
= 0; x
< width
; x
++) {
474 Cell lCell
= logical
[x
][y
];
475 Cell pCell
= physical
[x
][y
];
477 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
478 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
480 if (!lCell
.equals(pCell
)
488 if (xPixel
+ frame
.textWidth
> xMax
) {
489 xMax
= xPixel
+ frame
.textWidth
;
494 if (yPixel
+ frame
.textHeight
> yMax
) {
495 yMax
= yPixel
+ frame
.textHeight
;
501 if (xMin
+ frame
.textWidth
>= xMax
) {
502 xMax
+= frame
.textWidth
;
504 if (yMin
+ frame
.textHeight
>= yMax
) {
505 yMax
+= frame
.textHeight
;
508 // Repaint the desired area
509 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
510 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
514 * Put the cursor at (x,y).
516 * @param visible if true, the cursor should be visible
517 * @param x column coordinate to put the cursor on
518 * @param y row coordinate to put the cursor on
521 public void putCursor(final boolean visible
, final int x
, final int y
) {
523 && (cursorY
<= height
- 1)
524 && (cursorX
<= width
- 1)
526 // Make the current cursor position dirty
527 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
528 physical
[cursorX
][cursorY
].setChar('X');
530 physical
[cursorX
][cursorY
].setChar('Q');
534 super.putCursor(visible
, x
, y
);
538 * Convert pixel column position to text cell column position.
540 * @param x pixel column position
541 * @return text cell column position
543 public int textColumn(final int x
) {
544 return ((x
- frame
.left
) / frame
.textWidth
);
548 * Convert pixel row position to text cell row position.
550 * @param y pixel row position
551 * @return text cell row position
553 public int textRow(final int y
) {
554 return ((y
- frame
.top
) / frame
.textHeight
);