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; |
84614868 | 36 | import java.awt.Insets; |
30bd4abd KL |
37 | import java.awt.Point; |
38 | import java.awt.Rectangle; | |
39 | import java.awt.Toolkit; | |
1ac2ccb1 | 40 | import java.awt.geom.Rectangle2D; |
30bd4abd | 41 | import java.awt.image.BufferedImage; |
e3dfbd23 | 42 | import java.awt.image.BufferStrategy; |
84614868 | 43 | import java.io.InputStream; |
b5f2a6db | 44 | import java.util.Date; |
aed33687 KL |
45 | import javax.swing.JFrame; |
46 | import javax.swing.SwingUtilities; | |
1ac2ccb1 | 47 | |
b5f2a6db KL |
48 | import jexer.bits.Cell; |
49 | import jexer.bits.CellAttributes; | |
50 | import jexer.session.SwingSessionInfo; | |
51 | ||
1ac2ccb1 | 52 | /** |
a4406f4e | 53 | * This Screen implementation draws to a Java Swing JFrame. |
1ac2ccb1 | 54 | */ |
a4406f4e | 55 | public final class SwingScreen extends Screen { |
1ac2ccb1 | 56 | |
e3dfbd23 KL |
57 | /** |
58 | * If true, use double buffering thread. | |
59 | */ | |
60 | private static final boolean doubleBuffer = true; | |
61 | ||
b5f2a6db KL |
62 | /** |
63 | * Cursor style to draw. | |
64 | */ | |
65 | public enum CursorStyle { | |
66 | /** | |
67 | * Use an underscore for the cursor. | |
68 | */ | |
69 | UNDERLINE, | |
70 | ||
71 | /** | |
72 | * Use a solid block for the cursor. | |
73 | */ | |
74 | BLOCK, | |
75 | ||
76 | /** | |
77 | * Use an outlined block for the cursor. | |
78 | */ | |
79 | OUTLINE | |
80 | } | |
81 | ||
84614868 KL |
82 | private static Color MYBLACK; |
83 | private static Color MYRED; | |
84 | private static Color MYGREEN; | |
85 | private static Color MYYELLOW; | |
86 | private static Color MYBLUE; | |
87 | private static Color MYMAGENTA; | |
88 | private static Color MYCYAN; | |
89 | private static Color MYWHITE; | |
90 | ||
91 | private static Color MYBOLD_BLACK; | |
92 | private static Color MYBOLD_RED; | |
93 | private static Color MYBOLD_GREEN; | |
94 | private static Color MYBOLD_YELLOW; | |
95 | private static Color MYBOLD_BLUE; | |
96 | private static Color MYBOLD_MAGENTA; | |
97 | private static Color MYBOLD_CYAN; | |
98 | private static Color MYBOLD_WHITE; | |
99 | ||
100 | private static boolean dosColors = false; | |
101 | ||
102 | /** | |
a4406f4e | 103 | * Setup Swing colors to match DOS color palette. |
84614868 KL |
104 | */ |
105 | private static void setDOSColors() { | |
106 | if (dosColors) { | |
107 | return; | |
108 | } | |
109 | MYBLACK = new Color(0x00, 0x00, 0x00); | |
110 | MYRED = new Color(0xa8, 0x00, 0x00); | |
111 | MYGREEN = new Color(0x00, 0xa8, 0x00); | |
112 | MYYELLOW = new Color(0xa8, 0x54, 0x00); | |
113 | MYBLUE = new Color(0x00, 0x00, 0xa8); | |
114 | MYMAGENTA = new Color(0xa8, 0x00, 0xa8); | |
115 | MYCYAN = new Color(0x00, 0xa8, 0xa8); | |
116 | MYWHITE = new Color(0xa8, 0xa8, 0xa8); | |
117 | MYBOLD_BLACK = new Color(0x54, 0x54, 0x54); | |
118 | MYBOLD_RED = new Color(0xfc, 0x54, 0x54); | |
119 | MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54); | |
120 | MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54); | |
121 | MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc); | |
122 | MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc); | |
123 | MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc); | |
124 | MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc); | |
125 | ||
126 | dosColors = true; | |
127 | } | |
128 | ||
1ac2ccb1 | 129 | /** |
a4406f4e | 130 | * SwingFrame is our top-level hook into the Swing system. |
1ac2ccb1 | 131 | */ |
a4406f4e | 132 | class SwingFrame extends JFrame { |
1ac2ccb1 | 133 | |
cf9af8df KL |
134 | /** |
135 | * Serializable version. | |
136 | */ | |
137 | private static final long serialVersionUID = 1; | |
138 | ||
84614868 KL |
139 | /** |
140 | * The terminus font resource filename. | |
141 | */ | |
142 | private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf"; | |
143 | ||
e3dfbd23 KL |
144 | /** |
145 | * The BufferStrategy object needed for double-buffering. | |
146 | */ | |
147 | private BufferStrategy bufferStrategy; | |
148 | ||
1ac2ccb1 KL |
149 | /** |
150 | * The TUI Screen data. | |
151 | */ | |
a4406f4e | 152 | SwingScreen screen; |
1ac2ccb1 KL |
153 | |
154 | /** | |
155 | * Width of a character cell. | |
156 | */ | |
157 | private int textWidth = 1; | |
158 | ||
159 | /** | |
160 | * Height of a character cell. | |
161 | */ | |
162 | private int textHeight = 1; | |
163 | ||
84614868 KL |
164 | /** |
165 | * Descent of a character cell. | |
166 | */ | |
167 | private int maxDescent = 0; | |
168 | ||
847a4bc5 KL |
169 | /** |
170 | * System-dependent Y adjustment for text in the character cell. | |
171 | */ | |
172 | private int textAdjustY = 0; | |
173 | ||
174 | /** | |
175 | * System-dependent X adjustment for text in the character cell. | |
176 | */ | |
177 | private int textAdjustX = 0; | |
178 | ||
1ac2ccb1 | 179 | /** |
b5f2a6db | 180 | * Top pixel absolute location. |
1ac2ccb1 KL |
181 | */ |
182 | private int top = 30; | |
84614868 | 183 | |
1ac2ccb1 | 184 | /** |
b5f2a6db | 185 | * Left pixel absolute location. |
1ac2ccb1 KL |
186 | */ |
187 | private int left = 30; | |
84614868 | 188 | |
b5f2a6db KL |
189 | /** |
190 | * The cursor style to draw. | |
191 | */ | |
192 | private CursorStyle cursorStyle = CursorStyle.UNDERLINE; | |
193 | ||
194 | /** | |
195 | * The number of millis to wait before switching the blink from | |
196 | * visible to invisible. | |
197 | */ | |
198 | private long blinkMillis = 500; | |
199 | ||
200 | /** | |
201 | * If true, the cursor should be visible right now based on the blink | |
202 | * time. | |
203 | */ | |
204 | private boolean cursorBlinkVisible = true; | |
205 | ||
206 | /** | |
207 | * The time that the blink last flipped from visible to invisible or | |
208 | * from invisible to visible. | |
209 | */ | |
210 | private long lastBlinkTime = 0; | |
211 | ||
84614868 | 212 | /** |
a4406f4e | 213 | * Convert a CellAttributes foreground color to an Swing Color. |
84614868 KL |
214 | * |
215 | * @param attr the text attributes | |
a4406f4e | 216 | * @return the Swing Color |
84614868 KL |
217 | */ |
218 | private Color attrToForegroundColor(final CellAttributes attr) { | |
7c870d89 | 219 | if (attr.isBold()) { |
84614868 KL |
220 | if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) { |
221 | return MYBOLD_BLACK; | |
222 | } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) { | |
223 | return MYBOLD_RED; | |
224 | } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) { | |
225 | return MYBOLD_BLUE; | |
226 | } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) { | |
227 | return MYBOLD_GREEN; | |
228 | } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) { | |
229 | return MYBOLD_YELLOW; | |
230 | } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) { | |
231 | return MYBOLD_CYAN; | |
232 | } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) { | |
233 | return MYBOLD_MAGENTA; | |
234 | } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) { | |
235 | return MYBOLD_WHITE; | |
236 | } | |
237 | } else { | |
238 | if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) { | |
239 | return MYBLACK; | |
240 | } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) { | |
241 | return MYRED; | |
242 | } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) { | |
243 | return MYBLUE; | |
244 | } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) { | |
245 | return MYGREEN; | |
246 | } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) { | |
247 | return MYYELLOW; | |
248 | } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) { | |
249 | return MYCYAN; | |
250 | } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) { | |
251 | return MYMAGENTA; | |
252 | } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) { | |
253 | return MYWHITE; | |
254 | } | |
255 | } | |
256 | throw new IllegalArgumentException("Invalid color: " + attr.getForeColor().getValue()); | |
257 | } | |
258 | ||
259 | /** | |
a4406f4e | 260 | * Convert a CellAttributes background color to an Swing Color. |
84614868 KL |
261 | * |
262 | * @param attr the text attributes | |
a4406f4e | 263 | * @return the Swing Color |
84614868 KL |
264 | */ |
265 | private Color attrToBackgroundColor(final CellAttributes attr) { | |
84614868 KL |
266 | if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) { |
267 | return MYBLACK; | |
268 | } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) { | |
269 | return MYRED; | |
270 | } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) { | |
271 | return MYBLUE; | |
272 | } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) { | |
273 | return MYGREEN; | |
274 | } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) { | |
275 | return MYYELLOW; | |
276 | } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) { | |
277 | return MYCYAN; | |
278 | } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) { | |
279 | return MYMAGENTA; | |
280 | } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) { | |
281 | return MYWHITE; | |
282 | } | |
283 | throw new IllegalArgumentException("Invalid color: " + attr.getBackColor().getValue()); | |
284 | } | |
285 | ||
1ac2ccb1 KL |
286 | /** |
287 | * Public constructor. | |
84614868 KL |
288 | * |
289 | * @param screen the Screen that Backend talks to | |
1ac2ccb1 | 290 | */ |
a4406f4e | 291 | public SwingFrame(final SwingScreen screen) { |
84614868 KL |
292 | this.screen = screen; |
293 | setDOSColors(); | |
294 | ||
b5f2a6db KL |
295 | // Figure out my cursor style |
296 | String cursorStyleString = System.getProperty("jexer.Swing.cursorStyle", | |
297 | "underline").toLowerCase(); | |
298 | ||
299 | if (cursorStyleString.equals("underline")) { | |
300 | cursorStyle = CursorStyle.UNDERLINE; | |
301 | } else if (cursorStyleString.equals("outline")) { | |
302 | cursorStyle = CursorStyle.OUTLINE; | |
303 | } else if (cursorStyleString.equals("block")) { | |
304 | cursorStyle = CursorStyle.BLOCK; | |
305 | } | |
306 | ||
1ac2ccb1 | 307 | setTitle("Jexer Application"); |
aed33687 | 308 | setBackground(Color.black); |
84614868 KL |
309 | |
310 | try { | |
30bd4abd | 311 | // Always try to use Terminus, the one decent font. |
84614868 KL |
312 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
313 | InputStream in = loader.getResourceAsStream(FONTFILE); | |
314 | Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in); | |
315 | Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22); | |
316 | setFont(terminus); | |
317 | } catch (Exception e) { | |
318 | e.printStackTrace(); | |
319 | // setFont(new Font("Liberation Mono", Font.PLAIN, 24)); | |
320 | setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24)); | |
321 | } | |
aed33687 | 322 | pack(); |
30bd4abd KL |
323 | |
324 | // Kill the X11 cursor | |
325 | // Transparent 16 x 16 pixel cursor image. | |
326 | BufferedImage cursorImg = new BufferedImage(16, 16, | |
327 | BufferedImage.TYPE_INT_ARGB); | |
30bd4abd KL |
328 | // Create a new blank cursor. |
329 | Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor( | |
330 | cursorImg, new Point(0, 0), "blank cursor"); | |
331 | setCursor(blankCursor); | |
091d8a06 KL |
332 | |
333 | // Be capable of seeing Tab / Shift-Tab | |
334 | setFocusTraversalKeysEnabled(false); | |
335 | ||
336 | // Save the text cell width/height | |
bd8d51fa | 337 | getFontDimensions(); |
e3dfbd23 KL |
338 | |
339 | // Setup double-buffering | |
0d47c546 | 340 | if (SwingScreen.doubleBuffer) { |
e3dfbd23 KL |
341 | setIgnoreRepaint(true); |
342 | createBufferStrategy(2); | |
343 | bufferStrategy = getBufferStrategy(); | |
344 | } | |
1ac2ccb1 KL |
345 | } |
346 | ||
347 | /** | |
bd8d51fa | 348 | * Figure out my font dimensions. |
1ac2ccb1 | 349 | */ |
bd8d51fa | 350 | private void getFontDimensions() { |
84614868 KL |
351 | Graphics gr = getGraphics(); |
352 | FontMetrics fm = gr.getFontMetrics(); | |
353 | maxDescent = fm.getMaxDescent(); | |
354 | Rectangle2D bounds = fm.getMaxCharBounds(gr); | |
355 | int leading = fm.getLeading(); | |
356 | textWidth = (int)Math.round(bounds.getWidth()); | |
357 | textHeight = (int)Math.round(bounds.getHeight()) - maxDescent; | |
358 | // This also produces the same number, but works better for ugly | |
359 | // monospace. | |
360 | textHeight = fm.getMaxAscent() + maxDescent - leading; | |
847a4bc5 KL |
361 | |
362 | if (System.getProperty("os.name").startsWith("Windows")) { | |
363 | textAdjustY = -1; | |
364 | textAdjustX = 0; | |
365 | } | |
bd8d51fa | 366 | } |
84614868 | 367 | |
bd8d51fa KL |
368 | /** |
369 | * Resize to font dimensions. | |
370 | */ | |
371 | public void resizeToScreen() { | |
84614868 KL |
372 | // Figure out the thickness of borders and use that to set the |
373 | // final size. | |
374 | Insets insets = getInsets(); | |
375 | left = insets.left; | |
376 | top = insets.top; | |
377 | ||
378 | setSize(textWidth * screen.width + insets.left + insets.right, | |
379 | textHeight * screen.height + insets.top + insets.bottom); | |
1ac2ccb1 KL |
380 | } |
381 | ||
30bd4abd KL |
382 | /** |
383 | * Update redraws the whole screen. | |
384 | * | |
a4406f4e | 385 | * @param gr the Swing Graphics context |
30bd4abd KL |
386 | */ |
387 | @Override | |
388 | public void update(final Graphics gr) { | |
389 | // The default update clears the area. Don't do that, instead | |
390 | // just paint it directly. | |
391 | paint(gr); | |
392 | } | |
393 | ||
1ac2ccb1 KL |
394 | /** |
395 | * Paint redraws the whole screen. | |
396 | * | |
a4406f4e | 397 | * @param gr the Swing Graphics context |
1ac2ccb1 KL |
398 | */ |
399 | @Override | |
84614868 | 400 | public void paint(final Graphics gr) { |
87a17f3c KL |
401 | // Do nothing until the screen reference has been set. |
402 | if (screen == null) { | |
403 | return; | |
404 | } | |
405 | if (screen.frame == null) { | |
406 | return; | |
407 | } | |
408 | ||
b5f2a6db KL |
409 | // See if it is time to flip the blink time. |
410 | long nowTime = (new Date()).getTime(); | |
411 | if (nowTime > blinkMillis + lastBlinkTime) { | |
412 | lastBlinkTime = nowTime; | |
413 | cursorBlinkVisible = !cursorBlinkVisible; | |
414 | } | |
415 | ||
87a17f3c KL |
416 | int xCellMin = 0; |
417 | int xCellMax = screen.width; | |
418 | int yCellMin = 0; | |
419 | int yCellMax = screen.height; | |
420 | ||
30bd4abd | 421 | Rectangle bounds = gr.getClipBounds(); |
87a17f3c KL |
422 | if (bounds != null) { |
423 | // Only update what is in the bounds | |
424 | xCellMin = screen.textColumn(bounds.x); | |
bd8d51fa | 425 | xCellMax = screen.textColumn(bounds.x + bounds.width); |
87a17f3c KL |
426 | if (xCellMax > screen.width) { |
427 | xCellMax = screen.width; | |
428 | } | |
429 | if (xCellMin >= xCellMax) { | |
430 | xCellMin = xCellMax - 2; | |
431 | } | |
432 | if (xCellMin < 0) { | |
433 | xCellMin = 0; | |
434 | } | |
435 | yCellMin = screen.textRow(bounds.y); | |
bd8d51fa | 436 | yCellMax = screen.textRow(bounds.y + bounds.height); |
87a17f3c KL |
437 | if (yCellMax > screen.height) { |
438 | yCellMax = screen.height; | |
439 | } | |
440 | if (yCellMin >= yCellMax) { | |
441 | yCellMin = yCellMax - 2; | |
442 | } | |
443 | if (yCellMin < 0) { | |
444 | yCellMin = 0; | |
445 | } | |
bd8d51fa KL |
446 | } else { |
447 | // We need a total repaint | |
448 | reallyCleared = true; | |
87a17f3c | 449 | } |
1ac2ccb1 | 450 | |
87a17f3c KL |
451 | // Prevent updates to the screen's data from the TApplication |
452 | // threads. | |
453 | synchronized (screen) { | |
454 | /* | |
455 | System.err.printf("bounds %s X %d %d Y %d %d\n", | |
456 | bounds, xCellMin, xCellMax, yCellMin, yCellMax); | |
457 | */ | |
458 | ||
459 | for (int y = yCellMin; y < yCellMax; y++) { | |
460 | for (int x = xCellMin; x < xCellMax; x++) { | |
461 | ||
462 | int xPixel = x * textWidth + left; | |
463 | int yPixel = y * textHeight + top; | |
464 | ||
465 | Cell lCell = screen.logical[x][y]; | |
466 | Cell pCell = screen.physical[x][y]; | |
467 | ||
55b4f29b KL |
468 | if (!lCell.equals(pCell) |
469 | || lCell.isBlink() | |
470 | || reallyCleared) { | |
e3dfbd23 | 471 | |
55b4f29b KL |
472 | Cell lCellColor = new Cell(); |
473 | lCellColor.setTo(lCell); | |
474 | ||
475 | // Check for reverse | |
476 | if (lCell.isReverse()) { | |
477 | lCellColor.setForeColor(lCell.getBackColor()); | |
478 | lCellColor.setBackColor(lCell.getForeColor()); | |
479 | } | |
e3dfbd23 | 480 | |
87a17f3c KL |
481 | // Draw the background rectangle, then the |
482 | // foreground character. | |
55b4f29b | 483 | gr.setColor(attrToBackgroundColor(lCellColor)); |
87a17f3c | 484 | gr.fillRect(xPixel, yPixel, textWidth, textHeight); |
55b4f29b KL |
485 | |
486 | // Handle blink and underline | |
487 | if (!lCell.isBlink() | |
488 | || (lCell.isBlink() && cursorBlinkVisible) | |
489 | ) { | |
490 | gr.setColor(attrToForegroundColor(lCellColor)); | |
491 | char [] chars = new char[1]; | |
492 | chars[0] = lCell.getChar(); | |
847a4bc5 KL |
493 | gr.drawChars(chars, 0, 1, xPixel + textAdjustX, |
494 | yPixel + textHeight - maxDescent | |
495 | + textAdjustY); | |
496 | ||
55b4f29b KL |
497 | if (lCell.isUnderline()) { |
498 | gr.fillRect(xPixel, yPixel + textHeight - 2, | |
499 | textWidth, 2); | |
500 | } | |
501 | } | |
87a17f3c KL |
502 | |
503 | // Physical is always updated | |
504 | physical[x][y].setTo(lCell); | |
30bd4abd KL |
505 | } |
506 | } | |
87a17f3c | 507 | } |
84614868 | 508 | |
87a17f3c | 509 | // Draw the cursor if it is visible |
b5f2a6db | 510 | if (cursorVisible |
87a17f3c KL |
511 | && (cursorY <= screen.height - 1) |
512 | && (cursorX <= screen.width - 1) | |
b5f2a6db | 513 | && cursorBlinkVisible |
87a17f3c KL |
514 | ) { |
515 | int xPixel = cursorX * textWidth + left; | |
516 | int yPixel = cursorY * textHeight + top; | |
517 | Cell lCell = screen.logical[cursorX][cursorY]; | |
518 | gr.setColor(attrToForegroundColor(lCell)); | |
b5f2a6db | 519 | switch (cursorStyle) { |
329fd62e KL |
520 | default: |
521 | // Fall through... | |
b5f2a6db KL |
522 | case UNDERLINE: |
523 | gr.fillRect(xPixel, yPixel + textHeight - 2, | |
524 | textWidth, 2); | |
525 | break; | |
526 | case BLOCK: | |
527 | gr.fillRect(xPixel, yPixel, textWidth, textHeight); | |
528 | break; | |
529 | case OUTLINE: | |
530 | gr.drawRect(xPixel, yPixel, textWidth - 1, | |
531 | textHeight - 1); | |
532 | break; | |
533 | } | |
1ac2ccb1 | 534 | } |
30bd4abd | 535 | |
87a17f3c KL |
536 | dirty = false; |
537 | reallyCleared = false; | |
538 | } // synchronized (screen) | |
1ac2ccb1 | 539 | } |
bd8d51fa | 540 | |
a4406f4e | 541 | } // class SwingFrame |
1ac2ccb1 KL |
542 | |
543 | /** | |
a4406f4e | 544 | * The raw Swing JFrame. Note package private access. |
1ac2ccb1 | 545 | */ |
a4406f4e | 546 | SwingFrame frame; |
1ac2ccb1 | 547 | |
e3dfbd23 KL |
548 | /** |
549 | * Restore terminal to normal state. | |
550 | */ | |
551 | public void shutdown() { | |
552 | frame.dispose(); | |
553 | } | |
554 | ||
1ac2ccb1 KL |
555 | /** |
556 | * Public constructor. | |
557 | */ | |
a4406f4e | 558 | public SwingScreen() { |
aed33687 KL |
559 | try { |
560 | SwingUtilities.invokeAndWait(new Runnable() { | |
561 | public void run() { | |
a4406f4e KL |
562 | SwingScreen.this.frame = new SwingFrame(SwingScreen.this); |
563 | SwingScreen.this.sessionInfo = | |
564 | new SwingSessionInfo(SwingScreen.this.frame, | |
bd8d51fa KL |
565 | frame.textWidth, |
566 | frame.textHeight); | |
567 | ||
a4406f4e | 568 | SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(), |
bd8d51fa KL |
569 | sessionInfo.getWindowHeight()); |
570 | ||
a4406f4e KL |
571 | SwingScreen.this.frame.resizeToScreen(); |
572 | SwingScreen.this.frame.setVisible(true); | |
aed33687 | 573 | } |
329fd62e | 574 | }); |
aed33687 KL |
575 | } catch (Exception e) { |
576 | e.printStackTrace(); | |
577 | } | |
1ac2ccb1 KL |
578 | } |
579 | ||
bd8d51fa KL |
580 | /** |
581 | * The sessionInfo. | |
582 | */ | |
a4406f4e | 583 | private SwingSessionInfo sessionInfo; |
bd8d51fa | 584 | |
30bd4abd | 585 | /** |
a4406f4e | 586 | * Create the SwingSessionInfo. Note package private access. |
30bd4abd KL |
587 | * |
588 | * @return the sessionInfo | |
589 | */ | |
a4406f4e | 590 | SwingSessionInfo getSessionInfo() { |
30bd4abd KL |
591 | return sessionInfo; |
592 | } | |
593 | ||
1ac2ccb1 KL |
594 | /** |
595 | * Push the logical screen to the physical device. | |
596 | */ | |
597 | @Override | |
598 | public void flushPhysical() { | |
87a17f3c KL |
599 | |
600 | if (reallyCleared) { | |
601 | // Really refreshed, do it all | |
0d47c546 | 602 | if (SwingScreen.doubleBuffer) { |
e3dfbd23 KL |
603 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); |
604 | frame.paint(gr); | |
605 | gr.dispose(); | |
606 | frame.bufferStrategy.show(); | |
607 | Toolkit.getDefaultToolkit().sync(); | |
608 | } else { | |
609 | frame.repaint(); | |
610 | } | |
87a17f3c KL |
611 | return; |
612 | } | |
613 | ||
614 | // Do nothing if nothing happened. | |
615 | if (!dirty) { | |
616 | return; | |
617 | } | |
618 | ||
30bd4abd KL |
619 | // Request a repaint, let the frame's repaint/update methods do the |
620 | // right thing. | |
87a17f3c | 621 | |
30bd4abd KL |
622 | // Find the minimum-size damaged region. |
623 | int xMin = frame.getWidth(); | |
624 | int xMax = 0; | |
625 | int yMin = frame.getHeight(); | |
626 | int yMax = 0; | |
30bd4abd | 627 | |
87a17f3c KL |
628 | synchronized (this) { |
629 | for (int y = 0; y < height; y++) { | |
630 | for (int x = 0; x < width; x++) { | |
631 | Cell lCell = logical[x][y]; | |
632 | Cell pCell = physical[x][y]; | |
633 | ||
634 | int xPixel = x * frame.textWidth + frame.left; | |
635 | int yPixel = y * frame.textHeight + frame.top; | |
636 | ||
637 | if (!lCell.equals(pCell) | |
638 | || ((x == cursorX) | |
639 | && (y == cursorY) | |
640 | && cursorVisible) | |
55b4f29b | 641 | || lCell.isBlink() |
87a17f3c KL |
642 | ) { |
643 | if (xPixel < xMin) { | |
644 | xMin = xPixel; | |
645 | } | |
646 | if (xPixel + frame.textWidth > xMax) { | |
647 | xMax = xPixel + frame.textWidth; | |
648 | } | |
649 | if (yPixel < yMin) { | |
650 | yMin = yPixel; | |
651 | } | |
652 | if (yPixel + frame.textHeight > yMax) { | |
653 | yMax = yPixel + frame.textHeight; | |
654 | } | |
30bd4abd KL |
655 | } |
656 | } | |
657 | } | |
658 | } | |
87a17f3c KL |
659 | if (xMin + frame.textWidth >= xMax) { |
660 | xMax += frame.textWidth; | |
661 | } | |
662 | if (yMin + frame.textHeight >= yMax) { | |
663 | yMax += frame.textHeight; | |
664 | } | |
30bd4abd | 665 | |
87a17f3c | 666 | // Repaint the desired area |
e3dfbd23 KL |
667 | // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, |
668 | // yMin, yMax); | |
0d47c546 | 669 | if (SwingScreen.doubleBuffer) { |
e3dfbd23 KL |
670 | Graphics gr = frame.bufferStrategy.getDrawGraphics(); |
671 | Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin, | |
672 | yMax - yMin); | |
673 | gr.setClip(bounds); | |
674 | frame.paint(gr); | |
675 | gr.dispose(); | |
676 | frame.bufferStrategy.show(); | |
677 | Toolkit.getDefaultToolkit().sync(); | |
678 | } else { | |
679 | frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin); | |
680 | } | |
1ac2ccb1 | 681 | } |
30bd4abd KL |
682 | |
683 | /** | |
684 | * Put the cursor at (x,y). | |
685 | * | |
686 | * @param visible if true, the cursor should be visible | |
687 | * @param x column coordinate to put the cursor on | |
688 | * @param y row coordinate to put the cursor on | |
689 | */ | |
690 | @Override | |
691 | public void putCursor(final boolean visible, final int x, final int y) { | |
b5f2a6db KL |
692 | |
693 | if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) { | |
694 | // See if it is time to flip the blink time. | |
695 | long nowTime = (new Date()).getTime(); | |
696 | if (nowTime < frame.blinkMillis + frame.lastBlinkTime) { | |
697 | // Nothing has changed, so don't do anything. | |
698 | return; | |
699 | } | |
700 | } | |
701 | ||
702 | if (cursorVisible | |
30bd4abd KL |
703 | && (cursorY <= height - 1) |
704 | && (cursorX <= width - 1) | |
705 | ) { | |
706 | // Make the current cursor position dirty | |
87a17f3c | 707 | if (physical[cursorX][cursorY].getChar() == 'Q') { |
30bd4abd KL |
708 | physical[cursorX][cursorY].setChar('X'); |
709 | } else { | |
87a17f3c | 710 | physical[cursorX][cursorY].setChar('Q'); |
30bd4abd KL |
711 | } |
712 | } | |
713 | ||
714 | super.putCursor(visible, x, y); | |
715 | } | |
716 | ||
87a17f3c KL |
717 | /** |
718 | * Convert pixel column position to text cell column position. | |
719 | * | |
720 | * @param x pixel column position | |
721 | * @return text cell column position | |
722 | */ | |
723 | public int textColumn(final int x) { | |
724 | return ((x - frame.left) / frame.textWidth); | |
725 | } | |
726 | ||
727 | /** | |
728 | * Convert pixel row position to text cell row position. | |
729 | * | |
730 | * @param y pixel row position | |
731 | * @return text cell row position | |
732 | */ | |
733 | public int textRow(final int y) { | |
734 | return ((y - frame.top) / frame.textHeight); | |
735 | } | |
736 | ||
1ac2ccb1 | 737 | } |