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