Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
1ac2ccb1 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
1ac2ccb1 | 5 | * |
e16dda65 | 6 | * Copyright (C) 2016 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 | */ |
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 KL |
167 | |
168 | /** | |
169 | * Width of a character cell. | |
170 | */ | |
171 | private int textWidth = 1; | |
172 | ||
173 | /** | |
174 | * Height of a character cell. | |
175 | */ | |
176 | private int textHeight = 1; | |
177 | ||
84614868 KL |
178 | /** |
179 | * Descent of a character cell. | |
180 | */ | |
181 | private int maxDescent = 0; | |
182 | ||
847a4bc5 KL |
183 | /** |
184 | * System-dependent Y adjustment for text in the character cell. | |
185 | */ | |
186 | private int textAdjustY = 0; | |
187 | ||
188 | /** | |
189 | * System-dependent X adjustment for text in the character cell. | |
190 | */ | |
191 | private int textAdjustX = 0; | |
192 | ||
1ac2ccb1 | 193 | /** |
b5f2a6db | 194 | * Top pixel absolute location. |
1ac2ccb1 KL |
195 | */ |
196 | private int top = 30; | |
84614868 | 197 | |
1ac2ccb1 | 198 | /** |
b5f2a6db | 199 | * Left pixel absolute location. |
1ac2ccb1 KL |
200 | */ |
201 | private int left = 30; | |
84614868 | 202 | |
b5f2a6db KL |
203 | /** |
204 | * The cursor style to draw. | |
205 | */ | |
206 | private CursorStyle cursorStyle = CursorStyle.UNDERLINE; | |
207 | ||
208 | /** | |
209 | * The number of millis to wait before switching the blink from | |
210 | * visible to invisible. | |
211 | */ | |
212 | private long blinkMillis = 500; | |
213 | ||
214 | /** | |
215 | * If true, the cursor should be visible right now based on the blink | |
216 | * time. | |
217 | */ | |
218 | private boolean cursorBlinkVisible = true; | |
219 | ||
220 | /** | |
221 | * The time that the blink last flipped from visible to invisible or | |
222 | * from invisible to visible. | |
223 | */ | |
224 | private long lastBlinkTime = 0; | |
225 | ||
84614868 | 226 | /** |
a4406f4e | 227 | * Convert a CellAttributes foreground color to an Swing Color. |
84614868 KL |
228 | * |
229 | * @param attr the text attributes | |
a4406f4e | 230 | * @return the Swing Color |
84614868 KL |
231 | */ |
232 | private Color attrToForegroundColor(final CellAttributes attr) { | |
7c870d89 | 233 | if (attr.isBold()) { |
84614868 KL |
234 | if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) { |
235 | return MYBOLD_BLACK; | |
236 | } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) { | |
237 | return MYBOLD_RED; | |
238 | } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) { | |
239 | return MYBOLD_BLUE; | |
240 | } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) { | |
241 | return MYBOLD_GREEN; | |
242 | } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) { | |
243 | return MYBOLD_YELLOW; | |
244 | } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) { | |
245 | return MYBOLD_CYAN; | |
246 | } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) { | |
247 | return MYBOLD_MAGENTA; | |
248 | } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) { | |
249 | return MYBOLD_WHITE; | |
250 | } | |
251 | } else { | |
252 | if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) { | |
253 | return MYBLACK; | |
254 | } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) { | |
255 | return MYRED; | |
256 | } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) { | |
257 | return MYBLUE; | |
258 | } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) { | |
259 | return MYGREEN; | |
260 | } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) { | |
261 | return MYYELLOW; | |
262 | } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) { | |
263 | return MYCYAN; | |
264 | } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) { | |
265 | return MYMAGENTA; | |
266 | } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) { | |
267 | return MYWHITE; | |
268 | } | |
269 | } | |
c8165631 KL |
270 | throw new IllegalArgumentException("Invalid color: " + |
271 | attr.getForeColor().getValue()); | |
84614868 KL |
272 | } |
273 | ||
274 | /** | |
a4406f4e | 275 | * Convert a CellAttributes background color to an Swing Color. |
84614868 KL |
276 | * |
277 | * @param attr the text attributes | |
a4406f4e | 278 | * @return the Swing Color |
84614868 KL |
279 | */ |
280 | private Color attrToBackgroundColor(final CellAttributes attr) { | |
84614868 KL |
281 | if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) { |
282 | return MYBLACK; | |
283 | } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) { | |
284 | return MYRED; | |
285 | } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) { | |
286 | return MYBLUE; | |
287 | } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) { | |
288 | return MYGREEN; | |
289 | } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) { | |
290 | return MYYELLOW; | |
291 | } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) { | |
292 | return MYCYAN; | |
293 | } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) { | |
294 | return MYMAGENTA; | |
295 | } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) { | |
296 | return MYWHITE; | |
297 | } | |
c8165631 KL |
298 | throw new IllegalArgumentException("Invalid color: " + |
299 | attr.getBackColor().getValue()); | |
84614868 KL |
300 | } |
301 | ||
1ac2ccb1 KL |
302 | /** |
303 | * Public constructor. | |
84614868 KL |
304 | * |
305 | * @param screen the Screen that Backend talks to | |
1ac2ccb1 | 306 | */ |
a4406f4e | 307 | public SwingFrame(final SwingScreen screen) { |
84614868 KL |
308 | this.screen = screen; |
309 | setDOSColors(); | |
310 | ||
b5f2a6db | 311 | // Figure out my cursor style |
c8165631 KL |
312 | String cursorStyleString = System.getProperty( |
313 | "jexer.Swing.cursorStyle", "underline").toLowerCase(); | |
b5f2a6db KL |
314 | |
315 | if (cursorStyleString.equals("underline")) { | |
316 | cursorStyle = CursorStyle.UNDERLINE; | |
317 | } else if (cursorStyleString.equals("outline")) { | |
318 | cursorStyle = CursorStyle.OUTLINE; | |
319 | } else if (cursorStyleString.equals("block")) { | |
320 | cursorStyle = CursorStyle.BLOCK; | |
321 | } | |
322 | ||
1ac2ccb1 | 323 | setTitle("Jexer Application"); |
aed33687 | 324 | setBackground(Color.black); |
84614868 KL |
325 | |
326 | try { | |
30bd4abd | 327 | // Always try to use Terminus, the one decent font. |
c8165631 KL |
328 | ClassLoader loader = Thread.currentThread(). |
329 | getContextClassLoader(); | |
84614868 KL |
330 | InputStream in = loader.getResourceAsStream(FONTFILE); |
331 | Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in); | |
332 | Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22); | |
333 | setFont(terminus); | |
334 | } catch (Exception e) { | |
335 | e.printStackTrace(); | |
336 | // setFont(new Font("Liberation Mono", Font.PLAIN, 24)); | |
337 | setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24)); | |
338 | } | |
aed33687 | 339 | pack(); |
30bd4abd KL |
340 | |
341 | // Kill the X11 cursor | |
342 | // Transparent 16 x 16 pixel cursor image. | |
343 | BufferedImage cursorImg = new BufferedImage(16, 16, | |
344 | BufferedImage.TYPE_INT_ARGB); | |
30bd4abd KL |
345 | // Create a new blank cursor. |
346 | Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor( | |
347 | cursorImg, new Point(0, 0), "blank cursor"); | |
348 | setCursor(blankCursor); | |
091d8a06 KL |
349 | |
350 | // Be capable of seeing Tab / Shift-Tab | |
351 | setFocusTraversalKeysEnabled(false); | |
352 | ||
353 | // Save the text cell width/height | |
bd8d51fa | 354 | getFontDimensions(); |
e3dfbd23 | 355 | |
c8165631 KL |
356 | // Cache glyphs as they are rendered |
357 | glyphCacheBlink = new HashMap<Cell, BufferedImage>(); | |
358 | glyphCache = new HashMap<Cell, BufferedImage>(); | |
359 | ||
1d14ffab KL |
360 | // Setup triple-buffering |
361 | if (SwingScreen.tripleBuffer) { | |
e3dfbd23 | 362 | setIgnoreRepaint(true); |
1d14ffab | 363 | createBufferStrategy(3); |
e3dfbd23 KL |
364 | bufferStrategy = getBufferStrategy(); |
365 | } | |
1ac2ccb1 KL |
366 | } |
367 | ||
368 | /** | |
bd8d51fa | 369 | * Figure out my font dimensions. |
1ac2ccb1 | 370 | */ |
bd8d51fa | 371 | private void getFontDimensions() { |
84614868 KL |
372 | Graphics gr = getGraphics(); |
373 | FontMetrics fm = gr.getFontMetrics(); | |
374 | maxDescent = fm.getMaxDescent(); | |
375 | Rectangle2D bounds = fm.getMaxCharBounds(gr); | |
376 | int leading = fm.getLeading(); | |
377 | textWidth = (int)Math.round(bounds.getWidth()); | |
378 | textHeight = (int)Math.round(bounds.getHeight()) - maxDescent; | |
379 | // This also produces the same number, but works better for ugly | |
380 | // monospace. | |
381 | textHeight = fm.getMaxAscent() + maxDescent - leading; | |
847a4bc5 KL |
382 | |
383 | if (System.getProperty("os.name").startsWith("Windows")) { | |
384 | textAdjustY = -1; | |
385 | textAdjustX = 0; | |
386 | } | |
bd8d51fa | 387 | } |
84614868 | 388 | |
bd8d51fa KL |
389 | /** |
390 | * Resize to font dimensions. | |
391 | */ | |
392 | public void resizeToScreen() { | |
84614868 KL |
393 | // Figure out the thickness of borders and use that to set the |
394 | // final size. | |
395 | Insets insets = getInsets(); | |
396 | left = insets.left; | |
397 | top = insets.top; | |
398 | ||
399 | setSize(textWidth * screen.width + insets.left + insets.right, | |
400 | textHeight * screen.height + insets.top + insets.bottom); | |
1ac2ccb1 KL |
401 | } |
402 | ||
30bd4abd KL |
403 | /** |
404 | * Update redraws the whole screen. | |
405 | * | |
a4406f4e | 406 | * @param gr the Swing Graphics context |
30bd4abd KL |
407 | */ |
408 | @Override | |
409 | public void update(final Graphics gr) { | |
410 | // The default update clears the area. Don't do that, instead | |
411 | // just paint it directly. | |
412 | paint(gr); | |
413 | } | |
414 | ||
c8165631 KL |
415 | /** |
416 | * Draw one glyph to the screen. | |
417 | * | |
418 | * @param gr the Swing Graphics context | |
419 | * @param cell the Cell to draw | |
420 | * @param xPixel the x-coordinate to render to. 0 means the | |
421 | * left-most pixel column. | |
422 | * @param yPixel the y-coordinate to render to. 0 means the top-most | |
423 | * pixel row. | |
424 | */ | |
425 | private void drawGlyph(final Graphics gr, final Cell cell, | |
426 | final int xPixel, final int yPixel) { | |
427 | ||
428 | BufferedImage image = null; | |
429 | if (cell.isBlink() && !cursorBlinkVisible) { | |
430 | image = glyphCacheBlink.get(cell); | |
431 | } else { | |
432 | image = glyphCache.get(cell); | |
433 | } | |
434 | if (image != null) { | |
435 | gr.drawImage(image, xPixel, yPixel, this); | |
436 | return; | |
437 | } | |
438 | ||
439 | // Generate glyph and draw it. | |
440 | ||
441 | image = new BufferedImage(textWidth, textHeight, | |
442 | BufferedImage.TYPE_INT_ARGB); | |
443 | Graphics2D gr2 = image.createGraphics(); | |
444 | gr2.setFont(getFont()); | |
445 | ||
446 | Cell cellColor = new Cell(); | |
447 | cellColor.setTo(cell); | |
448 | ||
449 | // Check for reverse | |
450 | if (cell.isReverse()) { | |
451 | cellColor.setForeColor(cell.getBackColor()); | |
452 | cellColor.setBackColor(cell.getForeColor()); | |
453 | } | |
454 | ||
455 | // Draw the background rectangle, then the foreground character. | |
456 | gr2.setColor(attrToBackgroundColor(cellColor)); | |
457 | gr2.fillRect(0, 0, textWidth, textHeight); | |
458 | ||
459 | // Handle blink and underline | |
460 | if (!cell.isBlink() | |
461 | || (cell.isBlink() && cursorBlinkVisible) | |
462 | ) { | |
463 | gr2.setColor(attrToForegroundColor(cellColor)); | |
464 | char [] chars = new char[1]; | |
465 | chars[0] = cell.getChar(); | |
466 | gr2.drawChars(chars, 0, 1, 0 + textAdjustX, | |
467 | 0 + textHeight - maxDescent + textAdjustY); | |
468 | ||
469 | if (cell.isUnderline()) { | |
470 | gr2.fillRect(0, 0 + textHeight - 2, textWidth, 2); | |
471 | } | |
472 | } | |
473 | gr2.dispose(); | |
474 | ||
475 | // We need a new key that will not be mutated by invertCell(). | |
476 | Cell key = new Cell(); | |
477 | key.setTo(cell); | |
478 | if (cell.isBlink() && !cursorBlinkVisible) { | |
479 | glyphCacheBlink.put(key, image); | |
480 | } else { | |
481 | glyphCache.put(key, image); | |
482 | } | |
483 | ||
484 | gr.drawImage(image, xPixel, yPixel, this); | |
485 | } | |
486 | ||
487 | /** | |
488 | * Check if the cursor is visible, and if so draw it. | |
489 | * | |
490 | * @param gr the Swing Graphics context | |
491 | */ | |
492 | private void drawCursor(final Graphics gr) { | |
493 | ||
494 | if (cursorVisible | |
495 | && (cursorY <= screen.height - 1) | |
496 | && (cursorX <= screen.width - 1) | |
497 | && cursorBlinkVisible | |
498 | ) { | |
499 | int xPixel = cursorX * textWidth + left; | |
500 | int yPixel = cursorY * textHeight + top; | |
501 | Cell lCell = screen.logical[cursorX][cursorY]; | |
502 | gr.setColor(attrToForegroundColor(lCell)); | |
503 | switch (cursorStyle) { | |
504 | default: | |
505 | // Fall through... | |
506 | case UNDERLINE: | |
507 | gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2); | |
508 | break; | |
509 | case BLOCK: | |
510 | gr.fillRect(xPixel, yPixel, textWidth, textHeight); | |
511 | break; | |
512 | case OUTLINE: | |
513 | gr.drawRect(xPixel, yPixel, textWidth - 1, textHeight - 1); | |
514 | break; | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
1ac2ccb1 KL |
519 | /** |
520 | * Paint redraws the whole screen. | |
521 | * | |
a4406f4e | 522 | * @param gr the Swing Graphics context |
1ac2ccb1 KL |
523 | */ |
524 | @Override | |
84614868 | 525 | public void paint(final Graphics gr) { |
87a17f3c KL |
526 | // Do nothing until the screen reference has been set. |
527 | if (screen == null) { | |
528 | return; | |
529 | } | |
530 | if (screen.frame == null) { | |
531 | return; | |
532 | } | |
533 | ||
b5f2a6db KL |
534 | // See if it is time to flip the blink time. |
535 | long nowTime = (new Date()).getTime(); | |
536 | if (nowTime > blinkMillis + lastBlinkTime) { | |
537 | lastBlinkTime = nowTime; | |
538 | cursorBlinkVisible = !cursorBlinkVisible; | |
539 | } | |
540 | ||
87a17f3c KL |
541 | int xCellMin = 0; |
542 | int xCellMax = screen.width; | |
543 | int yCellMin = 0; | |
544 | int yCellMax = screen.height; | |
545 | ||
30bd4abd | 546 | Rectangle bounds = gr.getClipBounds(); |
87a17f3c KL |
547 | if (bounds != null) { |
548 | // Only update what is in the bounds | |
549 | xCellMin = screen.textColumn(bounds.x); | |
bd8d51fa | 550 | xCellMax = screen.textColumn(bounds.x + bounds.width); |
87a17f3c KL |
551 | if (xCellMax > screen.width) { |
552 | xCellMax = screen.width; | |
553 | } | |
554 | if (xCellMin >= xCellMax) { | |
555 | xCellMin = xCellMax - 2; | |
556 | } | |
557 | if (xCellMin < 0) { | |
558 | xCellMin = 0; | |
559 | } | |
560 | yCellMin = screen.textRow(bounds.y); | |
bd8d51fa | 561 | yCellMax = screen.textRow(bounds.y + bounds.height); |
87a17f3c KL |
562 | if (yCellMax > screen.height) { |
563 | yCellMax = screen.height; | |
564 | } | |
565 | if (yCellMin >= yCellMax) { | |
566 | yCellMin = yCellMax - 2; | |
567 | } | |
568 | if (yCellMin < 0) { | |
569 | yCellMin = 0; | |
570 | } | |
bd8d51fa KL |
571 | } else { |
572 | // We need a total repaint | |
573 | reallyCleared = true; | |
87a17f3c | 574 | } |
1ac2ccb1 | 575 | |
87a17f3c KL |
576 | // Prevent updates to the screen's data from the TApplication |
577 | // threads. | |
578 | synchronized (screen) { | |
579 | /* | |
580 | System.err.printf("bounds %s X %d %d Y %d %d\n", | |
581 | bounds, xCellMin, xCellMax, yCellMin, yCellMax); | |
582 | */ | |
583 | ||
584 | for (int y = yCellMin; y < yCellMax; y++) { | |
585 | for (int x = xCellMin; x < xCellMax; x++) { | |
586 | ||
587 | int xPixel = x * textWidth + left; | |
588 | int yPixel = y * textHeight + top; | |
589 | ||
590 | Cell lCell = screen.logical[x][y]; | |
591 | Cell pCell = screen.physical[x][y]; | |
592 | ||
55b4f29b KL |
593 | if (!lCell.equals(pCell) |
594 | || lCell.isBlink() | |
595 | || reallyCleared) { | |
e3dfbd23 | 596 | |
c8165631 | 597 | drawGlyph(gr, lCell, xPixel, yPixel); |
87a17f3c KL |
598 | |
599 | // Physical is always updated | |
600 | physical[x][y].setTo(lCell); | |
30bd4abd KL |
601 | } |
602 | } | |
87a17f3c | 603 | } |
c8165631 | 604 | drawCursor(gr); |
30bd4abd | 605 | |
87a17f3c KL |
606 | dirty = false; |
607 | reallyCleared = false; | |
608 | } // synchronized (screen) | |
1ac2ccb1 | 609 | } |
bd8d51fa | 610 | |
a4406f4e | 611 | } // class SwingFrame |
1ac2ccb1 KL |
612 | |
613 | /** | |
a4406f4e | 614 | * The raw Swing JFrame. Note package private access. |
1ac2ccb1 | 615 | */ |
a4406f4e | 616 | SwingFrame frame; |
1ac2ccb1 | 617 | |
e3dfbd23 KL |
618 | /** |
619 | * Restore terminal to normal state. | |
620 | */ | |
621 | public void shutdown() { | |
622 | frame.dispose(); | |
623 | } | |
624 | ||
1ac2ccb1 KL |
625 | /** |
626 | * Public constructor. | |
627 | */ | |
a4406f4e | 628 | public SwingScreen() { |
aed33687 KL |
629 | try { |
630 | SwingUtilities.invokeAndWait(new Runnable() { | |
631 | public void run() { | |
a4406f4e KL |
632 | SwingScreen.this.frame = new SwingFrame(SwingScreen.this); |
633 | SwingScreen.this.sessionInfo = | |
634 | new SwingSessionInfo(SwingScreen.this.frame, | |
bd8d51fa KL |
635 | frame.textWidth, |
636 | frame.textHeight); | |
637 | ||
a4406f4e | 638 | SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(), |
bd8d51fa KL |
639 | sessionInfo.getWindowHeight()); |
640 | ||
a4406f4e KL |
641 | SwingScreen.this.frame.resizeToScreen(); |
642 | SwingScreen.this.frame.setVisible(true); | |
aed33687 | 643 | } |
329fd62e | 644 | }); |
aed33687 KL |
645 | } catch (Exception e) { |
646 | e.printStackTrace(); | |
647 | } | |
1ac2ccb1 KL |
648 | } |
649 | ||
bd8d51fa KL |
650 | /** |
651 | * The sessionInfo. | |
652 | */ | |
a4406f4e | 653 | private SwingSessionInfo sessionInfo; |
bd8d51fa | 654 | |
30bd4abd | 655 | /** |
a4406f4e | 656 | * Create the SwingSessionInfo. Note package private access. |
30bd4abd KL |
657 | * |
658 | * @return the sessionInfo | |
659 | */ | |
a4406f4e | 660 | SwingSessionInfo getSessionInfo() { |
30bd4abd KL |
661 | return sessionInfo; |
662 | } | |
663 | ||
1ac2ccb1 KL |
664 | /** |
665 | * Push the logical screen to the physical device. | |
666 | */ | |
667 | @Override | |
668 | public void flushPhysical() { | |
87a17f3c | 669 | |
1d14ffab KL |
670 | /* |
671 | System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n", | |
672 | reallyCleared, dirty); | |
673 | */ | |
674 | ||
675 | // If reallyCleared is set, we have to draw everything. | |
676 | if ((frame.bufferStrategy != null) && (reallyCleared == true)) { | |
677 | // Triple-buffering: we have to redraw everything on this thread. | |
678 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); | |
679 | frame.paint(gr); | |
680 | gr.dispose(); | |
681 | frame.bufferStrategy.show(); | |
682 | // sync() doesn't seem to help the tearing for me. | |
683 | // Toolkit.getDefaultToolkit().sync(); | |
684 | return; | |
685 | } else if ((frame.bufferStrategy == null) && (reallyCleared == true)) { | |
686 | // Repaint everything on the Swing thread. | |
687 | frame.repaint(); | |
87a17f3c KL |
688 | return; |
689 | } | |
690 | ||
691 | // Do nothing if nothing happened. | |
692 | if (!dirty) { | |
693 | return; | |
694 | } | |
695 | ||
c8165631 KL |
696 | if (frame.bufferStrategy != null) { |
697 | // See if it is time to flip the blink time. | |
698 | long nowTime = (new Date()).getTime(); | |
699 | if (nowTime > frame.blinkMillis + frame.lastBlinkTime) { | |
700 | frame.lastBlinkTime = nowTime; | |
701 | frame.cursorBlinkVisible = !frame.cursorBlinkVisible; | |
702 | } | |
703 | ||
704 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); | |
705 | ||
706 | synchronized (this) { | |
707 | for (int y = 0; y < height; y++) { | |
708 | for (int x = 0; x < width; x++) { | |
709 | Cell lCell = logical[x][y]; | |
710 | Cell pCell = physical[x][y]; | |
711 | ||
712 | int xPixel = x * frame.textWidth + frame.left; | |
713 | int yPixel = y * frame.textHeight + frame.top; | |
714 | ||
715 | if (!lCell.equals(pCell) | |
716 | || ((x == cursorX) | |
717 | && (y == cursorY) | |
718 | && cursorVisible) | |
719 | || (lCell.isBlink()) | |
720 | ) { | |
721 | frame.drawGlyph(gr, lCell, xPixel, yPixel); | |
722 | physical[x][y].setTo(lCell); | |
723 | } | |
724 | } | |
725 | } | |
726 | frame.drawCursor(gr); | |
727 | } // synchronized (this) | |
728 | ||
729 | gr.dispose(); | |
730 | frame.bufferStrategy.show(); | |
731 | // sync() doesn't seem to help the tearing for me. | |
732 | // Toolkit.getDefaultToolkit().sync(); | |
733 | return; | |
734 | } | |
735 | ||
736 | // Swing thread version: request a repaint, but limit it to the area | |
737 | // that has changed. | |
87a17f3c | 738 | |
30bd4abd KL |
739 | // Find the minimum-size damaged region. |
740 | int xMin = frame.getWidth(); | |
741 | int xMax = 0; | |
742 | int yMin = frame.getHeight(); | |
743 | int yMax = 0; | |
30bd4abd | 744 | |
87a17f3c KL |
745 | synchronized (this) { |
746 | for (int y = 0; y < height; y++) { | |
747 | for (int x = 0; x < width; x++) { | |
748 | Cell lCell = logical[x][y]; | |
749 | Cell pCell = physical[x][y]; | |
750 | ||
751 | int xPixel = x * frame.textWidth + frame.left; | |
752 | int yPixel = y * frame.textHeight + frame.top; | |
753 | ||
754 | if (!lCell.equals(pCell) | |
755 | || ((x == cursorX) | |
756 | && (y == cursorY) | |
757 | && cursorVisible) | |
55b4f29b | 758 | || lCell.isBlink() |
87a17f3c KL |
759 | ) { |
760 | if (xPixel < xMin) { | |
761 | xMin = xPixel; | |
762 | } | |
763 | if (xPixel + frame.textWidth > xMax) { | |
764 | xMax = xPixel + frame.textWidth; | |
765 | } | |
766 | if (yPixel < yMin) { | |
767 | yMin = yPixel; | |
768 | } | |
769 | if (yPixel + frame.textHeight > yMax) { | |
770 | yMax = yPixel + frame.textHeight; | |
771 | } | |
30bd4abd KL |
772 | } |
773 | } | |
774 | } | |
775 | } | |
87a17f3c KL |
776 | if (xMin + frame.textWidth >= xMax) { |
777 | xMax += frame.textWidth; | |
778 | } | |
779 | if (yMin + frame.textHeight >= yMax) { | |
780 | yMax += frame.textHeight; | |
781 | } | |
30bd4abd | 782 | |
87a17f3c | 783 | // Repaint the desired area |
1d14ffab KL |
784 | /* |
785 | System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, | |
786 | yMin, yMax); | |
787 | */ | |
788 | if (frame.bufferStrategy != null) { | |
c8165631 KL |
789 | // This path should never be taken, but is left here for |
790 | // completeness. | |
e3dfbd23 KL |
791 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); |
792 | Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin, | |
793 | yMax - yMin); | |
794 | gr.setClip(bounds); | |
795 | frame.paint(gr); | |
796 | gr.dispose(); | |
797 | frame.bufferStrategy.show(); | |
1d14ffab KL |
798 | // sync() doesn't seem to help the tearing for me. |
799 | // Toolkit.getDefaultToolkit().sync(); | |
e3dfbd23 | 800 | } else { |
c8165631 | 801 | // Repaint on the Swing thread. |
e3dfbd23 KL |
802 | frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin); |
803 | } | |
1ac2ccb1 | 804 | } |
30bd4abd KL |
805 | |
806 | /** | |
807 | * Put the cursor at (x,y). | |
808 | * | |
809 | * @param visible if true, the cursor should be visible | |
810 | * @param x column coordinate to put the cursor on | |
811 | * @param y row coordinate to put the cursor on | |
812 | */ | |
813 | @Override | |
814 | public void putCursor(final boolean visible, final int x, final int y) { | |
b5f2a6db KL |
815 | |
816 | if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) { | |
817 | // See if it is time to flip the blink time. | |
818 | long nowTime = (new Date()).getTime(); | |
819 | if (nowTime < frame.blinkMillis + frame.lastBlinkTime) { | |
820 | // Nothing has changed, so don't do anything. | |
821 | return; | |
822 | } | |
823 | } | |
824 | ||
825 | if (cursorVisible | |
30bd4abd KL |
826 | && (cursorY <= height - 1) |
827 | && (cursorX <= width - 1) | |
828 | ) { | |
829 | // Make the current cursor position dirty | |
87a17f3c | 830 | if (physical[cursorX][cursorY].getChar() == 'Q') { |
30bd4abd KL |
831 | physical[cursorX][cursorY].setChar('X'); |
832 | } else { | |
87a17f3c | 833 | physical[cursorX][cursorY].setChar('Q'); |
30bd4abd KL |
834 | } |
835 | } | |
836 | ||
837 | super.putCursor(visible, x, y); | |
838 | } | |
839 | ||
87a17f3c KL |
840 | /** |
841 | * Convert pixel column position to text cell column position. | |
842 | * | |
843 | * @param x pixel column position | |
844 | * @return text cell column position | |
845 | */ | |
846 | public int textColumn(final int x) { | |
847 | return ((x - frame.left) / frame.textWidth); | |
848 | } | |
849 | ||
850 | /** | |
851 | * Convert pixel row position to text cell row position. | |
852 | * | |
853 | * @param y pixel row position | |
854 | * @return text cell row position | |
855 | */ | |
856 | public int textRow(final int y) { | |
857 | return ((y - frame.top) / frame.textHeight); | |
858 | } | |
859 | ||
1ac2ccb1 | 860 | } |