#55 restore default attributes on exit
[nikiroo-utils.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
955c55b7
KL
156 // Require at least one line for the display.
157 setMinimumWindowHeight(3);
158
a69ed767 159 this.closeOnExit = closeOnExit;
4d2c61b4
KL
160 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
161
162 // Claim the keystrokes the emulator will need.
163 addShortcutKeys();
a69ed767 164
4d2c61b4
KL
165 // Add shortcut text
166 newStatusBar(i18n.getString("statusBarRunning"));
6f8ff91a 167
4d2c61b4 168 // Spin it up
2bc32111
KL
169 terminal = new TTerminalWidget(this, 0, 0, new TAction() {
170 public void DO() {
171 onShellExit();
172 }
173 });
6f8ff91a
KL
174 }
175
176 /**
177 * Public constructor spawns a shell.
178 *
179 * @param application TApplication that manages this window
180 * @param x column relative to parent
181 * @param y row relative to parent
182 * @param flags mask of CENTERED, MODAL, or RESIZABLE
183 */
184 public TTerminalWindow(final TApplication application, final int x,
185 final int y, final int flags) {
186
a69ed767
KL
187 this(application, x, y, flags,
188 System.getProperty("jexer.TTerminal.closeOnExit",
189 "false").equals("true"));
190
191 }
192
193 /**
194 * Public constructor spawns a shell.
195 *
196 * @param application TApplication that manages this window
197 * @param x column relative to parent
198 * @param y row relative to parent
199 * @param flags mask of CENTERED, MODAL, or RESIZABLE
200 * @param closeOnExit if true, close the window when the shell exits
201 */
202 public TTerminalWindow(final TApplication application, final int x,
203 final int y, final int flags, final boolean closeOnExit) {
204
6f8ff91a
KL
205 super(application, i18n.getString("windowTitle"), x, y,
206 80 + 2, 24 + 2, flags);
207
955c55b7
KL
208 // Require at least one line for the display.
209 setMinimumWindowHeight(3);
210
a69ed767 211 this.closeOnExit = closeOnExit;
4d2c61b4 212 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
a69ed767 213
4d2c61b4
KL
214 // Claim the keystrokes the emulator will need.
215 addShortcutKeys();
6f8ff91a 216
4d2c61b4
KL
217 // Add shortcut text
218 newStatusBar(i18n.getString("statusBarRunning"));
6f8ff91a 219
4d2c61b4 220 // Spin it up
2bc32111
KL
221 terminal = new TTerminalWidget(this, 0, 0, new TAction() {
222 public void DO() {
223 onShellExit();
224 }
225 });
6f8ff91a
KL
226 }
227
615a0d99
KL
228 // ------------------------------------------------------------------------
229 // TScrollableWindow ------------------------------------------------------
230 // ------------------------------------------------------------------------
55d2b2c2 231
34a42e78
KL
232 /**
233 * Draw the display buffer.
234 */
235 @Override
236 public void draw() {
4d2c61b4
KL
237 setTitle(terminal.getTitle());
238 reflowData();
ab215e38 239 super.draw();
107bba16
KL
240 }
241
be72cb5c 242 /**
615a0d99 243 * Handle window/screen resize events.
aa77d682 244 *
615a0d99 245 * @param resize resize event
aa77d682 246 */
615a0d99
KL
247 @Override
248 public void onResize(final TResizeEvent resize) {
4d2c61b4
KL
249 if (resize.getType() == TResizeEvent.Type.WIDGET) {
250 terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
251 getWidth() - 2, getHeight() - 2));
615a0d99 252
4d2c61b4
KL
253 // Resize the scroll bars
254 reflowData();
255 placeScrollbars();
256 }
257 return;
aa77d682
KL
258 }
259
260 /**
615a0d99 261 * Resize scrollbars for a new width/height.
aa77d682 262 */
615a0d99
KL
263 @Override
264 public void reflowData() {
4d2c61b4
KL
265 // Vertical scrollbar
266 terminal.reflowData();
267 setTopValue(terminal.getTopValue());
268 setBottomValue(terminal.getBottomValue());
269 setVerticalBigChange(terminal.getVerticalBigChange());
270 setVerticalValue(terminal.getVerticalValue());
aa77d682
KL
271 }
272
b2d49e0f 273 /**
615a0d99
KL
274 * Handle keystrokes.
275 *
276 * @param keypress keystroke event
34a42e78 277 */
615a0d99
KL
278 @Override
279 public void onKeypress(final TKeypressEvent keypress) {
4d2c61b4
KL
280 if (terminal.isReading()) {
281 terminal.onKeypress(keypress);
282 } else {
283 super.onKeypress(keypress);
34a42e78 284 }
34a42e78
KL
285 }
286
287 /**
288 * Handle mouse press events.
289 *
290 * @param mouse mouse button press event
291 */
292 @Override
293 public void onMouseDown(final TMouseEvent mouse) {
bd8d51fa
KL
294 if (inWindowMove || inWindowResize) {
295 // TWindow needs to deal with this.
296 super.onMouseDown(mouse);
297 return;
298 }
34a42e78 299
34a42e78
KL
300 super.onMouseDown(mouse);
301 }
302
bd8d51fa
KL
303 /**
304 * Handle mouse release events.
305 *
306 * @param mouse mouse button release event
307 */
308 @Override
309 public void onMouseUp(final TMouseEvent mouse) {
310 if (inWindowMove || inWindowResize) {
311 // TWindow needs to deal with this.
312 super.onMouseUp(mouse);
313 return;
314 }
315
bd8d51fa 316 super.onMouseUp(mouse);
dbf8e80a
KL
317
318 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
319 // Clicked on vertical scrollbar
320 terminal.setVerticalValue(getVerticalValue());
321 }
bd8d51fa
KL
322 }
323
324 /**
325 * Handle mouse motion events.
326 *
327 * @param mouse mouse motion event
328 */
329 @Override
330 public void onMouseMotion(final TMouseEvent mouse) {
331 if (inWindowMove || inWindowResize) {
332 // TWindow needs to deal with this.
333 super.onMouseMotion(mouse);
334 return;
335 }
336
bd8d51fa 337 super.onMouseMotion(mouse);
dbf8e80a
KL
338
339 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
340 // Clicked/dragged on vertical scrollbar
341 terminal.setVerticalValue(getVerticalValue());
342 }
bd8d51fa
KL
343 }
344
615a0d99
KL
345 // ------------------------------------------------------------------------
346 // TTerminalWindow --------------------------------------------------------
347 // ------------------------------------------------------------------------
348
80b1b7b5
KL
349 /**
350 * Returns true if this window does not want the application-wide mouse
351 * cursor drawn over it.
352 *
353 * @return true if this window does not want the application-wide mouse
354 * cursor drawn over it
355 */
356 @Override
357 public boolean hasHiddenMouse() {
4d2c61b4 358 return terminal.hasHiddenMouse();
80b1b7b5
KL
359 }
360
615a0d99
KL
361 /**
362 * Claim the keystrokes the emulator will need.
363 */
364 private void addShortcutKeys() {
365 addShortcutKeypress(kbCtrlA);
366 addShortcutKeypress(kbCtrlB);
367 addShortcutKeypress(kbCtrlC);
368 addShortcutKeypress(kbCtrlD);
369 addShortcutKeypress(kbCtrlE);
370 addShortcutKeypress(kbCtrlF);
371 addShortcutKeypress(kbCtrlG);
372 addShortcutKeypress(kbCtrlH);
373 addShortcutKeypress(kbCtrlU);
374 addShortcutKeypress(kbCtrlJ);
375 addShortcutKeypress(kbCtrlK);
376 addShortcutKeypress(kbCtrlL);
377 addShortcutKeypress(kbCtrlM);
378 addShortcutKeypress(kbCtrlN);
379 addShortcutKeypress(kbCtrlO);
380 addShortcutKeypress(kbCtrlP);
381 addShortcutKeypress(kbCtrlQ);
382 addShortcutKeypress(kbCtrlR);
383 addShortcutKeypress(kbCtrlS);
384 addShortcutKeypress(kbCtrlT);
385 addShortcutKeypress(kbCtrlU);
386 addShortcutKeypress(kbCtrlV);
387 addShortcutKeypress(kbCtrlW);
388 addShortcutKeypress(kbCtrlX);
389 addShortcutKeypress(kbCtrlY);
390 addShortcutKeypress(kbCtrlZ);
391 addShortcutKeypress(kbF1);
392 addShortcutKeypress(kbF2);
393 addShortcutKeypress(kbF3);
394 addShortcutKeypress(kbF4);
395 addShortcutKeypress(kbF5);
396 addShortcutKeypress(kbF6);
397 addShortcutKeypress(kbF7);
398 addShortcutKeypress(kbF8);
399 addShortcutKeypress(kbF9);
400 addShortcutKeypress(kbF10);
401 addShortcutKeypress(kbF11);
402 addShortcutKeypress(kbF12);
403 addShortcutKeypress(kbAltA);
404 addShortcutKeypress(kbAltB);
405 addShortcutKeypress(kbAltC);
406 addShortcutKeypress(kbAltD);
407 addShortcutKeypress(kbAltE);
408 addShortcutKeypress(kbAltF);
409 addShortcutKeypress(kbAltG);
410 addShortcutKeypress(kbAltH);
411 addShortcutKeypress(kbAltU);
412 addShortcutKeypress(kbAltJ);
413 addShortcutKeypress(kbAltK);
414 addShortcutKeypress(kbAltL);
415 addShortcutKeypress(kbAltM);
416 addShortcutKeypress(kbAltN);
417 addShortcutKeypress(kbAltO);
418 addShortcutKeypress(kbAltP);
419 addShortcutKeypress(kbAltQ);
420 addShortcutKeypress(kbAltR);
421 addShortcutKeypress(kbAltS);
422 addShortcutKeypress(kbAltT);
423 addShortcutKeypress(kbAltU);
424 addShortcutKeypress(kbAltV);
425 addShortcutKeypress(kbAltW);
426 addShortcutKeypress(kbAltX);
427 addShortcutKeypress(kbAltY);
428 addShortcutKeypress(kbAltZ);
429 }
430
615a0d99
KL
431 /**
432 * Hook for subclasses to be notified of the shell termination.
433 */
434 public void onShellExit() {
a69ed767
KL
435 if (closeOnExit) {
436 close();
437 }
4d2c61b4 438 clearShortcutKeypresses();
615a0d99
KL
439 getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
440 }
441
34a42e78 442}