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));
260 // Kill the X11 cursor
261 // Transparent 16 x 16 pixel cursor image.
262 BufferedImage cursorImg
= new BufferedImage(16, 16,
263 BufferedImage
.TYPE_INT_ARGB
);
265 // Create a new blank cursor.
266 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
267 cursorImg
, new Point(0, 0), "blank cursor");
268 setCursor(blankCursor
);
273 * Figure out my font dimensions.
275 private void getFontDimensions() {
276 Graphics gr
= getGraphics();
277 FontMetrics fm
= gr
.getFontMetrics();
278 maxDescent
= fm
.getMaxDescent();
279 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
280 int leading
= fm
.getLeading();
281 textWidth
= (int)Math
.round(bounds
.getWidth());
282 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
283 // This also produces the same number, but works better for ugly
285 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
289 * Resize to font dimensions.
291 public void resizeToScreen() {
292 // Figure out the thickness of borders and use that to set the
294 Insets insets
= getInsets();
298 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
299 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
303 * Update redraws the whole screen.
305 * @param gr the AWT Graphics context
308 public void update(final Graphics gr
) {
309 // The default update clears the area. Don't do that, instead
310 // just paint it directly.
315 * Paint redraws the whole screen.
317 * @param gr the AWT Graphics context
320 public void paint(final Graphics gr
) {
321 // Do nothing until the screen reference has been set.
322 if (screen
== null) {
325 if (screen
.frame
== null) {
330 int xCellMax
= screen
.width
;
332 int yCellMax
= screen
.height
;
334 Rectangle bounds
= gr
.getClipBounds();
335 if (bounds
!= null) {
336 // Only update what is in the bounds
337 xCellMin
= screen
.textColumn(bounds
.x
);
338 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
);
339 if (xCellMax
> screen
.width
) {
340 xCellMax
= screen
.width
;
342 if (xCellMin
>= xCellMax
) {
343 xCellMin
= xCellMax
- 2;
348 yCellMin
= screen
.textRow(bounds
.y
);
349 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
);
350 if (yCellMax
> screen
.height
) {
351 yCellMax
= screen
.height
;
353 if (yCellMin
>= yCellMax
) {
354 yCellMin
= yCellMax
- 2;
360 // We need a total repaint
361 reallyCleared
= true;
364 // Prevent updates to the screen's data from the TApplication
366 synchronized (screen
) {
368 System.err.printf("bounds %s X %d %d Y %d %d\n",
369 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
372 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
373 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
375 int xPixel
= x
* textWidth
+ left
;
376 int yPixel
= y
* textHeight
+ top
;
378 Cell lCell
= screen
.logical
[x
][y
];
379 Cell pCell
= screen
.physical
[x
][y
];
381 if (!lCell
.equals(pCell
) || reallyCleared
) {
382 // Draw the background rectangle, then the
383 // foreground character.
384 gr
.setColor(attrToBackgroundColor(lCell
));
385 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
386 gr
.setColor(attrToForegroundColor(lCell
));
387 char [] chars
= new char[1];
388 chars
[0] = lCell
.getChar();
389 gr
.drawChars(chars
, 0, 1, xPixel
,
390 yPixel
+ textHeight
- maxDescent
);
392 // Physical is always updated
393 physical
[x
][y
].setTo(lCell
);
398 // Draw the cursor if it is visible
400 && (cursorY
<= screen
.height
- 1)
401 && (cursorX
<= screen
.width
- 1)
403 int xPixel
= cursorX
* textWidth
+ left
;
404 int yPixel
= cursorY
* textHeight
+ top
;
405 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
406 gr
.setColor(attrToForegroundColor(lCell
));
407 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
411 reallyCleared
= false;
412 } // synchronized (screen)
418 * The raw AWT Frame. Note package private access.
423 * Public constructor.
427 SwingUtilities
.invokeAndWait(new Runnable() {
429 AWTScreen
.this.frame
= new AWTFrame(AWTScreen
.this);
430 AWTScreen
.this.sessionInfo
=
431 new AWTSessionInfo(AWTScreen
.this.frame
,
435 AWTScreen
.this.setDimensions(sessionInfo
.getWindowWidth(),
436 sessionInfo
.getWindowHeight());
438 AWTScreen
.this.frame
.resizeToScreen();
439 AWTScreen
.this.frame
.setVisible(true);
442 } catch (Exception e
) {
450 private AWTSessionInfo sessionInfo
;
453 * Create the AWTSessionInfo. Note package private access.
455 * @return the sessionInfo
457 AWTSessionInfo
getSessionInfo() {
462 * Push the logical screen to the physical device.
465 public void flushPhysical() {
468 // Really refreshed, do it all
473 // Do nothing if nothing happened.
478 // Request a repaint, let the frame's repaint/update methods do the
481 // Find the minimum-size damaged region.
482 int xMin
= frame
.getWidth();
484 int yMin
= frame
.getHeight();
487 synchronized (this) {
488 for (int y
= 0; y
< height
; y
++) {
489 for (int x
= 0; x
< width
; x
++) {
490 Cell lCell
= logical
[x
][y
];
491 Cell pCell
= physical
[x
][y
];
493 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
494 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
496 if (!lCell
.equals(pCell
)
504 if (xPixel
+ frame
.textWidth
> xMax
) {
505 xMax
= xPixel
+ frame
.textWidth
;
510 if (yPixel
+ frame
.textHeight
> yMax
) {
511 yMax
= yPixel
+ frame
.textHeight
;
517 if (xMin
+ frame
.textWidth
>= xMax
) {
518 xMax
+= frame
.textWidth
;
520 if (yMin
+ frame
.textHeight
>= yMax
) {
521 yMax
+= frame
.textHeight
;
524 // Repaint the desired area
525 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
526 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
530 * Put the cursor at (x,y).
532 * @param visible if true, the cursor should be visible
533 * @param x column coordinate to put the cursor on
534 * @param y row coordinate to put the cursor on
537 public void putCursor(final boolean visible
, final int x
, final int y
) {
539 && (cursorY
<= height
- 1)
540 && (cursorX
<= width
- 1)
542 // Make the current cursor position dirty
543 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
544 physical
[cursorX
][cursorY
].setChar('X');
546 physical
[cursorX
][cursorY
].setChar('Q');
550 super.putCursor(visible
, x
, y
);
554 * Convert pixel column position to text cell column position.
556 * @param x pixel column position
557 * @return text cell column position
559 public int textColumn(final int x
) {
560 return ((x
- frame
.left
) / frame
.textWidth
);
564 * Convert pixel row position to text cell row position.
566 * @param y pixel row position
567 * @return text cell row position
569 public int textRow(final int y
) {
570 return ((y
- frame
.top
) / frame
.textHeight
);