draw mouse arrows on pane split
[fanfix.git] / src / jexer / TTerminalWindow.java
CommitLineData
daa4106c 1/*
34a42e78
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
34a42e78 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
34a42e78 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
34a42e78 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
34a42e78 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
34a42e78
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
d1115203
KL
31import java.awt.Font;
32import java.awt.FontMetrics;
33import java.awt.Graphics2D;
5fc7bf09 34import java.awt.image.BufferedImage;
d1115203
KL
35
36import java.io.InputStream;
34a42e78 37import java.io.IOException;
55d2b2c2 38import java.lang.reflect.Field;
339652cc 39import java.text.MessageFormat;
a69ed767 40import java.util.ArrayList;
d1115203 41import java.util.HashMap;
34a42e78 42import java.util.List;
bd8d51fa 43import java.util.Map;
339652cc 44import java.util.ResourceBundle;
34a42e78 45
d1115203 46import jexer.backend.ECMA48Terminal;
0d86ab84 47import jexer.backend.GlyphMaker;
d1115203
KL
48import jexer.backend.MultiScreen;
49import jexer.backend.SwingTerminal;
34a42e78
KL
50import jexer.bits.Cell;
51import jexer.bits.CellAttributes;
52import jexer.event.TKeypressEvent;
b2d49e0f 53import jexer.event.TMenuEvent;
34a42e78
KL
54import jexer.event.TMouseEvent;
55import jexer.event.TResizeEvent;
b2d49e0f 56import jexer.menu.TMenu;
34a42e78 57import jexer.tterminal.DisplayLine;
be72cb5c 58import jexer.tterminal.DisplayListener;
34a42e78
KL
59import jexer.tterminal.ECMA48;
60import static jexer.TKeypress.*;
61
62/**
63 * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window.
64 */
4d2c61b4 65public class TTerminalWindow extends TScrollableWindow {
34a42e78 66
339652cc
KL
67 /**
68 * Translated strings.
69 */
70 private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName());
71
615a0d99
KL
72 // ------------------------------------------------------------------------
73 // Variables --------------------------------------------------------------
74 // ------------------------------------------------------------------------
75
34a42e78 76 /**
4d2c61b4 77 * The terminal.
34a42e78 78 */
4d2c61b4 79 private TTerminalWidget terminal;
1d99a38f 80
a69ed767
KL
81 /**
82 * If true, close the window when the shell exits.
83 */
84 private boolean closeOnExit = false;
85
615a0d99
KL
86 // ------------------------------------------------------------------------
87 // Constructors -----------------------------------------------------------
88 // ------------------------------------------------------------------------
34a42e78 89
b2d49e0f
KL
90 /**
91 * Public constructor spawns a custom command line.
92 *
93 * @param application TApplication that manages this window
94 * @param x column relative to parent
95 * @param y row relative to parent
96 * @param commandLine the command line to execute
97 */
98 public TTerminalWindow(final TApplication application, final int x,
99 final int y, final String commandLine) {
100
00691e80 101 this(application, x, y, RESIZABLE, commandLine.split("\\s+"),
a69ed767
KL
102 System.getProperty("jexer.TTerminal.closeOnExit",
103 "false").equals("true"));
104 }
105
106 /**
107 * Public constructor spawns a custom command line.
108 *
109 * @param application TApplication that manages this window
110 * @param x column relative to parent
111 * @param y row relative to parent
112 * @param commandLine the command line to execute
113 * @param closeOnExit if true, close the window when the command exits
114 */
115 public TTerminalWindow(final TApplication application, final int x,
116 final int y, final String commandLine, final boolean closeOnExit) {
117
00691e80 118 this(application, x, y, RESIZABLE, commandLine.split("\\s+"),
a69ed767 119 closeOnExit);
b2d49e0f
KL
120 }
121
6f8ff91a
KL
122 /**
123 * Public constructor spawns a custom command line.
124 *
125 * @param application TApplication that manages this window
126 * @param x column relative to parent
127 * @param y row relative to parent
128 * @param flags mask of CENTERED, MODAL, or RESIZABLE
a0d734e6 129 * @param command the command line to execute
6f8ff91a
KL
130 */
131 public TTerminalWindow(final TApplication application, final int x,
a0d734e6 132 final int y, final int flags, final String [] command) {
6f8ff91a 133
a69ed767
KL
134 this(application, x, y, flags, command,
135 System.getProperty("jexer.TTerminal.closeOnExit",
136 "false").equals("true"));
137 }
138
139 /**
140 * Public constructor spawns a custom command line.
141 *
142 * @param application TApplication that manages this window
143 * @param x column relative to parent
144 * @param y row relative to parent
145 * @param flags mask of CENTERED, MODAL, or RESIZABLE
146 * @param command the command line to execute
147 * @param closeOnExit if true, close the window when the command exits
148 */
149 public TTerminalWindow(final TApplication application, final int x,
150 final int y, final int flags, final String [] command,
151 final boolean closeOnExit) {
152
6f8ff91a
KL
153 super(application, i18n.getString("windowTitle"), x, y,
154 80 + 2, 24 + 2, flags);
155
a69ed767 156 this.closeOnExit = closeOnExit;
4d2c61b4
KL
157 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
158
159 // Claim the keystrokes the emulator will need.
160 addShortcutKeys();
a69ed767 161
4d2c61b4
KL
162 // Add shortcut text
163 newStatusBar(i18n.getString("statusBarRunning"));
6f8ff91a 164
4d2c61b4
KL
165 // Spin it up
166 terminal = new TTerminalWidget(this, 0, 0);
6f8ff91a
KL
167 }
168
169 /**
170 * Public constructor spawns a shell.
171 *
172 * @param application TApplication that manages this window
173 * @param x column relative to parent
174 * @param y row relative to parent
175 * @param flags mask of CENTERED, MODAL, or RESIZABLE
176 */
177 public TTerminalWindow(final TApplication application, final int x,
178 final int y, final int flags) {
179
a69ed767
KL
180 this(application, x, y, flags,
181 System.getProperty("jexer.TTerminal.closeOnExit",
182 "false").equals("true"));
183
184 }
185
186 /**
187 * Public constructor spawns a shell.
188 *
189 * @param application TApplication that manages this window
190 * @param x column relative to parent
191 * @param y row relative to parent
192 * @param flags mask of CENTERED, MODAL, or RESIZABLE
193 * @param closeOnExit if true, close the window when the shell exits
194 */
195 public TTerminalWindow(final TApplication application, final int x,
196 final int y, final int flags, final boolean closeOnExit) {
197
6f8ff91a
KL
198 super(application, i18n.getString("windowTitle"), x, y,
199 80 + 2, 24 + 2, flags);
200
a69ed767 201 this.closeOnExit = closeOnExit;
4d2c61b4 202 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
a69ed767 203
4d2c61b4
KL
204 // Claim the keystrokes the emulator will need.
205 addShortcutKeys();
6f8ff91a 206
4d2c61b4
KL
207 // Add shortcut text
208 newStatusBar(i18n.getString("statusBarRunning"));
6f8ff91a 209
4d2c61b4
KL
210 // Spin it up
211 terminal = new TTerminalWidget(this, 0, 0);
6f8ff91a
KL
212 }
213
615a0d99
KL
214 // ------------------------------------------------------------------------
215 // TScrollableWindow ------------------------------------------------------
216 // ------------------------------------------------------------------------
55d2b2c2 217
34a42e78
KL
218 /**
219 * Draw the display buffer.
220 */
221 @Override
222 public void draw() {
4d2c61b4
KL
223 setTitle(terminal.getTitle());
224 reflowData();
ab215e38 225 super.draw();
107bba16
KL
226 }
227
be72cb5c 228 /**
615a0d99 229 * Handle window/screen resize events.
aa77d682 230 *
615a0d99 231 * @param resize resize event
aa77d682 232 */
615a0d99
KL
233 @Override
234 public void onResize(final TResizeEvent resize) {
4d2c61b4
KL
235 if (resize.getType() == TResizeEvent.Type.WIDGET) {
236 terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
237 getWidth() - 2, getHeight() - 2));
615a0d99 238
4d2c61b4
KL
239 // Resize the scroll bars
240 reflowData();
241 placeScrollbars();
242 }
243 return;
aa77d682
KL
244 }
245
246 /**
615a0d99 247 * Resize scrollbars for a new width/height.
aa77d682 248 */
615a0d99
KL
249 @Override
250 public void reflowData() {
4d2c61b4
KL
251 // Vertical scrollbar
252 terminal.reflowData();
253 setTopValue(terminal.getTopValue());
254 setBottomValue(terminal.getBottomValue());
255 setVerticalBigChange(terminal.getVerticalBigChange());
256 setVerticalValue(terminal.getVerticalValue());
aa77d682
KL
257 }
258
b2d49e0f 259 /**
615a0d99
KL
260 * Handle keystrokes.
261 *
262 * @param keypress keystroke event
34a42e78 263 */
615a0d99
KL
264 @Override
265 public void onKeypress(final TKeypressEvent keypress) {
4d2c61b4
KL
266 if (terminal.isReading()) {
267 terminal.onKeypress(keypress);
268 } else {
269 super.onKeypress(keypress);
34a42e78 270 }
34a42e78
KL
271 }
272
273 /**
274 * Handle mouse press events.
275 *
276 * @param mouse mouse button press event
277 */
278 @Override
279 public void onMouseDown(final TMouseEvent mouse) {
bd8d51fa
KL
280 if (inWindowMove || inWindowResize) {
281 // TWindow needs to deal with this.
282 super.onMouseDown(mouse);
283 return;
284 }
34a42e78 285
34a42e78
KL
286 super.onMouseDown(mouse);
287 }
288
bd8d51fa
KL
289 /**
290 * Handle mouse release events.
291 *
292 * @param mouse mouse button release event
293 */
294 @Override
295 public void onMouseUp(final TMouseEvent mouse) {
296 if (inWindowMove || inWindowResize) {
297 // TWindow needs to deal with this.
298 super.onMouseUp(mouse);
299 return;
300 }
301
bd8d51fa
KL
302 super.onMouseUp(mouse);
303 }
304
305 /**
306 * Handle mouse motion events.
307 *
308 * @param mouse mouse motion event
309 */
310 @Override
311 public void onMouseMotion(final TMouseEvent mouse) {
312 if (inWindowMove || inWindowResize) {
313 // TWindow needs to deal with this.
314 super.onMouseMotion(mouse);
315 return;
316 }
317
bd8d51fa
KL
318 super.onMouseMotion(mouse);
319 }
320
615a0d99
KL
321 // ------------------------------------------------------------------------
322 // TTerminalWindow --------------------------------------------------------
323 // ------------------------------------------------------------------------
324
80b1b7b5
KL
325 /**
326 * Returns true if this window does not want the application-wide mouse
327 * cursor drawn over it.
328 *
329 * @return true if this window does not want the application-wide mouse
330 * cursor drawn over it
331 */
332 @Override
333 public boolean hasHiddenMouse() {
4d2c61b4 334 return terminal.hasHiddenMouse();
80b1b7b5
KL
335 }
336
615a0d99
KL
337 /**
338 * Claim the keystrokes the emulator will need.
339 */
340 private void addShortcutKeys() {
341 addShortcutKeypress(kbCtrlA);
342 addShortcutKeypress(kbCtrlB);
343 addShortcutKeypress(kbCtrlC);
344 addShortcutKeypress(kbCtrlD);
345 addShortcutKeypress(kbCtrlE);
346 addShortcutKeypress(kbCtrlF);
347 addShortcutKeypress(kbCtrlG);
348 addShortcutKeypress(kbCtrlH);
349 addShortcutKeypress(kbCtrlU);
350 addShortcutKeypress(kbCtrlJ);
351 addShortcutKeypress(kbCtrlK);
352 addShortcutKeypress(kbCtrlL);
353 addShortcutKeypress(kbCtrlM);
354 addShortcutKeypress(kbCtrlN);
355 addShortcutKeypress(kbCtrlO);
356 addShortcutKeypress(kbCtrlP);
357 addShortcutKeypress(kbCtrlQ);
358 addShortcutKeypress(kbCtrlR);
359 addShortcutKeypress(kbCtrlS);
360 addShortcutKeypress(kbCtrlT);
361 addShortcutKeypress(kbCtrlU);
362 addShortcutKeypress(kbCtrlV);
363 addShortcutKeypress(kbCtrlW);
364 addShortcutKeypress(kbCtrlX);
365 addShortcutKeypress(kbCtrlY);
366 addShortcutKeypress(kbCtrlZ);
367 addShortcutKeypress(kbF1);
368 addShortcutKeypress(kbF2);
369 addShortcutKeypress(kbF3);
370 addShortcutKeypress(kbF4);
371 addShortcutKeypress(kbF5);
372 addShortcutKeypress(kbF6);
373 addShortcutKeypress(kbF7);
374 addShortcutKeypress(kbF8);
375 addShortcutKeypress(kbF9);
376 addShortcutKeypress(kbF10);
377 addShortcutKeypress(kbF11);
378 addShortcutKeypress(kbF12);
379 addShortcutKeypress(kbAltA);
380 addShortcutKeypress(kbAltB);
381 addShortcutKeypress(kbAltC);
382 addShortcutKeypress(kbAltD);
383 addShortcutKeypress(kbAltE);
384 addShortcutKeypress(kbAltF);
385 addShortcutKeypress(kbAltG);
386 addShortcutKeypress(kbAltH);
387 addShortcutKeypress(kbAltU);
388 addShortcutKeypress(kbAltJ);
389 addShortcutKeypress(kbAltK);
390 addShortcutKeypress(kbAltL);
391 addShortcutKeypress(kbAltM);
392 addShortcutKeypress(kbAltN);
393 addShortcutKeypress(kbAltO);
394 addShortcutKeypress(kbAltP);
395 addShortcutKeypress(kbAltQ);
396 addShortcutKeypress(kbAltR);
397 addShortcutKeypress(kbAltS);
398 addShortcutKeypress(kbAltT);
399 addShortcutKeypress(kbAltU);
400 addShortcutKeypress(kbAltV);
401 addShortcutKeypress(kbAltW);
402 addShortcutKeypress(kbAltX);
403 addShortcutKeypress(kbAltY);
404 addShortcutKeypress(kbAltZ);
405 }
406
615a0d99
KL
407 /**
408 * Hook for subclasses to be notified of the shell termination.
409 */
410 public void onShellExit() {
a69ed767
KL
411 if (closeOnExit) {
412 close();
413 }
4d2c61b4 414 clearShortcutKeypresses();
615a0d99
KL
415 getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
416 }
417
34a42e78 418}