fix eclipse warnings
[nikiroo-utils.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.Graphics;
42 import java.awt.Insets;
43 import java.awt.Point;
44 import java.awt.Rectangle;
45 import java.awt.Toolkit;
46 import java.awt.geom.Rectangle2D;
47 import java.awt.image.BufferedImage;
48 import java.io.InputStream;
49 import javax.swing.JFrame;
50 import javax.swing.SwingUtilities;
51
52 /**
53 * This Screen implementation draws to a Java AWT Frame.
54 */
55 public final class AWTScreen extends Screen {
56
57 private static Color MYBLACK;
58 private static Color MYRED;
59 private static Color MYGREEN;
60 private static Color MYYELLOW;
61 private static Color MYBLUE;
62 private static Color MYMAGENTA;
63 private static Color MYCYAN;
64 private static Color MYWHITE;
65
66 private static Color MYBOLD_BLACK;
67 private static Color MYBOLD_RED;
68 private static Color MYBOLD_GREEN;
69 private static Color MYBOLD_YELLOW;
70 private static Color MYBOLD_BLUE;
71 private static Color MYBOLD_MAGENTA;
72 private static Color MYBOLD_CYAN;
73 private static Color MYBOLD_WHITE;
74
75 private static boolean dosColors = false;
76
77 /**
78 * Setup AWT colors to match DOS color palette.
79 */
80 private static void setDOSColors() {
81 if (dosColors) {
82 return;
83 }
84 MYBLACK = new Color(0x00, 0x00, 0x00);
85 MYRED = new Color(0xa8, 0x00, 0x00);
86 MYGREEN = new Color(0x00, 0xa8, 0x00);
87 MYYELLOW = new Color(0xa8, 0x54, 0x00);
88 MYBLUE = new Color(0x00, 0x00, 0xa8);
89 MYMAGENTA = new Color(0xa8, 0x00, 0xa8);
90 MYCYAN = new Color(0x00, 0xa8, 0xa8);
91 MYWHITE = new Color(0xa8, 0xa8, 0xa8);
92 MYBOLD_BLACK = new Color(0x54, 0x54, 0x54);
93 MYBOLD_RED = new Color(0xfc, 0x54, 0x54);
94 MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54);
95 MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54);
96 MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc);
97 MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc);
98 MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc);
99 MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc);
100
101 dosColors = true;
102 }
103
104 /**
105 * AWTFrame is our top-level hook into the AWT system.
106 */
107 class AWTFrame extends JFrame {
108
109 /**
110 * Serializable version.
111 */
112 private static final long serialVersionUID = 1;
113
114 /**
115 * The terminus font resource filename.
116 */
117 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
118
119 /**
120 * The TUI Screen data.
121 */
122 AWTScreen screen;
123
124 /**
125 * Width of a character cell.
126 */
127 private int textWidth = 1;
128
129 /**
130 * Height of a character cell.
131 */
132 private int textHeight = 1;
133
134 /**
135 * Descent of a character cell.
136 */
137 private int maxDescent = 0;
138
139 /**
140 * Top pixel value.
141 */
142 private int top = 30;
143
144 /**
145 * Left pixel value.
146 */
147 private int left = 30;
148
149 /**
150 * Convert a CellAttributes foreground color to an AWT Color.
151 *
152 * @param attr the text attributes
153 * @return the AWT Color
154 */
155 private Color attrToForegroundColor(final CellAttributes attr) {
156 /*
157 * TODO:
158 * reverse
159 * blink
160 * underline
161 */
162 if (attr.getBold()) {
163 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
164 return MYBOLD_BLACK;
165 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
166 return MYBOLD_RED;
167 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
168 return MYBOLD_BLUE;
169 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
170 return MYBOLD_GREEN;
171 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
172 return MYBOLD_YELLOW;
173 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
174 return MYBOLD_CYAN;
175 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
176 return MYBOLD_MAGENTA;
177 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
178 return MYBOLD_WHITE;
179 }
180 } else {
181 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
182 return MYBLACK;
183 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
184 return MYRED;
185 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
186 return MYBLUE;
187 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
188 return MYGREEN;
189 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
190 return MYYELLOW;
191 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
192 return MYCYAN;
193 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
194 return MYMAGENTA;
195 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
196 return MYWHITE;
197 }
198 }
199 throw new IllegalArgumentException("Invalid color: " + attr.getForeColor().getValue());
200 }
201
202 /**
203 * Convert a CellAttributes background color to an AWT Color.
204 *
205 * @param attr the text attributes
206 * @return the AWT Color
207 */
208 private Color attrToBackgroundColor(final CellAttributes attr) {
209 /*
210 * TODO:
211 * reverse
212 * blink
213 * underline
214 */
215 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
216 return MYBLACK;
217 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
218 return MYRED;
219 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
220 return MYBLUE;
221 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
222 return MYGREEN;
223 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
224 return MYYELLOW;
225 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
226 return MYCYAN;
227 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
228 return MYMAGENTA;
229 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
230 return MYWHITE;
231 }
232 throw new IllegalArgumentException("Invalid color: " + attr.getBackColor().getValue());
233 }
234
235 /**
236 * Public constructor.
237 *
238 * @param screen the Screen that Backend talks to
239 */
240 public AWTFrame(final AWTScreen screen) {
241 this.screen = screen;
242 setDOSColors();
243
244 setTitle("Jexer Application");
245 setBackground(Color.black);
246 // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
247 // setFont(new Font("Liberation Mono", Font.BOLD, 16));
248 // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
249
250 try {
251 // Always try to use Terminus, the one decent font.
252 ClassLoader loader = Thread.currentThread().getContextClassLoader();
253 InputStream in = loader.getResourceAsStream(FONTFILE);
254 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
255 Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22);
256 setFont(terminus);
257 } catch (Exception e) {
258 e.printStackTrace();
259 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
260 setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
261 }
262 pack();
263
264 // Kill the X11 cursor
265 // Transparent 16 x 16 pixel cursor image.
266 BufferedImage cursorImg = new BufferedImage(16, 16,
267 BufferedImage.TYPE_INT_ARGB);
268
269 // Create a new blank cursor.
270 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
271 cursorImg, new Point(0, 0), "blank cursor");
272 setCursor(blankCursor);
273 getFontDimensions();
274 }
275
276 /**
277 * Figure out my font dimensions.
278 */
279 private void getFontDimensions() {
280 Graphics gr = getGraphics();
281 FontMetrics fm = gr.getFontMetrics();
282 maxDescent = fm.getMaxDescent();
283 Rectangle2D bounds = fm.getMaxCharBounds(gr);
284 int leading = fm.getLeading();
285 textWidth = (int)Math.round(bounds.getWidth());
286 textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
287 // This also produces the same number, but works better for ugly
288 // monospace.
289 textHeight = fm.getMaxAscent() + maxDescent - leading;
290 }
291
292 /**
293 * Resize to font dimensions.
294 */
295 public void resizeToScreen() {
296 // Figure out the thickness of borders and use that to set the
297 // final size.
298 Insets insets = getInsets();
299 left = insets.left;
300 top = insets.top;
301
302 setSize(textWidth * screen.width + insets.left + insets.right,
303 textHeight * screen.height + insets.top + insets.bottom);
304 }
305
306 /**
307 * Update redraws the whole screen.
308 *
309 * @param gr the AWT Graphics context
310 */
311 @Override
312 public void update(final Graphics gr) {
313 // The default update clears the area. Don't do that, instead
314 // just paint it directly.
315 paint(gr);
316 }
317
318 /**
319 * Paint redraws the whole screen.
320 *
321 * @param gr the AWT Graphics context
322 */
323 @Override
324 public void paint(final Graphics gr) {
325 // Do nothing until the screen reference has been set.
326 if (screen == null) {
327 return;
328 }
329 if (screen.frame == null) {
330 return;
331 }
332
333 int xCellMin = 0;
334 int xCellMax = screen.width;
335 int yCellMin = 0;
336 int yCellMax = screen.height;
337
338 Rectangle bounds = gr.getClipBounds();
339 if (bounds != null) {
340 // Only update what is in the bounds
341 xCellMin = screen.textColumn(bounds.x);
342 xCellMax = screen.textColumn(bounds.x + bounds.width);
343 if (xCellMax > screen.width) {
344 xCellMax = screen.width;
345 }
346 if (xCellMin >= xCellMax) {
347 xCellMin = xCellMax - 2;
348 }
349 if (xCellMin < 0) {
350 xCellMin = 0;
351 }
352 yCellMin = screen.textRow(bounds.y);
353 yCellMax = screen.textRow(bounds.y + bounds.height);
354 if (yCellMax > screen.height) {
355 yCellMax = screen.height;
356 }
357 if (yCellMin >= yCellMax) {
358 yCellMin = yCellMax - 2;
359 }
360 if (yCellMin < 0) {
361 yCellMin = 0;
362 }
363 } else {
364 // We need a total repaint
365 reallyCleared = true;
366 }
367
368 // Prevent updates to the screen's data from the TApplication
369 // threads.
370 synchronized (screen) {
371 /*
372 System.err.printf("bounds %s X %d %d Y %d %d\n",
373 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
374 */
375
376 for (int y = yCellMin; y < yCellMax; y++) {
377 for (int x = xCellMin; x < xCellMax; x++) {
378
379 int xPixel = x * textWidth + left;
380 int yPixel = y * textHeight + top;
381
382 Cell lCell = screen.logical[x][y];
383 Cell pCell = screen.physical[x][y];
384
385 if (!lCell.equals(pCell) || reallyCleared) {
386 // Draw the background rectangle, then the
387 // foreground character.
388 gr.setColor(attrToBackgroundColor(lCell));
389 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
390 gr.setColor(attrToForegroundColor(lCell));
391 char [] chars = new char[1];
392 chars[0] = lCell.getChar();
393 gr.drawChars(chars, 0, 1, xPixel,
394 yPixel + textHeight - maxDescent);
395
396 // Physical is always updated
397 physical[x][y].setTo(lCell);
398 }
399 }
400 }
401
402 // Draw the cursor if it is visible
403 if ((cursorVisible)
404 && (cursorY <= screen.height - 1)
405 && (cursorX <= screen.width - 1)
406 ) {
407 int xPixel = cursorX * textWidth + left;
408 int yPixel = cursorY * textHeight + top;
409 Cell lCell = screen.logical[cursorX][cursorY];
410 gr.setColor(attrToForegroundColor(lCell));
411 gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2);
412 }
413
414 dirty = false;
415 reallyCleared = false;
416 } // synchronized (screen)
417 }
418
419 } // class AWTFrame
420
421 /**
422 * The raw AWT Frame. Note package private access.
423 */
424 AWTFrame frame;
425
426 /**
427 * Public constructor.
428 */
429 public AWTScreen() {
430 try {
431 SwingUtilities.invokeAndWait(new Runnable() {
432 public void run() {
433 AWTScreen.this.frame = new AWTFrame(AWTScreen.this);
434 AWTScreen.this.sessionInfo =
435 new AWTSessionInfo(AWTScreen.this.frame,
436 frame.textWidth,
437 frame.textHeight);
438
439 AWTScreen.this.setDimensions(sessionInfo.getWindowWidth(),
440 sessionInfo.getWindowHeight());
441
442 AWTScreen.this.frame.resizeToScreen();
443 AWTScreen.this.frame.setVisible(true);
444 }
445 } );
446 } catch (Exception e) {
447 e.printStackTrace();
448 }
449 }
450
451 /**
452 * The sessionInfo.
453 */
454 private AWTSessionInfo sessionInfo;
455
456 /**
457 * Create the AWTSessionInfo. Note package private access.
458 *
459 * @return the sessionInfo
460 */
461 AWTSessionInfo getSessionInfo() {
462 return sessionInfo;
463 }
464
465 /**
466 * Push the logical screen to the physical device.
467 */
468 @Override
469 public void flushPhysical() {
470
471 if (reallyCleared) {
472 // Really refreshed, do it all
473 frame.repaint();
474 return;
475 }
476
477 // Do nothing if nothing happened.
478 if (!dirty) {
479 return;
480 }
481
482 // Request a repaint, let the frame's repaint/update methods do the
483 // right thing.
484
485 // Find the minimum-size damaged region.
486 int xMin = frame.getWidth();
487 int xMax = 0;
488 int yMin = frame.getHeight();
489 int yMax = 0;
490
491 synchronized (this) {
492 for (int y = 0; y < height; y++) {
493 for (int x = 0; x < width; x++) {
494 Cell lCell = logical[x][y];
495 Cell pCell = physical[x][y];
496
497 int xPixel = x * frame.textWidth + frame.left;
498 int yPixel = y * frame.textHeight + frame.top;
499
500 if (!lCell.equals(pCell)
501 || ((x == cursorX)
502 && (y == cursorY)
503 && cursorVisible)
504 ) {
505 if (xPixel < xMin) {
506 xMin = xPixel;
507 }
508 if (xPixel + frame.textWidth > xMax) {
509 xMax = xPixel + frame.textWidth;
510 }
511 if (yPixel < yMin) {
512 yMin = yPixel;
513 }
514 if (yPixel + frame.textHeight > yMax) {
515 yMax = yPixel + frame.textHeight;
516 }
517 }
518 }
519 }
520 }
521 if (xMin + frame.textWidth >= xMax) {
522 xMax += frame.textWidth;
523 }
524 if (yMin + frame.textHeight >= yMax) {
525 yMax += frame.textHeight;
526 }
527
528 // Repaint the desired area
529 frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
530 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
531 }
532
533 /**
534 * Put the cursor at (x,y).
535 *
536 * @param visible if true, the cursor should be visible
537 * @param x column coordinate to put the cursor on
538 * @param y row coordinate to put the cursor on
539 */
540 @Override
541 public void putCursor(final boolean visible, final int x, final int y) {
542 if ((cursorVisible)
543 && (cursorY <= height - 1)
544 && (cursorX <= width - 1)
545 ) {
546 // Make the current cursor position dirty
547 if (physical[cursorX][cursorY].getChar() == 'Q') {
548 physical[cursorX][cursorY].setChar('X');
549 } else {
550 physical[cursorX][cursorY].setChar('Q');
551 }
552 }
553
554 super.putCursor(visible, x, y);
555 }
556
557 /**
558 * Convert pixel column position to text cell column position.
559 *
560 * @param x pixel column position
561 * @return text cell column position
562 */
563 public int textColumn(final int x) {
564 return ((x - frame.left) / frame.textWidth);
565 }
566
567 /**
568 * Convert pixel row position to text cell row position.
569 *
570 * @param y pixel row position
571 * @return text cell row position
572 */
573 public int textRow(final int y) {
574 return ((y - frame.top) / frame.textHeight);
575 }
576
577 }