a3fc41f19fa03563ca292c5f4feed4e28fd0c33f
[fanfix.git] / src / jexer / io / AWTScreen.java
1 /**
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
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.
16 *
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.
21 *
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
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.io;
32
33 import jexer.bits.Cell;
34 import jexer.bits.CellAttributes;
35 import jexer.session.AWTSessionInfo;
36
37 import java.awt.Color;
38 import java.awt.Cursor;
39 import java.awt.Font;
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;
52
53 /**
54 * This Screen implementation draws to a Java AWT Frame.
55 */
56 public final class AWTScreen extends Screen {
57
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;
66
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;
75
76 private static boolean dosColors = false;
77
78 /**
79 * Setup AWT colors to match DOS color palette.
80 */
81 private static void setDOSColors() {
82 if (dosColors) {
83 return;
84 }
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);
101
102 dosColors = true;
103 }
104
105 /**
106 * AWTFrame is our top-level hook into the AWT system.
107 */
108 class AWTFrame extends JFrame {
109
110 /**
111 * The terminus font resource filename.
112 */
113 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
114
115 /**
116 * The TUI Screen data.
117 */
118 AWTScreen screen;
119
120 /**
121 * Width of a character cell.
122 */
123 private int textWidth = 1;
124
125 /**
126 * Height of a character cell.
127 */
128 private int textHeight = 1;
129
130 /**
131 * Descent of a character cell.
132 */
133 private int maxDescent = 0;
134
135 /**
136 * Top pixel value.
137 */
138 private int top = 30;
139
140 /**
141 * Left pixel value.
142 */
143 private int left = 30;
144
145 /**
146 * Convert a CellAttributes foreground color to an AWT Color.
147 *
148 * @param attr the text attributes
149 * @return the AWT Color
150 */
151 private Color attrToForegroundColor(final CellAttributes attr) {
152 /*
153 * TODO:
154 * reverse
155 * blink
156 * underline
157 */
158 if (attr.getBold()) {
159 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
160 return MYBOLD_BLACK;
161 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
162 return MYBOLD_RED;
163 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
164 return MYBOLD_BLUE;
165 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
166 return MYBOLD_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)) {
170 return MYBOLD_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)) {
174 return MYBOLD_WHITE;
175 }
176 } else {
177 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
178 return MYBLACK;
179 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
180 return MYRED;
181 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
182 return MYBLUE;
183 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
184 return MYGREEN;
185 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
186 return MYYELLOW;
187 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
188 return MYCYAN;
189 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
190 return MYMAGENTA;
191 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
192 return MYWHITE;
193 }
194 }
195 throw new IllegalArgumentException("Invalid color: " + attr.getForeColor().getValue());
196 }
197
198 /**
199 * Convert a CellAttributes background color to an AWT Color.
200 *
201 * @param attr the text attributes
202 * @return the AWT Color
203 */
204 private Color attrToBackgroundColor(final CellAttributes attr) {
205 /*
206 * TODO:
207 * reverse
208 * blink
209 * underline
210 */
211 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
212 return MYBLACK;
213 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
214 return MYRED;
215 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
216 return MYBLUE;
217 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
218 return MYGREEN;
219 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
220 return MYYELLOW;
221 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
222 return MYCYAN;
223 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
224 return MYMAGENTA;
225 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
226 return MYWHITE;
227 }
228 throw new IllegalArgumentException("Invalid color: " + attr.getBackColor().getValue());
229 }
230
231 /**
232 * Public constructor.
233 *
234 * @param screen the Screen that Backend talks to
235 */
236 public AWTFrame(final AWTScreen screen) {
237 this.screen = screen;
238 setDOSColors();
239
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));
245
246 try {
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);
252 setFont(terminus);
253 } catch (Exception e) {
254 e.printStackTrace();
255 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
256 setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
257 }
258 pack();
259 setVisible(true);
260 resizeToScreen();
261
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);
266
267 // Create a new blank cursor.
268 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
269 cursorImg, new Point(0, 0), "blank cursor");
270 setCursor(blankCursor);
271 }
272
273 /**
274 * Resize to font dimensions.
275 */
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
285 // monospace.
286 textHeight = fm.getMaxAscent() + maxDescent - leading;
287
288 // Figure out the thickness of borders and use that to set the
289 // final size.
290 Insets insets = getInsets();
291 left = insets.left;
292 top = insets.top;
293
294 setSize(textWidth * screen.width + insets.left + insets.right,
295 textHeight * screen.height + insets.top + insets.bottom);
296
297 /*
298 System.err.printf("W: %d H: %d MD: %d L: %d\n", textWidth,
299 textHeight, maxDescent, leading);
300 */
301 }
302
303 /**
304 * Update redraws the whole screen.
305 *
306 * @param gr the AWT Graphics context
307 */
308 @Override
309 public void update(final Graphics gr) {
310 // The default update clears the area. Don't do that, instead
311 // just paint it directly.
312 paint(gr);
313 }
314
315 /**
316 * Paint redraws the whole screen.
317 *
318 * @param gr the AWT Graphics context
319 */
320 @Override
321 public void paint(final Graphics gr) {
322 // Do nothing until the screen reference has been set.
323 if (screen == null) {
324 return;
325 }
326 if (screen.frame == null) {
327 return;
328 }
329
330 int xCellMin = 0;
331 int xCellMax = screen.width;
332 int yCellMin = 0;
333 int yCellMax = screen.height;
334
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;
342 }
343 if (xCellMin >= xCellMax) {
344 xCellMin = xCellMax - 2;
345 }
346 if (xCellMin < 0) {
347 xCellMin = 0;
348 }
349 yCellMin = screen.textRow(bounds.y);
350 yCellMax = screen.textRow(bounds.y + bounds.height) + 1;
351 if (yCellMax > screen.height) {
352 yCellMax = screen.height;
353 }
354 if (yCellMin >= yCellMax) {
355 yCellMin = yCellMax - 2;
356 }
357 if (yCellMin < 0) {
358 yCellMin = 0;
359 }
360 }
361
362 // Prevent updates to the screen's data from the TApplication
363 // threads.
364 synchronized (screen) {
365 /*
366 System.err.printf("bounds %s X %d %d Y %d %d\n",
367 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
368 */
369
370 for (int y = yCellMin; y < yCellMax; y++) {
371 for (int x = xCellMin; x < xCellMax; x++) {
372
373 int xPixel = x * textWidth + left;
374 int yPixel = y * textHeight + top;
375
376 Cell lCell = screen.logical[x][y];
377 Cell pCell = screen.physical[x][y];
378
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);
389
390 // Physical is always updated
391 physical[x][y].setTo(lCell);
392 }
393 }
394 }
395
396 // Draw the cursor if it is visible
397 if ((cursorVisible)
398 && (cursorY <= screen.height - 1)
399 && (cursorX <= screen.width - 1)
400 ) {
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);
406 }
407
408 dirty = false;
409 reallyCleared = false;
410 } // synchronized (screen)
411 }
412 }
413
414 /**
415 * The raw AWT Frame. Note package private access.
416 */
417 AWTFrame frame;
418
419 /**
420 * Public constructor.
421 */
422 public AWTScreen() {
423 try {
424 SwingUtilities.invokeAndWait(new Runnable() {
425 public void run() {
426 AWTScreen.this.frame = new AWTFrame(AWTScreen.this);
427 }
428 } );
429 } catch (Exception e) {
430 e.printStackTrace();
431 }
432 }
433
434 /**
435 * Create the AWTSessionInfo. Note package private access.
436 *
437 * @return the sessionInfo
438 */
439 AWTSessionInfo getSessionInfo() {
440 AWTSessionInfo sessionInfo = new AWTSessionInfo(frame, frame.textWidth,
441 frame.textHeight);
442 return sessionInfo;
443 }
444
445 /**
446 * Push the logical screen to the physical device.
447 */
448 @Override
449 public void flushPhysical() {
450
451 if (reallyCleared) {
452 // Really refreshed, do it all
453 frame.repaint();
454 return;
455 }
456
457 // Do nothing if nothing happened.
458 if (!dirty) {
459 return;
460 }
461
462 // Request a repaint, let the frame's repaint/update methods do the
463 // right thing.
464
465 // Find the minimum-size damaged region.
466 int xMin = frame.getWidth();
467 int xMax = 0;
468 int yMin = frame.getHeight();
469 int yMax = 0;
470
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];
476
477 int xPixel = x * frame.textWidth + frame.left;
478 int yPixel = y * frame.textHeight + frame.top;
479
480 if (!lCell.equals(pCell)
481 || ((x == cursorX)
482 && (y == cursorY)
483 && cursorVisible)
484 ) {
485 if (xPixel < xMin) {
486 xMin = xPixel;
487 }
488 if (xPixel + frame.textWidth > xMax) {
489 xMax = xPixel + frame.textWidth;
490 }
491 if (yPixel < yMin) {
492 yMin = yPixel;
493 }
494 if (yPixel + frame.textHeight > yMax) {
495 yMax = yPixel + frame.textHeight;
496 }
497 }
498 }
499 }
500 }
501 if (xMin + frame.textWidth >= xMax) {
502 xMax += frame.textWidth;
503 }
504 if (yMin + frame.textHeight >= yMax) {
505 yMax += frame.textHeight;
506 }
507
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);
511 }
512
513 /**
514 * Put the cursor at (x,y).
515 *
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
519 */
520 @Override
521 public void putCursor(final boolean visible, final int x, final int y) {
522 if ((cursorVisible)
523 && (cursorY <= height - 1)
524 && (cursorX <= width - 1)
525 ) {
526 // Make the current cursor position dirty
527 if (physical[cursorX][cursorY].getChar() == 'Q') {
528 physical[cursorX][cursorY].setChar('X');
529 } else {
530 physical[cursorX][cursorY].setChar('Q');
531 }
532 }
533
534 super.putCursor(visible, x, y);
535 }
536
537 /**
538 * Convert pixel column position to text cell column position.
539 *
540 * @param x pixel column position
541 * @return text cell column position
542 */
543 public int textColumn(final int x) {
544 return ((x - frame.left) / frame.textWidth);
545 }
546
547 /**
548 * Convert pixel row position to text cell row position.
549 *
550 * @param y pixel row position
551 * @return text cell row position
552 */
553 public int textRow(final int y) {
554 return ((y - frame.top) / frame.textHeight);
555 }
556
557 }