Update copyright to 2017
[nikiroo-utils.git] / src / jexer / io / SwingScreen.java
CommitLineData
daa4106c 1/*
1ac2ccb1
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
1ac2ccb1 5 *
a2018e99 6 * Copyright (C) 2017 Kevin Lamonte
1ac2ccb1 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
1ac2ccb1 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
1ac2ccb1 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
1ac2ccb1
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.io;
30
1ac2ccb1
KL
31import java.awt.Color;
32import java.awt.Cursor;
33import java.awt.Font;
34import java.awt.FontMetrics;
1ac2ccb1 35import java.awt.Graphics;
c8165631 36import java.awt.Graphics2D;
84614868 37import java.awt.Insets;
30bd4abd
KL
38import java.awt.Point;
39import java.awt.Rectangle;
40import java.awt.Toolkit;
1ac2ccb1 41import java.awt.geom.Rectangle2D;
30bd4abd 42import java.awt.image.BufferedImage;
e3dfbd23 43import java.awt.image.BufferStrategy;
84614868 44import java.io.InputStream;
b5f2a6db 45import java.util.Date;
c8165631 46import java.util.HashMap;
aed33687
KL
47import javax.swing.JFrame;
48import javax.swing.SwingUtilities;
1ac2ccb1 49
b5f2a6db
KL
50import jexer.bits.Cell;
51import jexer.bits.CellAttributes;
52import jexer.session.SwingSessionInfo;
53
1ac2ccb1 54/**
a4406f4e 55 * This Screen implementation draws to a Java Swing JFrame.
1ac2ccb1 56 */
a4406f4e 57public final class SwingScreen extends Screen {
1ac2ccb1 58
e3dfbd23 59 /**
1d14ffab 60 * If true, use triple buffering thread.
e3dfbd23 61 */
1d14ffab 62 private static final boolean tripleBuffer = true;
e3dfbd23 63
b5f2a6db
KL
64 /**
65 * Cursor style to draw.
66 */
67 public enum CursorStyle {
68 /**
69 * Use an underscore for the cursor.
70 */
71 UNDERLINE,
72
73 /**
74 * Use a solid block for the cursor.
75 */
76 BLOCK,
77
78 /**
79 * Use an outlined block for the cursor.
80 */
81 OUTLINE
82 }
83
84614868
KL
84 private static Color MYBLACK;
85 private static Color MYRED;
86 private static Color MYGREEN;
87 private static Color MYYELLOW;
88 private static Color MYBLUE;
89 private static Color MYMAGENTA;
90 private static Color MYCYAN;
91 private static Color MYWHITE;
92
93 private static Color MYBOLD_BLACK;
94 private static Color MYBOLD_RED;
95 private static Color MYBOLD_GREEN;
96 private static Color MYBOLD_YELLOW;
97 private static Color MYBOLD_BLUE;
98 private static Color MYBOLD_MAGENTA;
99 private static Color MYBOLD_CYAN;
100 private static Color MYBOLD_WHITE;
101
102 private static boolean dosColors = false;
103
104 /**
a4406f4e 105 * Setup Swing colors to match DOS color palette.
84614868
KL
106 */
107 private static void setDOSColors() {
108 if (dosColors) {
109 return;
110 }
111 MYBLACK = new Color(0x00, 0x00, 0x00);
112 MYRED = new Color(0xa8, 0x00, 0x00);
113 MYGREEN = new Color(0x00, 0xa8, 0x00);
114 MYYELLOW = new Color(0xa8, 0x54, 0x00);
115 MYBLUE = new Color(0x00, 0x00, 0xa8);
116 MYMAGENTA = new Color(0xa8, 0x00, 0xa8);
117 MYCYAN = new Color(0x00, 0xa8, 0xa8);
118 MYWHITE = new Color(0xa8, 0xa8, 0xa8);
119 MYBOLD_BLACK = new Color(0x54, 0x54, 0x54);
120 MYBOLD_RED = new Color(0xfc, 0x54, 0x54);
121 MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54);
122 MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54);
123 MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc);
124 MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc);
125 MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc);
126 MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc);
127
128 dosColors = true;
129 }
130
1ac2ccb1 131 /**
a4406f4e 132 * SwingFrame is our top-level hook into the Swing system.
1ac2ccb1 133 */
a4406f4e 134 class SwingFrame extends JFrame {
1ac2ccb1 135
cf9af8df
KL
136 /**
137 * Serializable version.
138 */
139 private static final long serialVersionUID = 1;
140
84614868
KL
141 /**
142 * The terminus font resource filename.
143 */
144 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
145
e3dfbd23 146 /**
1d14ffab 147 * The BufferStrategy object needed for triple-buffering.
e3dfbd23
KL
148 */
149 private BufferStrategy bufferStrategy;
150
c8165631
KL
151 /**
152 * A cache of previously-rendered glyphs for blinking text, when it
153 * is not visible.
154 */
155 private HashMap<Cell, BufferedImage> glyphCacheBlink;
156
157 /**
158 * A cache of previously-rendered glyphs for non-blinking, or
159 * blinking-and-visible, text.
160 */
161 private HashMap<Cell, BufferedImage> glyphCache;
162
1ac2ccb1
KL
163 /**
164 * The TUI Screen data.
165 */
a4406f4e 166 SwingScreen screen;
1ac2ccb1 167
55d2b2c2
KL
168 /**
169 * If true, we were successful getting Terminus.
170 */
171 private boolean gotTerminus = false;
172
1ac2ccb1
KL
173 /**
174 * Width of a character cell.
175 */
176 private int textWidth = 1;
177
178 /**
179 * Height of a character cell.
180 */
181 private int textHeight = 1;
182
84614868
KL
183 /**
184 * Descent of a character cell.
185 */
186 private int maxDescent = 0;
187
847a4bc5
KL
188 /**
189 * System-dependent Y adjustment for text in the character cell.
190 */
191 private int textAdjustY = 0;
192
193 /**
194 * System-dependent X adjustment for text in the character cell.
195 */
196 private int textAdjustX = 0;
197
1ac2ccb1 198 /**
b5f2a6db 199 * Top pixel absolute location.
1ac2ccb1
KL
200 */
201 private int top = 30;
84614868 202
1ac2ccb1 203 /**
b5f2a6db 204 * Left pixel absolute location.
1ac2ccb1
KL
205 */
206 private int left = 30;
84614868 207
b5f2a6db
KL
208 /**
209 * The cursor style to draw.
210 */
211 private CursorStyle cursorStyle = CursorStyle.UNDERLINE;
212
213 /**
214 * The number of millis to wait before switching the blink from
215 * visible to invisible.
216 */
217 private long blinkMillis = 500;
218
219 /**
220 * If true, the cursor should be visible right now based on the blink
221 * time.
222 */
223 private boolean cursorBlinkVisible = true;
224
225 /**
226 * The time that the blink last flipped from visible to invisible or
227 * from invisible to visible.
228 */
229 private long lastBlinkTime = 0;
230
84614868 231 /**
a4406f4e 232 * Convert a CellAttributes foreground color to an Swing Color.
84614868
KL
233 *
234 * @param attr the text attributes
a4406f4e 235 * @return the Swing Color
84614868
KL
236 */
237 private Color attrToForegroundColor(final CellAttributes attr) {
7c870d89 238 if (attr.isBold()) {
84614868
KL
239 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
240 return MYBOLD_BLACK;
241 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
242 return MYBOLD_RED;
243 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
244 return MYBOLD_BLUE;
245 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
246 return MYBOLD_GREEN;
247 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
248 return MYBOLD_YELLOW;
249 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
250 return MYBOLD_CYAN;
251 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
252 return MYBOLD_MAGENTA;
253 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
254 return MYBOLD_WHITE;
255 }
256 } else {
257 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
258 return MYBLACK;
259 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
260 return MYRED;
261 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
262 return MYBLUE;
263 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
264 return MYGREEN;
265 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
266 return MYYELLOW;
267 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
268 return MYCYAN;
269 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
270 return MYMAGENTA;
271 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
272 return MYWHITE;
273 }
274 }
c8165631
KL
275 throw new IllegalArgumentException("Invalid color: " +
276 attr.getForeColor().getValue());
84614868
KL
277 }
278
279 /**
a4406f4e 280 * Convert a CellAttributes background color to an Swing Color.
84614868
KL
281 *
282 * @param attr the text attributes
a4406f4e 283 * @return the Swing Color
84614868
KL
284 */
285 private Color attrToBackgroundColor(final CellAttributes attr) {
84614868
KL
286 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
287 return MYBLACK;
288 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
289 return MYRED;
290 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
291 return MYBLUE;
292 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
293 return MYGREEN;
294 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
295 return MYYELLOW;
296 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
297 return MYCYAN;
298 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
299 return MYMAGENTA;
300 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
301 return MYWHITE;
302 }
c8165631
KL
303 throw new IllegalArgumentException("Invalid color: " +
304 attr.getBackColor().getValue());
84614868
KL
305 }
306
1ac2ccb1
KL
307 /**
308 * Public constructor.
84614868
KL
309 *
310 * @param screen the Screen that Backend talks to
1ac2ccb1 311 */
a4406f4e 312 public SwingFrame(final SwingScreen screen) {
84614868
KL
313 this.screen = screen;
314 setDOSColors();
315
b5f2a6db 316 // Figure out my cursor style
c8165631
KL
317 String cursorStyleString = System.getProperty(
318 "jexer.Swing.cursorStyle", "underline").toLowerCase();
b5f2a6db
KL
319
320 if (cursorStyleString.equals("underline")) {
321 cursorStyle = CursorStyle.UNDERLINE;
322 } else if (cursorStyleString.equals("outline")) {
323 cursorStyle = CursorStyle.OUTLINE;
324 } else if (cursorStyleString.equals("block")) {
325 cursorStyle = CursorStyle.BLOCK;
326 }
327
1ac2ccb1 328 setTitle("Jexer Application");
aed33687 329 setBackground(Color.black);
84614868
KL
330
331 try {
30bd4abd 332 // Always try to use Terminus, the one decent font.
c8165631
KL
333 ClassLoader loader = Thread.currentThread().
334 getContextClassLoader();
84614868
KL
335 InputStream in = loader.getResourceAsStream(FONTFILE);
336 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
55d2b2c2 337 Font terminus = terminusRoot.deriveFont(Font.PLAIN, 20);
84614868 338 setFont(terminus);
55d2b2c2 339 gotTerminus = true;
84614868
KL
340 } catch (Exception e) {
341 e.printStackTrace();
342 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
343 setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
344 }
aed33687 345 pack();
30bd4abd
KL
346
347 // Kill the X11 cursor
348 // Transparent 16 x 16 pixel cursor image.
349 BufferedImage cursorImg = new BufferedImage(16, 16,
350 BufferedImage.TYPE_INT_ARGB);
30bd4abd
KL
351 // Create a new blank cursor.
352 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
353 cursorImg, new Point(0, 0), "blank cursor");
354 setCursor(blankCursor);
091d8a06
KL
355
356 // Be capable of seeing Tab / Shift-Tab
357 setFocusTraversalKeysEnabled(false);
358
359 // Save the text cell width/height
bd8d51fa 360 getFontDimensions();
e3dfbd23 361
c8165631
KL
362 // Cache glyphs as they are rendered
363 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
364 glyphCache = new HashMap<Cell, BufferedImage>();
365
1d14ffab
KL
366 // Setup triple-buffering
367 if (SwingScreen.tripleBuffer) {
e3dfbd23 368 setIgnoreRepaint(true);
1d14ffab 369 createBufferStrategy(3);
e3dfbd23
KL
370 bufferStrategy = getBufferStrategy();
371 }
1ac2ccb1
KL
372 }
373
374 /**
bd8d51fa 375 * Figure out my font dimensions.
1ac2ccb1 376 */
bd8d51fa 377 private void getFontDimensions() {
84614868
KL
378 Graphics gr = getGraphics();
379 FontMetrics fm = gr.getFontMetrics();
380 maxDescent = fm.getMaxDescent();
381 Rectangle2D bounds = fm.getMaxCharBounds(gr);
382 int leading = fm.getLeading();
383 textWidth = (int)Math.round(bounds.getWidth());
55d2b2c2
KL
384 // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
385
386 // This produces the same number, but works better for ugly
84614868
KL
387 // monospace.
388 textHeight = fm.getMaxAscent() + maxDescent - leading;
847a4bc5 389
55d2b2c2
KL
390 if (gotTerminus == true) {
391 textHeight++;
392 }
393
847a4bc5
KL
394 if (System.getProperty("os.name").startsWith("Windows")) {
395 textAdjustY = -1;
396 textAdjustX = 0;
397 }
55d2b2c2
KL
398 if (System.getProperty("os.name").startsWith("Mac")) {
399 textAdjustY = -1;
400 textAdjustX = 0;
401 }
bd8d51fa 402 }
84614868 403
bd8d51fa
KL
404 /**
405 * Resize to font dimensions.
406 */
407 public void resizeToScreen() {
84614868
KL
408 // Figure out the thickness of borders and use that to set the
409 // final size.
410 Insets insets = getInsets();
411 left = insets.left;
412 top = insets.top;
413
414 setSize(textWidth * screen.width + insets.left + insets.right,
415 textHeight * screen.height + insets.top + insets.bottom);
1ac2ccb1
KL
416 }
417
30bd4abd
KL
418 /**
419 * Update redraws the whole screen.
420 *
a4406f4e 421 * @param gr the Swing Graphics context
30bd4abd
KL
422 */
423 @Override
424 public void update(final Graphics gr) {
425 // The default update clears the area. Don't do that, instead
426 // just paint it directly.
427 paint(gr);
428 }
429
c8165631
KL
430 /**
431 * Draw one glyph to the screen.
432 *
433 * @param gr the Swing Graphics context
434 * @param cell the Cell to draw
435 * @param xPixel the x-coordinate to render to. 0 means the
436 * left-most pixel column.
437 * @param yPixel the y-coordinate to render to. 0 means the top-most
438 * pixel row.
439 */
440 private void drawGlyph(final Graphics gr, final Cell cell,
441 final int xPixel, final int yPixel) {
442
443 BufferedImage image = null;
444 if (cell.isBlink() && !cursorBlinkVisible) {
445 image = glyphCacheBlink.get(cell);
446 } else {
447 image = glyphCache.get(cell);
448 }
449 if (image != null) {
450 gr.drawImage(image, xPixel, yPixel, this);
451 return;
452 }
453
454 // Generate glyph and draw it.
455
456 image = new BufferedImage(textWidth, textHeight,
457 BufferedImage.TYPE_INT_ARGB);
458 Graphics2D gr2 = image.createGraphics();
459 gr2.setFont(getFont());
460
461 Cell cellColor = new Cell();
462 cellColor.setTo(cell);
463
464 // Check for reverse
465 if (cell.isReverse()) {
466 cellColor.setForeColor(cell.getBackColor());
467 cellColor.setBackColor(cell.getForeColor());
468 }
469
470 // Draw the background rectangle, then the foreground character.
471 gr2.setColor(attrToBackgroundColor(cellColor));
472 gr2.fillRect(0, 0, textWidth, textHeight);
473
474 // Handle blink and underline
475 if (!cell.isBlink()
476 || (cell.isBlink() && cursorBlinkVisible)
477 ) {
478 gr2.setColor(attrToForegroundColor(cellColor));
479 char [] chars = new char[1];
480 chars[0] = cell.getChar();
481 gr2.drawChars(chars, 0, 1, 0 + textAdjustX,
482 0 + textHeight - maxDescent + textAdjustY);
483
484 if (cell.isUnderline()) {
485 gr2.fillRect(0, 0 + textHeight - 2, textWidth, 2);
486 }
487 }
488 gr2.dispose();
489
490 // We need a new key that will not be mutated by invertCell().
491 Cell key = new Cell();
492 key.setTo(cell);
493 if (cell.isBlink() && !cursorBlinkVisible) {
494 glyphCacheBlink.put(key, image);
495 } else {
496 glyphCache.put(key, image);
497 }
498
499 gr.drawImage(image, xPixel, yPixel, this);
500 }
501
502 /**
503 * Check if the cursor is visible, and if so draw it.
504 *
505 * @param gr the Swing Graphics context
506 */
507 private void drawCursor(final Graphics gr) {
508
509 if (cursorVisible
510 && (cursorY <= screen.height - 1)
511 && (cursorX <= screen.width - 1)
512 && cursorBlinkVisible
513 ) {
514 int xPixel = cursorX * textWidth + left;
515 int yPixel = cursorY * textHeight + top;
516 Cell lCell = screen.logical[cursorX][cursorY];
517 gr.setColor(attrToForegroundColor(lCell));
518 switch (cursorStyle) {
519 default:
520 // Fall through...
521 case UNDERLINE:
522 gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2);
523 break;
524 case BLOCK:
525 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
526 break;
527 case OUTLINE:
528 gr.drawRect(xPixel, yPixel, textWidth - 1, textHeight - 1);
529 break;
530 }
531 }
532 }
533
1ac2ccb1
KL
534 /**
535 * Paint redraws the whole screen.
536 *
a4406f4e 537 * @param gr the Swing Graphics context
1ac2ccb1
KL
538 */
539 @Override
84614868 540 public void paint(final Graphics gr) {
87a17f3c
KL
541 // Do nothing until the screen reference has been set.
542 if (screen == null) {
543 return;
544 }
545 if (screen.frame == null) {
546 return;
547 }
548
b5f2a6db
KL
549 // See if it is time to flip the blink time.
550 long nowTime = (new Date()).getTime();
551 if (nowTime > blinkMillis + lastBlinkTime) {
552 lastBlinkTime = nowTime;
553 cursorBlinkVisible = !cursorBlinkVisible;
554 }
555
87a17f3c
KL
556 int xCellMin = 0;
557 int xCellMax = screen.width;
558 int yCellMin = 0;
559 int yCellMax = screen.height;
560
30bd4abd 561 Rectangle bounds = gr.getClipBounds();
87a17f3c
KL
562 if (bounds != null) {
563 // Only update what is in the bounds
564 xCellMin = screen.textColumn(bounds.x);
bd8d51fa 565 xCellMax = screen.textColumn(bounds.x + bounds.width);
87a17f3c
KL
566 if (xCellMax > screen.width) {
567 xCellMax = screen.width;
568 }
569 if (xCellMin >= xCellMax) {
570 xCellMin = xCellMax - 2;
571 }
572 if (xCellMin < 0) {
573 xCellMin = 0;
574 }
575 yCellMin = screen.textRow(bounds.y);
bd8d51fa 576 yCellMax = screen.textRow(bounds.y + bounds.height);
87a17f3c
KL
577 if (yCellMax > screen.height) {
578 yCellMax = screen.height;
579 }
580 if (yCellMin >= yCellMax) {
581 yCellMin = yCellMax - 2;
582 }
583 if (yCellMin < 0) {
584 yCellMin = 0;
585 }
bd8d51fa
KL
586 } else {
587 // We need a total repaint
588 reallyCleared = true;
87a17f3c 589 }
1ac2ccb1 590
87a17f3c
KL
591 // Prevent updates to the screen's data from the TApplication
592 // threads.
593 synchronized (screen) {
594 /*
595 System.err.printf("bounds %s X %d %d Y %d %d\n",
596 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
597 */
598
599 for (int y = yCellMin; y < yCellMax; y++) {
600 for (int x = xCellMin; x < xCellMax; x++) {
601
602 int xPixel = x * textWidth + left;
603 int yPixel = y * textHeight + top;
604
605 Cell lCell = screen.logical[x][y];
606 Cell pCell = screen.physical[x][y];
607
55b4f29b
KL
608 if (!lCell.equals(pCell)
609 || lCell.isBlink()
610 || reallyCleared) {
e3dfbd23 611
c8165631 612 drawGlyph(gr, lCell, xPixel, yPixel);
87a17f3c
KL
613
614 // Physical is always updated
615 physical[x][y].setTo(lCell);
30bd4abd
KL
616 }
617 }
87a17f3c 618 }
c8165631 619 drawCursor(gr);
30bd4abd 620
87a17f3c
KL
621 dirty = false;
622 reallyCleared = false;
623 } // synchronized (screen)
1ac2ccb1 624 }
bd8d51fa 625
a4406f4e 626 } // class SwingFrame
1ac2ccb1
KL
627
628 /**
a4406f4e 629 * The raw Swing JFrame. Note package private access.
1ac2ccb1 630 */
a4406f4e 631 SwingFrame frame;
1ac2ccb1 632
e3dfbd23
KL
633 /**
634 * Restore terminal to normal state.
635 */
636 public void shutdown() {
637 frame.dispose();
638 }
639
1ac2ccb1
KL
640 /**
641 * Public constructor.
642 */
a4406f4e 643 public SwingScreen() {
aed33687
KL
644 try {
645 SwingUtilities.invokeAndWait(new Runnable() {
646 public void run() {
a4406f4e
KL
647 SwingScreen.this.frame = new SwingFrame(SwingScreen.this);
648 SwingScreen.this.sessionInfo =
649 new SwingSessionInfo(SwingScreen.this.frame,
bd8d51fa
KL
650 frame.textWidth,
651 frame.textHeight);
652
a4406f4e 653 SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(),
bd8d51fa
KL
654 sessionInfo.getWindowHeight());
655
a4406f4e
KL
656 SwingScreen.this.frame.resizeToScreen();
657 SwingScreen.this.frame.setVisible(true);
aed33687 658 }
329fd62e 659 });
aed33687
KL
660 } catch (Exception e) {
661 e.printStackTrace();
662 }
1ac2ccb1
KL
663 }
664
bd8d51fa
KL
665 /**
666 * The sessionInfo.
667 */
a4406f4e 668 private SwingSessionInfo sessionInfo;
bd8d51fa 669
30bd4abd 670 /**
a4406f4e 671 * Create the SwingSessionInfo. Note package private access.
30bd4abd
KL
672 *
673 * @return the sessionInfo
674 */
a4406f4e 675 SwingSessionInfo getSessionInfo() {
30bd4abd
KL
676 return sessionInfo;
677 }
678
1ac2ccb1
KL
679 /**
680 * Push the logical screen to the physical device.
681 */
682 @Override
683 public void flushPhysical() {
87a17f3c 684
1d14ffab
KL
685 /*
686 System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n",
687 reallyCleared, dirty);
688 */
689
690 // If reallyCleared is set, we have to draw everything.
691 if ((frame.bufferStrategy != null) && (reallyCleared == true)) {
692 // Triple-buffering: we have to redraw everything on this thread.
693 Graphics gr = frame.bufferStrategy.getDrawGraphics();
694 frame.paint(gr);
695 gr.dispose();
696 frame.bufferStrategy.show();
697 // sync() doesn't seem to help the tearing for me.
698 // Toolkit.getDefaultToolkit().sync();
699 return;
700 } else if ((frame.bufferStrategy == null) && (reallyCleared == true)) {
701 // Repaint everything on the Swing thread.
702 frame.repaint();
87a17f3c
KL
703 return;
704 }
705
706 // Do nothing if nothing happened.
707 if (!dirty) {
708 return;
709 }
710
c8165631
KL
711 if (frame.bufferStrategy != null) {
712 // See if it is time to flip the blink time.
713 long nowTime = (new Date()).getTime();
714 if (nowTime > frame.blinkMillis + frame.lastBlinkTime) {
715 frame.lastBlinkTime = nowTime;
716 frame.cursorBlinkVisible = !frame.cursorBlinkVisible;
717 }
718
719 Graphics gr = frame.bufferStrategy.getDrawGraphics();
720
721 synchronized (this) {
722 for (int y = 0; y < height; y++) {
723 for (int x = 0; x < width; x++) {
724 Cell lCell = logical[x][y];
725 Cell pCell = physical[x][y];
726
727 int xPixel = x * frame.textWidth + frame.left;
728 int yPixel = y * frame.textHeight + frame.top;
729
730 if (!lCell.equals(pCell)
731 || ((x == cursorX)
732 && (y == cursorY)
733 && cursorVisible)
734 || (lCell.isBlink())
735 ) {
736 frame.drawGlyph(gr, lCell, xPixel, yPixel);
737 physical[x][y].setTo(lCell);
738 }
739 }
740 }
741 frame.drawCursor(gr);
742 } // synchronized (this)
743
744 gr.dispose();
745 frame.bufferStrategy.show();
746 // sync() doesn't seem to help the tearing for me.
747 // Toolkit.getDefaultToolkit().sync();
748 return;
749 }
750
751 // Swing thread version: request a repaint, but limit it to the area
752 // that has changed.
87a17f3c 753
30bd4abd
KL
754 // Find the minimum-size damaged region.
755 int xMin = frame.getWidth();
756 int xMax = 0;
757 int yMin = frame.getHeight();
758 int yMax = 0;
30bd4abd 759
87a17f3c
KL
760 synchronized (this) {
761 for (int y = 0; y < height; y++) {
762 for (int x = 0; x < width; x++) {
763 Cell lCell = logical[x][y];
764 Cell pCell = physical[x][y];
765
766 int xPixel = x * frame.textWidth + frame.left;
767 int yPixel = y * frame.textHeight + frame.top;
768
769 if (!lCell.equals(pCell)
770 || ((x == cursorX)
771 && (y == cursorY)
772 && cursorVisible)
55b4f29b 773 || lCell.isBlink()
87a17f3c
KL
774 ) {
775 if (xPixel < xMin) {
776 xMin = xPixel;
777 }
778 if (xPixel + frame.textWidth > xMax) {
779 xMax = xPixel + frame.textWidth;
780 }
781 if (yPixel < yMin) {
782 yMin = yPixel;
783 }
784 if (yPixel + frame.textHeight > yMax) {
785 yMax = yPixel + frame.textHeight;
786 }
30bd4abd
KL
787 }
788 }
789 }
790 }
87a17f3c
KL
791 if (xMin + frame.textWidth >= xMax) {
792 xMax += frame.textWidth;
793 }
794 if (yMin + frame.textHeight >= yMax) {
795 yMax += frame.textHeight;
796 }
30bd4abd 797
87a17f3c 798 // Repaint the desired area
1d14ffab
KL
799 /*
800 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
801 yMin, yMax);
802 */
803 if (frame.bufferStrategy != null) {
c8165631
KL
804 // This path should never be taken, but is left here for
805 // completeness.
e3dfbd23
KL
806 Graphics gr = frame.bufferStrategy.getDrawGraphics();
807 Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin,
808 yMax - yMin);
809 gr.setClip(bounds);
810 frame.paint(gr);
811 gr.dispose();
812 frame.bufferStrategy.show();
1d14ffab
KL
813 // sync() doesn't seem to help the tearing for me.
814 // Toolkit.getDefaultToolkit().sync();
e3dfbd23 815 } else {
c8165631 816 // Repaint on the Swing thread.
e3dfbd23
KL
817 frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
818 }
1ac2ccb1 819 }
30bd4abd
KL
820
821 /**
822 * Put the cursor at (x,y).
823 *
824 * @param visible if true, the cursor should be visible
825 * @param x column coordinate to put the cursor on
826 * @param y row coordinate to put the cursor on
827 */
828 @Override
829 public void putCursor(final boolean visible, final int x, final int y) {
b5f2a6db
KL
830
831 if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) {
832 // See if it is time to flip the blink time.
833 long nowTime = (new Date()).getTime();
834 if (nowTime < frame.blinkMillis + frame.lastBlinkTime) {
835 // Nothing has changed, so don't do anything.
836 return;
837 }
838 }
839
840 if (cursorVisible
30bd4abd
KL
841 && (cursorY <= height - 1)
842 && (cursorX <= width - 1)
843 ) {
844 // Make the current cursor position dirty
87a17f3c 845 if (physical[cursorX][cursorY].getChar() == 'Q') {
30bd4abd
KL
846 physical[cursorX][cursorY].setChar('X');
847 } else {
87a17f3c 848 physical[cursorX][cursorY].setChar('Q');
30bd4abd
KL
849 }
850 }
851
852 super.putCursor(visible, x, y);
853 }
854
87a17f3c
KL
855 /**
856 * Convert pixel column position to text cell column position.
857 *
858 * @param x pixel column position
859 * @return text cell column position
860 */
861 public int textColumn(final int x) {
862 return ((x - frame.left) / frame.textWidth);
863 }
864
865 /**
866 * Convert pixel row position to text cell row position.
867 *
868 * @param y pixel row position
869 * @return text cell row position
870 */
871 public int textRow(final int y) {
872 return ((y - frame.top) / frame.textHeight);
873 }
874
55d2b2c2
KL
875 /**
876 * Set the window title.
877 *
878 * @param title the new title
879 */
880 public void setTitle(final String title) {
881 frame.setTitle(title);
882 }
883
1ac2ccb1 884}