Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer.io; | |
30 | ||
1ac2ccb1 KL |
31 | import java.awt.Color; |
32 | import java.awt.Cursor; | |
33 | import java.awt.Font; | |
34 | import java.awt.FontMetrics; | |
1ac2ccb1 | 35 | import java.awt.Graphics; |
c8165631 | 36 | import java.awt.Graphics2D; |
84614868 | 37 | import java.awt.Insets; |
30bd4abd KL |
38 | import java.awt.Point; |
39 | import java.awt.Rectangle; | |
40 | import java.awt.Toolkit; | |
1ac2ccb1 | 41 | import java.awt.geom.Rectangle2D; |
30bd4abd | 42 | import java.awt.image.BufferedImage; |
e3dfbd23 | 43 | import java.awt.image.BufferStrategy; |
84614868 | 44 | import java.io.InputStream; |
b5f2a6db | 45 | import java.util.Date; |
c8165631 | 46 | import java.util.HashMap; |
aed33687 KL |
47 | import javax.swing.JFrame; |
48 | import javax.swing.SwingUtilities; | |
1ac2ccb1 | 49 | |
b5f2a6db KL |
50 | import jexer.bits.Cell; |
51 | import jexer.bits.CellAttributes; | |
52 | import jexer.session.SwingSessionInfo; | |
53 | ||
1ac2ccb1 | 54 | /** |
a4406f4e | 55 | * This Screen implementation draws to a Java Swing JFrame. |
1ac2ccb1 | 56 | */ |
a4406f4e | 57 | public final class SwingScreen extends Screen { |
1ac2ccb1 | 58 | |
e3dfbd23 | 59 | /** |
1d14ffab | 60 | * If true, use triple buffering thread. |
e3dfbd23 | 61 | */ |
e685a47d | 62 | private static 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 | ||
0ab16dbb KL |
328 | if (System.getProperty("jexer.Swing.tripleBuffer") != null) { |
329 | if (System.getProperty("jexer.Swing.tripleBuffer"). | |
330 | equals("false")) { | |
e685a47d | 331 | |
0ab16dbb KL |
332 | SwingScreen.tripleBuffer = false; |
333 | } | |
e685a47d KL |
334 | } |
335 | ||
1ac2ccb1 | 336 | setTitle("Jexer Application"); |
aed33687 | 337 | setBackground(Color.black); |
84614868 KL |
338 | |
339 | try { | |
30bd4abd | 340 | // Always try to use Terminus, the one decent font. |
c8165631 KL |
341 | ClassLoader loader = Thread.currentThread(). |
342 | getContextClassLoader(); | |
84614868 KL |
343 | InputStream in = loader.getResourceAsStream(FONTFILE); |
344 | Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in); | |
55d2b2c2 | 345 | Font terminus = terminusRoot.deriveFont(Font.PLAIN, 20); |
84614868 | 346 | setFont(terminus); |
55d2b2c2 | 347 | gotTerminus = true; |
84614868 KL |
348 | } catch (Exception e) { |
349 | e.printStackTrace(); | |
350 | // setFont(new Font("Liberation Mono", Font.PLAIN, 24)); | |
351 | setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24)); | |
352 | } | |
aed33687 | 353 | pack(); |
30bd4abd KL |
354 | |
355 | // Kill the X11 cursor | |
356 | // Transparent 16 x 16 pixel cursor image. | |
357 | BufferedImage cursorImg = new BufferedImage(16, 16, | |
358 | BufferedImage.TYPE_INT_ARGB); | |
30bd4abd KL |
359 | // Create a new blank cursor. |
360 | Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor( | |
361 | cursorImg, new Point(0, 0), "blank cursor"); | |
362 | setCursor(blankCursor); | |
091d8a06 KL |
363 | |
364 | // Be capable of seeing Tab / Shift-Tab | |
365 | setFocusTraversalKeysEnabled(false); | |
366 | ||
367 | // Save the text cell width/height | |
bd8d51fa | 368 | getFontDimensions(); |
e3dfbd23 | 369 | |
c8165631 KL |
370 | // Cache glyphs as they are rendered |
371 | glyphCacheBlink = new HashMap<Cell, BufferedImage>(); | |
372 | glyphCache = new HashMap<Cell, BufferedImage>(); | |
373 | ||
1d14ffab KL |
374 | // Setup triple-buffering |
375 | if (SwingScreen.tripleBuffer) { | |
e3dfbd23 | 376 | setIgnoreRepaint(true); |
1d14ffab | 377 | createBufferStrategy(3); |
e3dfbd23 KL |
378 | bufferStrategy = getBufferStrategy(); |
379 | } | |
1ac2ccb1 KL |
380 | } |
381 | ||
6ba187ac KL |
382 | /** |
383 | * Figure out what textAdjustX and textAdjustY should be, based on | |
384 | * the location of a vertical bar (to find textAdjustY) and a | |
385 | * horizontal bar (to find textAdjustX). | |
386 | * | |
387 | * @return true if textAdjustX and textAdjustY were guessed at | |
388 | * correctly | |
389 | */ | |
390 | private boolean getFontAdjustments() { | |
391 | BufferedImage image = null; | |
392 | ||
393 | // What SHOULD happen is that the topmost/leftmost white pixel is | |
394 | // at position (gr2x, gr2y). But it might also be off by a pixel | |
395 | // in either direction. | |
396 | ||
397 | Graphics2D gr2 = null; | |
398 | int gr2x = 3; | |
399 | int gr2y = 3; | |
400 | image = new BufferedImage(textWidth * 2, textHeight * 2, | |
401 | BufferedImage.TYPE_INT_ARGB); | |
402 | ||
403 | gr2 = image.createGraphics(); | |
404 | gr2.setFont(getFont()); | |
405 | gr2.setColor(java.awt.Color.BLACK); | |
406 | gr2.fillRect(0, 0, textWidth * 2, textHeight * 2); | |
407 | gr2.setColor(java.awt.Color.WHITE); | |
408 | char [] chars = new char[1]; | |
409 | chars[0] = jexer.bits.GraphicsChars.VERTICAL_BAR; | |
410 | gr2.drawChars(chars, 0, 1, gr2x, gr2y + textHeight - maxDescent); | |
411 | gr2.dispose(); | |
412 | ||
413 | for (int x = 0; x < textWidth; x++) { | |
414 | for (int y = 0; y < textHeight; y++) { | |
415 | ||
416 | /* | |
417 | System.err.println("X: " + x + " Y: " + y + " " + | |
418 | image.getRGB(x, y)); | |
419 | */ | |
420 | ||
421 | if ((image.getRGB(x, y) & 0xFFFFFF) != 0) { | |
422 | textAdjustY = (gr2y - y); | |
423 | ||
424 | // System.err.println("textAdjustY: " + textAdjustY); | |
425 | x = textWidth; | |
426 | break; | |
427 | } | |
428 | } | |
429 | } | |
430 | ||
431 | gr2 = image.createGraphics(); | |
432 | gr2.setFont(getFont()); | |
433 | gr2.setColor(java.awt.Color.BLACK); | |
434 | gr2.fillRect(0, 0, textWidth * 2, textHeight * 2); | |
435 | gr2.setColor(java.awt.Color.WHITE); | |
436 | chars[0] = jexer.bits.GraphicsChars.SINGLE_BAR; | |
437 | gr2.drawChars(chars, 0, 1, gr2x, gr2y + textHeight - maxDescent); | |
438 | gr2.dispose(); | |
439 | ||
440 | for (int x = 0; x < textWidth; x++) { | |
441 | for (int y = 0; y < textHeight; y++) { | |
442 | ||
443 | /* | |
444 | System.err.println("X: " + x + " Y: " + y + " " + | |
445 | image.getRGB(x, y)); | |
446 | */ | |
447 | ||
448 | if ((image.getRGB(x, y) & 0xFFFFFF) != 0) { | |
449 | textAdjustX = (gr2x - x); | |
450 | ||
451 | // System.err.println("textAdjustX: " + textAdjustX); | |
452 | return true; | |
453 | } | |
454 | } | |
455 | } | |
456 | ||
457 | // Something weird happened, don't rely on this function. | |
458 | // System.err.println("getFontAdjustments: false"); | |
459 | return false; | |
460 | } | |
461 | ||
462 | ||
1ac2ccb1 | 463 | /** |
bd8d51fa | 464 | * Figure out my font dimensions. |
1ac2ccb1 | 465 | */ |
bd8d51fa | 466 | private void getFontDimensions() { |
84614868 KL |
467 | Graphics gr = getGraphics(); |
468 | FontMetrics fm = gr.getFontMetrics(); | |
469 | maxDescent = fm.getMaxDescent(); | |
470 | Rectangle2D bounds = fm.getMaxCharBounds(gr); | |
471 | int leading = fm.getLeading(); | |
472 | textWidth = (int)Math.round(bounds.getWidth()); | |
55d2b2c2 KL |
473 | // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent; |
474 | ||
475 | // This produces the same number, but works better for ugly | |
84614868 KL |
476 | // monospace. |
477 | textHeight = fm.getMaxAscent() + maxDescent - leading; | |
847a4bc5 | 478 | |
55d2b2c2 KL |
479 | if (gotTerminus == true) { |
480 | textHeight++; | |
481 | } | |
482 | ||
6ba187ac KL |
483 | if (getFontAdjustments() == false) { |
484 | // We were unable to programmatically determine textAdjustX | |
485 | // and textAdjustY, so try some guesses based on operating | |
486 | // system. | |
487 | if (System.getProperty("os.name").startsWith("Windows")) { | |
488 | textAdjustY = -1; | |
489 | textAdjustX = 0; | |
490 | } | |
491 | if (System.getProperty("os.name").startsWith("Mac")) { | |
492 | textAdjustY = -1; | |
493 | textAdjustX = 0; | |
494 | } | |
55d2b2c2 | 495 | } |
bd8d51fa | 496 | } |
84614868 | 497 | |
bd8d51fa KL |
498 | /** |
499 | * Resize to font dimensions. | |
500 | */ | |
501 | public void resizeToScreen() { | |
84614868 KL |
502 | // Figure out the thickness of borders and use that to set the |
503 | // final size. | |
504 | Insets insets = getInsets(); | |
505 | left = insets.left; | |
506 | top = insets.top; | |
507 | ||
508 | setSize(textWidth * screen.width + insets.left + insets.right, | |
509 | textHeight * screen.height + insets.top + insets.bottom); | |
1ac2ccb1 KL |
510 | } |
511 | ||
30bd4abd KL |
512 | /** |
513 | * Update redraws the whole screen. | |
514 | * | |
a4406f4e | 515 | * @param gr the Swing Graphics context |
30bd4abd KL |
516 | */ |
517 | @Override | |
518 | public void update(final Graphics gr) { | |
519 | // The default update clears the area. Don't do that, instead | |
520 | // just paint it directly. | |
521 | paint(gr); | |
522 | } | |
523 | ||
c8165631 KL |
524 | /** |
525 | * Draw one glyph to the screen. | |
526 | * | |
527 | * @param gr the Swing Graphics context | |
528 | * @param cell the Cell to draw | |
529 | * @param xPixel the x-coordinate to render to. 0 means the | |
530 | * left-most pixel column. | |
531 | * @param yPixel the y-coordinate to render to. 0 means the top-most | |
532 | * pixel row. | |
533 | */ | |
534 | private void drawGlyph(final Graphics gr, final Cell cell, | |
535 | final int xPixel, final int yPixel) { | |
536 | ||
537 | BufferedImage image = null; | |
538 | if (cell.isBlink() && !cursorBlinkVisible) { | |
539 | image = glyphCacheBlink.get(cell); | |
540 | } else { | |
541 | image = glyphCache.get(cell); | |
542 | } | |
543 | if (image != null) { | |
544 | gr.drawImage(image, xPixel, yPixel, this); | |
545 | return; | |
546 | } | |
547 | ||
548 | // Generate glyph and draw it. | |
e685a47d KL |
549 | Graphics2D gr2 = null; |
550 | int gr2x = xPixel; | |
551 | int gr2y = yPixel; | |
552 | if (tripleBuffer) { | |
553 | image = new BufferedImage(textWidth, textHeight, | |
554 | BufferedImage.TYPE_INT_ARGB); | |
555 | gr2 = image.createGraphics(); | |
556 | gr2.setFont(getFont()); | |
557 | gr2x = 0; | |
558 | gr2y = 0; | |
559 | } else { | |
560 | gr2 = (Graphics2D) gr; | |
561 | } | |
c8165631 KL |
562 | |
563 | Cell cellColor = new Cell(); | |
564 | cellColor.setTo(cell); | |
565 | ||
566 | // Check for reverse | |
567 | if (cell.isReverse()) { | |
568 | cellColor.setForeColor(cell.getBackColor()); | |
569 | cellColor.setBackColor(cell.getForeColor()); | |
570 | } | |
571 | ||
572 | // Draw the background rectangle, then the foreground character. | |
573 | gr2.setColor(attrToBackgroundColor(cellColor)); | |
e685a47d | 574 | gr2.fillRect(gr2x, gr2y, textWidth, textHeight); |
c8165631 KL |
575 | |
576 | // Handle blink and underline | |
577 | if (!cell.isBlink() | |
578 | || (cell.isBlink() && cursorBlinkVisible) | |
579 | ) { | |
580 | gr2.setColor(attrToForegroundColor(cellColor)); | |
581 | char [] chars = new char[1]; | |
582 | chars[0] = cell.getChar(); | |
e685a47d KL |
583 | gr2.drawChars(chars, 0, 1, gr2x + textAdjustX, |
584 | gr2y + textHeight - maxDescent + textAdjustY); | |
c8165631 KL |
585 | |
586 | if (cell.isUnderline()) { | |
e685a47d | 587 | gr2.fillRect(gr2x, gr2y + textHeight - 2, textWidth, 2); |
c8165631 KL |
588 | } |
589 | } | |
c8165631 | 590 | |
e685a47d KL |
591 | if (tripleBuffer) { |
592 | gr2.dispose(); | |
593 | ||
594 | // We need a new key that will not be mutated by | |
595 | // invertCell(). | |
596 | Cell key = new Cell(); | |
597 | key.setTo(cell); | |
598 | if (cell.isBlink() && !cursorBlinkVisible) { | |
599 | glyphCacheBlink.put(key, image); | |
600 | } else { | |
601 | glyphCache.put(key, image); | |
602 | } | |
603 | ||
604 | gr.drawImage(image, xPixel, yPixel, this); | |
c8165631 KL |
605 | } |
606 | ||
c8165631 KL |
607 | } |
608 | ||
609 | /** | |
610 | * Check if the cursor is visible, and if so draw it. | |
611 | * | |
612 | * @param gr the Swing Graphics context | |
613 | */ | |
614 | private void drawCursor(final Graphics gr) { | |
615 | ||
616 | if (cursorVisible | |
617 | && (cursorY <= screen.height - 1) | |
618 | && (cursorX <= screen.width - 1) | |
619 | && cursorBlinkVisible | |
620 | ) { | |
621 | int xPixel = cursorX * textWidth + left; | |
622 | int yPixel = cursorY * textHeight + top; | |
623 | Cell lCell = screen.logical[cursorX][cursorY]; | |
624 | gr.setColor(attrToForegroundColor(lCell)); | |
625 | switch (cursorStyle) { | |
626 | default: | |
627 | // Fall through... | |
628 | case UNDERLINE: | |
629 | gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2); | |
630 | break; | |
631 | case BLOCK: | |
632 | gr.fillRect(xPixel, yPixel, textWidth, textHeight); | |
633 | break; | |
634 | case OUTLINE: | |
635 | gr.drawRect(xPixel, yPixel, textWidth - 1, textHeight - 1); | |
636 | break; | |
637 | } | |
638 | } | |
639 | } | |
640 | ||
1ac2ccb1 KL |
641 | /** |
642 | * Paint redraws the whole screen. | |
643 | * | |
a4406f4e | 644 | * @param gr the Swing Graphics context |
1ac2ccb1 KL |
645 | */ |
646 | @Override | |
84614868 | 647 | public void paint(final Graphics gr) { |
87a17f3c KL |
648 | // Do nothing until the screen reference has been set. |
649 | if (screen == null) { | |
650 | return; | |
651 | } | |
652 | if (screen.frame == null) { | |
653 | return; | |
654 | } | |
655 | ||
b5f2a6db KL |
656 | // See if it is time to flip the blink time. |
657 | long nowTime = (new Date()).getTime(); | |
658 | if (nowTime > blinkMillis + lastBlinkTime) { | |
659 | lastBlinkTime = nowTime; | |
660 | cursorBlinkVisible = !cursorBlinkVisible; | |
661 | } | |
662 | ||
87a17f3c KL |
663 | int xCellMin = 0; |
664 | int xCellMax = screen.width; | |
665 | int yCellMin = 0; | |
666 | int yCellMax = screen.height; | |
667 | ||
30bd4abd | 668 | Rectangle bounds = gr.getClipBounds(); |
87a17f3c KL |
669 | if (bounds != null) { |
670 | // Only update what is in the bounds | |
671 | xCellMin = screen.textColumn(bounds.x); | |
bd8d51fa | 672 | xCellMax = screen.textColumn(bounds.x + bounds.width); |
87a17f3c KL |
673 | if (xCellMax > screen.width) { |
674 | xCellMax = screen.width; | |
675 | } | |
676 | if (xCellMin >= xCellMax) { | |
677 | xCellMin = xCellMax - 2; | |
678 | } | |
679 | if (xCellMin < 0) { | |
680 | xCellMin = 0; | |
681 | } | |
682 | yCellMin = screen.textRow(bounds.y); | |
bd8d51fa | 683 | yCellMax = screen.textRow(bounds.y + bounds.height); |
87a17f3c KL |
684 | if (yCellMax > screen.height) { |
685 | yCellMax = screen.height; | |
686 | } | |
687 | if (yCellMin >= yCellMax) { | |
688 | yCellMin = yCellMax - 2; | |
689 | } | |
690 | if (yCellMin < 0) { | |
691 | yCellMin = 0; | |
692 | } | |
bd8d51fa KL |
693 | } else { |
694 | // We need a total repaint | |
695 | reallyCleared = true; | |
87a17f3c | 696 | } |
1ac2ccb1 | 697 | |
87a17f3c KL |
698 | // Prevent updates to the screen's data from the TApplication |
699 | // threads. | |
700 | synchronized (screen) { | |
701 | /* | |
702 | System.err.printf("bounds %s X %d %d Y %d %d\n", | |
703 | bounds, xCellMin, xCellMax, yCellMin, yCellMax); | |
704 | */ | |
705 | ||
706 | for (int y = yCellMin; y < yCellMax; y++) { | |
707 | for (int x = xCellMin; x < xCellMax; x++) { | |
708 | ||
709 | int xPixel = x * textWidth + left; | |
710 | int yPixel = y * textHeight + top; | |
711 | ||
712 | Cell lCell = screen.logical[x][y]; | |
713 | Cell pCell = screen.physical[x][y]; | |
714 | ||
55b4f29b KL |
715 | if (!lCell.equals(pCell) |
716 | || lCell.isBlink() | |
717 | || reallyCleared) { | |
e3dfbd23 | 718 | |
c8165631 | 719 | drawGlyph(gr, lCell, xPixel, yPixel); |
87a17f3c KL |
720 | |
721 | // Physical is always updated | |
722 | physical[x][y].setTo(lCell); | |
30bd4abd KL |
723 | } |
724 | } | |
87a17f3c | 725 | } |
c8165631 | 726 | drawCursor(gr); |
30bd4abd | 727 | |
87a17f3c KL |
728 | dirty = false; |
729 | reallyCleared = false; | |
730 | } // synchronized (screen) | |
1ac2ccb1 | 731 | } |
bd8d51fa | 732 | |
a4406f4e | 733 | } // class SwingFrame |
1ac2ccb1 KL |
734 | |
735 | /** | |
a4406f4e | 736 | * The raw Swing JFrame. Note package private access. |
1ac2ccb1 | 737 | */ |
a4406f4e | 738 | SwingFrame frame; |
1ac2ccb1 | 739 | |
e3dfbd23 KL |
740 | /** |
741 | * Restore terminal to normal state. | |
742 | */ | |
743 | public void shutdown() { | |
744 | frame.dispose(); | |
745 | } | |
746 | ||
1ac2ccb1 KL |
747 | /** |
748 | * Public constructor. | |
749 | */ | |
a4406f4e | 750 | public SwingScreen() { |
aed33687 KL |
751 | try { |
752 | SwingUtilities.invokeAndWait(new Runnable() { | |
753 | public void run() { | |
a4406f4e KL |
754 | SwingScreen.this.frame = new SwingFrame(SwingScreen.this); |
755 | SwingScreen.this.sessionInfo = | |
756 | new SwingSessionInfo(SwingScreen.this.frame, | |
bd8d51fa KL |
757 | frame.textWidth, |
758 | frame.textHeight); | |
759 | ||
a4406f4e | 760 | SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(), |
bd8d51fa KL |
761 | sessionInfo.getWindowHeight()); |
762 | ||
a4406f4e KL |
763 | SwingScreen.this.frame.resizeToScreen(); |
764 | SwingScreen.this.frame.setVisible(true); | |
aed33687 | 765 | } |
329fd62e | 766 | }); |
aed33687 KL |
767 | } catch (Exception e) { |
768 | e.printStackTrace(); | |
769 | } | |
1ac2ccb1 KL |
770 | } |
771 | ||
bd8d51fa KL |
772 | /** |
773 | * The sessionInfo. | |
774 | */ | |
a4406f4e | 775 | private SwingSessionInfo sessionInfo; |
bd8d51fa | 776 | |
30bd4abd | 777 | /** |
a4406f4e | 778 | * Create the SwingSessionInfo. Note package private access. |
30bd4abd KL |
779 | * |
780 | * @return the sessionInfo | |
781 | */ | |
a4406f4e | 782 | SwingSessionInfo getSessionInfo() { |
30bd4abd KL |
783 | return sessionInfo; |
784 | } | |
785 | ||
1ac2ccb1 KL |
786 | /** |
787 | * Push the logical screen to the physical device. | |
788 | */ | |
789 | @Override | |
790 | public void flushPhysical() { | |
87a17f3c | 791 | |
1d14ffab KL |
792 | /* |
793 | System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n", | |
794 | reallyCleared, dirty); | |
795 | */ | |
796 | ||
797 | // If reallyCleared is set, we have to draw everything. | |
798 | if ((frame.bufferStrategy != null) && (reallyCleared == true)) { | |
799 | // Triple-buffering: we have to redraw everything on this thread. | |
800 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); | |
801 | frame.paint(gr); | |
802 | gr.dispose(); | |
803 | frame.bufferStrategy.show(); | |
804 | // sync() doesn't seem to help the tearing for me. | |
805 | // Toolkit.getDefaultToolkit().sync(); | |
806 | return; | |
807 | } else if ((frame.bufferStrategy == null) && (reallyCleared == true)) { | |
808 | // Repaint everything on the Swing thread. | |
809 | frame.repaint(); | |
87a17f3c KL |
810 | return; |
811 | } | |
812 | ||
813 | // Do nothing if nothing happened. | |
814 | if (!dirty) { | |
815 | return; | |
816 | } | |
817 | ||
c8165631 KL |
818 | if (frame.bufferStrategy != null) { |
819 | // See if it is time to flip the blink time. | |
820 | long nowTime = (new Date()).getTime(); | |
821 | if (nowTime > frame.blinkMillis + frame.lastBlinkTime) { | |
822 | frame.lastBlinkTime = nowTime; | |
823 | frame.cursorBlinkVisible = !frame.cursorBlinkVisible; | |
824 | } | |
825 | ||
826 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); | |
827 | ||
828 | synchronized (this) { | |
829 | for (int y = 0; y < height; y++) { | |
830 | for (int x = 0; x < width; x++) { | |
831 | Cell lCell = logical[x][y]; | |
832 | Cell pCell = physical[x][y]; | |
833 | ||
834 | int xPixel = x * frame.textWidth + frame.left; | |
835 | int yPixel = y * frame.textHeight + frame.top; | |
836 | ||
837 | if (!lCell.equals(pCell) | |
838 | || ((x == cursorX) | |
839 | && (y == cursorY) | |
840 | && cursorVisible) | |
841 | || (lCell.isBlink()) | |
842 | ) { | |
843 | frame.drawGlyph(gr, lCell, xPixel, yPixel); | |
844 | physical[x][y].setTo(lCell); | |
845 | } | |
846 | } | |
847 | } | |
848 | frame.drawCursor(gr); | |
849 | } // synchronized (this) | |
850 | ||
851 | gr.dispose(); | |
852 | frame.bufferStrategy.show(); | |
853 | // sync() doesn't seem to help the tearing for me. | |
854 | // Toolkit.getDefaultToolkit().sync(); | |
855 | return; | |
856 | } | |
857 | ||
858 | // Swing thread version: request a repaint, but limit it to the area | |
859 | // that has changed. | |
87a17f3c | 860 | |
30bd4abd KL |
861 | // Find the minimum-size damaged region. |
862 | int xMin = frame.getWidth(); | |
863 | int xMax = 0; | |
864 | int yMin = frame.getHeight(); | |
865 | int yMax = 0; | |
30bd4abd | 866 | |
87a17f3c KL |
867 | synchronized (this) { |
868 | for (int y = 0; y < height; y++) { | |
869 | for (int x = 0; x < width; x++) { | |
870 | Cell lCell = logical[x][y]; | |
871 | Cell pCell = physical[x][y]; | |
872 | ||
873 | int xPixel = x * frame.textWidth + frame.left; | |
874 | int yPixel = y * frame.textHeight + frame.top; | |
875 | ||
876 | if (!lCell.equals(pCell) | |
877 | || ((x == cursorX) | |
878 | && (y == cursorY) | |
879 | && cursorVisible) | |
55b4f29b | 880 | || lCell.isBlink() |
87a17f3c KL |
881 | ) { |
882 | if (xPixel < xMin) { | |
883 | xMin = xPixel; | |
884 | } | |
885 | if (xPixel + frame.textWidth > xMax) { | |
886 | xMax = xPixel + frame.textWidth; | |
887 | } | |
888 | if (yPixel < yMin) { | |
889 | yMin = yPixel; | |
890 | } | |
891 | if (yPixel + frame.textHeight > yMax) { | |
892 | yMax = yPixel + frame.textHeight; | |
893 | } | |
30bd4abd KL |
894 | } |
895 | } | |
896 | } | |
897 | } | |
87a17f3c KL |
898 | if (xMin + frame.textWidth >= xMax) { |
899 | xMax += frame.textWidth; | |
900 | } | |
901 | if (yMin + frame.textHeight >= yMax) { | |
902 | yMax += frame.textHeight; | |
903 | } | |
30bd4abd | 904 | |
87a17f3c | 905 | // Repaint the desired area |
1d14ffab KL |
906 | /* |
907 | System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, | |
908 | yMin, yMax); | |
909 | */ | |
910 | if (frame.bufferStrategy != null) { | |
c8165631 KL |
911 | // This path should never be taken, but is left here for |
912 | // completeness. | |
e3dfbd23 KL |
913 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); |
914 | Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin, | |
915 | yMax - yMin); | |
916 | gr.setClip(bounds); | |
917 | frame.paint(gr); | |
918 | gr.dispose(); | |
919 | frame.bufferStrategy.show(); | |
1d14ffab KL |
920 | // sync() doesn't seem to help the tearing for me. |
921 | // Toolkit.getDefaultToolkit().sync(); | |
e3dfbd23 | 922 | } else { |
c8165631 | 923 | // Repaint on the Swing thread. |
e3dfbd23 KL |
924 | frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin); |
925 | } | |
1ac2ccb1 | 926 | } |
30bd4abd KL |
927 | |
928 | /** | |
929 | * Put the cursor at (x,y). | |
930 | * | |
931 | * @param visible if true, the cursor should be visible | |
932 | * @param x column coordinate to put the cursor on | |
933 | * @param y row coordinate to put the cursor on | |
934 | */ | |
935 | @Override | |
936 | public void putCursor(final boolean visible, final int x, final int y) { | |
b5f2a6db KL |
937 | |
938 | if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) { | |
939 | // See if it is time to flip the blink time. | |
940 | long nowTime = (new Date()).getTime(); | |
941 | if (nowTime < frame.blinkMillis + frame.lastBlinkTime) { | |
942 | // Nothing has changed, so don't do anything. | |
943 | return; | |
944 | } | |
945 | } | |
946 | ||
947 | if (cursorVisible | |
30bd4abd KL |
948 | && (cursorY <= height - 1) |
949 | && (cursorX <= width - 1) | |
950 | ) { | |
951 | // Make the current cursor position dirty | |
87a17f3c | 952 | if (physical[cursorX][cursorY].getChar() == 'Q') { |
30bd4abd KL |
953 | physical[cursorX][cursorY].setChar('X'); |
954 | } else { | |
87a17f3c | 955 | physical[cursorX][cursorY].setChar('Q'); |
30bd4abd KL |
956 | } |
957 | } | |
958 | ||
959 | super.putCursor(visible, x, y); | |
960 | } | |
961 | ||
87a17f3c KL |
962 | /** |
963 | * Convert pixel column position to text cell column position. | |
964 | * | |
965 | * @param x pixel column position | |
966 | * @return text cell column position | |
967 | */ | |
968 | public int textColumn(final int x) { | |
969 | return ((x - frame.left) / frame.textWidth); | |
970 | } | |
971 | ||
972 | /** | |
973 | * Convert pixel row position to text cell row position. | |
974 | * | |
975 | * @param y pixel row position | |
976 | * @return text cell row position | |
977 | */ | |
978 | public int textRow(final int y) { | |
979 | return ((y - frame.top) / frame.textHeight); | |
980 | } | |
981 | ||
55d2b2c2 KL |
982 | /** |
983 | * Set the window title. | |
984 | * | |
985 | * @param title the new title | |
986 | */ | |
987 | public void setTitle(final String title) { | |
988 | frame.setTitle(title); | |
989 | } | |
990 | ||
1ac2ccb1 | 991 | } |