Redraw on Windows
[nikiroo-utils.git] / src / jexer / backend / SwingTerminal.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.backend;
30
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Font;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics2D;
36 import java.awt.Graphics;
37 import java.awt.Insets;
38 import java.awt.Rectangle;
39 import java.awt.Toolkit;
40 import java.awt.event.ComponentEvent;
41 import java.awt.event.ComponentListener;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.KeyListener;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
47 import java.awt.event.MouseWheelEvent;
48 import java.awt.event.MouseWheelListener;
49 import java.awt.event.WindowEvent;
50 import java.awt.event.WindowListener;
51 import java.awt.geom.Rectangle2D;
52 import java.awt.image.BufferedImage;
53 import java.io.InputStream;
54 import java.util.HashMap;
55 import java.util.LinkedList;
56 import java.util.List;
57 import java.util.Map;
58 import javax.swing.JComponent;
59 import javax.swing.JFrame;
60 import javax.swing.ImageIcon;
61 import javax.swing.SwingUtilities;
62
63 import jexer.TKeypress;
64 import jexer.bits.Cell;
65 import jexer.bits.CellAttributes;
66 import jexer.event.TCommandEvent;
67 import jexer.event.TInputEvent;
68 import jexer.event.TKeypressEvent;
69 import jexer.event.TMouseEvent;
70 import jexer.event.TResizeEvent;
71 import static jexer.TCommand.*;
72 import static jexer.TKeypress.*;
73
74 /**
75 * This Screen backend reads keystrokes and mouse events and draws to either
76 * a Java Swing JFrame (potentially triple-buffered) or a JComponent.
77 *
78 * This class is a bit of an inversion of typical GUI classes. It performs
79 * all of the drawing logic from SwingTerminal (which is not a Swing class),
80 * and uses a SwingComponent wrapper class to call the JFrame or JComponent
81 * methods.
82 */
83 public class SwingTerminal extends LogicalScreen
84 implements TerminalReader,
85 ComponentListener, KeyListener,
86 MouseListener, MouseMotionListener,
87 MouseWheelListener, WindowListener {
88
89 // ------------------------------------------------------------------------
90 // Constants --------------------------------------------------------------
91 // ------------------------------------------------------------------------
92
93 /**
94 * The icon image location.
95 */
96 private static final String ICONFILE = "jexer_logo_128.png";
97
98 /**
99 * The terminus font resource filename.
100 */
101 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
102
103 /**
104 * Cursor style to draw.
105 */
106 public enum CursorStyle {
107 /**
108 * Use an underscore for the cursor.
109 */
110 UNDERLINE,
111
112 /**
113 * Use a solid block for the cursor.
114 */
115 BLOCK,
116
117 /**
118 * Use an outlined block for the cursor.
119 */
120 OUTLINE
121 }
122
123 // ------------------------------------------------------------------------
124 // Variables --------------------------------------------------------------
125 // ------------------------------------------------------------------------
126
127 // Colors to map DOS colors to AWT colors.
128 private static Color MYBLACK;
129 private static Color MYRED;
130 private static Color MYGREEN;
131 private static Color MYYELLOW;
132 private static Color MYBLUE;
133 private static Color MYMAGENTA;
134 private static Color MYCYAN;
135 private static Color MYWHITE;
136 private static Color MYBOLD_BLACK;
137 private static Color MYBOLD_RED;
138 private static Color MYBOLD_GREEN;
139 private static Color MYBOLD_YELLOW;
140 private static Color MYBOLD_BLUE;
141 private static Color MYBOLD_MAGENTA;
142 private static Color MYBOLD_CYAN;
143 private static Color MYBOLD_WHITE;
144
145 /**
146 * When true, all the MYBLACK, MYRED, etc. colors are set.
147 */
148 private static boolean dosColors = false;
149
150 /**
151 * The Swing component or frame to draw to.
152 */
153 private SwingComponent swing;
154
155 /**
156 * A cache of previously-rendered glyphs for blinking text, when it is
157 * not visible.
158 */
159 private Map<Cell, BufferedImage> glyphCacheBlink;
160
161 /**
162 * A cache of previously-rendered glyphs for non-blinking, or
163 * blinking-and-visible, text.
164 */
165 private Map<Cell, BufferedImage> glyphCache;
166
167 /**
168 * If true, we were successful at getting the font dimensions.
169 */
170 private boolean gotFontDimensions = false;
171
172 /**
173 * The currently selected font.
174 */
175 private Font font = null;
176
177 /**
178 * The currently selected font size in points.
179 */
180 private int fontSize = 16;
181
182 /**
183 * Width of a character cell in pixels.
184 */
185 private int textWidth = 1;
186
187 /**
188 * Height of a character cell in pixels.
189 */
190 private int textHeight = 1;
191
192 /**
193 * Width of a character cell in pixels, as reported by font.
194 */
195 private int fontTextWidth = 1;
196
197 /**
198 * Height of a character cell in pixels, as reported by font.
199 */
200 private int fontTextHeight = 1;
201
202 /**
203 * Descent of a character cell in pixels.
204 */
205 private int maxDescent = 0;
206
207 /**
208 * System-dependent Y adjustment for text in the character cell.
209 */
210 private int textAdjustY = 0;
211
212 /**
213 * System-dependent X adjustment for text in the character cell.
214 */
215 private int textAdjustX = 0;
216
217 /**
218 * System-dependent height adjustment for text in the character cell.
219 */
220 private int textAdjustHeight = 0;
221
222 /**
223 * System-dependent width adjustment for text in the character cell.
224 */
225 private int textAdjustWidth = 0;
226
227 /**
228 * Top pixel absolute location.
229 */
230 private int top = 30;
231
232 /**
233 * Left pixel absolute location.
234 */
235 private int left = 30;
236
237 /**
238 * The cursor style to draw.
239 */
240 private CursorStyle cursorStyle = CursorStyle.UNDERLINE;
241
242 /**
243 * The number of millis to wait before switching the blink from visible
244 * to invisible. Set to 0 or negative to disable blinking.
245 */
246 private long blinkMillis = 500;
247
248 /**
249 * If true, the cursor should be visible right now based on the blink
250 * time.
251 */
252 private boolean cursorBlinkVisible = true;
253
254 /**
255 * The time that the blink last flipped from visible to invisible or
256 * from invisible to visible.
257 */
258 private long lastBlinkTime = 0;
259
260 /**
261 * The session information.
262 */
263 private SwingSessionInfo sessionInfo;
264
265 /**
266 * The listening object that run() wakes up on new input.
267 */
268 private Object listener;
269
270 /**
271 * The event queue, filled up by a thread reading on input.
272 */
273 private List<TInputEvent> eventQueue;
274
275 /**
276 * The last reported mouse X position.
277 */
278 private int oldMouseX = -1;
279
280 /**
281 * The last reported mouse Y position.
282 */
283 private int oldMouseY = -1;
284
285 /**
286 * true if mouse1 was down. Used to report mouse1 on the release event.
287 */
288 private boolean mouse1 = false;
289
290 /**
291 * true if mouse2 was down. Used to report mouse2 on the release event.
292 */
293 private boolean mouse2 = false;
294
295 /**
296 * true if mouse3 was down. Used to report mouse3 on the release event.
297 */
298 private boolean mouse3 = false;
299
300 // ------------------------------------------------------------------------
301 // Constructors -----------------------------------------------------------
302 // ------------------------------------------------------------------------
303
304 /**
305 * Public constructor creates a new JFrame to render to.
306 *
307 * @param windowWidth the number of text columns to start with
308 * @param windowHeight the number of text rows to start with
309 * @param fontSize the size in points. Good values to pick are: 16, 20,
310 * 22, and 24.
311 * @param listener the object this backend needs to wake up when new
312 * input comes in
313 */
314 public SwingTerminal(final int windowWidth, final int windowHeight,
315 final int fontSize, final Object listener) {
316
317 this.fontSize = fontSize;
318
319 setDOSColors();
320 reloadOptions();
321
322 try {
323 SwingUtilities.invokeAndWait(new Runnable() {
324 public void run() {
325
326 JFrame frame = new JFrame() {
327
328 /**
329 * Serializable version.
330 */
331 private static final long serialVersionUID = 1;
332
333 /**
334 * The code that performs the actual drawing.
335 */
336 public SwingTerminal screen = null;
337
338 /*
339 * Anonymous class initializer saves the screen
340 * reference, so that paint() and the like call out
341 * to SwingTerminal.
342 */
343 {
344 this.screen = SwingTerminal.this;
345 }
346
347 /**
348 * Update redraws the whole screen.
349 *
350 * @param gr the Swing Graphics context
351 */
352 @Override
353 public void update(final Graphics gr) {
354 // The default update clears the area. Don't do
355 // that, instead just paint it directly.
356 paint(gr);
357 }
358
359 /**
360 * Paint redraws the whole screen.
361 *
362 * @param gr the Swing Graphics context
363 */
364 @Override
365 public void paint(final Graphics gr) {
366 if (screen != null) {
367 screen.paint(gr);
368 }
369 }
370 };
371
372 // Set icon
373 ClassLoader loader = Thread.currentThread().
374 getContextClassLoader();
375 frame.setIconImage((new ImageIcon(loader.
376 getResource(ICONFILE))).getImage());
377
378 // Get the Swing component
379 SwingTerminal.this.swing = new SwingComponent(frame);
380
381 // Hang onto top and left for drawing.
382 Insets insets = SwingTerminal.this.swing.getInsets();
383 SwingTerminal.this.left = insets.left;
384 SwingTerminal.this.top = insets.top;
385
386 // Load the font so that we can set sessionInfo.
387 setDefaultFont();
388
389 // Get the default cols x rows and set component size
390 // accordingly.
391 SwingTerminal.this.sessionInfo =
392 new SwingSessionInfo(SwingTerminal.this.swing,
393 SwingTerminal.this.textWidth,
394 SwingTerminal.this.textHeight,
395 windowWidth, windowHeight);
396
397 SwingTerminal.this.setDimensions(sessionInfo.
398 getWindowWidth(), sessionInfo.getWindowHeight());
399
400 SwingTerminal.this.resizeToScreen();
401 SwingTerminal.this.swing.setVisible(true);
402 }
403 });
404 } catch (java.lang.reflect.InvocationTargetException e) {
405 e.printStackTrace();
406 } catch (InterruptedException e) {
407 e.printStackTrace();
408 }
409
410 this.listener = listener;
411 mouse1 = false;
412 mouse2 = false;
413 mouse3 = false;
414 eventQueue = new LinkedList<TInputEvent>();
415
416 // Add listeners to Swing.
417 swing.addKeyListener(this);
418 swing.addWindowListener(this);
419 swing.addComponentListener(this);
420 swing.addMouseListener(this);
421 swing.addMouseMotionListener(this);
422 swing.addMouseWheelListener(this);
423 }
424
425 /**
426 * Public constructor renders to an existing JComponent.
427 *
428 * @param component the Swing component to render to
429 * @param windowWidth the number of text columns to start with
430 * @param windowHeight the number of text rows to start with
431 * @param fontSize the size in points. Good values to pick are: 16, 20,
432 * 22, and 24.
433 * @param listener the object this backend needs to wake up when new
434 * input comes in
435 */
436 public SwingTerminal(final JComponent component, final int windowWidth,
437 final int windowHeight, final int fontSize, final Object listener) {
438
439 this.fontSize = fontSize;
440
441 setDOSColors();
442 reloadOptions();
443
444 try {
445 SwingUtilities.invokeAndWait(new Runnable() {
446 public void run() {
447
448 JComponent newComponent = new JComponent() {
449
450 /**
451 * Serializable version.
452 */
453 private static final long serialVersionUID = 1;
454
455 /**
456 * The code that performs the actual drawing.
457 */
458 public SwingTerminal screen = null;
459
460 /*
461 * Anonymous class initializer saves the screen
462 * reference, so that paint() and the like call out
463 * to SwingTerminal.
464 */
465 {
466 this.screen = SwingTerminal.this;
467 }
468
469 /**
470 * Update redraws the whole screen.
471 *
472 * @param gr the Swing Graphics context
473 */
474 @Override
475 public void update(final Graphics gr) {
476 // The default update clears the area. Don't do
477 // that, instead just paint it directly.
478 paint(gr);
479 }
480
481 /**
482 * Paint redraws the whole screen.
483 *
484 * @param gr the Swing Graphics context
485 */
486 @Override
487 public void paint(final Graphics gr) {
488 if (screen != null) {
489 screen.paint(gr);
490 }
491 }
492 };
493 component.setLayout(new BorderLayout());
494 component.add(newComponent);
495
496 // Allow key events to be received
497 component.setFocusable(true);
498
499 // Get the Swing component
500 SwingTerminal.this.swing = new SwingComponent(component);
501
502 // Hang onto top and left for drawing.
503 Insets insets = SwingTerminal.this.swing.getInsets();
504 SwingTerminal.this.left = insets.left;
505 SwingTerminal.this.top = insets.top;
506
507 // Load the font so that we can set sessionInfo.
508 setDefaultFont();
509
510 // Get the default cols x rows and set component size
511 // accordingly.
512 SwingTerminal.this.sessionInfo =
513 new SwingSessionInfo(SwingTerminal.this.swing,
514 SwingTerminal.this.textWidth,
515 SwingTerminal.this.textHeight);
516 }
517 });
518 } catch (java.lang.reflect.InvocationTargetException e) {
519 e.printStackTrace();
520 } catch (InterruptedException e) {
521 e.printStackTrace();
522 }
523
524 this.listener = listener;
525 mouse1 = false;
526 mouse2 = false;
527 mouse3 = false;
528 eventQueue = new LinkedList<TInputEvent>();
529
530 // Add listeners to Swing.
531 swing.addKeyListener(this);
532 swing.addWindowListener(this);
533 swing.addComponentListener(this);
534 swing.addMouseListener(this);
535 swing.addMouseMotionListener(this);
536 swing.addMouseWheelListener(this);
537 }
538
539 // ------------------------------------------------------------------------
540 // LogicalScreen ----------------------------------------------------------
541 // ------------------------------------------------------------------------
542
543 /**
544 * Set the window title.
545 *
546 * @param title the new title
547 */
548 @Override
549 public void setTitle(final String title) {
550 swing.setTitle(title);
551 }
552
553 /**
554 * Push the logical screen to the physical device.
555 */
556 @Override
557 public void flushPhysical() {
558 // See if it is time to flip the blink time.
559 long nowTime = System.currentTimeMillis();
560 if (nowTime >= blinkMillis + lastBlinkTime) {
561 lastBlinkTime = nowTime;
562 cursorBlinkVisible = !cursorBlinkVisible;
563 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
564 }
565
566 if ((swing.getFrame() != null)
567 && (swing.getBufferStrategy() != null)
568 ) {
569 do {
570 clearPhysical();
571 do {
572 drawToSwing();
573 } while (swing.getBufferStrategy().contentsRestored());
574
575 swing.getBufferStrategy().show();
576 Toolkit.getDefaultToolkit().sync();
577 } while (swing.getBufferStrategy().contentsLost());
578
579 } else {
580 // Non-triple-buffered, call drawToSwing() once
581 drawToSwing();
582 }
583 }
584
585 // ------------------------------------------------------------------------
586 // TerminalReader ---------------------------------------------------------
587 // ------------------------------------------------------------------------
588
589 /**
590 * Check if there are events in the queue.
591 *
592 * @return if true, getEvents() has something to return to the backend
593 */
594 public boolean hasEvents() {
595 synchronized (eventQueue) {
596 return (eventQueue.size() > 0);
597 }
598 }
599
600 /**
601 * Return any events in the IO queue.
602 *
603 * @param queue list to append new events to
604 */
605 public void getEvents(final List<TInputEvent> queue) {
606 synchronized (eventQueue) {
607 if (eventQueue.size() > 0) {
608 synchronized (queue) {
609 queue.addAll(eventQueue);
610 }
611 eventQueue.clear();
612 }
613 }
614 }
615
616 /**
617 * Restore terminal to normal state.
618 */
619 public void closeTerminal() {
620 shutdown();
621 }
622
623 /**
624 * Set listener to a different Object.
625 *
626 * @param listener the new listening object that run() wakes up on new
627 * input
628 */
629 public void setListener(final Object listener) {
630 this.listener = listener;
631 }
632
633 /**
634 * Reload options from System properties.
635 */
636 public void reloadOptions() {
637 // Figure out my cursor style.
638 String cursorStyleString = System.getProperty(
639 "jexer.Swing.cursorStyle", "underline").toLowerCase();
640 if (cursorStyleString.equals("underline")) {
641 cursorStyle = CursorStyle.UNDERLINE;
642 } else if (cursorStyleString.equals("outline")) {
643 cursorStyle = CursorStyle.OUTLINE;
644 } else if (cursorStyleString.equals("block")) {
645 cursorStyle = CursorStyle.BLOCK;
646 }
647
648 // Pull the system property for triple buffering.
649 if (System.getProperty("jexer.Swing.tripleBuffer",
650 "true").equals("true")
651 ) {
652 SwingComponent.tripleBuffer = true;
653 } else {
654 SwingComponent.tripleBuffer = false;
655 }
656 }
657
658 // ------------------------------------------------------------------------
659 // SwingTerminal ----------------------------------------------------------
660 // ------------------------------------------------------------------------
661
662 /**
663 * Get the width of a character cell in pixels.
664 *
665 * @return the width in pixels of a character cell
666 */
667 public int getTextWidth() {
668 return textWidth;
669 }
670
671 /**
672 * Get the height of a character cell in pixels.
673 *
674 * @return the height in pixels of a character cell
675 */
676 public int getTextHeight() {
677 return textHeight;
678 }
679
680 /**
681 * Setup Swing colors to match DOS color palette.
682 */
683 private static void setDOSColors() {
684 if (dosColors) {
685 return;
686 }
687 MYBLACK = new Color(0x00, 0x00, 0x00);
688 MYRED = new Color(0xa8, 0x00, 0x00);
689 MYGREEN = new Color(0x00, 0xa8, 0x00);
690 MYYELLOW = new Color(0xa8, 0x54, 0x00);
691 MYBLUE = new Color(0x00, 0x00, 0xa8);
692 MYMAGENTA = new Color(0xa8, 0x00, 0xa8);
693 MYCYAN = new Color(0x00, 0xa8, 0xa8);
694 MYWHITE = new Color(0xa8, 0xa8, 0xa8);
695 MYBOLD_BLACK = new Color(0x54, 0x54, 0x54);
696 MYBOLD_RED = new Color(0xfc, 0x54, 0x54);
697 MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54);
698 MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54);
699 MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc);
700 MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc);
701 MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc);
702 MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc);
703
704 dosColors = true;
705 }
706
707 /**
708 * Get the number of millis to wait before switching the blink from
709 * visible to invisible.
710 *
711 * @return the number of milli to wait before switching the blink from
712 * visible to invisible
713 */
714 public long getBlinkMillis() {
715 return blinkMillis;
716 }
717
718 /**
719 * Get the font size in points.
720 *
721 * @return font size in points
722 */
723 public int getFontSize() {
724 return fontSize;
725 }
726
727 /**
728 * Set the font size in points.
729 *
730 * @param fontSize font size in points
731 */
732 public void setFontSize(final int fontSize) {
733 this.fontSize = fontSize;
734 Font newFont = font.deriveFont((float) fontSize);
735 setFont(newFont);
736 }
737
738 /**
739 * Set to a new font, and resize the screen to match its dimensions.
740 *
741 * @param font the new font
742 */
743 public void setFont(final Font font) {
744 synchronized (this) {
745 this.font = font;
746 getFontDimensions();
747 swing.setFont(font);
748 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
749 glyphCache = new HashMap<Cell, BufferedImage>();
750 resizeToScreen();
751 }
752 }
753
754 /**
755 * Get the font this screen was last set to.
756 *
757 * @return the font
758 */
759 public Font getFont() {
760 return font;
761 }
762
763 /**
764 * Set the font to Terminus, the best all-around font for both CP437 and
765 * ISO8859-1.
766 */
767 public void setDefaultFont() {
768 try {
769 ClassLoader loader = Thread.currentThread().getContextClassLoader();
770 InputStream in = loader.getResourceAsStream(FONTFILE);
771 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
772 Font terminus = terminusRoot.deriveFont(Font.PLAIN, fontSize);
773 font = terminus;
774 } catch (java.awt.FontFormatException e) {
775 e.printStackTrace();
776 font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
777 } catch (java.io.IOException e) {
778 e.printStackTrace();
779 font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
780 }
781
782 setFont(font);
783 }
784
785 /**
786 * Get the X text adjustment.
787 *
788 * @return X text adjustment
789 */
790 public int getTextAdjustX() {
791 return textAdjustX;
792 }
793
794 /**
795 * Set the X text adjustment.
796 *
797 * @param textAdjustX the X text adjustment
798 */
799 public void setTextAdjustX(final int textAdjustX) {
800 synchronized (this) {
801 this.textAdjustX = textAdjustX;
802 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
803 glyphCache = new HashMap<Cell, BufferedImage>();
804 clearPhysical();
805 }
806 }
807
808 /**
809 * Get the Y text adjustment.
810 *
811 * @return Y text adjustment
812 */
813 public int getTextAdjustY() {
814 return textAdjustY;
815 }
816
817 /**
818 * Set the Y text adjustment.
819 *
820 * @param textAdjustY the Y text adjustment
821 */
822 public void setTextAdjustY(final int textAdjustY) {
823 synchronized (this) {
824 this.textAdjustY = textAdjustY;
825 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
826 glyphCache = new HashMap<Cell, BufferedImage>();
827 clearPhysical();
828 }
829 }
830
831 /**
832 * Get the height text adjustment.
833 *
834 * @return height text adjustment
835 */
836 public int getTextAdjustHeight() {
837 return textAdjustHeight;
838 }
839
840 /**
841 * Set the height text adjustment.
842 *
843 * @param textAdjustHeight the height text adjustment
844 */
845 public void setTextAdjustHeight(final int textAdjustHeight) {
846 synchronized (this) {
847 this.textAdjustHeight = textAdjustHeight;
848 textHeight = fontTextHeight + textAdjustHeight;
849 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
850 glyphCache = new HashMap<Cell, BufferedImage>();
851 clearPhysical();
852 }
853 }
854
855 /**
856 * Get the width text adjustment.
857 *
858 * @return width text adjustment
859 */
860 public int getTextAdjustWidth() {
861 return textAdjustWidth;
862 }
863
864 /**
865 * Set the width text adjustment.
866 *
867 * @param textAdjustWidth the width text adjustment
868 */
869 public void setTextAdjustWidth(final int textAdjustWidth) {
870 synchronized (this) {
871 this.textAdjustWidth = textAdjustWidth;
872 textWidth = fontTextWidth + textAdjustWidth;
873 glyphCacheBlink = new HashMap<Cell, BufferedImage>();
874 glyphCache = new HashMap<Cell, BufferedImage>();
875 clearPhysical();
876 }
877 }
878
879 /**
880 * Convert a CellAttributes foreground color to an Swing Color.
881 *
882 * @param attr the text attributes
883 * @return the Swing Color
884 */
885 private Color attrToForegroundColor(final CellAttributes attr) {
886 int rgb = attr.getForeColorRGB();
887 if (rgb >= 0) {
888 int red = (rgb >> 16) & 0xFF;
889 int green = (rgb >> 8) & 0xFF;
890 int blue = rgb & 0xFF;
891
892 return new Color(red, green, blue);
893 }
894
895 if (attr.isBold()) {
896 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
897 return MYBOLD_BLACK;
898 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
899 return MYBOLD_RED;
900 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
901 return MYBOLD_BLUE;
902 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
903 return MYBOLD_GREEN;
904 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
905 return MYBOLD_YELLOW;
906 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
907 return MYBOLD_CYAN;
908 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
909 return MYBOLD_MAGENTA;
910 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
911 return MYBOLD_WHITE;
912 }
913 } else {
914 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
915 return MYBLACK;
916 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
917 return MYRED;
918 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
919 return MYBLUE;
920 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
921 return MYGREEN;
922 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
923 return MYYELLOW;
924 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
925 return MYCYAN;
926 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
927 return MYMAGENTA;
928 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
929 return MYWHITE;
930 }
931 }
932 throw new IllegalArgumentException("Invalid color: " +
933 attr.getForeColor().getValue());
934 }
935
936 /**
937 * Convert a CellAttributes background color to an Swing Color.
938 *
939 * @param attr the text attributes
940 * @return the Swing Color
941 */
942 private Color attrToBackgroundColor(final CellAttributes attr) {
943 int rgb = attr.getBackColorRGB();
944 if (rgb >= 0) {
945 int red = (rgb >> 16) & 0xFF;
946 int green = (rgb >> 8) & 0xFF;
947 int blue = rgb & 0xFF;
948
949 return new Color(red, green, blue);
950 }
951
952 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
953 return MYBLACK;
954 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
955 return MYRED;
956 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
957 return MYBLUE;
958 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
959 return MYGREEN;
960 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
961 return MYYELLOW;
962 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
963 return MYCYAN;
964 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
965 return MYMAGENTA;
966 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
967 return MYWHITE;
968 }
969 throw new IllegalArgumentException("Invalid color: " +
970 attr.getBackColor().getValue());
971 }
972
973 /**
974 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
975 * textAdjustWidth should be, based on the location of a vertical bar and
976 * a horizontal bar.
977 */
978 private void getFontAdjustments() {
979 BufferedImage image = null;
980
981 // What SHOULD happen is that the topmost/leftmost white pixel is at
982 // position (gr2x, gr2y). But it might also be off by a pixel in
983 // either direction.
984
985 Graphics2D gr2 = null;
986 int gr2x = 3;
987 int gr2y = 3;
988 image = new BufferedImage(fontTextWidth * 2, fontTextHeight * 2,
989 BufferedImage.TYPE_INT_ARGB);
990
991 gr2 = image.createGraphics();
992 gr2.setFont(swing.getFont());
993 gr2.setColor(java.awt.Color.BLACK);
994 gr2.fillRect(0, 0, fontTextWidth * 2, fontTextHeight * 2);
995 gr2.setColor(java.awt.Color.WHITE);
996 char [] chars = new char[1];
997 chars[0] = jexer.bits.GraphicsChars.SINGLE_BAR;
998 gr2.drawChars(chars, 0, 1, gr2x, gr2y + fontTextHeight - maxDescent);
999 chars[0] = jexer.bits.GraphicsChars.VERTICAL_BAR;
1000 gr2.drawChars(chars, 0, 1, gr2x, gr2y + fontTextHeight - maxDescent);
1001 gr2.dispose();
1002
1003 int top = fontTextHeight * 2;
1004 int bottom = -1;
1005 int left = fontTextWidth * 2;
1006 int right = -1;
1007 textAdjustX = 0;
1008 textAdjustY = 0;
1009 textAdjustHeight = 0;
1010 textAdjustWidth = 0;
1011
1012 for (int x = 0; x < fontTextWidth * 2; x++) {
1013 for (int y = 0; y < fontTextHeight * 2; y++) {
1014
1015 /*
1016 System.err.println("H X: " + x + " Y: " + y + " " +
1017 image.getRGB(x, y));
1018 */
1019
1020 if ((image.getRGB(x, y) & 0xFFFFFF) != 0) {
1021 // Pixel is present.
1022 if (y < top) {
1023 top = y;
1024 }
1025 if (y > bottom) {
1026 bottom = y;
1027 }
1028 if (x < left) {
1029 left = x;
1030 }
1031 if (x > right) {
1032 right = x;
1033 }
1034 }
1035 }
1036 }
1037 if (left < right) {
1038 textAdjustX = (gr2x - left);
1039 textAdjustWidth = fontTextWidth - (right - left + 1);
1040 }
1041 if (top < bottom) {
1042 textAdjustY = (gr2y - top);
1043 textAdjustHeight = fontTextHeight - (bottom - top + 1);
1044 }
1045 // System.err.println("top " + top + " bottom " + bottom);
1046 // System.err.println("left " + left + " right " + right);
1047
1048 // Special case: do not believe fonts that claim to be wider than
1049 // they are tall.
1050 if (fontTextWidth >= fontTextHeight) {
1051 textAdjustX = 0;
1052 textAdjustWidth = 0;
1053 fontTextWidth = fontTextHeight / 2;
1054 }
1055 }
1056
1057 /**
1058 * Figure out my font dimensions. This code path works OK for the JFrame
1059 * case, and can be called immediately after JFrame creation.
1060 */
1061 private void getFontDimensions() {
1062 swing.setFont(font);
1063 Graphics gr = swing.getGraphics();
1064 if (gr == null) {
1065 return;
1066 }
1067 getFontDimensions(gr);
1068 }
1069
1070 /**
1071 * Figure out my font dimensions. This code path is needed to lazy-load
1072 * the information inside paint().
1073 *
1074 * @param gr Graphics object to use
1075 */
1076 private void getFontDimensions(final Graphics gr) {
1077 swing.setFont(font);
1078 FontMetrics fm = gr.getFontMetrics();
1079 maxDescent = fm.getMaxDescent();
1080 Rectangle2D bounds = fm.getMaxCharBounds(gr);
1081 int leading = fm.getLeading();
1082 fontTextWidth = (int)Math.round(bounds.getWidth());
1083 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1084
1085 // This produces the same number, but works better for ugly
1086 // monospace.
1087 fontTextHeight = fm.getMaxAscent() + maxDescent - leading;
1088
1089 getFontAdjustments();
1090 textHeight = fontTextHeight + textAdjustHeight;
1091 textWidth = fontTextWidth + textAdjustWidth;
1092
1093 if (sessionInfo != null) {
1094 sessionInfo.setTextCellDimensions(textWidth, textHeight);
1095 }
1096 gotFontDimensions = true;
1097 }
1098
1099 /**
1100 * Resize the physical screen to match the logical screen dimensions.
1101 */
1102 @Override
1103 public void resizeToScreen() {
1104 swing.setDimensions(textWidth * width, textHeight * height);
1105 clearPhysical();
1106 }
1107
1108 /**
1109 * Draw one cell's image to the screen.
1110 *
1111 * @param gr the Swing Graphics context
1112 * @param cell the Cell to draw
1113 * @param xPixel the x-coordinate to render to. 0 means the
1114 * left-most pixel column.
1115 * @param yPixel the y-coordinate to render to. 0 means the top-most
1116 * pixel row.
1117 */
1118 private void drawImage(final Graphics gr, final Cell cell,
1119 final int xPixel, final int yPixel) {
1120
1121 /*
1122 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1123 " " + cell);
1124 */
1125
1126 // Draw the background rectangle, then the foreground character.
1127 assert (cell.isImage());
1128 gr.setColor(cell.getBackground());
1129 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
1130
1131 BufferedImage image = cell.getImage();
1132 if (image != null) {
1133 if (swing.getFrame() != null) {
1134 gr.drawImage(image, xPixel, yPixel, swing.getFrame());
1135 } else {
1136 gr.drawImage(image, xPixel, yPixel, swing.getComponent());
1137 }
1138 return;
1139 }
1140 }
1141
1142 /**
1143 * Draw one glyph to the screen.
1144 *
1145 * @param gr the Swing Graphics context
1146 * @param cell the Cell to draw
1147 * @param xPixel the x-coordinate to render to. 0 means the
1148 * left-most pixel column.
1149 * @param yPixel the y-coordinate to render to. 0 means the top-most
1150 * pixel row.
1151 */
1152 private void drawGlyph(final Graphics gr, final Cell cell,
1153 final int xPixel, final int yPixel) {
1154
1155 /*
1156 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1157 " " + cell);
1158 */
1159
1160 BufferedImage image = null;
1161 if (cell.isBlink() && !cursorBlinkVisible) {
1162 image = glyphCacheBlink.get(cell);
1163 } else {
1164 image = glyphCache.get(cell);
1165 }
1166 if (image != null) {
1167 if (swing.getFrame() != null) {
1168 gr.drawImage(image, xPixel, yPixel, swing.getFrame());
1169 } else {
1170 gr.drawImage(image, xPixel, yPixel, swing.getComponent());
1171 }
1172 return;
1173 }
1174
1175 // Generate glyph and draw it.
1176 Graphics2D gr2 = null;
1177 int gr2x = xPixel;
1178 int gr2y = yPixel;
1179 if ((SwingComponent.tripleBuffer) && (swing.getFrame() != null)) {
1180 image = new BufferedImage(textWidth, textHeight,
1181 BufferedImage.TYPE_INT_ARGB);
1182 gr2 = image.createGraphics();
1183 gr2.setFont(swing.getFont());
1184 gr2x = 0;
1185 gr2y = 0;
1186 } else {
1187 gr2 = (Graphics2D) gr;
1188 }
1189
1190 Cell cellColor = new Cell();
1191 cellColor.setTo(cell);
1192
1193 // Check for reverse
1194 if (cell.isReverse()) {
1195 cellColor.setForeColor(cell.getBackColor());
1196 cellColor.setBackColor(cell.getForeColor());
1197 }
1198
1199 // Draw the background rectangle, then the foreground character.
1200 gr2.setColor(attrToBackgroundColor(cellColor));
1201 gr2.fillRect(gr2x, gr2y, textWidth, textHeight);
1202
1203 // Handle blink and underline
1204 if (!cell.isBlink()
1205 || (cell.isBlink() && cursorBlinkVisible)
1206 ) {
1207 gr2.setColor(attrToForegroundColor(cellColor));
1208 char [] chars = new char[1];
1209 chars[0] = cell.getChar();
1210 gr2.drawChars(chars, 0, 1, gr2x + textAdjustX,
1211 gr2y + textHeight - maxDescent + textAdjustY);
1212
1213 if (cell.isUnderline()) {
1214 gr2.fillRect(gr2x, gr2y + textHeight - 2, textWidth, 2);
1215 }
1216 }
1217
1218 if ((SwingComponent.tripleBuffer) && (swing.getFrame() != null)) {
1219 gr2.dispose();
1220
1221 // We need a new key that will not be mutated by
1222 // invertCell().
1223 Cell key = new Cell();
1224 key.setTo(cell);
1225 if (cell.isBlink() && !cursorBlinkVisible) {
1226 glyphCacheBlink.put(key, image);
1227 } else {
1228 glyphCache.put(key, image);
1229 }
1230
1231 if (swing.getFrame() != null) {
1232 gr.drawImage(image, xPixel, yPixel, swing.getFrame());
1233 } else {
1234 gr.drawImage(image, xPixel, yPixel, swing.getComponent());
1235 }
1236 }
1237
1238 }
1239
1240 /**
1241 * Check if the cursor is visible, and if so draw it.
1242 *
1243 * @param gr the Swing Graphics context
1244 */
1245 private void drawCursor(final Graphics gr) {
1246
1247 if (cursorVisible
1248 && (cursorY >= 0)
1249 && (cursorX >= 0)
1250 && (cursorY <= height - 1)
1251 && (cursorX <= width - 1)
1252 && cursorBlinkVisible
1253 ) {
1254 int xPixel = cursorX * textWidth + left;
1255 int yPixel = cursorY * textHeight + top;
1256 Cell lCell = logical[cursorX][cursorY];
1257 gr.setColor(attrToForegroundColor(lCell));
1258 switch (cursorStyle) {
1259 default:
1260 // Fall through...
1261 case UNDERLINE:
1262 gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2);
1263 break;
1264 case BLOCK:
1265 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
1266 break;
1267 case OUTLINE:
1268 gr.drawRect(xPixel, yPixel, textWidth - 1, textHeight - 1);
1269 break;
1270 }
1271 }
1272 }
1273
1274 /**
1275 * Reset the blink timer.
1276 */
1277 private void resetBlinkTimer() {
1278 lastBlinkTime = System.currentTimeMillis();
1279 cursorBlinkVisible = true;
1280 }
1281
1282 /**
1283 * Paint redraws the whole screen.
1284 *
1285 * @param gr the Swing Graphics context
1286 */
1287 public void paint(final Graphics gr) {
1288
1289 if (gotFontDimensions == false) {
1290 // Lazy-load the text width/height
1291 getFontDimensions(gr);
1292 /*
1293 System.err.println("textWidth " + textWidth +
1294 " textHeight " + textHeight);
1295 System.err.println("FONT: " + swing.getFont() + " font " + font);
1296 */
1297 }
1298
1299 if ((swing.getFrame() != null)
1300 && (swing.getBufferStrategy() != null)
1301 && (SwingUtilities.isEventDispatchThread())
1302 ) {
1303 // System.err.println("paint(), skip first paint on swing thread");
1304 return;
1305 }
1306
1307 int xCellMin = 0;
1308 int xCellMax = width;
1309 int yCellMin = 0;
1310 int yCellMax = height;
1311
1312 Rectangle bounds = gr.getClipBounds();
1313 if (bounds != null) {
1314 // Only update what is in the bounds
1315 xCellMin = textColumn(bounds.x);
1316 xCellMax = textColumn(bounds.x + bounds.width);
1317 if (xCellMax > width) {
1318 xCellMax = width;
1319 }
1320 if (xCellMin >= xCellMax) {
1321 xCellMin = xCellMax - 2;
1322 }
1323 if (xCellMin < 0) {
1324 xCellMin = 0;
1325 }
1326 yCellMin = textRow(bounds.y);
1327 yCellMax = textRow(bounds.y + bounds.height);
1328 if (yCellMax > height) {
1329 yCellMax = height;
1330 }
1331 if (yCellMin >= yCellMax) {
1332 yCellMin = yCellMax - 2;
1333 }
1334 if (yCellMin < 0) {
1335 yCellMin = 0;
1336 }
1337 } else {
1338 // We need a total repaint
1339 reallyCleared = true;
1340 }
1341
1342 // Prevent updates to the screen's data from the TApplication
1343 // threads.
1344 synchronized (this) {
1345
1346 /*
1347 System.err.printf("bounds %s X %d %d Y %d %d\n",
1348 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1349 */
1350
1351 for (int y = yCellMin; y < yCellMax; y++) {
1352 for (int x = xCellMin; x < xCellMax; x++) {
1353
1354 int xPixel = x * textWidth + left;
1355 int yPixel = y * textHeight + top;
1356
1357 Cell lCell = logical[x][y];
1358 Cell pCell = physical[x][y];
1359
1360 if (!lCell.equals(pCell)
1361 || lCell.isBlink()
1362 || reallyCleared
1363 || (swing.getFrame() == null)) {
1364
1365 if (lCell.isImage()) {
1366 drawImage(gr, lCell, xPixel, yPixel);
1367 } else {
1368 drawGlyph(gr, lCell, xPixel, yPixel);
1369 }
1370
1371 // Physical is always updated
1372 physical[x][y].setTo(lCell);
1373 }
1374 }
1375 }
1376 drawCursor(gr);
1377
1378 reallyCleared = false;
1379 } // synchronized (this)
1380 }
1381
1382 /**
1383 * Restore terminal to normal state.
1384 */
1385 public void shutdown() {
1386 swing.dispose();
1387 }
1388
1389 /**
1390 * Push the logical screen to the physical device.
1391 */
1392 private void drawToSwing() {
1393
1394 /*
1395 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1396 reallyCleared, dirty);
1397 */
1398
1399 // If reallyCleared is set, we have to draw everything.
1400 if ((swing.getFrame() != null)
1401 && (swing.getBufferStrategy() != null)
1402 && (reallyCleared == true)
1403 ) {
1404 // Triple-buffering: we have to redraw everything on this thread.
1405 Graphics gr = swing.getBufferStrategy().getDrawGraphics();
1406 swing.paint(gr);
1407 gr.dispose();
1408 swing.getBufferStrategy().show();
1409 Toolkit.getDefaultToolkit().sync();
1410 return;
1411 } else if (((swing.getFrame() != null)
1412 && (swing.getBufferStrategy() == null))
1413 || (reallyCleared == true)
1414 ) {
1415 // Repaint everything on the Swing thread.
1416 // System.err.println("REPAINT ALL");
1417 swing.repaint();
1418 return;
1419 }
1420
1421 if ((swing.getFrame() != null) && (swing.getBufferStrategy() != null)) {
1422 Graphics gr = swing.getBufferStrategy().getDrawGraphics();
1423
1424 synchronized (this) {
1425 for (int y = 0; y < height; y++) {
1426 for (int x = 0; x < width; x++) {
1427 Cell lCell = logical[x][y];
1428 Cell pCell = physical[x][y];
1429
1430 int xPixel = x * textWidth + left;
1431 int yPixel = y * textHeight + top;
1432
1433 if (!lCell.equals(pCell)
1434 || ((x == cursorX)
1435 && (y == cursorY)
1436 && cursorVisible)
1437 || (lCell.isBlink())
1438 ) {
1439 if (lCell.isImage()) {
1440 drawImage(gr, lCell, xPixel, yPixel);
1441 } else {
1442 drawGlyph(gr, lCell, xPixel, yPixel);
1443 }
1444 physical[x][y].setTo(lCell);
1445 }
1446 }
1447 }
1448 drawCursor(gr);
1449 } // synchronized (this)
1450
1451 gr.dispose();
1452 swing.getBufferStrategy().show();
1453 Toolkit.getDefaultToolkit().sync();
1454 return;
1455 }
1456
1457 // Swing thread version: request a repaint, but limit it to the area
1458 // that has changed.
1459
1460 // Find the minimum-size damaged region.
1461 int xMin = swing.getWidth();
1462 int xMax = 0;
1463 int yMin = swing.getHeight();
1464 int yMax = 0;
1465
1466 synchronized (this) {
1467 for (int y = 0; y < height; y++) {
1468 for (int x = 0; x < width; x++) {
1469 Cell lCell = logical[x][y];
1470 Cell pCell = physical[x][y];
1471
1472 int xPixel = x * textWidth + left;
1473 int yPixel = y * textHeight + top;
1474
1475 if (!lCell.equals(pCell)
1476 || ((x == cursorX)
1477 && (y == cursorY)
1478 && cursorVisible)
1479 || lCell.isBlink()
1480 ) {
1481 if (xPixel < xMin) {
1482 xMin = xPixel;
1483 }
1484 if (xPixel + textWidth > xMax) {
1485 xMax = xPixel + textWidth;
1486 }
1487 if (yPixel < yMin) {
1488 yMin = yPixel;
1489 }
1490 if (yPixel + textHeight > yMax) {
1491 yMax = yPixel + textHeight;
1492 }
1493 }
1494 }
1495 }
1496 }
1497 if (xMin + textWidth >= xMax) {
1498 xMax += textWidth;
1499 }
1500 if (yMin + textHeight >= yMax) {
1501 yMax += textHeight;
1502 }
1503
1504 // Repaint the desired area
1505 /*
1506 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1507 yMin, yMax);
1508 */
1509
1510 if ((swing.getFrame() != null) && (swing.getBufferStrategy() != null)) {
1511 // This path should never be taken, but is left here for
1512 // completeness.
1513 Graphics gr = swing.getBufferStrategy().getDrawGraphics();
1514 Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin,
1515 yMax - yMin);
1516 gr.setClip(bounds);
1517 swing.paint(gr);
1518 gr.dispose();
1519 swing.getBufferStrategy().show();
1520 Toolkit.getDefaultToolkit().sync();
1521 } else {
1522 // Repaint on the Swing thread.
1523 swing.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
1524 }
1525 }
1526
1527 /**
1528 * Convert pixel column position to text cell column position.
1529 *
1530 * @param x pixel column position
1531 * @return text cell column position
1532 */
1533 public int textColumn(final int x) {
1534 int column = ((x - left) / textWidth);
1535 if (column < 0) {
1536 column = 0;
1537 }
1538 if (column > width - 1) {
1539 column = width - 1;
1540 }
1541 return column;
1542 }
1543
1544 /**
1545 * Convert pixel row position to text cell row position.
1546 *
1547 * @param y pixel row position
1548 * @return text cell row position
1549 */
1550 public int textRow(final int y) {
1551 int row = ((y - top) / textHeight);
1552 if (row < 0) {
1553 row = 0;
1554 }
1555 if (row > height - 1) {
1556 row = height - 1;
1557 }
1558 return row;
1559 }
1560
1561 /**
1562 * Getter for sessionInfo.
1563 *
1564 * @return the SessionInfo
1565 */
1566 public SessionInfo getSessionInfo() {
1567 return sessionInfo;
1568 }
1569
1570 /**
1571 * Getter for the underlying Swing component.
1572 *
1573 * @return the SwingComponent
1574 */
1575 public SwingComponent getSwingComponent() {
1576 return swing;
1577 }
1578
1579 // ------------------------------------------------------------------------
1580 // KeyListener ------------------------------------------------------------
1581 // ------------------------------------------------------------------------
1582
1583 /**
1584 * Pass Swing keystrokes into the event queue.
1585 *
1586 * @param key keystroke received
1587 */
1588 public void keyReleased(final KeyEvent key) {
1589 // Ignore release events
1590 }
1591
1592 /**
1593 * Pass Swing keystrokes into the event queue.
1594 *
1595 * @param key keystroke received
1596 */
1597 public void keyTyped(final KeyEvent key) {
1598 // Ignore typed events
1599 }
1600
1601 /**
1602 * Pass Swing keystrokes into the event queue.
1603 *
1604 * @param key keystroke received
1605 */
1606 public void keyPressed(final KeyEvent key) {
1607 boolean alt = false;
1608 boolean shift = false;
1609 boolean ctrl = false;
1610 char ch = ' ';
1611 boolean isKey = false;
1612 if (key.isActionKey()) {
1613 isKey = true;
1614 } else {
1615 ch = key.getKeyChar();
1616 }
1617 alt = key.isAltDown();
1618 ctrl = key.isControlDown();
1619 shift = key.isShiftDown();
1620
1621 /*
1622 System.err.printf("Swing Key: %s\n", key);
1623 System.err.printf(" isKey: %s\n", isKey);
1624 System.err.printf(" alt: %s\n", alt);
1625 System.err.printf(" ctrl: %s\n", ctrl);
1626 System.err.printf(" shift: %s\n", shift);
1627 System.err.printf(" ch: %s\n", ch);
1628 */
1629
1630 // Special case: not return the bare modifier presses
1631 switch (key.getKeyCode()) {
1632 case KeyEvent.VK_ALT:
1633 return;
1634 case KeyEvent.VK_ALT_GRAPH:
1635 return;
1636 case KeyEvent.VK_CONTROL:
1637 return;
1638 case KeyEvent.VK_SHIFT:
1639 return;
1640 case KeyEvent.VK_META:
1641 return;
1642 default:
1643 break;
1644 }
1645
1646 TKeypress keypress = null;
1647 if (isKey) {
1648 switch (key.getKeyCode()) {
1649 case KeyEvent.VK_F1:
1650 keypress = new TKeypress(true, TKeypress.F1, ' ',
1651 alt, ctrl, shift);
1652 break;
1653 case KeyEvent.VK_F2:
1654 keypress = new TKeypress(true, TKeypress.F2, ' ',
1655 alt, ctrl, shift);
1656 break;
1657 case KeyEvent.VK_F3:
1658 keypress = new TKeypress(true, TKeypress.F3, ' ',
1659 alt, ctrl, shift);
1660 break;
1661 case KeyEvent.VK_F4:
1662 keypress = new TKeypress(true, TKeypress.F4, ' ',
1663 alt, ctrl, shift);
1664 break;
1665 case KeyEvent.VK_F5:
1666 keypress = new TKeypress(true, TKeypress.F5, ' ',
1667 alt, ctrl, shift);
1668 break;
1669 case KeyEvent.VK_F6:
1670 keypress = new TKeypress(true, TKeypress.F6, ' ',
1671 alt, ctrl, shift);
1672 break;
1673 case KeyEvent.VK_F7:
1674 keypress = new TKeypress(true, TKeypress.F7, ' ',
1675 alt, ctrl, shift);
1676 break;
1677 case KeyEvent.VK_F8:
1678 keypress = new TKeypress(true, TKeypress.F8, ' ',
1679 alt, ctrl, shift);
1680 break;
1681 case KeyEvent.VK_F9:
1682 keypress = new TKeypress(true, TKeypress.F9, ' ',
1683 alt, ctrl, shift);
1684 break;
1685 case KeyEvent.VK_F10:
1686 keypress = new TKeypress(true, TKeypress.F10, ' ',
1687 alt, ctrl, shift);
1688 break;
1689 case KeyEvent.VK_F11:
1690 keypress = new TKeypress(true, TKeypress.F11, ' ',
1691 alt, ctrl, shift);
1692 break;
1693 case KeyEvent.VK_F12:
1694 keypress = new TKeypress(true, TKeypress.F12, ' ',
1695 alt, ctrl, shift);
1696 break;
1697 case KeyEvent.VK_HOME:
1698 keypress = new TKeypress(true, TKeypress.HOME, ' ',
1699 alt, ctrl, shift);
1700 break;
1701 case KeyEvent.VK_END:
1702 keypress = new TKeypress(true, TKeypress.END, ' ',
1703 alt, ctrl, shift);
1704 break;
1705 case KeyEvent.VK_PAGE_UP:
1706 keypress = new TKeypress(true, TKeypress.PGUP, ' ',
1707 alt, ctrl, shift);
1708 break;
1709 case KeyEvent.VK_PAGE_DOWN:
1710 keypress = new TKeypress(true, TKeypress.PGDN, ' ',
1711 alt, ctrl, shift);
1712 break;
1713 case KeyEvent.VK_INSERT:
1714 keypress = new TKeypress(true, TKeypress.INS, ' ',
1715 alt, ctrl, shift);
1716 break;
1717 case KeyEvent.VK_DELETE:
1718 keypress = new TKeypress(true, TKeypress.DEL, ' ',
1719 alt, ctrl, shift);
1720 break;
1721 case KeyEvent.VK_RIGHT:
1722 keypress = new TKeypress(true, TKeypress.RIGHT, ' ',
1723 alt, ctrl, shift);
1724 break;
1725 case KeyEvent.VK_LEFT:
1726 keypress = new TKeypress(true, TKeypress.LEFT, ' ',
1727 alt, ctrl, shift);
1728 break;
1729 case KeyEvent.VK_UP:
1730 keypress = new TKeypress(true, TKeypress.UP, ' ',
1731 alt, ctrl, shift);
1732 break;
1733 case KeyEvent.VK_DOWN:
1734 keypress = new TKeypress(true, TKeypress.DOWN, ' ',
1735 alt, ctrl, shift);
1736 break;
1737 case KeyEvent.VK_TAB:
1738 // Special case: distinguish TAB vs BTAB
1739 if (shift) {
1740 keypress = kbShiftTab;
1741 } else {
1742 keypress = kbTab;
1743 }
1744 break;
1745 case KeyEvent.VK_ENTER:
1746 keypress = new TKeypress(true, TKeypress.ENTER, ' ',
1747 alt, ctrl, shift);
1748 break;
1749 case KeyEvent.VK_ESCAPE:
1750 keypress = new TKeypress(true, TKeypress.ESC, ' ',
1751 alt, ctrl, shift);
1752 break;
1753 case KeyEvent.VK_BACK_SPACE:
1754 keypress = kbBackspace;
1755 break;
1756 default:
1757 // Unsupported, ignore
1758 return;
1759 }
1760 }
1761
1762 if (keypress == null) {
1763 switch (ch) {
1764 case 0x08:
1765 // Disambiguate ^H from Backspace.
1766 if (KeyEvent.getKeyText(key.getKeyCode()).equals("H")) {
1767 // This is ^H.
1768 keypress = kbBackspace;
1769 } else {
1770 // We are emulating Xterm here, where the backspace key
1771 // on the keyboard returns ^?.
1772 keypress = kbBackspaceDel;
1773 }
1774 break;
1775 case 0x0A:
1776 keypress = kbEnter;
1777 break;
1778 case 0x1B:
1779 keypress = kbEsc;
1780 break;
1781 case 0x0D:
1782 keypress = kbEnter;
1783 break;
1784 case 0x09:
1785 if (shift) {
1786 keypress = kbShiftTab;
1787 } else {
1788 keypress = kbTab;
1789 }
1790 break;
1791 case 0x7F:
1792 keypress = kbDel;
1793 break;
1794 default:
1795 if (!alt && ctrl && !shift) {
1796 ch = KeyEvent.getKeyText(key.getKeyCode()).charAt(0);
1797 }
1798 // Not a special key, put it together
1799 keypress = new TKeypress(false, 0, ch, alt, ctrl, shift);
1800 }
1801 }
1802
1803 // Save it and we are done.
1804 synchronized (eventQueue) {
1805 eventQueue.add(new TKeypressEvent(keypress));
1806 resetBlinkTimer();
1807 }
1808 if (listener != null) {
1809 synchronized (listener) {
1810 listener.notifyAll();
1811 }
1812 }
1813 }
1814
1815 // ------------------------------------------------------------------------
1816 // WindowListener ---------------------------------------------------------
1817 // ------------------------------------------------------------------------
1818
1819 /**
1820 * Pass window events into the event queue.
1821 *
1822 * @param event window event received
1823 */
1824 public void windowActivated(final WindowEvent event) {
1825 // Force a total repaint
1826 synchronized (this) {
1827 clearPhysical();
1828 }
1829 }
1830
1831 /**
1832 * Pass window events into the event queue.
1833 *
1834 * @param event window event received
1835 */
1836 public void windowClosed(final WindowEvent event) {
1837 // Ignore
1838 }
1839
1840 /**
1841 * Pass window events into the event queue.
1842 *
1843 * @param event window event received
1844 */
1845 public void windowClosing(final WindowEvent event) {
1846 // Drop a cmAbort and walk away
1847 synchronized (eventQueue) {
1848 eventQueue.add(new TCommandEvent(cmAbort));
1849 resetBlinkTimer();
1850 }
1851 if (listener != null) {
1852 synchronized (listener) {
1853 listener.notifyAll();
1854 }
1855 }
1856 }
1857
1858 /**
1859 * Pass window events into the event queue.
1860 *
1861 * @param event window event received
1862 */
1863 public void windowDeactivated(final WindowEvent event) {
1864 // Ignore
1865 }
1866
1867 /**
1868 * Pass window events into the event queue.
1869 *
1870 * @param event window event received
1871 */
1872 public void windowDeiconified(final WindowEvent event) {
1873 // Ignore
1874 }
1875
1876 /**
1877 * Pass window events into the event queue.
1878 *
1879 * @param event window event received
1880 */
1881 public void windowIconified(final WindowEvent event) {
1882 // Ignore
1883 }
1884
1885 /**
1886 * Pass window events into the event queue.
1887 *
1888 * @param event window event received
1889 */
1890 public void windowOpened(final WindowEvent event) {
1891 // Ignore
1892 }
1893
1894 // ------------------------------------------------------------------------
1895 // ComponentListener ------------------------------------------------------
1896 // ------------------------------------------------------------------------
1897
1898 /**
1899 * Pass component events into the event queue.
1900 *
1901 * @param event component event received
1902 */
1903 public void componentHidden(final ComponentEvent event) {
1904 // Ignore
1905 }
1906
1907 /**
1908 * Pass component events into the event queue.
1909 *
1910 * @param event component event received
1911 */
1912 public void componentShown(final ComponentEvent event) {
1913 // Ignore
1914 }
1915
1916 /**
1917 * Pass component events into the event queue.
1918 *
1919 * @param event component event received
1920 */
1921 public void componentMoved(final ComponentEvent event) {
1922 // Ignore
1923 }
1924
1925 /**
1926 * Pass component events into the event queue.
1927 *
1928 * @param event component event received
1929 */
1930 public void componentResized(final ComponentEvent event) {
1931 if (gotFontDimensions == false) {
1932 // We are still waiting to get font information. Don't pass a
1933 // resize event up.
1934 // System.err.println("size " + swing.getComponent().getSize());
1935 return;
1936 }
1937
1938 // Drop a new TResizeEvent into the queue
1939 sessionInfo.queryWindowSize();
1940 synchronized (eventQueue) {
1941 TResizeEvent windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN,
1942 sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight());
1943 eventQueue.add(windowResize);
1944 resetBlinkTimer();
1945 /*
1946 System.err.println("Add resize event: " + windowResize.getWidth() +
1947 " x " + windowResize.getHeight());
1948 */
1949 }
1950 if (listener != null) {
1951 synchronized (listener) {
1952 listener.notifyAll();
1953 }
1954 }
1955 }
1956
1957 // ------------------------------------------------------------------------
1958 // MouseMotionListener ----------------------------------------------------
1959 // ------------------------------------------------------------------------
1960
1961 /**
1962 * Pass mouse events into the event queue.
1963 *
1964 * @param mouse mouse event received
1965 */
1966 public void mouseDragged(final MouseEvent mouse) {
1967 int modifiers = mouse.getModifiersEx();
1968 boolean eventMouse1 = false;
1969 boolean eventMouse2 = false;
1970 boolean eventMouse3 = false;
1971 if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
1972 eventMouse1 = true;
1973 }
1974 if ((modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
1975 eventMouse2 = true;
1976 }
1977 if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
1978 eventMouse3 = true;
1979 }
1980 mouse1 = eventMouse1;
1981 mouse2 = eventMouse2;
1982 mouse3 = eventMouse3;
1983 int x = textColumn(mouse.getX());
1984 int y = textRow(mouse.getY());
1985
1986 TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_MOTION,
1987 x, y, x, y, mouse1, mouse2, mouse3, false, false);
1988
1989 synchronized (eventQueue) {
1990 eventQueue.add(mouseEvent);
1991 resetBlinkTimer();
1992 }
1993 if (listener != null) {
1994 synchronized (listener) {
1995 listener.notifyAll();
1996 }
1997 }
1998 }
1999
2000 /**
2001 * Pass mouse events into the event queue.
2002 *
2003 * @param mouse mouse event received
2004 */
2005 public void mouseMoved(final MouseEvent mouse) {
2006 int x = textColumn(mouse.getX());
2007 int y = textRow(mouse.getY());
2008 if ((x == oldMouseX) && (y == oldMouseY)) {
2009 // Bail out, we've moved some pixels but not a whole text cell.
2010 return;
2011 }
2012 oldMouseX = x;
2013 oldMouseY = y;
2014
2015 TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_MOTION,
2016 x, y, x, y, mouse1, mouse2, mouse3, false, false);
2017
2018 synchronized (eventQueue) {
2019 eventQueue.add(mouseEvent);
2020 resetBlinkTimer();
2021 }
2022 if (listener != null) {
2023 synchronized (listener) {
2024 listener.notifyAll();
2025 }
2026 }
2027 }
2028
2029 // ------------------------------------------------------------------------
2030 // MouseListener ----------------------------------------------------------
2031 // ------------------------------------------------------------------------
2032
2033 /**
2034 * Pass mouse events into the event queue.
2035 *
2036 * @param mouse mouse event received
2037 */
2038 public void mouseClicked(final MouseEvent mouse) {
2039 // Ignore
2040 }
2041
2042 /**
2043 * Pass mouse events into the event queue.
2044 *
2045 * @param mouse mouse event received
2046 */
2047 public void mouseEntered(final MouseEvent mouse) {
2048 // Ignore
2049 }
2050
2051 /**
2052 * Pass mouse events into the event queue.
2053 *
2054 * @param mouse mouse event received
2055 */
2056 public void mouseExited(final MouseEvent mouse) {
2057 // Ignore
2058 }
2059
2060 /**
2061 * Pass mouse events into the event queue.
2062 *
2063 * @param mouse mouse event received
2064 */
2065 public void mousePressed(final MouseEvent mouse) {
2066 int modifiers = mouse.getModifiersEx();
2067 boolean eventMouse1 = false;
2068 boolean eventMouse2 = false;
2069 boolean eventMouse3 = false;
2070 if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
2071 eventMouse1 = true;
2072 }
2073 if ((modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
2074 eventMouse2 = true;
2075 }
2076 if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
2077 eventMouse3 = true;
2078 }
2079 mouse1 = eventMouse1;
2080 mouse2 = eventMouse2;
2081 mouse3 = eventMouse3;
2082 int x = textColumn(mouse.getX());
2083 int y = textRow(mouse.getY());
2084
2085 TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_DOWN,
2086 x, y, x, y, mouse1, mouse2, mouse3, false, false);
2087
2088 synchronized (eventQueue) {
2089 eventQueue.add(mouseEvent);
2090 resetBlinkTimer();
2091 }
2092 if (listener != null) {
2093 synchronized (listener) {
2094 listener.notifyAll();
2095 }
2096 }
2097 }
2098
2099 /**
2100 * Pass mouse events into the event queue.
2101 *
2102 * @param mouse mouse event received
2103 */
2104 public void mouseReleased(final MouseEvent mouse) {
2105 int modifiers = mouse.getModifiersEx();
2106 boolean eventMouse1 = false;
2107 boolean eventMouse2 = false;
2108 boolean eventMouse3 = false;
2109 if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
2110 eventMouse1 = true;
2111 }
2112 if ((modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
2113 eventMouse2 = true;
2114 }
2115 if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
2116 eventMouse3 = true;
2117 }
2118 if (mouse1) {
2119 mouse1 = false;
2120 eventMouse1 = true;
2121 }
2122 if (mouse2) {
2123 mouse2 = false;
2124 eventMouse2 = true;
2125 }
2126 if (mouse3) {
2127 mouse3 = false;
2128 eventMouse3 = true;
2129 }
2130 int x = textColumn(mouse.getX());
2131 int y = textRow(mouse.getY());
2132
2133 TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_UP,
2134 x, y, x, y, eventMouse1, eventMouse2, eventMouse3, false, false);
2135
2136 synchronized (eventQueue) {
2137 eventQueue.add(mouseEvent);
2138 resetBlinkTimer();
2139 }
2140 if (listener != null) {
2141 synchronized (listener) {
2142 listener.notifyAll();
2143 }
2144 }
2145 }
2146
2147 // ------------------------------------------------------------------------
2148 // MouseWheelListener -----------------------------------------------------
2149 // ------------------------------------------------------------------------
2150
2151 /**
2152 * Pass mouse events into the event queue.
2153 *
2154 * @param mouse mouse event received
2155 */
2156 public void mouseWheelMoved(final MouseWheelEvent mouse) {
2157 int modifiers = mouse.getModifiersEx();
2158 boolean eventMouse1 = false;
2159 boolean eventMouse2 = false;
2160 boolean eventMouse3 = false;
2161 boolean mouseWheelUp = false;
2162 boolean mouseWheelDown = false;
2163 if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
2164 eventMouse1 = true;
2165 }
2166 if ((modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
2167 eventMouse2 = true;
2168 }
2169 if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
2170 eventMouse3 = true;
2171 }
2172 mouse1 = eventMouse1;
2173 mouse2 = eventMouse2;
2174 mouse3 = eventMouse3;
2175 int x = textColumn(mouse.getX());
2176 int y = textRow(mouse.getY());
2177 if (mouse.getWheelRotation() > 0) {
2178 mouseWheelDown = true;
2179 }
2180 if (mouse.getWheelRotation() < 0) {
2181 mouseWheelUp = true;
2182 }
2183
2184 TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_DOWN,
2185 x, y, x, y, mouse1, mouse2, mouse3, mouseWheelUp, mouseWheelDown);
2186
2187 synchronized (eventQueue) {
2188 eventQueue.add(mouseEvent);
2189 resetBlinkTimer();
2190 }
2191 if (listener != null) {
2192 synchronized (listener) {
2193 listener.notifyAll();
2194 }
2195 }
2196 }
2197
2198 }